Ejemplo n.º 1
0
def get_config_instance(fallback_config_instance,
                        app_name=DEFAULT_APP_NAME,
                        file_name=DEFAULT_CONFIG_FILENAME):
    """
    Either retrieve a ``SafeConfigParser`` instance from disk of create a fallback config.

    If we can not find a config file under its expected location, we trigger creation
    of a new default file. Either way a ``SafeConfigParser`` instance is returned.

    Args:
        fallback_config_instance: Default config instance to be written to disk if none
            is present.
        app_name (text_type, optional): Name of the application, defaults to
        ``'projecthamster``. Allows you to use your own application specific
        namespace if you wish.
        file_name (text_type, optional): Name of the config file. Defaults to
        ``config.conf``.

    Returns:
        SafeConfigParser: Either the config loaded from file or an instance representing
            the content of our newly creating default config.
    """
    config = SafeConfigParser()
    path = get_config_path(app_name)
    if not config.read(path):
        config = write_config_file(app_name,
                                   fallback_config_instance,
                                   file_name=file_name)
    return config
Ejemplo n.º 2
0
def get_config_instance(fallback_config_instance, app_name=DEFAULT_APP_NAME,
        file_name=DEFAULT_CONFIG_FILENAME):
    """
    Either retrieve a ``SafeConfigParser`` instance from disk of create a fallback config.

    If we can not find a config file under its expected location, we trigger creation
    of a new default file. Either way a ``SafeConfigParser`` instance is returned.

    Args:
        fallback_config_instance: Default config instance to be written to disk if none
            is present.
        app_name (text_type, optional): Name of the application, defaults to
        ``'projecthamster``. Allows you to use your own application specific
        namespace if you wish.
        file_name (text_type, optional): Name of the config file. Defaults to
        ``config.conf``.

    Returns:
        SafeConfigParser: Either the config loaded from file or an instance representing
            the content of our newly creating default config.
    """
    config = SafeConfigParser()
    path = get_config_path(app_name)
    if not config.read(path):
        config = write_config_file(app_name, fallback_config_instance, file_name=file_name)
    return config
Ejemplo n.º 3
0
def get_config_instance(fallback_config_instance, app_name, file_name):
    """Patched version of ``hamster-lib`` helper function until it get fixed upstream."""
    from hamster_lib.helpers import config_helpers
    from backports.configparser import SafeConfigParser
    config = SafeConfigParser()
    path = config_helpers.get_config_path(app_name, file_name)
    existing_config = config.read(path)
    if not existing_config:
        config = config_helpers.write_config_file(fallback_config_instance, app_name,
                                                  file_name=file_name)
    return config
Ejemplo n.º 4
0
def _get_config_instance():
    """
    Return a SafeConfigParser instance.

    If we can not find a config file under its expected location, we trigger creation
    of a new default file and return its instance.

    Returns:
        SafeConfigParser: Either the config loaded from file or an instance representing
            the content of our newly creating default config.
    """
    config = SafeConfigParser()
    configfile_path = _get_config_path()
    if not config.read(configfile_path):
        click.echo(_("No valid config file found. Trying to create a new default config"
                     " at: '{}'.".format(configfile_path)))
        config = _write_config_file(configfile_path)
        click.echo(_("A new default config file has been successfully created."))
    return config
Ejemplo n.º 5
0
def _get_config_instance():
    """
    Return a SafeConfigParser instance.

    If we can not find a config file under its expected location, we trigger creation
    of a new default file and return its instance.

    Returns:
        SafeConfigParser: Either the config loaded from file or an instance representing
            the content of our newly creating default config.
    """
    config = SafeConfigParser()
    configfile_path = _get_config_path()
    if not config.read(configfile_path):
        click.echo(
            _("No valid config file found. Trying to create a new default config"
              " at: '{}'.".format(configfile_path)))
        config = _write_config_file(configfile_path)
        click.echo(
            _("A new default config file has been successfully created."))
    return config