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
28
# \ Next character is a literal
29
# a Abbreviated weekday name (Sun..Sat)
30
# b Abbreviated month name (Jan..Dec)
31
# d day of month (01..31)
34
# j day of year (001..366)
39
# u week number of year with Sunday as first day of
41
# v week number of year with Monday as first day of
45
# Predefined literals: "/-.:, "
54
from BaseMask import BaseMask, MaskSection, Literal
55
from gnue.common.utils.GDateTime import GDateTime, InvalidDate
56
from FormatExceptions import *
58
class DateLiteral (Literal):
59
def addSelfToDate(self, value, date):
62
def isValidEntry(self, value):
63
return value == self.literal or value in predefinedDateLiterals
65
class NumberMask (BaseMask):
66
def __init__(self, outputMask, inputMask=None, outputMask2=None):
68
self.predefinedLiterals = predefinedDateLiterals
70
self.basetype = "date"
71
self.literalClass = DateLiteral
73
# TODO: Make NumberMask's defaultmask be based on locale settings
74
self.defaultmask = "m/d/y"
77
'a': _aSection, 'A': _ASection,
78
'b': _bSection, 'B': _BSection,
79
'c': _cSection, 'd': _dSection,
80
'D': _DSection, 'h': _hSection,
82
'g': _gSection, 'G': _GSection,
83
'j': _jSection, 'J': _JSection,
84
'm': _mSection, 'M': _mSection,
85
'i': _iSection, 'I': _ISection,
86
'p': _pSection, 'P': _PSection,
87
's': _sSection, 'S': _SSection,
88
'u': _uSection, 'U': _USection,
89
'v': _vSection, 'V': _VSection,
91
'x': _xSection, 'X': _XSection,
92
'y': _ySection, 'Y': _YSection }
94
BaseMask.__init__(self, outputMask, inputMask, outputMask2)
96
self.value = GDateTime()
98
self.inputMaskPos = []
99
self.inputMaskLen = []
100
for i in range(len(self.inputHandlers)):
101
self.inputMaskPos.append(0)
102
self.inputMaskLen.append(0)
105
def processEdit (self, str, pos=0, replaces=0):
110
nv = "%s%s%s" % (nv[:pos],str,nv[pos+replaces:])
113
while section < len(self.inputMaskPos) and \
114
self.inputMaskPos[section] < pos:
119
if not self.inputHandlers[section]\
120
.isValidEntry(nv[self.inputMaskPos[section]:i+1]):
121
if i == self.inputMaskPos[section] and \
122
not isinstance(self.inputHandlers[section],Literal):
123
self.inputMaskLen[section] = i - self.inputMaskPos[section]
126
self.inputMaskLen[section] = i - self.inputMaskPos[section]
127
if section == len(self.inputHandlers) - 1:
132
self.inputMaskPos[section] = i
133
self.inputMaskLen[section] = 0
137
self.inputMaskLen[section] = i - self.inputMaskPos[section]
139
for i in range(section+1, len(self.inputHandlers)):
140
self.inputMaskLen[i] = 0
147
def getFormattedOutput(self, secondary=0):
149
value = self._buildDate(self.entry, 1)
151
handlers = self.output2Handlers
153
handlers = self.outputHandlers
156
for m in self.outputHandlers:
157
rv += m.getFormattedValue(value)
160
except InvalidDate, msg:
161
raise InvalidEntry, msg
164
def getFormattedDisplay(self):
166
value = self._buildDate(self.entry, 1)
169
for m in self.outputHandlers:
170
rv += m.getFormattedValue(value)
173
except InvalidDate, msg:
174
raise InvalidEntry, msg
177
def getFormattedInput(self, padding=""):
179
value = self._buildDate(self.entry, 0)
181
# self.lastInputPos =
183
print self.inputMaskLen
185
for i in range(len(self.inputHandlers)):
186
if self.inputMaskLen[i] or isinstance(self.inputHandlers[i],Literal):
187
rv += self.inputHandlers[i].getFormattedValue(value)
189
rv += padding * self.inputHandlers[i].maxLength
194
def _buildDate(self, value, mustValidate=0):
197
for i in range(len(self.inputHandlers)):
198
if self.inputMaskLen[i] or isinstance(self.inputHandlers[i],Literal):
199
self.inputHandlers[i].addSelfToDate(
200
value[ self.inputMaskPos[i]: \
201
self.inputMaskPos[i] + self.inputMaskLen[i]], date )
203
tmsg = _("Invalid Entry")
204
raise InvalidEntry, tmsg
210
class _baseDateSection (MaskSection):
214
def isValidEntry(self, value):
217
def addSelfToDate(self, value, date):
221
class _aSection(_baseDateSection):
223
_baseDateSection.__init__(self)
227
def getFormattedValue(self, date):
228
return weekdayAbbrevNames[date.getDayOfWeek()]
230
def isValidEntry(self, value):
233
def addSelfToDate(self, value, date):
237
class _ASection(_aSection):
238
def getFormattedValue(self, date):
239
return weekdayNames[date.getDayOfWeek()]
242
class _bSection(_baseDateSection):
244
_baseDateSection.__init__(self)
248
def getFormattedValue(self, date):
249
return monthAbbrevNames[(date.month or 1) - 1]
251
def isValidEntry(self, value):
254
def addSelfToDate(self, value, date):
258
class _BSection(_bSection):
259
def getFormattedValue(self, date):
260
return monthNames[(date.month or 1) - 1]
263
class _cSection(_baseDateSection):
265
_baseDateSection.__init__(self)
269
def getFormattedValue(self, date):
270
# TODO: Implement this mask element
271
return "*" * self.maxLength
273
def isValidEntry(self, value):
276
def addSelfToDate(self, value, date):
280
class _dSection(_baseDateSection):
282
_baseDateSection.__init__(self)
286
def getFormattedValue(self, date):
287
return "%02i" % date.day
289
def isValidEntry(self, value):
292
return (v >= 0 and v <= 31) or (v == 0 and len(value) == 1)
296
def addSelfToDate(self, value, date):
297
date.day = int(value or 1)
300
class _DSection(_dSection):
301
def getFormattedValue(self, date):
302
return "%i" % date.day
305
class _hSection(_baseDateSection):
307
_baseDateSection.__init__(self)
311
def getFormattedValue(self, date):
312
return "%02i" % date.hour
314
def isValidEntry(self, value):
317
def addSelfToDate(self, value, date):
318
date.hour = int(value or 0)
321
class _HSection(_hSection):
322
def getFormattedValue(self, date):
323
return "%i" % date.hour
326
class _gSection(_baseDateSection):
328
_baseDateSection.__init__(self)
332
def getFormattedValue(self, date):
333
# TODO: Implement this mask element
334
return "*" * self.maxLength
336
def isValidEntry(self, value):
339
def addSelfToDate(self, value, date):
343
class _GSection(_gSection):
344
def getFormattedValue(self, date):
345
# TODO: Implement this mask element
346
return "*" * self.maxLength
349
class _jSection(_baseDateSection):
351
_baseDateSection.__init__(self)
355
def getFormattedValue(self, date):
356
# TODO: Implement this mask element
357
return "*" * self.maxLength
359
def isValidEntry(self, value):
362
def addSelfToDate(self, value, date):
366
class _JSection(_jSection):
367
def getFormattedValue(self, date):
368
# TODO: Implement this mask element
369
return "*" * self.maxLength
372
class _mSection(_baseDateSection):
374
_baseDateSection.__init__(self)
378
def getFormattedValue(self, date):
379
return "%02i" % date.month
381
def isValidEntry(self, value):
384
return (v >= 0 and v <= 12) or (v == 0 and len(value) == 1)
388
def addSelfToDate(self, value, date):
389
date.month = int(value or 1)
392
class _MSection(_baseDateSection):
393
def getFormattedValue(self, date):
394
return "%i" % date.month
397
class _iSection(_baseDateSection):
399
_baseDateSection.__init__(self)
403
def getFormattedValue(self, date):
404
return "%02i" % date.minute
406
def isValidEntry(self, value):
409
return (v >= 0 and v <= 59) or (v == 0 and len(value) == 1)
413
def addSelfToDate(self, value, date):
414
date.minute = int(value)
417
class _ISection(_iSection):
418
def getFormattedValue(self, date):
419
return "%2i" % date.minute
422
class _pSection(_baseDateSection):
424
_baseDateSection.__init__(self)
428
def getFormattedValue(self, date):
429
# TODO: Implement this mask element
430
return "*" * self.maxLength
432
def isValidEntry(self, value):
435
def addSelfToDate(self, value, date):
439
class _PSection(_pSection):
440
def getFormattedValue(self, date):
441
# TODO: Implement this mask element
442
return "*" * self.maxLength
445
class _sSection(_baseDateSection):
447
_baseDateSection.__init__(self)
451
def getFormattedValue(self, date):
452
return "%02i" % date.second
454
def isValidEntry(self, value):
457
return (v >= 0 and v <= 59) or (v == 0 and len(value) == 1)
461
def addSelfToDate(self, value, date):
462
date.second = int(value)
465
class _SSection(_sSection):
466
def getFormattedValue(self, date):
467
return "%2i" % date.second
470
class _uSection(_baseDateSection):
472
_baseDateSection.__init__(self)
476
def getFormattedValue(self, date):
477
# TODO: Implement this mask element
478
return "*" * self.maxLength
480
def isValidEntry(self, value):
483
def addSelfToDate(self, value, date):
487
class _USection(_uSection):
488
def getFormattedValue(self, date):
489
# TODO: Implement this mask element
490
return "*" * self.maxLength
493
class _vSection(_baseDateSection):
495
_baseDateSection.__init__(self)
499
def getFormattedValue(self, date):
500
# TODO: Implement this mask element
501
return "*" * self.maxLength
503
def isValidEntry(self, value):
506
def addSelfToDate(self, value, date):
510
class _VSection(_vSection):
511
def getFormattedValue(self, date):
512
# TODO: Implement this mask element
513
return "*" * self.maxLength
516
class _wSection(_baseDateSection):
518
_baseDateSection.__init__(self)
522
def getFormattedValue(self, date):
523
# TODO: Implement this mask element
524
return "*" * self.maxLength
526
def isValidEntry(self, value):
529
def addSelfToDate(self, value, date):
533
class _xSection(_baseDateSection):
535
_baseDateSection.__init__(self)
539
def getFormattedValue(self, date):
540
# TODO: Implement this mask element
541
return "*" * self.maxLength
543
def isValidEntry(self, value):
546
def addSelfToDate(self, value, date):
550
class _XSection(_xSection):
551
def getFormattedValue(self, date):
552
# TODO: Implement this mask element
553
return "*" * self.maxLength
556
class _ySection(_baseDateSection):
558
_baseDateSection.__init__(self)
562
def getFormattedValue(self, date):
563
return "%02i" % divmod(date.year, 100)[1]
565
def isValidEntry(self, value):
568
return (v >= 0) and (v <= 99)
572
def addSelfToDate(self, value, date):
573
# TODO: Temporary year hack!
581
class _YSection(_ySection):
583
_baseDateSection.__init__(self)
587
def getFormattedValue(self, date):
588
return "%04i" % date.year
590
def isValidEntry(self, value):
593
return (v >= 1) and (v <= 9999)
597
def addSelfToDate(self, value, date):
598
date.year = int(value)