Commit a70ab87e authored by Marco Mariani's avatar Marco Mariani

use prettytable

parent 472421c4
...@@ -3,12 +3,12 @@ ...@@ -3,12 +3,12 @@
import collections import collections
import logging import logging
from slapos.cli.config import ConfigCommand
from slapos.proxy import ProxyConfig
import lxml.etree import lxml.etree
import prettytable
import sqlite3 import sqlite3
from slapos.cli.config import ConfigCommand
from slapos.proxy import ProxyConfig
from slapos.proxy.db_version import DB_VERSION from slapos.proxy.db_version import DB_VERSION
...@@ -53,33 +53,31 @@ tbl_partition = 'partition' + DB_VERSION ...@@ -53,33 +53,31 @@ tbl_partition = 'partition' + DB_VERSION
tbl_partition_network = 'partition_network' + DB_VERSION tbl_partition_network = 'partition_network' + DB_VERSION
tbl_slave = 'slave' + DB_VERSION tbl_slave = 'slave' + DB_VERSION
null_str = u"-"
def coalesce(*seq):
el = None
for el in seq:
if el is not None:
return el
return el
def print_table(qry, tablename, skip=None): def print_table(qry, tablename, skip=None):
if skip is None: if skip is None:
skip = set() skip = set()
columns = [c[0] for c in qry.description if c[0] not in skip] columns = [c[0] for c in qry.description if c[0] not in skip]
rows = [] rows = []
for row in qry.fetchall(): for row in qry.fetchall():
line = {} rows.append([coalesce(row[col], '-') for col in columns])
for col in columns:
val = row[col]
if val is None:
val = null_str
line[col] = val.strip()
rows.append(line)
max_width = {col: len(col) for col in columns}
for row in rows:
for col in columns:
val = row[col]
max_width[col] = max(max_width[col], len(val) if val else 0)
hdr = [col.center(max_width[col]) for col in columns] pt = prettytable.PrettyTable(columns)
# https://code.google.com/p/prettytable/wiki/Tutorial
print for row in rows:
pt.add_row(row)
if rows: if rows:
print 'table %s:' % tablename, print 'table %s:' % tablename,
...@@ -92,12 +90,8 @@ def print_table(qry, tablename, skip=None): ...@@ -92,12 +90,8 @@ def print_table(qry, tablename, skip=None):
else: else:
print print
print ' | '.join(hdr) print pt.get_string(border=True, padding_width=0, vrules=prettytable.NONE)
print '-+-'.join('-' * len(h) for h in hdr) print
for row in rows:
cells = [row[col].ljust(max_width[col]) for col in columns]
print ' | '.join(cells)
def print_params(conn): def print_params(conn):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment