added some desired features
[public/library.git] / librarian.py
index ce4635b..eee1ec6 100755 (executable)
@@ -1,14 +1,29 @@
 #!/usr/bin/env python
 
 import curses
+import dbLayer as db
+import browser
+import form
+import helpBar
+
+import bookData
+
 
 stdscr=0
+hb=0
+
+menu_commands = [(' q','quit')]
 
 def menutest(s, l):
     global stdscr
+    global hb
     stdscr=s
     curses.curs_set(0)
     (rows,cols)=stdscr.getmaxyx()
+    bar = curses.newwin(1,cols-2,rows-1,1)
+    hb = helpBar.helpBar(bar)
+    hb.command=menu_commands
+    hb.refresh()
     w = curses.newwin(10,40,(rows-10)/2, (cols-40)/2)
 
     menu(w, l)
@@ -23,17 +38,29 @@ def menu(w, items):
 
     w.refresh()
     ch=w.getch()
-    while (ch!=113): # leave on q
-        if ch==curses.KEY_UP:
+    while (ch!=113 and ch!=27): # leave on q or ESC
+        if ch==curses.KEY_UP or ch==107 or ch==16:
             if highlight!=0:
                 w.chgat(highlight,0, 0)
                 highlight -= 1
+                while(items[highlight][0]==""):
+                    highlight -=1
                 w.chgat(highlight,0, curses.A_REVERSE)
-        if ch==curses.KEY_DOWN:
+        if ch==curses.KEY_DOWN or ch==106 or ch==14:
             if highlight!=len(items)-1:
                 w.chgat(highlight,0, 0)
                 highlight += 1
+                while(items[highlight][0]==""):
+                    highlight +=1
                 w.chgat(highlight,0, curses.A_REVERSE)
+        if ch==curses.KEY_PPAGE:
+            w.chgat(highlight,0, 0)
+            highlight = 0
+            w.chgat(highlight,0, curses.A_REVERSE)
+        if ch==curses.KEY_NPAGE:
+            w.chgat(highlight,0, 0)
+            highlight = len(items)-1
+            w.chgat(highlight,0, curses.A_REVERSE)
         if ch==114 or ch==10:
             (s,f)=items[highlight]
             f()
@@ -48,141 +75,60 @@ def redrawMenu(w,items,highlight):
         i +=1
     w.chgat(highlight, 0, curses.A_REVERSE)
     w.refresh()
-
-# items is a list of (label, value) pairs
-def redrawForm(w, caption, items, buttonlabel, m):
-    (y,x)=w.getmaxyx()
-    w.border()
-    curses.curs_set(1)
-    w.addstr(1,1,caption)
-    r=3
-    for l,v in items:
-        c = m-len(l)-2
-        w.addstr(r,c,l+":")
-        w.addstr(r,m,v)
-        r+=2
-    w.addstr(r,x-len(buttonlabel)-len("<cancel>")-6, "<cancel>  <"+buttonlabel+">")
-    w.refresh()
-
-
-#the final form for book data entry - takes caption and book info.
-def bookForm(caption, book, buttonlabel):
-    labels = ["ISBN", "LCCN", "Title", "Subtitle", "Authors", "Edition",
-              "Publisher", "Publish Date", "Publish Year", "Publish Month", "Publish location",
-              "Pages", "Pagination", "weight"]
-    entries = []
-    m = 0
-    for l in labels:
-        m = max(len(l),m)
-        if l.lower() in book:
-            entries.append(book[l.lower()])
-        else:
-            entries.append("")
-    m+=4
-
-    w=curses.newwin(34,50,1,10)
-    (y,x)=w.getmaxyx()
-    w.keypad(1)
-    redrawForm(w,caption,zip(labels,entries),buttonlabel,m)
-    bcol = [x-len(buttonlabel)-len("<cancel>")-6, x-len(buttonlabel)-4]
-    bwidth = [8,len(buttonlabel)+2]
-    
-    highlight=0
-    b = -1
-    r=3
-    w.chgat(r,m,x-m-2,curses.A_UNDERLINE)
-    w.move(r,m+len(entries[highlight]))
-    ch = w.getch()
-    while (ch!=113):
-        if ch==curses.KEY_UP:
-            if highlight == len(labels):
-                w.chgat(r,bcol[b],bwidth[b],curses.A_NORMAL)
-                highlight = len(labels)-1
-                b = -1
-                r=3+2*highlight
-                w.chgat(r,m,x-m-2,curses.A_UNDERLINE)
-                w.move(r,m+len(entries[highlight]))
-                curses.curs_set(1)
-            elif highlight!=0:
-                w.chgat(r,m,x-m-2,curses.A_NORMAL)
-                highlight -= 1
-                r=3+2*highlight
-                w.chgat(r,m,x-m-2,curses.A_UNDERLINE)
-                w.move(r,m+len(entries[highlight]))
-        if ch==curses.KEY_DOWN:
-            if highlight >= len(labels) -1:
-                highlight = len(labels)
-                b += 1
-                b = min(b,1)
-                curses.curs_set(0)
-                w.chgat(r,m,x-m-2,curses.A_NORMAL)
-                r = y-3
-                w.chgat(r,bcol[b],bwidth[b],curses.A_REVERSE)
-            else:
-                w.chgat(r,m,x-m-2,curses.A_NORMAL)
-                highlight += 1
-                r=3+2*highlight
-                w.chgat(r,m,x-m-2,curses.A_UNDERLINE)
-                w.move(r,m+len(entries[highlight]))
-        if ch==10:
-            if b != -1:
-                if b == 0:
-                    w.clear()
-                    w.refresh()
-                    return {}
-                elif b == 1:
-                    w.clear()
-                    w.refresh()
-                    return {"title": "I was 'added'"}
-            elif highlight == len(labels)-1:
-                highlight = len(labels)
-                b=0
-                curses.curs_set(0)
-                w.chgat(r,m,x-m-2,curses.A_NORMAL)
-                r = y-3
-                w.chgat(r,bcol[b],bwidth[b],curses.A_REVERSE)
-            else:
-                w.chgat(r,m,x-m-2,curses.A_NORMAL)
-                highlight += 1
-                r=3+2*highlight
-                w.chgat(r,m,x-m-2,curses.A_UNDERLINE)
-                w.move(r,m+len(entries[highlight]))
-
-
-        w.refresh()
-        ch = w.getch()
-
-    curses.curs_set(0)
-    w.clear()
-    w.refresh()
-    return {"title":"this is what I returned"}
+    hb.commands=menu_commands
+    hb.refresh()
 
 
 def addForm():
