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
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':
......@@ -208,9 +209,13 @@ class Config:
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
......
custom_imports = dict(
imports=['r'],
allow_failed_imports=False)
import os
os.environ["TEST_VALUE"] = 'test'
# Copyright (c) Open-MMLab. All rights reserved.
import argparse
import json
import os
import os.path as osp
import shutil
import tempfile
import pytest
......@@ -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):
......
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