Flask-cli.py

from __future__ import annotations

import ast

import collections.abc as cabc

import importlib.metadata

import inspect

import os

import platform

import re

import sys

import traceback

import typing as t

from functools import update_wrapper

from operator import itemgetter

from types import ModuleType

import click

from click.core import ParameterSource

from werkzeug import run_simple

from werkzeug.serving import is_running_from_reloader

from werkzeug.utils import import_string

from .globals import current_app

from .helpers import get_debug_flag

from .helpers import get_load_dotenv

if t.TYPE_CHECKING:

    import ssl

    from _typeshed.wsgi import StartResponse

    from _typeshed.wsgi import WSGIApplication

    from _typeshed.wsgi import WSGIEnvironment

    from .app import Flask


 

class NoAppException(click.UsageError):

    """Raised if an application cannot be found or loaded."""


 

def find_best_app(module: ModuleType) -> Flask:

    """Given a module instance this tries to find the best possible

    application in the module or raises an exception.

    """

    from . import Flask

    # Search for the most common names first.

    for attr_name in ("app", "application"):

        app = getattr(module, attr_name, None)

        if isinstance(app, Flask):

            return app

    # Otherwise find the only object that is a Flask instance.

    matches = [v for v in module.__dict__.values() if isinstance(v, Flask)]

    if len(matches) == 1:

        return matches[0]

    elif len(matches) > 1:

        raise NoAppException(

            "Detected multiple Flask applications in module"

            f" '{module.__name__}'. Use '{module.__name__}:name'"

            " to specify the correct one."

        )

    # Search for app factory functions.

    for attr_name in ("create_app", "make_app"):

        app_factory = getattr(module, attr_name, None)

        if inspect.isfunction(app_factory):

            try:

                app = app_factory()

                if isinstance(app, Flask):

                    return app

            except TypeError as e:

                if not _called_with_wrong_args(app_factory):

                    raise

                raise NoAppException(

                    f"Detected factory '{attr_name}' in module '{module.__name__}',"

                    " but could not call it without arguments. Use"

                    f" '{module.__name__}:{attr_name}(args)'"

                    " to specify arguments."

                ) from e

    raise NoAppException(

        "Failed to find Flask application or factory in module"

        f" '{module.__name__}'. Use '{module.__name__}:name'"

        " to specify one."

    )

这段代码是一个Flask应用程序的辅助功能的一部分。它包含了寻找最佳应用程序实例的函数find_best_app,并定义了一个名为NoAppException的异常类,用于在无法找到或加载应用程序时引发异常。

find_best_app函数接受一个模块实例作为参数,并尝试在该模块中找到最适合的应用程序实例。它首先搜索常见的应用程序命名约定(例如"app"或"application"),然后查找模块中的Flask实例对象。如果找到多个匹配项,则引发NoAppException异常。如果没有找到合适的应用程序实例,则尝试查找应用程序工厂函数(例如"create_app"或"make_app")并调用该函数来获取应用程序实例。如果这些尝试都失败,则会引发NoAppException异常。

def _called_with_wrong_args(f: t.Callable[..., Flask]) -> bool:

    """Check whether calling a function raised a ``TypeError`` because

    the call failed or because something in the factory raised the

    error.

    :param f: The function that was called.

    :return: ``True`` if the call failed.

    """

    tb = sys.exc_info()[2]

    try:

        while tb is not None:

            if tb.tb_frame.f_code is f.__code__:

                # In the function, it was called successfully.

                return False

            tb = tb.tb_next

        # Didn't reach the function.

        return True

    finally:

        # Delete tb to break a circular reference.

        # https://docs.python.org/2/library/sys.html#sys.exc_info

        del tb


 

def find_app_by_string(module: ModuleType, app_name: str) -> Flask:

    """Check if the given string is a variable name or a function. Call

    a function to get the app instance, or return the variable directly.

    """

    from . import Flask

    # Parse app_name as a single expression to determine if it's a valid

    # attribute name or function call.

    try:

        expr = ast.parse(app_name.strip(), mode="eval").body

    except SyntaxError:

        raise NoAppException(

            f"Failed to parse {app_name!r} as an attribute name or function call."

        ) from None

    if isinstance(expr, ast.Name):

        name = expr.id

        args = []

        kwargs = {}

    elif isinstance(expr, ast.Call):

        # Ensure the function name is an attribute name only.

        if not isinstance(expr.func, ast.Name):

            raise NoAppException(

                f"Function reference must be a simple name: {app_name!r}."

            )

        name = expr.func.id

        # Parse the positional and keyword arguments as literals.

        try:

            args = [ast.literal_eval(arg) for arg in expr.args]

            kwargs = {

                kw.arg: ast.literal_eval(kw.value)

                for kw in expr.keywords

                if kw.arg is not None

            }

        except ValueError:

            # literal_eval gives cryptic error messages, show a generic

            # message with the full expression instead.

            raise NoAppException(

                f"Failed to parse arguments as literal values: {app_name!r}."

            ) from None

    else:

        raise NoAppException(

            f"Failed to parse {app_name!r} as an attribute name or function call."

        )

    try:

        attr = getattr(module, name)

    except AttributeError as e:

        raise NoAppException(

            f"Failed to find attribute {name!r} in {module.__name__!r}."

        ) from e

    # If the attribute is a function, call it with any args and kwargs

    # to get the real application.

    if inspect.isfunction(attr):

        try:

            app = attr(*args, **kwargs)

        except TypeError as e:

            if not _called_with_wrong_args(attr):

                raise

            raise NoAppException(

                f"The factory {app_name!r} in module"

                f" {module.__name__!r} could not be called with the"

                " specified arguments."

            ) from e

    else:

        app = attr

    if isinstance(app, Flask):

        return app

    raise NoAppException(

        "A valid Flask application was not obtained from"

        f" '{module.__name__}:{app_name}'."

    )

