Commits

Olemis Lang committed ebe428e

GViz QL [ refs dataviz-302 ] : Check for syntax errors in OPTION format_syntax ... ok

  • Participants
  • Parent commits 710961d

Comments (0)

Files changed (2)

trac-dev/gviz/tracgviz/gvizql.py

     self.no_values = self.no_format = False
     self.format_syntax = {}
 
-  RE_OPTION_FORMAT_SYNTAX = compile('(?:([^`][^:`]*?)|(?:`([^`]+)`)):([^,]+?)(?:,|$)')
+  RE_OPTION_FORMAT_SYNTAX = compile('(?:([^`][^:`]*?)|(?:`([^`]+)`))'
+                                    ':([^,]+?)(?:,|$)')
 
   def configure(self, options):
     r"""Store formatting options
     """
     self.no_values = 'no_values' in options
     self.no_format = 'no_format' in options
-    r = self.RE_OPTION_FORMAT_SYNTAX
-    self.format_syntax = {} if 'format_syntax' not in options \
-        else dict([(a or b).strip(), c.strip()] 
-                  for a,b,c in r.findall(options['format_syntax']))
+
+    if 'format_syntax' not in options:
+      self.format_syntax = {}
+    else:
+      matches = self.RE_OPTION_FORMAT_SYNTAX.finditer(options['format_syntax'])
+      self.format_syntax = {}
+      i = 0
+      for m in matches:
+        if m.start() == i:
+          i = m.end() + 1
+          name, quoted, value = m.groups()
+          self.format_syntax[(name or quoted).strip()] = value.strip()
+        else:
+          raise GVizIllegalPattern('Bad syntax in format_syntax option at %d' %
+                                   (i,))
+      else:
+        # Syntax error may be located at the end of string
+        if i < len(options['format_syntax']):
+          raise GVizIllegalPattern('Bad syntax in format_syntax option at %d' %
+                                   (i,))
 
   #-------------------------
   #  Custom format syntax
     if p.startswith('_') or p not in vars(self.PyICU.URBNFRuleSetTag):
       raise GVizIllegalPattern('Invalid predefined number rule %s' % (p,))
     p = getattr(self.PyICU.URBNFRuleSetTag, p)
-    return self.PyICU.RuleBasedNumberFormat(p, self.PyICU.Local.getUS())
+    return self.PyICU.RuleBasedNumberFormat(p, self.PyICU.Locale.getUS())
 
   # Contains supported GViz format syntax
   # TODO: Transform into instance attribute

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

       Contradictory options no_value and no_format
 
       """,
+  'Parsing OPTIONS errors' : r"""
+      >>> parse("select * options no_values, format_syntax:'salary:PredefinedNumberRule,:,' "
+      ...       "format salary 'SPELLOUT'", 'opts', 'cols', 'fmt')
+      *****
+      * Tokens
+      *****
+      Token.Keyword.Reserved select
+      Token.Name.Other *
+      Token.Keyword.Reserved options
+      Token.Name.Variable no_values
+      Token.Punctuation ,
+      Token.Name.Variable format_syntax
+      Token.Punctuation :
+      Token.Literal.String.Single 'salary:PredefinedNumberRule,:,'
+      Token.Keyword.Reserved format
+      Token.Name.Variable salary
+      Token.Literal.String.Single 'SPELLOUT'
+      *****
+      * Parsing
+      *****
+      [('no_values', None), (u'format_syntax', 'salary:PredefinedNumberRule,:,')]
+      None
+      [('salary', 'SPELLOUT')]
+      *****
+      * Result
+      *****
+      GVizIllegalPattern  :  Bad syntax in format_syntax option at 29
+
+
+      """,
     'Parsing (failures)' : r"""
       >>> parse("  select ", 'cols', fail=True)
       *****