You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by bh...@apache.org on 2015/06/05 17:10:57 UTC

cloudstack-cloudmonkey git commit: cloudmonkey: add a new output format CSV

Repository: cloudstack-cloudmonkey
Updated Branches:
  refs/heads/master a7a1db63a -> 4e71dd93b


cloudmonkey: add a new output format CSV

With CSV, it's possible to now export CloudStack data to an excel sheet for
example :)

Signed-off-by: Rohit Yadav <ro...@shapeblue.com>


Project: http://git-wip-us.apache.org/repos/asf/cloudstack-cloudmonkey/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack-cloudmonkey/commit/4e71dd93
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack-cloudmonkey/tree/4e71dd93
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack-cloudmonkey/diff/4e71dd93

Branch: refs/heads/master
Commit: 4e71dd93b3cde983c0068f3fa386c9504d3d1a02
Parents: a7a1db6
Author: Rohit Yadav <ro...@shapeblue.com>
Authored: Fri Jun 5 17:10:15 2015 +0200
Committer: Rohit Yadav <ro...@shapeblue.com>
Committed: Fri Jun 5 17:10:54 2015 +0200

----------------------------------------------------------------------
 cloudmonkey/cloudmonkey.py | 27 +++++++++++++++++++++++++++
 cloudmonkey/config.py      |  2 +-
 2 files changed, 28 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack-cloudmonkey/blob/4e71dd93/cloudmonkey/cloudmonkey.py
----------------------------------------------------------------------
diff --git a/cloudmonkey/cloudmonkey.py b/cloudmonkey/cloudmonkey.py
index 4760439..44baf58 100644
--- a/cloudmonkey/cloudmonkey.py
+++ b/cloudmonkey/cloudmonkey.py
@@ -22,6 +22,7 @@ try:
     import argparse
     import atexit
     import cmd
+    import csv
     import copy
     import json
     import logging
@@ -256,6 +257,28 @@ class CloudMonkeyShell(cmd.Cmd, object):
             xml = dicttoxml(result, attr_type=False, custom_root=custom_root)
             self.monkeyprint(parseString(xml).toprettyxml())
 
+        def print_result_csv(result):
+            if "count" in result:
+                result.pop("count")
+
+            if len(result.keys()) == 1:
+                item = result[result.keys()[0]]
+                if isinstance(item, list):
+                    result = item
+                elif isinstance(item, dict):
+                    result = [item]
+
+            if isinstance(result, list) and len(result) > 0:
+                if isinstance(result[0], dict):
+                    writer = csv.DictWriter(sys.stdout, result[0].keys())
+                    writer.writeheader()
+                    for item in result:
+                        writer.writerow(item)
+            elif isinstance(result, dict):
+                writer = csv.DictWriter(sys.stdout, result.keys())
+                writer.writeheader()
+                writer.writerow(result)
+
         def print_result_tabular(result):
             def print_table(printer, toprow):
                 if printer:
@@ -307,6 +330,10 @@ class CloudMonkeyShell(cmd.Cmd, object):
             print_result_xml(filtered_result)
             return
 
+        if self.display == "csv":
+            print_result_csv(filtered_result)
+            return
+
         if isinstance(filtered_result, dict):
             print_result_as_dict(filtered_result)
         elif isinstance(filtered_result, list):

http://git-wip-us.apache.org/repos/asf/cloudstack-cloudmonkey/blob/4e71dd93/cloudmonkey/config.py
----------------------------------------------------------------------
diff --git a/cloudmonkey/config.py b/cloudmonkey/config.py
index d92df25..4f55382 100644
--- a/cloudmonkey/config.py
+++ b/cloudmonkey/config.py
@@ -39,7 +39,7 @@ param_type = ['boolean', 'date', 'float', 'integer', 'short', 'list',
 iterable_type = ['set', 'list', 'object']
 
 # cloudmonkey display types
-display_types = ["json", "xml", "table", "default"]
+display_types = ["json", "xml", "csv", "table", "default"]
 
 config_dir = expanduser('~/.cloudmonkey')
 config_file = expanduser(config_dir + '/config')