def test_import_module_from_file(topdir): with assert_raises(AssertionError): # we support only submodule files ending with .py ATM. TODO import_module_from_file(op.join(topdir, 'dltestm2', 'dlsub1')) dlsub2_path = op.join(topdir, 'dltestm2', 'dlsub2.py') mod = import_module_from_file(dlsub2_path) eq_(mod.__name__, 'dlsub2') # we are not asking to import as submod of the dltestm1 assert_in('dlsub2', sys.modules) try: sys.path.append(topdir) import dltestm2 mod = import_module_from_file(dlsub2_path, pkg=dltestm2) eq_(mod.__name__, 'dltestm2.dlsub2') assert_in('dltestm2.dlsub2', sys.modules) finally: sys.path.pop(sys.path.index(topdir))
def __init__(self, dataset=None): """Retrieves the configured set of rules Rules are defined by classes ... + __datalad_hirni_rules datalad.hirni.dicom2spec.rules ... multiple Parameters ---------- dataset: Dataset Dataset to read possibly customized rules from """ from datalad.utils import assure_list from datalad import cfg as dl_cfg from datalad_hirni.support.default_rules import DefaultRules cfg = dataset.config if dataset else dl_cfg self._rule_set = [] # get a list of paths to build the rule set from # Note: assure_list is supposed to return empty list if there's nothing self._file_list = \ assure_list(cfg.get("datalad.hirni.dicom2spec.rules")) lgr.debug("loaded list of rule files: %s", self._file_list) for file in self._file_list: if not op.exists(file) or not op.isfile(file): lgr.warning( "Ignored invalid path for dicom2spec rules " "definition: %s", file) continue from datalad.utils import import_module_from_file from datalad.dochelpers import exc_str try: mod = import_module_from_file(file) except Exception as e: # any exception means full stop raise ValueError("Rules definition file at {} is broken: {}" "".format(file, exc_str(e))) # check file's __datalad_hirni_rules for the actual class: if not hasattr(mod, "__datalad_hirni_rules"): raise ValueError("Rules definition file {} missed attribute " "'__datalad_hirni_rules'.".format(file)) self._rule_set.append(getattr(mod, "__datalad_hirni_rules")) if not self._rule_set: self._rule_set = [DefaultRules]
def _load_plugin(filepath, fail=True): from datalad.utils import import_module_from_file try: mod = import_module_from_file(filepath, pkg=datalad) except Exception as e: # any exception means full stop raise ValueError('plugin at {} is broken: {}'.format( filepath, exc_str(e))) # TODO check all symbols whether they are derived from Interface if not hasattr(mod, magic_plugin_symbol): msg = "loading plugin '%s' did not yield a '%s' symbol, found: %s", \ filepath, magic_plugin_symbol, dir(mod) if fail: raise ValueError(*msg) else: lgr.debug(*msg) return return getattr(mod, magic_plugin_symbol)