Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/flask/blueprints.py: 48%
25 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-09 07:17 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-09 07:17 +0000
1from __future__ import annotations
3import os
4import typing as t
5from datetime import timedelta
7from .globals import current_app
8from .helpers import send_from_directory
9from .sansio.blueprints import Blueprint as SansioBlueprint
10from .sansio.blueprints import BlueprintSetupState as BlueprintSetupState # noqa
12if t.TYPE_CHECKING: # pragma: no cover
13 from .wrappers import Response
16class 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
27 class.
29 .. versionchanged:: 2.0
30 The default configuration is ``None`` instead of 12 hours.
32 .. versionadded:: 0.9
33 """
34 value = current_app.config["SEND_FILE_MAX_AGE_DEFAULT"]
36 if value is None:
37 return None
39 if isinstance(value, timedelta):
40 return int(value.total_seconds())
42 return value
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.
50 Note this is a duplicate of the same method in the Flask
51 class.
53 .. versionadded:: 0.5
55 """
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
64 )
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.
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:
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
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".
84 Note this is a duplicate of the same method in the Flask
85 class.
87 """
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)