added lookup by lccn
[public/library.git] / librarian.py
1 #!/usr/bin/env python
2
3 import curses
4 import dbLayer as db
5 import browser
6 import form
7 import helpBar
8
9 import bookData
10
11
12 stdscr=0
13 hb=0
14
15 menu_commands = [(' q','quit')]
16
17 def menutest(s, l):
18     global stdscr
19     global hb
20     stdscr=s
21     curses.curs_set(0)
22     (rows,cols)=stdscr.getmaxyx()
23     bar = curses.newwin(1,cols-2,rows-1,1)
24     hb = helpBar.helpBar(bar)
25     hb.command=menu_commands
26     hb.refresh()
27     w = curses.newwin(10,40,(rows-10)/2, (cols-40)/2)
28
29     menu(w, l)
30     
31     curses.curs_set(1)
32
33 # item is a list of (string, callable) tuples
34 def menu(w, items):
35     w.keypad(1)
36     highlight=0
37     redrawMenu(w,items,highlight)
38
39     w.refresh()
40     ch=w.getch()
41     while (ch!=113 and ch!=27): # leave on q or ESC
42         if ch==curses.KEY_UP or ch==107 or ch==16:
43             if highlight!=0:
44                 w.chgat(highlight,0, 0)
45                 highlight -= 1
46                 while(items[highlight][0]==""):
47                     highlight -=1
48                 w.chgat(highlight,0, curses.A_REVERSE)
49         if ch==curses.KEY_DOWN or ch==106 or ch==14:
50             if highlight!=len(items)-1:
51                 w.chgat(highlight,0, 0)
52                 highlight += 1
53                 while(items[highlight][0]==""):
54                     highlight +=1
55                 w.chgat(highlight,0, curses.A_REVERSE)
56         if ch==curses.KEY_PPAGE:
57             w.chgat(highlight,0, 0)
58             highlight = 0
59             w.chgat(highlight,0, curses.A_REVERSE)
60         if ch==curses.KEY_NPAGE:
61             w.chgat(highlight,0, 0)
62             highlight = len(items)-1
63             w.chgat(highlight,0, curses.A_REVERSE)
64         if ch==114 or ch==10:
65             (s,f)=items[highlight]
66             f()
67             redrawMenu(w,items,highlight)
68         w.refresh()
69         ch = w.getch()
70
71 def redrawMenu(w,items,highlight):
72     i=0
73     for (mitem,fun) in items:
74         w.addstr(i,0, mitem)
75         i +=1
76     w.chgat(highlight, 0, curses.A_REVERSE)
77     w.refresh()
78     hb.commands=menu_commands
79     hb.refresh()
80
81
82 def addForm():
83     w=curses.newwin(1,1)
84     (my,mx)=stdscr.getmaxyx()
85     bf = form.bookForm(w,hb)
86     (r,c)=w.getmaxyx()
87     w.mvwin((my-r)/2,(mx-c)/2)
88     bf.lookup_isbn=bookData.openLibrary_isbn
89     bf.lookup_lccn=bookData.openLibrary_lccn
90     bf.caption='Add a Book'
91     bf.blabel = 'Add'
92     book = bf.eventLoop()
93     bf.clear()
94     if len(book)!=0:
95         db.addBook(book)
96
97 def updateMenu():
98     w=curses.newwin(1,50,10,10)
99     w.addstr("I will be used to update or modify book records")
100     w.refresh()
101
102 def trashMenu():
103     (my,mx)=stdscr.getmaxyx()
104     w=curses.newwin(20,80,(my-20)/2,(mx-80)/2)
105     b = browser.trashBrowser(w,hb)
106     b.refreshBooks()
107     b.eventLoop()
108     b.clear()
109
110 def browseMenu():
111     (my,mx)=stdscr.getmaxyx()
112     w=curses.newwin(20,80,(my-20)/2,(mx-80)/2)
113     b = browser.bookBrowser(w,hb)
114     b.refreshBooks()
115     b.eventLoop()
116     b.clear()
117
118 def catMenu():
119     (my,mx)=stdscr.getmaxyx()
120     w=curses.newwin(10,40,(my-10)/2,(mx-40)/2)
121     c = browser.categoryBrowser(w,hb)
122     c.refreshCategories()
123     c.sortByColumn('category')
124     c.eventLoop()
125     c.clear()
126
127
128 m = [("Browse Library", browseMenu),
129      ("Add Book", addForm),
130      ("Categories", catMenu),
131      ("View Trash", trashMenu),
132      ("",exit),
133      ("Exit", exit)]
134 curses.wrapper(menutest, m)