1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-10 07:29:17 +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 yaml config file manager
""" """
import yaml import inspect
import os import os
import shlex import shlex
import yaml
# local import # local import
from dotdrop.dotfile import Dotfile from dotdrop.dotfile import Dotfile
from dotdrop.templategen import Templategen from dotdrop.templategen import Templategen
from dotdrop.logger import Logger from dotdrop.logger import Logger
from dotdrop.action import Action, Transform 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 from dotdrop.linktypes import LinkTypes
@@ -40,7 +42,7 @@ class Cfg:
# import keys # import keys
key_import_vars = 'import_variables' key_import_vars = 'import_variables'
key_import_actions = 'import_actions' key_import_actions = 'import_actions'
key_import_profiles = 'import_profiles' key_import_configs = 'import_configs'
# actions keys # actions keys
key_actions = 'actions' key_actions = 'actions'
@@ -277,13 +279,9 @@ class Cfg:
# parse external profiles # parse external profiles
try: 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: for config in ext_configs:
ext_config = Cfg(config) self._merge_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
except KeyError: except KeyError:
pass pass
@@ -457,6 +455,23 @@ class Cfg:
self.log.dbg('dotfiles for \"{}\": {}'.format(k, df)) self.log.dbg('dotfiles for \"{}\": {}'.format(k, df))
return True 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): def _load_ext_variables(self, paths, profile=None):
"""load external variables""" """load external variables"""
variables = {} variables = {}

View File

@@ -130,3 +130,11 @@ def must_ignore(paths, ignores, debug=False):
LOG.dbg('ignore \"{}\" match: {}'.format(i, p)) LOG.dbg('ignore \"{}\" match: {}'.format(i, p))
return True return True
return False 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', def create_fake_config(directory, configname='config.yaml',
dotpath='dotfiles', backup=True, create=True, dotpath='dotfiles', backup=True, create=True,
import_profiles=()): import_configs=()):
"""Create a fake config file""" """Create a fake config file"""
path = os.path.join(directory, configname) path = os.path.join(directory, configname)
workdir = os.path.join(directory, 'workdir') 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(' create: {}\n'.format(str(create)))
f.write(' dotpath: {}\n'.format(dotpath)) f.write(' dotpath: {}\n'.format(dotpath))
f.write(' workdir: {}\n'.format(workdir)) f.write(' workdir: {}\n'.format(workdir))
if import_profiles: if import_configs:
f.write(' import_profiles:\n') f.write(' import_configs:\n')
f.write(yaml_dashed_list(import_profiles, 4)) f.write(yaml_dashed_list(import_configs, 4))
f.write('dotfiles:\n') f.write('dotfiles:\n')
f.write('profiles:\n') f.write('profiles:\n')
f.write('actions:\n') f.write('actions:\n')

View File

@@ -217,7 +217,7 @@ profiles:
dotpath=self.CONFIG_DOTPATH, dotpath=self.CONFIG_DOTPATH,
backup=self.CONFIG_BACKUP, backup=self.CONFIG_BACKUP,
create=self.CONFIG_CREATE, create=self.CONFIG_CREATE,
import_profiles=(imported,)) import_configs=(imported,))
# keys # keys
keys = { keys = {