[PATCH 1/2] pwclient: add output format option to list/search command

Jani Nikula jani.nikula at intel.com
Tue Oct 21 19:18:53 AEDT 2014


Make scripting easier by letting the user specify the output format. The
format string may contain tag references to fields, such as %{id} and
%{msgid}.

Signed-off-by: Jani Nikula <jani.nikula at intel.com>
---
 apps/patchwork/bin/pwclient | 38 ++++++++++++++++++++++++++++----------
 1 file changed, 28 insertions(+), 10 deletions(-)

diff --git a/apps/patchwork/bin/pwclient b/apps/patchwork/bin/pwclient
index 64c6817e5700..1713eb730f85 100755
--- a/apps/patchwork/bin/pwclient
+++ b/apps/patchwork/bin/pwclient
@@ -29,6 +29,7 @@ import subprocess
 import base64
 import ConfigParser
 import shutil
+import re
 
 # Default Patchwork remote XML-RPC server URL
 # This script will check the PW_XMLRPC_URL environment variable
@@ -134,14 +135,24 @@ def person_ids_by_name(rpc, name):
     people = rpc.person_list(name, 0)
     return map(lambda x: x['id'], people)
 
-def list_patches(patches):
+def list_patches(patches, format_str=None):
     """Dump a list of patches to stdout."""
-    print("%-7s %-12s %s" % ("ID", "State", "Name"))
-    print("%-7s %-12s %s" % ("--", "-----", "----"))
-    for patch in patches:
-        print("%-7d %-12s %s" % (patch['id'], patch['state'], patch['name']))
+    if format_str:
+        format_field_re = re.compile("%{([a-z0-9_]+)}")
 
-def action_list(rpc, filter, submitter_str, delegate_str):
+        def patch_field(matchobj):
+            fieldname = matchobj.group(1)
+            return str(patch[fieldname])
+
+        for patch in patches:
+            print(format_field_re.sub(patch_field, format_str))
+    else:
+        print("%-7s %-12s %s" % ("ID", "State", "Name"))
+        print("%-7s %-12s %s" % ("--", "-----", "----"))
+        for patch in patches:
+            print("%-7d %-12s %s" % (patch['id'], patch['state'], patch['name']))
+
+def action_list(rpc, filter, submitter_str, delegate_str, format_str=None):
     filter.resolve_ids(rpc)
 
     if submitter_str != "":
@@ -158,7 +169,7 @@ def action_list(rpc, filter, submitter_str, delegate_str):
                 f = filter
                 f.add("submitter_id", id)
                 patches = rpc.patch_list(f.d)
-                list_patches(patches)
+                list_patches(patches, format_str)
         return
 
     if delegate_str != "":
@@ -174,11 +185,11 @@ def action_list(rpc, filter, submitter_str, delegate_str):
                 f = filter
                 f.add("delegate_id", id)
                 patches = rpc.patch_list(f.d)
-                list_patches(patches)
+                list_patches(patches, format_str)
         return
 
     patches = rpc.patch_list(filter.d)
-    list_patches(patches)
+    list_patches(patches, format_str)
 
 def action_projects(rpc):
     projects = rpc.project_list("", 0)
@@ -367,6 +378,10 @@ def main():
         help='''Filter by Message-Id'''
     )
     filter_parser.add_argument(
+        '-f', metavar='FORMAT',
+        help='''Print output in the given format. You can use tags matching fields, e.g. %%{id}, %%{state}, or %%{msgid}.'''
+    )
+    filter_parser.add_argument(
         'patch_name', metavar='STR', nargs='?',
         help='substring to search for patches by name',
     )
@@ -487,6 +502,7 @@ def main():
     filt = Filter()
     submitter_str = ""
     delegate_str = ""
+    format_str = None
     project_str = ""
     commit_str = ""
     state_str = ""
@@ -503,6 +519,8 @@ def main():
         submitter_str = args.get('w')
     if args.get('d'):
         delegate_str = args.get('d')
+    if args.get('f'):
+        format_str = args.get('f')
     if args.get('hash'):
         hash_str = args.get('hash')
     if args.get('id'):
@@ -640,7 +658,7 @@ def main():
     if action == 'list' or action == 'search':
         if args.get('patch_name') != None:
             filt.add("name__icontains", args.get('patch_name'))
-        action_list(rpc, filt, submitter_str, delegate_str)
+        action_list(rpc, filt, submitter_str, delegate_str, format_str)
 
     elif action.startswith('project'):
         action_projects(rpc)
-- 
2.1.1



More information about the Patchwork mailing list