We're basically functional now
[public/library.git] / librarian.py
1 #!/usr/bin/env python
2
3 import curses
4 import dbLayer as db
5 import browser
6 import bookForm
7 import bookData
8
9 stdscr=0
10
11 def menutest(s, l):
12     global stdscr
13     stdscr=s
14     curses.curs_set(0)
15     (rows,cols)=stdscr.getmaxyx()
16     w = curses.newwin(10,40,(rows-10)/2, (cols-40)/2)
17
18     menu(w, l)
19     
20     curses.curs_set(1)
21
22 # item is a list of (string, callable) tuples
23 def menu(w, items):
24     w.keypad(1)
25     highlight=0
26     redrawMenu(w,items,highlight)
27
28     w.refresh()
29     ch=w.getch()
30     while (ch!=113 and ch!=27): # leave on q or ESC
31         if ch==curses.KEY_UP:
32             if highlight!=0:
33                 w.chgat(highlight,0, 0)
34                 highlight -= 1
35                 while(items[highlight][0]==""):
36                     highlight -=1
37                 w.chgat(highlight,0, curses.A_REVERSE)
38         if ch==curses.KEY_DOWN:
39             if highlight!=len(items)-1:
40                 w.chgat(highlight,0, 0)
41                 highlight += 1
42                 while(items[highlight][0]==""):
43                     highlight +=1
44                 w.chgat(highlight,0, curses.A_REVERSE)
45         if ch==curses.KEY_PPAGE:
46             w.chgat(highlight,0, 0)
47             highlight = 0
48             w.chgat(highlight,0, curses.A_REVERSE)
49         if ch==curses.KEY_NPAGE:
50             w.chgat(highlight,0, 0)
51             highlight = len(items)-1
52             w.chgat(highlight,0, curses.A_REVERSE)
53         if ch==114 or ch==10:
54             (s,f)=items[highlight]
55             f()
56             redrawMenu(w,items,highlight)
57         w.refresh()
58         ch = w.getch()
59
60 def redrawMenu(w,items,highlight):
61     i=0
62     for (mitem,fun) in items:
63         w.addstr(i,0, mitem)
64         i +=1
65     w.chgat(highlight, 0, curses.A_REVERSE)
66     w.refresh()
67
68
69 def addForm():
70     book = {"title":"A Book of Tests", "pages":"123"}
71     w=curses.newwin(1,1,20,20)
72     bf = bookForm.bookForm(w)
73     bf.lookup=bookData.openLibrary
74     bf.caption='Add a Book'
75     bf.blabel = 'Add'
76     bf.updateEntries(book)
77     book = bf.eventLoop()
78     if len(book)!=0:
79         db.addBook(book)
80
81
82 def updateMenu():
83     w=curses.newwin(1,50,10,10)
84     w.addstr("I will be used to update or modify book records")
85     w.refresh()
86
87 def deleteMenu():
88     w=curses.newwin(1,50,10,10)
89     w.addstr("I will be used to delete book records")
90     w.refresh()
91
92 def browseMenu():
93     w=curses.newwin(10,80,20,20)
94     b = browser.browserWindow(w)
95     b.startBrowser()
96
97
98 m = [("Browse Library", browseMenu),
99      ("Add Book or other item", addForm),
100      ("Modify/Update record", updateMenu),
101      ("Remove book from catalogue", deleteMenu),
102      ("",exit),
103      ("Exit", exit)]
104 curses.wrapper(menutest, m)