From 458ad0869337f256d0f0f68955fdc76dcfd236ed Mon Sep 17 00:00:00 2001 From: chenke Date: Thu, 18 Jul 2019 11:14:19 +0800 Subject: [PATCH] Optimize method list_opts() in watcher/conf/opts.py Actually list_opts() return a list like[1], So we don't need to convert list to dict and then convert to list[2]. The reason why we need to convert it before is to put together the same group of configuration objects, but we don't need it actually. Now, the list_opts()'s result like this[3]. Reference: [1]. [(Group1,[cfgObj1,cfgObj2....]),(Group2,[cfgObj3,cfgObj3....])..] [2]. https://github.com/openstack/watcher/blob/375ae32fadb8a165f112874ccb8645b637c1ae0a/watcher/conf/opts.py#L51-L52 [3]. [(Group1,[cfgObj1]),(Group1,[cfgObj2]),(Group2,[cfgObj3,cfgObj3....])..] Change-Id: I50fcc5f812be42038852662639fb10c6dd2f6f72 --- watcher/conf/opts.py | 31 ++++--------------------------- 1 file changed, 4 insertions(+), 27 deletions(-) diff --git a/watcher/conf/opts.py b/watcher/conf/opts.py index 5af0314e3..7baff600e 100644 --- a/watcher/conf/opts.py +++ b/watcher/conf/opts.py @@ -26,7 +26,6 @@ in this package. It is assumed that: * this module is only used in the context of sample file generation """ -import collections import importlib import os import pkgutil @@ -34,22 +33,18 @@ import pkgutil LIST_OPTS_FUNC_NAME = "list_opts" -def _tupleize(dct): - """Take the dict of options and convert to the 2-tuple format.""" - return [(key, val) for key, val in dct.items()] - - def list_opts(): """Grouped list of all the Watcher-specific configuration options :return: A list of ``(group, [opt_1, opt_2])`` tuple pairs, where ``group`` is either a group name as a string or an OptGroup object. """ - opts = collections.defaultdict(list) + opts = list() module_names = _list_module_names() imported_modules = _import_modules(module_names) - _append_config_options(imported_modules, opts) - return _tupleize(opts) + for mod in imported_modules: + opts.extend(mod.list_opts()) + return opts def _list_module_names(): @@ -75,21 +70,3 @@ def _import_modules(module_names): else: imported_modules.append(mod) return imported_modules - - -def _process_old_opts(configs): - """Convert old-style 2-tuple configs to dicts.""" - if isinstance(configs, tuple): - configs = [configs] - return {label: options for label, options in configs} - - -def _append_config_options(imported_modules, config_options): - for mod in imported_modules: - configs = mod.list_opts() - # TODO(markus_z): Remove this compatibility shim once all list_opts() - # functions have been updated to return dicts. - if not isinstance(configs, dict): - configs = _process_old_opts(configs) - for key, val in configs.items(): - config_options[key].extend(val)