-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdynamic_loader.py
More file actions
26 lines (20 loc) · 1 KB
/
dynamic_loader.py
File metadata and controls
26 lines (20 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/python3
#encoding:utf-8
#Tutorial: http://www.knight-of-pi.org/loading-python-modules-dynamically-with-importlib-and-getattr
#Licence: http://creativecommons.org/licenses/by-nc-sa/3.0/
# Author: Johannes Bergs
import glob, importlib
def load_components(source='./components/'):
""" Import a number of modules from a given source dynamically for
all modules following the pattern source/xyz/xyz.py with a class Xyz """
components = []
device_directories = [device for device in glob.glob(source + u"*/")
if not '__pycache__/' in device]
for directory in device_directories:
component = directory[len(source):].strip('/')
component_path = source.strip('./').strip('/') + 2 * ('.' + component)
mod = importlib.import_module(component_path)
components.append(getattr(mod, component.capitalize()))
return components
if __name__ == "__main__":
print([component.__name__ for component in load_components()])