Commits

Olemis Lang committed d87e2e7

GViz QL : ICU datetime patterns implemented. All tests for FORMAT clause ... [ok]

Comments (0)

Files changed (2)

trac-dev/gviz/tracgviz/gvizql.py

     else:
       raise GVizRuntimeError("Format not supported for %s type" % (coltype,))
 
-  def _formatter(self, colnm, coltype, pattern):
-    if coltype is None:
+  def _formatter(self, col, pattern):
+    if col is None:
       raise LookupError("Unknown column '%s'" % (colnm,))
+    colnm, coltype = col[:2]
     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'):
+    elif coltype in ('date', 'timeofday', 'datetime'):
       if self.PyICU is None:
-        raise GVizRuntimeError("PyICU needed to format '%s' '%s'" % col[:2])
+        raise GVizRuntimeError("PyICU needed to format '%s' '%s'" % \
+            (colnm, coltype))
       else:
         return self.PyICU.SimpleDateFormat(pattern)
-    else:
+    elif coltype == 'number':
       if self.PyICU is None:
-        raise GVizRuntimeError("Format not supported for %s type" % (coltype,))
+        raise GVizRuntimeError("PyICU needed to format '%s' '%s'" % \
+            (colnm, coltype))
       else:
         return self.PyICU.DecimalFormat(pattern)
