From d2d1a606b773302825bfdbb3b6a37c44da85385b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Cavalheiro?= Date: Sat, 5 Mar 2022 17:44:14 +0000 Subject: [PATCH 1/4] create class variable to hold config file entries prefix --- doit/doit_cmd.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/doit/doit_cmd.py b/doit/doit_cmd.py index 5c9d143d..9ba3e06c 100644 --- a/doit/doit_cmd.py +++ b/doit/doit_cmd.py @@ -52,6 +52,9 @@ class DoitConfig(): _TOML_LIBS = ['toml', 'tomlkit'] PLUGIN_TYPES = ['command', 'loader', 'backend', 'reporter'] + # configuration file entry prefix + PREFIX = 'tool.doit' + def __init__(self): self._toml = None self.config = defaultdict(dict) @@ -59,7 +62,7 @@ def __init__(self): def loads(self, config_filenames): for config_filename in config_filenames: if str(config_filename).lower().endswith('.toml'): - prefix = 'tool.doit' if config_filename == 'pyproject.toml' else '' + prefix = self.PREFIX if config_filename == 'pyproject.toml' else '' toml_config = self.load_config_toml(config_filename, prefix) for section in toml_config: self.config[section].update(toml_config[section].items()) From 54219e1157f236508f75014e77a18f65fe9a549e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Cavalheiro?= Date: Sun, 6 Mar 2022 17:50:42 +0000 Subject: [PATCH 2/4] Make application name configurable through DoitMain instead of DoitConfig --- doit/doit_cmd.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/doit/doit_cmd.py b/doit/doit_cmd.py index 9ba3e06c..94f2eda2 100644 --- a/doit/doit_cmd.py +++ b/doit/doit_cmd.py @@ -52,17 +52,15 @@ class DoitConfig(): _TOML_LIBS = ['toml', 'tomlkit'] PLUGIN_TYPES = ['command', 'loader', 'backend', 'reporter'] - # configuration file entry prefix - PREFIX = 'tool.doit' - - def __init__(self): + def __init__(self, application_name): self._toml = None self.config = defaultdict(dict) + self._application_name = application_name def loads(self, config_filenames): for config_filename in config_filenames: if str(config_filename).lower().endswith('.toml'): - prefix = self.PREFIX if config_filename == 'pyproject.toml' else '' + prefix = f"tool.{self._application_name}" if config_filename == 'pyproject.toml' else '' toml_config = self.load_config_toml(config_filename, prefix) for section in toml_config: self.config[section].update(toml_config[section].items()) @@ -162,7 +160,8 @@ class DoitMain(object): def __init__(self, task_loader=None, config_filenames=('pyproject.toml', 'doit.cfg'), - extra_config=None): + extra_config=None, + application_name='doit'): """ :param extra_config: dict of extra argument values (by argument name) This is parameter is only used by explicit API call. @@ -182,7 +181,7 @@ def __init__(self, task_loader=None, self.config[section].update(items) # combine config option from INI/TOML files and API - config_in = DoitConfig() + config_in = DoitConfig(application_name) config_in.loads(config_filenames) for section, vals in config_in.as_dict().items(): self.config[section].update(vals) From ae5c4265c16a4e321a6d5afa0ffe758fd9354260 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Cavalheiro?= Date: Sun, 6 Mar 2022 18:42:05 +0000 Subject: [PATCH 3/4] Make configuration files prefix configurable for any TOML filename (not only pyproject.toml) --- doit/doit_cmd.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/doit/doit_cmd.py b/doit/doit_cmd.py index 94f2eda2..9ffe33ea 100644 --- a/doit/doit_cmd.py +++ b/doit/doit_cmd.py @@ -57,10 +57,15 @@ def __init__(self, application_name): self.config = defaultdict(dict) self._application_name = application_name - def loads(self, config_filenames): + def loads(self, config_filenames, toml_config_files_prefix): for config_filename in config_filenames: if str(config_filename).lower().endswith('.toml'): - prefix = f"tool.{self._application_name}" if config_filename == 'pyproject.toml' else '' + if toml_config_files_prefix: + prefix = toml_config_files_prefix + elif config_filename == 'pyproject.toml': + prefix = f'tool.{self._application_name}' + else: + prefix = '' toml_config = self.load_config_toml(config_filename, prefix) for section in toml_config: self.config[section].update(toml_config[section].items()) @@ -161,7 +166,8 @@ class DoitMain(object): def __init__(self, task_loader=None, config_filenames=('pyproject.toml', 'doit.cfg'), extra_config=None, - application_name='doit'): + application_name='doit', + toml_config_files_prefix=None): """ :param extra_config: dict of extra argument values (by argument name) This is parameter is only used by explicit API call. @@ -182,7 +188,7 @@ def __init__(self, task_loader=None, # combine config option from INI/TOML files and API config_in = DoitConfig(application_name) - config_in.loads(config_filenames) + config_in.loads(config_filenames, toml_config_files_prefix=toml_config_files_prefix) for section, vals in config_in.as_dict().items(): self.config[section].update(vals) From 8d297b25a18f5c6b006ae3cc18d3edc20b22ab62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Cavalheiro?= Date: Sun, 6 Mar 2022 18:58:37 +0000 Subject: [PATCH 4/4] Add unit test --- tests/sample_with_prefix.toml | 12 ++++++++++++ tests/test_doit_cmd.py | 13 +++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 tests/sample_with_prefix.toml diff --git a/tests/sample_with_prefix.toml b/tests/sample_with_prefix.toml new file mode 100644 index 00000000..c28a87b3 --- /dev/null +++ b/tests/sample_with_prefix.toml @@ -0,0 +1,12 @@ +[tool.doit] +optx = "6" +opty = "7" + +[tool.doit.plugins.command] +foo = "tests.sample_plugin:MyCmd" + +[tool.doit.commands.foo] +nval = 33 + +[tool.doit.tasks.bar] +opt = "baz" diff --git a/tests/test_doit_cmd.py b/tests/test_doit_cmd.py index e9c8323e..55f53500 100644 --- a/tests/test_doit_cmd.py +++ b/tests/test_doit_cmd.py @@ -178,6 +178,19 @@ def test_merge_api_toml_config(self): assert main.config['foo'] == {'nval': 33} assert main.config['task:bar'] == {'opt': "baz"} + def test_merge_api_toml_config_with_prefix(self): + config_filename = os.path.join(os.path.dirname(__file__), 'sample_with_prefix.toml') + main = doit_cmd.DoitMain(config_filenames=config_filename, + toml_config_files_prefix="tool.doit") + assert 1 == len(main.config['COMMAND']) + # test loaded plugin command is actually used with plugin name + assert 'foo' in main.get_cmds() + # INI has higher preference the api_config + assert main.config['GLOBAL'] == {'optx':'6', 'opty':'7'} + + assert main.config['foo'] == {'nval': 33} + assert main.config['task:bar'] == {'opt': "baz"} + def test_find_pyproject_toml_config(self): config_filename = os.path.join(os.path.dirname(__file__), 'pyproject.toml')