Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create class variable to hold config file entries prefix #419

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions doit/doit_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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.
Expand All @@ -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)

Expand Down
12 changes: 12 additions & 0 deletions tests/sample_with_prefix.toml
Original file line number Diff line number Diff line change
@@ -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"
13 changes: 13 additions & 0 deletions tests/test_doit_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down