Patchwork [1/4] qapi-types.py: Add a main() like function

login
register
mail settings
Submitter Luiz Capitulino
Date Oct. 17, 2011, 3:29 p.m.
Message ID <1318865377-3328-2-git-send-email-lcapitulino@redhat.com>
Download mbox | patch
Permalink /patch/120235/
State New
Headers show

Comments

Luiz Capitulino - Oct. 17, 2011, 3:29 p.m.
Makes it easier to read the code.

Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
---
 scripts/qapi-types.py |  230 ++++++++++++++++++++++++------------------------
 1 files changed, 115 insertions(+), 115 deletions(-)
Michael Roth - Oct. 17, 2011, 9:19 p.m.
Reviewed-by: Michael Roth <mdroth@linux.vnet.ibm.com>

On Mon, 17 Oct 2011 13:29:34 -0200, Luiz Capitulino <lcapitulino@redhat.com> wrote:
> Makes it easier to read the code.
> 
> Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
> ---
>  scripts/qapi-types.py |  230 ++++++++++++++++++++++++------------------------
>  1 files changed, 115 insertions(+), 115 deletions(-)
> 
> diff --git a/scripts/qapi-types.py b/scripts/qapi-types.py
> index f64d84c..986ff1e 100644
> --- a/scripts/qapi-types.py
> +++ b/scripts/qapi-types.py
> @@ -161,118 +161,118 @@ void qapi_free_%(type)s(%(c_type)s obj)
>                  c_type=c_type(name),type=name)
>      return ret
> 
> -
> -try:
> -    opts, args = getopt.gnu_getopt(sys.argv[1:], "p:o:", ["prefix=", "output-dir="])
> -except getopt.GetoptError, err:
> -    print str(err)
> -    sys.exit(1)
> -
> -output_dir = ""
> -prefix = ""
> -c_file = 'qapi-types.c'
> -h_file = 'qapi-types.h'
> -
> -for o, a in opts:
> -    if o in ("-p", "--prefix"):
> -        prefix = a
> -    elif o in ("-o", "--output-dir"):
> -        output_dir = a + "/"
> -
> -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
> -
> -fdef = open(c_file, 'w')
> -fdecl = open(h_file, 'w')
> -
> -fdef.write(mcgen('''
> -/* AUTOMATICALLY GENERATED, DO NOT MODIFY */
> -
> -/*
> - * deallocation functions for schema-defined QAPI types
> - *
> - * Copyright IBM, Corp. 2011
> - *
> - * Authors:
> - *  Anthony Liguori   <aliguori@us.ibm.com>
> - *  Michael Roth      <mdroth@linux.vnet.ibm.com>
> - *
> - * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
> - * See the COPYING.LIB file in the top-level directory.
> - *
> - */
> -
> -#include "qapi/qapi-dealloc-visitor.h"
> -#include "%(prefix)sqapi-types.h"
> -#include "%(prefix)sqapi-visit.h"
> -
> -''',             prefix=prefix))
> -
> -fdecl.write(mcgen('''
> -/* AUTOMATICALLY GENERATED, DO NOT MODIFY */
> -
> -/*
> - * schema-defined QAPI types
> - *
> - * Copyright IBM, Corp. 2011
> - *
> - * Authors:
> - *  Anthony Liguori   <aliguori@us.ibm.com>
> - *
> - * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
> - * See the COPYING.LIB file in the top-level directory.
> - *
> - */
> -
> -#ifndef %(guard)s
> -#define %(guard)s
> -
> -#include "qapi/qapi-types-core.h"
> -''',
> -                  guard=guardname(h_file)))
> -
> -exprs = parse_schema(sys.stdin)
> -
> -for expr in exprs:
> -    ret = "\n"
> -    if expr.has_key('type'):
> -        ret += generate_fwd_struct(expr['type'], expr['data'])
> -    elif expr.has_key('enum'):
> -        ret += generate_enum(expr['enum'], expr['data'])
> -        fdef.write(generate_enum_lookup(expr['enum'], expr['data']))
> -    elif expr.has_key('union'):
> -        ret += generate_fwd_struct(expr['union'], expr['data']) + "\n"
> -        ret += generate_enum('%sKind' % expr['union'], expr['data'].keys())
> -    else:
> -        continue
> -    fdecl.write(ret)
> -
> -for expr in exprs:
> -    ret = "\n"
> -    if expr.has_key('type'):
> -        ret += generate_struct(expr['type'], "", expr['data']) + "\n"
> -        ret += generate_type_cleanup_decl(expr['type'] + "List")
> -        fdef.write(generate_type_cleanup(expr['type'] + "List") + "\n")
> -        ret += generate_type_cleanup_decl(expr['type'])
> -        fdef.write(generate_type_cleanup(expr['type']) + "\n")
> -    elif expr.has_key('union'):
> -        ret += generate_union(expr['union'], expr['data'])
> -    else:
> -        continue
> -    fdecl.write(ret)
> -
> -fdecl.write('''
> -#endif
> -''')
> -
> -fdecl.flush()
> -fdecl.close()
> -
> -fdef.flush()
> -fdef.close()
> +if __name__ == '__main__':
> +    try:
> +        opts, args = getopt.gnu_getopt(sys.argv[1:], "p:o:", ["prefix=", "output-dir="])
> +    except getopt.GetoptError, err:
> +        print str(err)
> +        sys.exit(1)
> +    
> +    output_dir = ""
> +    prefix = ""
> +    c_file = 'qapi-types.c'
> +    h_file = 'qapi-types.h'
> +    
> +    for o, a in opts:
> +        if o in ("-p", "--prefix"):
> +            prefix = a
> +        elif o in ("-o", "--output-dir"):
> +            output_dir = a + "/"
> +    
> +    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
> +    
> +    fdef = open(c_file, 'w')
> +    fdecl = open(h_file, 'w')
> +    
> +    fdef.write(mcgen('''
> +    /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
> +    
> +    /*
> +     * deallocation functions for schema-defined QAPI types
> +     *
> +     * Copyright IBM, Corp. 2011
> +     *
> +     * Authors:
> +     *  Anthony Liguori   <aliguori@us.ibm.com>
> +     *  Michael Roth      <mdroth@linux.vnet.ibm.com>
> +     *
> +     * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
> +     * See the COPYING.LIB file in the top-level directory.
> +     *
> +     */
> +    
> +    #include "qapi/qapi-dealloc-visitor.h"
> +    #include "%(prefix)sqapi-types.h"
> +    #include "%(prefix)sqapi-visit.h"
> +    
> +    ''',             prefix=prefix))
> +    
> +    fdecl.write(mcgen('''
> +    /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
> +    
> +    /*
> +     * schema-defined QAPI types
> +     *
> +     * Copyright IBM, Corp. 2011
> +     *
> +     * Authors:
> +     *  Anthony Liguori   <aliguori@us.ibm.com>
> +     *
> +     * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
> +     * See the COPYING.LIB file in the top-level directory.
> +     *
> +     */
> +    
> +    #ifndef %(guard)s
> +    #define %(guard)s
> +    
> +    #include "qapi/qapi-types-core.h"
> +    ''',
> +                      guard=guardname(h_file)))
> +    
> +    exprs = parse_schema(sys.stdin)
> +    
> +    for expr in exprs:
> +        ret = "\n"
> +        if expr.has_key('type'):
> +            ret += generate_fwd_struct(expr['type'], expr['data'])
> +        elif expr.has_key('enum'):
> +            ret += generate_enum(expr['enum'], expr['data'])
> +            fdef.write(generate_enum_lookup(expr['enum'], expr['data']))
> +        elif expr.has_key('union'):
> +            ret += generate_fwd_struct(expr['union'], expr['data']) + "\n"
> +            ret += generate_enum('%sKind' % expr['union'], expr['data'].keys())
> +        else:
> +            continue
> +        fdecl.write(ret)
> +    
> +    for expr in exprs:
> +        ret = "\n"
> +        if expr.has_key('type'):
> +            ret += generate_struct(expr['type'], "", expr['data']) + "\n"
> +            ret += generate_type_cleanup_decl(expr['type'] + "List")
> +            fdef.write(generate_type_cleanup(expr['type'] + "List") + "\n")
> +            ret += generate_type_cleanup_decl(expr['type'])
> +            fdef.write(generate_type_cleanup(expr['type']) + "\n")
> +        elif expr.has_key('union'):
> +            ret += generate_union(expr['union'], expr['data'])
> +        else:
> +            continue
> +        fdecl.write(ret)
> +    
> +    fdecl.write('''
> +    #endif
> +    ''')
> +    
> +    fdecl.flush()
> +    fdecl.close()
> +    
> +    fdef.flush()
> +    fdef.close()
> -- 
> 1.7.7.rc3
>

