2011-03-05 23:08:34 +01:00
|
|
|
from web.library.models import Placement
|
2011-03-05 22:58:18 +01:00
|
|
|
|
2011-03-05 23:57:27 +01:00
|
|
|
def print_shelf(shelf, category=None, title=None):
|
2011-03-05 22:58:18 +01:00
|
|
|
# Discover the dimensions of the shelves
|
|
|
|
cols = len(shelf)
|
|
|
|
rows, cell_height, cell_width = 0,0,0
|
|
|
|
for col in shelf:
|
|
|
|
rows = max(rows, len(col))
|
|
|
|
for cell in col:
|
|
|
|
if len(cell) > 0:
|
|
|
|
cell_height = max(cell_height, len(cell))
|
|
|
|
cell_width = max(cell_width, max([len(str(item)) for item in cell]))
|
|
|
|
|
|
|
|
# Define format strings used to generate output
|
|
|
|
title_width = (cell_width+2)*cols
|
|
|
|
title_pre = " "*( title_width/2 )
|
|
|
|
title_post = " "*( title_width - title_width/2 )
|
|
|
|
|
|
|
|
format_line = (("+-" + "-"*(cell_width+2))*cols + "+\n")
|
|
|
|
format_title = ("|" + title_pre + "Hylle %s" + title_post + "|\n")
|
|
|
|
format_cell = "| %s%s "
|
|
|
|
format_endl = "|\n"
|
|
|
|
|
|
|
|
# Pretty-format the shelf in an ugly way
|
|
|
|
output = []
|
|
|
|
output.append(format_line)
|
|
|
|
|
|
|
|
if title:
|
|
|
|
output.append( format_title % title )
|
|
|
|
output.append( format_line )
|
|
|
|
|
|
|
|
for j in xrange(0, rows):
|
|
|
|
for k in xrange(0, cell_height):
|
|
|
|
for i in xrange(0, cols):
|
|
|
|
try:
|
|
|
|
content = str(shelf[i][j][k])
|
|
|
|
|
|
|
|
except:
|
|
|
|
content = ""
|
2011-03-05 23:57:27 +01:00
|
|
|
|
|
|
|
if category and content.upper() == category.upper():
|
|
|
|
content = "\x1B[31;1m" + content.upper() + "\x1B[0m"
|
2011-03-05 22:58:18 +01:00
|
|
|
|
|
|
|
spacing = " "*(cell_width-len(content))
|
|
|
|
output.append( format_cell %(content, spacing) )
|
|
|
|
|
|
|
|
output.append(format_endl)
|
|
|
|
|
|
|
|
output.append(format_line)
|
|
|
|
|
|
|
|
# Send the result to stdout
|
|
|
|
print "".join(output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def print_all_shelves():
|
|
|
|
for shelfname in Placement.get_all_shelves():
|
|
|
|
print_shelf(Placement.shelf_as_list(shelfname), title=shelfname)
|
|
|
|
|
|
|
|
|
2011-03-05 23:57:27 +01:00
|
|
|
def print_shelf_cmd(shelfname, category):
|
2011-03-05 22:58:18 +01:00
|
|
|
if shelfname:
|
2011-03-05 23:57:27 +01:00
|
|
|
print_shelf(Placement.shelf_as_list(shelfname), category=category, title=shelfname)
|
2011-03-05 22:58:18 +01:00
|
|
|
else:
|
|
|
|
print_all_shelves()
|
|
|
|
|