made tests use python3
[library/.git] / librarian.py
1 #!/usr/bin/env python3
2
3 import curses
4 import db_layer as db
5 import browser
6 import form
7 import help_bar as helpBar
8
9 import book_data
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,width=mx-20)
86     (r,c)=w.getmaxyx()
87     w.mvwin((my-r)//2,(mx-c)//2)
88     bf.lookup_isbn=book_data.openLibrary_isbn
89     bf.lookup_lccn=book_data.openLibrary_lccn
90     bf.caption='Add a Book'
91     bf.blabel = 'Add'
92     book = bf.event_loop()
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(3,5)
105     b = browser.trashBrowser(w,hb,my-10,mx-10)
106     (r,c) = w.getmaxyx()
107     w.mvwin((my-r)//2 -2, (mx-c)//2)
108     b.refreshBooks()
109     b.eventLoop()
110     b.clear()
111
112 def browseMenu():
113     (my,mx)=stdscr.getmaxyx()
114     w=curses.newwin(3,5)
115     b = browser.bookBrowser(w,hb, my-10, mx-10)
116     (r,c) = w.getmaxyx()
117     w.mvwin((my-r)//2 -2, (mx-c)//2)
118     b.refreshBooks()
119     b.eventLoop()
120     b.clear()
121
122 def catMenu():
123     (my,mx)=stdscr.getmaxyx()
124     w=curses.newwin(3,5)
125     cat = browser.categoryBrowser(w,hb, 10,40)
126     (r,c) = w.getmaxyx()
127     w.mvwin((my-r)//2 -2, (mx-c)//2)
128     cat.refreshCategories()
129     cat.sortByColumn('category')
130     cat.eventLoop()
131     cat.clear()
132
133
134 if __name__ == "__main__":
135     db.initializeDatabase()
136     m = [("Browse Library", browseMenu),
137          ("Add Book", addForm),
138          ("Categories", catMenu),
139          ("View Trash", trashMenu),
140          ("",exit),
141          ("Exit", exit)]
142     curses.wrapper(menutest, m)
143
144