diff --git a/tools/cli/cloudmonkey/cloudmonkey.py b/tools/cli/cloudmonkey/cloudmonkey.py index 94006c9577a..842af268ee7 100644 --- a/tools/cli/cloudmonkey/cloudmonkey.py +++ b/tools/cli/cloudmonkey/cloudmonkey.py @@ -27,6 +27,7 @@ try: import shlex import sys import types + import copy from cachemaker import loadcache, savecache, monkeycache, splitverbsubject from config import __version__, __description__, __projecturl__ @@ -162,6 +163,44 @@ class CloudMonkeyShell(cmd.Cmd, object): self.monkeyprint(printer) return PrettyTable(toprow) + # method: print_result_json( result, result_filter ) + # parameters: result - raw results from the API call + # result_filter - filterset + # description: prints result as a json object + def print_result_json(result, result_filter=None): + tfilter = {} # temp var to hold a dict of the filters + tresult = copy.deepcopy(result) # dupe the result to filter + if result_filter != None: + for res in result_filter: + tfilter[ res ] = 1 + myresults = {} + for okey, oval in result.iteritems(): + if isinstance( oval, dict ): + for tkey in x: + if tkey not in tfilter: + try: + del( tresult[okey][x][tkey] ) + except: + pass + elif isinstance( oval, list ): + for x in range( len( oval ) ): + if isinstance( oval[x], dict ): + for tkey in oval[x]: + if tkey not in tfilter: + try: + del( tresult[okey][x][tkey] ) + except: + pass + else: + try: + del( tresult[ okey ][ x ] ) + except: + pass + print json.dumps(tresult, + sort_keys=True, + indent=2, + separators=(',', ': ')) + def print_result_tabular(result, result_filter=None): toprow = None printer = None @@ -183,6 +222,12 @@ class CloudMonkeyShell(cmd.Cmd, object): self.monkeyprint(printer) def print_result_as_dict(result, result_filter=None): + + # tabularize overrides self.display + if self.display == "json" and not self.tabularize == "true": + print_result_json(result, result_filter) + return + for key in sorted(result.keys(), key=lambda x: x not in ['id', 'count', 'name'] and x): if not (isinstance(result[key], list) or @@ -195,7 +240,7 @@ class CloudMonkeyShell(cmd.Cmd, object): def print_result_as_list(result, result_filter=None): for node in result: # Tabular print if it's a list of dict and tabularize is true - if isinstance(node, dict) and self.tabularize == 'true': + if isinstance(node, dict) and (self.display == 'tabularize' or self.tabularize == 'true'): print_result_tabular(result, result_filter) break self.print_result(node) @@ -318,7 +363,7 @@ class CloudMonkeyShell(cmd.Cmd, object): autocompletions = uuids search_string = value - if self.tabularize == "true" and subject != "": + if (self.display == "tabularize" or self.display == "json" or self.tabularize == "true") and subject != "": autocompletions.append("filter=") return [s for s in autocompletions if s.startswith(search_string)] @@ -459,7 +504,6 @@ class CloudMonkeyShell(cmd.Cmd, object): self.monkeyprint("Bye!") return self.do_EOF(args) - class MonkeyParser(OptionParser): def format_help(self, formatter=None): if formatter is None: @@ -473,7 +517,6 @@ class MonkeyParser(OptionParser): result.append("\nTry cloudmonkey [help|?]\n") return "".join(result) - def main(): parser = MonkeyParser() parser.add_option("-c", "--config-file", diff --git a/tools/cli/cloudmonkey/config.py b/tools/cli/cloudmonkey/config.py index 75605df93b7..b21ae4c608d 100644 --- a/tools/cli/cloudmonkey/config.py +++ b/tools/cli/cloudmonkey/config.py @@ -56,7 +56,8 @@ config_fields['core']['log_file'] = expanduser(config_dir + '/log') # ui config_fields['ui']['color'] = 'true' config_fields['ui']['prompt'] = '> ' -config_fields['ui']['tabularize'] = 'false' +config_fields['ui']['tabularize'] = 'false' # deprecate - REMOVE +config_fields['ui']['display'] = 'default' # default display mechanism # server config_fields['server']['host'] = 'localhost' @@ -111,9 +112,19 @@ def read_config(get_attr, set_attr, config_file): for section in config_fields.keys(): for key in config_fields[section].keys(): try: + if( key == "tabularize" ): # this key is deprecated + print "\ntabularize config parameter is deprecated:", + print "please switch to display =", + print "[default,json,tabularize]\n" set_attr(key, config.get(section, key)) except Exception: - missing_keys.append(key) + if( key == "tabularize" ): # this key is deprecated + set_attr( key, "false" ) # set default + elif( key == "display" ): # this key is deprecated + config = write_config(get_attr, config_file, True) + set_attr( key, "default" ) # set default + else: + missing_keys.append(key) if len(missing_keys) > 0: print "Please fix `%s` in %s" % (', '.join(missing_keys), config_file)