-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrule.py
72 lines (53 loc) · 2.06 KB
/
rule.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
import re
from languagetype import LanguageType
class Rule(object):
""" A Rule is a base class for performing some action. An example of a rule is when parsing, if field ends in Id, map to int. """
enabled = True
def __init__(self):
pass
def description(self):
pass
def run(self, value):
pass
class ParseRule(Rule):
comparison = '' # endswith, startswith, contains, matches (Can be a enum)
comparedTo = '' # what should go in the comparison
def __init__(self, comparison, comparedTo, mapTo):
super().__init__()
self.comparison = comparison
self.comparedTo = comparedTo
self.mapTo = mapTo
def run(self, value):
super().run(value)
if self.comparison == 'endswith':
if value.endswith(self.comparedTo):
return self.mapTo
elif self.comparison == 'startswith':
if value.startswith(self.comparedTo):
return self.mapTo
elif self.comparison == 'contains':
if self.comparedTo in value:
return self.mapTo
elif self.comparison == 'matches':
regex = re.compile(r'(?P<Type>' + self.comparedTo + ')', re.IGNORECASE)
m = re.search(regex, value)
if m is None:
return None
elif len(m.group('Type')) is not len(value):
return None
return self.mapTo
return None
def description(self):
return 'On Parse: VALUE {0} {1}, return {2}'.format(self.comparison, self.comparedTo, self.mapTo)
class RenameRule(Rule):
""" Used to map a LanguageType (generic enum) to an actual string """
def __init__(self, source, target):
super().__init__()
self.source_type = source
self.target_type = target
def description(self):
return 'On Rename: {1} -> {2}'.format(self.source_type, self.target_type)
def run(self, source):
if source == self.source_type:
return self.target_type
return None