这段代码包括两个函数:_called_with_wrong_args 和 find_app_by_string_called_with_wrong_args 函数的目的是检查函数调用是由于函数本身的问题导致的 TypeError 错误,还是由于工厂函数内部引发的错误。它通过检查函数调用的堆栈信息来判断函数是否成功调用。如果堆栈信息表明函数未成功调用,则返回 True。这样的设计可用于在函数调用出现问题的情况下执行其他操作,或者用于记录函数调用失败的情况。

find_app_by_string 函数的作用是通过字符串检查是否为变量名或函数,并根据情况调用函数来获取应用实例,或直接返回变量。它首先尝试解析给定的字符串为表达式,以确定其是否为有效的属性名或函数调用。然后,根据解析出来的内容,尝试从模块中获取属性,并检查属性类型,如果是函数则尝试调用该函数,并返回实际的应用实例。

def prepare_import(path: str) -> str:

    """Given a filename this will try to calculate the python path, add it

    to the search path and return the actual module name that is expected.

    """

    path = os.path.realpath(path)

    fname, ext = os.path.splitext(path)

    if ext == ".py":

        path = fname

    if os.path.basename(path) == "__init__":

        path = os.path.dirname(path)

    module_name = []

    # move up until outside package structure (no __init__.py)

    while True:

        path, name = os.path.split(path)

        module_name.append(name)

        if not os.path.exists(os.path.join(path, "__init__.py")):

            break

    if sys.path[0] != path:

        sys.path.insert(0, path)

    return ".".join(module_name[::-1])


 

@t.overload

def locate_app(

    module_name: str, app_name: str | None, raise_if_not_found: t.Literal[True] = True

) -> Flask: ...


 

@t.overload

def locate_app(

    module_name: str, app_name: str | None, raise_if_not_found: t.Literal[False] = ...

) -> Flask | None: ...


 

def locate_app(

    module_name: str, app_name: str | None, raise_if_not_found: bool = True

) -> Flask | None:

    try:

        __import__(module_name)

    except ImportError:

        # Reraise the ImportError if it occurred within the imported module.

        # Determine this by checking whether the trace has a depth > 1.

        if sys.exc_info()[2].tb_next:  # type: ignore[union-attr]

            raise NoAppException(

                f"While importing {module_name!r}, an ImportError was"

                f" raised:\n\n{traceback.format_exc()}"

            ) from None

        elif raise_if_not_found:

            raise NoAppException(f"Could not import {module_name!r}.") from None

        else:

            return None

    module = sys.modules[module_name]

    if app_name is None:

        return find_best_app(module)

    else:

        return find_app_by_string(module, app_name)

这段代码中的prepare_import函数用于根据给定的文件名计算出相应的Python路径,并将其添加到搜索路径中,然后返回预期的实际模块名称。该函数对应下面的locate_app函数,后者用于导入指定的模块,并根据模块名和应用名来定位应用程序

prepare_import函数:
输入:给定的文件名path。
功能:计算出Python路径并将其添加到搜索路径中,然后返回实际的模块名称。
过程:使用os.path.realpath获取给定路径的真实路径。使用os.path.splitext获取路径的文件名和扩展名,如果扩展名是".py",则将路径更新为文件名。
如果路径的基本名称是"init",则将路径更新为其目录名称。从给定路径一直向上移动,直到找到模块的根目录(即没有__init__.py文件的目录)。在此过程中,记录每一级的名称。将计算出的模块路径添加到Python搜索路径(sys.path)的最前面,返回计算出的模块路径。

locate_app函数:
输入:模块名称module_name、应用名称app_name和raise_if_not_found标志。
功能:根据模块名和应用名导入应用程序,并定位应用程序。
过程:尝试导入指定的模块,如果导入失败,则根据raise_if_not_found标志来决定是引发NoAppException异常还是返回None。如果成功导入模块,则根据应用名来定位应用程序,如果应用名为None,则调用find_best_app函数找到最佳应用程序,否则调用find_app_by_string函数根据应用名字符串来定位应用程序,最终返回定位到的应用程序。

