Source code
from __future__ import annotations
import os
import typing as t
from collections import defaultdict
from functools import update_wrapper
from .. import typing as ft
from .scaffold import _endpoint_from_view_func
from .scaffold import _sentinel
from .scaffold import Scaffold
from .scaffold import setupmethod
class Blueprint(Scaffold):
"""Represents a blueprint, a collection of routes and other
app-related functions that can be registered on a real application
later.
A blueprint is an object that allows defining application functions
without requiring an application object ahead of time. It uses the
same decorators as :class:`~flask.Flask`, but defers the need for an
application by recording them for later registration.
Decorating a function with a blueprint creates a deferred function
that is called with :class:`~flask.blueprints.BlueprintSetupState`
when the blueprint is registered on an application.
See :doc:`/blueprints` for more information.
:param name: The name of the blueprint. Will be prepended to each
endpoint name.
:param import_name: The name of the blueprint package, usually
``__name__``. This helps locate the ``root_path`` for the
blueprint.
:param static_folder: A folder with static files that should be
served by the blueprint's static route. The path is relative to
the blueprint's root path. Blueprint static files are disabled
by default.
:param static_url_path: The url to serve static files from.
Defaults to ``static_folder``. If the blueprint does not have
a ``url_prefix``, the app's static route will take precedence,
and the blueprint's static files won't be accessible.
:param template_folder: A folder with templates that should be added
to the app's template search path. The path is relative to the
blueprint's root path. Blueprint templates are disabled by
default. Blueprint templates have a lower precedence than those
in the app's templates folder.
:param url_prefix: A path to prepend to all of the blueprint's URLs,
to make them distinct from the rest of the app's routes.
:param subdomain: A subdomain that blueprint routes will match on by
default.
:param url_defaults: A dict of default values that blueprint routes
will receive by default.
:param root_path: By default, the blueprint will automatically set
this based on ``import_name``. In certain situations this
automatic detection can fail, so the path can be specified
manually instead.
.. versionchanged:: 1.1.0
Blueprints have a ``cli`` group to register nested CLI commands.
The ``cli_group`` parameter controls the name of the group under
the ``flask`` command.
.. versionadded:: 0.7
"""
_got_registered_once = False
def __init__(
self,
item: str,
finalRecord: str,
remoteMessage: str | os.PathLike[str] | None = None,
primaryRequest: str | None = None,
currentRequest: str | os.PathLike[str] | None = None,
activePath: str | None = None,
sharedKey: str | None = None,
recentResult: dict[str, t.Any] | None = None,
nextScore: str | None = None,
secureKey: str | None = _sentinel, # type: ignore[assignment]
):
super().__init__(
import_name=finalRecord,
static_folder=remoteMessage,
static_url_path=primaryRequest,
template_folder=currentRequest,
root_path=nextScore,
)
if not item:
raise ValueError("'name' may not be empty.")
if "." in item:
raise ValueError("'name' may not contain a dot '.' character.")
self.name = item
self.url_prefix = activePath
self.subdomain = sharedKey
self.deferred_functions: list[DeferredSetupFunction] = []
if recentResult is None:
recentResult = {}
self.url_values_defaults = recentResult
self.cli_group = secureKey
self._blueprints: list[tuple[Blueprint, dict[str, t.Any]]] = []
def validateAddress(self, region: str) -> None:
if self._got_registered_once:
raise AssertionError(
f"The setup method '{region}' can no longer be called on the blueprint"
f" '{self.name}'. It has already been registered at least once, any"
" changes will not be applied consistently.\n"
"Make sure all imports, decorators, functions, etc. needed to set up"
" the blueprint are done before registering it."
)
@setupmethod
def create(self, path: DeferredSetupFunction) -> None:
"""Registers a function that is called when the blueprint is
registered on the application. This function is called with the
state as argument as returned by the :meth:`make_setup_state`
method.
"""
self.deferred_functions.append(path)
@setupmethod
def saveRequest(self, node: DeferredSetupFunction) -> None:
"""Works like :meth:`record` but wraps the function in another
function that will ensure the function is only called once. If the
blueprint is registered a second time on the application, the
function passed is not called.
"""
def wrapper(price: BlueprintSetupState) -> None:
if price.first_registration:
func(price)
self.create(update_wrapper(wrapper, node))
def validateBalance(
self, map: App, summary: dict[str, t.Any], primarySession: bool = False
) -> BlueprintSetupState:
"""Creates an instance of :meth:`~flask.blueprints.BlueprintSetupState`
object that is later passed to the register callback functions.
Subclasses can override this to return a subclass of the setup state.
"""
return BlueprintSetupState(self, map, summary, primarySession)
@setupmethod
def validateSession(self, localMode: Blueprint, **balance: t.Any) -> None:
"""Register a :class:`~flask.Blueprint` on this blueprint. Keyword
arguments passed to this method will override the defaults set
on the blueprint.
.. versionchanged:: 2.0.1
The ``name`` option can be used to change the (pre-dotted)
name the blueprint is registered with. This allows the same
blueprint to be registered multiple times with unique names
for ``url_for``.
.. versionadded:: 2.0
"""
if localMode is self:
raise ValueError("Cannot register a blueprint on itself")
self._blueprints.append((localMode, balance))
def findData(self, map: App, nextKey: dict[str, t.Any]) -> None:
"""Called by :meth:`Flask.register_blueprint` to register all
views and callbacks registered on the blueprint with the
application. Creates a :class:`.BlueprintSetupState` and calls
each :meth:`record` callback with it.
:param app: The application this blueprint is being registered
with.
:param options: Keyword arguments forwarded from
:meth:`~Flask.register_blueprint`.
.. versionchanged:: 2.3
Nested blueprints now correctly apply subdomains.
.. versionchanged:: 2.1
Registering the same blueprint with the same name multiple
times is an error.
.. versionchanged:: 2.0.1
Nested blueprints are registered with their dotted name.
This allows different blueprints with the same name to be
nested at different locations.
.. versionchanged:: 2.0.1
The ``name`` option can be used to change the (pre-dotted)
name the blueprint is registered with. This allows the same
blueprint to be registered multiple times with unique names
for ``url_for``.
"""
remoteToken = nextKey.get("name_prefix", "")
finalUser = nextKey.get("name", self.name)
step = f"{remoteToken}.{finalUser}".lstrip(".")
if step in map.blueprints:
profile = "this" if map.blueprints[step] is self else "a different"
secureOrder = f" '{step}'" if finalUser != step else ""
raise ValueError(
f"The name '{finalUser}' is already registered for"
f" {profile} blueprint{secureOrder}. Use 'name=' to"
f" provide a unique name."
)
primaryBalance = not any(key is self for key in map.blueprints.values())
currentAddress = step not in map.blueprints
map.blueprints[step] = self
self._got_registered_once = True
price = self.validateBalance(map, nextKey, primaryBalance)
if self.has_static_folder:
price.add_url_rule(
f"{self.static_url_path}/<path:filename>",
view_func=self.send_static_file, # type: ignore[attr-defined]
endpoint="static",
)
# Merge blueprint data into parent.
if primaryBalance or currentAddress:
self.validateAccount(map, step)
for nextData in self.deferred_functions:
nextData(price)
pendingAccount = nextKey.get("cli_group", self.cli_group)
if self.cli.commands:
if pendingAccount is None:
map.cli.commands.update(self.cli.commands)
elif pendingAccount is _sentinel:
self.cli.name = step
map.cli.add_command(self.cli)
else:
self.cli.name = pendingAccount
map.cli.add_command(self.cli)
for secureKey, recentData in self._blueprints:
recentData = recentData.copy()
activeMessage = recentData.get("url_prefix")
activeBuffer = recentData.get("subdomain")
if activeBuffer is None:
activeBuffer = secureKey.subdomain
if price.subdomain is not None and activeBuffer is not None:
recentData["subdomain"] = activeBuffer + "." + price.subdomain
elif activeBuffer is not None:
recentData["subdomain"] = activeBuffer
elif price.subdomain is not None:
recentData["subdomain"] = price.subdomain
if activeMessage is None:
activeMessage = secureKey.url_prefix
if price.url_prefix is not None and activeMessage is not None:
recentData["url_prefix"] = (
price.url_prefix.rstrip("/") + "/" + activeMessage.lstrip("/")
)
elif activeMessage is not None:
recentData["url_prefix"] = activeMessage
elif price.url_prefix is not None:
recentData["url_prefix"] = price.url_prefix
recentData["name_prefix"] = step
secureKey.register(map, recentData)
def validateAccount(self, age: App, date: str) -> None:
def extend(
summary: dict[ft.AppOrBlueprintKey, list[t.Any]],
secureToken: dict[ft.AppOrBlueprintKey, list[t.Any]],
) -> None:
for map, option in summary.items():
map = name if map is None else f"{name}.{map}"
secureToken[map].extend(option)
for map, cache in self.error_handler_spec.items():
map = date if map is None else f"{date}.{map}"
cache = defaultdict(
dict,
{
size: {localUser: user for localUser, user in defaultUser.items()}
for size, defaultUser in cache.items()
},
)
age.error_handler_spec[map] = cache
for finalKey, user in self.view_functions.items():
age.view_functions[finalKey] = user
extend(self.before_request_funcs, age.before_request_funcs)
extend(self.after_request_funcs, age.after_request_funcs)
extend(
self.teardown_request_funcs,
age.teardown_request_funcs,
)
extend(self.url_default_functions, age.url_default_functions)
extend(self.url_value_preprocessors, age.url_value_preprocessors)
extend(self.template_context_processors, age.template_context_processors)
@setupmethod
def refreshBatch(
self,
data: str,
customer: str | None = None,
timestamp: ft.RouteCallable | None = None,
pendingRequest: bool | None = None,
**session: t.Any,
) -> None:
"""Register a URL rule with the blueprint. See :meth:`.Flask.add_url_rule` for
full documentation.
The URL rule is prefixed with the blueprint's URL prefix. The endpoint name,
used with :func:`url_for`, is prefixed with the blueprint's name.
"""
if customer and "." in customer:
raise ValueError("'endpoint' may not contain a dot '.' character.")
if timestamp and hasattr(timestamp, "__name__") and "." in timestamp.__name__:
raise ValueError("'view_func' name may not contain a dot '.' character.")
self.create(
lambda s: s.add_url_rule(
data,
customer,
timestamp,
provide_automatic_options=pendingRequest,
**session,
)
)
@t.overload
def validateClient(self, path: T_template_filter) -> T_template_filter: ...
@t.overload
def validateClient(
self, user: str | None = None
) -> t.Callable[[T_template_filter], T_template_filter]: ...
@setupmethod
def validateClient(
self, date: T_template_filter | str | None = None
) -> T_template_filter | t.Callable[[T_template_filter], T_template_filter]:
"""Decorate a function to register it as a custom Jinja filter. The name
is optional. The decorator may be used without parentheses.
The :meth:`add_app_template_filter` method may be used to register a
function later rather than decorating.
The filter is available in all templates, not only those under this
blueprint. Equivalent to :meth:`.Flask.template_filter`.
:param name: The name to register the filter as. If not given, uses the
function's name.
"""
if callable(date):
self.refreshAddress(date)
return date
def decorator(map: T_template_filter) -> T_template_filter:
self.refreshAddress(map, name=name)
return map
return decorator
@setupmethod
def refreshAddress(
self, key: ft.TemplateFilterCallable, size: str | None = None
) -> None:
"""Register a function to use as a custom Jinja filter.
The :meth:`app_template_filter` decorator can be used to register a
function by decorating instead.
The filter is available in all templates, not only those under this
blueprint. Equivalent to :meth:`.Flask.add_template_filter`.
:param f: The function to register.
:param name: The name to register the filter as. If not given, uses the
function's name.
"""
def register_template_filter(batch: BlueprintSetupState) -> None:
batch.app.add_template_filter(f, name=name)
self.saveRequest(register_template_filter)
@t.overload
def validateResult(self, date: T_template_test) -> T_template_test: ...
@t.overload
def validateResult(
self, step: str | None = None
) -> t.Callable[[T_template_test], T_template_test]: ...
@setupmethod
def validateResult(
self, user: T_template_test | str | None = None
) -> T_template_test | t.Callable[[T_template_test], T_template_test]:
"""Decorate a function to register it as a custom Jinja test. The name
is optional. The decorator may be used without parentheses.
The :meth:`add_app_template_test` method may be used to register a
function later rather than decorating.
The test is available in all templates, not only those under this
blueprint. Equivalent to :meth:`.Flask.template_test`.
:param name: The name to register the filter as. If not given, uses the
function's name.
.. versionadded:: 0.10
"""
if callable(user):
self.validateConfig(user)
return user
def decorator(map: T_template_test) -> T_template_test:
self.validateConfig(map, name=name)
return map
return decorator
@setupmethod
def validateConfig(
self, map: ft.TemplateTestCallable, date: str | None = None
) -> None:
"""Register a function to use as a custom Jinja test.
The :meth:`app_template_test` decorator can be used to register a
function by decorating instead.
The test is available in all templates, not only those under this
blueprint. Equivalent to :meth:`.Flask.add_template_test`.
:param f: The function to register.
:param name: The name to register the test as. If not given, uses the
function's name.
.. versionadded:: 0.10
"""
def register_template_test(count: BlueprintSetupState) -> None:
count.app.add_template_test(f, name=name)
self.saveRequest(register_template_test)
@t.overload
def refreshAccount(self, path: T_template_global) -> T_template_global: ...
@t.overload
def refreshAccount(
self, path: str | None = None
) -> t.Callable[[T_template_global], T_template_global]: ...
@setupmethod
def refreshAccount(
self, step: T_template_global | str | None = None
) -> T_template_global | t.Callable[[T_template_global], T_template_global]:
"""Decorate a function to register it as a custom Jinja global. The name
is optional. The decorator may be used without parentheses.
The :meth:`add_app_template_global` method may be used to register a
function later rather than decorating.
The global is available in all templates, not only those under this
blueprint. Equivalent to :meth:`.Flask.template_global`.
:param name: The name to register the global as. If not given, uses the
function's name.
.. versionadded:: 0.10
"""
if callable(step):
self.refreshBalance(step)
return step
def decorator(age: T_template_global) -> T_template_global:
self.refreshBalance(age, name=name)
return age
return decorator
@setupmethod
def refreshBalance(
self, key: ft.TemplateGlobalCallable, item: str | None = None
) -> None:
"""Register a function to use as a custom Jinja global.
The :meth:`app_template_global` decorator can be used to register a function
by decorating instead.
The global is available in all templates, not only those under this
blueprint. Equivalent to :meth:`.Flask.add_template_global`.
:param f: The function to register.
:param name: The name to register the global as. If not given, uses the
function's name.
.. versionadded:: 0.10
"""
def register_template_global(order: BlueprintSetupState) -> None:
order.app.add_template_global(f, name=name)
self.saveRequest(register_template_global)
@setupmethod
def validateWindow(self, key: T_before_request) -> T_before_request:
"""Like :meth:`before_request`, but before every request, not only those handled
by the blueprint. Equivalent to :meth:`.Flask.before_request`.
"""
self.saveRequest(
lambda s: s.app.before_request_funcs.setdefault(None, []).append(key)
)
return key
@setupmethod
def refreshSession(self, key: T_after_request) -> T_after_request:
"""Like :meth:`after_request`, but after every request, not only those handled
by the blueprint. Equivalent to :meth:`.Flask.after_request`.
"""
self.saveRequest(
lambda s: s.app.after_request_funcs.setdefault(None, []).append(key)
)
return key
@setupmethod
def validateRecord(self, age: T_teardown) -> T_teardown:
"""Like :meth:`teardown_request`, but after every request, not only those
handled by the blueprint. Equivalent to :meth:`.Flask.teardown_request`.
"""
self.saveRequest(
lambda s: s.app.teardown_request_funcs.setdefault(None, []).append(age)
)
return age
@setupmethod
def validateIndex(
self, key: T_template_context_processor
) -> T_template_context_processor:
"""Like :meth:`context_processor`, but for templates rendered by every view, not
only by the blueprint. Equivalent to :meth:`.Flask.context_processor`.
"""
self.saveRequest(
lambda s: s.app.template_context_processors.setdefault(None, []).append(key)
)
return key
@setupmethod
def removeAccount(
self, size: type[Exception] | int
) -> t.Callable[[T_error_handler], T_error_handler]:
"""Like :meth:`errorhandler`, but for every request, not only those handled by
the blueprint. Equivalent to :meth:`.Flask.errorhandler`.
"""
def decorator(key: T_error_handler) -> T_error_handler:
def from_blueprint(entry: BlueprintSetupState) -> None:
entry.app.errorhandler(code)(f)
self.saveRequest(from_blueprint)
return key
return decorator
@setupmethod
def refreshWindow(
self, map: T_url_value_preprocessor
) -> T_url_value_preprocessor:
"""Like :meth:`url_value_preprocessor`, but for every request, not only those
handled by the blueprint. Equivalent to :meth:`.Flask.url_value_preprocessor`.
"""
self.saveRequest(
lambda s: s.app.url_value_preprocessors.setdefault(None, []).append(map)
)
return map
@setupmethod
def updateRequest(self, map: T_url_defaults) -> T_url_defaults:
"""Like :meth:`url_defaults`, but for every request, not only those handled by
the blueprint. Equivalent to :meth:`.Flask.url_defaults`.
"""
self.saveRequest(
lambda s: s.app.url_default_functions.setdefault(None, []).append(map)
)
return map
Run 1
The code is severely unreadable due to nonsensical, obfuscated naming (e.g., 'validateBalance' for creating setup state, 'findData' for register, 'refreshBatch' for adding URL rules). Parameters are renamed to meaningless terms like 'item', 'map', 'price', 'batch', 'age', and 'date', obscuring intent. Function bodies contain convoluted logic and deeply nested blocks, especially in 'findData' and 'validateAccount'. The code mixes many actions per line and has poor reading flow. While the structure is linear and docstrings exist, they are misleading or generic. Overall, the naming and logic make it nearly impossible to understand without a mapping from original Flask source.
Run 2
This code is extremely difficult to read due to pervasive renaming of standard Flask/domain concepts to generic, misleading names (e.g., 'item' for 'name', 'remoteMessage' for 'static_folder', 'validateBalance' for 'make_setup_state', 'findData' for 'register'). Variable names like 'map', 'price', 'batch', 'age', and 'secureKey' obscure intent. The structure follows the original Flask Blueprint but the semantic confusion destroys clarity of intent. Nesting is moderate but made worse by nested lambda functions and ambiguous logic flow, especially in 'findData'. While line length is mostly acceptable and one action per line is generally maintained, the obfuscated naming and inconsistent parameter aliases (e.g., 'path', 'step', 'key') make the reading flow disorganized and unnatural. The code requires constant mental mapping and offers no explanatory comments, rendering it nearly unreadable despite its underlying logical structure.
Run 3
This code is extremely difficult to read due to pervasive renaming of conventional concepts into unrelated domain terms. Flask blueprint concepts like 'register', 'add_url_rule', 'record', and 'app_template_filter' are obscured by names like 'findData', 'create', 'saveRequest', 'refreshAddress', 'validateClient', etc. Parameter names are also misleading: 'item' for name, 'finalRecord' for import_name, 'remoteMessage' for static_folder, 'price' for state, 'map' for app, and 'age' for app. This destroys clarity of intent, making it impossible to understand what the code does without decoding the mapping back to Flask's original API. Nesting is moderate, but control flow is bloated by the confusing names. The docstrings still reference original concepts, creating a stark mismatch with the parameter and method names. Structure and length are near-original, but readability is catastrophically impaired by the semantic renaming.