U a<+@sNddlZddlZddlZddlZddlmZGdddZGdddeZ dS)N) import_stringc@s\eZdZdZd eejejddddZd ej ej ej dddZ ej ej dd d d Z dS)ConfigAttributez(Makes an attribute forward to the configN)name get_converterreturncCs||_||_dSN)__name__r)selfrrr MC:\Users\vtejo\AppData\Local\Temp\pip-unpacked-wheel-e702oxwa\flask\config.py__init__ szConfigAttribute.__init__)objownerrcCs0|dkr |S|j|j}|jdk r,||}|Sr)configrr)r r rrvr r r __get__s    zConfigAttribute.__get__)r valuercCs||j|j<dSr)rr)r r rr r r __set__szConfigAttribute.__set__)N)N) r __module__ __qualname____doc__strtOptionalCallabler Anyrrr r r r r src@seZdZdZdeejeddddZd ee e ddd Z d!ee e d d d Z ej e efdd ddZd"eejejejgejfe e dddZd#ee e d ddZd$ejejeejfeje dddZd%ee e ejeejfdddZedddZdS)&ConfigaWorks exactly like a dict but provides ways to fill it from files or special dictionaries. There are two common patterns to populate the config. Either you can fill the config from a config file:: app.config.from_pyfile('yourconfig.cfg') Or alternatively you can define the configuration options in the module that calls :meth:`from_object` or provide an import path to a module that should be loaded. It is also possible to tell it to use the same module and with that provide the configuration values just before the call:: DEBUG = True SECRET_KEY = 'development key' app.config.from_object(__name__) In both cases (loading from any Python file or loading from modules), only uppercase keys are added to the config. This makes it possible to use lowercase values in the config file for temporary values that are not added to the config or to define the config keys in the same file that implements the application. Probably the most interesting way to load configurations is from an environment variable pointing to a file:: app.config.from_envvar('YOURAPPLICATION_SETTINGS') In this case before launching the application you have to set this environment variable to the file you want to use. On Linux and OS X use the export statement:: export YOURAPPLICATION_SETTINGS='/path/to/config/file' On windows use `set` instead. :param root_path: path to which files are read relative from. When the config object is created by the application, this is the application's :attr:`~flask.Flask.root_path`. :param defaults: an optional dictionary of default values N) root_pathdefaultsrcCst||p i||_dSr)dictr r)r rrr r r r HszConfig.__init__F) variable_namesilentrcCs6tj|}|s(|rdStd|d|j||dS)aLoads a configuration from an environment variable pointing to a configuration file. This is basically just a shortcut with nicer error messages for this line of code:: app.config.from_pyfile(os.environ['YOURAPPLICATION_SETTINGS']) :param variable_name: name of the environment variable :param silent: set to ``True`` if you want silent failure for missing files. :return: bool. ``True`` if able to load config, ``False`` otherwise. 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 filer!)osenvironget RuntimeError from_pyfile)r r r!rr r r from_envvarLs  zConfig.from_envvar)filenamer!rc Cstj|j|}td}||_z4t|dd}tt | |d|j W5QRXWnXt k r}z:|r|j t jt jt jfkrWYdSd|jd|_W5d}~XYnX||d S) aUpdates the values in the config from a Python file. This function behaves as if the file was imported as module with the :meth:`from_object` function. :param filename: the filename of the config. This can either be an absolute filename or a filename relative to the root path. :param silent: set to ``True`` if you want silent failure for missing files. .. versionadded:: 0.7 `silent` parameter. rrb)modeexecF#Unable to load configuration file ()NT)r#pathjoinrtypes ModuleType__file__openr,compileread__dict__OSErrorerrnoENOENTEISDIRENOTDIRstrerror from_object)r r)r!d config_fileer r r r'ds &  zConfig.from_pyfile)r rcCs:t|trt|}t|D]}|rt||||<qdS)aUpdates the values from the given object. An object can be of one of the following two types: - a string: in this case the object with that name will be imported - an actual object reference: that object is used directly Objects are usually either modules or classes. :meth:`from_object` loads only the uppercase attributes of the module/class. A ``dict`` object will not work with :meth:`from_object` because the keys of a ``dict`` are not attributes of the ``dict`` class. Example of module-based configuration:: app.config.from_object('yourapplication.default_config') from yourapplication import default_config app.config.from_object(default_config) Nothing is done to the object before loading. If the object is a class and has ``@property`` attributes, it needs to be instantiated before being passed to this method. You should not use this function to load the actual configuration but rather configuration defaults. The actual config should be loaded with :meth:`from_pyfile` and ideally from a location not within the package because the package might be installed system wide. See :ref:`config-dev-prod` for an example of class-based configuration using :meth:`from_object`. :param obj: an import name or object N) isinstancerrdirisuppergetattr)r r keyr r r r>s  zConfig.from_object)r)loadr!rc Cstj|j|}z t|}||}W5QRXWnTtk r}z6|rb|jtjtjfkrbWYdSd|j d|_ W5d}~XYnX| |S)aUpdate the values in the config from a file that is loaded using the ``load`` parameter. The loaded data is passed to the :meth:`from_mapping` method. .. code-block:: python import toml app.config.from_file("config.toml", load=toml.load) :param filename: The path to the data file. This can be an absolute path or relative to the config root path. :param load: A callable that takes a file handle and returns a mapping of loaded data from the file. :type load: ``Callable[[Reader], Mapping]`` where ``Reader`` implements a ``read`` method. :param silent: Ignore the file if it doesn't exist. .. versionadded:: 2.0 Fr-r.N) r#r/r0rr4r8r9r:r;r= from_mapping)r r)rGr!fr rAr r r from_files  zConfig.from_filecCs6ddl}ddlm}|jdtdd|j||j|dS) a Update the values in the config from a JSON file. The loaded data is passed to the :meth:`from_mapping` method. :param filename: The path to the JSON file. This can be an absolute path or relative to the config root path. :param silent: Ignore the file if it doesn't exist. .. deprecated:: 2.0.0 Will be removed in Flask 2.1. Use :meth:`from_file` instead. This was removed early in 2.0.0, was added back in 2.0.1. .. versionadded:: 0.11 rN)jsonze'from_json' is deprecated and will be removed in Flask 2.1. Use 'from_file(path, json.load)' instead.) stacklevelr")warningsrLwarnDeprecationWarningrJrG)r r)r!rOrLr r r from_jsons zConfig.from_json)mappingkwargsrcKsFi}|dk r|||||D]\}}|r(|||<q(dS)z{Updates the config like :meth:`update` ignoring items with non-upper keys. .. versionadded:: 0.11 NT)updateitemsrD)r rTrUZmappingsrFrr r r rHs   zConfig.from_mappingT) namespace lowercasetrim_namespacercCsTi}|D]B\}}||s q |r6|t|d}n|}|rF|}|||<q |S)aiReturns a dictionary containing a subset of configuration options that match the specified namespace/prefix. Example usage:: app.config['IMAGE_STORE_TYPE'] = 'fs' app.config['IMAGE_STORE_PATH'] = '/var/app/images' app.config['IMAGE_STORE_BASE_URL'] = 'http://img.website.com' image_store_config = app.config.get_namespace('IMAGE_STORE_') The resulting dictionary `image_store_config` would look like:: { 'type': 'fs', 'path': '/var/app/images', 'base_url': 'http://img.website.com' } This is often useful when configuration options map directly to keyword arguments in functions or class constructors. :param namespace: a configuration namespace :param lowercase: a flag indicating if the keys of the resulting dictionary should be lowercase :param trim_namespace: a flag indicating if the keys of the resulting dictionary should not include the namespace .. versionadded:: 0.11 N)rW startswithlenlower)r rXrYrZrkvrFr r r get_namespaces  zConfig.get_namespace)rcCsdt|jdt|dS)N< >)typerr__repr__)r r r r re"szConfig.__repr__)N)F)F)F)F)N)TT)rrrrrrrrr boolr(r'Unionobjectr>rIOrMappingrJrSrHDictr`rer r r r rs8+* '  +r) r9r#r1typingrZwerkzeug.utilsrrrrr r r r s