~ubuntu-branches/ubuntu/hardy/gnue-common/hardy

« back to all changes in this revision

Viewing changes to src/formatting/NumberMask.py

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Mitchell
  • Date: 2005-03-09 11:06:31 UTC
  • Revision ID: james.westby@ubuntu.com-20050309110631-8gvvn39q7tjz1kj6
Tags: upstream-0.5.14
ImportĀ upstreamĀ versionĀ 0.5.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# This file is part of GNU Enterprise.
 
3
#
 
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.
 
8
#
 
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.
 
13
#
 
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.
 
18
#
 
19
# Copyright 2001-2005 Free Software Foundation
 
20
#
 
21
# FILE:
 
22
# NumberMask.py
 
23
#
 
24
# DESCRIPTION:
 
25
#
 
26
# NOTES:
 
27
#
 
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)
 
32
#       h     hour (01..12)
 
33
#       g     hour (00..23)
 
34
#       j     day of year (001..366)
 
35
#       m     month (01..12)
 
36
#       i     minute (00..59)
 
37
#       p     am or pm
 
38
#       s     second (00..60)
 
39
#       u     week number of year with Sunday  as  first  day  of
 
40
#             week (00..53)
 
41
#       v     week  number  of  year  with Monday as first day of
 
42
#             week (01..52)
 
43
#       y     year
 
44
#
 
45
#     Predefined literals:  "/-.:, "
 
46
#
 
47
# Example: 
 
48
#
 
49
#  m/d/y                01/01/2001
 
50
#  m/y                  01/2001
 
51
#  h:i p                12:29 pm
 
52
#
 
53
 
 
54
from BaseMask import BaseMask, MaskSection, Literal
 
55
from gnue.common.utils.GDateTime import GDateTime, InvalidDate
 
56
from FormatExceptions import *
 
57
 
 
58
class DateLiteral (Literal): 
 
59
  def addSelfToDate(self, value, date):
 
60
    pass
 
61
 
 
62
  def isValidEntry(self, value): 
 
63
    return value == self.literal or value in predefinedDateLiterals
 
64
 
 
65
class NumberMask (BaseMask):
 
66
  def __init__(self, outputMask, inputMask=None, outputMask2=None): 
 
67
 
 
68
    self.predefinedLiterals = predefinedDateLiterals
 
69
 
 
70
    self.basetype = "date"
 
71
    self.literalClass = DateLiteral
 
72
 
 
73
    # TODO: Make NumberMask's defaultmask be based on locale settings
 
74
    self.defaultmask = "m/d/y"
 
75
 
 
76
    self.maskMappings = { 
 
77
      'a': _aSection,       'A': _ASection, 
 
78
      'b': _bSection,       'B': _BSection, 
 
79
      'c': _cSection,       'd': _dSection, 
 
80
      'D': _DSection,       'h': _hSection, 
 
81
      '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, 
 
90
      'w': _wSection, 
 
91
      'x': _xSection,       'X': _XSection, 
 
92
      'y': _ySection,       'Y': _YSection }
 
93
 
 
94
    BaseMask.__init__(self, outputMask, inputMask, outputMask2)
 
95
 
 
96
    self.value = GDateTime()
 
97
    self.entry = ""
 
98
    self.inputMaskPos = []
 
99
    self.inputMaskLen = []
 
100
    for i in range(len(self.inputHandlers)): 
 
101
      self.inputMaskPos.append(0)
 
102
      self.inputMaskLen.append(0)
 
103
 
 
104
 
 
105
  def processEdit (self, str, pos=0, replaces=0): 
 
106
    nv = self.entry
 
107
    if pos == len(nv): 
 
108
      nv += str
 
109
    else: 
 
110
      nv = "%s%s%s" % (nv[:pos],str,nv[pos+replaces:])
 
111
 
 
112
    section = 0 
 
113
    while section < len(self.inputMaskPos) and \
 
114
          self.inputMaskPos[section] < pos:
 
115
      section += 1
 
116
 
 
117
    i = pos
 