+    else:
+      raise GVizRuntimeError("Format not supported for %s type" % (coltype,))
 
   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.
     """
     cols = dict([col[0], col] for col in schema)
-    patterns = dict([colnm, self._formatter(colnm, cols.get(colnm), p)] \
+    patterns = dict([colnm, self._formatter(cols.get(colnm), p)] \
         for colnm, p in self.fmt)
 
     #raise ValueError('\n'.join([str(patterns), str(schema), str(list(data))]))

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

       *****
       * 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 = (datetime.date(2005, 3, 19), u'19-Mar-2005')
+         age = 35
+         isSenior = (True, 'not yet')
+         seniorityStartTime = 2007-12-02 15:56:00
+      = Row =
+         name = Dave
+         dept = Eng
+         lunchTime = 12:00:00
+         salary = (500, u'500.00')
+         hireDate = (datetime.date(2006, 4, 19), u'19-Apr-2006')
+         age = 27
+         isSenior = (False, 'of course!')
+         seniorityStartTime = None
+      = Row =
+         name = Sally
+         dept = Eng
+         lunchTime = 13:00:00
+         salary = (600, u'600.00')
+         hireDate = (datetime.date(2005, 10, 10), u'10-Oct-2005')
+         age = 30
+         isSenior = (False, 'of course!')
+         seniorityStartTime = None
+      = Row =
+         name = Ben
+         dept = Sales
+         lunchTime = 12:00:00
+         salary = (400, u'400.00')
+         hireDate = (datetime.date(2002, 10, 10), u'10-Oct-2002')
+         age = 32
+         isSenior = (True, 'not yet')
+         seniorityStartTime = 2005-03-09 12:30:00
+      = Row =
+         name = Dana
+         dept = Sales
+         lunchTime = 12:00:00
+         salary = (350, u'350.00')
+         hireDate = (datetime.date(2004, 9, 8), u'08-Sep-2004')
+         age = 25
+         isSenior = (False, 'of course!')
+         seniorityStartTime = None
+      = Row =
+         name = Mike
+         dept = Marketing
+         lunchTime = 13:00:00
+         salary = (800, u'800.00')
+         hireDate = (datetime.date(2005, 1, 10), u'10-Jan-2005')
+         age = 24
+         isSenior = (True, 'not yet')
+         seniorityStartTime = 2007-12-30 14:40:00
 
       >>> parse("format salary '#,##0.00', hireDate 'dd-MMM-yyyy', "
       ...       "isSenior 'Yes!:Not yet'", 'fmt')
       *****
       * 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 = (datetime.date(2005, 3, 19), u'19-Mar-2005')
+         age = 35
+         isSenior = (True, 'Yes!')
+         seniorityStartTime = 2007-12-02 15:56:00
+      = Row =
+         name = Dave
+         dept = Eng
+         lunchTime = 12:00:00
+         salary = (500, u'500.00')
+         hireDate = (datetime.date(2006, 4, 19), u'19-Apr-2006')
+         age = 27
+         isSenior = (False, 'Not yet')
+         seniorityStartTime = None
+      = Row =
+         name = Sally
+         dept = Eng
+         lunchTime = 13:00:00
+         salary = (600, u'600.00')
+         hireDate = (datetime.date(2005, 10, 10), u'10-Oct-2005')
+         age = 30
+         isSenior = (False, 'Not yet')
+         seniorityStartTime = None
+      = Row =
+         name = Ben
+         dept = Sales
+         lunchTime = 12:00:00
+         salary = (400, u'400.00')
+         hireDate = (datetime.date(2002, 10, 10), u'10-Oct-2002')
+         age = 32
+         isSenior = (True, 'Yes!')
+         seniorityStartTime = 2005-03-09 12:30:00
+      = Row =
+         name = Dana
+         dept = Sales
+         lunchTime = 12:00:00
+         salary = (350, u'350.00')
+         hireDate = (datetime.date(2004, 9, 8), u'08-Sep-2004')
+         age = 25
+         isSenior = (False, 'Not yet')
+         seniorityStartTime = None
+      = Row =
+         name = Mike
+         dept = Marketing
+         lunchTime = 13:00:00
+         salary = (800, u'800.00')
+         hireDate = (datetime.date(2005, 1, 10), u'10-Jan-2005')
+         age = 24
+         isSenior = (True, 'Yes!')
+         seniorityStartTime = 2007-12-30 14:40:00
 
       >>> parse("select salary, hireDate, isSenior   , lunchTime "
       ...       "format salary '#,##0.00', hireDate 'dd-MMM-yyyy', "
       *****
       * Result
       *****
-      GVizUnsupportedQueryOp  :  Unable to evaluate FORMAT clause. Either the whole clause or an specific feature is not supported yet.
+      = Columns =
+      salary number
+      hireDate date
+      isSenior boolean
+      lunchTime timeofday
+      = Row =
+         salary = (1000, u'1,000.00')
+         hireDate = (datetime.date(2005, 3, 19), u'19-Mar-2005')
+         isSenior = (True, 'Yes!')
+         lunchTime = 12:00:00
+      = Row =
+         salary = (500, u'500.00')
+         hireDate = (datetime.date(2006, 4, 19), u'19-Apr-2006')
+         isSenior = (False, 'Not yet')
+         lunchTime = 12:00:00
+      = Row =
+         salary = (600, u'600.00')
+         hireDate = (datetime.date(2005, 10, 10), u'10-Oct-2005')
+         isSenior = (False, 'Not yet')
+         lunchTime = 13:00:00
+      = Row =
+         salary = (400, u'400.00')
+         hireDate = (datetime.date(2002, 10, 10), u'10-Oct-2002')
+         isSenior = (True, 'Yes!')
+         lunchTime = 12:00:00
+      = Row =
+         salary = (350, u'350.00')
+         hireDate = (datetime.date(2004, 9, 8), u'08-Sep-2004')
+         isSenior = (False, 'Not yet')
+         lunchTime = 12:00:00
+      = Row =
+         salary = (800, u'800.00')
+         hireDate = (datetime.date(2005, 1, 10), u'10-Jan-2005')
+         isSenior = (True, 'Yes!')
+         lunchTime = 13:00:00
 
       """,
   'Parsing FORMAT (complex)' : r"""
       = Columns =
       lower(name) + '@example.com' number E-mail
       upper(dept) string DEPT
+      isSenior boolean
+      seniorityStartTime datetime
+      salary * 0.05 number
       = Row =
          lower(name) + '@example.com' = john@example.com
          upper(dept) = ENG
+         isSenior = (True, 'SKILLED')
+         seniorityStartTime = (datetime.datetime(2007, 12, 2, 15, 56), u"Sun, Dec 2, '07")
+         salary * 0.05 = (50.0, u'50.00')
       = Row =
          lower(name) + '@example.com' = dave@example.com
          upper(dept) = ENG
+         isSenior = (False, 'beginner')
+         seniorityStartTime = None
+         salary * 0.05 = (25.0, u'25.00')
       = Row =
          lower(name) + '@example.com' = sally@example.com
          upper(dept) = ENG
+         isSenior = (False, 'beginner')
+         seniorityStartTime = None
+         salary * 0.05 = (30.0, u'30.00')
       = Row =
          lower(name) + '@example.com' = ben@example.com
          upper(dept) = SALES
+         isSenior = (True, 'SKILLED')
+         seniorityStartTime = (datetime.datetime(2005, 3, 9, 12, 30), u"Wed, Mar 9, '05")
+         salary * 0.05 = (20.0, u'20.00')
       = Row =
          lower(name) + '@example.com' = dana@example.com
          upper(dept) = SALES
+         isSenior = (False, 'beginner')
+         seniorityStartTime = None
+         salary * 0.05 = (17.5, u'17.50')
       = Row =
          lower(name) + '@example.com' = mike@example.com
          upper(dept) = MARKETING
+         isSenior = (True, 'SKILLED')
+         seniorityStartTime = (datetime.datetime(2007, 12, 30, 14, 40), u"Sun, Dec 30, '07")
+         salary * 0.05 = (40.0, u'40.00')
+
 
       """,
   'Parsing OPTIONS' : r"""