-
Notifications
You must be signed in to change notification settings - Fork 25
/
Validator.py
207 lines (171 loc) · 4.78 KB
/
Validator.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
# The Admin4 Project
# (c) 2013-2022 Andreas Pflug
#
# Licensed under the Apache License,
# see LICENSE.TXT for conditions of usage
import wx
import time
from wh import prettyDate
class Validator():
@staticmethod
def Get(name):
return Validator.validators.get(name.lower())
def __init__(self, ctl, _unused_params=None):
self.chars=[]
self.ctl=ctl
def OnChar(self, ev):
keycode = int(ev.GetKeyCode())
if keycode < 256 and keycode >31:
if not chr(keycode) in self.chars:
return
a,e=self.ctl.GetSelection()
if a == e:
l=len(self.ctl.GetValue())
if l >= self.len:
return
ev.Skip()
def GetValue(self):
return self.ctl.GetValue()
def SetValue(self, val):
self.ctl.SetValue(str(val))
class UIntValidator(Validator):
def __init__(self, ctl, params):
Validator.__init__(self, ctl)
if len(params):
self.len=int(params[0])
else:
self.len=99
self.chars="0123456789"
ctl.Bind(wx.EVT_CHAR, self.OnChar)
def GetValue(self):
v=self.ctl.GetValue()
if v:
return int(v)
return None
def SetValue(self, val):
if val or isinstance(val, int):
self.ctl.SetValue(str(int(val)))
else:
self.ctl.SetValue("")
class IntValidator(UIntValidator):
def __init__(self, ctl, params):
UIntValidator.__init__(self, ctl, params)
def OnChar(self, ev):
keycode = int(ev.GetKeyCode())
if keycode < 256 and keycode >31:
if self.ctl.GetInsertionPoint() == 0:
a,e=self.ctl.GetSelection()
if e == a:
if self.ctl.GetValue()[0:1] == '-':
return
if chr(keycode) == '-':
ev.Skip()
return
Validator.OnChar(self, ev)
def GetValue(self):
v=self.ctl.GetValue()
if v and v != "-":
return int(v)
return None
class MacValidator(Validator):
def __init__(self, ctl, _unused_params):
Validator.__init__(self, ctl)
self.len=17
self.chars="0123456789abcdef"
ctl.Bind(wx.EVT_CHAR, self.OnChar)
def IsValid(self):
return len(self.ctl.GetValue() == self.len)
def GetValue(self):
v=self.ctl.GetValue()
if len(v) == self.len:
return v.lower().replace('.', ':').replace('-', ':')
return None
def OnChar(self, ev):
keycode = int(ev.GetKeyCode())
if keycode < 256 and keycode >31:
if self.ctl.GetInsertionPoint() == 17:
return
if len(self.ctl.GetValue()) == 17:
a,e=self.ctl.GetSelection()
if a == e:
return
if self.ctl.GetInsertionPoint() in (2,5,8,11,14):
if not chr(keycode) in ".-:":
return
else:
if not chr(keycode) in self.chars:
return
ev.Skip()
class TimestampValidator(Validator):
def __init__(self, ctl, _unused_params):
Validator.__init__(self, ctl)
def getFormat(self):
tmp=self.ctl.GetValue()
if len(tmp) < 12:
return "%Y-%m-%d"
elif len(tmp.split(':')) < 3:
return "%Y-%m-%d %H:%M"
else:
return "%Y-%m-%d %H:%M:%S"
def IsValid(self):
value=self.ctl.GetValue()
if value:
try:
time.strptime(value, self.getFormat())
except:
return False
return True
def GetValue(self):
try:
ts=time.strptime(self.ctl.GetValue(), self.getFormat())
return time.mktime(ts)
except:
return None
def SetValue(self, value):
if value:
self.ctl.SetValue(prettyDate(value, True))
else:
self.ctl.SetValue("")
class IntTimestampValidator(TimestampValidator):
def GetValue(self):
value=TimestampValidator.GetValue(self)
if value != None:
return int(value)
return None
class BoolValidator(Validator):
def __init__(self, ctl, params):
Validator.__init__(self, ctl)
self.TrueValue=None
self.FalseValue=None
if len(params):
try:
self.TrueValue=int(params[0])
except:
self.TrueValue=params[0]
if len(params) > 1:
try:
self.FalseValue=int(params[1])
except:
self.FalseValue=params[1]
def GetValue(self):
if self.TrueValue == self.FalseValue:
return self.ctl.GetValue()
elif self.ctl.GetValue():
return self.TrueValue
else:
return self.FalseValue
def SetValue(self, value):
if self.TrueValue == self.FalseValue:
self.ctl.SetValue(value)
elif self.TrueValue != None:
self.ctl.SetValue(value == self.TrueValue)
else:
self.ctl.SetValue(value != self.FalseValue)
Validator.validators = {
'uint' : UIntValidator,
'int': IntValidator,
'bool': BoolValidator,
'timestamp': TimestampValidator,
'int_ts': IntTimestampValidator,
'mac': MacValidator
}