1
|
1 |
|
import logging |
2
|
1 |
|
import os |
3
|
|
|
|
4
|
1 |
|
log = logging.getLogger(__name__) |
5
|
|
|
|
6
|
1 |
|
shared_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')) |
7
|
|
|
|
8
|
|
|
|
9
|
1 |
|
def import_modules(directory, include=None, exclude=None): |
10
|
1 |
|
base_name = os.path.relpath(directory, shared_dir).replace(os.path.sep, '.') |
11
|
|
|
|
12
|
1 |
|
log.debug('Importing modules from: %s', base_name) |
13
|
|
|
|
14
|
1 |
|
for name in os.listdir(directory): |
15
|
|
|
# Match `name` against filters |
16
|
1 |
|
if include and name not in include: |
17
|
|
|
continue |
18
|
|
|
|
19
|
1 |
|
if exclude and name in exclude: |
20
|
1 |
|
continue |
21
|
|
|
|
22
|
|
|
# Build module path |
23
|
1 |
|
path = os.path.join(directory, name) |
24
|
|
|
|
25
|
|
|
# Check if `path` has a valid module |
26
|
1 |
|
if not is_module(path, name): |
27
|
|
|
continue |
28
|
|
|
|
29
|
|
|
# Import module |
30
|
1 |
|
if not import_module(path, base_name, name): |
31
|
|
|
log.warn('Unable to import module: %r', name) |
32
|
|
|
|
33
|
|
|
|
34
|
1 |
|
def import_module(path, base_name, name): |
35
|
|
|
# Get full module name |
36
|
1 |
|
full_name = get_name(path, base_name, name) |
37
|
|
|
|
38
|
1 |
|
if full_name is None: |
39
|
|
|
return False |
40
|
|
|
|
41
|
|
|
# Try import module |
42
|
1 |
|
try: |
43
|
1 |
|
__import__(full_name) |
44
|
1 |
|
return True |
45
|
|
|
except Exception, ex: |
46
|
|
|
log.error('Exception raised trying to import module %r: %s', full_name, ex, exc_info=True) |
47
|
|
|
|
48
|
|
|
return False |
49
|
|
|
|
50
|
|
|
|
51
|
1 |
|
def get_name(path, base_name, name): |
52
|
|
|
# Strip extension from module files |
53
|
1 |
|
if os.path.isfile(path): |
54
|
1 |
|
name, _ = os.path.splitext(name) |
55
|
|
|
|
56
|
|
|
# Return file module name |
57
|
1 |
|
return '%s.%s' % ( |
58
|
|
|
base_name, |
59
|
|
|
name |
60
|
|
|
) |
61
|
|
|
|
62
|
|
|
|
63
|
1 |
|
def is_module(path, name): |
64
|
|
|
# Check module file is valid |
65
|
1 |
|
if os.path.isfile(path): |
66
|
1 |
|
return is_module_file(path, name) |
67
|
|
|
|
68
|
|
|
# Check module directory is valid |
69
|
1 |
|
if os.path.isdir(path): |
70
|
1 |
|
return is_module_directory(path, name) |
71
|
|
|
|
72
|
1 |
|
return False |
73
|
|
|
|
74
|
|
|
|
75
|
1 |
|
def is_module_directory(path, name): |
76
|
|
|
# Ignore directories without an "__init__.py" file |
77
|
1 |
|
if not os.path.exists(os.path.join(path, '__init__.py')): |
78
|
1 |
|
return False |
79
|
|
|
|
80
|
|
|
# Ignore directories with a "." or "_" prefix |
81
|
1 |
|
if name.startswith('.') or name.startswith('_'): |
82
|
1 |
|
log.info('Ignoring invalid module: %r', path) |
83
|
1 |
|
return False |
84
|
|
|
|
85
|
|
|
# Ignore directories with a "." suffix |
86
|
1 |
|
if name.endswith('.'): |
87
|
1 |
|
log.info('Ignoring invalid module: %r', path) |
88
|
1 |
|
return False |
89
|
|
|
|
90
|
1 |
|
return True |
91
|
|
|
|
92
|
|
|
|
93
|
1 |
|
def is_module_file(path, name): |
94
|
|
|
# Ignore files without the "*.py" extension |
95
|
1 |
|
if not name.endswith('.py'): |
96
|
1 |
|
return False |
97
|
|
|
|
98
|
|
|
# Split extension from `name` |
99
|
1 |
|
name, _ = os.path.splitext(name) |
100
|
|
|
|
101
|
|
|
# Ignore files with a "." or "_" prefix |
102
|
1 |
|
if name.startswith('.') or name.startswith('_'): |
103
|
1 |
|
log.info('Ignoring invalid module: %r', path) |
104
|
1 |
|
return False |
105
|
|
|
|
106
|
|
|
# Ignore files with a "." suffix |
107
|
1 |
|
if name.endswith('.'): |
108
|
1 |
|
log.info('Ignoring invalid module: %r', path) |
109
|
1 |
|
return False |
110
|
|
|
|
111
|
|
|
# Valid module |
112
|
|
|
return True |
113
|
|
|
|