[libvirt] [PATCH 3/4] python: optimize SAX xml parsing event handler

Guannan Ren gren at redhat.com
Thu Feb 28 10:03:45 UTC 2013


close(), getmethodname(), cdata() are not standard methods from
parent class ContentHandler and not being used anywhere in codes,
so remove them.

In docParser, actually, we are overloading three parent methods
startElement(), endElement() and characters(), so I rename back
three of these method names for example from start()
to startElement() which makes it simpler to read.

make other improvements in codes.
---
 python/generator.py | 53 ++++++++++++++++-------------------------------------
 1 file changed, 16 insertions(+), 37 deletions(-)

diff --git a/python/generator.py b/python/generator.py
index 39e654b..7ccd471 100755
--- a/python/generator.py
+++ b/python/generator.py
@@ -30,41 +30,24 @@ def getparser(debug):
     target = docParser(debug)
     parser = make_parser()
     parser.setContentHandler(target)
-    return parser, target
+    return parser
 
 class docParser(ContentHandler):
     def __init__(self, debug = False):
         self.debug = debug;
-        self._methodname = None
-        self._data = []
+        self.data = []
         self.in_function = 0
 
-        self.startElement = self.start
-        self.endElement = self.end
-        self.characters = self.data
-
-    def close(self):
-        if self.debug:
-            print "close"
-
-    def getmethodname(self):
-        return self._methodname
-
-    def data(self, text):
-        if self.debug:
-            print "data %s" % text
-        self._data.append(text)
-
-    def cdata(self, text):
+    def characters(self, text):
         if self.debug:
             print "data %s" % text
-        self._data.append(text)
+        self.data.append(text)
 
-    def start(self, tag, attrs):
+    def startElement(self, tag, attrs):
         if self.debug:
             print "start %s, %s" % (tag, attrs)
         if tag == 'function':
-            self._data = []
+            self.data = []
             self.in_function = 1
             self.function = None
             self.function_cond = None
@@ -80,9 +63,9 @@ class docParser(ContentHandler):
             if attrs.has_key('module'):
                 self.function_module= attrs['module']
         elif tag == 'cond':
-            self._data = []
+            self.data = []
         elif tag == 'info':
-            self._data = []
+            self.data = []
         elif tag == 'arg':
             if self.in_function == 1:
                 self.function_arg_name = None
@@ -117,7 +100,7 @@ class docParser(ContentHandler):
             elif attrs['file'] == "libvirt-qemu":
                 qemu_enum(attrs['type'],attrs['name'],attrs['value'])
 
-    def end(self, tag):
+    def endElement(self, tag):
         if self.debug:
             print "end %s" % tag
         if tag == 'function':
@@ -167,17 +150,13 @@ class docParser(ContentHandler):
                                         self.function_return_info,
                                         self.function_return_field]
         elif tag == 'info':
-            str = ''
-            for c in self._data:
-                str = str + c
             if self.in_function == 1:
-                self.function_descr = str
+                text = ''.join(self.data)
+                self.function_descr = text
         elif tag == 'cond':
-            str = ''
-            for c in self._data:
-                str = str + c
             if self.in_function == 1:
-                self.function_cond = str
+                text = ''.join(self.data)
+                self.function_cond = text
 
 
 def function(name, desc, ret, args, file, module, cond):
@@ -792,14 +771,14 @@ def buildStubs(module, stubs_buiding_debug = False, xml_parsing_debug = False):
     try:
         f = open(os.path.join(srcPref,api_xml))
         data = f.read()
-        (parser, target) = getparser(xml_parsing_debug)
+        parser = getparser(xml_parsing_debug)
         parser.feed(data)
         parser.close()
     except IOError, msg:
         try:
             f = open(os.path.join(srcPref,"..","docs",api_xml))
             data = f.read()
-            (parser, target)  = getparser(xml_parsing_debug)
+            parser = getparser(xml_parsing_debug)
             parser.feed(data)
             parser.close()
         except IOError, msg:
@@ -816,7 +795,7 @@ def buildStubs(module, stubs_buiding_debug = False, xml_parsing_debug = False):
     try:
         f = open(os.path.join(srcPref, override_api_xml))
         data = f.read()
-        (parser, target)  = getparser(xml_parsing_debug)
+        parser = getparser(xml_parsing_debug)
         parser.feed(data)
         parser.close()
     except IOError, msg:
-- 
1.7.11.2




More information about the libvir-list mailing list