Projects/worblehat-old
Projects
/
worblehat-old
Archived
12
0
Fork 0
This repository has been archived on 2024-07-04. You can view files and clone it, but cannot push or open issues or pull requests.
worblehat-old/python/placement.py

67 lines
1.7 KiB
Python
Raw Permalink Normal View History

2011-03-05 23:08:34 +01:00
from web.library.models import Placement
def print_shelf(shelf, category=None, title=None):
# 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 = ""
if category and content.upper() == category.upper():
content = "\x1B[31;1m" + content.upper() + "\x1B[0m"
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)
def print_shelf_cmd(shelfname, category):
if shelfname:
print_shelf(Placement.shelf_as_list(shelfname), category=category, title=shelfname)
else:
print_all_shelves()