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
Ejemplo n.º 6
0
    def _config_to_configparser(self, config):
        """
        Return a ConfigParser instance representing a given config dictionary.

        Args:
            config (dict): Dictionary of config key/value pairs.

        Returns:
            SafeConfigParser: SafeConfigParser instance representing config.
        """
        def get_store():
            return config['store']

        def get_day_start():
            return config['day_start'].strftime('%H:%M:%S')

        def get_fact_min_delta():
            return text_type(config['fact_min_delta'])

        def get_tmpfile_path():
            return text_type(config['tmpfile_path'])

        def get_db_engine():
            return config['db_engine']

        def get_db_path():
            return text_type(config['db_path'])

        cp_instance = SafeConfigParser()
        cp_instance.add_section('Backend')
        cp_instance.set('Backend', 'store', get_store())
        cp_instance.set('Backend', 'day_start', get_day_start())
        cp_instance.set('Backend', 'fact_min_delta', get_fact_min_delta())
        cp_instance.set('Backend', 'tmpfile_path', get_tmpfile_path())
        cp_instance.set('Backend', 'db_engine', get_db_engine())
        cp_instance.set('Backend', 'db_path', get_db_path())

        return cp_instance
Ejemplo n.º 7
0
def config_instance(request):
    """A dummy instance of ``SafeConfigParser``."""
    return SafeConfigParser()
Ejemplo n.º 8
0
def configparser_instance(request):
    """Provide a ``ConfigParser`` instance and its expected config dict."""
    config = SafeConfigParser()
    config.add_section('Backend')
    config.set('Backend', 'store', 'sqlalchemy')
    config.set('Backend', 'day_start', '05:00:00')
    config.set('Backend', 'fact_min_delta', '60')
    config.set('Backend', 'tmpfile_path', '/tmp')
    config.set('Backend', 'db_engine', 'sqlite')
    config.set('Backend', 'db_path', '/tmp/hamster.db')
    config.set('Backend', 'db_host', 'www.example.com')
    config.set('Backend', 'db_port', '22')
    config.set('Backend', 'db_name', 'hamster')
    config.set('Backend', 'db_user', 'hamster')
    config.set('Backend', 'db_password', 'hamster')

    expectation = {
        'store': text_type('sqlalchemy'),
        'day_start': datetime.datetime.strptime('05:00:00', '%H:%M:%S').time(),
        'fact_min_delta': 60,
        'tmpfile_path': text_type('/tmp'),
        'db_engine': text_type('sqlite'),
        'db_path': text_type('/tmp/hamster.db'),
        'db_host': text_type('www.example.com'),
        'db_port': 22,
        'db_name': text_type('hamster'),
        'db_user': text_type('hamster'),
        'db_password': text_type('hamster'),
    }

    return config, expectation
Ejemplo n.º 9
0
    def _config_to_configparser(self, config):
        """
        Return a ConfigParser instance representing a given config dictionary.

        Args:
            config (dict): Dictionary of config key/value pairs.

        Returns:
            SafeConfigParser: SafeConfigParser instance representing config.
        """
        def get_store():
            return config['store']

        def get_day_start():
            return config['day_start'].strftime('%H:%M:%S')

        def get_fact_min_delta():
            return text_type(config['fact_min_delta'])

        def get_tmpfile_path():
            return text_type(config['tmpfile_path'])

        def get_db_engine():
            return config['db_engine']

        def get_db_path():
            return text_type(config['db_path'])

        cp_instance = SafeConfigParser()
        cp_instance.add_section('Backend')
        cp_instance.set('Backend', 'store', get_store())
        cp_instance.set('Backend', 'day_start', get_day_start())
        cp_instance.set('Backend', 'fact_min_delta', get_fact_min_delta())
        cp_instance.set('Backend', 'tmpfile_path', get_tmpfile_path())
        cp_instance.set('Backend', 'db_engine', get_db_engine())
        cp_instance.set('Backend', 'db_path', get_db_path())

        return cp_instance
