diff --git a/bob/extension/config.py b/bob/extension/config.py index 98e3d8c4c174907bb0df8469065c13f348eab710..bc4905cb7b0a889270251f91c4ada7d1d4fe0a8a 100644 --- a/bob/extension/config.py +++ b/bob/extension/config.py @@ -90,17 +90,25 @@ def _resolve_entry_point_or_modules(paths, entry_point_group): ------- paths : [str] The resolved paths pointing to existing files. + names : [str] + The valid python module names to bind each of the files to + """ + entries = {e.name: e for e in pkg_resources.iter_entry_points(entry_point_group)} files = [] + names = [] for i, path in enumerate(paths): + old_path = path - # if it is already a file - if isfile(path): - pass - # If it is an entry point name + module_name = 'user_config' #fixed module name for files with full paths + + # if it already points to a file + if isfile(path): pass + + # If it is an entry point name, collect path and module name elif path in entries: module_name = entries[path].module_name path = _get_module_filename(module_name) @@ -109,6 +117,7 @@ def _resolve_entry_point_or_modules(paths, entry_point_group): "The specified entry point: `{}' pointing to module: `{}' and " "resolved to: `{}' does not point to an existing " "file.".format(old_path, module_name, path)) + # If it is not a path nor an entry point name, it is a module name then? else: path = _get_module_filename(path) @@ -116,8 +125,11 @@ def _resolve_entry_point_or_modules(paths, entry_point_group): raise ValueError( "The specified path: `{}' resolved to: `{}' is not a file, entry " "point name, or a module name".format(old_path, path)) + files.append(path) - return files + names.append(module_name) + + return files, names def load(paths, context=None, entry_point_group=None): @@ -147,22 +159,28 @@ def load(paths, context=None, entry_point_group=None): mod : :any:`module` A module representing the resolved context, after loading the provided modules and resolving all variables. - ''' - mod = imp.new_module('config') - if context is not None: - mod.__dict__.update(context) + ''' # resolve entry points to paths if entry_point_group is not None: - paths = _resolve_entry_point_or_modules(paths, entry_point_group) - - for k in paths: - logger.debug("Loading configuration file `%s'...", k) - mod = _load_context(k, mod) + paths, names = _resolve_entry_point_or_modules(paths, entry_point_group) + else: + names = len(paths)*['user_config'] + ctxt = imp.new_module('initial_context') + if context is not None: + ctxt.__dict__.update(context) # Small gambiarra (https://www.urbandictionary.com/define.php?term=Gambiarra) # to avoid the garbage collector to collect some already imported modules. - LOADED_CONFIGS.append(mod) + LOADED_CONFIGS.append(ctxt) + + for k,n in zip(paths, names): + logger.debug("Loading configuration file `%s'...", k) + mod = imp.new_module(n) + mod.__dict__.update(ctxt.__dict__) + mod.__name__ = n #reverse module-name override + LOADED_CONFIGS.append(mod) + ctxt = _load_context(k, mod) return mod diff --git a/bob/extension/data/resource_config.py b/bob/extension/data/resource_config.py new file mode 100644 index 0000000000000000000000000000000000000000..2ba8046898a68e9f1b34d121f38441e4644e8db8 --- /dev/null +++ b/bob/extension/data/resource_config.py @@ -0,0 +1,3 @@ +a = 1 +b = a + 2 +from .. import rc diff --git a/bob/extension/test_config.py b/bob/extension/test_config.py index 4d4bd837cba913dfca2dd27e822c73e30ffc9262..8d777f2ec39e9aced6cad686b1ca1f4fcfd207a1 100644 --- a/bob/extension/test_config.py +++ b/bob/extension/test_config.py @@ -47,8 +47,9 @@ def test_entry_point_configs(): # test when all kinds of paths c = load([ os.path.join(path, 'basic_config.py'), - 'basic_config', + 'resource_config', 'bob.extension.data.basic_config', ], entry_point_group='bob.extension.test_config_load') assert hasattr(c, "a") and c.a == 1 assert hasattr(c, "b") and c.b == 3 + assert hasattr(c, "rc") diff --git a/doc/framework.rst b/doc/framework.rst index d1e070ab1e1f43f695ff26cc2e03dcd7595e1825..2865435837bb64cf53b0a36a33066a4ac016eb6c 100644 --- a/doc/framework.rst +++ b/doc/framework.rst @@ -115,7 +115,7 @@ to provide the group name of the entry points: .. doctest:: entry_point >>> group = 'bob.extension.test_config_load' # the group name of entry points - >>> file1 = 'basic_config' # an entry point name + >>> file1 = 'resource_config' # an entry point name >>> file2 = 'bob.extension.data.load_config' # module name >>> configuration = load([file1, file2], entry_point_group=group) >>> print("a = %d \nb = %d"%(configuration.a, configuration.b)) # doctest: +NORMALIZE_WHITESPACE diff --git a/setup.py b/setup.py index f1629339486fef641ec1f1130a8d6e0ac3b2633d..74e2f1f300889642bdf23f836cc94bd3bea01fdc 100644 --- a/setup.py +++ b/setup.py @@ -43,7 +43,7 @@ setup( ], # some test entry_points 'bob.extension.test_config_load': [ - 'basic_config = bob.extension.data.basic_config', + 'resource_config = bob.extension.data.resource_config', ], },