[libvirt] [PATCH 1/4] python: global variable and debugging improvement for generator.py

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


* Put import clause in front of global variables
* Sink __name__ == "__main__" to the bottom of this script and
  support "import generator"
* Remove "quiet" and "debug" global variables and use
  stubs_buiding_debug and xml_parsing_debug variable instead
---
 python/generator.py | 105 +++++++++++++++++++++++++++-------------------------
 1 file changed, 54 insertions(+), 51 deletions(-)

diff --git a/python/generator.py b/python/generator.py
index e4c9579..246767c 100755
--- a/python/generator.py
+++ b/python/generator.py
@@ -3,31 +3,20 @@
 # generate python wrappers from the XML API description
 #
 
-functions = {}
-lxc_functions = {}
-qemu_functions = {}
-enums = {} # { enumType: { enumConstant: enumValue } }
-lxc_enums = {} # { enumType: { enumConstant: enumValue } }
-qemu_enums = {} # { enumType: { enumConstant: enumValue } }
-
 import os
 import sys
 import string
 import re
 
-quiet=True
+from xml.sax import ContentHandler
+from xml.sax import make_parser
 
-if __name__ == "__main__":
-    # launched as a script
-    srcPref = os.path.dirname(sys.argv[0])
-    if len(sys.argv) > 1:
-        python = sys.argv[1]
-    else:
-        print "Python binary not specified"
-        sys.exit(1)
-else:
-    # imported
-    srcPref = os.path.dirname(__file__)
+functions = {}
+lxc_functions = {}
+qemu_functions = {}
+enums = {} # { enumType: { enumConstant: enumValue } }
+lxc_enums = {} # { enumType: { enumConstant: enumValue } }
+qemu_enums = {} # { enumType: { enumConstant: enumValue } }
 
 #######################################################################
 #
@@ -35,20 +24,17 @@ else:
 #  libvirt API description
 #
 #######################################################################
-import os
-import xml.sax
 
-debug = 0
-
-def getparser():
+def getparser(debug):
     # Attach parser to an unmarshalling object. return both objects.
-    target = docParser()
-    parser = xml.sax.make_parser()
+    target = docParser(debug)
+    parser = make_parser()
     parser.setContentHandler(target)
     return parser, target
 
-class docParser(xml.sax.handler.ContentHandler):
-    def __init__(self):
+class docParser(ContentHandler):
+    def __init__(self, debug = False):
+        self.debug = debug;
         self._methodname = None
         self._data = []
         self.in_function = 0
@@ -58,24 +44,24 @@ class docParser(xml.sax.handler.ContentHandler):
         self.characters = self.data
 
     def close(self):
-        if debug:
+        if self.debug:
             print "close"
 
     def getmethodname(self):
         return self._methodname
 
     def data(self, text):
-        if debug:
+        if self.debug:
             print "data %s" % text
         self._data.append(text)
 
     def cdata(self, text):
-        if debug:
+        if self.debug:
             print "data %s" % text
         self._data.append(text)
 
     def start(self, tag, attrs):
-        if debug:
+        if self.debug:
             print "start %s, %s" % (tag, attrs)
         if tag == 'function':
             self._data = []
@@ -132,7 +118,7 @@ class docParser(xml.sax.handler.ContentHandler):
                 qemu_enum(attrs['type'],attrs['name'],attrs['value'])
 
     def end(self, tag):
-        if debug:
+        if self.debug:
             print "end %s" % tag
         if tag == 'function':
             # fuctions come from source files, hence 'virerror.c'
@@ -779,7 +765,7 @@ def print_function_wrapper(module, name, output, export, include):
             return 0
     return 1
 
-def buildStubs(module):
+def buildStubs(module, stubs_buiding_debug = False, xml_parsing_debug = False):
     global py_types
     global py_return_types
     global unknown_types
@@ -806,14 +792,14 @@ def buildStubs(module):
     try:
         f = open(os.path.join(srcPref,api_xml))
         data = f.read()
-        (parser, target)  = getparser()
+        (parser, target) = 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()
+            (parser, target)  = getparser(xml_parsing_debug)
             parser.feed(data)
             parser.close()
         except IOError, msg:
@@ -821,7 +807,7 @@ def buildStubs(module):
             sys.exit(1)
 
     n = len(funcs.keys())
-    if not quiet:
+    if stubs_buiding_debug:
         print "Found %d functions in %s" % ((n), api_xml)
 
     override_api_xml = "%s-override-api.xml" % module
@@ -830,13 +816,13 @@ def buildStubs(module):
     try:
         f = open(os.path.join(srcPref, override_api_xml))
         data = f.read()
-        (parser, target)  = getparser()
+        (parser, target)  = getparser(xml_parsing_debug)
         parser.feed(data)
         parser.close()
     except IOError, msg:
         print file, ":", msg
 
-    if not quiet:
+    if stubs_buiding_debug:
         # XXX: This is not right, same function already in @functions
         # will be overwritten.
         print "Found %d functions in %s" % ((len(funcs.keys()) - n), override_api_xml)
@@ -879,7 +865,7 @@ def buildStubs(module):
     export.close()
     wrapper.close()
 
-    if not quiet:
+    if stubs_buiding_debug:
         print "Generated %d wrapper functions" % nb_wrap
 
     if unknown_types:
@@ -1955,15 +1941,32 @@ def lxcBuildWrappers(module):
 
     fd.close()
 
+if __name__ == "__main__":
+    # launched as a script
 
-quiet = 0
-if buildStubs("libvirt") < 0:
-    sys.exit(1)
-if buildStubs("libvirt-lxc") < 0:
-    sys.exit(1)
-if buildStubs("libvirt-qemu") < 0:
-    sys.exit(1)
-buildWrappers("libvirt")
-lxcBuildWrappers("libvirt-lxc")
-qemuBuildWrappers("libvirt-qemu")
-sys.exit(0)
+    stubs_buiding_debug = False
+    xml_parsing_debug = False
+
+    srcPref = os.path.dirname(sys.argv[0])
+    if len(sys.argv) > 1:
+        python = sys.argv[1]
+    else:
+        print "Python binary not specified"
+        sys.exit(1)
+
+    if buildStubs("libvirt", stubs_buiding_debug, xml_parsing_debug) < 0:
+        sys.exit(1)
+    if buildStubs("libvirt-lxc", stubs_buiding_debug, xml_parsing_debug) < 0:
+        sys.exit(1)
+    if buildStubs("libvirt-qemu", stubs_buiding_debug, xml_parsing_debug) < 0:
+        sys.exit(1)
+
+    buildWrappers("libvirt")
+    lxcBuildWrappers("libvirt-lxc")
+    qemuBuildWrappers("libvirt-qemu")
+    sys.exit(0)
+else:
+    # imported
+    srcPref = os.path.dirname(__file__)
+    python = sys.executable
+    assert python
-- 
1.7.11.2




More information about the libvir-list mailing list