library/librarian.py

143 lines
3.4 KiB
Python
Raw Normal View History

2013-10-21 19:38:21 -04:00
#!/usr/bin/env python3
2012-03-10 02:56:31 -05:00
import curses
import db_layer as db
2012-03-19 22:50:09 -04:00
import browser
2013-10-25 17:25:07 -04:00
import form
import help_bar as helpBar
2012-03-21 17:18:20 -04:00
import book_data
2012-03-10 02:56:31 -05:00
2012-03-21 17:18:20 -04:00
2012-03-15 15:21:42 -04:00
stdscr=0
2012-03-21 17:18:20 -04:00
hb=0
menu_commands = [(' q','quit')]
2012-03-15 15:21:42 -04:00
def menutest(s, l):
global stdscr
2012-03-21 17:18:20 -04:00
global hb
2012-03-15 15:21:42 -04:00
stdscr=s
2012-03-10 02:56:31 -05:00
curses.curs_set(0)
(rows,cols)=stdscr.getmaxyx()
2012-03-21 17:18:20 -04:00
bar = curses.newwin(1,cols-2,rows-1,1)
hb = helpBar.helpBar(bar)
hb.command=menu_commands
hb.refresh()
2013-10-21 19:38:21 -04:00
w = curses.newwin(10,40,(rows-10)//2, (cols-40)//2)
2012-03-10 02:56:31 -05:00
menu(w, l)
curses.curs_set(1)
# item is a list of (string, callable) tuples
def menu(w, items):
w.keypad(1)
highlight=0
2012-03-15 15:21:42 -04:00
redrawMenu(w,items,highlight)
2012-03-10 02:56:31 -05:00
w.refresh()
ch=w.getch()
while (ch!=113 and ch!=27): # leave on q or ESC
2012-03-24 12:12:12 -04:00
if ch==curses.KEY_UP or ch==107 or ch==16:
2012-03-10 02:56:31 -05:00
if highlight!=0:
w.chgat(highlight,0, 0)
highlight -= 1
while(items[highlight][0]==""):
highlight -=1
2012-03-10 02:56:31 -05:00
w.chgat(highlight,0, curses.A_REVERSE)
2012-03-24 12:12:12 -04:00
if ch==curses.KEY_DOWN or ch==106 or ch==14:
2012-03-10 02:56:31 -05:00
if highlight!=len(items)-1:
w.chgat(highlight,0, 0)
highlight += 1
while(items[highlight][0]==""):
highlight +=1
2012-03-10 02:56:31 -05:00
w.chgat(highlight,0, curses.A_REVERSE)
if ch==curses.KEY_PPAGE:
w.chgat(highlight,0, 0)
highlight = 0
w.chgat(highlight,0, curses.A_REVERSE)
if ch==curses.KEY_NPAGE:
w.chgat(highlight,0, 0)
highlight = len(items)-1
w.chgat(highlight,0, curses.A_REVERSE)
2012-03-10 02:56:31 -05:00
if ch==114 or ch==10:
(s,f)=items[highlight]
2012-03-14 17:44:13 -04:00
f()
2012-03-15 15:21:42 -04:00
redrawMenu(w,items,highlight)
2012-03-10 02:56:31 -05:00
w.refresh()
ch = w.getch()
2012-03-15 15:21:42 -04:00
def redrawMenu(w,items,highlight):
i=0
for (mitem,fun) in items:
w.addstr(i,0, mitem)
i +=1
w.chgat(highlight, 0, curses.A_REVERSE)
w.refresh()
2012-03-21 17:18:20 -04:00
hb.commands=menu_commands
hb.refresh()
2012-03-15 15:21:42 -04:00
def addForm():
2012-03-21 17:18:20 -04:00
w=curses.newwin(1,1)
(my,mx)=stdscr.getmaxyx()
bf = form.BookForm(w,hb,width=mx-20)
2012-03-21 17:18:20 -04:00
(r,c)=w.getmaxyx()
2013-10-21 19:38:21 -04:00
w.mvwin((my-r)//2,(mx-c)//2)
bf.lookup_isbn=book_data.openLibrary_isbn
bf.lookup_lccn=book_data.openLibrary_lccn
2012-03-20 13:12:33 -04:00
bf.caption='Add a Book'
bf.blabel = 'Add'
book = bf.event_loop()
bf.clear()
2012-03-17 02:39:58 -04:00
if len(book)!=0:
db.addBook(book)
2012-03-15 15:21:42 -04:00
2012-03-14 17:44:13 -04:00
def updateMenu():
w=curses.newwin(1,50,10,10)
2012-03-10 03:13:17 -05:00
w.addstr("I will be used to update or modify book records")
w.refresh()
2012-03-30 20:20:38 -04:00
def trashMenu():
(my,mx)=stdscr.getmaxyx()
w=curses.newwin(3,5)
b = browser.trashBrowser(w,hb,my-10,mx-10)
(r,c) = w.getmaxyx()
w.mvwin((my-r)//2 -2, (mx-c)//2)
2012-03-30 20:20:38 -04:00
b.refreshBooks()
b.eventLoop()
b.clear()
2012-03-10 02:56:31 -05:00
2012-03-14 17:44:13 -04:00
def browseMenu():
2012-03-21 17:18:20 -04:00
(my,mx)=stdscr.getmaxyx()
w=curses.newwin(3,5)
b = browser.bookBrowser(w,hb, my-10, mx-10)
(r,c) = w.getmaxyx()
w.mvwin((my-r)//2 -2, (mx-c)//2)
2012-03-24 12:12:12 -04:00
b.refreshBooks()
b.eventLoop()
b.clear()
def catMenu():
(my,mx)=stdscr.getmaxyx()
w=curses.newwin(3,5)
cat = browser.categoryBrowser(w,hb, 10,40)
(r,c) = w.getmaxyx()
w.mvwin((my-r)//2 -2, (mx-c)//2)
cat.refreshCategories()
cat.sortByColumn('category')
cat.eventLoop()
cat.clear()
2012-03-19 22:50:09 -04:00
2012-03-10 02:56:31 -05:00
2012-03-10 03:13:17 -05:00
m = [("Browse Library", browseMenu),
("Add Book", addForm),
("Categories", catMenu),
2012-03-30 20:20:38 -04:00
("View Trash", trashMenu),
("",exit),
2012-03-15 16:07:43 -04:00
("Exit", exit)]
2012-03-10 02:56:31 -05:00
curses.wrapper(menutest, m)