def get_version(ctx: click.Context, param: click.Parameter, value: t.Any) -> None:

    if not value or ctx.resilient_parsing:

        return

    flask_version = importlib.metadata.version("flask")

    werkzeug_version = importlib.metadata.version("werkzeug")

    click.echo(

        f"Python {platform.python_version()}\n"

        f"Flask {flask_version}\n"

        f"Werkzeug {werkzeug_version}",

        color=ctx.color,

    )

    ctx.exit()


 

version_option = click.Option(

    ["--version"],

    help="Show the Flask version.",

    expose_value=False,

    callback=get_version,

    is_flag=True,

    is_eager=True,

)

这段代码是使用Python的Click库来创建一个用于显示Flask版本的命令行选项。这个命令行选项可以被附加到任何Click命令中,当用户指定--version选项时,将会显示Flask和Werkzeug的版本信息。

首先定义了一个名为get_version的函数,它接受click的上下文对象(ctx)、参数对象(param)和值(value)作为参数。在这个函数中,它使用importlib库来获取Flask和Werkzeug的版本信息,并通过click库的echo方法来输出这些信息。最后,使用ctx对象的exit方法来表示命令成功完成。

然后,创建了一个名为version_option的click.Option对象,这个对象代表了--version命令行选项。它具有以下特点:

  • ["--version"]:定义了--version选项的名称
  • help="Show the Flask version.":作为帮助文本,当用户运行命令时,此文本会显示帮助信息
  • expose_value=False:表示不需要将选项的值暴露给命令的回调函数
  • callback=get_version:指定了在选项被触发时要调用的回调函数,这里指定了上面定义的get_version函数
  • is_flag=True:指定了该选项是一个标志选项,即不需要跟一个具体的值
  • is_eager=True:表示在执行命令时立即处理该选项,而不是等待到命令执行后再处理

通过这些定义,当用户在命令行中指定--version选项时,就会触发get_version函数,打印出Python、Flask和Werkzeug的版本信息。

class ScriptInfo:

    """Helper object to deal with Flask applications.  This is usually not

    necessary to interface with as it's used internally in the dispatching

    to click.  In future versions of Flask this object will most likely play

    a bigger role.  Typically it's created automatically by the

    :class:`FlaskGroup` but you can also manually create it and pass it

    onwards as click object.

    """

    def __init__(

        self,

        app_import_path: str | None = None,

        create_app: t.Callable[..., Flask] | None = None,

        set_debug_flag: bool = True,

    ) -> None:

        #: Optionally the import path for the Flask application.

        self.app_import_path = app_import_path

        #: Optionally a function that is passed the script info to create

        #: the instance of the application.

        self.create_app = create_app

        #: A dictionary with arbitrary data that can be associated with

        #: this script info.

        self.data: dict[t.Any, t.Any] = {}

        self.set_debug_flag = set_debug_flag

        self._loaded_app: Flask | None = None

    def load_app(self) -> Flask:

        """Loads the Flask app (if not yet loaded) and returns it.  Calling

        this multiple times will just result in the already loaded app to

        be returned.

        """

        if self._loaded_app is not None:

            return self._loaded_app

        if self.create_app is not None:

            app: Flask | None = self.create_app()

        else:

            if self.app_import_path:

                path, name = (

                    re.split(r":(?![\\/])", self.app_import_path, maxsplit=1) + [None]

                )[:2]

                import_name = prepare_import(path)

                app = locate_app(import_name, name)

            else:

                for path in ("wsgi.py", "app.py"):

                    import_name = prepare_import(path)

                    app = locate_app(import_name, None, raise_if_not_found=False)

                    if app is not None:

                        break

        if app is None:

            raise NoAppException(

                "Could not locate a Flask application. Use the"

                " 'flask --app' option, 'FLASK_APP' environment"

                " variable, or a 'wsgi.py' or 'app.py' file in the"

                " current directory."

            )

        if self.set_debug_flag:

            # Update the app's debug flag through the descriptor so that

            # other values repopulate as well.

            app.debug = get_debug_flag()

        self._loaded_app = app

        return app

这段代码定义ScriptInfo的类,它用于处理Flask应用程序的辅助对象。该类包含以下主要功能:

  • ScriptInfo类具有一个构造函数,其中包含app_import_pathcreate_appset_debug_flag等参数。这些参数用于配置Flask应用程序的导入路径、创建应用程序的函数以及设置调试标志。
  • load_app方法用于加载Flask应用程序。它首先检查是否已经加载了应用程序,如果没有,则使用自定义的create_app函数创建应用程序,或者通过app_import_path自动查找应用程序并进行加载。如果无法找到应用程序,则会引发NoAppException异常。
  • 如果设置了set_debug_flag,则会通过描述符来更新应用程序的调试标志,以确保其他相关值也得到重新填充。

