]> jfr.im git - dlqueue.git/blob - venv/lib/python3.11/site-packages/flask/blueprints.py
init: venv aand flask
[dlqueue.git] / venv / lib / python3.11 / site-packages / flask / blueprints.py
1 from __future__ import annotations
2
3 import os
4 import typing as t
5 from datetime import timedelta
6
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
11
12 if t.TYPE_CHECKING: # pragma: no cover
13 from .wrappers import Response
14
15
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.
20
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.
25
26 Note this is a duplicate of the same method in the Flask
27 class.
28
29 .. versionchanged:: 2.0
30 The default configuration is ``None`` instead of 12 hours.
31
32 .. versionadded:: 0.9
33 """
34 value = current_app.config["SEND_FILE_MAX_AGE_DEFAULT"]
35
36 if value is None:
37 return None
38
39 if isinstance(value, timedelta):
40 return int(value.total_seconds())
41
42 return value
43
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
48 set.
49
50 Note this is a duplicate of the same method in the Flask
51 class.
52
53 .. versionadded:: 0.5
54
55 """
56 if not self.has_static_folder:
57 raise RuntimeError("'static_folder' must be set to serve static_files.")
58
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
64 )
65
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
68 reading.
69
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
72 with:
73
74 .. code-block:: python
75
76 with app.open_resource("schema.sql") as f:
77 conn.executescript(f.read())
78
79 :param resource: Path to the resource relative to
80 :attr:`root_path`.
81 :param mode: Open the file in this mode. Only reading is
82 supported, valid values are "r" (or "rt") and "rb".
83
84 Note this is a duplicate of the same method in the Flask
85 class.
86
87 """
88 if mode not in {"r", "rt", "rb"}:
89 raise ValueError("Resources can only be opened for reading.")
90
91 return open(os.path.join(self.root_path, resource), mode)