La til placement.py, som inneholder funksjoner for å skrive ut ASCII-figur over bokhyller.
This commit is contained in:
parent
aff4e4c2a2
commit
26416a5aff
|
@ -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()
|
||||||
|
|
|
@ -9,6 +9,7 @@ from util import *
|
||||||
import getopt
|
import getopt
|
||||||
import sys
|
import sys
|
||||||
import search
|
import search
|
||||||
|
import placement
|
||||||
|
|
||||||
def show_book_or_person(ids, commit_format=False, tmp_file=False):
|
def show_book_or_person(ids, commit_format=False, tmp_file=False):
|
||||||
for id in ids:
|
for id in ids:
|
||||||
|
@ -86,7 +87,11 @@ commands = { 'show':
|
||||||
'edit':
|
'edit':
|
||||||
{ 'args': [('ids', (1,None))],
|
{ 'args': [('ids', (1,None))],
|
||||||
'options': [],
|
'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':
|
flags = { 'commit_format':
|
||||||
|
|
Reference in New Issue