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()