-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathCatPredicate.py
266 lines (220 loc) · 8.21 KB
/
CatPredicate.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
import re
import wx
import wx.lib.intctrl
import os
import itertools
import Utils
from DNSManager import AutoWidthListCtrl
import ReadSignOnSheet
import Model
def SetToIntervals( s ):
if not s:
return []
seq = sorted( s )
intervals = [(seq[0], seq[0])]
for num in itertools.islice(seq, 1, len(seq)):
if num <= intervals[-1][1]:
pass
elif num == intervals[-1][1] + 1:
intervals[-1] = (intervals[-1][0], num)
else:
intervals.append( (num, num) )
return intervals
def IntervalsToSet( intervals ):
ret = set()
for i in intervals:
ret.update( range(i[0], i[1]+1) )
return ret
class CategoryPredicate:
def __init__( self ):
# None == ignore
self.numberSet = None
self.genderMatch = None
self.ageRange = None
self.categoryMatch = None
self.intervals = []
self.exclude = set()
def match( self, info ):
isMatch = True
bib, gender, category, age = [
info.get('Bib#', -1),
info.get('Gender', ReadSignOnSheet.ExcelLink.OpenCode),
info.get('Category','').lower(),
info.get('Age',-1)]
if isMatch and bib in self.exclude:
isMatch = False
if isMatch and self.intervals is not None:
isMatch = False
for i in self.intervals:
if i[0] <= bib <= i[1]:
isMatch = True
break
if isMatch and self.genderMatch is not None:
isMatch = (gender == self.genderMatch)
if isMatch and self.categoryMatch is not None:
isMatch = False
for c in self.categoryMatch:
if category == c.lower():
isMatch = True
break
if isMatch and self.ageRange is not None:
isMatch = (self.ageRange[0] <= age <= self.ageRange[1])
return isMatch
badRangeCharsRE = re.compile( '[^0-9,-]' )
nonDigits = re.compile( '[^0-9]' )
def _getStr( self ):
s = ['{}'.format(i[0]) if i[0] == i[1] else '%d-%d' % i for i in self.intervals]
s.extend( ['-{}'.format(i[0]) if i[0] == i[1] else '-{}-{}'.format(*i) for i in SetToIntervals(self.exclude)] )
s = ','.join( s )
if self.genderMatch is not None:
s += ';Gender=' + ['Open', 'Men', 'Women'][self.genderMatch]
if self.categoryMatch is not None:
s += ';Category={' + '|'.join(self.categoryMatch) + '}'
if self.ageRange is not None:
s += ';Age=[{}..{}]'.format( self.ageRange[0], self.ageRange[1] )
def _setStr( self, sIn ):
self.genderMatch = None
self.categoryMatch = None
self.ageRange = None
for s in sIn.split(';'):
s = s.strip()
if s.startswith( 'Gender=' ):
f = s.split('=', 1)[1].strip()
genderFirstChar = f[1:].lower()
if genderFirstChar in 'mh':
self.genderMatch = 1
elif genderFirstChar in 'wfl':
self.genderMatch = 2
else:
self.genderMatch = 0
elif s.startswith( 'Category=' ):
f = s.split('=', 1)[1].strip()
f = f[1:-1] # Remove braces.
cats = f.split('|')
self.categoryMatch = [c.strip() for c in cats]
if not self.categoryMatch:
self.categoryMatch = None
elif s.startswith( 'Age=' ):
self.ageRange = []
f = s.split('=')[1].strip()
for r in f.split( '..' ):
r = self.nonDigits.sub( '', r )
try:
self.ageRange.append( int(r) )
except Exception as e:
self.ageRange = None
break
if self.ageRange is not None:
if len(self.ageRange) != 2:
self.ageRange = None
else:
self.ageRange = [self.ageRange[0], self.ageRange[-1]]
if self.ageRange[0] > self.ageRange[1]:
self.ageRange[0], self.ageRange[1] = self.ageRange[1], self.ageRange[0]
else:
s = self.badRangeCharsRE.sub( '', '{}'.format(s) )
self.intervals = []
self.exclude = set()
for f in s.split(','):
if not f:
continue
try:
if f.startswith('-'): # Check for exclusion.
f = f[1:]
isExclusion = True
else:
isExclusion = False
bounds = [int(b) for b in f.split('-') if b]
if not bounds:
continue
if len(bounds) > 2: # Fix numbers not in proper x-y range format.
del bounds[2:]
elif len(bounds) == 1:
bounds.append( bounds[0] )
bounds[0] = min(bounds[0], 99999) # Keep the numbers in a reasonable range to avoid crashing.
bounds[1] = min(bounds[1], 99999)
if bounds[0] > bounds[1]: # Swap the range if out of order.
bounds[0], bounds[1] = bounds[1], bounds[0]
if isExclusion:
self.exclude.update( range(bounds[0], bounds[1]+1) )
else:
self.intervals.append( tuple(bounds) )
except Exception as e:
# Ignore any parsing errors.
print( e )
self.intervals.sort()
predicate = property(_getStr, _setStr)
def set( self, numberRange = None, genderMatch = None, categoryMatch = None, ageRange = None ):
pass
class CategoryPredicateDialog( wx.Dialog ):
def __init__( self, parent, catPredicate, id = wx.ID_ANY ):
super().__init__( parent, id, "Category Predicate",
style=wx.DEFAULT_DIALOG_STYLE|wx.TAB_TRAVERSAL )
self.catPredicate = catPredicate
border = 2
bs = wx.GridBagSizer(vgap=3, hgap=3)
row = 0
numbersLabel = wx.StaticText( self, label='{}:'.format(_('Numbers')) )
bs.Add( numbersLabel, pos=(row,0), span=(1,1),
border = border, flag=wx.LEFT|wx.TOP|wx.BOTTOM|wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_RIGHT )
self.numbers = wx.TextCtrl( self )
bs.Add( self.numbers, pos=(row,1), span=(1,1),
border = border, flag=wx.RIGHT|wx.TOP|wx.BOTTOM|wx.ALIGN_CENTER_VERTICAL|wx.EXPAND )
row += 1
genderLabel = wx.StaticText( self, label = '{}:'.format(_('Gender')) )
bs.Add( genderLabel, pos=(row,0), span=(1,1),
border = border, flag=wx.LEFT|wx.TOP|wx.BOTTOM|wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_RIGHT )
self.gender = wx.Choice( self, choices=['Open', 'Men', 'Women'] )
bs.Add( self.gender, pos=(row,1), span=(1,1),
border = border, flag=wx.RIGHT|wx.TOP|wx.BOTTOM|wx.ALIGN_CENTER_VERTICAL|wx.EXPAND )
self.gender.SetSelection( 0 )
row += 1
ageLabel = wx.StaticText( self, label = '{}:'.format(_('Age')) )
bs.Add( ageLabel, pos=(row,0), span=(1,1),
border = border, flag=wx.LEFT|wx.TOP|wx.BOTTOM|wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_RIGHT )
hs = wx.BoxSizer( wx.HORIZONTAL )
self.ageRange = [None, None]
self.ageRange[0] = wx.lib.intctrl.IntCtrl( self, min = 0, max = 110, allow_none = True, limited = True,
size=(32,-1) )
self.ageRange[1] = wx.lib.intctrl.IntCtrl( self, min = 0, max = 110, allow_none = True, limited = True,
size=(32,-1) )
hs.Add( self.ageRange[0] )
hs.Add( wx.StaticText(self, label = ' ' + _('to') + ' '), flag=wx.ALIGN_CENTER_VERTICAL )
hs.Add( self.ageRange[1] )
bs.Add( hs, pos = (row,1), span=(1,1),
border = border, flag=wx.LEFT|wx.TOP|wx.BOTTOM|wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_LEFT )
row += 1
categoryLabel = wx.StaticText( self, label = '{}:'.format(_('Categories')) )
bs.Add( categoryLabel, pos=(row,0), span=(1,1),
border = 8, flag=wx.LEFT|wx.TOP|wx.ALIGN_TOP|wx.ALIGN_RIGHT )
self.il = wx.ImageList(16, 16)
self.sm_rt = self.il.Add(wx.Bitmap( os.path.join(Utils.getImageFolder(), 'SmallRightArrow.png'), wx.BITMAP_TYPE_PNG))
self.sm_up = self.il.Add(wx.Bitmap( os.path.join(Utils.getImageFolder(), 'SmallUpArrow.png'), wx.BITMAP_TYPE_PNG))
self.sm_dn = self.il.Add(wx.Bitmap( os.path.join(Utils.getImageFolder(), 'SmallDownArrow.png'), wx.BITMAP_TYPE_PNG ))
self.categoryList = AutoWidthListCtrl( self, size=(-1,180), style = wx.LC_REPORT
| wx.BORDER_SUNKEN
| wx.LC_HRULES
)
self.categoryList.SetImageList(self.il, wx.IMAGE_LIST_SMALL)
self.categoryList.InsertColumn(0, _("Category"))
bs.Add( self.categoryList, pos=(row,1), span=(1,1),
border = border, flag=wx.RIGHT|wx.TOP|wx.BOTTOM|wx.EXPAND )
row += 1
btnSizer = self.CreateStdDialogButtonSizer( wx.OK|wx.CANCEL )
border = 4
if btnSizer:
row += 1
bs.Add( btnSizer, pos=(row, 0), span=(1,2), flag=wx.ALL|wx.EXPAND, border = border )
bs.AddGrowableCol( 1 )
bs.AddGrowableRow( row - 2 )
self.SetSizerAndFit(bs)
bs.Fit( self )
if __name__ == '__main__':
app = wx.App(False)
mainWin = wx.Frame(None,title="CrossMan", size=(1024,600))
Model.newRace()
Model.race.enableJChipIntegration = True
catPredicate = CategoryPredicate()
catPredicateDialog = CategoryPredicateDialog(mainWin, catPredicate )
catPredicateDialog.ShowModal()
app.MainLoop()