From 55e6ff46b8691443587a246a1babf9b6c943a8f6 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Wed, 22 Sep 2021 11:16:17 +0200 Subject: [PATCH] implement user input for #318 --- dotdrop/cfg_aggregator.py | 5 +++-- dotdrop/cfg_yaml.py | 30 ++++++++++++++++++++++++++++-- dotdrop/utils.py | 14 ++++++++++++++ 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/dotdrop/cfg_aggregator.py b/dotdrop/cfg_aggregator.py index 9cce4cc..d7b3437 100644 --- a/dotdrop/cfg_aggregator.py +++ b/dotdrop/cfg_aggregator.py @@ -206,10 +206,11 @@ class CfgAggregator: # parsing ######################################################## - def _load(self): + def _load(self, reloading=False): """load lower level config""" self.cfgyaml = CfgYaml(self.path, self.profile_key, + reloading=reloading, debug=self.debug) # settings @@ -361,7 +362,7 @@ class CfgAggregator: self.log.dbg('reloading config') olddebug = self.debug self.debug = False - self._load() + self._load(reloading=True) self.debug = olddebug @classmethod diff --git a/dotdrop/cfg_yaml.py b/dotdrop/cfg_yaml.py index 3b8b868..cec8fa2 100644 --- a/dotdrop/cfg_yaml.py +++ b/dotdrop/cfg_yaml.py @@ -33,7 +33,7 @@ from dotdrop.settings import Settings from dotdrop.logger import Logger from dotdrop.templategen import Templategen from dotdrop.linktypes import LinkTypes -from dotdrop.utils import shellrun, uniq_list +from dotdrop.utils import shellrun, uniq_list, userinput from dotdrop.exceptions import YamlException, UndefinedException @@ -50,6 +50,7 @@ class CfgYaml: key_trans_w = 'trans_write' key_variables = 'variables' key_dvariables = 'dynvariables' + key_uvariables = 'uservariables' action_pre = 'pre' action_post = 'post' @@ -98,16 +99,19 @@ class CfgYaml: allowed_link_val = [lnk_nolink, lnk_link, lnk_children] top_entries = [key_dotfiles, key_settings, key_profiles] - def __init__(self, path, profile=None, addprofiles=None, debug=False): + def __init__(self, path, profile=None, addprofiles=None, + reloading=False, debug=False): """ config parser @path: config file path @profile: the selected profile @addprofiles: included profiles + @reloading: true when reloading @debug: debug flag """ self._path = os.path.abspath(path) self._profile = profile + self._reloading = reloading self._debug = debug self._log = Logger(debug=self._debug) # config needs to be written @@ -163,6 +167,10 @@ class CfgYaml: dvariables = self._parse_blk_dynvariables(self._yaml_dict) self._add_variables(dvariables, template=False) + # parse the "uservariables" block + uvariables = self._parse_blk_uservariables(self._yaml_dict) + self._add_variables(uvariables, template=False) + # now template variables and dynvariables from the same pool self._rec_resolve_variables(self.variables) # and execute dvariables @@ -565,6 +573,24 @@ class CfgYaml: self._debug_dict('dynvariables block', dvariables) return dvariables + def _parse_blk_uservariables(self, dic): + """parse the "uservariables" block""" + uvariables = self._get_entry(dic, + self.key_uvariables, + mandatory=False) + + uvars = {} + if not self._reloading and uvariables: + for k, v in uvariables.items(): + content = userinput(v, debug=self._debug) + uvars[k] = content + + if uvars: + uvars = uvars.copy() + if self._debug: + self._debug_dict('uservariables block', uvars) + return uvars + ######################################################## # parsing helpers ######################################################## diff --git a/dotdrop/utils.py b/dotdrop/utils.py index c3dcf1c..e9fe828 100644 --- a/dotdrop/utils.py +++ b/dotdrop/utils.py @@ -69,6 +69,20 @@ def shellrun(cmd, debug=False): return ret == 0, out +def userinput(prompt, debug=False): + """ + get user input + return user input + """ + if debug: + LOG.dbg('get user input for \"{}\"'.format(prompt), force=True) + pre = 'Please provide the value for \"{}\": '.format(prompt) + res = input(pre) + if debug: + LOG.dbg('user input result: {}'.format(res), force=True) + return res + + def fastdiff(left, right): """fast compare files and returns True if different""" return not filecmp.cmp(left, right, shallow=False)