def run(self) -> None: """Business logic for `refitt config get`.""" path = PATH[SITE].config for site in ('local', 'user', 'system'): if getattr(self, site) is True: path = PATH[site].config if not os.path.exists(path): raise RuntimeError(f'{path} does not exist') config = Namespace.from_local(path) if self.varpath == '.': self.print_result(config) return if '.' not in self.varpath: if self.varpath in config: self.print_result(config[self.varpath]) return else: raise RuntimeError(f'"{self.varpath}" not found in {path}') if self.varpath.startswith('.'): raise RuntimeError(f'section name cannot start with "."') section, *subsections, variable = self.varpath.split('.') if section not in config: raise RuntimeError(f'"{section}" is not a section') config_section = config[section] if subsections: subpath = f'{section}' try: for subsection in subsections: subpath += f'.{subsection}' if not isinstance(config_section[subsection], Mapping): raise RuntimeError( f'"{subpath}" not a section in {path}') else: config_section = config_section[subsection] except KeyError as error: raise RuntimeError( f'"{subpath}" not found in {path}') from error if self.expand: try: value = getattr(config_section, variable) except ValueError as error: raise RuntimeError(*error.args) from error if value is None: raise RuntimeError(f'"{variable}" not found in {path}') self.print_result(value) return if variable not in config_section: raise RuntimeError(f'"{self.varpath}" not found in {path}') self.print_result(config_section[variable])
def test_to_local(self) -> None: """Test Namespace.to_local dispatch method.""" # test round trip for ftype in FACTORIES: ns = Namespace(TEST_DICT) ns.to_local(f'{TMPDIR}/{ftype}.{ftype}') assert ns == Namespace.from_local(f'{TMPDIR}/{ftype}.{ftype}') # test not implemented with pytest.raises(NotImplementedError): ns = Namespace(TEST_DICT) ns.to_local(f'{TMPDIR}/config.special')
def update_config(site: str, data: dict) -> None: """ Extend the current configuration and commit it to disk. Parameters: site (str): Either "local", "user", or "system" data (dict): Sectioned mappable to update configuration file. Example: >>> update_config('user', { ... 'database': { ... 'user': '******' ... } ... }) """ init_config(site) new_config = Namespace.from_local(CONF_PATH[site]) new_config.update(data) new_config.to_local(CONF_PATH[site])
def update_config(site: str, data: dict) -> None: """ Extend the current configuration and commit it to disk. Args: site (str): Either "local", "user", or "system" data (dict): Sectioned mappable to update configuration file. Example: >>> update_config('user', { ... 'database': { ... 'user': '******' ... } ... }) """ path = get_site(site).config new_config = Namespace.from_local(path, ignore_if_missing=True) new_config.update(data) new_config.to_local(path)
def test_from_local(self) -> None: """Test automatic file type deduction and allow for missing files.""" # clear existing files for ftype in FACTORIES: filepath = f'{TMPDIR}/{ftype}.{ftype}' if os.path.exists(filepath): os.remove(filepath) with pytest.raises(FileNotFoundError): Namespace.from_local(f'{TMPDIR}/toml.toml') with pytest.raises(FileNotFoundError): Namespace.from_local(f'{TMPDIR}/yaml.yaml') with pytest.raises(FileNotFoundError): Namespace.from_local(f'{TMPDIR}/json.json') assert (Namespace() == Namespace.from_local(f'{TMPDIR}/toml.toml', ignore_if_missing=True) == Namespace.from_local(f'{TMPDIR}/yaml.yaml', ignore_if_missing=True) == Namespace.from_local(f'{TMPDIR}/json.json', ignore_if_missing=True)) with pytest.raises(NotImplementedError): Namespace.from_local(f'{TMPDIR}/config.special') # write all test data to local files for ftype, data in FACTORIES.items(): with open(f'{TMPDIR}/{ftype}.{ftype}', mode='w') as output: output.write(data) assert (Namespace(TEST_DICT) == Namespace.from_local(f'{TMPDIR}/toml.toml') == Namespace.from_local(f'{TMPDIR}/tml.tml') == Namespace.from_local(f'{TMPDIR}/yaml.yaml') == Namespace.from_local(f'{TMPDIR}/yml.yml') == Namespace.from_local(f'{TMPDIR}/json.json'))