commit-gnue
[Top][All Lists]
Advanced

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

[gnue] r6939 - in trunk/gnue-common/src: datasources definitions


From: johannes
Subject: [gnue] r6939 - in trunk/gnue-common/src: datasources definitions
Date: Wed, 2 Feb 2005 13:09:24 -0600 (CST)

Author: johannes
Date: 2005-02-02 13:09:23 -0600 (Wed, 02 Feb 2005)
New Revision: 6939

Modified:
   trunk/gnue-common/src/datasources/GDataSource.py
   trunk/gnue-common/src/definitions/GParser.py
Log:
Improved exception handling for SAXParserErrors and MarkupErrors, which provide
an URL and a line number now


Modified: trunk/gnue-common/src/datasources/GDataSource.py
===================================================================
--- trunk/gnue-common/src/datasources/GDataSource.py    2005-02-02 14:48:02 UTC 
(rev 6938)
+++ trunk/gnue-common/src/datasources/GDataSource.py    2005-02-02 19:09:23 UTC 
(rev 6939)
@@ -441,8 +441,9 @@
     # If there is both, an order_by attribute *and* a sorting-tag, we've stop
     child = self.findChildOfType ('GCSortOrder')
     if child is not None and hasattr (self, 'order_by'):
-      raise MarkupError, u_("The use of order_by is depreciated. Please "
-                            "use <sortorder> instead")
+      raise MarkupError, \
+          (u_("The use of order_by is depreciated. Please use <sortorder> "
+              "instead"), self._url, self._lineNumber)
 
     # If there's a sorting tag, we'll use this first
     if child is not None:
@@ -495,7 +496,9 @@
           result.append (item)
 
     else:
-      raise MarkupError, u_("Unknown type/format of 'order-by' attribute")
+      raise MarkupError, \
+          (u_("Unknown type/format of 'order-by' attribute"), self._url,
+            self._lineNumber)
 
     return result
 

Modified: trunk/gnue-common/src/definitions/GParser.py
===================================================================
--- trunk/gnue-common/src/definitions/GParser.py        2005-02-02 14:48:02 UTC 
(rev 6938)
+++ trunk/gnue-common/src/definitions/GParser.py        2005-02-02 19:09:23 UTC 
(rev 6939)
@@ -50,6 +50,8 @@
 
 
 import string
+import os
+
 from gnue.common.apps import errors
 from gnue.common.formatting import GTypecast
 from gnue.common.definitions.GParserHelpers import GContent
@@ -60,9 +62,21 @@
 # Error classed raised for markup errors
 #
 class MarkupError(errors.ApplicationError):
-  pass
+  def __init__ (self, message, url = None, line = None):
+    errors.ApplicationError.__init__ (self, message)
+    text = [message]
+    if url is not None:
+      if line is not None:
+        msg = u_("XML markup error in '%(url)s' at line %(line)s:") \
+              % {'url': url, 'line': line}
+      else:
+        msg = u_("XML markup error in '%(url)s':") % {'url': url}
 
+      text.insert (0, msg)
 
+    self.detail = string.join (text, os.linesep)
+
+
 #######################################################
 #
 # loadXMLObject
@@ -84,7 +98,7 @@
 #######################################################
 
 def loadXMLObject(stream, handler, rootType, xmlFileType,
-  initialize=True, attributes={}, initParameters={}):
+    initialize=True, attributes={}, initParameters={}, url = None):
 
   # Create a parser
   parser = xml.sax.make_parser()
@@ -100,28 +114,42 @@
   object = None
 
   # Create the handler
-  dh = handler()
+  dh = handler ()
+
+  # pass the url of the stream and a pointer to the parser instance down to the
+  # handler, so it's able to do better error reporting
+  if url is None:
+    url = hasattr (stream, 'url') and stream.url or '[unknown]'
+
+  dh.url = url
+  dh.parser = parser
+
   dh.initValidation()
 
   # Tell the parser to use our handler
   parser.setContentHandler(dh)
+  
+  try:
+    parser.parse (stream)
 
-  parser.parse(stream)
+  except xml.sax.SAXParseException, e:
+    raise MarkupError, (errors.getException () [2], url, e.getLineNumber ())
 
   object = dh.getRoot()
 
   if not object:
     tmsg = u_("Error loading %s: empty definition file") % (xmlFileType)
-    raise MarkupError, tmsg
+    raise MarkupError, (tmsg, url)
+
   elif object._type != rootType:
     tmsg = u_("Error loading %(filetype)s: not a valid %(filetype)s definition 
"
               "(expected: %(expected)s, got: %(got)s)") \
            % {'filetype': xmlFileType,
               'expected': rootType,
               'got'     : object._type}
-    raise MarkupError, tmsg
+    raise MarkupError, (tmsg, url)
 
