From c09b3f4d7dc8e25ef1e4874b94f77e3f4eecb6ff Mon Sep 17 00:00:00 2001 From: Chandrasekhar Ramakrishnan <chandrasekhar.ramakrishnan@id.ethz.ch> Date: Fri, 10 Mar 2017 14:59:50 +0100 Subject: [PATCH] SSDM-4670: Made configuration of the location resolver more flexible. --- src/python/OBis/obis/dm/config.py | 32 +++++++++++++++----------- src/python/OBis/obis/dm/config_test.py | 19 +++++++-------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/python/OBis/obis/dm/config.py b/src/python/OBis/obis/dm/config.py index ca81cac52dc..dfebbc1b65f 100644 --- a/src/python/OBis/obis/dm/config.py +++ b/src/python/OBis/obis/dm/config.py @@ -84,14 +84,16 @@ class ConfigEnv(object): return location -def default_location_resolver(location): - """Given a location, return a path""" - if location.root == 'user_home': - root = os.path.expanduser('~') - else: - # Use the current directory in the remaining case - root = './' - return os.path.join(root, location.basename) +class LocationResolver(object): + def __init__(self): + self.location_roots = { + 'user_home': os.path.expanduser('~'), + 'data_set': './' + } + + def resolve_location(self, location): + root = self.location_roots[location.root] + return os.path.join(root, location.basename) class ConfigResolver(object): @@ -99,10 +101,10 @@ class ConfigResolver(object): def __init__(self, env=None, location_resolver=None): self.env = env if env is not None else ConfigEnv() - self.location_resolver = location_resolver if location_resolver is not None else default_location_resolver + self.location_resolver = location_resolver if location_resolver is not None else LocationResolver() self.location_search_order = ['global', 'local'] self.location_cache = {} - self.initialize_location_cache() + self.is_intialized = False def initialize_location_cache(self): env = self.env @@ -115,7 +117,7 @@ class ConfigResolver(object): for k, v in loc.items(): self.initialize_location(k, v, cache[key]) else: - root_path = self.location_resolver(loc) + root_path = self.location_resolver.resolve_location(loc) config_path = os.path.join(root_path, 'config.json') if os.path.exists(config_path): with open(config_path) as f: @@ -124,7 +126,8 @@ class ConfigResolver(object): def config_dict(self): """Return a configuration dictionary by applying the lookup/resolution rules.""" - + if not self.is_intialized: + self.initialize_location_cache() env = self.env result = {} # Iterate over the locations in the specified order searching for parameter values. @@ -143,6 +146,9 @@ class ConfigResolver(object): :param loc: Either 'local' or 'global' :return: """ + if not self.is_intialized: + self.initialize_location_cache() + param = self.env.params[name] location_config_dict = self.set_cache_value_for_parameter(param, value, loc) if loc != 'global': @@ -153,7 +159,7 @@ class ConfigResolver(object): else: location_path = [loc] location = self.env.location_at_path(location_path) - location_dir_path = self.location_resolver(location) + location_dir_path = self.location_resolver.resolve_location(location) if not os.path.exists(location_dir_path): print("Create {}".format(location_dir_path)) os.makedirs(location_dir_path) diff --git a/src/python/OBis/obis/dm/config_test.py b/src/python/OBis/obis/dm/config_test.py index 81c9fd210a7..cdecc246a9a 100644 --- a/src/python/OBis/obis/dm/config_test.py +++ b/src/python/OBis/obis/dm/config_test.py @@ -17,7 +17,8 @@ from . import config def test_config_location_resolver(): loc = config.ConfigLocation(['global'], 'user_home', '.obis') - assert config.default_location_resolver(loc) == os.path.join(os.path.expanduser("~"), '.obis') + location_resolver = config.LocationResolver() + assert location_resolver.resolve_location(loc) == os.path.join(os.path.expanduser("~"), '.obis') def user_config_test_data_path(): @@ -31,19 +32,14 @@ def copy_user_config_test_data(tmpdir): return config_test_data_src, config_test_data_dst -def location_resolver_for_test(tmpdir): - def resolver(location): - if location.root == 'user_home': - return os.path.join(str(tmpdir), 'user_config', location.basename) - else: - return config.default_location_resolver(location) - - return resolver +def configure_resolver_for_test(resolver, tmpdir): + resolver.location_resolver.location_roots['user_home'] = os.path.join(str(tmpdir), 'user_config') def test_read_config(tmpdir): copy_user_config_test_data(tmpdir) - resolver = config.ConfigResolver(location_resolver=location_resolver_for_test(tmpdir)) + resolver = config.ConfigResolver() + configure_resolver_for_test(resolver, tmpdir) config_dict = resolver.config_dict() assert config_dict is not None with open(os.path.join(user_config_test_data_path(), ".obis", "config.json")) as f: @@ -53,7 +49,8 @@ def test_read_config(tmpdir): def test_write_config(tmpdir): copy_user_config_test_data(tmpdir) - resolver = config.ConfigResolver(location_resolver=location_resolver_for_test(tmpdir)) + resolver = config.ConfigResolver() + configure_resolver_for_test(resolver, tmpdir) config_dict = resolver.config_dict() assert config_dict is not None with open(os.path.join(user_config_test_data_path(), ".obis", "config.json")) as f: -- GitLab