diff mbox series

[RFC,13/32] qapi: Use argparse to open schema file

Message ID 20171002152552.27999-14-armbru@redhat.com
State New
Headers show
Series Command line QAPIfication | expand

Commit Message

Markus Armbruster Oct. 2, 2017, 3:25 p.m. UTC
QAPISchema.__init__() opens the schema file.  Since it doesn't bother
to catch exceptions, an invalid schema argument is reported like this:

    Traceback (most recent call last):
      File "../scripts/qapi-commands.py", line 318, in <module>
	schema = QAPISchema(args.schema)
      File "/work/armbru/qemu/scripts/qapi.py", line 1464, in __init__
	parser = QAPISchemaParser(open(fname, 'r'))
    IOError: [Errno 2] No such file or directory: 'nonexistent'

Leave it to argparse, which handles the exception like this:

    usage: qapi-commands.py [-h] [-o OUTPUT_DIR] [-p PREFIX] schema
    qapi-commands.py: error: argument schema: can't open 'nonexistent': [Errno 2] No such file or directory: 'nonexistent'

Too verbose for my taste, but let's not second-guess the standard
library.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 scripts/qapi.py                | 6 +++---
 scripts/qapi2texi.py           | 2 +-
 tests/qapi-schema/test-qapi.py | 2 +-
 3 files changed, 5 insertions(+), 5 deletions(-)

Comments

Marc-André Lureau Oct. 4, 2017, 11:18 a.m. UTC | #1
On Mon, Oct 2, 2017 at 5:25 PM, Markus Armbruster <armbru@redhat.com> wrote:
> QAPISchema.__init__() opens the schema file.  Since it doesn't bother
> to catch exceptions, an invalid schema argument is reported like this:
>
>     Traceback (most recent call last):
>       File "../scripts/qapi-commands.py", line 318, in <module>
>         schema = QAPISchema(args.schema)
>       File "/work/armbru/qemu/scripts/qapi.py", line 1464, in __init__
>         parser = QAPISchemaParser(open(fname, 'r'))
>     IOError: [Errno 2] No such file or directory: 'nonexistent'
>
> Leave it to argparse, which handles the exception like this:
>
>     usage: qapi-commands.py [-h] [-o OUTPUT_DIR] [-p PREFIX] schema
>     qapi-commands.py: error: argument schema: can't open 'nonexistent': [Errno 2] No such file or directory: 'nonexistent'
>
> Too verbose for my taste, but let's not second-guess the standard
> library.

indeed :)

>
> Signed-off-by: Markus Armbruster <armbru@redhat.com>

Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>


> ---
>  scripts/qapi.py                | 6 +++---
>  scripts/qapi2texi.py           | 2 +-
>  tests/qapi-schema/test-qapi.py | 2 +-
>  3 files changed, 5 insertions(+), 5 deletions(-)
>
> diff --git a/scripts/qapi.py b/scripts/qapi.py
> index 25f6c81b08..a33203e82d 100644
> --- a/scripts/qapi.py
> +++ b/scripts/qapi.py
> @@ -1450,9 +1450,9 @@ class QAPISchemaEvent(QAPISchemaEntity):
>
>
>  class QAPISchema(object):
> -    def __init__(self, fname):
> +    def __init__(self, file):
>          try:
> -            parser = QAPISchemaParser(open(fname, 'r'))
> +            parser = QAPISchemaParser(file)
>              self.exprs = check_exprs(parser.exprs)
>              self.docs = parser.docs
>              self._entity_dict = {}
> @@ -1934,7 +1934,7 @@ def common_argument_parser(builtins=False):
>                          help='output directory')
>      parser.add_argument('-p', '--prefix', default='', type=prefix,
>                          help='prefix to add to output files')
> -    parser.add_argument('schema',
> +    parser.add_argument('schema', type=argparse.FileType('r'),
>                          help='QAPI schema source file')
>      return parser
>
> diff --git a/scripts/qapi2texi.py b/scripts/qapi2texi.py
> index fd90d8953e..d95d7541a3 100755
> --- a/scripts/qapi2texi.py
> +++ b/scripts/qapi2texi.py
> @@ -282,7 +282,7 @@ def texi_schema(schema):
>  def main(argv):
>      """Takes schema argument, prints result to stdout"""
>      parser = argparse.ArgumentParser()
> -    parser.add_argument('schema',
> +    parser.add_argument('schema', type=argparse.FileType('r'),
>                          help='QAPI schema source file')
>      args = parser.parse_args()
>
> diff --git a/tests/qapi-schema/test-qapi.py b/tests/qapi-schema/test-qapi.py
> index a7e21d016f..225417d861 100644
> --- a/tests/qapi-schema/test-qapi.py
> +++ b/tests/qapi-schema/test-qapi.py
> @@ -53,7 +53,7 @@ class QAPISchemaTestVisitor(QAPISchemaVisitor):
>                  print '    case %s: %s' % (v.name, v.type.name)
>
>  parser = argparse.ArgumentParser()
> -parser.add_argument('schema',
> +parser.add_argument('schema', type=argparse.FileType('r'),
>                      help='QAPI schema source file')
>  args = parser.parse_args()
>
> --
> 2.13.6
>
>
diff mbox series

Patch

diff --git a/scripts/qapi.py b/scripts/qapi.py
index 25f6c81b08..a33203e82d 100644
--- a/scripts/qapi.py
+++ b/scripts/qapi.py
@@ -1450,9 +1450,9 @@  class QAPISchemaEvent(QAPISchemaEntity):
 
 
 class QAPISchema(object):
-    def __init__(self, fname):
+    def __init__(self, file):
         try:
-            parser = QAPISchemaParser(open(fname, 'r'))
+            parser = QAPISchemaParser(file)
             self.exprs = check_exprs(parser.exprs)
             self.docs = parser.docs
             self._entity_dict = {}
@@ -1934,7 +1934,7 @@  def common_argument_parser(builtins=False):
                         help='output directory')
     parser.add_argument('-p', '--prefix', default='', type=prefix,
                         help='prefix to add to output files')
-    parser.add_argument('schema',
+    parser.add_argument('schema', type=argparse.FileType('r'),
                         help='QAPI schema source file')
     return parser
 
diff --git a/scripts/qapi2texi.py b/scripts/qapi2texi.py
index fd90d8953e..d95d7541a3 100755
--- a/scripts/qapi2texi.py
+++ b/scripts/qapi2texi.py
@@ -282,7 +282,7 @@  def texi_schema(schema):
 def main(argv):
     """Takes schema argument, prints result to stdout"""
     parser = argparse.ArgumentParser()
-    parser.add_argument('schema',
+    parser.add_argument('schema', type=argparse.FileType('r'),
                         help='QAPI schema source file')
     args = parser.parse_args()
 
diff --git a/tests/qapi-schema/test-qapi.py b/tests/qapi-schema/test-qapi.py
index a7e21d016f..225417d861 100644
--- a/tests/qapi-schema/test-qapi.py
+++ b/tests/qapi-schema/test-qapi.py
@@ -53,7 +53,7 @@  class QAPISchemaTestVisitor(QAPISchemaVisitor):
                 print '    case %s: %s' % (v.name, v.type.name)
 
 parser = argparse.ArgumentParser()
-parser.add_argument('schema',
+parser.add_argument('schema', type=argparse.FileType('r'),
                     help='QAPI schema source file')
 args = parser.parse_args()