move module loading to one function
paulfantom committed
Jun 13, 2016 at 12:34 UTC
c3e7f54c8be542c2532be3da50d02431be14f417
1 file changed
+25
-29
plugins.d/python.d.plugin
+25
-29
@@ -25,31 +25,13 @@ class PythonCharts(object):
25
if interval is None:
26
interval = 1
27
self.default_priority = 90000
28
- # check if plugin directory exists
29
- if not os.path.isdir(modules_path):
30
- debug("cannot find charts directory ", modules_path)
31
- sys.stdout.write("DISABLE\n")
32
- sys.exit(1)
28
# set configuration directory
29
self.configs = modules_configs
30
31
# load modules
37
- self.modules = []
38
- if len(modules) > 0:
39
- for m in modules:
40
- mod = self.import_plugin(modules_path + m + ".chart.py")
41
- if mod is not None:
42
- self.modules.append(mod)
43
- else:
44
- self.autoload_modules(modules_path)
32
+ self.modules = self.load_modules(modules_path,modules)
33
46
- # check if there are any modules loaded
47
- if len(self.modules) == 0:
48
- debug("cannot find modules", modules_path)
49
- sys.stdout.write("DISABLE\n")
50
- sys.exit(1)
51
-
52
- # check if loaded modules is on disabled modules list
34
+ # check if loaded modules are on disabled modules list
35
for mod in self.modules:
36
if mod.__name__ in modules_disabled:
37
self.modules.remove(mod)
@@ -90,15 +72,29 @@ class PythonCharts(object):
72
debug(str(e))
73
return None
74
93
- def autoload_modules(self, path):
94
- # scan directory specified in path and load all modules from there
95
- # function modifies self.modules
96
- names = os.listdir(path)
97
- for mod in names:
98
- m = self.import_plugin(path + mod)
99
- if m is not None:
100
- debug("loading chart: '" + path + mod + "'")
101
- self.modules.append(m)
75
+ def load_modules(self, path, modules):
76
+ # check if plugin directory exists
77
+ if not os.path.isdir(path):
78
+ debug("cannot find charts directory ", path)
79
+ sys.stdout.write("DISABLE\n")
80
+ sys.exit(1)
81
+
82
+ # load modules
83
+ loaded = []
84
+ if len(modules) > 0:
85
+ for m in modules:
86
+ mod = self.import_plugin(path + m + ".chart.py")
87
+ if mod is not None:
88
+ loaded.append(mod)
89
+ else:
90
+ # scan directory specified in path and load all modules from there
91
+ names = os.listdir(path)
92
+ for mod in names:
93
+ m = self.import_plugin(path + mod)
94
+ if m is not None:
95
+ debug("loading chart: '" + path + mod + "'")
96
+ loaded.append(m)
97
+ return loaded
98
99
def load_configs(self):
100
# function modifies every loaded module in self.modules