diff --git a/doit/doit_cmd.py b/doit/doit_cmd.py index 5c9d143d..9ffe33ea 100644 --- a/doit/doit_cmd.py +++ b/doit/doit_cmd.py @@ -52,14 +52,20 @@ class DoitConfig(): _TOML_LIBS = ['toml', 'tomlkit'] PLUGIN_TYPES = ['command', 'loader', 'backend', 'reporter'] - 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): + def loads(self, config_filenames, toml_config_files_prefix): for config_filename in config_filenames: if str(config_filename).lower().endswith('.toml'): - prefix = 'tool.doit' 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()) @@ -159,7 +165,9 @@ class DoitMain(object): def __init__(self, task_loader=None, config_filenames=('pyproject.toml', 'doit.cfg'), - extra_config=None): + extra_config=None, + 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. @@ -179,8 +187,8 @@ 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.loads(config_filenames) + config_in = DoitConfig(application_name) + 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) 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')