From 33dd19a5339476ecbc5c616a879f32648b437381 Mon Sep 17 00:00:00 2001 From: InsanePrawn Date: Sat, 9 Oct 2021 22:01:11 +0200 Subject: [PATCH] Add `config init` and `config profile init` --- config.py | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 95 insertions(+), 3 deletions(-) diff --git a/config.py b/config.py index 4fb6a40..9d37cef 100644 --- a/config.py +++ b/config.py @@ -43,6 +43,7 @@ CONFIG_DEFAULTS = { 'default': deepcopy(PROFILE_DEFAULTS), }, } +CONFIG_SECTIONS = list(CONFIG_DEFAULTS.keys()) CONFIG_RUNTIME_DEFAULTS = { 'verbose': False, @@ -286,15 +287,26 @@ class ConfigStateHolder: return os.path.join(self.get_path('packages'), arch) def dump(self) -> str: + """dump toml representation of `self.file`""" dump_toml(self.file) def write(self, path=None): + """write toml representation of `self.file` to `path`""" if path is None: path = self.runtime['config_file'] os.makedirs(os.path.dirname(path), exist_ok=True) dump_file(path, self.file) logging.info(f'Created config file at {path}') + def invalidate_profile_cache(self): + """Clear the profile cache (usually after modification)""" + self._profile_cache = None + + def update(self, config_fragment: dict[str, dict]): + if 'profiles' in config_fragment and self.file['profiles'] != config_fragment['profiles']: + self.invalidate_profile_cache() + self.file = merge_configs(config_fragment, conf_base=self.file) + config = ConfigStateHolder(file_conf_base=CONFIG_DEFAULTS) @@ -311,11 +323,91 @@ def cmd_config(): pass +noninterace_flag = click.option('--non-interactive', is_flag=True) +noop_flag = click.option('--noop', '-n', is_flag=True) + + @cmd_config.command(name='init') -def cmd_init(): +@noninterace_flag +@noop_flag +@click.option( + '--sections', + '-s', + multiple=True, + type=click.Choice(CONFIG_SECTIONS), + default=CONFIG_SECTIONS, + show_choices=True, +) +def cmd_config_init(sections: list[str] = CONFIG_SECTIONS, non_interactive: bool = False, noop: bool = False): """Initialize the config file""" - # TODO - config.write() + if not non_interactive: + results = {} + for section in set(sections) - set(['profiles']): + results[section] = {} + for key, current in config.file[section].items(): + result = config_prompt(key, current, type(current)) + if result: + print(f'{key} = {result}') + results[section][key] = result + + if 'profiles' in sections: + cmd_profile_init.callback(config.file['profiles']['current'], noop=noop, non_interactive=non_interactive) + pass + config.update(results) + if not noop: + if not click.confirm(f'Do you want to save your changes to {config.runtime["config_file"]}?'): + return + + if not noop: + config.write() + else: + logging.info('--noop passed, not writing to file!') + + +@cmd_config.group(name='profile') +def cmd_profile(): + """Manage config profiles""" + pass + + +def list_to_comma_str(l: list[str]) -> str: + return ','.join(l if l else []) + + +def comma_str_to_list(s: str) -> list[str]: + return [a for a in s.split(',') if a] + + +def config_prompt(key, default, type): + if type == list: + default = list_to_comma_str(default) + result = comma_str_to_list(click.prompt(key.capitalize(), default=default, show_default=True)) + else: + default = '' if not default else default + result = click.prompt(key.capitalize(), type=str, default=default, show_default=True) + + return result + + +@cmd_profile.command(name='init') +@noninterace_flag +@noop_flag +@click.argument('name', required=False) +def cmd_profile_init(name: str = None, non_interactive: bool = False, noop: bool = False): + """Create or edit a profile""" + profile = {key: None for key in PROFILE_DEFAULTS.keys()} + # don't use get_profile() here because we need the sparse profile + if name in config.file['profiles']: + profile |= config.file['profiles'][name] + else: + logging.info(f"Profile {name} doesn't exist yet, creating new profile.") + + logging.info(f'Configuring profile "{name}"') + for key, current in profile.items(): + result = config_prompt(key, profile[key], type(PROFILE_DEFAULTS[key])) + if result: + print(f'{key} = {result}') + profile[key] = result # temporary demo