This patch adds 'api' module into spacecmd.

Signed-Off-By: Satoru SATOH <ss...@redhat.com>

---
 spacecmd/src/lib/api.py |   95 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 95 insertions(+), 0 deletions(-)
 create mode 100644 spacecmd/src/lib/api.py

diff --git a/spacecmd/src/lib/api.py b/spacecmd/src/lib/api.py
new file mode 100644
index 0000000..0c1d05c
--- /dev/null
+++ b/spacecmd/src/lib/api.py
@@ -0,0 +1,95 @@
+#
+# Licensed under the GNU General Public License Version 3
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# Copyright 2011 Satoru SATOH <ss...@redhat.com>
+#
+
+from optparse import Option
+from spacecmd.utils import *
+
+import sys
+import logging
+
+
+def help_api(self):
+    print 'api: call RHN API with arguements directly'
+    print '''usage: api [options] API_STRING
+
+options:
+  -A, --args  Arguments for the API other than session id in comma separated
+              strings or JSON expression
+  -F, --format   Output format
+  -o, --output   Output file
+
+examples:
+  api api.getApiCallList
+  api --args "sysgroup_A" systemgroup.listSystems
+  api -A "rhel-i386-server-5,2011-04-01,2011-05-01" -F "%(name)s" \\
+      channel.software.listAllPackages
+'''
+
+
+def do_api(self, args):
+    options = [ Option('-A', '--args', default=''),
+                Option('-F', '--format', default=''),
+                Option('-o', '--output', default='') ]
+
+    (args, options) = parse_arguments(args, options)
+
+    if not args:
+        self.help_api()
+        return
+
+    api_name = args[0]
+    api_args = parse_api_args(options.args)
+
+    if options.output:
+        try:
+            output = open(options.output, "w")
+        except IOError:
+            logging.warn("Could not open to write: " + options.output)
+            logging.info("Fallback output to stdout")
+
+            output = sys.stdout
+    else:
+        output = sys.stdout
+
+    api = getattr(self.client, api_name, None)
+
+    if not callable(api):
+        logging.warn("No such API: " + api_name)
+        return
+
+    try:
+        res = api(self.session, *api_args)
+
+        if not isinstance(res, list):
+            res = [res]
+
+        if options.format:
+            for r in res:
+                output.write(options.format % r + "\n")
+        else:
+            json_dump(res, output, indent=2)
+
+        output == sys.stdout or output.close()
+
+    except:
+        output == sys.stdout or output.close()
+
+
+# vim:ts=4:expandtab:
-- 
1.7.6

_______________________________________________
Spacewalk-devel mailing list
Spacewalk-devel@redhat.com
https://www.redhat.com/mailman/listinfo/spacewalk-devel

Reply via email to