better handling of disabled modules
paulfantom committed
Jun 18, 2016 at 19:25 UTC
69a95bef44b46cb45773c146ad4b3ba8ea3edef5
1 file changed
+12
-9
plugins.d/python.d.plugin
+12
-9
@@ -17,6 +17,7 @@ except ImportError:
17
sys.stdout.write('DISABLE\n')
18
sys.exit(1)
19
20
+MODULE_EXTENSION = ".chart.py"
21
BASE_CONFIG = {'update_every' : 10,
22
'priority': 12345,
23
'retries' : 0}
@@ -35,11 +36,8 @@ class PythonCharts(object):
36
self.configs = modules_configs
37
38
# load modules
38
- modules = self._load_modules(modules_path,modules)
39
-
40
- # check if loaded modules are on disabled modules list
41
- loaded_modules = [ m for m in modules if m.__name__ not in modules_disabled ]
42
-
39
+ loaded_modules = self._load_modules(modules_path,modules, modules_disabled)
40
+
41
# load configuration files
42
configured_modules = self._load_configs(loaded_modules)
43
@@ -83,16 +81,16 @@ class PythonCharts(object):
81
# try to import module using only its path
82
if name is None:
83
name = path.split('/')[-1]
86
- if name[-9:] != ".chart.py":
84
+ if name[-len(MODULE_EXTENSION):] != MODULE_EXTENSION:
85
return None
88
- name = name[:-9]
86
+ name = name[:-len(MODULE_EXTENSION)]
87
try:
88
return importlib.machinery.SourceFileLoader(name, path).load_module()
89
except Exception as e:
90
debug(str(e))
91
return None
92
95
- def _load_modules(self, path, modules):
93
+ def _load_modules(self, path, modules, disabled):
94
# check if plugin directory exists
95
if not os.path.isdir(path):
96
debug("cannot find charts directory ", path)
@@ -103,13 +101,18 @@ class PythonCharts(object):
101
loaded = []
102
if len(modules) > 0:
103
for m in modules:
106
- mod = self._import_module(path + m + ".chart.py")
104
+ if m in disabled:
105
+ continue
106
+ mod = self._import_module(path + m + MODULE_EXTENSION)
107
if mod is not None:
108
loaded.append(mod)
109
else:
110
# scan directory specified in path and load all modules from there
111
names = os.listdir(path)
112
for mod in names:
113
+ if mod.strip(MODULE_EXTENSION) in disabled:
114
+ debug("disabling:",mod.strip(MODULE_EXTENSION))
115
+ continue
116
m = self._import_module(path + mod)
117
if m is not None:
118
debug("loading chart: '" + path + mod + "'")