118
    while i < len(nv):
 
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]
 
124
          return 1
 
125
        else: 
 
126
          self.inputMaskLen[section] = i - self.inputMaskPos[section]
 
127
          if section == len(self.inputHandlers) - 1: 
 
128
            if i != len(nv)-1:
 
129
              return 1
 
130
          else: 
 
131
            section += 1
 
132
            self.inputMaskPos[section] = i
 
133
            self.inputMaskLen[section] = 0
 
134
      else: 
 
135
        i += 1
 
136
 
 
137
    self.inputMaskLen[section] = i - self.inputMaskPos[section]
 
138
 
 
139
    for i in range(section+1, len(self.inputHandlers)): 
 
140
      self.inputMaskLen[i] = 0
 
141
      
 
142
     
 
143
    self.entry = nv
 
144
    return 1
 
145
 
 
146
 
 
147
  def getFormattedOutput(self, secondary=0): 
 
148
    rv = ""
 
149
    value = self._buildDate(self.entry, 1)
 
150
    if secondary: 
 
151
      handlers = self.output2Handlers
 
152
    else: 
 
153
      handlers = self.outputHandlers
 
154
 
 
155
    try: 
 
156
      for m in self.outputHandlers: 
 
157
        rv += m.getFormattedValue(value)
 
158
 
 
159
      return rv
 
160
    except InvalidDate, msg:
 
161
      raise InvalidEntry, msg
 
162
 
 
163
 
 
164
  def getFormattedDisplay(self): 
 
165
    rv = ""
 
166
    value = self._buildDate(self.entry, 1)
 
167
 
 
168
    try: 
 
169
      for m in self.outputHandlers: 
 
170
        rv += m.getFormattedValue(value)
 
171
 
 
172
      return rv
 
173
    except InvalidDate, msg:
 
174
      raise InvalidEntry, msg
 
175
 
 
176
 
 
177
  def getFormattedInput(self, padding=""): 
 
178
    rv = ""
 
179
    value = self._buildDate(self.entry, 0)
 
180
 
 
181
#    self.lastInputPos = 
 
182
 
 
183
    print self.inputMaskLen
 
184
 
 
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)
 
188
      elif len(padding): 
 
189
        rv += padding * self.inputHandlers[i].maxLength
 
190
 
 
191
    return rv
 
192
 
 
193
 
 
194
  def _buildDate(self, value, mustValidate=0): 
 
195
    date = GDateTime()
 
196
 
 
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 )
 
202
      elif mustValidate: 
 
203
        tmsg = _("Invalid Entry") 
 
204
        raise InvalidEntry, tmsg
 
205
 
 
206
    return date
 
207
    
 
208
 
 
209
 
 
210
class _baseDateSection (MaskSection): 
 
211
  def __init__(self): 
 
212
    pass
 
213
 
 
214
  def isValidEntry(self, value):
 
215
    return 0
 
216
 
 
217
  def addSelfToDate(self, value, date): 
 
218
    pass
 
219
 
 
220
 
 
221
class _aSection(_baseDateSection): 
 
222
  def __init__(self): 
 
223
    _baseDateSection.__init__(self)
 
224
    self.minLength = 1
 
225
    self.maxLength = 2
 
226
 
 
227
  def getFormattedValue(self, date): 
 
228
    return weekdayAbbrevNames[date.getDayOfWeek()]
 
229
 
 
230
  def isValidEntry(self, value):
 
231
    return 0
 
232
 
 
233
  def addSelfToDate(self, value, date): 
 
234
    pass
 
235
 
 
236
 
 
237
class _ASection(_aSection): 
 
238
  def getFormattedValue(self, date): 
 
239
    return weekdayNames[date.getDayOfWeek()]
 
240
 
 
241
 
 
242
class _bSection(_baseDateSection): 
 
243
  def __init__(self): 
 
244
    _baseDateSection.__init__(self)
 
245
    self.minLength = 1
 
246
    self.maxLength = 2
 
247
 
 
248
  def getFormattedValue(self, date): 
 
