Patchwork [v2,1/3] qapi.py: Move common code to evaluate()

login
register
mail settings
Submitter Kevin Wolf
Date June 21, 2013, 3:46 p.m.
Message ID <1371829589-18045-2-git-send-email-kwolf@redhat.com>
Download mbox | patch
Permalink /patch/253256/
State New
Headers show

Comments

Kevin Wolf - June 21, 2013, 3:46 p.m.
Don't duplicate more code than is really necessary.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
---
 scripts/qapi.py | 24 ++++++++++--------------
 1 file changed, 10 insertions(+), 14 deletions(-)

Patch

diff --git a/scripts/qapi.py b/scripts/qapi.py
index 02ad668..3a64769 100644
--- a/scripts/qapi.py
+++ b/scripts/qapi.py
@@ -76,12 +76,18 @@  def parse(tokens):
         return tokens[0], tokens[1:]
 
 def evaluate(string):
-    return parse(map(lambda x: x, tokenize(string)))[0]
+    expr_eval = parse(map(lambda x: x, tokenize(string)))[0]
+
+    if expr_eval.has_key('enum'):
+        add_enum(expr_eval['enum'])
+    elif expr_eval.has_key('union'):
+        add_enum('%sKind' % expr_eval['union'])
+
+    return expr_eval
 
 def parse_schema(fp):
     exprs = []
     expr = ''
-    expr_eval = None
 
     for line in fp:
         if line.startswith('#') or line == '\n':
@@ -90,23 +96,13 @@  def parse_schema(fp):
         if line.startswith(' '):
             expr += line
         elif expr:
-            expr_eval = evaluate(expr)
-            if expr_eval.has_key('enum'):
-                add_enum(expr_eval['enum'])
-            elif expr_eval.has_key('union'):
-                add_enum('%sKind' % expr_eval['union'])
-            exprs.append(expr_eval)
+            exprs.append(evaluate(expr))
             expr = line
         else:
             expr += line
 
     if expr:
-        expr_eval = evaluate(expr)
-        if expr_eval.has_key('enum'):
-            add_enum(expr_eval['enum'])
-        elif expr_eval.has_key('union'):
-            add_enum('%sKind' % expr_eval['union'])
-        exprs.append(expr_eval)
+        exprs.append(evaluate(expr))
 
     return exprs