From 5411daa2d3d7046da74788df5b1dbdf64dc22874 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ciro=20Santilli=20=E5=85=AD=E5=9B=9B=E4=BA=8B=E4=BB=B6=20?= =?UTF-8?q?=E6=B3=95=E8=BD=AE=E5=8A=9F?= Date: Sun, 5 May 2019 00:00:00 +0000 Subject: [PATCH] cli-function: raise if the config file is given and does not exist --- cli_function.py | 45 +++++++++++++++++++++++++-------------------- common.py | 4 ++-- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/cli_function.py b/cli_function.py index 1d163bd..6dee84d 100755 --- a/cli_function.py +++ b/cli_function.py @@ -147,15 +147,15 @@ class CliFunction: def _do_main(self, kwargs): return self.main(**self._get_args(kwargs)) - def __init__(self, config_file=None, description=None, extra_config_params=None): + def __init__(self, default_config_file=None, description=None, extra_config_params=None): self._arguments = collections.OrderedDict() - self._config_file = config_file + self._default_config_file = default_config_file self._description = description self.extra_config_params = extra_config_params - if self._config_file is not None: + if self._default_config_file is not None: self.add_argument( '--config-file', - default=self._config_file, + default=self._default_config_file, help='Path to the configuration file to use' ) @@ -172,30 +172,35 @@ class CliFunction: args_with_defaults = kwargs.copy() # Add missing args from config file. config_file = None + args_given = {} if 'config_file' in args_with_defaults and args_with_defaults['config_file'] is not None: config_file = args_with_defaults['config_file'] + args_given['config_file'] = True else: - config_file = self._config_file - args_given = {} + config_file = self._default_config_file + args_given['config_file'] = False for key in self._arguments: args_given[key] = not ( not key in args_with_defaults or args_with_defaults[key] is None or self._arguments[key].nargs == '*' and args_with_defaults[key] == [] ) - if config_file is not None and os.path.exists(config_file): - config_configs = {} - config = imp.load_source('config', config_file) - if self.extra_config_params is None: - config.set_args(config_configs) - else: - config.set_args(config_configs, self.extra_config_params) - for key in config_configs: - if key not in self._arguments: - raise Exception('Unknown key in config file: ' + key) - if not args_given[key]: - args_with_defaults[key] = config_configs[key] - args_given[key] = True + if config_file is not None: + if os.path.exists(config_file): + config_configs = {} + config = imp.load_source('config', config_file) + if self.extra_config_params is None: + config.set_args(config_configs) + else: + config.set_args(config_configs, self.extra_config_params) + for key in config_configs: + if key not in self._arguments: + raise Exception('Unknown key in config file: ' + key) + if not args_given[key]: + args_with_defaults[key] = config_configs[key] + args_given[key] = True + elif args_given['config_file']: + raise Exception('Config file does not exist: ' + config_file) # Add missing args from hard-coded defaults. for key in self._arguments: argument = self._arguments[key] @@ -329,7 +334,7 @@ if __name__ == '__main__': class OneCliFunction(CliFunction): def __init__(self): super().__init__( - config_file='cli_function_test_config.py', + default_config_file='cli_function_test_config.py', description = '''\ Description of this amazing function! diff --git a/common.py b/common.py index f798e1b..0411f25 100644 --- a/common.py +++ b/common.py @@ -113,7 +113,7 @@ consts['userland_out_exts'] = [ consts['userland_executable_ext'], consts['obj_ext'], ] -consts['config_file'] = os.path.join(consts['data_dir'], 'config.py') +consts['default_config_file'] = os.path.join(consts['data_dir'], 'config.py') consts['magic_fail_string'] = b'lkmc_test_fail' consts['baremetal_lib_basename'] = 'lib' consts['emulator_userland_only_short_to_long_dict'] = collections.OrderedDict([ @@ -156,7 +156,7 @@ class LkmcCliFunction(cli_function.CliFunction): :ptype defaults: Dict[str,Any] :param defaults: override the default value of an argument ''' - kwargs['config_file'] = consts['config_file'] + kwargs['default_config_file'] = consts['default_config_file'] kwargs['extra_config_params'] = os.path.basename(inspect.getfile(self.__class__)) if defaults is None: defaults = {}