display: New display mode based on jmespath for 5.3 version. (#44)
This add to cloudmonkey CLI the abilities to make a JMESPATH filter with following changes in version 5.3:
Add new argument 'query'.
Add new display type 'jmespath'.
Import jmespath module.
diff --git a/cloudmonkey/cloudmonkey.py b/cloudmonkey/cloudmonkey.py
index b106362..e8da457 100644
--- a/cloudmonkey/cloudmonkey.py
+++ b/cloudmonkey/cloudmonkey.py
@@ -31,6 +31,7 @@
import sys
import time
import types
+ import jmespath
from cachemaker import loadcache, savecache, monkeycache, splitverbsubject
from config import __version__, __description__, __projecturl__
@@ -234,7 +235,7 @@
else:
print output
- def print_result(self, result, result_filter=[]):
+ def print_result(self, result, result_filter=[], result_query=''):
if not result or len(result) == 0:
return
@@ -277,6 +278,21 @@
ensure_ascii=False,
separators=(',', ': ')))
+ def print_result_jmespath(result, result_query):
+ try:
+ expression = jmespath.compile(result_query)
+ except Exception as e:
+ raise ValueError("Bad value for --query: %s \n\n %s" % (result_query, str(e)))
+
+ try:
+ self.monkeyprint(json.dumps(expression.search(result),
+ sort_keys=True,
+ indent=2,
+ ensure_ascii=False,
+ separators=(',', ': ')))
+ except Exception as e:
+ raise ValueError("Bad formatted JSON for JMESPATH: %s \n\n %s" % (result, str(e)))
+
def print_result_xml(result):
custom_root = "CloudStack-%s" % self.profile.replace(" ", "_")
xml = dicttoxml(result, attr_type=False, custom_root=custom_root)
@@ -369,6 +385,10 @@
print_result_json(filtered_result)
return
+ if self.display == "jmespath":
+ print_result_jmespath(filtered_result, result_query)
+ return
+
if self.display == "xml":
print_result_xml(filtered_result)
return
@@ -457,6 +477,10 @@
x.partition("=")[2]],
args[1:])[x] for x in range(len(args) - 1))
+ field_query = []
+ if 'query' in args_dict:
+ field_query = args_dict.pop('query')
+
field_filter = []
if 'filter' in args_dict:
field_filter = filter(lambda x: x.strip() != '',
@@ -489,7 +513,7 @@
try:
responsekeys = filter(lambda x: 'response' in x, result.keys())
for responsekey in responsekeys:
- self.print_result(result[responsekey], field_filter)
+ self.print_result(result[responsekey], field_filter, field_query)
if apiname.startswith("list") and "id" not in args_dict:
self.update_param_cache(apiname, result)
except Exception as e:
diff --git a/cloudmonkey/config.py b/cloudmonkey/config.py
index 47235ef..ac6daa1 100644
--- a/cloudmonkey/config.py
+++ b/cloudmonkey/config.py
@@ -39,7 +39,7 @@
iterable_type = ['set', 'list', 'object']
# cloudmonkey display types
-display_types = ["json", "xml", "csv", "table", "default"]
+display_types = ["jmespath","json", "xml", "csv", "table", "default"]
config_dir = expanduser('~/.cloudmonkey')
config_file = expanduser(config_dir + '/config')
diff --git a/setup.py b/setup.py
index 4422328..f3912fb 100644
--- a/setup.py
+++ b/setup.py
@@ -29,6 +29,7 @@
from cloudmonkey import __project__, __projecturl__, __projectemail__
requires = [
+ 'jmespath',
'Pygments>=1.5',
'argcomplete',
'dicttoxml',