Merge pull request #6 from groleo/master

genheaders.py: make it usable as a module
diff --git a/api/genheaders.py b/api/genheaders.py
index 50372a4..d170133 100755
--- a/api/genheaders.py
+++ b/api/genheaders.py
@@ -37,39 +37,6 @@
 diagFilename = 'diag.txt'
 regFilename = 'gl.xml'
 
-if __name__ == '__main__':
-    i = 1
-    while (i < len(sys.argv)):
-        arg = sys.argv[i]
-        i = i + 1
-        if (arg == '-debug'):
-            write('Enabling debug (-debug)', file=sys.stderr)
-            debug = True
-        elif (arg == '-dump'):
-            write('Enabling dump (-dump)', file=sys.stderr)
-            dump = True
-        elif (arg == '-noprotect'):
-            write('Disabling inclusion protection in output headers', file=sys.stderr)
-            protect = False
-        elif (arg == '-profile'):
-            write('Enabling profiling (-profile)', file=sys.stderr)
-            profile = True
-        elif (arg == '-registry'):
-            regFilename = sys.argv[i]
-            i = i+1
-            write('Using registry ', regFilename, file=sys.stderr)
-        elif (arg == '-time'):
-            write('Enabling timing (-time)', file=sys.stderr)
-            timeit = True
-        elif (arg == '-validate'):
-            write('Enabling group validation (-validate)', file=sys.stderr)
-            validate = True
-        elif (arg[0:1] == '-'):
-            write('Unrecognized argument:', arg, file=sys.stderr)
-            exit(1)
-        else:
-            target = arg
-            write('Using target', target, file=sys.stderr)
 
 # Simple timer functions
 startTime = None
@@ -83,26 +50,6 @@
         write(msg, endTime - startTime)
         startTime = None
 
-# Load & parse registry
-reg = Registry()
-
-startTimer()
-tree = etree.parse(regFilename)
-endTimer('Time to make ElementTree =')
-
-startTimer()
-reg.loadElementTree(tree)
-endTimer('Time to parse ElementTree =')
-
-if (validate):
-    reg.validateGroups()
-
-if (dump):
-    write('***************************************')
-    write('Performing Registry dump to regdump.txt')
-    write('***************************************')
-    reg.dumpReg(filehandle = open('regdump.txt','w'))
-
 # Turn a list of strings into a regexp string matching exactly those strings
 def makeREstring(list):
     return '^(' + '|'.join(list) + ')$'
@@ -600,13 +547,6 @@
     None
 ]
 
-# create error/warning & diagnostic files
-if (errFilename):
-    errWarn = open(errFilename,'w')
-else:
-    errWarn = sys.stderr
-diag = open(diagFilename, 'w')
-
 def genHeaders():
     # Loop over targets, building each
     generated = 0
@@ -629,12 +569,73 @@
     if (target and generated == 0):
         write('Failed to generate target:', target)
 
-if (debug):
-    pdb.run('genHeaders()')
-elif (profile):
-    import cProfile, pstats
-    cProfile.run('genHeaders()', 'profile.txt')
-    p = pstats.Stats('profile.txt')
-    p.strip_dirs().sort_stats('time').print_stats(50)
-else:
-    genHeaders()
+if __name__ == '__main__':
+    i = 1
+    while (i < len(sys.argv)):
+        arg = sys.argv[i]
+        i = i + 1
+        if (arg == '-debug'):
+            write('Enabling debug (-debug)', file=sys.stderr)
+            debug = True
+        elif (arg == '-dump'):
+            write('Enabling dump (-dump)', file=sys.stderr)
+            dump = True
+        elif (arg == '-noprotect'):
+            write('Disabling inclusion protection in output headers', file=sys.stderr)
+            protect = False
+        elif (arg == '-profile'):
+            write('Enabling profiling (-profile)', file=sys.stderr)
+            profile = True
+        elif (arg == '-registry'):
+            regFilename = sys.argv[i]
+            i = i+1
+            write('Using registry ', regFilename, file=sys.stderr)
+        elif (arg == '-time'):
+            write('Enabling timing (-time)', file=sys.stderr)
+            timeit = True
+        elif (arg == '-validate'):
+            write('Enabling group validation (-validate)', file=sys.stderr)
+            validate = True
+        elif (arg[0:1] == '-'):
+            write('Unrecognized argument:', arg, file=sys.stderr)
+            exit(1)
+        else:
+            target = arg
+            write('Using target', target, file=sys.stderr)
+
+    # Load & parse registry
+    reg = Registry()
+
+    startTimer()
+    tree = etree.parse(regFilename)
+    endTimer('Time to make ElementTree =')
+
+    startTimer()
+    reg.loadElementTree(tree)
+    endTimer('Time to parse ElementTree =')
+
+    if (validate):
+        reg.validateGroups()
+
+    if (dump):
+        write('***************************************')
+        write('Performing Registry dump to regdump.txt')
+        write('***************************************')
+        reg.dumpReg(filehandle = open('regdump.txt','w'))
+
+    # create error/warning & diagnostic files
+    if (errFilename):
+        errWarn = open(errFilename,'w')
+    else:
+        errWarn = sys.stderr
+    diag = open(diagFilename, 'w')
+
+    if (debug):
+        pdb.run('genHeaders()')
+    elif (profile):
+        import cProfile, pstats
+        cProfile.run('genHeaders()', 'profile.txt')
+        p = pstats.Stats('profile.txt')
+        p.strip_dirs().sort_stats('time').print_stats(50)
+    else:
+        genHeaders()