summaryrefslogtreecommitdiff
path: root/src/leap/bitmask
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
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')
-rwxr-xr-xsrc/leap/bitmask/cli/bitmask_cli.py6
-rw-r--r--src/leap/bitmask/cli/command.py45
-rw-r--r--src/leap/bitmask/cli/mail.py6
-rw-r--r--src/leap/bitmask/cli/provider.py2
-rw-r--r--src/leap/bitmask/cli/user.py8
-rw-r--r--src/leap/bitmask/cli/vpn.py6
6 files changed, 38 insertions, 35 deletions
diff --git a/src/leap/bitmask/cli/bitmask_cli.py b/src/leap/bitmask/cli/bitmask_cli.py
index 2483d476..eafdb1e4 100755
--- a/src/leap/bitmask/cli/bitmask_cli.py
+++ b/src/leap/bitmask/cli/bitmask_cli.py
@@ -131,11 +131,11 @@ OPTIONAL ARGUMENTS:
def stop(self, raw_args):
self.data = ['core', 'stop']
self.cfg.set('bonafide', 'active', "")
- return self._send(printer=command.default_dict_printer)
+ return self._send(printer=command.default_printer)
def stats(self, raw_args):
self.data = ['core', 'stats']
- return self._send(printer=command.default_dict_printer)
+ return self._send(printer=command.default_printer)
@defer.inlineCallbacks
@@ -166,7 +166,7 @@ def execute():
yield cli.execute(args)
cmdline = ' '.join(sys.argv)
if 'ctl start' in cmdline or 'ctl restart' in cmdline:
- command.default_dict_printer({'start': 'ok'})
+ command.default_printer({'start': 'ok'})
except Exception as e:
print(Fore.RED + "ERROR: " + Fore.RESET +
"%s" % str(e))
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
diff --git a/src/leap/bitmask/cli/mail.py b/src/leap/bitmask/cli/mail.py
index 933f72a9..0e8b6584 100644
--- a/src/leap/bitmask/cli/mail.py
+++ b/src/leap/bitmask/cli/mail.py
@@ -73,7 +73,7 @@ SUBCOMMANDS:
uid = self.cfg.get('bonafide', 'active', default=None)
self.data += ['get_token', uid]
- return self._send(command.default_dict_printer)
+ return self._send(command.default_printer)
def add_msg(self, raw_args):
parser = argparse.ArgumentParser(
@@ -101,7 +101,7 @@ SUBCOMMANDS:
self.data += ['add_msg', userid, mailbox, rawmsg]
- return self._send(command.default_dict_printer)
+ return self._send(command.default_printer)
def mixnet_status(self, raw_args):
parser = argparse.ArgumentParser(
@@ -120,4 +120,4 @@ SUBCOMMANDS:
userid = self.cfg.get('bonafide', 'active', default=None)
self.data += ['mixnet_status', userid, subargs.address[0]]
- return self._send(command.default_dict_printer)
+ return self._send(command.default_printer)
diff --git a/src/leap/bitmask/cli/provider.py b/src/leap/bitmask/cli/provider.py
index 7c30df37..fb3e53e0 100644
--- a/src/leap/bitmask/cli/provider.py
+++ b/src/leap/bitmask/cli/provider.py
@@ -56,7 +56,7 @@ SUBCOMMANDS:
subargs = parser.parse_args(raw_args)
self.data += ['read', subargs.domain[0]]
- return self._send(printer=command.default_dict_printer)
+ return self._send(printer=command.default_printer)
def delete(self, raw_args):
parser = argparse.ArgumentParser(
diff --git a/src/leap/bitmask/cli/user.py b/src/leap/bitmask/cli/user.py
index 9af31b3b..deedae01 100644
--- a/src/leap/bitmask/cli/user.py
+++ b/src/leap/bitmask/cli/user.py
@@ -78,7 +78,7 @@ SUBCOMMANDS:
passwd = self._getpass_twice()
self.data += ['create', username, passwd,
subargs.invite, 'True']
- return self._send(printer=command.default_dict_printer)
+ return self._send(printer=command.default_printer)
def auth(self, raw_args):
passwd = None
@@ -92,7 +92,7 @@ SUBCOMMANDS:
passwd = getpass.getpass()
self.data += ['authenticate', username, passwd, 'True']
self.cfg.set('bonafide', 'active', username)
- return self._send(printer=command.default_dict_printer)
+ return self._send(printer=command.default_printer)
def logout(self, raw_args):
username = self._username(raw_args)
@@ -101,7 +101,7 @@ SUBCOMMANDS:
active = self.cfg.get('bonafide', 'active', default=None)
if active == username:
self.cfg.set('bonafide', 'active', "")
- return self._send(printer=command.default_dict_printer)
+ return self._send(printer=command.default_printer)
def list(self, raw_args):
self.data += ['list']
@@ -112,7 +112,7 @@ SUBCOMMANDS:
current_passwd = getpass.getpass('Current password: ')
new_passwd = self._getpass_twice('New password: ')
self.data += ['update', username, current_passwd, new_passwd]
- return self._send(printer=command.default_dict_printer)
+ return self._send(printer=command.default_printer)
def active(self, raw_args):
username = self.cfg.get('bonafide', 'active', default='')
diff --git a/src/leap/bitmask/cli/vpn.py b/src/leap/bitmask/cli/vpn.py
index bcc2a448..cf4cf169 100644
--- a/src/leap/bitmask/cli/vpn.py
+++ b/src/leap/bitmask/cli/vpn.py
@@ -69,7 +69,7 @@ SUBCOMMANDS:
self.data += ['start', provider]
- return self._send(command.default_dict_printer)
+ return self._send(command.default_printer)
def status(self, raw_args):
self.data += ['status']
@@ -95,7 +95,7 @@ SUBCOMMANDS:
self.data += ['check', provider]
- return self._send(command.default_dict_printer)
+ return self._send(command.default_printer)
def list(self, raw_args):
self.data += ['list']
@@ -116,7 +116,7 @@ SUBCOMMANDS:
uid = self.cfg.get('bonafide', 'active', default=None)
self.data += ['get_cert', uid]
- return self._send(command.default_dict_printer)
+ return self._send(command.default_printer)
def location_printer(result):