Skip to content

Commit

Permalink
import_modules_from_strings when loading cfg from file (open-mmlab#606)
Browse files Browse the repository at this point in the history
* import_modules_from_strings when loading cfg from file

* add unittest to tell whether the feature is enabled as expected

* minor

* set an environment variable instead of writing a file

* use 'shutil' instead of 'os.system'
  • Loading branch information
dreamerlin authored and wxzs5 committed Nov 20, 2020
1 parent 47aa850 commit f9d3bf3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
7 changes: 6 additions & 1 deletion mmcv/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from addict import Dict
from yapf.yapflib.yapf_api import FormatCode

from .misc import import_modules_from_strings
from .path import check_file_exist

if platform.system() == 'Windows':
Expand Down Expand Up @@ -208,9 +209,13 @@ def _merge_a_into_b(a, b):
return b

@staticmethod
def fromfile(filename, use_predefined_variables=True):
def fromfile(filename,
use_predefined_variables=True,
import_custom_modules=True):
cfg_dict, cfg_text = Config._file2dict(filename,
use_predefined_variables)
if import_custom_modules and cfg_dict.get('custom_imports', None):
import_modules_from_strings(**cfg_dict['custom_imports'])
return Config(cfg_dict, cfg_text=cfg_text, filename=filename)

@staticmethod
Expand Down
3 changes: 3 additions & 0 deletions tests/data/config/q.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
custom_imports = dict(
imports=['r'],
allow_failed_imports=False)
3 changes: 3 additions & 0 deletions tests/data/config/r.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import os

os.environ["TEST_VALUE"] = 'test'
15 changes: 15 additions & 0 deletions tests/test_utils/test_config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Copyright (c) Open-MMLab. All rights reserved.
import argparse
import json
import os
import os.path as osp
import shutil
import tempfile

import pytest
Expand Down Expand Up @@ -140,6 +142,19 @@ def test_fromfile():
assert cfg.text == osp.abspath(osp.expanduser(cfg_file)) + '\n' + \
open(cfg_file, 'r').read()

# test custom_imports for Config.fromfile
cfg_file = osp.join(data_path, 'config', 'q.py')
imported_file = osp.join(data_path, 'config', 'r.py')
target_pkg = osp.join(osp.dirname(__file__), 'r.py')

# Since the imported config will be regarded as a tmp file
# it should be copied to the directory at the same level
shutil.copy(imported_file, target_pkg)
Config.fromfile(cfg_file, import_custom_modules=True)

assert os.environ.pop('TEST_VALUE') == 'test'
os.remove(target_pkg)

with pytest.raises(FileNotFoundError):
Config.fromfile('no_such_file.py')
with pytest.raises(IOError):
Expand Down

0 comments on commit f9d3bf3

Please # to comment.