[gedit-code-assistance] Move comment parsing for a schema to a more efficient function.



commit 41635a40a5f5fc7055efbc69f2f9d2999c60432a
Author: Jono <jono foodnotblogs com>
Date:   Fri Jun 29 13:57:28 2012 -0400

    Move comment parsing for a schema to a more efficient function.
    
    Removed generic excaption catch since it might not be meaningfull
    to the user without pointing them to a line number.

 backends/xml/gcpbackendxml/document.py |   80 +++++++++++++++++++-------------
 1 files changed, 48 insertions(+), 32 deletions(-)
---
diff --git a/backends/xml/gcpbackendxml/document.py b/backends/xml/gcpbackendxml/document.py
index 5a2970e..04853b2 100644
--- a/backends/xml/gcpbackendxml/document.py
+++ b/backends/xml/gcpbackendxml/document.py
@@ -114,6 +114,29 @@ class ParseThread(threading.Thread):
         else:   # it is probably a string or an Exception
             self.parse_errors.append((line, column, prefix + ": " + str(error)))
 
+
+    def lookForSchema(self, xml):
+        
+        """ This function looks through the comment tags for a schema reference
+            it returns on the first reference it finds in no particular order """
+            
+        for pre in (True, False):
+
+            for comment in xml.itersiblings(tag=etree.Comment, preceding=pre):
+                
+                refLine = comment.text.split(':', 1)
+
+                if refLine[0].strip().lower() == 'schema' and len(refLine) == 2:
+
+                    schemaLocation = refLine[1].strip()
+                    schemaRef = self.getSchema(schemaLocation)
+
+                    if schemaRef != None and schemaRef['type'] != None:
+                        return (schemaRef, schemaLocation, comment.sourceline)
+
+        return (None, None, None)
+
+        
     def run(self):
 
         docType = 'XML'
@@ -153,44 +176,36 @@ class ParseThread(threading.Thread):
 
             # parse XML comments in document for a reference to a schema
 
-            for pre in (True, False):
-
-                for comment in xml.itersiblings(tag=etree.Comment, preceding=pre):
-                    
-                    refLine = comment.text.split(':', 1)
-
-                    if refLine[0].strip().lower() == 'schema' and len(refLine) == 2:
-
-                        try:
+            try:
 
-                            schemaLocation = refLine[1].strip()
-                            schemaRef = self.getSchema(schemaLocation)
+                (schemaRef, schemaLocation, commentLine) = self.lookForSchema(xml)
+                
+                if schemaRef != None:
 
-                            if schemaRef != None and schemaRef['type'] != None:
+                    try:
+                        if schemaRef['type'] == "XSD":
+                            schema = etree.XMLSchema(schemaRef['xml'])
+                        elif schemaRef['type'] == "RelaxNG":
+                            schema = etree.RelaxNG(schemaRef['xml'])
 
-                                try:
-                                    if schemaRef['type'] == "XSD":
-                                        schema = etree.XMLSchema(schemaRef['xml'])
-                                    elif schemaRef['type'] == "RelaxNG":
-                                        schema = etree.RelaxNG(schemaRef['xml'])
+                        schema.assertValid(xml)
 
-                                    schema.assertValid(xml)
+                    except (etree.DocumentInvalid, etree.RelaxNGValidateError, etree.XMLSchemaValidateError):
+                        for error in schema.error_log:
+                            self.addError(schemaRef['type'] + " validation error", error)
 
-                                except (etree.DocumentInvalid, etree.RelaxNGValidateError, etree.XMLSchemaValidateError):
-                                    for error in schema.error_log:
-                                        self.addError(schemaRef['type'] + " validation error", error)
+                    except (etree.RelaxNGError, etree.XMLSchemaParseError):
+                        self.addError(schemaRef['type'] + " error", "Schema is invalid " + schemaLocation, commentLine)
 
-                                except (etree.RelaxNGError, etree.XMLSchemaParseError):
-                                    self.addError(schemaRef['type'] + " error", "Schema is invalid " + schemaLocation, comment.sourceline)
+                    except Exception as e:
+                        self.addError(schemaRef['type'] + " error", e)
 
-                                except Exception as e:
-                                    self.addError(schemaRef['type'] + " error", e)
+            except etree.XMLSyntaxError as e:
+                self.addError("Schema error", "Unable to parse schema XML " + schemaLocation, commentLine)
 
-                        except etree.XMLSyntaxError as e:
-                            self.addError("Schema error", "Unable to parse schema XML " + schemaLocation, comment.sourceline)
+            except Exception as e:
+                self.addError("Schema error", e, commentLine)
 
-                        except Exception as e:
-                            self.addError("Schema error", e, comment.sourceline)
 
         # handle XML parse errors
 
@@ -198,9 +213,10 @@ class ParseThread(threading.Thread):
             for error in e.error_log:
                 self.addError("XML parsing error", error)
 
-        except Exception as e:
-            self.addError("XML parsing error", e)
-            traceback.print_exc(file=sys.stdout)
+        # ignore other exceptions
+        
+        except:
+            pass
 
         self.clock.acquire()
 



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