pass_script_info = click.make_pass_decorator(ScriptInfo, ensure=True)

F = t.TypeVar("F", bound=t.Callable[..., t.Any])


 

def with_appcontext(f: F) -> F:

    """Wraps a callback so that it's guaranteed to be executed with the

    script's application context.

    Custom commands (and their options) registered under ``app.cli`` or

    ``blueprint.cli`` will always have an app context available, this

    decorator is not required in that case.

    .. versionchanged:: 2.2

        The app context is active for subcommands as well as the

        decorated callback. The app context is always available to

        ``app.cli`` command and parameter callbacks.

    """

    @click.pass_context

    def decorator(ctx: click.Context, /, *args: t.Any, **kwargs: t.Any) -> t.Any:

        if not current_app:

            app = ctx.ensure_object(ScriptInfo).load_app()

            ctx.with_resource(app.app_context())

        return ctx.invoke(f, *args, **kwargs)

    return update_wrapper(decorator, f)  # type: ignore[return-value]


 

class AppGroup(click.Group):

    """This works similar to a regular click :class:`~click.Group` but it

    changes the behavior of the :meth:`command` decorator so that it

    automatically wraps the functions in :func:`with_appcontext`.

    Not to be confused with :class:`FlaskGroup`.

    """

    def command(  # type: ignore[override]

        self, *args: t.Any, **kwargs: t.Any

    ) -> t.Callable[[t.Callable[..., t.Any]], click.Command]:

        """This works exactly like the method of the same name on a regular

        :class:`click.Group` but it wraps callbacks in :func:`with_appcontext`

        unless it's disabled by passing ``with_appcontext=False``.

        """

        wrap_for_ctx = kwargs.pop("with_appcontext", True)

        def decorator(f: t.Callable[..., t.Any]) -> click.Command:

            if wrap_for_ctx:

                f = with_appcontext(f)

            return super(AppGroup, self).command(*args, **kwargs)(f)  # type: ignore[no-any-return]

        return decorator

    def group(  # type: ignore[override]

        self, *args: t.Any, **kwargs: t.Any

    ) -> t.Callable[[t.Callable[..., t.Any]], click.Group]:

        """This works exactly like the method of the same name on a regular

        :class:`click.Group` but it defaults the group class to

        :class:`AppGroup`.

        """

        kwargs.setdefault("cls", AppGroup)

        return super().group(*args, **kwargs)  # type: ignore[no-any-return]


 

def _set_app(ctx: click.Context, param: click.Option, value: str | None) -> str | None:

    if value is None:

        return None

    info = ctx.ensure_object(ScriptInfo)

    info.app_import_path = value

    return value


 

# This option is eager so the app will be available if --help is given.

# --help is also eager, so --app must be before it in the param list.

# no_args_is_help bypasses eager processing, so this option must be

# processed manually in that case to ensure FLASK_APP gets picked up.

_app_option = click.Option(

    ["-A", "--app"],

    metavar="IMPORT",

    help=(

        "The Flask application or factory function to load, in the form 'module:name'."

        " Module can be a dotted import or file path. Name is not required if it is"

        " 'app', 'application', 'create_app', or 'make_app', and can be 'name(args)' to"

        " pass arguments."

    ),

    is_eager=True,

    expose_value=False,

    callback=_set_app,

)


 

def _set_debug(ctx: click.Context, param: click.Option, value: bool) -> bool | None:

    # If the flag isn't provided, it will default to False. Don't use

    # that, let debug be set by env in that case.

    source = ctx.get_parameter_source(param.name)  # type: ignore[arg-type]

    if source is not None and source in (

        ParameterSource.DEFAULT,

        ParameterSource.DEFAULT_MAP,

    ):

        return None

    # Set with env var instead of ScriptInfo.load so that it can be

    # accessed early during a factory function.

    os.environ["FLASK_DEBUG"] = "1" if value else "0"

    return value


 

_debug_option = click.Option(

    ["--debug/--no-debug"],

    help="Set debug mode.",

    expose_value=False,

    callback=_set_debug,

)


 

def _env_file_callback(

    ctx: click.Context, param: click.Option, value: str | None

) -> str | None:

    if value is None:

        return None

    import importlib

    try:

        importlib.import_module("dotenv")

    except ImportError:

        raise click.BadParameter(

            "python-dotenv must be installed to load an env file.",

            ctx=ctx,

            param=param,

        ) from None

    # Don't check FLASK_SKIP_DOTENV, that only disables automatically

    # loading .env and .flaskenv files.

    load_dotenv(value)

    return value

