diff --git a/dotdrop/config.py b/dotdrop/config.py index 9ab5588..ab0e204 100644 --- a/dotdrop/config.py +++ b/dotdrop/config.py @@ -5,16 +5,18 @@ Copyright (c) 2017, deadc0de6 yaml config file manager """ -import yaml +import inspect import os import shlex +import yaml + # local import from dotdrop.dotfile import Dotfile from dotdrop.templategen import Templategen from dotdrop.logger import Logger from dotdrop.action import Action, Transform -from dotdrop.utils import strip_home, shell +from dotdrop.utils import is_dict, is_not_magic, strip_home, shell from dotdrop.linktypes import LinkTypes @@ -40,7 +42,7 @@ class Cfg: # import keys key_import_vars = 'import_variables' key_import_actions = 'import_actions' - key_import_profiles = 'import_profiles' + key_import_configs = 'import_configs' # actions keys key_actions = 'actions' @@ -277,13 +279,9 @@ class Cfg: # parse external profiles try: - ext_configs = self.lnk_settings[self.key_import_profiles] + ext_configs = self.lnk_settings[self.key_import_configs] or () for config in ext_configs: - ext_config = Cfg(config) - self.dotfiles.update(ext_config.dotfiles) - self.lnk_profiles.update(ext_config.lnk_profiles) - self.prodots.update(ext_config.prodots) - # need variables, actions and so on + self._merge_cfg(config) except KeyError: pass @@ -457,6 +455,23 @@ class Cfg: self.log.dbg('dotfiles for \"{}\": {}'.format(k, df)) return True + def _merge_cfg(self, config_path): + try: + ext_config = Cfg(self._abs_path(config_path)) + except ValueError: + raise ValueError( + 'external config file not found: {}'.format(config_path)) + + ext_members = ( + (name, member) + for name, member in inspect.getmembers(ext_config, is_dict) + if name != 'content' and is_not_magic(name) + ) + for name, ext_member in ext_members: + self_member = getattr(self, name, {}) + ext_member.update(self_member) + setattr(self, name, ext_member) + def _load_ext_variables(self, paths, profile=None): """load external variables""" variables = {} diff --git a/dotdrop/utils.py b/dotdrop/utils.py index 70b1a61..81404e8 100644 --- a/dotdrop/utils.py +++ b/dotdrop/utils.py @@ -130,3 +130,11 @@ def must_ignore(paths, ignores, debug=False): LOG.dbg('ignore \"{}\" match: {}'.format(i, p)) return True return False + + +def is_dict(obj): + return isinstance(obj, dict) + + +def is_not_magic(name): + return (name[0:2], name[-2:]) != ('__', '__') diff --git a/tests/helpers.py b/tests/helpers.py index 3309729..c06854b 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -157,7 +157,7 @@ def yaml_dashed_list(items, indent=0): def create_fake_config(directory, configname='config.yaml', dotpath='dotfiles', backup=True, create=True, - import_profiles=()): + import_configs=()): """Create a fake config file""" path = os.path.join(directory, configname) workdir = os.path.join(directory, 'workdir') @@ -167,9 +167,9 @@ def create_fake_config(directory, configname='config.yaml', f.write(' create: {}\n'.format(str(create))) f.write(' dotpath: {}\n'.format(dotpath)) f.write(' workdir: {}\n'.format(workdir)) - if import_profiles: - f.write(' import_profiles:\n') - f.write(yaml_dashed_list(import_profiles, 4)) + if import_configs: + f.write(' import_configs:\n') + f.write(yaml_dashed_list(import_configs, 4)) f.write('dotfiles:\n') f.write('profiles:\n') f.write('actions:\n') diff --git a/tests/test_config.py b/tests/test_config.py index 86871fb..b331ab0 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -217,7 +217,7 @@ profiles: dotpath=self.CONFIG_DOTPATH, backup=self.CONFIG_BACKUP, create=self.CONFIG_CREATE, - import_profiles=(imported,)) + import_configs=(imported,)) # keys keys = {