summaryrefslogtreecommitdiff
path: root/src/leap/bitmask/cli/command.py
diff options
context:
space:
mode:
authorRuben Pollan <meskio@sindominio.net>2017-08-31 10:53:20 +0200
committerRuben Pollan <meskio@sindominio.net>2017-08-31 12:56:43 +0200
commit0db7a322a103b9886e74d9e017aa4a6298fdbe55 (patch)
treead13ccc656fb73fb23d06e307032188c0e50afe8 /src/leap/bitmask/cli/command.py
parent663c87c221c42e081b5947e298bc9f0541e6913a (diff)
[feat] make the cli default_printer more generic
Now the default_printer can handle other types than dict, like lists or strings.
Diffstat (limited to 'src/leap/bitmask/cli/command.py')
-rw-r--r--src/leap/bitmask/cli/command.py45
1 files changed, 24 insertions, 21 deletions
diff --git a/src/leap/bitmask/cli/command.py b/src/leap/bitmask/cli/command.py
index 72d1ebcf..fd32ab95 100644
--- a/src/leap/bitmask/cli/command.py
+++ b/src/leap/bitmask/cli/command.py
@@ -38,31 +38,34 @@ def _print_result(result):
print Fore.GREEN + '%s' % result + Fore.RESET
-def default_dict_printer(result):
-
- def pprint(value):
- if not isinstance(value, str):
- value = str(value)
- if value in ('OFF', 'OFFLINE', 'ABORTED', 'False'):
+def default_printer(result, key=None):
+ if isinstance(result, (str, unicode)):
+ if result in ('OFF', 'OFFLINE', 'ABORTED', 'False'):
color = Fore.RED
else:
color = Fore.GREEN
- print(Fore.RESET + key.ljust(10) + color + value + Fore.RESET)
-
- if not result:
- return
- for key, value in result.items():
- if isinstance(value, list):
- if value and isinstance(value[0], list):
- value = map(lambda l: ' '.join(l), value)
- for item in value:
- pprint('\t' + item)
- else:
- value = ' '.join(value)
- pprint(value)
+ print_str = ""
+ if key is not None:
+ print_str = Fore.RESET + key.ljust(10)
+ print_str += color + result + Fore.RESET
+ print(print_str)
+
+ elif isinstance(result, list):
+ if result and isinstance(result[0], list):
+ result = map(lambda l: ' '.join(l), result)
+ for item in result:
+ default_printer('\t' + item, key)
else:
- pprint(value)
+ result = ' '.join(result)
+ default_printer(result, key)
+
+ elif isinstance(result, dict):
+ for key, value in result.items():
+ default_printer(value, key)
+
+ else:
+ default_printer(str(result), key)
def print_status(status, depth=0):
@@ -129,7 +132,7 @@ class Command(object):
# and use the default printer
if args.command in self.commands:
self.data += [args.command] + raw_args[1:]
- return self._send(printer=default_dict_printer)
+ return self._send(printer=default_printer)
elif (args.command == 'execute' or
args.command.startswith('_') or