这段代码是为了创建一个用于管理Flask应用的命令行实用程序。

  1. pass_script_info是一个装饰器,它通过make_pass_decorator函数创建,确保上下文的ScriptInfo对象能够在命令回调中可用。

  2. with_appcontext是一个装饰器,保证回调使用应用程序的上下文执行。通过click.pass_context装饰器访问上下文,并在需要时加载应用程序上下文。这样做可以确保在回调中可以使用当前应用程序的上下文。

  3. AppGroup是一个自定义的click.Group子类,它具有与标准click.Group相似的行为,但它修改了command装饰器的行为,以自动将函数包装在with_appcontext装饰器中。这确保了注册的命令在执行时始终处于应用程序的上下文中。

  4. _set_app是一个用于处理--app选项的回调函数。它将app_import_path设置为ScriptInfo对象的属性,这样就可以在应用程序中使用FLASK_APP环境变量或--app命令行选项指定的应用程序。

  5. _set_debug是一个用于处理--debug--no-debug选项的回调函数。它根据选项值设置FLASK_DEBUG环境变量,并返回布尔值表示是否启用调试模式。

  6. _env_file_callback是处理--env-file选项的回调函数。它使用python-dotenv库加载环境文件,并返回文件名。

# This option is eager so env vars are loaded as early as possible to be

# used by other options.

_env_file_option = click.Option(

    ["-e", "--env-file"],

    type=click.Path(exists=True, dir_okay=False),

    help="Load environment variables from this file. python-dotenv must be installed.",

    is_eager=True,

    expose_value=False,

    callback=_env_file_callback,

)


 

class FlaskGroup(AppGroup):

    """Special subclass of the :class:`AppGroup` group that supports

    loading more commands from the configured Flask app.  Normally a

    developer does not have to interface with this class but there are

    some very advanced use cases for which it makes sense to create an

    instance of this. see :ref:`custom-scripts`.

    :param add_default_commands: if this is True then the default run and

        shell commands will be added.

    :param add_version_option: adds the ``--version`` option.

    :param create_app: an optional callback that is passed the script info and

        returns the loaded app.

    :param load_dotenv: Load the nearest :file:`.env` and :file:`.flaskenv`

        files to set environment variables. Will also change the working

        directory to the directory containing the first file found.

    :param set_debug_flag: Set the app's debug flag.

    .. versionchanged:: 2.2

        Added the ``-A/--app``, ``--debug/--no-debug``, ``-e/--env-file`` options.

    .. versionchanged:: 2.2

        An app context is pushed when running ``app.cli`` commands, so

        ``@with_appcontext`` is no longer required for those commands.

    .. versionchanged:: 1.0

        If installed, python-dotenv will be used to load environment variables

        from :file:`.env` and :file:`.flaskenv` files.

    """

    def __init__(

        self,

        add_default_commands: bool = True,

        create_app: t.Callable[..., Flask] | None = None,

        add_version_option: bool = True,

        load_dotenv: bool = True,

        set_debug_flag: bool = True,

        **extra: t.Any,

    ) -> None:

        params = list(extra.pop("params", None) or ())

        # Processing is done with option callbacks instead of a group

        # callback. This allows users to make a custom group callback

        # without losing the behavior. --env-file must come first so

        # that it is eagerly evaluated before --app.

        params.extend((_env_file_option, _app_option, _debug_option))

        if add_version_option:

            params.append(version_option)

        if "context_settings" not in extra:

            extra["context_settings"] = {}

        extra["context_settings"].setdefault("auto_envvar_prefix", "FLASK")

        super().__init__(params=params, **extra)

        self.create_app = create_app

        self.load_dotenv = load_dotenv

        self.set_debug_flag = set_debug_flag

        if add_default_commands:

            self.add_command(run_command)

            self.add_command(shell_command)

            self.add_command(routes_command)

        self._loaded_plugin_commands = False

    def _load_plugin_commands(self) -> None:

        if self._loaded_plugin_commands:

            return

        if sys.version_info >= (3, 10):

            from importlib import metadata

        else:

            # Use a backport on Python < 3.10. We technically have

            # importlib.metadata on 3.8+, but the API changed in 3.10,

            # so use the backport for consistency.

            import importlib_metadata as metadata

        for ep in metadata.entry_points(group="flask.commands"):

            self.add_command(ep.load(), ep.name)

        self._loaded_plugin_commands = True

    def get_command(self, ctx: click.Context, name: str) -> click.Command | None:

        self._load_plugin_commands()

        # Look up built-in and plugin commands, which should be

        # available even if the app fails to load.

        rv = super().get_command(ctx, name)

        if rv is not None:

            return rv

        info = ctx.ensure_object(ScriptInfo)

        # Look up commands provided by the app, showing an error and

        # continuing if the app couldn't be loaded.

        try:

            app = info.load_app()

        except NoAppException as e:

            click.secho(f"Error: {e.format_message()}\n", err=True, fg="red")

            return None

        # Push an app context for the loaded app unless it is already

        # active somehow. This makes the context available to parameter

        # and command callbacks without needing @with_appcontext.

        if not current_app or current_app._get_current_object() is not app:  # type: ignore[attr-defined]

            ctx.with_resource(app.app_context())

        return app.cli.get_command(ctx, name)

    def list_commands(self, ctx: click.Context) -> list[str]:

        self._load_plugin_commands()

        # Start with the built-in and plugin commands.

        rv = set(super().list_commands(ctx))

        info = ctx.ensure_object(ScriptInfo)

        # Add commands provided by the app, showing an error and

        # continuing if the app couldn't be loaded.

        try:

            rv.update(info.load_app().cli.list_commands(ctx))

        except NoAppException as e:

            # When an app couldn't be loaded, show the error message

            # without the traceback.

            click.secho(f"Error: {e.format_message()}\n", err=True, fg="red")

        except Exception:

            # When any other errors occurred during loading, show the

            # full traceback.

            click.secho(f"{traceback.format_exc()}\n", err=True, fg="red")

        return sorted(rv)

    def make_context(

        self,

        info_name: str | None,

        args: list[str],

        parent: click.Context | None = None,

        **extra: t.Any,

    ) -> click.Context:

        # Set a flag to tell app.run to become a no-op. If app.run was

        # not in a __name__ == __main__ guard, it would start the server

        # when importing, blocking whatever command is being called.

        os.environ["FLASK_RUN_FROM_CLI"] = "true"

        # Attempt to load .env and .flask env files. The --env-file

        # option can cause another file to be loaded.

        if get_load_dotenv(self.load_dotenv):

            load_dotenv()

        if "obj" not in extra and "obj" not in self.context_settings:

            extra["obj"] = ScriptInfo(

                create_app=self.create_app, set_debug_flag=self.set_debug_flag

            )

        return super().make_context(info_name, args, parent=parent, **extra)

    def parse_args(self, ctx: click.Context, args: list[str]) -> list[str]:

        if not args and self.no_args_is_help:

            # Attempt to load --env-file and --app early in case they

            # were given as env vars. Otherwise no_args_is_help will not

            # see commands from app.cli.

            _env_file_option.handle_parse_result(ctx, {}, [])

            _app_option.handle_parse_result(ctx, {}, [])

        return super().parse_args(ctx, args)


 