249
    return monthAbbrevNames[(date.month or 1) - 1]
 
250
 
 
251
  def isValidEntry(self, value):
 
252
    return 0
 
253
 
 
254
  def addSelfToDate(self, value, date): 
 
255
    pass
 
256
 
 
257
 
 
258
class _BSection(_bSection): 
 
259
  def getFormattedValue(self, date): 
 
260
    return monthNames[(date.month or 1) - 1]
 
261
 
 
262
 
 
263
class _cSection(_baseDateSection): 
 
264
  def __init__(self): 
 
265
    _baseDateSection.__init__(self)
 
266
    self.minLength = 1
 
267
    self.maxLength = 2
 
268
 
 
269
  def getFormattedValue(self, date): 
 
270
    # TODO: Implement this mask element
 
271
    return "*" * self.maxLength
 
272
 
 
273
  def isValidEntry(self, value):
 
274
    return 0
 
275
 
 
276
  def addSelfToDate(self, value, date): 
 
277
    pass
 
278
 
 
279
 
 
280
class _dSection(_baseDateSection): 
 
281
  def __init__(self): 
 
282
    _baseDateSection.__init__(self)
 
283
    self.minLength = 1
 
284
    self.maxLength = 2
 
285
 
 
286
  def getFormattedValue(self, date): 
 
287
    return "%02i" % date.day
 
288
 
 
289
  def isValidEntry(self, value):
 
290
    try: 
 
291
      v = int(value)
 
292
      return (v >= 0 and v <= 31) or (v == 0 and len(value) == 1)
 
293
    except ValueError: 
 
294
      return 0
 
295
 
 
296
  def addSelfToDate(self, value, date): 
 
297
    date.day = int(value or 1)
 
298
 
 
299
 
 
300
class _DSection(_dSection): 
 
301
  def getFormattedValue(self, date): 
 
302
    return "%i" % date.day
 
303
 
 
304
 
 
305
class _hSection(_baseDateSection): 
 
306
  def __init__(self): 
 
307
    _baseDateSection.__init__(self)
 
308
    self.minLength = 1
 
309
    self.maxLength = 2
 
310
 
 
311
  def getFormattedValue(self, date): 
 
312
    return "%02i" % date.hour
 
313
 
 
314
  def isValidEntry(self, value):
 
315
    return 0
 
316
 
 
317
  def addSelfToDate(self, value, date): 
 
318
    date.hour = int(value or 0)
 
319
 
 
320
 
 
321
class _HSection(_hSection): 
 
322
  def getFormattedValue(self, date): 
 
323
    return "%i" % date.hour
 
324
 
 
325
 
 
326
class _gSection(_baseDateSection): 
 
327
  def __init__(self): 
 
328
    _baseDateSection.__init__(self)
 
329
    self.minLength = 1
 
330
    self.maxLength = 2
 
331
 
 
332
  def getFormattedValue(self, date): 
 
333
    # TODO: Implement this mask element
 
334
    return "*" * self.maxLength
 
335
 
 
336
  def isValidEntry(self, value):
 
337
    return 0
 
338
 
 
339
  def addSelfToDate(self, value, date): 
 
340
    pass
 
341
 
 
342
 
 
343
class _GSection(_gSection): 
 
344
  def getFormattedValue(self, date): 
 
345
    # TODO: Implement this mask element
 
346
    return "*" * self.maxLength
 
347
 
 
348
 
 
349
class _jSection(_baseDateSection): 
 
350
  def __init__(self): 
 
351
    _baseDateSection.__init__(self)
 
352
    self.minLength = 1
 
353
    self.maxLength = 2
 
354
 
 
355
  def getFormattedValue(self, date): 
 
356
    # TODO: Implement this mask element
 
357
    return "*" * self.maxLength
 
358
 
 
359
  def isValidEntry(self, value):
 
360
    return 0
 
361
 
 
362
  def addSelfToDate(self, value, date): 
 
363
    pass
 
364
 
 
365
 
 
366
class _JSection(_jSection): 
 
