def _env_init()

in dyno/app/__init__.py [0:0]


def _env_init(app_env):
    """
    Select the proper configuration class from the `cfg` module and return it
    for use in application initlization.

    Note
    ----
    This function has the side effect of printing to the console in some cases.

    Parameters
    ----------
    app_env : str
        An application environment to run in. Must be either `test`, `dev` or `prod`.
        Otherwise, an exception will be thrown.

    Returns
    -------
    obj
        An object suitable for use in configuring Flask. Typically used with Flask().config.from_object()

    Raises
    ------
    Exception
        If the type of environment is not recognized, an exception of type Exception will be raised.

    Examples
    --------
    Create a Flask object and give it a configuration

    >>> app = Flask(__name__)
    >>> config_class = _env_init(app_env)
    >>> app.config.from_object(config_class)

    """
    if app_env == 'test':
        print('\n\n\033[1;33;40m ** Starting with test configuration ** \033[m \n\n')
        return import_string('dyno.app.cfg.TestingConfig')()

    elif app_env == 'dev':
        print('\n\n\033[1;33;40m ** Starting with development configuration ** \033[m \n\n')
        return import_string('dyno.app.cfg.DevelopmentConfig')()

    elif app_env == 'prod':
        return import_string('dyno.app.cfg.ProductionConfig')()
    else:
        raise Exception('Must specify valid app_env when calling create_app()')