significant improvements to database (adding/removing books, categories, etc.)
[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=bookData.openLibrary
89     bf.caption='Add a Book'
90     bf.blabel = 'Add'
91     book = bf.eventLoop()
92     bf.clear()
93     if len(book)!=0:
94         db.addBook(book)
95
96 def updateMenu():
97     w=curses.newwin(1,50,10,10)
98     w.addstr("I will be used to update or modify book records")
99     w.refresh()
100
101 def trashMenu():
102     (my,mx)=stdscr.getmaxyx()
103     w=curses.newwin(20,80,(my-20)/2,(mx-80)/2)
104     b = browser.trashBrowser(w,hb)
105     b.refreshBooks()
106     b.eventLoop()
107     b.clear()
108
109 def browseMenu():
110     (my,mx)=stdscr.getmaxyx()
111     w=curses.newwin(20,80,(my-20)/2,(mx-80)/2)
112     b = browser.bookBrowser(w,hb)
113     b.refreshBooks()
114     b.eventLoop()
115     b.clear()
116
117 def catMenu():
118     (my,mx)=stdscr.getmaxyx()
119     w=curses.newwin(10,40,(my-10)/2,(mx-40)/2)
120     c = browser.categoryBrowser(w,hb)
121     c.refreshCategories()
122     c.sortByColumn('category')
123     c.eventLoop()
124     c.clear()
125
126
127 m = [("Browse Library", browseMenu),
128      ("Add Book", addForm),
129      ("Categories", catMenu),
130      ("View Trash", trashMenu),
131      ("",exit),
132      ("Exit", exit)]
133 curses.wrapper(menutest, m)