# -*- coding: utf-8 -*- from flask.ext import script class Command(script.Command): """ This changes the Command class from Flask-Script in such way, that url_for will generate correct urls. """ def __call__(self, app=None, *args, **kwargs): base_url = '%s://%s%s' % ( app.config.get('PREFERRED_URL_SCHEME') or 'http', app.config.get('SERVER_NAME') or 'localhost', app.config.get('APPLICATION_ROOT') or '/') with app.test_request_context(base_url=base_url): return self.run(*args, **kwargs) 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))