-  dh.finalValidation()
+  dh.finalValidation ()
 
   # Set the root object's attributes
   #
@@ -208,6 +236,7 @@
     self._requiredTags = []
     self._singleInstanceTags = []
     self._tagCounts = {}
+    self.url = None
 
   #
   # Called by client code to get the "root" node
@@ -246,7 +275,7 @@
     for element in self._requiredTags:
       if self._tagCounts[element] < 1:
         tmsg = u_("File is missing required tag <%s>") % (element)
-        raise MarkupError, tmsg
+        raise MarkupError, (tmsg, self.url)
 
 
   #
@@ -270,8 +299,9 @@
 
       except KeyError:
         raise MarkupError, \
-            u_("Error processing <%(tagname)s> tag [I do not know what a "
-               "<%(tagname)s> tag does]") % {'tagname': name}
+            (u_("Error processing <%(tagname)s> tag [I do not know what a "
+                "<%(tagname)s> tag does]") % {'tagname': name}, self.url,
+                self.parser.getLineNumber ())
 
       xmlns = {}
 
@@ -281,7 +311,7 @@
         if attrns:
           if not self.xmlNamespaceAttributesAsPrefixes:
             tmsg = _("Unexpected namespace on attribute")
-            raise MarkupError, tmsg
+            raise MarkupError, (tmsg, self.url, self.parser.getLineNumber ())
           prefix = attrns.split(':')[-1]
           attrs[prefix + '__' + attr] = saxattrs[qattr]
           xmlns[prefix] = attrns
@@ -294,10 +324,10 @@
             loadedxmlattrs[attr] = attrs[attr]
           except KeyError:
             raise MarkupError, \
-                u_('Error processing <%(tagname)s> tag [I do not '
+                (u_('Error processing <%(tagname)s> tag [I do not '
                    'recognize the "%(attribute)s" attribute]') \
                 % {'tagname': name,
-                   'attribute': attr}
+                   'attribute': attr}, self.url, self.parser.getLineNumber ())
 
           # this error shouldn't occure anymore
           #except UnicodeError:
@@ -320,7 +350,7 @@
                      % {'tag'      : name,
                         'attribute': attr,
                         'duplicate': saxattrs [qattr]}
-              raise MarkupError, tmsg
+              raise MarkupError, (tmsg, self.url, self.parser.getLineNumber ())
 
             # FIXME: If we really want to have a working 'Unique'-Attribute, we
             # would add the following line. But this would break forms atm.
@@ -341,7 +371,7 @@
                         '"%(attribute)s" not present]') \
                     % {'tagname'  : name,
                         'attribute': attr}
-              raise MarkupError, tmsg
+              raise MarkupError, (tmsg, self.url, self.parser.getLineNumber ())
         except (AttributeError, KeyError), msg: 
           raise errors.SystemError, _( 
             'Error in GParser xmlElement definition for %s/%s'
@@ -381,7 +411,7 @@
                   "element needs to be in default namespace") \
               % {'namespace': ns,
                   'name'     : name}
-        raise MarkupError, tmsg
+        raise MarkupError, (tmsg, self.url, self.parser.getLineNumber ())
 
       object._xmltag = name
       object._xmlnamespace = ns
@@ -396,12 +426,17 @@
       # namespace qualifier and we are not masquerading
       #
       tmsg =  u_("WARNING: Markup includes unsupported namespace '%s'." ) % 
(ns)
-      raise MarkupError, tmsg
+      raise MarkupError, (tmsg, self.url, self.parser.getLineNumber ())
 
 
     # Save the attributes loaded from XML file
     # (i.e., attributes that were not defaulted)
     object._loadedxmlattrs = loadedxmlattrs
+    
+    # We make the url of the xml-stream and the line number of the element
+    # available to the instance (for later error handling)
+    object._lineNumber     = self.parser.getLineNumber ()
+    object._url            = self.url
 
     # Set the attributes
     object.__dict__.update(attrs)
@@ -455,6 +490,7 @@
     gDebug (50, "</%s>" % name)
 
 
+
 class GImportItem(GObj):
   def __init__(self, parent=None, type="GCImport-Item"):
     GObj.__init__(self, parent, type=type)
@@ -507,10 +543,11 @@
            gDebug (5, ">>> Moving %s" % key)
        rv._buildObject()
      else:
-         raise MarkupError, u_("Unable to find an importable object named "
-                               "%(name)s in %(library)s") \
-                            % {'name'   : self.name,
-                               'library': self.library}
+         raise MarkupError, \
+             (u_("Unable to find an importable object named %(name)s in "
+                 "%(library)s") \
+              % {'name'   : self.name, 'library': self.library},
+              self.url)
 
   #
   # __findImportItem





reply via email to

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