Patchwork [04/22] qapi: qapi_visit.py, make code useable as module

login
register
mail settings
Submitter Michael Roth
Date July 24, 2012, 5:20 p.m.
Message ID <1343150454-4677-5-git-send-email-mdroth@linux.vnet.ibm.com>
Download mbox | patch
Permalink /patch/172981/
State New
Headers show

Comments

Michael Roth - July 24, 2012, 5:20 p.m.
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
---
 scripts/qapi_visit.py |  143 +++++++++++++++++++++++++------------------------
 1 file changed, 74 insertions(+), 69 deletions(-)
Anthony Liguori - July 24, 2012, 10:13 p.m.
Michael Roth <mdroth@linux.vnet.ibm.com> writes:

Reviewed-by: Anthony Liguori <aliguori@us.ibm.com>

Regards,

Anthony Liguori


> Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
> ---
>  scripts/qapi_visit.py |  143 +++++++++++++++++++++++++------------------------
>  1 file changed, 74 insertions(+), 69 deletions(-)
>
> diff --git a/scripts/qapi_visit.py b/scripts/qapi_visit.py
> index 04ef7c4..25707f5 100644
> --- a/scripts/qapi_visit.py
> +++ b/scripts/qapi_visit.py
> @@ -224,55 +224,57 @@ void visit_type_%(name)s(Visitor *m, %(name)s * obj, const char *name, Error **e
>  ''',
>                  name=name)
>  
> -try:
> -    opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:o:",
> -                                   ["source", "header", "prefix=", "output-dir="])
> -except getopt.GetoptError, err:
> -    print str(err)
> -    sys.exit(1)
> -
> -output_dir = ""
> -prefix = ""
> -c_file = 'qapi-visit.c'
> -h_file = 'qapi-visit.h'
> -
> -do_c = False
> -do_h = False
> -
> -for o, a in opts:
> -    if o in ("-p", "--prefix"):
> -        prefix = a
> -    elif o in ("-o", "--output-dir"):
> -        output_dir = a + "/"
> -    elif o in ("-c", "--source"):
> +def main(argv=[]):
> +    try:
> +        opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:o:",
> +                                       ["source", "header", "prefix=",
> +                                        "output-dir="])
> +    except getopt.GetoptError, err:
> +        print str(err)
> +        sys.exit(1)
> +
> +    output_dir = ""
> +    prefix = ""
> +    c_file = 'qapi-visit.c'
> +    h_file = 'qapi-visit.h'
> +
> +    do_c = False
> +    do_h = False
> +
> +    for o, a in opts:
> +        if o in ("-p", "--prefix"):
> +            prefix = a
> +        elif o in ("-o", "--output-dir"):
> +            output_dir = a + "/"
> +        elif o in ("-c", "--source"):
> +            do_c = True
> +        elif o in ("-h", "--header"):
> +            do_h = True
> +
> +    if not do_c and not do_h:
>          do_c = True
> -    elif o in ("-h", "--header"):
>          do_h = True
>  
> -if not do_c and not do_h:
> -    do_c = True
> -    do_h = True
> +    c_file = output_dir + prefix + c_file
> +    h_file = output_dir + prefix + h_file
>  
> -c_file = output_dir + prefix + c_file
> -h_file = output_dir + prefix + h_file
> +    try:
> +        os.makedirs(output_dir)
> +    except os.error, e:
> +        if e.errno != errno.EEXIST:
> +            raise
>  
> -try:
> -    os.makedirs(output_dir)
> -except os.error, e:
> -    if e.errno != errno.EEXIST:
> -        raise
> -
> -def maybe_open(really, name, opt):
> -    if really:
> -        return open(name, opt)
> -    else:
> -        import StringIO
> -        return StringIO.StringIO()
> +    def maybe_open(really, name, opt):
> +        if really:
> +            return open(name, opt)
> +        else:
> +            import StringIO
> +            return StringIO.StringIO()
>  
> -fdef = maybe_open(do_c, c_file, 'w')
> -fdecl = maybe_open(do_h, h_file, 'w')
> +    fdef = maybe_open(do_c, c_file, 'w')
> +    fdecl = maybe_open(do_h, h_file, 'w')
>  
> -fdef.write(mcgen('''
> +    fdef.write(mcgen('''
>  /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
>  
>  /*
> @@ -292,7 +294,7 @@ fdef.write(mcgen('''
>  ''',
>                   header=basename(h_file)))
>  
> -fdecl.write(mcgen('''
> +    fdecl.write(mcgen('''
>  /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
>  
>  /*
> @@ -316,37 +318,40 @@ fdecl.write(mcgen('''
>  ''',
>                    prefix=prefix, guard=guardname(h_file)))
>  
> -exprs = parse_schema(sys.stdin)
> +    exprs = parse_schema(sys.stdin)
>  
> -for expr in exprs:
> -    if expr.has_key('type'):
> -        ret = generate_visit_struct(expr['type'], expr['data'])
> -        ret += generate_visit_list(expr['type'], expr['data'])
> -        fdef.write(ret)
> +    for expr in exprs:
> +        if expr.has_key('type'):
> +            ret = generate_visit_struct(expr['type'], expr['data'])
> +            ret += generate_visit_list(expr['type'], expr['data'])
> +            fdef.write(ret)
>  
> -        ret = generate_declaration(expr['type'], expr['data'])
> -        fdecl.write(ret)
> -    elif expr.has_key('union'):
> -        ret = generate_visit_union(expr['union'], expr['data'])
> -        ret += generate_visit_list(expr['union'], expr['data'])
> -        fdef.write(ret)
> +            ret = generate_declaration(expr['type'], expr['data'])
> +            fdecl.write(ret)
> +        elif expr.has_key('union'):
> +            ret = generate_visit_union(expr['union'], expr['data'])
> +            ret += generate_visit_list(expr['union'], expr['data'])
> +            fdef.write(ret)
>  
> -        ret = generate_decl_enum('%sKind' % expr['union'], expr['data'].keys())
> -        ret += generate_declaration(expr['union'], expr['data'])
> -        fdecl.write(ret)
> -    elif expr.has_key('enum'):
> -        ret = generate_visit_enum(expr['enum'], expr['data'])
> -        fdef.write(ret)
> +            ret = generate_decl_enum('%sKind' % expr['union'], expr['data'].keys())
> +            ret += generate_declaration(expr['union'], expr['data'])
> +            fdecl.write(ret)
> +        elif expr.has_key('enum'):
> +            ret = generate_visit_enum(expr['enum'], expr['data'])
> +            fdef.write(ret)
>  
> -        ret = generate_decl_enum(expr['enum'], expr['data'])
> -        fdecl.write(ret)
> +            ret = generate_decl_enum(expr['enum'], expr['data'])
> +            fdecl.write(ret)
>  
> -fdecl.write('''
> +    fdecl.write('''
>  #endif
> -''')
> +    ''')
> +
> +    fdecl.flush()
> +    fdecl.close()
>  
> -fdecl.flush()
> -fdecl.close()
> +    fdef.flush()
> +    fdef.close()
>  
> -fdef.flush()
> -fdef.close()
> +if __name__ == '__main__':
> +    sys.exit(main(sys.argv))
> -- 
> 1.7.9.5

Patch

diff --git a/scripts/qapi_visit.py b/scripts/qapi_visit.py
index 04ef7c4..25707f5 100644
--- a/scripts/qapi_visit.py
+++ b/scripts/qapi_visit.py
@@ -224,55 +224,57 @@  void visit_type_%(name)s(Visitor *m, %(name)s * obj, const char *name, Error **e
 ''',
                 name=name)
 
-try:
-    opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:o:",
-                                   ["source", "header", "prefix=", "output-dir="])
-except getopt.GetoptError, err:
-    print str(err)
-    sys.exit(1)
-
-output_dir = ""
-prefix = ""
-c_file = 'qapi-visit.c'
-h_file = 'qapi-visit.h'
-
-do_c = False
-do_h = False
-
-for o, a in opts:
-    if o in ("-p", "--prefix"):
-        prefix = a
-    elif o in ("-o", "--output-dir"):
-        output_dir = a + "/"
-    elif o in ("-c", "--source"):
+def main(argv=[]):
+    try:
+        opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:o:",
+                                       ["source", "header", "prefix=",
+                                        "output-dir="])
+    except getopt.GetoptError, err:
+        print str(err)
+        sys.exit(1)
+
+    output_dir = ""
+    prefix = ""
+    c_file = 'qapi-visit.c'
+    h_file = 'qapi-visit.h'
+
+    do_c = False
+    do_h = False
+
+    for o, a in opts:
+        if o in ("-p", "--prefix"):
+            prefix = a
+        elif o in ("-o", "--output-dir"):
+            output_dir = a + "/"
+        elif o in ("-c", "--source"):
+            do_c = True
+        elif o in ("-h", "--header"):
+            do_h = True
+
+    if not do_c and not do_h:
         do_c = True
-    elif o in ("-h", "--header"):
         do_h = True
 
-if not do_c and not do_h:
-    do_c = True
-    do_h = True
+    c_file = output_dir + prefix + c_file
+    h_file = output_dir + prefix + h_file
 
-c_file = output_dir + prefix + c_file
-h_file = output_dir + prefix + h_file
+    try:
+        os.makedirs(output_dir)
+    except os.error, e:
+        if e.errno != errno.EEXIST:
+            raise
 
-try:
-    os.makedirs(output_dir)
-except os.error, e:
-    if e.errno != errno.EEXIST:
-        raise
-
-def maybe_open(really, name, opt):
-    if really:
-        return open(name, opt)
-    else:
-        import StringIO
-        return StringIO.StringIO()
+    def maybe_open(really, name, opt):
+        if really:
+            return open(name, opt)
+        else:
+            import StringIO
+            return StringIO.StringIO()
 
-fdef = maybe_open(do_c, c_file, 'w')
-fdecl = maybe_open(do_h, h_file, 'w')
+    fdef = maybe_open(do_c, c_file, 'w')
+    fdecl = maybe_open(do_h, h_file, 'w')
 
-fdef.write(mcgen('''
+    fdef.write(mcgen('''
 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
 
 /*
@@ -292,7 +294,7 @@  fdef.write(mcgen('''
 ''',
                  header=basename(h_file)))
 
-fdecl.write(mcgen('''
+    fdecl.write(mcgen('''
 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
 
 /*
@@ -316,37 +318,40 @@  fdecl.write(mcgen('''
 ''',
                   prefix=prefix, guard=guardname(h_file)))
 
-exprs = parse_schema(sys.stdin)
+    exprs = parse_schema(sys.stdin)
 
-for expr in exprs:
-    if expr.has_key('type'):
-        ret = generate_visit_struct(expr['type'], expr['data'])
-        ret += generate_visit_list(expr['type'], expr['data'])
-        fdef.write(ret)
+    for expr in exprs:
+        if expr.has_key('type'):
+            ret = generate_visit_struct(expr['type'], expr['data'])
+            ret += generate_visit_list(expr['type'], expr['data'])
+            fdef.write(ret)
 
-        ret = generate_declaration(expr['type'], expr['data'])
-        fdecl.write(ret)
-    elif expr.has_key('union'):
-        ret = generate_visit_union(expr['union'], expr['data'])
-        ret += generate_visit_list(expr['union'], expr['data'])
-        fdef.write(ret)
+            ret = generate_declaration(expr['type'], expr['data'])
+            fdecl.write(ret)
+        elif expr.has_key('union'):
+            ret = generate_visit_union(expr['union'], expr['data'])
+            ret += generate_visit_list(expr['union'], expr['data'])
+            fdef.write(ret)
 
-        ret = generate_decl_enum('%sKind' % expr['union'], expr['data'].keys())
-        ret += generate_declaration(expr['union'], expr['data'])
-        fdecl.write(ret)
-    elif expr.has_key('enum'):
-        ret = generate_visit_enum(expr['enum'], expr['data'])
-        fdef.write(ret)
+            ret = generate_decl_enum('%sKind' % expr['union'], expr['data'].keys())
+            ret += generate_declaration(expr['union'], expr['data'])
+            fdecl.write(ret)
+        elif expr.has_key('enum'):
+            ret = generate_visit_enum(expr['enum'], expr['data'])
+            fdef.write(ret)
 
-        ret = generate_decl_enum(expr['enum'], expr['data'])
-        fdecl.write(ret)
+            ret = generate_decl_enum(expr['enum'], expr['data'])
+            fdecl.write(ret)
 
-fdecl.write('''
+    fdecl.write('''
 #endif
-''')
+    ''')
+
+    fdecl.flush()
+    fdecl.close()
 
-fdecl.flush()
-fdecl.close()
+    fdef.flush()
+    fdef.close()
 
-fdef.flush()
-fdef.close()
+if __name__ == '__main__':
+    sys.exit(main(sys.argv))