config.py 12 KB

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