Unverified Commit a260a96a authored by Jintao Lin's avatar Jintao Lin Committed by GitHub
Browse files

import_modules_from_strings when loading cfg from file (#606)

* 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'
parent 46095957
...@@ -12,6 +12,7 @@ from importlib import import_module ...@@ -12,6 +12,7 @@ from importlib import import_module
from addict import Dict from addict import Dict
from yapf.yapflib.yapf_api import FormatCode from yapf.yapflib.yapf_api import FormatCode
from .misc import import_modules_from_strings
from .path import check_file_exist from .path import check_file_exist
if platform.system() == 'Windows': if platform.system() == 'Windows':
...@@ -208,9 +209,13 @@ class Config: ...@@ -208,9 +209,13 @@ class Config:
return b return b
@staticmethod @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, cfg_dict, cfg_text = Config._file2dict(filename,
use_predefined_variables) 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) return Config(cfg_dict, cfg_text=cfg_text, filename=filename)
@staticmethod @staticmethod
......
custom_imports = dict(
imports=['r'],
allow_failed_imports=False)
import os
os.environ["TEST_VALUE"] = 'test'
# Copyright (c) Open-MMLab. All rights reserved. # Copyright (c) Open-MMLab. All rights reserved.
import argparse import argparse
import json import json
import os
import os.path as osp import os.path as osp
import shutil
import tempfile import tempfile
import pytest import pytest
...@@ -140,6 +142,19 @@ def test_fromfile(): ...@@ -140,6 +142,19 @@ def test_fromfile():
assert cfg.text == osp.abspath(osp.expanduser(cfg_file)) + '\n' + \ assert cfg.text == osp.abspath(osp.expanduser(cfg_file)) + '\n' + \
open(cfg_file, 'r').read() 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): with pytest.raises(FileNotFoundError):
Config.fromfile('no_such_file.py') Config.fromfile('no_such_file.py')
with pytest.raises(IOError): with pytest.raises(IOError):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment