Format: application/x-python-code
⬇ Download File (13.1 KB)
   1  o
   2  
   3  D'h�3�@s�ddlmZddlZddlZddlZddlZddlZddlm	Z	ej
   4  r-ddlZddl
   5  mZe�d�ZGdd�deje�ZGd	d
   6  �d
   7  e�ZdS)�)�annotationsN)�
   8  import_string�)�App�Tc@sTeZdZdZ	ddd	d
   9  �Zejddd��Zejddd��Zdd dd�Zd!dd�ZdS)"�ConfigAttributez(Makes an attribute forward to the configN�name�str�
  10  get_converter�t.Callable[[t.Any], T] | None�return�NonecCs||_||_dS�N)�__name__r
  11  )�selfrr
  12  �r�BC:\Users\shubh\Videos\agent\nova\Lib\site-packages\flask/config.py�__init__s
  13  zConfigAttribute.__init__�obj�owner�te.SelfcC�dSrr�rrrrrr�__get__�zConfigAttribute.__get__r�	type[App]rcCrrrrrrrr r�
  14  App | None�type[App] | None�T | te.SelfcCs0|dur|S|j|j}|jdur|�|�}|Sr)�configrr
  15  )rrr�rvrrrr#s
  16  
  17  �value�t.AnycCs||j|j<dSr)rr)rrr!rrr�__set__.szConfigAttribute.__set__r)rr	r
  18  rrr
  19  )rr
  20  rr
  21  rr)rrrrrr)rrrrrr)rrr!r"rr
  22  )	r�
  23  __module__�__qualname__�__doc__r�t�overloadrr#rrrrrs�rcs�eZdZdZ	d4d5�fd	d
  24  �
  25  Zd6d7dd�Z	d8ejd�d9dd�Z	d6d:dd�Z	d;dd�Z
  26  		 d<d=d$d%�Z	d4d>d*d+�Z	 d?d@d0d1�Z
  27  dAd2d3�Z�ZS)B�Configa�Works exactly like a dict but provides ways to fill it from files
  28      or special dictionaries.  There are two common patterns to populate the
  29      config.
  30  
  31      Either you can fill the config from a config file::
  32  
  33          app.config.from_pyfile('yourconfig.cfg')
  34  
  35      Or alternatively you can define the configuration options in the
  36      module that calls :meth:`from_object` or provide an import path to
  37      a module that should be loaded.  It is also possible to tell it to
  38      use the same module and with that provide the configuration values
  39      just before the call::
  40  
  41          DEBUG = True
  42          SECRET_KEY = 'development key'
  43          app.config.from_object(__name__)
  44  
  45      In both cases (loading from any Python file or loading from modules),
  46      only uppercase keys are added to the config.  This makes it possible to use
  47      lowercase values in the config file for temporary values that are not added
  48      to the config or to define the config keys in the same file that implements
  49      the application.
  50  
  51      Probably the most interesting way to load configurations is from an
  52      environment variable pointing to a file::
  53  
  54          app.config.from_envvar('YOURAPPLICATION_SETTINGS')
  55  
  56      In this case before launching the application you have to set this
  57      environment variable to the file you want to use.  On Linux and OS X
  58      use the export statement::
  59  
  60          export YOURAPPLICATION_SETTINGS='/path/to/config/file'
  61  
  62      On windows use `set` instead.
  63  
  64      :param root_path: path to which files are read relative from.  When the
  65                        config object is created by the application, this is
  66                        the application's :attr:`~flask.Flask.root_path`.
  67      :param defaults: an optional dictionary of default values
  68      N�	root_path�str | os.PathLike[str]�defaults�dict[str, t.Any] | Nonerr
  69  cst��|pi�||_dSr)�superrr*)rr*r,��	__class__rrr^s
  70  zConfig.__init__F�
  71  variable_namer	�silent�boolcCs6tj�|�}|s|rdStd|�d���|j||d�S)a�Loads a configuration from an environment variable pointing to
  72          a configuration file.  This is basically just a shortcut with nicer
  73          error messages for this line of code::
  74  
  75              app.config.from_pyfile(os.environ['YOURAPPLICATION_SETTINGS'])
  76  
  77          :param variable_name: name of the environment variable
  78          :param silent: set to ``True`` if you want silent failure for missing
  79                         files.
  80          :return: ``True`` if the file was loaded successfully.
  81          FzThe environment variable zv is not set and as such configuration could not be loaded. Set this variable and make it point to a configuration file)r2)�os�environ�get�RuntimeError�from_pyfile)rr1r2r rrr�from_envvarfs
  82  �zConfig.from_envvar�FLASK)�loads�prefixr;�t.Callable[[str], t.Any]c		Cs�|�d�}ttj�D]J}|�|�sq
  83  tj|}|�|�}z||�}Wn	ty+Ynwd|vr5|||<q
  84  |}|�d��^}}|D]}||vrKi||<||}qA|||<q
  85  dS)aLoad any environment variables that start with ``FLASK_``,
  86          dropping the prefix from the env key for the config key. Values
  87          are passed through a loading function to attempt to convert them
  88          to more specific types than strings.
  89  
  90          Keys are loaded in :func:`sorted` order.
  91  
  92          The default loading function attempts to parse values as any
  93          valid JSON type, including dicts and lists.
  94  
  95          Specific items in nested dicts can be set by separating the
  96          keys with double underscores (``__``). If an intermediate key
  97          doesn't exist, it will be initialized to an empty dict.
  98  
  99          :param prefix: Load env vars that start with this prefix,
 100              separated with an underscore (``_``).
 101          :param loads: Pass each string value to this function and use
 102              the returned value as the config value. If any error is
 103              raised it is ignored and the value remains a string. The
 104              default is :func:`json.loads`.
 105  
 106          .. versionadded:: 2.1
 107          �_�__T)�sortedr4r5�
 108  startswith�removeprefix�	Exception�split)	rr<r;�keyr!�current�parts�tail�partrrr�from_prefixed_env~s,
 109  
 110  
 111  
 112  �
 113  
 114  zConfig.from_prefixed_env�filenamec
 115  Cs�tj�|j|�}t�d�}||_z$t|dd��}tt	|�
 116  �|d�|j�Wd�n1s.wYWn)ty]}z|rP|j
 117  t
 118  jt
 119  jt
 120  jfvrPWYd}~dSd|j�d�|_�d}~ww|�|�d	S)
 121  aTUpdates the values in the config from a Python file.  This function
 122          behaves as if the file was imported as module with the
 123          :meth:`from_object` function.
 124  
 125          :param filename: the filename of the config.  This can either be an
 126                           absolute filename or a filename relative to the
 127                           root path.
 128          :param silent: set to ``True`` if you want silent failure for missing
 129                         files.
 130          :return: ``True`` if the file was loaded successfully.
 131  
 132          .. versionadded:: 0.7
 133             `silent` parameter.
 134          r�rb)�mode�execNF�#Unable to load configuration file (�)T)r4�path�joinr*�types�
 135  ModuleType�__file__�openrN�compile�read�__dict__�OSError�errno�ENOENT�EISDIR�ENOTDIR�strerror�from_object)rrKr2�d�config_file�errrr8�s"
 136  ����
 137  zConfig.from_pyfiler�object | strcCs:t|t�r	t|�}t|�D]
 138  }|��rt||�||<q
 139  dS)a�Updates the values from the given object.  An object can be of one
 140          of the following two types:
 141  
 142          -   a string: in this case the object with that name will be imported
 143          -   an actual object reference: that object is used directly
 144  
 145          Objects are usually either modules or classes. :meth:`from_object`
 146          loads only the uppercase attributes of the module/class. A ``dict``
 147          object will not work with :meth:`from_object` because the keys of a
 148          ``dict`` are not attributes of the ``dict`` class.
 149  
 150          Example of module-based configuration::
 151  
 152              app.config.from_object('yourapplication.default_config')
 153              from yourapplication import default_config
 154              app.config.from_object(default_config)
 155  
 156          Nothing is done to the object before loading. If the object is a
 157          class and has ``@property`` attributes, it needs to be
 158          instantiated before being passed to this method.
 159  
 160          You should not use this function to load the actual configuration but
 161          rather configuration defaults.  The actual config should be loaded
 162          with :meth:`from_pyfile` and ideally from a location not within the
 163          package because the package might be installed system wide.
 164  
 165          See :ref:`config-dev-prod` for an example of class-based configuration
 166          using :meth:`from_object`.
 167  
 168          :param obj: an import name or object
 169          N)�
 170  isinstancer	r�dir�isupper�getattr)rrrErrrr`�s
 171   ��zConfig.from_objectT�load�0t.Callable[[t.IO[t.Any]], t.Mapping[str, t.Any]]�textc
 172  Cs�tj�|j|�}zt||rdnd��}||�}Wd�n1s!wYWn'tyN}z|rA|jtjtjfvrAWYd}~dSd|j	�d�|_	�d}~ww|�
 173  |�S)a"Update the values in the config from a file that is loaded
 174          using the ``load`` parameter. The loaded data is passed to the
 175          :meth:`from_mapping` method.
 176  
 177          .. code-block:: python
 178  
 179              import json
 180              app.config.from_file("config.json", load=json.load)
 181  
 182              import tomllib
 183              app.config.from_file("config.toml", load=tomllib.load, text=False)
 184  
 185          :param filename: The path to the data file. This can be an
 186              absolute path or relative to the config root path.
 187          :param load: A callable that takes a file handle and returns a
 188              mapping of loaded data from the file.
 189          :type load: ``Callable[[Reader], Mapping]`` where ``Reader``
 190              implements a ``read`` method.
 191          :param silent: Ignore the file if it doesn't exist.
 192          :param text: Open the file in text or binary mode.
 193          :return: ``True`` if the file was loaded successfully.
 194  
 195          .. versionchanged:: 2.3
 196              The ``text`` parameter was added.
 197  
 198          .. versionadded:: 2.0
 199          �rrLNFrOrP)r4rQrRr*rVrZr[r\r]r_�from_mapping)rrKrir2rk�frrcrrr�	from_files"
 200  ����
 201  zConfig.from_file�mapping�t.Mapping[str, t.Any] | None�kwargsr"cKsFi}|dur|�|�|�|�|��D]\}}|��r |||<qdS)z�Updates the config like :meth:`update` ignoring items with
 202          non-upper keys.
 203  
 204          :return: Always returns ``True``.
 205  
 206          .. versionadded:: 0.11
 207          NT)�update�itemsrg)rrprrZmappingsrEr!rrrrm0s
 208  
 209  
 210  �zConfig.from_mapping�	namespace�	lowercase�trim_namespace�dict[str, t.Any]cCsTi}|��D]!\}}|�|�sq|r|t|�d�}n|}|r#|��}|||<q|S)aiReturns a dictionary containing a subset of configuration options
 211          that match the specified namespace/prefix. Example usage::
 212  
 213              app.config['IMAGE_STORE_TYPE'] = 'fs'
 214              app.config['IMAGE_STORE_PATH'] = '/var/app/images'
 215              app.config['IMAGE_STORE_BASE_URL'] = 'http://img.website.com'
 216              image_store_config = app.config.get_namespace('IMAGE_STORE_')
 217  
 218          The resulting dictionary `image_store_config` would look like::
 219  
 220              {
 221                  'type': 'fs',
 222                  'path': '/var/app/images',
 223                  'base_url': 'http://img.website.com'
 224              }
 225  
 226          This is often useful when configuration options map directly to
 227          keyword arguments in functions or class constructors.
 228  
 229          :param namespace: a configuration namespace
 230          :param lowercase: a flag indicating if the keys of the resulting
 231                            dictionary should be lowercase
 232          :param trim_namespace: a flag indicating if the keys of the resulting
 233                            dictionary should not include the namespace
 234  
 235          .. versionadded:: 0.11
 236          N)rtrA�len�lower)rrurvrwr �k�vrErrr�
 237  get_namespaceCs
 238  
 239  zConfig.get_namespacecCsdt|�j�dt�|��d�S)N�<� �>)�typer�dict�__repr__)rrrrr�nszConfig.__repr__r)r*r+r,r-rr
 240  )F)r1r	r2r3rr3)r:)r<r	r;r=rr3)rKr+r2r3rr3)rrdrr
 241  )FT)
 242  rKr+rirjr2r3rkr3rr3)rprqrrr"rr3)TT)rur	rvr3rwr3rrx)rr	)rr$r%r&rr9�jsonr;rJr8r`rormr}r��
 243  __classcell__rrr/rr)2s(.���>�
 244  *�1��+r))�
 245  __future__rr[r�r4rS�typingr'Zwerkzeug.utilsr�
 246  TYPE_CHECKINGZtyping_extensions�teZ
 247  sansio.appr�TypeVarr�Genericrr�r)rrrr�<module>s
 248