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

« back to all changes in this revision

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