From 770b4f553b37391ddf87825e478728d52365652b Mon Sep 17 00:00:00 2001 From: John Ladan Date: Tue, 20 Mar 2012 13:12:33 -0400 Subject: [PATCH] We're basically functional now --- bookData.py | 10 ++- bookForm.py | 6 +- librarian.py | 200 +++------------------------------------------------ 3 files changed, 16 insertions(+), 200 deletions(-) diff --git a/bookData.py b/bookData.py index 3a7c2e4..8e10054 100644 --- a/bookData.py +++ b/bookData.py @@ -7,7 +7,7 @@ The book is a dictionary of the form { string : a, ... } Keys: required: (ideally) title - Book/Article title - publishers - string containing semi-colon separated list eg. "UW Press; CSC, inc." + publisher - string containing semi-colon separated list eg. "UW Press; CSC, inc." authors - as above. each name is of the form "First Initials. Last" eg. "Calum T. Dalek; Conan T.B. Ladan" optional: subtitle - string @@ -42,11 +42,11 @@ def openLibrary(ISBN): for v in openBook["authors"]: book['authors'] += "; " + v['name'] book['authors'] = book['authors'][2:] - book["publishers"]="" + book["publisher"]="" if "publishers" in openBook: for v in openBook["publishers"]: - book["publishers"] += "; " + v['name'] - book['publishers'] = book['publishers'][2:] + book["publisher"] += "; " + v['name'] + book['publisher'] = book['publisher'][2:] if "publish_places" in openBook: book["publish locations"]="" for v in openBook["publish_places"]: @@ -69,5 +69,3 @@ def openLibrary(ISBN): book["subtitle"]=openBook["subtitle"] return book -book = openLibrary(9780865479104) -print dumps(book,indent=2) diff --git a/bookForm.py b/bookForm.py index 7926f7c..78f3800 100644 --- a/bookForm.py +++ b/bookForm.py @@ -12,7 +12,7 @@ class bookForm: caption = "Add a Book" blabel = "Add" labels = ["ISBN", "LCCN", "Title", "Subtitle", "Authors", "Edition", - "Publishers", "Publish Date", "Publish Year", "Publish Month", "Publish location", + "Publisher", "Publish Date", "Publish Year", "Publish Month", "Publish location", "Pages", "Pagination", "Weight"] entries = [] @@ -121,8 +121,7 @@ class bookForm: self.drawRow(self.hl) self.highlight() - - def returnBook(): + def returnBook(self): book = {} for k,v in zip(self.labels, self.entries): if v!="" and k.lower()!="publish date": @@ -132,6 +131,7 @@ class bookForm: def eventLoop(self): self.w.keypad(1) self.refresh() + self.mvCursor(+len(self.entries[self.hl])) self.highlight() ch = self.w.getch() diff --git a/librarian.py b/librarian.py index a99409c..9dfcaae 100755 --- a/librarian.py +++ b/librarian.py @@ -3,6 +3,8 @@ import curses import dbLayer as db import browser +import bookForm +import bookData stdscr=0 @@ -63,200 +65,16 @@ def redrawMenu(w,items,highlight): 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("")-6, " <"+buttonlabel+">") - w.refresh() - -def lists_to_dict(l1, l2): - book = {} - for k,v in zip(l1,l2): - if v!="" and k.lower()!="publish date": - book[k.lower()]=v - return book - - -#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,70,1,10) - (y,x)=w.getmaxyx() - w.keypad(1) - redrawForm(w,caption,zip(labels,entries),buttonlabel,m) - bcol = [x-len(buttonlabel)-len("")-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])) - cursor = len(entries[highlight]) - ch = w.getch() - while (1==1): - if ch==27: #escape key - curses.curs_set(0) - w.clear() - w.refresh() - return {} - 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) - cursor = len(entries[highlight]) - w.move(r,m+cursor) - 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) - cursor = len(entries[highlight]) - w.move(r,m+cursor) - elif ch==curses.KEY_PPAGE: - w.chgat(r,m,x-m-2,curses.A_NORMAL) - highlight=0 - b=-1 - r=3+2*highlight - w.chgat(r,m,x-m-2,curses.A_UNDERLINE) - cursor = len(entries[highlight]) - w.move(r,m+cursor) - curses.curs_set(1) - elif 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) - cursor = len(entries[highlight]) - w.move(r,m+cursor) - elif ch==curses.KEY_NPAGE: - if highlight!=len(labels): - 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) - elif ch==curses.KEY_LEFT: - if highlight == len(labels): - w.chgat(r,bcol[b],bwidth[b],curses.A_NORMAL) - b=0 - w.chgat(r,bcol[b],bwidth[b],curses.A_REVERSE) - else: - if cursor>0: - cursor-=1 - w.move(r,m+cursor) - elif ch==curses.KEY_RIGHT: - if highlight == len(labels): - w.chgat(r,bcol[b],bwidth[b],curses.A_NORMAL) - b=1 - w.chgat(r,bcol[b],bwidth[b],curses.A_REVERSE) - else: - if cursor < len(entries[highlight]): - cursor+=1 - w.move(r,m+cursor) - elif ch>31 and ch<127: - if highlight != len(labels): - entries[highlight]=entries[highlight][:cursor] + curses.keyname(ch) + entries[highlight][cursor:] - cursor+=1 - w.addnstr(r,m, entries[highlight]+(" "*40), x-m-2) - w.chgat(r,m,x-m-2,curses.A_UNDERLINE) - w.move(r,m+cursor) - elif ch==curses.KEY_BACKSPACE: - if highlight != len(labels) and cursor!=0: - cursor-=1 - entries[highlight]=entries[highlight][:cursor] + entries[highlight][cursor+1:] - w.addnstr(r,m, entries[highlight]+(" "*40), x-m-2) - w.chgat(r,m,x-m-2,curses.A_UNDERLINE) - w.move(r,m+cursor) - elif ch==curses.KEY_DC: - if highlight != len(labels): - entries[highlight]=entries[highlight][:cursor] + entries[highlight][cursor+1:] - w.addnstr(r,m, entries[highlight]+(" "*40), x-m-2) - w.chgat(r,m,x-m-2,curses.A_UNDERLINE) - w.move(r,m+cursor) - elif ch==curses.KEY_HOME: - if highlight != len(labels): - cursor=0 - w.move(r,m+cursor) - elif ch==curses.KEY_END: - if highlight != len(labels): - cursor=len(entries[highlight]) - w.move(r,m+cursor) - elif ch==10: - if b != -1: - if b == 0: - w.clear() - w.refresh() - return {} - elif b == 1: - w.clear() - w.refresh() - return lists_to_dict(labels,entries) - 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) - cursor = len(entries[highlight]) - w.move(r,m+cursor) - - - w.refresh() - ch = w.getch() - - curses.curs_set(0) - w.clear() - w.refresh() - return {} - def addForm(): book = {"title":"A Book of Tests", "pages":"123"} - book = bookForm("Add a book", book, "add") - #bookForm("View the book", book, "done") + w=curses.newwin(1,1,20,20) + bf = bookForm.bookForm(w) + bf.lookup=bookData.openLibrary + bf.caption='Add a Book' + bf.blabel = 'Add' + bf.updateEntries(book) + book = bf.eventLoop() if len(book)!=0: db.addBook(book)