1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-12 23:20:17 +00:00
This commit is contained in:
deadc0de6
2021-04-29 20:28:25 +02:00
parent c80ea1a4f2
commit d3a9aea44f
3 changed files with 133 additions and 104 deletions

View File

@@ -16,7 +16,7 @@ from dotdrop.settings import Settings
from dotdrop.profile import Profile from dotdrop.profile import Profile
from dotdrop.action import Action, Transform from dotdrop.action import Action, Transform
from dotdrop.logger import Logger from dotdrop.logger import Logger
from dotdrop.utils import strip_home from dotdrop.utils import strip_home, debug_list, debug_dict
from dotdrop.exceptions import UndefinedException from dotdrop.exceptions import UndefinedException
@@ -217,33 +217,27 @@ class CfgAggregator:
# dotfiles # dotfiles
self.dotfiles = Dotfile.parse_dict(self.cfgyaml.dotfiles) self.dotfiles = Dotfile.parse_dict(self.cfgyaml.dotfiles)
if self.debug: debug_list('dotfiles', self.dotfiles, self.debug)
self._debug_list('dotfiles', self.dotfiles)
# profiles # profiles
self.profiles = Profile.parse_dict(self.cfgyaml.profiles) self.profiles = Profile.parse_dict(self.cfgyaml.profiles)
if self.debug: debug_list('profiles', self.profiles, self.debug)
self._debug_list('profiles', self.profiles)
# actions # actions
self.actions = Action.parse_dict(self.cfgyaml.actions) self.actions = Action.parse_dict(self.cfgyaml.actions)
if self.debug: debug_list('actions', self.actions, self.debug)
self._debug_list('actions', self.actions)
# trans_r # trans_r
self.trans_r = Transform.parse_dict(self.cfgyaml.trans_r) self.trans_r = Transform.parse_dict(self.cfgyaml.trans_r)
if self.debug: debug_list('trans_r', self.trans_r, self.debug)
self._debug_list('trans_r', self.trans_r)
# trans_w # trans_w
self.trans_w = Transform.parse_dict(self.cfgyaml.trans_w) self.trans_w = Transform.parse_dict(self.cfgyaml.trans_w)
if self.debug: debug_list('trans_w', self.trans_w, self.debug)
self._debug_list('trans_w', self.trans_w)
# variables # variables
self.variables = self.cfgyaml.variables self.variables = self.cfgyaml.variables
if self.debug: debug_dict('variables', self.variables, self.debug)
self._debug_dict('variables', self.variables)
# patch dotfiles in profiles # patch dotfiles in profiles
self._patch_keys_to_objs(self.profiles, self._patch_keys_to_objs(self.profiles,
@@ -442,19 +436,3 @@ class CfgAggregator:
return next(x for x in self.trans_w if x.key == key) return next(x for x in self.trans_w if x.key == key)
except StopIteration: except StopIteration:
return None return None
def _debug_list(self, title, elems):
"""pretty print list"""
if not self.debug:
return
self.log.dbg('{}:'.format(title))
for elem in elems:
self.log.dbg('\t- {}'.format(elem))
def _debug_dict(self, title, elems):
"""pretty print dict"""
if not self.debug:
return
self.log.dbg('{}:'.format(title))
for k, val in elems.items():
self.log.dbg('\t- \"{}\": {}'.format(k, val))

View File

@@ -5,6 +5,8 @@ Copyright (c) 2017, deadc0de6
stores all options to use across dotdrop stores all options to use across dotdrop
""" """
# pylint: disable=W0201
import os import os
import sys import sys
import socket import socket
@@ -16,7 +18,7 @@ from dotdrop.linktypes import LinkTypes
from dotdrop.logger import Logger from dotdrop.logger import Logger
from dotdrop.cfg_aggregator import CfgAggregator as Cfg from dotdrop.cfg_aggregator import CfgAggregator as Cfg
from dotdrop.action import Action from dotdrop.action import Action
from dotdrop.utils import uniq_list from dotdrop.utils import uniq_list, debug_list, debug_dict
from dotdrop.exceptions import YamlException from dotdrop.exceptions import YamlException
ENV_PROFILE = 'DOTDROP_PROFILE' ENV_PROFILE = 'DOTDROP_PROFILE'
@@ -97,25 +99,38 @@ Options:
class AttrMonitor: class AttrMonitor:
"""monitor attribute setter"""
_set_attr_err = False _set_attr_err = False
# pylint: disable=W0235
def __setattr__(self, key, value): def __setattr__(self, key, value):
"""monitor attribute setting""" """monitor attribute setting"""
if not hasattr(self, key) and self._set_attr_err: super().__setattr__(key, value)
self._attr_change(key) # pylint: enable=W0235
super(AttrMonitor, self).__setattr__(key, value)
def _attr_set(self, attr): def _attr_set(self, attr):
"""do something when unexistent attr is set""" """do something when unexistent attr is set"""
pass
class Options(AttrMonitor): class Options(AttrMonitor):
"""dotdrop options manager"""
def __init__(self, args=None): def __init__(self, args=None):
"""constructor """constructor
@args: argument dictionary (if None use sys) @args: argument dictionary (if None use sys)
""" """
# attributes gotten from self.conf.get_settings()
self.banner = None
self.showdiff = None
self.default_actions = []
self.instignore = None
self.force_chmod = None
self.cmpignore = None
self.impignore = None
self.upignore = None
self.link_on_import = None
# args parsing
self.args = {} self.args = {}
if not args: if not args:
self.args = docopt(USAGE, version=VERSION) self.args = docopt(USAGE, version=VERSION)
@@ -149,6 +164,7 @@ class Options(AttrMonitor):
# start monitoring for bad attribute # start monitoring for bad attribute
self._set_attr_err = True self._set_attr_err = True
# pylint: disable=R0911
def _get_config_path(self): def _get_config_path(self):
"""get the config path""" """get the config path"""
# cli provided # cli provided
@@ -187,6 +203,7 @@ class Options(AttrMonitor):
return path return path
return '' return ''
# pylint: enable=R0911
def _header(self): def _header(self):
"""display the header""" """display the header"""
@@ -198,9 +215,73 @@ class Options(AttrMonitor):
self.conf = Cfg(self.confpath, self.profile, debug=self.debug, self.conf = Cfg(self.confpath, self.profile, debug=self.debug,
dry=self.dry) dry=self.dry)
# transform the config settings to self attribute # transform the config settings to self attribute
self._debug_dict('effective settings', self.conf.get_settings()) debug_dict('effective settings', self.conf.get_settings(), self.debug)
for k, v in self.conf.get_settings().items(): for k, val in self.conf.get_settings().items():
setattr(self, k, v) setattr(self, k, val)
def _apply_args_files(self):
"""files specifics"""
self.files_templateonly = self.args['--template']
self.files_grepable = self.args['--grepable']
def _apply_args_install(self):
"""install specifics"""
self.install_force_action = self.args['--force-actions']
self.install_temporary = self.args['--temp']
self.install_keys = self.args['<key>']
self.install_diff = not self.args['--nodiff']
self.install_showdiff = self.showdiff or self.args['--showdiff']
self.install_backup_suffix = BACKUP_SUFFIX
self.install_default_actions_pre = [a for a in self.default_actions
if a.kind == Action.pre]
self.install_default_actions_post = [a for a in self.default_actions
if a.kind == Action.post]
self.install_ignore = self.instignore
self.install_force_chmod = self.force_chmod
def _apply_args_compare(self):
"""compare specifics"""
self.compare_focus = self.args['--file']
self.compare_ignore = self.args['--ignore']
self.compare_ignore.extend(self.cmpignore)
self.compare_ignore.append('*{}'.format(self.install_backup_suffix))
self.compare_ignore = uniq_list(self.compare_ignore)
self.compare_fileonly = self.args['--file-only']
self.ignore_missing_in_dotdrop = self.ignore_missing_in_dotdrop or \
self.args['--ignore-missing']
def _apply_args_import(self):
"""import specifics"""
self.import_path = self.args['<path>']
self.import_as = self.args['--as']
self.import_mode = self.args['--preserve-mode']
self.import_ignore = self.args['--ignore']
self.import_ignore.extend(self.impignore)
self.import_ignore.append('*{}'.format(self.install_backup_suffix))
self.import_ignore = uniq_list(self.import_ignore)
def _apply_args_update(self):
"""update specifics"""
self.update_path = self.args['<path>']
self.update_iskey = self.args['--key']
self.update_ignore = self.args['--ignore']
self.update_ignore.extend(self.upignore)
self.update_ignore.append('*{}'.format(self.install_backup_suffix))
self.update_ignore = uniq_list(self.update_ignore)
self.update_showpatch = self.args['--show-patch']
def _apply_args_profiles(self):
"""profiles specifics"""
self.profiles_grepable = self.args['--grepable']
def _apply_args_remove(self):
"""remove specifics"""
self.remove_path = self.args['<path>']
self.remove_iskey = self.args['--key']
def _apply_args_detail(self):
"""detail specifics"""
self.detail_keys = self.args['<key>']
def _apply_args(self): def _apply_args(self):
"""apply cli args as attribute""" """apply cli args as attribute"""
@@ -238,60 +319,28 @@ class Options(AttrMonitor):
self.import_link = OPT_LINK[link] self.import_link = OPT_LINK[link]
# "files" specifics # "files" specifics
self.files_templateonly = self.args['--template'] self._apply_args_files()
self.files_grepable = self.args['--grepable']
# "profiles" specifics
self.profiles_grepable = self.args['--grepable']
# "install" specifics # "install" specifics
self.install_force_action = self.args['--force-actions'] self._apply_args_install()
self.install_temporary = self.args['--temp']
self.install_keys = self.args['<key>']
self.install_diff = not self.args['--nodiff']
self.install_showdiff = self.showdiff or self.args['--showdiff']
self.install_backup_suffix = BACKUP_SUFFIX
self.install_default_actions_pre = [a for a in self.default_actions
if a.kind == Action.pre]
self.install_default_actions_post = [a for a in self.default_actions
if a.kind == Action.post]
self.install_ignore = self.instignore
self.install_force_chmod = self.force_chmod
# "compare" specifics # "compare" specifics
self.compare_focus = self.args['--file'] self._apply_args_compare()
self.compare_ignore = self.args['--ignore']
self.compare_ignore.extend(self.cmpignore)
self.compare_ignore.append('*{}'.format(self.install_backup_suffix))
self.compare_ignore = uniq_list(self.compare_ignore)
self.compare_fileonly = self.args['--file-only']
self.ignore_missing_in_dotdrop = self.ignore_missing_in_dotdrop or \
self.args['--ignore-missing']
# "import" specifics # "import" specifics
self.import_path = self.args['<path>'] self._apply_args_import()
self.import_as = self.args['--as']
self.import_mode = self.args['--preserve-mode']
self.import_ignore = self.args['--ignore']
self.import_ignore.extend(self.impignore)
self.import_ignore.append('*{}'.format(self.install_backup_suffix))
self.import_ignore = uniq_list(self.import_ignore)
# "update" specifics # "update" specifics
self.update_path = self.args['<path>'] self._apply_args_update()
self.update_iskey = self.args['--key']
self.update_ignore = self.args['--ignore'] # "profiles" specifics
self.update_ignore.extend(self.upignore) self._apply_args_profiles()
self.update_ignore.append('*{}'.format(self.install_backup_suffix))
self.update_ignore = uniq_list(self.update_ignore)
self.update_showpatch = self.args['--show-patch']
# "detail" specifics # "detail" specifics
self.detail_keys = self.args['<key>'] self._apply_args_detail()
# "remove" specifics # "remove" specifics
self.remove_path = self.args['<path>'] self._apply_args_remove()
self.remove_iskey = self.args['--key']
def _fill_attr(self): def _fill_attr(self):
"""create attributes from conf""" """create attributes from conf"""
@@ -313,34 +362,13 @@ class Options(AttrMonitor):
val = getattr(self, att) val = getattr(self, att)
if callable(val): if callable(val):
continue continue
if type(val) is list: if isinstance(val, list):
self._debug_list('-> {}'.format(att), val) debug_list('-> {}'.format(att), val, self.debug)
elif type(val) is dict: elif isinstance(val, dict):
self._debug_dict('-> {}'.format(att), val) debug_dict('-> {}'.format(att), val, self.debug)
else: else:
self.log.dbg('-> {}: {}'.format(att, val)) self.log.dbg('-> {}: {}'.format(att, val))
def _attr_set(self, attr): def _attr_set(self, attr):
"""error when some inexistent attr is set""" """error when some inexistent attr is set"""
raise Exception('bad option: {}'.format(attr)) raise Exception('bad option: {}'.format(attr))
def _debug_list(self, title, elems):
"""pretty print list"""
if not self.debug:
return
self.log.dbg('{}:'.format(title))
for e in elems:
self.log.dbg('\t- {}'.format(e))
def _debug_dict(self, title, elems):
"""pretty print dict"""
if not self.debug:
return
self.log.dbg('{}:'.format(title))
for k, v in elems.items():
if type(v) is list:
self.log.dbg('\t- \"{}\":'.format(k))
for i in v:
self.log.dbg('\t\t- {}'.format(i))
else:
self.log.dbg('\t- \"{}\": {}'.format(k, v))

View File

@@ -398,3 +398,26 @@ def categorize(function, iterable):
element""" element"""
return (tuple(filter(function, iterable)), return (tuple(filter(function, iterable)),
tuple(itertools.filterfalse(function, iterable))) tuple(itertools.filterfalse(function, iterable)))
def debug_list(title, elems, debug):
"""pretty print list"""
if not debug:
return
LOG.dbg('{}:'.format(title))
for elem in elems:
LOG.dbg('\t- {}'.format(elem))
def debug_dict(title, elems, debug):
"""pretty print dict"""
if not debug:
return
LOG.dbg('{}:'.format(title))
for k, val in elems.items():
if isinstance(val, list):
LOG.dbg('\t- \"{}\":'.format(k))
for i in val:
LOG.dbg('\t\t- {}'.format(i))
else:
LOG.dbg('\t- \"{}\": {}'.format(k, val))