From 26416a5affd852884a2921f0d490bf99f953ad6a Mon Sep 17 00:00:00 2001 From: jabirali Date: Sat, 5 Mar 2011 21:58:18 +0000 Subject: [PATCH] =?UTF-8?q?La=20til=20placement.py,=20som=20inneholder=20f?= =?UTF-8?q?unksjoner=20for=20=C3=A5=20skrive=20ut=20ASCII-figur=20over=20b?= =?UTF-8?q?okhyller.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/placement.py | 63 +++++++++++++++++++++++++++++++++++++++++++++ python/worblehat.py | 7 ++++- 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 python/placement.py diff --git a/python/placement.py b/python/placement.py new file mode 100644 index 0000000..a229624 --- /dev/null +++ b/python/placement.py @@ -0,0 +1,63 @@ + + +def print_shelf(shelf, 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 = "" + + 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): + if shelfname: + print_shelf(shelfname, title=shelfname) + else: + print_all_shelves() + diff --git a/python/worblehat.py b/python/worblehat.py index b6c7bef..ccf507c 100755 --- a/python/worblehat.py +++ b/python/worblehat.py @@ -9,6 +9,7 @@ from util import * import getopt import sys import search +import placement def show_book_or_person(ids, commit_format=False, tmp_file=False): for id in ids: @@ -86,7 +87,11 @@ commands = { 'show': 'edit': { 'args': [('ids', (1,None))], 'options': [], - 'fun': edit_book_or_person } + 'fun': edit_book_or_person }, + 'map': + { 'args': [('shelfname', (0,1))], + 'options': [], + 'fun': placement.print_shelf_cmd } } flags = { 'commit_format':