-    book = {"title":"A Book of Tests", "pages":"123"}
-    book = bookForm("Add a book", book, "add")
-    stdscr.getch()
-    bookForm("View the book", book, "done")
-
+    w=curses.newwin(1,1)
+    (my,mx)=stdscr.getmaxyx()
+    bf = form.bookForm(w,hb)
+    (r,c)=w.getmaxyx()
+    w.mvwin((my-r)/2,(mx-c)/2)
+    bf.lookup_isbn=bookData.openLibrary_isbn
+    bf.lookup_lccn=bookData.openLibrary_lccn
+    bf.caption='Add a Book'
+    bf.blabel = 'Add'
+    book = bf.eventLoop()
+    bf.clear()
+    if len(book)!=0:
+        db.addBook(book)
 
 def updateMenu():
     w=curses.newwin(1,50,10,10)
     w.addstr("I will be used to update or modify book records")
     w.refresh()
 
-def deleteMenu():
-    w=curses.newwin(1,50,10,10)
-    w.addstr("I will be used to delete book records")
-    w.refresh()
+def trashMenu():
+    (my,mx)=stdscr.getmaxyx()
+    w=curses.newwin(20,80,(my-20)/2,(mx-80)/2)
+    b = browser.trashBrowser(w,hb)
+    b.refreshBooks()
+    b.eventLoop()
+    b.clear()
 
 def browseMenu():
-    w=curses.newwin(1,50,10,10)
-    w.addstr("I will be used to browse book records")
-    w.refresh()
+    (my,mx)=stdscr.getmaxyx()
+    w=curses.newwin(20,80,(my-20)/2,(mx-80)/2)
+    b = browser.bookBrowser(w,hb)
+    b.refreshBooks()
+    b.eventLoop()
+    b.clear()
+
+def catMenu():
+    (my,mx)=stdscr.getmaxyx()
+    w=curses.newwin(10,40,(my-10)/2,(mx-40)/2)
+    c = browser.categoryBrowser(w,hb)
+    c.refreshCategories()
+    c.sortByColumn('category')
+    c.eventLoop()
+    c.clear()
+
 
 m = [("Browse Library", browseMenu),
-     ("Add Book or other item", addForm),
-     ("Modify/Update record", updateMenu),
-     ("Remove book from catalogue", deleteMenu),
+     ("Add Book", addForm),
+     ("Categories", catMenu),
+     ("View Trash", trashMenu),
+     ("",exit),
      ("Exit", exit)]
 curses.wrapper(menutest, m)