Ejemplo n.º 10
0
    def generate_config(**kwargs):
            config = SafeConfigParser()
            # Backend
            config.add_section('Backend')
            config.set('Backend', 'store', kwargs.get('store', 'sqlalchemy'))
            config.set('Backend', 'daystart', kwargs.get('daystart', '00:00:00'))
            config.set('Backend', 'fact_min_delta', kwargs.get('fact_min_delta', '60'))
            config.set('Backend', 'db_engine', kwargs.get('db_engine', 'sqlite'))
            config.set('Backend', 'db_path', kwargs.get('db_path', os.path.join(
                tmpdir.strpath, 'hamster_db.sqlite')))
            config.set('Backend', 'db_host', kwargs.get('db_host', ''))
            config.set('Backend', 'db_name', kwargs.get('db_name', ''))
            config.set('Backend', 'db_port', kwargs.get('db_port', ''))
            config.set('Backend', 'db_user', kwargs.get('db_user', '')),
            config.set('Backend', 'db_password', kwargs.get('db_password', ''))

            # Client
            config.add_section('Client')
            config.set('Client', 'unsorted_localized', kwargs.get(
                'unsorted_localized', 'Unsorted'))
            config.set('Client', 'log_level', kwargs.get('log_level', 'debug'))
            config.set('Client', 'log_console', kwargs.get('log_console', '0'))
            config.set('Client', 'log_filename', kwargs.get('log_filename', faker.file_name()))
            return config
Ejemplo n.º 11
0
def _write_config_file(file_path):
    """
    Write a default config file to the specified location.

    Returns:
        SafeConfigParser: Instace written to file.
    """
    # [FIXME]
    # This may be usefull to turn into a proper command, so users can restore to
    # factory settings easily.

    def get_db_path():
        return os.path.join(str(AppDirs.user_data_dir), 'hamster_cli.sqlite')

    def get_tmp_file_path():
        return os.path.join(str(AppDirs.user_data_dir), 'hamster_cli.fact')

    config = SafeConfigParser()

    # Backend
    config.add_section('Backend')
    config.set('Backend', 'store', 'sqlalchemy')
    config.set('Backend', 'daystart', '00:00:00')
    config.set('Backend', 'fact_min_delta', '60')
    config.set('Backend', 'db_engine', 'sqlite')
    config.set('Backend', 'db_host', '')
    config.set('Backend', 'db_port', '')
    config.set('Backend', 'db_name', '')
    config.set('Backend', 'db_path', get_db_path())
    config.set('Backend', 'db_user', '')
    config.set('Backend', 'db_password', '')

    # Client
    config.add_section('Client')
    config.set('Client', 'unsorted_localized', 'Unsorted')
    config.set('Client', 'log_level', 'debug')
    config.set('Client', 'log_console', 'False')
    config.set('Client', 'log_filename', 'hamster_cli.log')

    configfile_path = os.path.dirname(file_path)
    if not os.path.lexists(configfile_path):
        os.makedirs(configfile_path)
    with open(file_path, 'w') as fobj:
        config.write(fobj)

    return config
Ejemplo n.º 12
0
def _write_config_file(file_path):
    """
    Write a default config file to the specified location.

    Returns:
        SafeConfigParser: Instace written to file.
    """

    # [FIXME]
    # This may be usefull to turn into a proper command, so users can restore to
    # factory settings easily.

    def get_db_path():
        return os.path.join(str(AppDirs.user_data_dir), 'hamster_cli.sqlite')

    def get_tmp_file_path():
        return os.path.join(str(AppDirs.user_data_dir), 'hamster_cli.fact')

    config = SafeConfigParser()

    # Backend
    config.add_section('Backend')
    config.set('Backend', 'store', 'sqlalchemy')
    config.set('Backend', 'daystart', '00:00:00')
    config.set('Backend', 'fact_min_delta', '60')
    config.set('Backend', 'db_engine', 'sqlite')
    config.set('Backend', 'db_host', '')
    config.set('Backend', 'db_port', '')
    config.set('Backend', 'db_name', '')
    config.set('Backend', 'db_path', get_db_path())
    config.set('Backend', 'db_user', '')
    config.set('Backend', 'db_password', '')

    # Client
    config.add_section('Client')
    config.set('Client', 'unsorted_localized', 'Unsorted')
    config.set('Client', 'log_level', 'debug')
    config.set('Client', 'log_console', 'False')
    config.set('Client', 'log_filename', 'hamster_cli.log')

    configfile_path = os.path.dirname(file_path)
    if not os.path.lexists(configfile_path):
        os.makedirs(configfile_path)
    with open(file_path, 'w') as fobj:
        config.write(fobj)

    return config