1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-04 16:49:42 +00:00

First implementation of import coinfigs: brutally merging dictionary members

This commit is contained in:
Davide Laezza
2019-04-19 21:39:59 +02:00
parent 02bae593e5
commit bcf89478ba
4 changed files with 37 additions and 14 deletions

View File

@@ -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 = {}

View File

@@ -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:]) != ('__', '__')

View File

@@ -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')

View File

@@ -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 = {