Module config

Expand source code
# Copyright (c) 2016-2021 InSeven Limited
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from flask import Config

import os.path

SERVICE_DIR = os.path.dirname(os.path.abspath(__file__))
SITE_DIR = os.path.dirname(SERVICE_DIR)


class keys(object):
    ROOT = "ROOT"


class Configuration(Config):
    ROOT = SITE_DIR

Classes

class Configuration (root_path: str, defaults: Union[dict, NoneType] = None)

Works 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

Expand source code
class Configuration(Config):
    ROOT = SITE_DIR

Ancestors

  • flask.config.Config
  • builtins.dict

Class variables

var ROOT
class keys
Expand source code
class keys(object):
    ROOT = "ROOT"

Class variables

var ROOT