367
  def getFormattedValue(self, date): 
 
368
    # TODO: Implement this mask element
 
369
    return "*" * self.maxLength
 
370
 
 
371
 
 
372
class _mSection(_baseDateSection): 
 
373
  def __init__(self): 
 
374
    _baseDateSection.__init__(self)
 
375
    self.minLength = 1
 
376
    self.maxLength = 2
 
377
 
 
378
  def getFormattedValue(self, date): 
 
379
    return "%02i" % date.month
 
380
 
 
381
  def isValidEntry(self, value):
 
382
    try: 
 
383
      v = int(value)
 
384
      return (v >= 0 and v <= 12) or (v == 0 and len(value) == 1)
 
385
    except ValueError: 
 
386
      return 0
 
387
 
 
388
  def addSelfToDate(self, value, date): 
 
389
    date.month = int(value or 1)
 
390
 
 
391
 
 
392
class _MSection(_baseDateSection): 
 
393
  def getFormattedValue(self, date): 
 
394
    return "%i" % date.month
 
395
 
 
396
 
 
397
class _iSection(_baseDateSection): 
 
398
  def __init__(self): 
 
399
    _baseDateSection.__init__(self)
 
400
    self.minLength = 1
 
401
    self.maxLength = 2
 
402
 
 
403
  def getFormattedValue(self, date): 
 
404
    return "%02i" % date.minute
 
405
 
 
406
  def isValidEntry(self, value):
 
407
    try: 
 
408
      v = int(value)
 
409
      return (v >= 0 and v <= 59) or (v == 0 and len(value) == 1)
 
410
    except ValueError: 
 
411
      return 0
 
412
 
 
413
  def addSelfToDate(self, value, date): 
 
414
    date.minute = int(value)
 
415
 
 
416
 
 
417
class _ISection(_iSection): 
 
418
  def getFormattedValue(self, date): 
 
419
    return "%2i" % date.minute
 
420
 
 
421
 
 
422
class _pSection(_baseDateSection): 
 
423
  def __init__(self): 
 
424
    _baseDateSection.__init__(self)
 
425
    self.minLength = 1
 
426
    self.maxLength = 2
 
427
 
 
428
  def getFormattedValue(self, date): 
 
429
    # TODO: Implement this mask element
 
430
    return "*" * self.maxLength
 
431
 
 
432
  def isValidEntry(self, value):
 
433
    return 0
 
434
 
 
435
  def addSelfToDate(self, value, date): 
 
436
    pass
 
437
 
 
438
 
 
439
class _PSection(_pSection): 
 
440
  def getFormattedValue(self, date): 
 
441
    # TODO: Implement this mask element
 
442
    return "*" * self.maxLength
 
443
 
 
444
 
 
445
class _sSection(_baseDateSection): 
 
446
  def __init__(self): 
 
447
    _baseDateSection.__init__(self)
 
448
    self.minLength = 1
 
449
    self.maxLength = 2
 
450
 
 
451
  def getFormattedValue(self, date): 
 
452
    return "%02i" % date.second
 
453
 
 
454
  def isValidEntry(self, value):
 
455
    try: 
 
456
      v = int(value)
 
457
      return (v >= 0 and v <= 59) or (v == 0 and len(value) == 1)
 
458
    except ValueError: 
 
459
      return 0
 
460
 
 
461
  def addSelfToDate(self, value, date): 
 
462
    date.second = int(value)
 
463
 
 
464
 
 
465
class _SSection(_sSection): 
 
466
  def getFormattedValue(self, date): 
 
467
    return "%2i" % date.second
 
468
 
 
469
 
 
470
class _uSection(_baseDateSection): 
 
471
  def __init__(self): 
 
472
    _baseDateSection.__init__(self)
 
473
    self.minLength = 1
 
474
    self.maxLength = 2
 
475
 
 
476
  def getFormattedValue(self, date): 
 
477
    # TODO: Implement this mask element
 
478
    return "*" * self.maxLength
 
479
 
 
480
  def isValidEntry(self, value):
 
481
    return 0
 
