config.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. from __future__ import annotations
  2. import errno
  3. import json
  4. import os
  5. import types
  6. import typing as t
  7. from werkzeug.utils import import_string
  8. class ConfigAttribute:
  9. """Makes an attribute forward to the config"""
  10. def __init__(self, name: str, get_converter: t.Callable | None = None) -> None:
  11. self.__name__ = name
  12. self.get_converter = get_converter
  13. def __get__(self, obj: t.Any, owner: t.Any = None) -> t.Any:
  14. if obj is None:
  15. return self
  16. rv = obj.config[self.__name__]
  17. if self.get_converter is not None:
  18. rv = self.get_converter(rv)
  19. return rv
  20. def __set__(self, obj: t.Any, value: t.Any) -> None:
  21. obj.config[self.__name__] = value
  22. class Config(dict):
  23. """Works exactly like a dict but provides ways to fill it from files
  24. or special dictionaries. There are two common patterns to populate the
  25. config.
  26. Either you can fill the config from a config file::
  27. app.config.from_pyfile('yourconfig.cfg')
  28. Or alternatively you can define the configuration options in the
  29. module that calls :meth:`from_object` or provide an import path to
  30. a module that should be loaded. It is also possible to tell it to
  31. use the same module and with that provide the configuration values
  32. just before the call::
  33. DEBUG = True
  34. SECRET_KEY = 'development key'
  35. app.config.from_object(__name__)
  36. In both cases (loading from any Python file or loading from modules),
  37. only uppercase keys are added to the config. This makes it possible to use
  38. lowercase values in the config file for temporary values that are not added
  39. to the config or to define the config keys in the same file that implements
  40. the application.
  41. Probably the most interesting way to load configurations is from an
  42. environment variable pointing to a file::
  43. app.config.from_envvar('YOURAPPLICATION_SETTINGS')
  44. In this case before launching the application you have to set this
  45. environment variable to the file you want to use. On Linux and OS X
  46. use the export statement::
  47. export YOURAPPLICATION_SETTINGS='/path/to/config/file'
  48. On windows use `set` instead.
  49. :param root_path: path to which files are read relative from. When the
  50. config object is created by the application, this is
  51. the application's :attr:`~flask.Flask.root_path`.
  52. :param defaults: an optional dictionary of default values
  53. """
  54. def __init__(
  55. self, root_path: str | os.PathLike, defaults: dict | None = None
  56. ) -> None:
  57. super().__init__(defaults or {})
  58. self.root_path = root_path
  59. def from_envvar(self, variable_name: str, silent: bool = False) -> bool:
  60. """Loads a configuration from an environment variable pointing to
  61. a configuration file. This is basically just a shortcut with nicer
  62. error messages for this line of code::
  63. app.config.from_pyfile(os.environ['YOURAPPLICATION_SETTINGS'])
  64. :param variable_name: name of the environment variable
  65. :param silent: set to ``True`` if you want silent failure for missing
  66. files.
  67. :return: ``True`` if the file was loaded successfully.
  68. """
  69. rv = os.environ.get(variable_name)
  70. if not rv:
  71. if silent:
  72. return False
  73. raise RuntimeError(
  74. f"The environment variable {variable_name!r} is not set"
  75. " and as such configuration could not be loaded. Set"
  76. " this variable and make it point to a configuration"
  77. " file"
  78. )
  79. return self.from_pyfile(rv, silent=silent)
  80. def from_prefixed_env(
  81. self, prefix: str = "FLASK", *, loads: t.Callable[[str], t.Any] = json.loads
  82. ) -> bool:
  83. """Load any environment variables that start with ``FLASK_``,
  84. dropping the prefix from the env key for the config key. Values
  85. are passed through a loading function to attempt to convert them
  86. to more specific types than strings.
  87. Keys are loaded in :func:`sorted` order.
  88. The default loading function attempts to parse values as any
  89. valid JSON type, including dicts and lists.
  90. Specific items in nested dicts can be set by separating the
  91. keys with double underscores (``__``). If an intermediate key
  92. doesn't exist, it will be initialized to an empty dict.
  93. :param prefix: Load env vars that start with this prefix,
  94. separated with an underscore (``_``).
  95. :param loads: Pass each string value to this function and use
  96. the returned value as the config value. If any error is
  97. raised it is ignored and the value remains a string. The
  98. default is :func:`json.loads`.
  99. .. versionadded:: 2.1
  100. """
  101. prefix = f"{prefix}_"
  102. len_prefix = len(prefix)
  103. for key in sorted(os.environ):
  104. if not key.startswith(prefix):
  105. continue
  106. value = os.environ[key]
  107. try:
  108. value = loads(value)
  109. except Exception:
  110. # Keep the value as a string if loading failed.
  111. pass
  112. # Change to key.removeprefix(prefix) on Python >= 3.9.
  113. key = key[len_prefix:]
  114. if "__" not in key:
  115. # A non-nested key, set directly.
  116. self[key] = value
  117. continue
  118. # Traverse nested dictionaries with keys separated by "__".
  119. current = self
  120. *parts, tail = key.split("__")
  121. for part in parts:
  122. # If an intermediate dict does not exist, create it.
  123. if part not in current:
  124. current[part] = {}
  125. current = current[part]
  126. current[tail] = value
  127. return True
  128. def from_pyfile(self, filename: str | os.PathLike, silent: bool = False) -> bool:
  129. """Updates the values in the config from a Python file. This function
  130. behaves as if the file was imported as module with the
  131. :meth:`from_object` function.
  132. :param filename: the filename of the config. This can either be an
  133. absolute filename or a filename relative to the
  134. root path.
  135. :param silent: set to ``True`` if you want silent failure for missing
  136. files.
  137. :return: ``True`` if the file was loaded successfully.
  138. .. versionadded:: 0.7
  139. `silent` parameter.
  140. """
  141. filename = os.path.join(self.root_path, filename)
  142. d = types.ModuleType("config")
  143. d.__file__ = filename
  144. try:
  145. with open(filename, mode="rb") as config_file:
  146. exec(compile(config_file.read(), filename, "exec"), d.__dict__)
  147. except OSError as e:
  148. if silent and e.errno in (errno.ENOENT, errno.EISDIR, errno.ENOTDIR):
  149. return False
  150. e.strerror = f"Unable to load configuration file ({e.strerror})"
  151. raise
  152. self.from_object(d)
  153. return True
  154. def from_object(self, obj: object | str) -> None:
  155. """Updates the values from the given object. An object can be of one
  156. of the following two types:
  157. - a string: in this case the object with that name will be imported
  158. - an actual object reference: that object is used directly
  159. Objects are usually either modules or classes. :meth:`from_object`
  160. loads only the uppercase attributes of the module/class. A ``dict``
  161. object will not work with :meth:`from_object` because the keys of a
  162. ``dict`` are not attributes of the ``dict`` class.
  163. Example of module-based configuration::
  164. app.config.from_object('yourapplication.default_config')
  165. from yourapplication import default_config
  166. app.config.from_object(default_config)
  167. Nothing is done to the object before loading. If the object is a
  168. class and has ``@property`` attributes, it needs to be
  169. instantiated before being passed to this method.
  170. You should not use this function to load the actual configuration but
  171. rather configuration defaults. The actual config should be loaded
  172. with :meth:`from_pyfile` and ideally from a location not within the
  173. package because the package might be installed system wide.
  174. See :ref:`config-dev-prod` for an example of class-based configuration
  175. using :meth:`from_object`.
  176. :param obj: an import name or object
  177. """
  178. if isinstance(obj, str):
  179. obj = import_string(obj)
  180. for key in dir(obj):
  181. if key.isupper():
  182. self[key] = getattr(obj, key)
  183. def from_file(
  184. self,
  185. filename: str | os.PathLike,
  186. load: t.Callable[[t.IO[t.Any]], t.Mapping],
  187. silent: bool = False,
  188. text: bool = True,
  189. ) -> bool:
  190. """Update the values in the config from a file that is loaded
  191. using the ``load`` parameter. The loaded data is passed to the
  192. :meth:`from_mapping` method.
  193. .. code-block:: python
  194. import json
  195. app.config.from_file("config.json", load=json.load)
  196. import tomllib
  197. app.config.from_file("config.toml", load=tomllib.load, text=False)
  198. :param filename: The path to the data file. This can be an
  199. absolute path or relative to the config root path.
  200. :param load: A callable that takes a file handle and returns a
  201. mapping of loaded data from the file.
  202. :type load: ``Callable[[Reader], Mapping]`` where ``Reader``
  203. implements a ``read`` method.
  204. :param silent: Ignore the file if it doesn't exist.
  205. :param text: Open the file in text or binary mode.
  206. :return: ``True`` if the file was loaded successfully.
  207. .. versionchanged:: 2.3
  208. The ``text`` parameter was added.
  209. .. versionadded:: 2.0
  210. """
  211. filename = os.path.join(self.root_path, filename)
  212. try:
  213. with open(filename, "r" if text else "rb") as f:
  214. obj = load(f)
  215. except OSError as e:
  216. if silent and e.errno in (errno.ENOENT, errno.EISDIR):
  217. return False
  218. e.strerror = f"Unable to load configuration file ({e.strerror})"
  219. raise
  220. return self.from_mapping(obj)
  221. def from_mapping(
  222. self, mapping: t.Mapping[str, t.Any] | None = None, **kwargs: t.Any
  223. ) -> bool:
  224. """Updates the config like :meth:`update` ignoring items with
  225. non-upper keys.
  226. :return: Always returns ``True``.
  227. .. versionadded:: 0.11
  228. """
  229. mappings: dict[str, t.Any] = {}
  230. if mapping is not None:
  231. mappings.update(mapping)
  232. mappings.update(kwargs)
  233. for key, value in mappings.items():
  234. if key.isupper():
  235. self[key] = value
  236. return True
  237. def get_namespace(
  238. self, namespace: str, lowercase: bool = True, trim_namespace: bool = True
  239. ) -> dict[str, t.Any]:
  240. """Returns a dictionary containing a subset of configuration options
  241. that match the specified namespace/prefix. Example usage::
  242. app.config['IMAGE_STORE_TYPE'] = 'fs'
  243. app.config['IMAGE_STORE_PATH'] = '/var/app/images'
  244. app.config['IMAGE_STORE_BASE_URL'] = 'http://img.website.com'
  245. image_store_config = app.config.get_namespace('IMAGE_STORE_')
  246. The resulting dictionary `image_store_config` would look like::
  247. {
  248. 'type': 'fs',
  249. 'path': '/var/app/images',
  250. 'base_url': 'http://img.website.com'
  251. }
  252. This is often useful when configuration options map directly to
  253. keyword arguments in functions or class constructors.
  254. :param namespace: a configuration namespace
  255. :param lowercase: a flag indicating if the keys of the resulting
  256. dictionary should be lowercase
  257. :param trim_namespace: a flag indicating if the keys of the resulting
  258. dictionary should not include the namespace
  259. .. versionadded:: 0.11
  260. """
  261. rv = {}
  262. for k, v in self.items():
  263. if not k.startswith(namespace):
  264. continue
  265. if trim_namespace:
  266. key = k[len(namespace) :]
  267. else:
  268. key = k
  269. if lowercase:
  270. key = key.lower()
  271. rv[key] = v
  272. return rv
  273. def __repr__(self) -> str:
  274. return f"<{type(self).__name__} {dict.__repr__(self)}>"