-
Notifications
You must be signed in to change notification settings - Fork 0
/
trans.py
29 lines (25 loc) · 1.22 KB
/
trans.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
from . import MatchTrans, FormatTrans
class Trans():
def __init__(self, template, forward=None, reverse=None, format=None, match=None, strict=False, debug=False):
'''
forward and format are the same function. Only one can be defined at a time.
reverse and match are the same function. Only one can be defined at a time.
'''
if not reverse and not forward and not format and not match:
raise Exception("(forward/format) or/and (reverse/match) transformation function must be present")
self.match = None
self.format = None
self.template = template
if reverse:
self.match = MatchTrans(template, reverse, strict, debug)
elif match:
self.match = MatchTrans(template, match, strict, debug)
if forward:
self.format = FormatTrans(template, forward, strict, debug)
elif format:
self.format = FormatTrans(template, format, strict, debug)
def transform(self, *args, dir='v', **kwargs):
if dir == 'format' and self.format:
return self.format.transform(*args, **kwargs)
elif dir == 'match' and self.match:
return self.match.transform(*args, **kwargs)