can now add categories from the category viewer
[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 browser_commands = [(' u','update'), (' d','delete'), (' q','quit')]
17
18 def menutest(s, l):
19     global stdscr
20     global hb
21     stdscr=s
22     curses.curs_set(0)
23     (rows,cols)=stdscr.getmaxyx()
24     bar = curses.newwin(1,cols-2,rows-1,1)
25     hb = helpBar.helpBar(bar)
26     hb.command=menu_commands
27     hb.refresh()
28     w = curses.newwin(10,40,(rows-10)/2, (cols-40)/2)
29
30     menu(w, l)
31     
32     curses.curs_set(1)
33
34 # item is a list of (string, callable) tuples
35 def menu(w, items):
36     w.keypad(1)
37     highlight=0
38     redrawMenu(w,items,highlight)
39
40     w.refresh()
41     ch=w.getch()
42     while (ch!=113 and ch!=27): # leave on q or ESC
43         if ch==curses.KEY_UP or ch==107 or ch==16:
44             if highlight!=0:
45                 w.chgat(highlight,0, 0)
46                 highlight -= 1
47                 while(items[highlight][0]==""):
48                     highlight -=1
49                 w.chgat(highlight,0, curses.A_REVERSE)
50         if ch==curses.KEY_DOWN or ch==106 or ch==14:
51             if highlight!=len(items)-1:
52                 w.chgat(highlight,0, 0)
53                 highlight += 1
54                 while(items[highlight][0]==""):
55                     highlight +=1
56                 w.chgat(highlight,0, curses.A_REVERSE)
57         if ch==curses.KEY_PPAGE:
58             w.chgat(highlight,0, 0)
59             highlight = 0
60             w.chgat(highlight,0, curses.A_REVERSE)
61         if ch==curses.KEY_NPAGE:
62             w.chgat(highlight,0, 0)
63             highlight = len(items)-1
64             w.chgat(highlight,0, curses.A_REVERSE)
65         if ch==114 or ch==10:
66             (s,f)=items[highlight]
67             f()
68             redrawMenu(w,items,highlight)
69         w.refresh()
70         ch = w.getch()
71
72 def redrawMenu(w,items,highlight):
73     i=0
74     for (mitem,fun) in items:
75         w.addstr(i,0, mitem)
76         i +=1
77     w.chgat(highlight, 0, curses.A_REVERSE)
78     w.refresh()
79     hb.commands=menu_commands
80     hb.refresh()
81
82
83 def addForm():
84     w=curses.newwin(1,1)
85     (my,mx)=stdscr.getmaxyx()
86     bf = form.bookForm(w)
87     (r,c)=w.getmaxyx()
88     w.mvwin((my-r)/2,(mx-c)/2)
89     bf.lookup=bookData.openLibrary
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 deleteMenu():
103     w=curses.newwin(1,50,10,10)
104     w.addstr("I will be used to delete book records")
105     w.refresh()
106
107 def browseMenu():
108     (my,mx)=stdscr.getmaxyx()
109     w=curses.newwin(20,80,(my-20)/2,(mx-80)/2)
110     b = browser.bookBrowser(w)
111     b.refreshBooks()
112     hb.commands=browser_commands
113     hb.refresh()
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)
121     c.refreshCategories()
122     c.sortByColumn('category')
123     hb.commands=browser_commands
124     hb.refresh()
125     c.eventLoop()
126     c.clear()
127
128
129 m = [("Browse Library", browseMenu),
130      ("Add Book or other item", addForm),
131      ("View the categories", catMenu),
132      ("Remove book from catalogue", deleteMenu),
133      ("",exit),
134      ("Exit", exit)]
135 curses.wrapper(menutest, m)