Coverage for watcher/conf/opts.py: 92%

28 statements  

« prev     ^ index     » next       coverage.py v7.8.2, created at 2025-06-17 12:22 +0000

1# Copyright 2016 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 Watcher. 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 watcher.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 importlib 

30import os 

31import pkgutil 

32 

33LIST_OPTS_FUNC_NAME = "list_opts" 

34 

35 

36def list_opts(): 

37 """Grouped list of all the Watcher-specific configuration options 

38 

39 :return: A list of ``(group, [opt_1, opt_2])`` tuple pairs, where ``group`` 

40 is either a group name as a string or an OptGroup object. 

41 """ 

42 opts = list() 

43 module_names = _list_module_names() 

44 imported_modules = _import_modules(module_names) 

45 for mod in imported_modules: 

46 opts.extend(mod.list_opts()) 

47 return 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("watcher.conf." + modname) 

65 if not hasattr(mod, LIST_OPTS_FUNC_NAME): 65 ↛ 66line 65 didn't jump to line 66 because the condition on line 65 was never true

66 msg = "The module 'watcher.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