diff options
| author | Mark McLoughlin <markmc@redhat.com> | 2012-04-24 14:56:07 +0100 |
|---|---|---|
| committer | Mark McLoughlin <markmc@redhat.com> | 2012-04-24 19:08:01 +0100 |
| commit | 6e4948b95278f86f5bf2925308797c014cb31c59 (patch) | |
| tree | ca728ebe9e626994d49b8e3f438ddd7a2e688836 /openstack | |
| parent | 0bfd6e106a88103b0b751a0b010e9e2055061933 (diff) | |
| download | oslo-6e4948b95278f86f5bf2925308797c014cb31c59.tar.gz oslo-6e4948b95278f86f5bf2925308797c014cb31c59.tar.xz oslo-6e4948b95278f86f5bf2925308797c014cb31c59.zip | |
Some refactoring of the cfg cache
A fairly misc bunch of changes:
- init cache before registering config-file and just let
register_cli_opt() clear the empty cache
- use @__clear_cache on set_default() and set_override() since
these are just used by the unit tests and doing so allows us
to kill _remove_from_cache()
- use @__clear_cache on reset() too
- remove recursion from _get() and the substitute param
- just use (group_name, opt_name) as the cache key
Change-Id: I66934e748eca9ec03e44d7f80a7e10d96a77d8eb
Diffstat (limited to 'openstack')
| -rw-r--r-- | openstack/common/cfg.py | 46 |
1 files changed, 12 insertions, 34 deletions
diff --git a/openstack/common/cfg.py b/openstack/common/cfg.py index 80e89eb..31da063 100644 --- a/openstack/common/cfg.py +++ b/openstack/common/cfg.py @@ -822,6 +822,8 @@ class ConfigOpts(collections.Mapping): usage=self.usage) self._cparser = None + self.__cache = {} + self.register_cli_opt( MultiStrOpt('config-file', default=self.default_config_files, @@ -829,10 +831,7 @@ class ConfigOpts(collections.Mapping): help='Path to a config file to use. Multiple config ' 'files can be specified, with values in later ' 'files taking precedence. The default files used ' - 'are: %s' % (self.default_config_files, )), - clear_cache=False) - - self.__cache = {} + 'are: %s' % (self.default_config_files, ))) def __clear_cache(f): @functools.wraps(f) @@ -843,20 +842,6 @@ class ConfigOpts(collections.Mapping): return __inner - def __remove_from_cache(self, group, name): - if isinstance(group, OptGroup): - group_name = group.name - else: - group_name = group - try: - del self.__cache[(group_name, name)] - except KeyError: - pass - try: - del self.__cache[(None, group_name, name)] - except KeyError: - pass - def __call__(self, args=None): """Parse command line arguments and config files. @@ -891,7 +876,7 @@ class ConfigOpts(collections.Mapping): :returns: the option value (after string subsititution) or a GroupAttr :raises: NoSuchOptError,ConfigFileValueError,TemplateSubstitutionError """ - return self._get(name, substitute=True) + return self._get(name) def __getitem__(self, key): """Look up an option value and perform string substitution.""" @@ -910,12 +895,12 @@ class ConfigOpts(collections.Mapping): """Return the number of options and option groups.""" return len(self._opts) + len(self._groups) + @__clear_cache def reset(self): """Reset the state of the object to before it was called.""" self._args = None self._cli_values = None self._cparser = None - self.__cache.clear() @__clear_cache def register_opt(self, opt, group=None): @@ -991,6 +976,7 @@ class ConfigOpts(collections.Mapping): self._groups[group.name] = copy.copy(group) + @__clear_cache def set_override(self, name, override, group=None): """Override an opt value. @@ -1002,10 +988,10 @@ class ConfigOpts(collections.Mapping): :param group: an option OptGroup object or group name :raises: NoSuchOptError, NoSuchGroupError """ - self.__remove_from_cache(group, name) opt_info = self._get_opt_info(name, group) opt_info['override'] = override + @__clear_cache def set_default(self, name, default, group=None): """Override an opt's default value. @@ -1017,7 +1003,6 @@ class ConfigOpts(collections.Mapping): :param group: an option OptGroup object or group name :raises: NoSuchOptError, NoSuchGroupError """ - self.__remove_from_cache(group, name) opt_info = self._get_opt_info(name, group) opt_info['default'] = default @@ -1089,22 +1074,15 @@ class ConfigOpts(collections.Mapping): """Print the help message for the current program.""" self._oparser.print_help(file) - def _get(self, name, group=None, substitute=False): + def _get(self, name, group=None): if isinstance(group, OptGroup): - group_name = group.name + key = (group.name, name) else: - group_name = group - if substitute: - key = (None, group_name, name) - else: - key = (group_name, name) + key = (group, name) try: return self.__cache[key] except KeyError: - if substitute: - value = self._substitute(self._get(name, group)) - else: - value = self._do_get(name, group) + value = self._substitute(self._do_get(name, group)) self.__cache[key] = value return value @@ -1249,7 +1227,7 @@ class ConfigOpts(collections.Mapping): def __getattr__(self, name): """Look up an option value and perform template substitution.""" - return self.conf._get(name, self.group, substitute=True) + return self.conf._get(name, self.group) def __getitem__(self, key): """Look up an option value and perform string substitution.""" |
