[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
gnue/gnue-common/src FormatMasks/BaseMask.py Fo...
From: |
Jason Cater |
Subject: |
gnue/gnue-common/src FormatMasks/BaseMask.py Fo... |
Date: |
Mon, 16 Jul 2001 13:03:07 -0700 |
CVSROOT: /home/cvs
Module name: gnue
Changes by: Jason Cater <address@hidden> 01/07/16 13:03:07
Modified files:
gnue-common/src/FormatMasks: BaseMask.py DateMask.py
FormatExceptions.py
Added files:
gnue-common/src: GDateTime.py
Log message:
Rearranged GDateTime, completed DateMask
CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/gnue/gnue-common/src/GDateTime.py?cvsroot=OldCVS&rev=1.1
http://savannah.gnu.org/cgi-bin/viewcvs/gnue/gnue-common/src/FormatMasks/BaseMask.py.diff?cvsroot=OldCVS&tr1=1.1&tr2=1.2&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/gnue/gnue-common/src/FormatMasks/DateMask.py.diff?cvsroot=OldCVS&tr1=1.1&tr2=1.2&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/gnue/gnue-common/src/FormatMasks/FormatExceptions.py.diff?cvsroot=OldCVS&tr1=1.1&tr2=1.2&r1=text&r2=text
Patches:
Index: gnue/gnue-common/src/FormatMasks/BaseMask.py
diff -u gnue/gnue-common/src/FormatMasks/BaseMask.py:1.1
gnue/gnue-common/src/FormatMasks/BaseMask.py:1.2
--- gnue/gnue-common/src/FormatMasks/BaseMask.py:1.1 Sun Jul 15 21:17:24 2001
+++ gnue/gnue-common/src/FormatMasks/BaseMask.py Mon Jul 16 13:03:07 2001
@@ -38,12 +38,14 @@
def getFormattedValue(self, value):
return self.literal
+ def isValidEntry(self, value):
+ return value == self.literal
class BaseMask:
value = ""
maskMappings = {}
- inputMaskHandlers = []
- outputMaskHandlers = []
+ inputHandlers = []
+ outputHandlers = []
predefinedLiterals = ""
basetype = "base"
defaultmask = ""
@@ -54,6 +56,7 @@
if inputMask != None:
self.inputHandlers = self.parseMask(inputMask)
+
# Take a mask string and break it into its elements and map it to handlers
def parseMask(self, mask):
maskHandler = []
@@ -78,7 +81,7 @@
isLiteral = 1
elif isLiteral or ch in self.predefinedLiterals:
isLiteral = 0
- maskHandler.append(Literal(ch))
+ maskHandler.append(self.literalClass(ch))
elif self.maskMappings.has_key(ch):
maskHandler.append(self.maskMappings[ch]())
else:
@@ -110,5 +113,7 @@
def getFormattedValue(self):
return self.value
+ def isValidEntry(self, value):
+ return 0
Index: gnue/gnue-common/src/FormatMasks/DateMask.py
diff -u gnue/gnue-common/src/FormatMasks/DateMask.py:1.1
gnue/gnue-common/src/FormatMasks/DateMask.py:1.2
--- gnue/gnue-common/src/FormatMasks/DateMask.py:1.1 Sun Jul 15 21:17:24 2001
+++ gnue/gnue-common/src/FormatMasks/DateMask.py Mon Jul 16 13:03:07 2001
@@ -19,7 +19,7 @@
# Copyright 2001 Free Software Foundation
#
# FILE:
-# BaseMask.py
+# DateMask.py
#
# DESCRIPTION:
#
@@ -52,29 +52,45 @@
#
from BaseMask import BaseMask, MaskSection, Literal
+from gnue.common, GDebug
+from gnue.common.GDateTime import GDateTime
from FormatExceptions import *
+# TODO: This is obviously not Internationalized!
+monthNames = ['January','February','March','April','May','June',
+ 'July','August','September','October','November','December']
+monthAbbrevNames = ['Jan','Feb','Mar','Apr','May','Jun',
+ 'Jul','Aug','Sep','Oct','Nov','Dec']
+
+weekdayNames = ['Sunday','Monday','Tuesday','Wednesday',
+ 'Thursday','Friday','Saturday']
+
+weekdayAbbrevNames = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat']
+
+
+predefinedDateLiterals = "-./: ,"
+
+
def isLeapYear(year):
return divmod(year,400)[1] == 0 or \
(divmod(year,4)[1] == 0 and divmod(year,100)[1] != 0)
-class GDateTime:
- def __init__(self):
- self.month = 1
- self.day = 1
- self.year = 0
- self.hour = 0
- self.minute = 0
- self.second = 0
+class DateLiteral (Literal):
+ def addSelfToDate(self, value, date):
+ pass
+
+ def isValidEntry(self, value):
+ return value == self.literal or value in predefinedDateLiterals
class DateMask (BaseMask):
def __init__(self, outputMask, inputMask=None):
- self.predefinedLiterals = "-./: ,"
+ self.predefinedLiterals = predefinedDateLiterals
self.basetype = "date"
+ self.literalClass = DateLiteral
# TODO: Make DateMask's defaultmask be based on locale settings
self.defaultmask = "m/d/y"
@@ -101,6 +117,11 @@
self.value = GDateTime()
self.entry = ""
+ self.inputMaskPos = []
+ self.inputMaskLen = []
+ for i in range(len(self.inputHandlers)):
+ self.inputMaskPos.append(0)
+ self.inputMaskLen.append(0)
def processEdit (self, str, pos=0, replaces=0):
@@ -110,20 +131,78 @@
else:
nv = "%s%s%s" % (nv[:pos],str,nv[pos+replaces:])
+ section = 0
+ while section < len(self.inputMaskPos) and \
+ self.inputMaskPos[section] < pos:
+ section = section + 1
+
+ i = pos
+ while i < len(nv):
+ if not self.inputHandlers[section]\
+ .isValidEntry(nv[self.inputMaskPos[section]:i+1]):
+ if i == self.inputMaskPos[section] and \
+ not isinstance(self.inputHandlers[section],Literal):
+ self.inputMaskLen[section] = i - self.inputMaskPos[section]
+ return 1
+ else:
+ self.inputMaskLen[section] = i - self.inputMaskPos[section]
+ if section == len(self.inputHandlers) - 1:
+ if i != len(nv)-1:
+ return 1
+ else:
+ section = section + 1
+ self.inputMaskPos[section] = i
+ self.inputMaskLen[section] = 0
+ else:
+ i = i + 1
+
+ self.inputMaskLen[section] = i - self.inputMaskPos[section]
+
+ for i in range(section+1, len(self.inputHandlers)):
+ self.inputMaskLen[i] = 0
+
+
self.entry = nv
+ return 1
def getFormattedOutput(self):
rv = ""
+ value = self._buildDate(self.entry, 1)
+
for m in self.outputHandlers:
- rv = rv + m.getFormattedValue(self.value)
+ rv = rv + m.getFormattedValue(value)
+
+ return rv
+
+
+ def getFormattedInput(self, padding=""):
+ rv = ""
+ value = self._buildDate(self.entry, 0)
+
+ self.lastInputPos
+
+ print self.inputMaskLen
+ for i in range(len(self.inputHandlers)):
+ if self.inputMaskLen[i] or isinstance(self.inputHandlers[i],Literal):
+ rv = rv + self.inputHandlers[i].getFormattedValue(value)
+ elif len(padding):
+ rv = rv + padding * self.inputHandlers[i].maxLength
+
return rv
+
- def _buildDate(self, value):
+ def _buildDate(self, value, mustValidate=0):
date = GDateTime()
- for m in self.inputHandlers:
- m.addSelfToDate(self.date)
+
+ for i in range(len(self.inputHandlers)):
+ if self.inputMaskLen[i] or isinstance(self.inputHandlers[i],Literal):
+ self.inputHandlers[i].addSelfToDate(
+ value[ self.inputMaskPos[i]: \
+ self.inputMaskPos[i] + self.inputMaskLen[i]], date )
+ elif mustValidate:
+ raise InvalidEntry, "Invalid Entry"
return date
@@ -136,7 +215,7 @@
def isValidEntry(self, value):
return 0
- def addSelfToDate(self, date):
+ def addSelfToDate(self, value, date):
pass
@@ -152,7 +231,7 @@
def isValidEntry(self, value):
return 0
- def addSelfToDate(self, date):
+ def addSelfToDate(self, value, date):
pass
@@ -168,18 +247,18 @@
self.maxLength = 2
def getFormattedValue(self, date):
- return "*" * self.maxLength
+ return monthAbbrevNames[(date.month or 1) - 1]
def isValidEntry(self, value):
return 0
- def addSelfToDate(self, date):
+ def addSelfToDate(self, value, date):
pass
class _BSection(_bSection):
def getFormattedValue(self, date):
- return "*" * self.maxLength
+ return monthNames[(date.month or 1) - 1]
class _cSection(_baseDateSection):
@@ -194,7 +273,7 @@
def isValidEntry(self, value):
return 0
- def addSelfToDate(self, date):
+ def addSelfToDate(self, value, date):
pass
@@ -214,8 +293,8 @@
except ValueError:
return 0
- def addSelfToDate(self, date):
- date.date = int(self.value or 1)
+ def addSelfToDate(self, value, date):
+ date.day = int(value or 1)
class _DSection(_dSection):
@@ -235,8 +314,8 @@
def isValidEntry(self, value):
return 0
- def addSelfToDate(self, date):
- date.hour = int(self.value or 0)
+ def addSelfToDate(self, value, date):
+ date.hour = int(value or 0)
class _HSection(_hSection):
@@ -256,7 +335,7 @@
def isValidEntry(self, value):
return 0
- def addSelfToDate(self, date):
+ def addSelfToDate(self, value, date):
pass
@@ -277,7 +356,7 @@
def isValidEntry(self, value):
return 0
- def addSelfToDate(self, date):
+ def addSelfToDate(self, value, date):
pass
@@ -302,8 +381,8 @@
except ValueError:
return 0
- def addSelfToDate(self, date):
- date.month = int(self.value or 1)
+ def addSelfToDate(self, value, date):
+ date.month = int(value or 1)
class _MSection(_baseDateSection):
@@ -327,8 +406,8 @@
except ValueError:
return 0
- def addSelfToDate(self, date):
- date.minute = int(self.value)
+ def addSelfToDate(self, value, date):
+ date.minute = int(value)
class _ISection(_iSection):
@@ -348,7 +427,7 @@
def isValidEntry(self, value):
return 0
- def addSelfToDate(self, date):
+ def addSelfToDate(self, value, date):
pass
@@ -373,8 +452,8 @@
except ValueError:
return 0
- def addSelfToDate(self, date):
- date.second = int(self.value)
+ def addSelfToDate(self, value, date):
+ date.second = int(value)
class _SSection(_sSection):
@@ -394,7 +473,7 @@
def isValidEntry(self, value):
return 0
- def addSelfToDate(self, date):
+ def addSelfToDate(self, value, date):
pass
@@ -415,7 +494,7 @@
def isValidEntry(self, value):
return 0
- def addSelfToDate(self, date):
+ def addSelfToDate(self, value, date):
pass
@@ -436,7 +515,7 @@
def isValidEntry(self, value):
return 0
- def addSelfToDate(self, date):
+ def addSelfToDate(self, value, date):
pass
@@ -452,7 +531,7 @@
def isValidEntry(self, value):
return 0
- def addSelfToDate(self, date):
+ def addSelfToDate(self, value, date):
pass
@@ -477,9 +556,13 @@
except ValueError:
return 0
- def addSelfToDate(self, date):
+ def addSelfToDate(self, value, date):
# TODO: Temporary year hack!
- date.year = int('20' + self.value)
+ v = int(value)
+ if v >= 50:
+ date.year = 1900 + v
+ else:
+ date.year = 2000 + v
class _YSection(_ySection):
@@ -498,13 +581,16 @@
except ValueError:
return 0
- def addSelfToDate(self, date):
- date.year = int(self.value)
+ def addSelfToDate(self, value, date):
+ date.year = int(value)
if __name__ == '__main__':
- dates = DateMask('\\D\\a\\t\\e: m.d.Y \\T\\i\\m\\e: h:i:s','m.d.y')
- dates.processEdit('123197')
- dates.processEdit('27',2,2)
+ dates = DateMask('\\D\\a\\t\\e: B d, Y \\T\\i\\m\\e: h:i:s','m.d.y')
+ print "processEdit1 <= %s" % dates.processEdit('123197')
+ print dates.getFormattedInput("_")
+ print dates.getFormattedOutput()
+ print "processEdit2 <= %s" %dates.processEdit('27',2,4)
+ print dates.getFormattedInput("_")
print dates.getFormattedOutput()
Index: gnue/gnue-common/src/FormatMasks/FormatExceptions.py
diff -u gnue/gnue-common/src/FormatMasks/FormatExceptions.py:1.1
gnue/gnue-common/src/FormatMasks/FormatExceptions.py:1.2
--- gnue/gnue-common/src/FormatMasks/FormatExceptions.py:1.1 Sun Jul 15
21:17:24 2001
+++ gnue/gnue-common/src/FormatMasks/FormatExceptions.py Mon Jul 16
13:03:07 2001
@@ -26,10 +26,26 @@
# NOTES:
#
-class PredefinedMaskNotFound (StandardError):
+# Base Exception for markup problems with mask
+class MaskMarkupError (StandardError):
pass
-class InvalidCharInMask (StandardError):
+# Developer specified a predefined mask (using "&name" notation)
+# that does not exist
+class PredefinedMaskNotFound (MaskMarkupError):
+ pass
+
+# The supplied mask includes a token that is not recognized
+class InvalidCharInMask (MaskMarkupError):
+ pass
+
+
+# Base exception for user/data input problems
+class MaskInputError (StandardError):
+ pass
+
+# The supplied input from user is invalid
+class InvalidEntry(MaskInputError):
pass
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- gnue/gnue-common/src FormatMasks/BaseMask.py Fo...,
Jason Cater <=