2
# This file is part of GNU Enterprise.
4
# GNU Enterprise is free software; you can redistribute it
5
# and/or modify it under the terms of the GNU General Public
6
# License as published by the Free Software Foundation; either
7
# version 2, or (at your option) any later version.
9
# GNU Enterprise is distributed in the hope that it will be
10
# useful, but WITHOUT ANY WARRANTY; without even the implied
11
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12
# PURPOSE. See the GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public
15
# License along with program; see the file COPYING. If not,
16
# write to the Free Software Foundation, Inc., 59 Temple Place
17
# - Suite 330, Boston, MA 02111-1307, USA.
19
# Copyright 2001-2005 Free Software Foundation
29
# {proper} Display all text in Proper case
30
# {upper} Display all text in Upper case
31
# {lower} Display all text in Lower case
33
# Predefined literals: " "
43
from BaseMask import BaseMask, MaskSection, Literal
44
from gnue.common.utils.GDateTime import GDateTime, InvalidDate
45
from FormatExceptions import *
47
class DateLiteral (Literal):
48
def addSelfToDate(self, value, date):
51
def isValidEntry(self, value):
52
return value == self.literal or value in predefinedDateLiterals
54
class TextMask (BaseMask):
55
def __init__(self, outputMask, inputMask=None, outputMask2=None):
57
self.predefinedLiterals = predefinedDateLiterals
59
self.basetype = "date"
60
self.literalClass = DateLiteral
62
# TODO: Make TextMask's defaultmask be based on locale settings
63
self.defaultmask = "m/d/y"
66
'a': _aSection, 'A': _ASection,
67
'b': _bSection, 'B': _BSection,
68
'c': _cSection, 'd': _dSection,
69
'D': _DSection, 'h': _hSection,
71
'g': _gSection, 'G': _GSection,
72
'j': _jSection, 'J': _JSection,
73
'm': _mSection, 'M': _mSection,
74
'i': _iSection, 'I': _ISection,
75
'p': _pSection, 'P': _PSection,
76
's': _sSection, 'S': _SSection,
77
'u': _uSection, 'U': _USection,
78
'v': _vSection, 'V': _VSection,
80
'x': _xSection, 'X': _XSection,
81
'y': _ySection, 'Y': _YSection }
83
BaseMask.__init__(self, outputMask, inputMask, outputMask2)
85
self.value = GDateTime()
87
self.inputMaskPos = []
88
self.inputMaskLen = []
89
for i in range(len(self.inputHandlers)):
90
self.inputMaskPos.append(0)
91
self.inputMaskLen.append(0)
94
def processEdit (self, str, pos=0, replaces=0):
99
nv = "%s%s%s" % (nv[:pos],str,nv[pos+replaces:])
102
while section < len(self.inputMaskPos) and \
103
self.inputMaskPos[section] < pos:
108
if not self.inputHandlers[section]\
109
.isValidEntry(nv[self.inputMaskPos[section]:i+1]):
110
if i == self.inputMaskPos[section] and \
111
not isinstance(self.inputHandlers[section],Literal):
112
self.inputMaskLen[section] = i - self.inputMaskPos[section]
115
self.inputMaskLen[section] = i - self.inputMaskPos[section]
116
if section == len(self.inputHandlers) - 1:
121
self.inputMaskPos[section] = i
122
self.inputMaskLen[section] = 0
126
self.inputMaskLen[section] = i - self.inputMaskPos[section]
128
for i in range(section+1, len(self.inputHandlers)):
129
self.inputMaskLen[i] = 0
136
def getFormattedOutput(self, secondary=0):
138
value = self._buildDate(self.entry, 1)
140
handlers = self.output2Handlers
142
handlers = self.outputHandlers
145
for m in self.outputHandlers:
146
rv += m.getFormattedValue(value)
149
except InvalidDate, msg:
150
raise InvalidEntry, msg
153
def getFormattedDisplay(self):
155
value = self._buildDate(self.entry, 1)
158
for m in self.outputHandlers:
159
rv += m.getFormattedValue(value)
162
except InvalidDate, msg:
163
raise InvalidEntry, msg
166
def getFormattedInput(self, padding=""):
168
value = self._buildDate(self.entry, 0)
170
# self.lastInputPos =
172
print self.inputMaskLen
174
for i in range(len(self.inputHandlers)):
175
if self.inputMaskLen[i] or isinstance(self.inputHandlers[i],Literal):
176
rv += self.inputHandlers[i].getFormattedValue(value)
178
rv += padding * self.inputHandlers[i].maxLength
183
def _buildDate(self, value, mustValidate=0):
186
for i in range(len(self.inputHandlers)):
187
if self.inputMaskLen[i] or isinstance(self.inputHandlers[i],Literal):
188
self.inputHandlers[i].addSelfToDate(
189
value[ self.inputMaskPos[i]: \
190
self.inputMaskPos[i] + self.inputMaskLen[i]], date )
192
tmsg = _("Invalid Entry")
193
raise InvalidEntry, tmsg
199
class _baseDateSection (MaskSection):
203
def isValidEntry(self, value):
206
def addSelfToDate(self, value, date):
210
class _aSection(_baseDateSection):
212
_baseDateSection.__init__(self)
216
def getFormattedValue(self, date):
217
return weekdayAbbrevNames[date.getDayOfWeek()]
219
def isValidEntry(self, value):
222
def addSelfToDate(self, value, date):
226
class _ASection(_aSection):
227
def getFormattedValue(self, date):
228
return weekdayNames[date.getDayOfWeek()]
231
class _bSection(_baseDateSection):
233
_baseDateSection.__init__(self)
237
def getFormattedValue(self, date):
238
return monthAbbrevNames[(date.month or 1) - 1]
240
def isValidEntry(self, value):
243
def addSelfToDate(self, value, date):
247
class _BSection(_bSection):
248
def getFormattedValue(self, date):
249
return monthNames[(date.month or 1) - 1]
252
class _cSection(_baseDateSection):
254
_baseDateSection.__init__(self)
258
def getFormattedValue(self, date):
259
# TODO: Implement this mask element
260
return "*" * self.maxLength
262
def isValidEntry(self, value):
265
def addSelfToDate(self, value, date):
269
class _dSection(_baseDateSection):
271
_baseDateSection.__init__(self)
275
def getFormattedValue(self, date):
276
return "%02i" % date.day
278
def isValidEntry(self, value):
281
return (v >= 0 and v <= 31) or (v == 0 and len(value) == 1)
285
def addSelfToDate(self, value, date):
286
date.day = int(value or 1)
289
class _DSection(_dSection):
290
def getFormattedValue(self, date):
291
return "%i" % date.day
294
class _hSection(_baseDateSection):
296
_baseDateSection.__init__(self)
300
def getFormattedValue(self, date):
301
return "%02i" % date.hour
303
def isValidEntry(self, value):
306
def addSelfToDate(self, value, date):
307
date.hour = int(value or 0)
310
class _HSection(_hSection):
311
def getFormattedValue(self, date):
312
return "%i" % date.hour
315
class _gSection(_baseDateSection):
317
_baseDateSection.__init__(self)
321
def getFormattedValue(self, date):
322
# TODO: Implement this mask element
323
return "*" * self.maxLength
325
def isValidEntry(self, value):
328
def addSelfToDate(self, value, date):
332
class _GSection(_gSection):
333
def getFormattedValue(self, date):
334
# TODO: Implement this mask element
335
return "*" * self.maxLength
338
class _jSection(_baseDateSection):
340
_baseDateSection.__init__(self)
344
def getFormattedValue(self, date):
345
# TODO: Implement this mask element
346
return "*" * self.maxLength
348
def isValidEntry(self, value):
351
def addSelfToDate(self, value, date):
355
class _JSection(_jSection):
356
def getFormattedValue(self, date):
357
# TODO: Implement this mask element
358
return "*" * self.maxLength
361
class _mSection(_baseDateSection):
363
_baseDateSection.__init__(self)
367
def getFormattedValue(self, date):
368
return "%02i" % date.month
370
def isValidEntry(self, value):
373
return (v >= 0 and v <= 12) or (v == 0 and len(value) == 1)
377
def addSelfToDate(self, value, date):
378
date.month = int(value or 1)
381
class _MSection(_baseDateSection):
382
def getFormattedValue(self, date):
383
return "%i" % date.month
386
class _iSection(_baseDateSection):
388
_baseDateSection.__init__(self)
392
def getFormattedValue(self, date):
393
return "%02i" % date.minute
395
def isValidEntry(self, value):
398
return (v >= 0 and v <= 59) or (v == 0 and len(value) == 1)
402
def addSelfToDate(self, value, date):
403
date.minute = int(value)
406
class _ISection(_iSection):
407
def getFormattedValue(self, date):
408
return "%2i" % date.minute
411
class _pSection(_baseDateSection):
413
_baseDateSection.__init__(self)
417
def getFormattedValue(self, date):
418
# TODO: Implement this mask element
419
return "*" * self.maxLength
421
def isValidEntry(self, value):
424
def addSelfToDate(self, value, date):
428
class _PSection(_pSection):
429
def getFormattedValue(self, date):
430
# TODO: Implement this mask element
431
return "*" * self.maxLength
434
class _sSection(_baseDateSection):
436
_baseDateSection.__init__(self)
440
def getFormattedValue(self, date):
441
return "%02i" % date.second
443
def isValidEntry(self, value):
446
return (v >= 0 and v <= 59) or (v == 0 and len(value) == 1)
450
def addSelfToDate(self, value, date):
451
date.second = int(value)
454
class _SSection(_sSection):
455
def getFormattedValue(self, date):
456
return "%2i" % date.second
459
class _uSection(_baseDateSection):
461
_baseDateSection.__init__(self)
465
def getFormattedValue(self, date):
466
# TODO: Implement this mask element
467
return "*" * self.maxLength
469
def isValidEntry(self, value):
472
def addSelfToDate(self, value, date):
476
class _USection(_uSection):
477
def getFormattedValue(self, date):
478
# TODO: Implement this mask element
479
return "*" * self.maxLength
482
class _vSection(_baseDateSection):
484
_baseDateSection.__init__(self)
488
def getFormattedValue(self, date):
489
# TODO: Implement this mask element
490
return "*" * self.maxLength
492
def isValidEntry(self, value):
495
def addSelfToDate(self, value, date):
499
class _VSection(_vSection):
500
def getFormattedValue(self, date):
501
# TODO: Implement this mask element
502
return "*" * self.maxLength
505
class _wSection(_baseDateSection):
507
_baseDateSection.__init__(self)
511
def getFormattedValue(self, date):
512
# TODO: Implement this mask element
513
return "*" * self.maxLength
515
def isValidEntry(self, value):
518
def addSelfToDate(self, value, date):
522
class _xSection(_baseDateSection):
524
_baseDateSection.__init__(self)
528
def getFormattedValue(self, date):
529
# TODO: Implement this mask element
530
return "*" * self.maxLength
532
def isValidEntry(self, value):
535
def addSelfToDate(self, value, date):
539
class _XSection(_xSection):
540
def getFormattedValue(self, date):
541
# TODO: Implement this mask element
542
return "*" * self.maxLength
545
class _ySection(_baseDateSection):
547
_baseDateSection.__init__(self)
551
def getFormattedValue(self, date):
552
return "%02i" % divmod(date.year, 100)[1]
554
def isValidEntry(self, value):
557
return (v >= 0) and (v <= 99)
561
def addSelfToDate(self, value, date):
562
# TODO: Temporary year hack!
570
class _YSection(_ySection):
572
_baseDateSection.__init__(self)
576
def getFormattedValue(self, date):
577
return "%04i" % date.year
579
def isValidEntry(self, value):
582
return (v >= 1) and (v <= 9999)
586
def addSelfToDate(self, value, date):
587
date.year = int(value)