1. Olemis Lang
  2. trac-gviz

Commits

Olemis Lang  committed 4fe542a

GViz QL : Test case for ICU number format rewritten ... [ok]

  • Participants
  • Parent commits 388efe9
  • Branches gviz_ql

Comments (0)

Files changed (2)

File trac-dev/gviz/tracgviz/gvizql.py

View file
 __metaclass__ = type
 
 from ast import literal_eval
-from datetime import datetime
+from datetime import date, datetime, time
 from itertools import islice, izip
 import logging
 import operator
       seq = Sequence([seq])
     self.fmt = seq
 
-  @staticmethod
-  def format_value(value, coltype, pattern):
+  DATETIME_CONVERTER = {
+      'date' : lambda x : datetime.combine(x, time()),
+      'timeofday' : lambda x : datetime.combine(date.min, x),
+      'datetime' : lambda x : x,
+    }
+
+  @classmethod
+  def format_value(cls, value, coltype, formatter):
     r"""Format value of given data type according to some formatting pattern.
     """
     if coltype == 'boolean':
+      # Formatter is a binary tuple
       idx = 0 if bool(value) else 1
-      return pattern[idx]
-    elif coltype in ('date', 'timeofday', 'datetime'):
-      # FIXME: ICU SimpleDateFormat
-      return str(value)
+      return formatter[idx]
+    elif coltype in cls.DATETIME_CONVERTER:
+      # Formatter is an instance of PyICU.SimpleDateFormat
+      return formatter.format(cls.DATETIME_CONVERTER[coltype](value))
     elif coltype == 'number':
-      # FIXME: ICU DecimalFormat
-      return str(value)
+      # Formatter is an instance of PyICU.DecimalFormat
+      return formatter.format(value)
     else:
       raise GVizRuntimeError("Format not supported for %s type" % (coltype,))
 
+  def _formatter(self, colnm, coltype, pattern):
+    if coltype is None:
+      raise LookupError("Unknown column '%s'" % (colnm,))
+    if coltype == 'boolean':
+      pattern = pattern.rsplit(':', 1)
+      if len(pattern) != 2:
+        raise GVizRuntimeError("Invalid formatting pattern '%s'" % (pattern,))
+      else:
+        return pattern
+    elif coltype in ('number', 'date', 'timeofday', 'datetime'):
+      if self.PyICU is None:
+        raise GVizRuntimeError("PyICU needed to format '%s' '%s'" % col[:2])
+      else:
+        return self.PyICU.SimpleDateFormat(pattern)
+    else:
+      if self.PyICU is None:
+        raise GVizRuntimeError("Format not supported for %s type" % (coltype,))
+      else:
+        return self.PyICU.DecimalFormat(pattern)
+
   def transform(self, schema, data):
     r"""Annotate result set with formatted values. In practice this means
     to transform data set into (value, formatted_value) tuples.
     """
-    patterns = dict(self.fmt)
-
-    # Special step for boolean patterns
-    for col in schema:
-      if col[0] in patterns:
-        if col[1] == 'boolean':
-          p = patterns[col[0]].rsplit(':', 1)
-          if len(p) != 2:
-            raise GVizRuntimeError("Invalid formatting pattern '%s'" % (p,))
-          else:
-            patterns[col[0]] = p
-        elif col[1] in ('number', 'date', 'timeofday', 'datetime'):
-          if self.PyICU is None:
-            raise GVizRuntimeError("PyICU needed to format '%s' '%s'" % col[:2])
-        else:
-          raise GVizRuntimeError("Format not supported for %s type" % (col[1],))
+    cols = dict([col[0], col] for col in schema)
+    patterns = dict([colnm, self._formatter(colnm, cols.get(colnm), p)] \
+        for colnm, p in self.fmt)
 
     #raise ValueError('\n'.join([str(patterns), str(schema), str(list(data))]))
     new_data = ([val if val is None or col[0] not in patterns else \

File trac-dev/gviz/tracgviz/testing/test_gvizql.py

View file
       *****
       * Result
       *****
-      GVizUnsupportedQueryOp  :  Unable to evaluate FORMAT clause. Either the whole clause or an specific feature is not supported yet.
-
+      = Columns =
+      name string
+      dept string
+      lunchTime timeofday
+      salary number
+      hireDate date
+      age number
+      isSenior boolean
+      seniorityStartTime datetime
+      = Row =
+         name = John
+         dept = Eng
+         lunchTime = 12:00:00
+         salary = (1000, u'1,000.00')
+         hireDate = 2005-03-19
+         age = 35
+         isSenior = True
+         seniorityStartTime = 2007-12-02 15:56:00
+      = Row =
+         name = Dave
+         dept = Eng
+         lunchTime = 12:00:00
+         salary = (500, u'500.00')
+         hireDate = 2006-04-19
+         age = 27
+         isSenior = False
+         seniorityStartTime = None
+      = Row =
+         name = Sally
+         dept = Eng
+         lunchTime = 13:00:00
+         salary = (600, u'600.00')
+         hireDate = 2005-10-10
+         age = 30
+         isSenior = False
+         seniorityStartTime = None
+      = Row =
+         name = Ben
+         dept = Sales
+         lunchTime = 12:00:00
+         salary = (400, u'400.00')
+         hireDate = 2002-10-10
+         age = 32
+         isSenior = True
+         seniorityStartTime = 2005-03-09 12:30:00
+      = Row =
+         name = Dana
+         dept = Sales
+         lunchTime = 12:00:00
+         salary = (350, u'350.00')
+         hireDate = 2004-09-08
+         age = 25
+         isSenior = False
+         seniorityStartTime = None
+      = Row =
+         name = Mike
+         dept = Marketing
+         lunchTime = 13:00:00
+         salary = (800, u'800.00')
+         hireDate = 2005-01-10
+         age = 24
+         isSenior = True
+         seniorityStartTime = 2007-12-30 14:40:00
 
       >>> parse("  format  salary   '#,##0.00' ," \
       ...       "hireDate 'dd-MMM-yyyy',      " \