summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexander Sulfrian <alexander@sulfrian.net>2016-01-26 01:54:17 +0100
committerAlexander Sulfrian <alexander@sulfrian.net>2016-02-02 04:22:17 +0100
commit88c4f3a3e2ac16006305e74ed03bd385aab75174 (patch)
tree182da7ee0841168d6b0a177dfe22f935280435cd
parent80031796e02eca59c6e6333b6e52d9503e0c6c5d (diff)
downloadweb-88c4f3a3e2ac16006305e74ed03bd385aab75174.tar.gz
web-88c4f3a3e2ac16006305e74ed03bd385aab75174.tar.bz2
web-88c4f3a3e2ac16006305e74ed03bd385aab75174.zip
manage: Add list-services command
-rw-r--r--accounts/utils/console.py61
-rwxr-xr-xmanage.py18
2 files changed, 78 insertions, 1 deletions
diff --git a/accounts/utils/console.py b/accounts/utils/console.py
new file mode 100644
index 0000000..f81bd36
--- /dev/null
+++ b/accounts/utils/console.py
@@ -0,0 +1,61 @@
+# -*- coding: utf-8 -*-
+
+class TablePrinter(object):
+
+ def __init__(self, headers=None, separator='|'):
+ self.headers = headers
+ self.separator = separator
+
+ self.format_string = ''
+ self.widths = list()
+
+ if headers is not None:
+ self._calulate_widths([headers])
+
+ def _calulate_widths(self, rows):
+ def _get_column_count(rows):
+ return min([len(row) for row in rows])
+
+ def _column_width(column, width):
+ widths = [len(elem) for elem in column]
+ widths.append(width)
+ return max(widths)
+
+ columns = _get_column_count(rows)
+ while len(self.widths) < columns:
+ self.widths.append(0)
+
+ self.widths = [_column_width(column, width)
+ for column, width
+ in zip(zip(*rows), self.widths)]
+ self._update_format_string()
+
+
+ def _update_format_string(self):
+ def _column_format(width):
+ return '%%-%ds' % width
+
+ sep = ' %s ' % self.separator
+ self.format_string = '%s %s %s' % (
+ self.separator,
+ sep.join(['%%-%ds' % width for width in self.widths]),
+ self.separator)
+
+ def output(self, rows):
+ self._calulate_widths(rows)
+
+ if self.headers is not None:
+ self._print_row(self.headers)
+ self._print_headline()
+
+ for row in rows:
+ self._print_row(row)
+
+ def _print_headline(self):
+ print('%s%s%s' % (
+ self.separator,
+ self.separator.join(['-' * (width + 2) for width in self.widths]),
+ self.separator))
+
+ def _print_row(self, row):
+ print(self.format_string % tuple(row))
diff --git a/manage.py b/manage.py
index 8149ff1..6e9a596 100755
--- a/manage.py
+++ b/manage.py
@@ -1,6 +1,20 @@
#!/usr/bin/env python
-from flask.ext.script import Manager, Server, Shell
+from flask.ext.script import Command, Manager, Server, Shell
+
from accounts import app
+from accounts.utils.console import TablePrinter
+
+
+class ListServices(Command):
+ """List the configured services."""
+
+ def __init__(self, app):
+ self.app = app
+
+ def run(self):
+ table = TablePrinter(['Name', 'URL'])
+ table.output([(service.name, service.url)
+ for service in self.app.all_services])
def main():
@@ -11,6 +25,8 @@ def main():
'debug', Server(host='::', use_debugger=True))
manager.add_command(
'shell', Shell(make_context=lambda: dict(app=app)))
+ manager.add_command(
+ 'list-services', ListServices(app))
manager.run()