-
Notifications
You must be signed in to change notification settings - Fork 4
/
base.py
68 lines (49 loc) · 1.56 KB
/
base.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
import os
_validators = []
class BaseValidator(object):
def __init__(self, *args, **kwargs):
pass
async def init(self):
pass
async def prepare(self, change):
pass
async def check(self, change):
pass
class Validator(BaseValidator):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._instances = []
for validator in _validators:
instance = validator(*args, **kwargs)
self._instances.append(instance)
async def init(self, ignore=frozenset()):
all_new = []
for instance in self._instances:
if instance.handle in ignore:
continue
new = await instance.init()
all_new.extend(new)
return all_new
async def prepare(self, change):
for instance in self._instances:
await instance.prepare(change)
async def check(self, change):
all_new = []
all_fixed = []
for instance in self._instances:
new, fixed = await instance.check(change)
all_new.extend(new)
all_fixed.extend(fixed)
return all_new, all_fixed
def register(cls):
_validators.append(cls)
return cls
def load_validators():
main = os.path.dirname(__file__)
for root, folders, files in os.walk(main):
if root == main or '__init__.py' not in files:
continue
__import__('.'.join(
__name__.split('.')[:-1] + os.path.relpath(root, main).split(os.path.sep))
)
load_validators()