Patch

diff --git a/scripts/qapi-types.py b/scripts/qapi-types.py
index f64d84c..986ff1e 100644
--- a/scripts/qapi-types.py
+++ b/scripts/qapi-types.py
@@ -161,118 +161,118 @@  void qapi_free_%(type)s(%(c_type)s obj)
                 c_type=c_type(name),type=name)
     return ret
 
-
-try:
-    opts, args = getopt.gnu_getopt(sys.argv[1:], "p:o:", ["prefix=", "output-dir="])
-except getopt.GetoptError, err:
-    print str(err)
-    sys.exit(1)
-
-output_dir = ""
-prefix = ""
-c_file = 'qapi-types.c'
-h_file = 'qapi-types.h'
-
-for o, a in opts:
-    if o in ("-p", "--prefix"):
-        prefix = a
-    elif o in ("-o", "--output-dir"):
-        output_dir = a + "/"
-
-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
-
-fdef = open(c_file, 'w')
-fdecl = open(h_file, 'w')
-
-fdef.write(mcgen('''
-/* AUTOMATICALLY GENERATED, DO NOT MODIFY */
-
-/*
- * deallocation functions for schema-defined QAPI types
- *
- * Copyright IBM, Corp. 2011
- *
- * Authors:
- *  Anthony Liguori   <aliguori@us.ibm.com>
- *  Michael Roth      <mdroth@linux.vnet.ibm.com>
- *
- * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
- * See the COPYING.LIB file in the top-level directory.
- *
- */
-
-#include "qapi/qapi-dealloc-visitor.h"
-#include "%(prefix)sqapi-types.h"
-#include "%(prefix)sqapi-visit.h"
-
-''',             prefix=prefix))
-
-fdecl.write(mcgen('''
-/* AUTOMATICALLY GENERATED, DO NOT MODIFY */
-
-/*
- * schema-defined QAPI types
- *
- * Copyright IBM, Corp. 2011
- *
- * Authors:
- *  Anthony Liguori   <aliguori@us.ibm.com>
- *
- * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
- * See the COPYING.LIB file in the top-level directory.
- *
- */
-
-#ifndef %(guard)s
-#define %(guard)s
-
-#include "qapi/qapi-types-core.h"
-''',
-                  guard=guardname(h_file)))
-
-exprs = parse_schema(sys.stdin)
-
-for expr in exprs:
-    ret = "\n"
-    if expr.has_key('type'):
-        ret += generate_fwd_struct(expr['type'], expr['data'])
-    elif expr.has_key('enum'):
-        ret += generate_enum(expr['enum'], expr['data'])
-        fdef.write(generate_enum_lookup(expr['enum'], expr['data']))
-    elif expr.has_key('union'):
-        ret += generate_fwd_struct(expr['union'], expr['data']) + "\n"
-        ret += generate_enum('%sKind' % expr['union'], expr['data'].keys())
-    else:
-        continue
-    fdecl.write(ret)
-
-for expr in exprs:
-    ret = "\n"
-    if expr.has_key('type'):
-        ret += generate_struct(expr['type'], "", expr['data']) + "\n"
-        ret += generate_type_cleanup_decl(expr['type'] + "List")
-        fdef.write(generate_type_cleanup(expr['type'] + "List") + "\n")
-        ret += generate_type_cleanup_decl(expr['type'])
-        fdef.write(generate_type_cleanup(expr['type']) + "\n")
-    elif expr.has_key('union'):
-        ret += generate_union(expr['union'], expr['data'])
-    else:
-        continue
-    fdecl.write(ret)
-
-fdecl.write('''
-#endif
-''')
-
-fdecl.flush()
-fdecl.close()
-
-fdef.flush()
-fdef.close()
+if __name__ == '__main__':
+    try:
+        opts, args = getopt.gnu_getopt(sys.argv[1:], "p:o:", ["prefix=", "output-dir="])
+    except getopt.GetoptError, err:
+        print str(err)
+        sys.exit(1)
+    
+    output_dir = ""
+    prefix = ""
+    c_file = 'qapi-types.c'
+    h_file = 'qapi-types.h'
+    
+    for o, a in opts:
+        if o in ("-p", "--prefix"):
+            prefix = a
+        elif o in ("-o", "--output-dir"):
+            output_dir = a + "/"
+    
+    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
+    
+    fdef = open(c_file, 'w')
+    fdecl = open(h_file, 'w')
+    
+    fdef.write(mcgen('''
+    /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
+    
+    /*
+     * deallocation functions for schema-defined QAPI types
+     *
+     * Copyright IBM, Corp. 2011
+     *
+     * Authors:
+     *  Anthony Liguori   <aliguori@us.ibm.com>
+     *  Michael Roth      <mdroth@linux.vnet.ibm.com>
+     *
+     * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+     * See the COPYING.LIB file in the top-level directory.
+     *
+     */
+    
+    #include "qapi/qapi-dealloc-visitor.h"
+    #include "%(prefix)sqapi-types.h"
+    #include "%(prefix)sqapi-visit.h"
+    
+    ''',             prefix=prefix))
+    
+    fdecl.write(mcgen('''
+    /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
+    
+    /*
+     * schema-defined QAPI types
+     *
+     * Copyright IBM, Corp. 2011
+     *
+     * Authors:
+     *  Anthony Liguori   <aliguori@us.ibm.com>
+     *
+     * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+     * See the COPYING.LIB file in the top-level directory.
+     *
+     */
+    
+    #ifndef %(guard)s
+    #define %(guard)s
+    
+    #include "qapi/qapi-types-core.h"
+    ''',
+                      guard=guardname(h_file)))
+    
+    exprs = parse_schema(sys.stdin)
+    
+    for expr in exprs:
+        ret = "\n"
+        if expr.has_key('type'):
+            ret += generate_fwd_struct(expr['type'], expr['data'])
+        elif expr.has_key('enum'):
+            ret += generate_enum(expr['enum'], expr['data'])
+            fdef.write(generate_enum_lookup(expr['enum'], expr['data']))
+        elif expr.has_key('union'):
+            ret += generate_fwd_struct(expr['union'], expr['data']) + "\n"
+            ret += generate_enum('%sKind' % expr['union'], expr['data'].keys())
+        else:
+            continue
+        fdecl.write(ret)
+    
+    for expr in exprs:
+        ret = "\n"
+        if expr.has_key('type'):
+            ret += generate_struct(expr['type'], "", expr['data']) + "\n"
+            ret += generate_type_cleanup_decl(expr['type'] + "List")
+            fdef.write(generate_type_cleanup(expr['type'] + "List") + "\n")
+            ret += generate_type_cleanup_decl(expr['type'])
+            fdef.write(generate_type_cleanup(expr['type']) + "\n")
+        elif expr.has_key('union'):
+            ret += generate_union(expr['union'], expr['data'])
+        else:
+            continue
+        fdecl.write(ret)
+    
+    fdecl.write('''
+    #endif
+    ''')
+    
+    fdecl.flush()
+    fdecl.close()
+    
+    fdef.flush()
+    fdef.close()