482
 
 
483
  def addSelfToDate(self, value, date): 
 
484
    pass
 
485
 
 
486
 
 
487
class _USection(_uSection): 
 
488
  def getFormattedValue(self, date): 
 
489
    # TODO: Implement this mask element
 
490
    return "*" * self.maxLength
 
491
 
 
492
 
 
493
class _vSection(_baseDateSection): 
 
494
  def __init__(self): 
 
495
    _baseDateSection.__init__(self)
 
496
    self.minLength = 1
 
497
    self.maxLength = 2
 
498
 
 
499
  def getFormattedValue(self, date): 
 
500
    # TODO: Implement this mask element
 
501
    return "*" * self.maxLength
 
502
 
 
503
  def isValidEntry(self, value):
 
504
    return 0
 
505
 
 
506
  def addSelfToDate(self, value, date): 
 
507
    pass
 
508
 
 
509
 
 
510
class _VSection(_vSection): 
 
511
  def getFormattedValue(self, date): 
 
512
    # TODO: Implement this mask element
 
513
    return "*" * self.maxLength
 
514
 
 
515
 
 
516
class _wSection(_baseDateSection): 
 
517
  def __init__(self): 
 
518
    _baseDateSection.__init__(self)
 
519
    self.minLength = 1
 
520
    self.maxLength = 2
 
521
 
 
522
  def getFormattedValue(self, date): 
 
523
    # TODO: Implement this mask element
 
524
    return "*" * self.maxLength
 
525
 
 
526
  def isValidEntry(self, value):
 
527
    return 0
 
528
 
 
529
  def addSelfToDate(self, value, date): 
 
530
    pass
 
531
 
 
532
 
 
533
class _xSection(_baseDateSection): 
 
534
  def __init__(self): 
 
535
    _baseDateSection.__init__(self)
 
536
    self.minLength = 1
 
537
    self.maxLength = 2
 
538
 
 
539
  def getFormattedValue(self, date): 
 
540
    # TODO: Implement this mask element
 
541
    return "*" * self.maxLength
 
542
 
 
543
  def isValidEntry(self, value):
 
544
    return 0
 
545
 
 
546
  def addSelfToDate(self, value, date): 
 
547
    pass
 
548
 
 
549
 
 
550
class _XSection(_xSection): 
 
551
  def getFormattedValue(self, date): 
 
552
    # TODO: Implement this mask element
 
553
    return "*" * self.maxLength
 
554
 
 
555
 
 
556
class _ySection(_baseDateSection): 
 
557
  def __init__(self): 
 
558
    _baseDateSection.__init__(self)
 
559
    self.minLength = 2
 
560
    self.maxLength = 2
 
561
 
 
562
  def getFormattedValue(self, date): 
 
563
    return "%02i" % divmod(date.year, 100)[1]
 
564
 
 
565
  def isValidEntry(self, value):
 
566
    try: 
 
567
      v = int(value)
 
568
      return (v >= 0) and (v <= 99)
 
569
    except ValueError: 
 
570
      return 0
 
571
 
 
572
  def addSelfToDate(self, value, date): 
 
573
    # TODO: Temporary year hack!
 
574
    v = int(value)
 
575
    if v >= 50: 
 
576
      date.year = 1900 + v
 
577
    else: 
 
578
      date.year = 2000 + v
 
579
 
 
580
 
 
581
class _YSection(_ySection): 
 
582
  def __init__(self): 
 
583
    _baseDateSection.__init__(self)
 
584
    self.minLength = 2
 
585
    self.maxLength = 4
 
586
 
 
587
  def getFormattedValue(self, date): 
 
588
    return "%04i" % date.year 
 
589
 
 
590
  def isValidEntry(self, value):
 
591
    try: 
 
592
      v = int(value)
 
593
      return (v >= 1) and (v <= 9999)
 
594
    except ValueError: 
 
595
      return 0
 
596
 
 
597
  def addSelfToDate(self, value, date): 
 
598
    date.year = int(value)
 
599
 
 
600
 
 
601