]>
jfr.im git - dlqueue.git/blob - venv/lib/python3.11/site-packages/flask/blueprints.py
1 from __future__
import annotations
5 from datetime
import timedelta
7 from .globals import current_app
8 from .helpers
import send_from_directory
9 from .sansio
.blueprints
import Blueprint
as SansioBlueprint
10 from .sansio
.blueprints
import BlueprintSetupState
as BlueprintSetupState
# noqa
12 if t
.TYPE_CHECKING
: # pragma: no cover
13 from .wrappers
import Response
16 class Blueprint(SansioBlueprint
):
17 def get_send_file_max_age(self
, filename
: str |
None) -> int |
None:
18 """Used by :func:`send_file` to determine the ``max_age`` cache
19 value for a given file path if it wasn't passed.
21 By default, this returns :data:`SEND_FILE_MAX_AGE_DEFAULT` from
22 the configuration of :data:`~flask.current_app`. This defaults
23 to ``None``, which tells the browser to use conditional requests
24 instead of a timed cache, which is usually preferable.
26 Note this is a duplicate of the same method in the Flask
29 .. versionchanged:: 2.0
30 The default configuration is ``None`` instead of 12 hours.
34 value
= current_app
.config
["SEND_FILE_MAX_AGE_DEFAULT"]
39 if isinstance(value
, timedelta
):
40 return int(value
.total_seconds())
44 def send_static_file(self
, filename
: str) -> Response
:
45 """The view function used to serve files from
46 :attr:`static_folder`. A route is automatically registered for
47 this view at :attr:`static_url_path` if :attr:`static_folder` is
50 Note this is a duplicate of the same method in the Flask
56 if not self
.has_static_folder
:
57 raise RuntimeError("'static_folder' must be set to serve static_files.")
59 # send_file only knows to call get_send_file_max_age on the app,
60 # call it here so it works for blueprints too.
61 max_age
= self
.get_send_file_max_age(filename
)
62 return send_from_directory(
63 t
.cast(str, self
.static_folder
), filename
, max_age
=max_age
66 def open_resource(self
, resource
: str, mode
: str = "rb") -> t
.IO
[t
.AnyStr
]:
67 """Open a resource file relative to :attr:`root_path` for
70 For example, if the file ``schema.sql`` is next to the file
71 ``app.py`` where the ``Flask`` app is defined, it can be opened
74 .. code-block:: python
76 with app.open_resource("schema.sql") as f:
77 conn.executescript(f.read())
79 :param resource: Path to the resource relative to
81 :param mode: Open the file in this mode. Only reading is
82 supported, valid values are "r" (or "rt") and "rb".
84 Note this is a duplicate of the same method in the Flask
88 if mode
not in {"r", "rt", "rb"}
:
89 raise ValueError("Resources can only be opened for reading.")
91 return open(os
.path
.join(self
.root_path
, resource
), mode
)