/
rules.py
350 lines (224 loc) · 9.44 KB
/
rules.py
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import os
import sys
import pkgutil
import importlib
import importlib.util
from . import exceptions
from . import discovering
_FABRICS = {}
_RULES = {}
def register(name, rule):
if name in _FABRICS:
raise exceptions.RuleAlreadyRegistered(rule=name)
_FABRICS[name] = rule
def remove(name):
if name in _FABRICS:
del _FABRICS[name]
def get_for_config(config):
uid = config.uid
if uid not in _RULES:
rules = []
for rule_config in config.rules:
fabric_type = rule_config['type']
if fabric_type not in _FABRICS:
raise exceptions.RuleNotRegistered(rule=fabric_type)
rule = _FABRICS[fabric_type](config=rule_config)
if not rule.verify_config():
raise exceptions.ConfigHasWrongFormat(path=config.path,
message='wrong format of rule {}'.format(fabric_type))
rules.append(rule)
_RULES[uid] = rules
return _RULES[uid]
def reset_rules_cache():
_RULES.clear()
class ImportCommand:
__slots__ = ('target_module', 'target_attribute', 'source_module', 'source_attribute')
def __init__(self, target_module, target_attribute, source_module, source_attribute):
self.target_module = target_module
self.target_attribute = target_attribute
self.source_module = source_module
self.source_attribute = source_attribute
def __call__(self):
imported_module = importlib.import_module(self.source_module)
if self.source_attribute is None:
value = imported_module
else:
value = getattr(imported_module, self.source_attribute)
setattr(self.target_module, self.target_attribute, value)
def __str__(self):
return 'ImportCommand({}, {}, {}, {})'.format(self.target_module,
self.target_attribute,
self.source_module,
self.source_attribute)
def __eq__(self, other):
return (self.__class__ == other.__class__ and
self.target_module == other.target_module and
self.target_attribute == other.target_attribute and
self.source_module == other.source_module and
self.source_attribute == other.source_attribute)
def __ne__(self, other):
return not self.__eq__(other)
class NoImportCommand(ImportCommand):
__slots__ = ()
def __init__(self):
super().__init__(target_module=None,
target_attribute=None,
source_module=None,
source_attribute=None)
def __call__(self):
pass
class BaseRule:
__slots__ = ('config',)
def __init__(self, config):
self.config = config
def verify_config(self):
return True
def apply(self, module, variable):
raise NotImplementedError
class CustomRule(BaseRule):
__slots__ = ()
def verify_config(self):
if 'variables' not in self.config:
return False
return super().verify_config()
def apply(self, module, variable):
if variable not in self.config['variables']:
return None
module_name = self.config['variables'][variable]['module']
attribute = self.config['variables'][variable].get('attribute')
return ImportCommand(module, variable, module_name, attribute)
class LocalModulesRule(BaseRule):
__slots__ = ()
_LOCAL_MODULES_CACHE = {}
def verify_config(self):
return super().verify_config()
def apply(self, module, variable):
package_name = getattr(module, '__package__', None)
if not package_name:
return None
if package_name not in self._LOCAL_MODULES_CACHE:
parent = sys.modules[package_name]
local_modules = set()
for module_finder, name, ispkg in pkgutil.iter_modules(path=parent.__path__):
local_modules.add(name)
self._LOCAL_MODULES_CACHE[package_name] = frozenset(local_modules)
if variable not in self._LOCAL_MODULES_CACHE[package_name]:
return None
return ImportCommand(target_module=module,
target_attribute=variable,
source_module='{}.{}'.format(package_name, variable),
source_attribute=None)
class GlobalModulesRule(BaseRule):
__slots__ = ()
def verify_config(self):
return super().verify_config()
def apply(self, module, variable):
spec = discovering.find_spec(variable)
if spec is None:
return None
return ImportCommand(target_module=module,
target_attribute=variable,
source_module=variable,
source_attribute=None)
# packages lists for every python version can be found here:
# https://github.com/jackmaney/python-stdlib-list
def _collect_stdlib_modules():
variables = {}
for compiled_module_name in sys.builtin_module_names:
variables[compiled_module_name] = {'module': compiled_module_name}
with open(os.path.join(os.path.dirname(__file__),
'fixtures',
'python_{}_{}_packages.txt'.format(sys.version_info.major, sys.version_info.minor))) as f:
for line in f.readlines():
names = line.strip().split('.')
for i in range(len(names)):
variables['_'.join(names[:i+1])] = {'module': '.'.join(names[:i+1])}
return variables
class StdLibRule(BaseRule):
__slots__ = ('_stdlib_modules',)
_STDLIB_MODULES = _collect_stdlib_modules()
def verify_config(self):
return super().verify_config()
def apply(self, module, variable):
if variable not in self._STDLIB_MODULES:
return None
module_name = self._STDLIB_MODULES[variable]['module']
attribute = self._STDLIB_MODULES[variable].get('attribute')
return ImportCommand(module, variable, module_name, attribute)
class PredefinedNamesRule(BaseRule):
__slots__ = ()
PREDEFINED_NAMES = frozenset({'__file__', '__annotations__'})
def verify_config(self):
return super().verify_config()
def apply(self, module, variable):
if variable in self.PREDEFINED_NAMES:
return NoImportCommand()
if variable in __builtins__:
return NoImportCommand()
return None
class PrefixRule(BaseRule):
__slots__ = ()
def verify_config(self):
if 'prefixes' not in self.config:
return False
for rule in self.config['prefixes']:
if 'prefix' not in rule:
return False
return super().verify_config()
def apply(self, module, variable):
for rule in self.config['prefixes']:
prefix = rule['prefix']
if not variable.startswith(prefix):
continue
return ImportCommand(module, variable, '{}.{}'.format(rule['module'], variable[len(prefix):]), None)
return None
class LocalModulesFromParentRule(BaseRule):
__slots__ = ()
def verify_config(self):
if 'suffixes' not in self.config:
return False
return super().verify_config()
def apply(self, module, variable):
package_name = module.__package__
for suffix in self.config['suffixes']:
if not package_name.endswith(suffix):
continue
base_package_name = package_name[:-len(suffix)]
source_module = '{}.{}'.format(base_package_name, variable)
if discovering.find_spec(source_module) is None:
continue
return ImportCommand(target_module=module,
target_attribute=variable,
source_module=source_module,
source_attribute=None)
class LocalModulesFromNamespaceRule(BaseRule):
__slots__ = ()
def verify_config(self):
return super().verify_config()
def apply(self, module, variable):
if 'map' not in self.config:
return False
package_name = module.__package__
for target, namespaces in self.config['map'].items():
for namespace in namespaces:
if package_name != target:
continue
spec = discovering.find_spec(namespace)
if spec is None:
continue
namespace_package = spec.parent
source_module = '{}.{}'.format(namespace_package, variable)
if discovering.find_spec(source_module) is None:
continue
return ImportCommand(target_module=module,
target_attribute=variable,
source_module=source_module,
source_attribute=None)
register('rule_predefined_names', PredefinedNamesRule)
register('rule_local_modules', LocalModulesRule)
register('rule_global_modules', GlobalModulesRule)
register('rule_custom', CustomRule)
register('rule_stdlib', StdLibRule)
register('rule_prefix', PrefixRule)
register('rule_local_modules_from_parent', LocalModulesFromParentRule)
register('rule_local_modules_from_namespace', LocalModulesFromNamespaceRule)