Skip to content

Commit

Permalink
Fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jbwang1997 committed Dec 2, 2022
1 parent 2f221b6 commit bad6e7c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
12 changes: 7 additions & 5 deletions mmengine/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,18 +335,20 @@ def _substitute_environment_vars(filename: str, temp_config_name: str):
"""
with open(filename, encoding='utf-8') as f:
config_file = f.read()
regexp = r'\{\{[\'\"]?\s*\$(\w+)\s*\:?\s*(\S*?)\s*[\'\"]?\}\}'
regexp = r'\{\{[\'\"]?\s*\$(\w+)\s*\:\s*(\S*?)\s*[\'\"]?\}\}'
keys = re.findall(regexp, config_file)
env_variables = dict()
for var_name, value in keys:
regexp = r'\{\{[\'\"]?\s*\$' + var_name + r'\s*\:?\s*' \
regexp = r'\{\{[\'\"]?\s*\$' + var_name + r'\s*\:\s*' \
+ value + r'\s*[\'\"]?\}\}'
if var_name in os.environ:
value = os.environ[var_name]
env_variables[var_name] = value
print_log(f'Using env variable `{var_name}` with value of '
f'{value} to replace item in config.')
elif not value:
print_log(
f'Using env variable `{var_name}` with value of '
f'{value} to replace item in config.',
logger='current')
if not value:
raise KeyError(f'`{var_name}` cannot be found in `os.environ`.'
f' Please set `{var_name}` in environment or '
'give a default value.')
Expand Down
2 changes: 1 addition & 1 deletion tests/data/config/py_config/test_environment_var.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
item1 = '{{ $ITEM1 }}'
item1 = '{{ $ITEM1: }}'
item2 = '{{ $ITEM2:default_value }}'
item3 = {{' $ITEM3:80 '}}
2 changes: 1 addition & 1 deletion tests/test_config/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def test_substitute_environment_vars(self, tmp_path):
cfg = tmp_path / 'tmp_cfg1.py'
substituted_cfg = tmp_path / 'tmp_cfg2.py'

cfg_text = 'a={{$A}}\n'
cfg_text = 'a={{$A:}}\n'
with open(cfg, 'w') as f:
f.write(cfg_text)
with pytest.raises(KeyError):
Expand Down

0 comments on commit bad6e7c

Please sign in to comment.