significant improvements to database (adding/removing books, categories, etc.)
[public/library.git] / browser.py
1 import sys
2 import curses
3 import dbLayer as db
4 from form import bookForm,categoryForm
5
6 class browserWindow:
7     hl=0
8     topline = 0
9     entries = []
10     selected = []
11     commands = [(' /', 'search'), (' n', 'find next'), (' N', 'find previous'), (' q', 'quit')]
12     cs = []
13     # column definitions are in (label, weight, specified width) triples
14     columnDefs = [('something',1,None)]
15     mx = my = 0
16     cx = cy = 0
17     # for searches
18     last_search = ""
19     found_index = 0
20
21     def __init__(self,window,helpbar):
22         self.w = window
23         self.hb = helpbar
24         self.updateGeometry()
25         self.commands = self.cs+self.commands
26
27     def sortByColumn(self, col):
28         self.entries.sort(key=lambda k: k.get(col)) # key=dict.get(col))
29         self.selected = map(lambda x: False, self.selected)
30
31     def updateGeometry(self):
32         (self.my,self.mx)=self.w.getmaxyx()
33         (y,x) = self.w.getbegyx()
34         self.cx = x + self.mx/2
35         self.cy = y + self.my/2
36         self.pageSize = self.my-3
37         self.calcColWidths()
38
39     def calcColWidths(self):
40         total_weights = 0
41         available_space = self.mx - len(self.columnDefs)
42         cols = []
43         for label,weight,value in self.columnDefs:
44             if value!=None:
45                 available_space -= value
46             else:
47                 total_weights+=weight
48
49         for label,weight,value in self.columnDefs:
50             if value!=None:
51                 cols.append((label,value))
52             else:
53                 cols.append((label,available_space*weight/total_weights))
54         self.columns=cols
55
56     def refresh(self):
57         self.hb.commands = self.commands
58         self.hb.refresh()
59         self.displayHeader()
60         for r in range(0,self.pageSize):
61             self.displayRow(r)
62         self.w.refresh()
63         self.highlight()
64
65     def clear(self):
66         self.w.erase()
67         self.w.refresh()
68
69     def centreChild(self,child):
70         (y,x)=child.getmaxyx()
71         child.mvwin(self.cy-y/2,self.cx-x/2)
72
73
74     def displayHeader(self):
75         cursor = 1
76         for header,width in self.columns:
77             self.w.addnstr(0,cursor,header+" "*width,width)
78             self.w.addstr(1,cursor,"-"*width)
79             cursor += width+1
80
81     def displayRow(self,row):
82         if self.topline+row < len(self.entries):
83             entry = self.entries[self.topline+row]
84             cursor = 1
85             if self.selected[self.topline+row]:
86                 self.w.addstr(row+2, 0, "*")
87             else:
88                 self.w.addstr(row+2, 0, " ")
89             for k,width in self.columns:
90                 if k.lower() in entry:
91                     self.w.addnstr(row+2,cursor,str(entry[k.lower()])+" "*width,width)
92                 cursor += width+1
93         else:
94             self.w.addstr(row+2,0," "*self.mx)
95
96     def highlight(self):
97         row = self.hl-self.topline+2
98         if row > 1 and row < self.my:
99             self.w.chgat(row,0,self.mx,curses.A_REVERSE)
100
101     def unHighlight(self):
102         row = self.hl-self.topline+2
103         if row > 1 and row < self.my:
104             self.w.chgat(row,0,self.mx,curses.A_NORMAL)
105
106     def mvHighlight(self,delta):
107         new = self.hl+delta
108         new = max(new,0)
109         new = min(new,len(self.entries)-1)
110         self.unHighlight()
111         self.hl = new
112         self.highlight()
113     
114     def scroll(self,delta):
115         self.unHighlight()
116         self.topline += delta
117         self.topline = min(self.topline,len(self.entries)-1)
118         self.topline = max(self.topline,0)
119         self.refresh()
120
121     def search(self, string):
122         i = 0
123         found = False
124         for e in self.entries:
125             for k,v in e.items():
126                 if str(v).find(string) != -1:
127                     found = True
128             if found:
129                 break
130             i += 1;
131         if found:
132             self.last_search = string
133             self.search_index = i
134             return i
135         else:
136             self.search_index = -1
137             return -1
138
139     def findNext(self):
140         if self.last_search == "" or self.search_index == -1:
141             return -1
142         found = False
143         for i in range(self.hl+1,len(self.entries)-1):
144             for k,v in self.entries[i].items():
145                 if str(v).find(self.last_search) != -1:
146                     found = True
147             if found:
148                 break
149         if found:
150             self.search_index = i
151             return i
152         else:
153             return -1
154
155     def findPrevious(self):
156         if self.last_search == "" or self.search_index == -1:
157             return -1
158         found = False
159         for i in range(self.hl-1, 0, -1):
160             for k,v in self.entries[i].items():
161                 if str(v).find(self.last_search) != -1:
162                     found = True
163             if found:
164                 break
165         if found:
166             self.search_index = i
167             return i
168         else:
169             return -1
170
171
172     def eventLoop(self):
173         self.w.keypad(1)
174         self.refresh()
175
176         ch = self.w.getch()
177         while ch != 27 and ch != 113:
178             ch = self.handleInput(ch)
179             if ch==113:
180                 return {}
181             self.w.refresh()
182             ch = self.w.getch()
183             self.hb.refresh()
184
185     def handleInput(self,ch):
186         if ch == curses.KEY_UP or ch == 107 or ch == 16:
187             if self.hl == self.topline:
188                 self.scroll(-self.pageSize/2-1)
189             self.mvHighlight(-1)
190         elif ch == curses.KEY_DOWN or ch == 106 or ch == 14:
191             if self.hl == self.topline+self.pageSize-1:
192                 self.scroll(+self.pageSize/2+1)
193             self.mvHighlight(+1)
194         elif ch == curses.KEY_PPAGE:
195             self.scroll(-self.pageSize)
196             self.mvHighlight(-self.pageSize)
197         elif ch == curses.KEY_NPAGE:
198             self.scroll(+self.pageSize)
199             self.mvHighlight(+self.pageSize)
200         elif ch == 47: # forward slash
201             string = self.hb.getSearch()
202             hl = self.search(string)
203             if hl != -1:
204                 delta = hl - self.hl
205                 self.scroll(delta)
206                 self.mvHighlight(delta)
207             else:
208                 self.hb.display(string+' not found')
209         elif ch == 110: # n
210             hl = self.findNext()
211             if hl != -1:
212                 delta = hl - self.hl
213                 self.scroll(delta)
214                 self.mvHighlight(delta)
215             else:
216                 self.hb.display(self.last_search+' not found')
217         elif ch == 78: # N
218             hl = self.findPrevious()
219             if hl != -1:
220                 delta = hl - self.hl
221                 self.scroll(delta)
222                 self.mvHighlight(delta)
223             else:
224                 self.hb.display(self.last_search+' not found')
225         elif ch == 32:
226             if len(self.selected)>0:
227                 self.selected[self.hl] = not self.selected[self.hl]
228             self.displayRow(self.hl-self.topline)
229             self.highlight()
230
231
232
233 class trashBrowser(browserWindow):
234     columnDefs = [('ID',0,3),
235                   ('ISBN',0,13),
236                   ('Authors',30,None),
237                   ('Title',60,None)]
238     
239     cs = [(' r', 'restore selected'), (' d', 'delete selected')]
240     
241     # redefinable functions
242     def viewSelection(self,book):
243         bookid = book['id']
244         w=curses.newwin(1,1,20,20)
245         bf = bookForm(w,self.hb,book)
246         self.centreChild(w)
247         bf.caption='Viewing Book '+str(bookid)
248         bf.blabel='done'
249         bf.eventLoop()
250         bf.clear()
251
252     def restoreSelected(self):
253         books = []
254         for sel,book in zip(self.selected, self.entries):
255             if sel:
256                 books.append(book)
257         db.restoreBooks(books)
258
259     def delSelected(self):
260         books = []
261         for sel,book in zip(self.selected, self.entries):
262             if sel:
263                 books.append(book)
264         db.deleteBooks(books)
265
266     def refreshBooks(self):
267         self.entries = db.getRemovedBooks()
268         self.selected = map(lambda x:False, self.entries)
269
270     def handleInput(self,ch):
271         browserWindow.handleInput(self,ch)
272         if ch == 10:
273             book = self.entries[self.hl]
274             self.viewSelection(book)
275             self.refresh()
276         if ch==114: #restore books
277             count=0
278             for s in self.selected[0:self.hl-1]:
279                 if s:
280                     count+=1
281             self.restoreSelected()
282             self.refreshBooks()
283             self.refresh()
284             self.scroll(-count)
285             self.mvHighlight(-count)
286         if ch==100: # delete books
287             count=0
288             for s in self.selected[0:self.hl-1]:
289                 if s:
290                     count+=1
291             self.delSelected()
292             self.refreshBooks()
293             self.refresh()
294             self.scroll(-count)
295             self.mvHighlight(-count)
296         return ch
297
298 class bookBrowser(browserWindow):
299     columnDefs = [('ID',0,3),
300                   ('ISBN',0,13),
301                   ('Authors',30,None),
302                   ('Title',60,None)]
303     
304     cs = [(' u', 'update'), (' d', 'delete selected')]
305     
306     # redefinable functions
307     def updateSelection(self,book):
308         bookid = book['id']
309         
310         w=curses.newwin(1,1)
311         bf=bookForm(w,self.hb,book)
312         self.centreChild(w)
313         bf.caption='Update Book '+str(bookid)
314         bf.blabel='update'
315         newbook = bf.eventLoop()
316         if len(newbook)!=0:
317             db.updateBook(newbook,bookid)
318         bf.clear()
319
320     def viewSelection(self,book):
321         bookid = book['id']
322         w=curses.newwin(1,1,20,20)
323         bf = bookForm(w,self.hb,book)
324         self.centreChild(w)
325         bf.caption='Viewing Book '+str(bookid)
326         bf.blabel='done'
327         bf.eventLoop()
328         bf.clear()
329
330     def categorizeSelection(self,book):
331         w = curses.newwin(40,20,20,20)
332         cs = categorySelector(w,self.hb)
333         self.centreChild(w)
334         cs.book = book
335         cs.refreshCategories()
336         cs.eventLoop()
337         cs.clear()
338     
339     def delSelected(self):
340         books = []
341         for sel,book in zip(self.selected, self.entries):
342             if sel:
343                 books.append(book)
344         db.removeBooks(books)
345
346     def refreshBooks(self):
347         self.entries = db.getBooks()
348         self.selected = map(lambda x:False, self.entries)
349
350     def handleInput(self,ch):
351         browserWindow.handleInput(self,ch)
352         if ch == 117: #update on 'u'
353             book = self.entries[self.hl]
354             self.updateSelection(book)
355             self.entries[self.hl]=db.getBookByID(book['id'])
356             self.refresh()
357         elif ch == 10:
358             book = self.entries[self.hl]
359             self.viewSelection(book)
360             self.refresh()
361         elif ch == 99:
362             book = self.entries[self.hl]
363             self.categorizeSelection(book)
364             self.refresh()
365         if ch==100:
366             count=0
367             for s in self.selected[0:self.hl-1]:
368                 if s:
369                     count+=1
370             self.delSelected()
371             self.refreshBooks()
372             self.refresh()
373             self.scroll(-count)
374             self.mvHighlight(-count)
375         return ch
376
377 class categoryBrowser(browserWindow):
378     columnDefs = [('Category',100,None)]
379     cs = [(' a', 'add category'), (' d', 'delete selected')]
380
381
382     def refreshCategories(self):
383         self.entries = db.getCategories()
384         self.sortByColumn('category')
385         self.selected = map(lambda x:False, self.entries)
386
387     def addCategory(self):
388         w = curses.newwin(1,1,10,10)
389         cf = categoryForm(w,self.hb)
390         self.centreChild(w)
391         cats = cf.eventLoop()
392         for c in cats:
393             db.addCategory(c)
394         cf.clear()
395
396     def delSelected(self):
397         categories = []
398         for sel,cat in zip(self.selected, self.entries):
399             if sel:
400                 categories.append(cat)
401         db.deleteCategories(categories)
402
403     def handleInput(self,ch):
404         browserWindow.handleInput(self,ch)
405         if ch==97:
406             self.addCategory()
407             self.refreshCategories()
408             self.refresh()
409         if ch==100:
410             count=0
411             for s in self.selected[0:self.hl-1]:
412                 if s:
413                     count+=1
414             self.delSelected()
415             self.refreshCategories()
416             self.refresh()
417             self.scroll(-count)
418             self.mvHighlight(-count)
419         return ch
420
421 class categorySelector(browserWindow):
422     columnDefs = [('Category',100,None)]
423     cs = [(' a', 'add category'), (' c', 'commit')]
424     book = {'id':''}
425     original=[]
426
427
428     def refreshCategories(self):
429         self.entries = db.getCategories()
430         self.sortByColumn('category')
431         self.refreshSelected()
432
433     def refreshSelected(self):
434         self.original = map(lambda x:False, self.entries)
435         cats = db.getBookCategories(self.book)
436         cats.sort()
437         cats.sort(key=lambda k: k.get('category')) # key=dict.get(col))
438         i = 0
439         j = 0
440         for cat in self.entries:
441             if i == len(cats):
442                 break
443             if cat['id']==cats[i]['cat_id']:
444                 self.original[j] = True;
445                 i+=1
446             j+=1
447         self.selected = self.original[:]
448
449
450     def addCategory(self):
451         w = curses.newwin(1,1,10,10)
452         cf = categoryForm(w,self.hb)
453         self.centreChild(w)
454         cats = cf.eventLoop()
455         for c in cats:
456             db.addCategory(c)
457         cf.clear()
458
459     def updateCategories(self):
460         # first removed the deselected ones
461         uncats = []
462         cats = []
463         for old, new, category in zip(self.original, self.selected, self.entries):
464             if old and (not new):
465                 uncats.append(category)
466             if (not old) and new:
467                 cats.append(category)
468         db.uncategorizeBook(self.book, uncats)
469         # add the newly selected categories
470         db.categorizeBook(self.book, cats)
471
472
473     def handleInput(self,ch):
474         browserWindow.handleInput(self,ch)
475         if ch==97:
476             self.addCategory()
477             self.refreshCategories()
478             self.refresh()
479         if ch==99:
480             self.updateCategories()
481             return 113
482