Ejemplo n.º 1
0
def test_upgrade_to_latest(tmpdir):
    # Copy logme.ini to tmpdir
    logme_file = current_dir / 'logme.ini'
    tmp_logme = tmpdir.join('logme.ini')
    shutil.copyfile(logme_file, tmp_logme)

    upgrade_to_latest(tmp_logme)

    # Validate upgraded file
    config = ConfigParser.from_files(tmp_logme)
    config_before = ConfigParser.from_files(logme_file)

    assert set(config.sections()) == set(NONLOGGER_CONFIGS +
                                         config_before.sections())

    # Validate the latest version has not been changed
    assert config.to_dict(section='latest') == \
           config_before.to_dict(section='latest') == \
           ver11_config

    for i in config.sections():
        if i == 'colors':
            continue

        conf_dict = config.to_dict(section=i)

        for k, v in conf_dict.items():
            if isinstance(v, dict):
                assert v.get('type') is not None

            assert all(c.islower() for c in k)
Ejemplo n.º 2
0
    def test_init_from_files_empty(self, tmpdir):
        file = tmpdir.join('foo.ini')
        file.open('w').close()

        assert Path(file).exists()

        with pytest.raises(InvalidConfig):
            ConfigParser.from_files(file)
Ejemplo n.º 3
0
    def test_remove(self, tmp_config):
        self.runner.invoke(cli, ['set', 'to_remove="command to be removed"'])

        config = ConfigParser.from_files(tmp_config)
        assert 'to_remove' in config.options('fetchme')

        self.runner.invoke(cli, ['remove', 'to_remove'])

        config_removed = ConfigParser.from_files(tmp_config)
        assert 'to_remove' not in config_removed.options('fetchme')
Ejemplo n.º 4
0
def test_upgrade_colors_config_not_changed(tmpdir):
    local_logme_file = current_dir / 'logme_with_color.ini'
    tmp_logme = tmpdir.join('logme.ini')

    shutil.copyfile(local_logme_file, tmp_logme)

    upgrade_to_latest(tmp_logme)

    config_before = ConfigParser.from_files(local_logme_file)
    config_after = ConfigParser.from_files(tmp_logme)

    assert config_before.items('colors') == config_after.items('colors')
Ejemplo n.º 5
0
def set(ctx, content, override):
    """
    Command for setting an alias to a long command

    :argument: content: key=value, e.g: ssh="ssh -i /path/to/my/key/file [email protected]"

    :option: --override, -o: option for overriding existing key

    :raises: ValueError, if the alias with provided name has already being set in .fetchmerc file
    """
    name, val = content.split('=', 1)

    if name in DEFAULT_COMMANDS:
        raise ValueError(F"'{name}' is a default command, and it cannot be set!")

    config_path = _get_config_path()
    config = ConfigParser.from_files(config_path)

    try:
        config.get('fetchme', name)
        if not override:
            raise ValueError(f"'{name}' already exist! use --override, or -o flag to override")
    except NoOptionError:
        pass

    config['fetchme'][name] = val

    with config_path.open('w') as file:
        config.write(file)
Ejemplo n.º 6
0
    def test_init_file_change(self, tmpdir, option, key, expected):

        self.runner.invoke(cli, ['init', '-p', tmpdir] + option)

        conf = ConfigParser.from_files(tmpdir.join('logme.ini'))

        assert conf.get(*key) == expected
Ejemplo n.º 7
0
    def test_remove_command(self, tmpdir):

        with cd(tmpdir):
            self.runner.invoke(cli, ['init'])
            self.runner.invoke(cli, ['add', 'test'])

            config_path = tmpdir.join('logme.ini')
            config_before = ConfigParser.from_files(config_path)

            assert set(config_before.sections()) == {'colors', 'logme', 'test'}

            result = self.runner.invoke(cli, ['remove', 'test'])
            config_after = ConfigParser.from_files(config_path)

            assert result.exit_code == 0
            assert config_after.sections() == ['colors', 'logme']
Ejemplo n.º 8
0
def init(ctx, project_root, override, mkdir, level, formatter, log_path):
    """
    Entry point.

    Command to set up a logme config file with master logger configuration

    """
    conf_content = get_color_tpl()
    master_logging_config = get_tpl('logme',
                                    level=level,
                                    formatter=formatter,
                                    filename=log_path)
    conf_content.update(master_logging_config)

    config = ConfigParser.from_dict(conf_content)

    abs_path = Path(project_root).resolve()
    conf_location = abs_path.joinpath('logme.ini')

    if not abs_path.exists():
        if not mkdir:
            raise NotADirectoryError(
                f"{abs_path.parent.resolve() / project_root} does not exist. If you'd "
                f"like to make the directory, please use '-mk' flag.")
        else:
            abs_path.mkdir(parents=True, exist_ok=True)

    if conf_location.exists() and not override:
        raise LogmeError(f"logme.ini already exists at {conf_location}")

    with conf_location.open('w') as conf:
        config.write(conf)
