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]. 375ae32fad/watcher/conf/opts.py (L51-L52)
[3]. [(Group1,[cfgObj1]),(Group1,[cfgObj2]),(Group2,[cfgObj3,cfgObj3....])..]
Change-Id: I50fcc5f812be42038852662639fb10c6dd2f6f72
This commit is contained in:
@@ -26,7 +26,6 @@ in this package. It is assumed that:
|
|||||||
* this module is only used in the context of sample file generation
|
* this module is only used in the context of sample file generation
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import collections
|
|
||||||
import importlib
|
import importlib
|
||||||
import os
|
import os
|
||||||
import pkgutil
|
import pkgutil
|
||||||
@@ -34,22 +33,18 @@ import pkgutil
|
|||||||
LIST_OPTS_FUNC_NAME = "list_opts"
|
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():
|
def list_opts():
|
||||||
"""Grouped list of all the Watcher-specific configuration options
|
"""Grouped list of all the Watcher-specific configuration options
|
||||||
|
|
||||||
:return: A list of ``(group, [opt_1, opt_2])`` tuple pairs, where ``group``
|
: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.
|
is either a group name as a string or an OptGroup object.
|
||||||
"""
|
"""
|
||||||
opts = collections.defaultdict(list)
|
opts = list()
|
||||||
module_names = _list_module_names()
|
module_names = _list_module_names()
|
||||||
imported_modules = _import_modules(module_names)
|
imported_modules = _import_modules(module_names)
|
||||||
_append_config_options(imported_modules, opts)
|
for mod in imported_modules:
|
||||||
return _tupleize(opts)
|
opts.extend(mod.list_opts())
|
||||||
|
return opts
|
||||||
|
|
||||||
|
|
||||||
def _list_module_names():
|
def _list_module_names():
|
||||||
@@ -75,21 +70,3 @@ def _import_modules(module_names):
|
|||||||
else:
|
else:
|
||||||
imported_modules.append(mod)
|
imported_modules.append(mod)
|
||||||
return imported_modules
|
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)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user