commit-gnue
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnue] r9349 - trunk/gnue-forms/src/input/displayHandlers


From: johannes
Subject: [gnue] r9349 - trunk/gnue-forms/src/input/displayHandlers
Date: Thu, 1 Feb 2007 03:46:32 -0600 (CST)

Author: johannes
Date: 2007-02-01 03:46:32 -0600 (Thu, 01 Feb 2007)
New Revision: 9349

Modified:
   trunk/gnue-forms/src/input/displayHandlers/datehandler.py
Log:
Added a displayhandler for time fields


Modified: trunk/gnue-forms/src/input/displayHandlers/datehandler.py
===================================================================
--- trunk/gnue-forms/src/input/displayHandlers/datehandler.py   2007-02-01 
08:18:22 UTC (rev 9348)
+++ trunk/gnue-forms/src/input/displayHandlers/datehandler.py   2007-02-01 
09:46:32 UTC (rev 9349)
@@ -37,11 +37,16 @@
 # Exceptions
 # =============================================================================
 
-class InvalidDateLiteral (errors.UserError):
-    def __init__ (self, value):
+class InvalidDateLiteral(errors.UserError):
+    def __init__(self, value):
         msg = u_("'%(value)s' is not a valid date-literal") % {'value': value}
         errors.UserError.__init__ (self, msg)
 
+class InvalidTimeLiteral(errors.UserError):
+    def __init__(self, value):
+        msg = u_("'%(value)s' is not a valid time-literal") % {'value': value}
+        errors.UserError.__init__ (self, msg)
+
 # =============================================================================
 # Base class for date related handlers
 # =============================================================================
@@ -113,13 +118,54 @@
         return (order, inter)
 
 
+    # -------------------------------------------------------------------------
+    # Get the ordering of the time components according to the given format
+    # -------------------------------------------------------------------------
 
+    def get_time_order(self, format):
+        """
+        """
 
+        order = ''
+        inter = []
+
+        text = datetime.time(13, 24, 56).strftime(str(format))
+        pat = re.compile('^(\d+)(\D*)')
+
+        match = pat.match(text)
+        while match:
+            part, rest = match.groups()
+            if part in ['13', '01']:
+                order += 'H'
+            elif part == '24':
+                order += 'M'
+            elif part == '56':
+                order += 'S'
+            else:
+                order += '?'
+
+            if rest:
+                inter.append(rest)
+
+            text = text[len(part + rest):]
+            if not text:
+                break
+
+            match = pat.match(text)
+
+        return (order, inter)
+
+
+
+
 # =============================================================================
 # Display handler for date fields
 # =============================================================================
 
 class Date(DateRelated):
+    """
+    Display handler for date values.
+    """
 
     # -------------------------------------------------------------------------
     # Constructor
@@ -314,15 +360,116 @@
         return new_text, new_cursor
 
 
-
 # =============================================================================
 # Display handler for time fields
 # =============================================================================
 
-class Time(BaseCursor):
-    pass
+class Time(DateRelated):
+    """
+    Display handler for time values.
+    """
 
+    # -------------------------------------------------------------------------
+    # Constructor
+    # -------------------------------------------------------------------------
 
+    def __init__(self, entry, eventHandler, subEventHandler, display_mask,
+                 input_mask):
+
+        DateRelated.__init__(self, entry, eventHandler, subEventHandler,
+                display_mask, input_mask)
+
+        self._input_mask = input_mask or gConfigForms('DateEditMask_Time')
+        self._display_mask = display_mask or gConfigForms('DateMask_Time')
+
+        self.__order, self.__inter = self.get_time_order(self._input_mask)
+
+
+    # -------------------------------------------------------------------------
+    # Parsing a (maybe partial) value
+    # -------------------------------------------------------------------------
+    def parse_display(self, display):
+        """
+        Try to figure out which datetime.time value is meant by the given
+        display string.
+        """
+
+        if not display:
+            return None
+
+        try:
+            # First have a look wether the input follows the requested format 
+            temp = time.strptime(display, self._input_mask)
+            return datetime.time(*temp[3:3+len(self.__order)])
+
+        except ValueError:
+            pass
+
+        kw = {'hour': 0, 'minute': 0, 'second': 0}
+        order = []
+        for char in self.__order:
+            if char == 'S':
+                order.append('second')
+            elif char == 'M':
+                order.append('minute')
+            else:
+                order.append('hour')
+
+        # If the input is a number of length 2 we treat it as the lowest part
+        if display.isdigit() and len(display) <= 2:
+            try:
+                kw[order[-1]] = int(display)
+                return datetime.time(**kw)
+
+            except ValueError:
+                raise InvalidTimeLiteral, display
+
+
+        # If the input is a 4-digit number or a string with two numbers
+        # separated by a non-digit string we treat it as the least significant
+        # two components according to the order of the original input mask
+        match = re.match('^(\d+)\D+(\d+)\s*$', display)
+        if (display.isdigit() and len(display) == 4) or match is not None:
+            if match:
+                (val1, val2) = match.groups()
+            else:
+                val1, val2 = display[:2], display[2:]
+
+            kw[order[-2]] = int(val1)
+            kw[order[-1]] = int(val2)
+
+            try:
+                return datetime.time(**kw)
+
+            except ValueError:
+                raise InvalidTimeLiteral, display
+
+        # If the input is a 6-digit number or a string with three numbers
+        # separated by a non-digit string we treat it as the least significant
+        # three components according to the order of the original input mask
+        match = re.match('^(\d+)\D+(\d+)\D+(\d+)\s*$', display)
+        if (display.isdigit() and len(display) == 6) or match is not None:
+            if match:
+                (val1, val2, val3) = match.groups()
+            else:
+                val1, val2, val3 = display[:2], display[2:4], display[4:]
+
+            kw[order[-3]] = int(val1)
+            kw[order[-2]] = int(val2)
+            kw[order[-1]] = int(val3)
+
+            try:
+                return datetime.time(**kw)
+
+            except ValueError:
+                raise InvalidTimeLiteral, display
+
+        # Does not seem to fit any pattern
+        raise InvalidTimeLiteral, display
+
+
+
+
 class DateTime(BaseCursor):
     """
     Class to handle the display and entry of date based fields.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]