def _path_is_ancestor(path: str, other: str) -> bool:

    """Take ``other`` and remove the length of ``path`` from it. Then join it

    to ``path``. If it is the original value, ``path`` is an ancestor of

    ``other``."""

    return os.path.join(path, other[len(path) :].lstrip(os.sep)) == other


 

def load_dotenv(path: str | os.PathLike[str] | None = None) -> bool:

    """Load "dotenv" files in order of precedence to set environment variables.

    If an env var is already set it is not overwritten, so earlier files in the

    list are preferred over later files.

    This is a no-op if `python-dotenv`_ is not installed.

    .. _python-dotenv: https://github.com/theskumar/python-dotenv#readme

    :param path: Load the file at this location instead of searching.

    :return: ``True`` if a file was loaded.

    .. versionchanged:: 2.0

        The current directory is not changed to the location of the

        loaded file.

    .. versionchanged:: 2.0

        When loading the env files, set the default encoding to UTF-8.

    .. versionchanged:: 1.1.0

        Returns ``False`` when python-dotenv is not installed, or when

        the given path isn't a file.

    .. versionadded:: 1.0

    """

    try:

        import dotenv

    except ImportError:

        if path or os.path.isfile(".env") or os.path.isfile(".flaskenv"):

            click.secho(

                " * Tip: There are .env or .flaskenv files present."

                ' Do "pip install python-dotenv" to use them.',

                fg="yellow",

                err=True,

            )

        return False

    # Always return after attempting to load a given path, don't load

    # the default files.

    if path is not None:

        if os.path.isfile(path):

            return dotenv.load_dotenv(path, encoding="utf-8")

        return False

    loaded = False

    for name in (".env", ".flaskenv"):

        path = dotenv.find_dotenv(name, usecwd=True)

        if not path:

            continue

        dotenv.load_dotenv(path, encoding="utf-8")

        loaded = True

    return loaded  # True if at least one file was located and loaded.


 

def show_server_banner(debug: bool, app_import_path: str | None) -> None:

    """Show extra startup messages the first time the server is run,

    ignoring the reloader.

    """

    if is_running_from_reloader():

        return

    if app_import_path is not None:

        click.echo(f" * Serving Flask app '{app_import_path}'")

    if debug is not None:

        click.echo(f" * Debug mode: {'on' if debug else 'off'}")


 

class CertParamType(click.ParamType):

    """Click option type for the ``--cert`` option. Allows either an

    existing file, the string ``'adhoc'``, or an import for a

    :class:`~ssl.SSLContext` object.

    """

    name = "path"

    def __init__(self) -> None:

        self.path_type = click.Path(exists=True, dir_okay=False, resolve_path=True)

    def convert(

        self, value: t.Any, param: click.Parameter | None, ctx: click.Context | None

    ) -> t.Any:

        try:

            import ssl

        except ImportError:

            raise click.BadParameter(

                'Using "--cert" requires Python to be compiled with SSL support.',

                ctx,

                param,

            ) from None

        try:

            return self.path_type(value, param, ctx)

        except click.BadParameter:

            value = click.STRING(value, param, ctx).lower()

            if value == "adhoc":

                try:

                    import cryptography  # noqa: F401

                except ImportError:

                    raise click.BadParameter(

                        "Using ad-hoc certificates requires the cryptography library.",

                        ctx,

                        param,

                    ) from None

                return value

            obj = import_string(value, silent=True)

            if isinstance(obj, ssl.SSLContext):

                return obj

            raise


 

