Coverage for nova/conf/opts.py: 0%

35 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-04-24 11:16 +0000

1# Copyright 2015 OpenStack Foundation 

2# All Rights Reserved. 

3# 

4# Licensed under the Apache License, Version 2.0 (the "License"); you may 

5# not use this file except in compliance with the License. You may obtain 

6# a copy of the License at 

7# 

8# http://www.apache.org/licenses/LICENSE-2.0 

9# 

10# Unless required by applicable law or agreed to in writing, software 

11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 

12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 

13# License for the specific language governing permissions and limitations 

14# under the License. 

15 

16""" 

17This is the single point of entry to generate the sample configuration 

18file for Nova. It collects all the necessary info from the other modules 

19in this package. It is assumed that: 

20 

21* every other module in this package has a 'list_opts' function which 

22 return a dict where 

23 * the keys are strings which are the group names 

24 * the value of each key is a list of config options for that group 

25* the nova.conf package doesn't have further packages with config options 

26* this module is only used in the context of sample file generation 

27""" 

28 

29import collections 

30import importlib 

31import os 

32import pkgutil 

33 

34LIST_OPTS_FUNC_NAME = "list_opts" 

35 

36 

37def _tupleize(dct): 

38 """Take the dict of options and convert to the 2-tuple format.""" 

39 return [(key, val) for key, val in dct.items()] 

40 

41 

42def list_opts(): 

43 opts = collections.defaultdict(list) 

44 module_names = _list_module_names() 

45 imported_modules = _import_modules(module_names) 

46 _append_config_options(imported_modules, opts) 

47 return _tupleize(opts) 

48 

49 

50def _list_module_names(): 

51 module_names = [] 

52 package_path = os.path.dirname(os.path.abspath(__file__)) 

53 for _, modname, ispkg in pkgutil.iter_modules(path=[package_path]): 

54 if modname == "opts" or ispkg: 

55 continue 

56 else: 

57 module_names.append(modname) 

58 return module_names 

59 

60 

61def _import_modules(module_names): 

62 imported_modules = [] 

63 for modname in module_names: 

64 mod = importlib.import_module("nova.conf." + modname) 

65 if not hasattr(mod, LIST_OPTS_FUNC_NAME): 

66 msg = "The module 'nova.conf.%s' should have a '%s' "\ 

67 "function which returns the config options." % \ 

68 (modname, LIST_OPTS_FUNC_NAME) 

69 raise Exception(msg) 

70 else: 

71 imported_modules.append(mod) 

72 return imported_modules 

73 

74 

75def _append_config_options(imported_modules, config_options): 

76 for mod in imported_modules: 

77 configs = mod.list_opts() 

78 for key, val in configs.items(): 

79 config_options[key].extend(val)