Categorizing and uncategorizing based on selection
[public/library.git] / librarian
1 #!/usr/bin/env python3
2
3 import curses
4 import library.database as db
5 import library.interface.browser as browser
6 import library.interface.form as form
7 import library.interface.help_bar as helpBar
8 import library.interface.checkout as co
9
10 from  library import book_data
11
12
13
14 stdscr=0
15 hb=0
16
17 menu_commands = [(' q','quit')]
18
19 def menutest(s, l):
20     global stdscr
21     global hb
22     stdscr=s
23     curses.curs_set(0)
24     (rows,cols)=stdscr.getmaxyx()
25     # set the default for the browser windows
26     browser.browserWindow._default_height = rows-10
27     browser.browserWindow._default_width = cols-10
28     bar = curses.newwin(1,cols-2,rows-1,1)
29     hb = helpBar.helpBar(bar)
30     hb.command=menu_commands
31     hb.refresh()
32     w = curses.newwin(15,40,(rows-10)//2, (cols-40)//2)
33
34     try:
35         menu(w, l)
36     except SystemExit: pass
37     except:
38         text = """An unexpected error occured.
39 You can contact the librarian (librarian@csclub.uwaterloo.ca),
40 but given the history of the library system, it seems unlikely
41 that somebody will be around to care.
42 The program will now quit."""
43         form.error_form(text, stdscr, hb)
44         raise
45
46     curses.curs_set(1)
47
48 # item is a list of (string, callable) tuples
49 def menu(w, items):
50     w.keypad(1)
51     highlight=0
52     redrawMenu(w,items,highlight)
53
54     w.refresh()
55     ch=w.getch()
56     while (ch!=113 and ch!=27): # leave on q or ESC
57         if ch==curses.KEY_UP or ch==107 or ch==16:
58             if highlight!=0:
59                 w.chgat(highlight,0, 0)
60                 highlight -= 1
61                 while(items[highlight][0]==""):
62                     highlight -=1
63                 w.chgat(highlight,0, curses.A_REVERSE)
64         if ch==curses.KEY_DOWN or ch==106 or ch==14:
65             if highlight!=len(items)-1:
66                 w.chgat(highlight,0, 0)
67                 highlight += 1
68                 while(items[highlight][0]==""):
69                     highlight +=1
70                 w.chgat(highlight,0, curses.A_REVERSE)
71         if ch==curses.KEY_PPAGE:
72             w.chgat(highlight,0, 0)
73             highlight = 0
74             w.chgat(highlight,0, curses.A_REVERSE)
75         if ch==curses.KEY_NPAGE:
76             w.chgat(highlight,0, 0)
77             highlight = len(items)-1
78             w.chgat(highlight,0, curses.A_REVERSE)
79         if ch==114 or ch==10:
80             (s,f)=items[highlight]
81             f()
82             redrawMenu(w,items,highlight)
83         w.refresh()
84         ch = w.getch()
85
86 def redrawMenu(w,items,highlight):
87     i=0
88     for (mitem,fun) in items:
89         w.addstr(i,0, mitem)
90         i +=1
91     w.chgat(highlight, 0, curses.A_REVERSE)
92     w.refresh()
93     hb.commands=menu_commands
94     hb.refresh()
95
96
97 def addForm():
98     w=curses.newwin(1,1)
99     (my,mx)=stdscr.getmaxyx()
100     bf = form.BookForm(w,hb,width=mx-20)
101     (r,c)=w.getmaxyx()
102     w.mvwin((my-r)//2,(mx-c)//2)
103     bf.lookup_isbn=book_data.openLibrary_isbn
104     bf.lookup_lccn=book_data.openLibrary_lccn
105     bf.caption='Add a Book'
106     bf.blabel = 'Add'
107     book = bf.event_loop()
108     bf.clear()
109     if len(book)!=0:
110         db.addBook(book)
111
112 def browseMenu():
113     w=curses.newwin(3,5)
114     b = browser.bookBrowser(w,hb)
115     (r,c) = w.getmaxyx()
116     (my,mx)=stdscr.getmaxyx()
117     w.mvwin((my-r)//2 -2, (mx-c)//2)
118     b.refreshBooks()
119     b.eventLoop()
120     b.clear()
121
122 def trashMenu():
123     w=curses.newwin(3,5)
124     b = browser.trashBrowser(w,hb)
125     (r,c) = w.getmaxyx()
126     (my,mx)=stdscr.getmaxyx()
127     w.mvwin((my-r)//2 -2, (mx-c)//2)
128     b.refreshBooks()
129     b.eventLoop()
130     b.clear()
131
132 def uncategorizedMenu():
133     w=curses.newwin(3,5)
134     b = browser.bookBrowser(w,hb)
135     (r,c) = w.getmaxyx()
136     (my,mx)=stdscr.getmaxyx()
137     w.mvwin((my-r)//2 -2, (mx-c)//2)
138     b.refreshBooksUncategorized()
139     b.eventLoop()
140     b.clear()
141
142 def checkedout_menu():
143     w=curses.newwin(3,5)
144     b = browser.bookBrowser(w,hb)
145     (r,c) = w.getmaxyx()
146     (my,mx)=stdscr.getmaxyx()
147     w.mvwin((my-r)//2 -2, (mx-c)//2)
148     b.refreshBooksCheckedout()
149     b.columnDefs = [("id",0,3),
150                     ("uwid",0,8),
151                     ("date",0,10),
152                     ("title",100,None)]
153     b.calcColWidths()
154     b.eventLoop()
155     b.clear()
156
157 def onshelf_menu():
158     w=curses.newwin(3,5)
159     b = browser.bookBrowser(w,hb)
160     (r,c) = w.getmaxyx()
161     (my,mx)=stdscr.getmaxyx()
162     w.mvwin((my-r)//2 -2, (mx-c)//2)
163     b.refreshBooksOnshelf()
164     b.eventLoop()
165     b.clear()
166
167 def co_menu():
168     w=curses.newwin(1,1)
169     (my,mx)=stdscr.getmaxyx()
170     co.checkout_procedure(w,hb,my//2,mx//2,mx)
171
172 def return_menu():
173     w=curses.newwin(1,1)
174     (my,mx)=stdscr.getmaxyx()
175     co.return_procedure(w,hb,my//2,mx//2,mx)
176
177 def catMenu():
178     (my,mx)=stdscr.getmaxyx()
179     w=curses.newwin(3,5)
180     cat = browser.categoryBrowser(w,hb)
181     (r,c) = w.getmaxyx()
182     w.mvwin((my-r)//2 -2, (mx-c)//2)
183     cat.refreshCategories()
184     cat.sortByColumn('category')
185     cat.eventLoop()
186     cat.clear()
187
188
189 if __name__ == "__main__":
190     db.initializeDatabase()
191     m = [("Browse Library", browseMenu),
192          ("Add Book", addForm),
193          ("Categories", catMenu),
194          ("View Trash", trashMenu),
195          ("View Uncategorized Books", uncategorizedMenu),
196          ("",exit),
197          ("Check Out a Book", co_menu),
198          ("Return a Book", return_menu),
199          ("",exit),
200          ("View Checked Out Books", checkedout_menu),
201          ("View On Shelf Books", onshelf_menu),
202          ("",exit),
203          ("Exit", exit)]
204     curses.wrapper(menutest, m)
205
206