def _validate_key(ctx: click.Context, param: click.Parameter, value: t.Any) -> t.Any:

    """The ``--key`` option must be specified when ``--cert`` is a file.

    Modifies the ``cert`` param to be a ``(cert, key)`` pair if needed.

    """

    cert = ctx.params.get("cert")

    is_adhoc = cert == "adhoc"

    try:

        import ssl

    except ImportError:

        is_context = False

    else:

        is_context = isinstance(cert, ssl.SSLContext)

    if value is not None:

        if is_adhoc:

            raise click.BadParameter(

                'When "--cert" is "adhoc", "--key" is not used.', ctx, param

            )

        if is_context:

            raise click.BadParameter(

                'When "--cert" is an SSLContext object, "--key" is not used.',

                ctx,

                param,

            )

        if not cert:

            raise click.BadParameter('"--cert" must also be specified.', ctx, param)

        ctx.params["cert"] = cert, value

    else:

        if cert and not (is_adhoc or is_context):

            raise click.BadParameter('Required when using "--cert".', ctx, param)

    return value


 

class SeparatedPathType(click.Path):

    """Click option type that accepts a list of values separated by the

    OS's path separator (``:``, ``;`` on Windows). Each value is

    validated as a :class:`click.Path` type.

    """

    def convert(

        self, value: t.Any, param: click.Parameter | None, ctx: click.Context | None

    ) -> t.Any:

        items = self.split_envvar_value(value)

        # can't call no-arg super() inside list comprehension until Python 3.12

        super_convert = super().convert

        return [super_convert(item, param, ctx) for item in items]


 

@click.command("run", short_help="Run a development server.")

@click.option("--host", "-h", default="127.0.0.1", help="The interface to bind to.")

@click.option("--port", "-p", default=5000, help="The port to bind to.")

@click.option(

    "--cert",

    type=CertParamType(),

    help="Specify a certificate file to use HTTPS.",

    is_eager=True,

)

@click.option(

    "--key",

    type=click.Path(exists=True, dir_okay=False, resolve_path=True),

    callback=_validate_key,

    expose_value=False,

    help="The key file to use when specifying a certificate.",

)

@click.option(

    "--reload/--no-reload",

    default=None,

    help="Enable or disable the reloader. By default the reloader "

    "is active if debug is enabled.",

)

@click.option(

    "--debugger/--no-debugger",

    default=None,

    help="Enable or disable the debugger. By default the debugger "

    "is active if debug is enabled.",

)

@click.option(

    "--with-threads/--without-threads",

    default=True,

    help="Enable or disable multithreading.",

)

@click.option(

    "--extra-files",

    default=None,

    type=SeparatedPathType(),

    help=(

        "Extra files that trigger a reload on change. Multiple paths"

        f" are separated by {os.path.pathsep!r}."

    ),

)

@click.option(

    "--exclude-patterns",

    default=None,

    type=SeparatedPathType(),

    help=(

        "Files matching these fnmatch patterns will not trigger a reload"

        " on change. Multiple patterns are separated by"

        f" {os.path.pathsep!r}."

    ),

)

@pass_script_info

def run_command(

    info: ScriptInfo,

    host: str,

    port: int,

    reload: bool,

    debugger: bool,

    with_threads: bool,

    cert: ssl.SSLContext | tuple[str, str | None] | t.Literal["adhoc"] | None,

    extra_files: list[str] | None,

    exclude_patterns: list[str] | None,

) -> None:

    """Run a local development server.

    This server is for development purposes only. It does not provide

    the stability, security, or performance of production WSGI servers.

    The reloader and debugger are enabled by default with the '--debug'

    option.

    """

    try:

        app: WSGIApplication = info.load_app()

    except Exception as e:

        if is_running_from_reloader():

            # When reloading, print out the error immediately, but raise

            # it later so the debugger or server can handle it.

            traceback.print_exc()

            err = e

            def app(

                environ: WSGIEnvironment, start_response: StartResponse

            ) -> cabc.Iterable[bytes]:

                raise err from None

        else:

            # When not reloading, raise the error immediately so the

            # command fails.

            raise e from None

    debug = get_debug_flag()

    if reload is None:

        reload = debug

    if debugger is None:

        debugger = debug

    show_server_banner(debug, info.app_import_path)

    run_simple(

        host,

        port,

        app,

        use_reloader=reload,

        use_debugger=debugger,

        threaded=with_threads,

        ssl_context=cert,

        extra_files=extra_files,

        exclude_patterns=exclude_patterns,

    )


 