Ejemplo n.º 9
0
    def test_set_override_existing(self, tmp_config):
        self.runner.invoke(cli, ['set', 'blah=hello'])
        result = self.runner.invoke(cli, ["set", 'blah=overriden value', '-o'])

        config = ConfigParser.from_files(tmp_config)

        assert result.exit_code == 0
        assert config.get('fetchme', 'blah') == 'overriden value'
Ejemplo n.º 10
0
    def test_set(self, tmp_config):
        result = self.runner.invoke(cli, ['set', 'blah=hello'])

        config = ConfigParser.from_files(tmp_config)

        assert result.exit_code == 0
        assert config.has_option('fetchme', 'blah')
        assert config.get('fetchme', 'blah') == 'hello'
Ejemplo n.º 11
0
 def test_to_dict_with_datefmt(self):
     config = ConfigParser.from_files(data.config_path)
     expected = {
         'formatter': {
             'fmt': '{asctime} - {name} - {levelname} - {message}',
             'datefmt': '%Y/%m/%d',
             'style': '{'
         },
         'Active': True
     }
     assert config.to_dict(section='date_format') == expected
Ejemplo n.º 12
0
def test_get_command_func(tmp_config, mock_subprocess):
    config = ConfigParser.from_files(tmp_config)
    subcommand = _get_command_func('test_command', config)

    assert inspect.isfunction(subcommand)

    subcommand(None)

    mock_subprocess.assert_called_with(['ls', '-al'])

    assert subcommand.__doc__.strip(
    ) == "Execute command alias 'test_command'; command: ls -al"
Ejemplo n.º 13
0
def _set_commands(click_group: click.core.Group):
    """
    Set commands to click group based on the options in .fetchmerc file
    """
    config_path = _get_config_path()
    config = ConfigParser.from_files(config_path)

    option_names = config.options('fetchme')

    for i in option_names:
        func = _get_command_func(i, config)

        click_group.command(name=i)(click.pass_context(func))
Ejemplo n.º 14
0
    def test_add_command(self, tmpdir):

        with cd(tmpdir):
            self.runner.invoke(cli, ['init'])
            result = self.runner.invoke(cli, ['add', 'blah'])

            config_path = tmpdir.join('logme.ini')
            config = ConfigParser.from_files(config_path)

            assert result.exit_code == 0
            assert Path(config_path).is_file()

            assert set(config.sections()) == {'colors', 'logme', 'blah'}
Ejemplo n.º 15
0
def upgrade_to_latest(config_path: Union[str, Path]):
    """
    upgrade the existing logme.ini file to latest version
    *Will write to existing file*

    :param config_path: logme.ini file
    """
    # config = read_config(config_path)
    config_dict = ConfigParser.from_files(config_path).to_dict()

    config_dict_updated = {}

    _upgrade_with_color_config(config_dict, config_dict_updated)

    for k, v in config_dict.items():
        if k not in NONLOGGER_CONFIGS:
            updated = _upgrade_logging_config_section(v)

            config_dict_updated[k] = updated

    new_conf = ConfigParser.from_dict(config_dict_updated)

    with open(config_path, 'w') as file:
        new_conf.write(file)
Ejemplo n.º 16
0
    def test_init_chained_options(self, tmpdir):

        tmp = tmpdir.join('my_project')

        self.runner.invoke(
            cli,
            ['init', '-p', tmp, '-mk', '-lp',
             tmp.join('var/log/dummy.log')])

        config = ConfigParser.from_files(tmp.join('logme.ini'))

        fh_conf = config.to_dict(section='logme', option='file')

        assert fh_conf['filename'] == tmp.join('var/log/dummy.log')
        assert set(fh_conf.keys()) == {'active', 'level', 'filename', 'type'}
Ejemplo n.º 17
0
def validate_conf(name: str, ini_file_path: Union[str, Path]):
    """
    Helper function for 'logme init' command,
    ensure the logger name passed in does not already exist in the ini file

    :param name: name of the section to be added
    :param ini_file_path: path of the logme.ini file
    """
    config = ConfigParser.from_files(ini_file_path)

    if config.has_section(name):
        raise LogmeError(
            f"'{name}' logging config already exists in config file: {ini_file_path}"
        )
    elif not config.has_section('logme'):
        raise LogmeError(f"{ini_file_path} is not a valid logme.ini file")
Ejemplo n.º 18
0
def add(ctx, project_root, name, level, formatter, log_path):
    """
    Command for adding a logger configuration in a logme.ini file. Assuming logme.ini exists
    """
    with ensure_conf_exist(project_root) as logme_conf:

        # check if section already exist
        validate_conf(name, logme_conf)

        conf_content = get_tpl(name,
                               level=level,
                               formatter=formatter,
                               filename=log_path)
        config = ConfigParser.from_dict(conf_content)

        with logme_conf.open('a') as conf:
            config.write(conf)
