[PATCH 2/4] pwclient: simplify hash/id handling

Bernhard Reutner-Fischer rep.dot.nop at gmail.com
Tue Aug 19 05:07:49 EST 2014


and obtain "action" from args.subcmd while at it

Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop at gmail.com>
---
 apps/patchwork/bin/pwclient | 67 +++++++++++++++++++--------------------------
 1 file changed, 28 insertions(+), 39 deletions(-)

diff --git a/apps/patchwork/bin/pwclient b/apps/patchwork/bin/pwclient
index b91a4b7..a31099d 100755
--- a/apps/patchwork/bin/pwclient
+++ b/apps/patchwork/bin/pwclient
@@ -322,9 +322,17 @@ def patch_id_from_hash(rpc, project, hash):
         patch = rpc.patch_get_by_hash(hash)
 
     if patch == {}:
-        return None
+        sys.stderr.write("No patch has the hash provided\n")
+        sys.exit(1)
 
-    return patch['id']
+    patch_id = patch['id']
+    # be super paranoid
+    try:
+        patch_id = int(patch_id)
+    except:
+        sys.stderr.write("Invalid patch ID obtained from server\n")
+        sys.exit(1)
+    return patch_id
 
 auth_actions = ['update']
 
@@ -417,11 +425,13 @@ def main():
         add_help=False,
         help='''Apply a patch (in the current dir, using -p1)'''
     )
+    apply_parser.set_defaults(subcmd='apply')
     git_am_parser = subparsers.add_parser(
         'git-am', parents=[hash_parser],
         add_help=False,
         help='''Apply a patch to current git branch using "git am".'''
     )
+    git_am_parser.set_defaults(subcmd='git-am')
     git_am_parser.add_argument(
         '-s', '--signoff',
         action='store_true',
@@ -432,31 +442,37 @@ def main():
         add_help=False,
         help='''Download a patch and save it locally'''
     )
+    get_parser.set_defaults(subcmd='get')
     info_parser = subparsers.add_parser(
         'info', parents=[hash_parser],
         add_help=False,
         help='''Display patchwork info about a given patch ID'''
     )
+    info_parser.set_defaults(subcmd='info')
     projects_parser = subparsers.add_parser(
         'projects',
         add_help=False,
         help='''List all projects'''
     )
+    projects_parser.set_defaults(subcmd='projects')
     states_parser = subparsers.add_parser(
         'states',
         add_help=False,
         help='''Show list of potential patch states'''
     )
+    states_parser.set_defaults(subcmd='states')
     view_parser = subparsers.add_parser(
         'view', parents=[hash_parser],
         add_help=False,
         help='''View a patch'''
     )
+    view_parser.set_defaults(subcmd='view')
     update_parser = subparsers.add_parser(
         'update', parents=[hash_parser],
         add_help=False,
         help='''Update patch'''
     )
+    update_parser.set_defaults(subcmd='update')
     update_parser.add_argument(
         '-c', metavar='COMMIT-REF',
         help='''commit reference hash'''
@@ -475,18 +491,19 @@ def main():
         below and an optional substring to search for patches
         by name'''
     )
+    list_parser.set_defaults(subcmd='list')
     search_parser = subparsers.add_parser("search",
         add_help=False,
         parents=[filter_parser],
         help='''Alias for "list"'''
     )
+    search_parser.set_defaults(subcmd='list')
     if len(sys.argv) < 2:
         action_parser.print_help()
         sys.exit(0)
 
     args = action_parser.parse_args()
-
-    action = sys.argv[1].lower()
+    args=dict(vars(args))
 
     # set defaults
     filt = Filter()
@@ -495,12 +512,13 @@ def main():
     project_str = ""
     commit_str = ""
     state_str = ""
-    hash_str = ""
+    hash_str = None
     msgid_str = ""
     id_str = None
     url = DEFAULT_URL
 
-    args=dict(vars(args))
+    action = args.get('subcmd')
+
     if args.get('s'):
         state_str = args.get('s')
     if args.get('p'):
@@ -609,11 +627,12 @@ def main():
         sys.exit(1)
 
     patch_id = None
+    # hash_str and id_str are mutually exclusive
     if hash_str:
         patch_id = patch_id_from_hash(rpc, project_str, hash_str)
-        if patch_id is None:
-            sys.stderr.write("No patch has the hash provided\n")
-            sys.exit(1)
+    else:
+        # id_str from argparse is an int
+        patch_id = id_str
 
     if action == 'list' or action == 'search':
         if args.get('patch_name') != None:
@@ -627,56 +646,26 @@ def main():
         action_states(rpc)
 
     elif action == 'view':
-        try:
-            patch_id = patch_id or int(id_str)
-        except:
-            sys.stderr.write("Invalid patch ID given\n")
-            sys.exit(1)
-
         s = rpc.patch_get_mbox(patch_id)
         if len(s) > 0:
             print unicode(s).encode("utf-8")
 
     elif action in ('get', 'save', 'info'):
-        try:
-            patch_id = patch_id or int(id_str)
-        except:
-            sys.stderr.write("Invalid patch ID given\n")
-            sys.exit(1)
-
         if action == 'info':
             action_info(rpc, patch_id)
         else:
             action_get(rpc, patch_id)
 
     elif action == 'apply':
-        try:
-            patch_id = patch_id or int(id_str)
-        except:
-            sys.stderr.write("Invalid patch ID given\n")
-            sys.exit(1)
-
         action_apply(rpc, patch_id)
 
     elif action == 'git-am':
-        try:
-            patch_id = patch_id or int(id_str)
-        except:
-            sys.stderr.write("Invalid patch ID given\n")
-            sys.exit(1)
-
         cmd = ['git', 'am']
         if args.get('signoff'):
             cmd.append('-s')
         action_apply(rpc, patch_id, cmd)
 
     elif action == 'update':
-        try:
-            patch_id = patch_id or int(id_str)
-        except:
-            sys.stderr.write("Invalid patch ID given\n")
-            sys.exit(1)
-
         action_update_patch(rpc, patch_id, state = state_str,
                 commit = commit_str)
 
-- 
2.0.1



More information about the Patchwork mailing list