run_command.params.insert(0, _debug_option)


 

@click.command("shell", short_help="Run a shell in the app context.")

@with_appcontext

def shell_command() -> None:

    """Run an interactive Python shell in the context of a given

    Flask application.  The application will populate the default

    namespace of this shell according to its configuration.

    This is useful for executing small snippets of management code

    without having to manually configure the application.

    """

    import code

    banner = (

        f"Python {sys.version} on {sys.platform}\n"

        f"App: {current_app.import_name}\n"

        f"Instance: {current_app.instance_path}"

    )

    ctx: dict[str, t.Any] = {}

    # Support the regular Python interpreter startup script if someone

    # is using it.

    startup = os.environ.get("PYTHONSTARTUP")

    if startup and os.path.isfile(startup):

        with open(startup) as f:

            eval(compile(f.read(), startup, "exec"), ctx)

    ctx.update(current_app.make_shell_context())

    # Site, customize, or startup script can set a hook to call when

    # entering interactive mode. The default one sets up readline with

    # tab and history completion.

    interactive_hook = getattr(sys, "__interactivehook__", None)

    if interactive_hook is not None:

        try:

            import readline

            from rlcompleter import Completer

        except ImportError:

            pass

        else:

            # rlcompleter uses __main__.__dict__ by default, which is

            # flask.__main__. Use the shell context instead.

            readline.set_completer(Completer(ctx).complete)

        interactive_hook()

    code.interact(banner=banner, local=ctx)


 

@click.command("routes", short_help="Show the routes for the app.")

@click.option(

    "--sort",

    "-s",

    type=click.Choice(("endpoint", "methods", "domain", "rule", "match")),

    default="endpoint",

    help=(

        "Method to sort routes by. 'match' is the order that Flask will match routes"

        " when dispatching a request."

    ),

)

@click.option("--all-methods", is_flag=True, help="Show HEAD and OPTIONS methods.")

@with_appcontext

def routes_command(sort: str, all_methods: bool) -> None:

    """Show all registered routes with endpoints and methods."""

    rules = list(current_app.url_map.iter_rules())

    if not rules:

        click.echo("No routes were registered.")

        return

    ignored_methods = set() if all_methods else {"HEAD", "OPTIONS"}

    host_matching = current_app.url_map.host_matching

    has_domain = any(rule.host if host_matching else rule.subdomain for rule in rules)

    rows = []

    for rule in rules:

        row = [

            rule.endpoint,

            ", ".join(sorted((rule.methods or set()) - ignored_methods)),

        ]

        if has_domain:

            row.append((rule.host if host_matching else rule.subdomain) or "")

        row.append(rule.rule)

        rows.append(row)

    headers = ["Endpoint", "Methods"]

    sorts = ["endpoint", "methods"]

    if has_domain:

        headers.append("Host" if host_matching else "Subdomain")

        sorts.append("domain")

    headers.append("Rule")

    sorts.append("rule")

    try:

        rows.sort(key=itemgetter(sorts.index(sort)))

    except ValueError:

        pass

    rows.insert(0, headers)

    widths = [max(len(row[i]) for row in rows) for i in range(len(headers))]

    rows.insert(1, ["-" * w for w in widths])

    template = "  ".join(f"{{{i}:<{w}}}" for i, w in enumerate(widths))

    for row in rows:

        click.echo(template.format(*row))


 

cli = FlaskGroup(

    name="flask",

    help="""\

A general utility script for Flask applications.

An application to load must be given with the '--app' option,

'FLASK_APP' environment variable, or with a 'wsgi.py' or 'app.py' file

in the current directory.

""",

)


 

def main() -> None:

    cli.main()


 

if __name__ == "__main__":

    main()

这段代码是一个基于Flask的命令行脚本,用于管理Flask应用。

  1. FlaskGroupAppGroup的特殊子类,支持从配置的Flask应用加载更多命令。可以添加默认的runshell命令,也可以添加--version选项。
  2. 也可以加载环境变量文件(.env或.flaskenv)来设置环境变量,并在运行app.cli命令时推送应用上下文。
  3. 也可以加载由应用提供的命令,并在应用加载失败时显示错误消息。
  4. 还提供了run命令,用于运行本地开发服务器,支持HTTPS、重载器、调试器、多线程,以及文件更改的实时加载。
  5. 提供了shell命令,用于在应用上下文中运行交互式Python shell。
  6. 提供了routes命令,用于显示应用的所有注册路由及其细节。
  7. 定义了自定义的CertParamTypeSeparatedPathType,用于处理--cert和文件路径类型的选项。
  8. 提供了load_dotenv函数,用于在加载"dotenv"文件以设置环境变量。
  9. 最后通过FlaskGroup创建了一个名为"flask"的命令行接口。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值