Ejemplo n.º 19
0
def test_color_config_none_exist(tmpdir):
    """
    Ensure color config returns None if none existent
    """
    logme_file = tmpdir.join('logme.ini')

    logger_conifg = get_logger_config(__file__)
    config_dict = {'logme': logger_conifg}

    config = ConfigParser.from_dict(config_dict)

    with open(logme_file, 'w') as file:
        config.write(file)

    color_config = get_color_config(logme_file)

    assert color_config is None
Ejemplo n.º 20
0
    def test_init(self, tmpdir, file_path, cmd_args):

        expected_file = Path(tmpdir.join(file_path))

        with cd(tmpdir):
            result = self.runner.invoke(cli, cmd_args)

            assert result.exit_code == 0
            assert expected_file.is_file()

            conf = ConfigParser.from_files(expected_file)
            assert conf.sections() == ['colors', 'logme']

            # Assert the first section is the color config
            with open(expected_file) as file:
                line = file.readline()
                assert line == '[colors]\n'
Ejemplo n.º 21
0
def get_config_content(caller_file_path: Union[str, Path], name: str) -> dict:
    """
    Get the config section as a dictionary

    :param caller_file_path: file path of the caller, __file__
    :param name: the section name in an .ini file

    :return: configuration as dict
    """

    init_file_path = get_ini_file_path(caller_file_path)

    config = ConfigParser.from_files(init_file_path)

    try:
        return config.to_dict(section=name)
    except NoSectionError:
        raise NoSectionError(
            f"'{name}' is not a valid configuration in {init_file_path}")
Ejemplo n.º 22
0
def remove(ctx, name):
    """
    Commands for removing a set alias

    :param: name: The name of the alias, defined in .fetchmerc file

    :raises: ValueError: if such alias does not exist
    """
    config_path = _get_config_path()
    config = ConfigParser.from_files(config_path)

    try:
        config.get('fetchme', name)
    except NoOptionError:
        raise InvalidConfigOption(f"'{name}' is not a valid config option. Avalables: {config.options('fetchme')}")

    config.remove_option('fetchme', name)

    with config_path.open('w') as file:
        config.write(file)
Ejemplo n.º 23
0
def _get_command_func(name: str, config: ConfigParser) -> Callable:
    """
    Get the stub function for commands.
    allowing the click group to automatically take commands from '.fetchmerc' options

    :param name: name of the option in .fetchmerc
    :param config: configuration, ConfigParser object

    :return: callable function
    """
    command = config.get('fetchme', name)

    @doc_parametrize(name=name, command=command)
    def subcommand(ctx):
        """
        Execute command alias '{name}'; command: {command}
        """
        command_fields = command.split(' ')

        subprocess.call(command_fields)

    return subcommand
Ejemplo n.º 24
0
def remove(ctx, name, project_root):
    """
    Command for removing a logger configuration in a logme.ini file.
    logme configuration cannot be removed
    """
    none_removables = {
        'logme':
        "'logme' master configuration cannot be removed!",
        'colors':
        "'colors' configuration cannot be removed! To remove color "
        "logging, set all color values to 'None'",
    }

    if none_removables.get(name):
        raise LogmeError(none_removables[name])

    with ensure_conf_exist(project_root) as logme_conf:

        config = ConfigParser.from_files(logme_conf)
        config.remove_section(name)

        with logme_conf.open('w+') as conf:
            config.write(conf)
Ejemplo n.º 25
0
    def test_to_dict_raise(self):
        config = ConfigParser.from_files(data.config_path)

        with pytest.raises(ValueError):
            config.to_dict(option='profile')
Ejemplo n.º 26
0
    def test_to_dict(self, parse_args, expected):
        config = ConfigParser.from_files(data.config_path)

        assert config.to_dict(**parse_args) == expected
Ejemplo n.º 27
0
    def test_init_from_dict(self):
        config = ConfigParser.from_dict(data.config_dict)

        assert set(config.sections()) == {
            'my_config', 'Fish_Profiles', 'date_format'
        }
Ejemplo n.º 28
0
    def test_init_from_files_raise(self, tmpdir):
        ini_path = tmpdir.join('test.ini')

        with pytest.raises(InvalidConfig):
            ConfigParser.from_files(ini_path)
Ejemplo n.º 29
0
 def test_init_from_files_non_exist(self):
     with pytest.raises(InvalidConfig):
         ConfigParser.from_files('blah.ini')
Ejemplo n.º 30
0
 def test_init_from_files(self, filenames):
     config = ConfigParser.from_files(filenames)
     assert set(config.sections()) == {
         'my_config', 'Fish_Profiles', 'date_format'
     }