c_client.py: allow importing

Submitted by Aleksandr Mezin on June 10, 2014, 8:44 a.m.

Details

Message ID 1402389890-32034-1-git-send-email-mezin.alexander@gmail.com
State New
Headers show

Commit Message

Aleksandr Mezin June 10, 2014, 8:44 a.m.
I am working on C++ bindings, which internally use C API.
Because of that, I need many parts of c_client code.
Instead of copying&pasting, I want to import c_client.
This patch makes it possible.

Signed-off-by: Alexander Mezin <mezin.alexander@gmail.com>
---
 src/c_client.py | 115 ++++++++++++++++++++++++++++----------------------------
 1 file changed, 58 insertions(+), 57 deletions(-)

Patch hide | download patch | download mbox

diff --git a/src/c_client.py b/src/c_client.py
index c94a9e6..b56e902 100644
--- a/src/c_client.py
+++ b/src/c_client.py
@@ -2961,71 +2961,72 @@  def c_error(self, name):
 
 
 # Main routine starts here
-
-# Must create an "output" dictionary before any xcbgen imports.
-output = {'open'    : c_open,
-          'close'   : c_close,
-          'simple'  : c_simple,
-          'enum'    : c_enum,
-          'struct'  : c_struct,
-          'union'   : c_union,
-          'request' : c_request,
-          'event'   : c_event,
-          'error'   : c_error, 
-          }
-
-# Boilerplate below this point
-
-# Check for the argument that specifies path to the xcbgen python package.
-try:
-    opts, args = getopt.getopt(sys.argv[1:], 'c:l:s:p:m')
-except getopt.GetoptError as err:
-    print(err)
-    print('Usage: c_client.py -c center_footer -l left_footer -s section [-p path] file.xml')
-    sys.exit(1)
-
-for (opt, arg) in opts:
-    if opt == '-c':
-        center_footer=arg
-    if opt == '-l':
-        left_footer=arg
-    if opt == '-s':
-        section=arg
-    if opt == '-p':
-        sys.path.insert(1, arg)
-    elif opt == '-m':
-        manpaths = True
-        sys.stdout.write('man_MANS = ')
-
-# Import the module class
-try:
-    from xcbgen.state import Module
-    from xcbgen.xtypes import *
-except ImportError:
-    print('''
+if __name__ == "__main__":
+
+    # Must create an "output" dictionary before any xcbgen imports.
+    output = {'open'    : c_open,
+              'close'   : c_close,
+              'simple'  : c_simple,
+              'enum'    : c_enum,
+              'struct'  : c_struct,
+              'union'   : c_union,
+              'request' : c_request,
+              'event'   : c_event,
+              'error'   : c_error,
+              }
+
+    # Boilerplate below this point
+
+    # Check for the argument that specifies path to the xcbgen python package.
+    try:
+        opts, args = getopt.getopt(sys.argv[1:], 'c:l:s:p:m')
+    except getopt.GetoptError as err:
+        print(err)
+        print('Usage: c_client.py -c center_footer -l left_footer -s section [-p path] file.xml')
+        sys.exit(1)
+
+    for (opt, arg) in opts:
+        if opt == '-c':
+            center_footer=arg
+        if opt == '-l':
+            left_footer=arg
+        if opt == '-s':
+            section=arg
+        if opt == '-p':
+            sys.path.insert(1, arg)
+        elif opt == '-m':
+            manpaths = True
+            sys.stdout.write('man_MANS = ')
+
+    # Import the module class
+    try:
+        from xcbgen.state import Module
+        from xcbgen.xtypes import *
+    except ImportError:
+        print('''
 Failed to load the xcbgen Python package!
 Make sure that xcb/proto installed it on your Python path.
 If not, you will need to create a .pth file or define $PYTHONPATH
 to extend the path.
 Refer to the README file in xcb/proto for more info.
 ''')
-    raise
-
-# Ensure the man subdirectory exists
-try:
-    os.mkdir('man')
-except OSError as e:
-    if e.errno != errno.EEXIST:
         raise
 
-today = time.strftime('%Y-%m-%d', time.gmtime(os.path.getmtime(args[0])))
+    # Ensure the man subdirectory exists
+    try:
+        os.mkdir('man')
+    except OSError as e:
+        if e.errno != errno.EEXIST:
+            raise
+
+    today = time.strftime('%Y-%m-%d', time.gmtime(os.path.getmtime(args[0])))
 
-# Parse the xml header
-module = Module(args[0], output)
+    # Parse the xml header
+    module = Module(args[0], output)
 
-# Build type-registry and resolve type dependencies
-module.register()
-module.resolve()
+    # Build type-registry and resolve type dependencies
+    module.register()
+    module.resolve()
 
-# Output the code
-module.generate()
+    # Output the code
+    module.generate()