diff --git a/bookData.py b/bookData.py index fba3e2e..3a7c2e4 100644 --- a/bookData.py +++ b/bookData.py @@ -27,11 +27,12 @@ Keys: # look up data from openlibrary.org using isbn -def openLibrary(isbn): +def openLibrary(ISBN): + isbn = str(ISBN) jsondata = urlopen("http://openlibrary.org/api/books?format=json&jscmd=data&bibkeys=ISBN:"+isbn) openBook = load(jsondata) if "ISBN:"+isbn not in openBook: - return openBook + return {'isbn':isbn,'title':'Book not found'} openBook = openBook["ISBN:"+isbn] # create my custom dict for books with the info we want. book = {"isbn" : isbn} @@ -68,13 +69,5 @@ def openLibrary(isbn): book["subtitle"]=openBook["subtitle"] return book -book = openLibrary("9780521714723") -print dumps(book, indent=2) -book = openLibrary("9780521565431") -print dumps(book, indent=2) -book = openLibrary("689145728392") -print dumps(book, indent=2) -book = openLibrary("9780321468932") -print dumps(book, indent=2) -book = openLibrary("9781555580414") -print dumps(book, indent=2) +book = openLibrary(9780865479104) +print dumps(book,indent=2) diff --git a/bookForm.py b/bookForm.py index 2ccfabb..7926f7c 100644 --- a/bookForm.py +++ b/bookForm.py @@ -1,24 +1,26 @@ import curses - - - - class bookForm: mx = my = 0 hl = 0 bt = -1 cursor = 0 + textStart = 0 left = 0 top = 2 row = 2 caption = "Add a Book" blabel = "Add" labels = ["ISBN", "LCCN", "Title", "Subtitle", "Authors", "Edition", - "Publisher", "Publish Date", "Publish Year", "Publish Month", "Publish location", + "Publishers", "Publish Date", "Publish Year", "Publish Month", "Publish location", "Pages", "Pagination", "Weight"] entries = [] + # redefineable functions lookup is called when 'enter' is pressed on ISBN + # and returns the looked-up book. Default returns nothing + def lookup(self,isbn): + return {'isbn':isbn} + def __init__(self,window,book={}): self.w = window self.w.resize(len(self.labels)+6,50) @@ -27,9 +29,10 @@ class bookForm: def updateGeometry(self): (self.my, self.mx) = self.w.getmaxyx() + self.left=0 for l in self.labels: self.left = max(len(l),self.left) - self.left += 2 + self.left += 4 self.width = self.mx-self.left-2 self.top = 2 # next, the buttons @@ -40,7 +43,7 @@ class bookForm: self.entries=[] for l in self.labels: if l.lower() in book: - self.entries.append(book[l.lower()]) + self.entries.append(str(book[l.lower()])) else: self.entries.append("") @@ -49,18 +52,21 @@ class bookForm: self.w.box() self.w.addstr(0,(self.mx-len(self.caption))/2,self.caption) r=self.top - for l,v in zip(self.labels,self.entries): + for l in self.labels: c = self.left-len(l)-2 self.w.addstr(r,c,l+":") - self.w.addnstr(r,self.left,v,self.width) + self.drawRow(r-self.top) r+=1 self.w.addstr(r+1,self.bcol[0], " <"+self.blabel+">") self.w.refresh() + def drawRow(self,row): + r = self.top + row + self.w.addnstr(r,self.left, self.entries[row]+" "*self.width, self.width) + def highlight(self): if self.bt == -1: self.w.chgat(self.row, self.left, self.width, curses.A_UNDERLINE) - self.cursor = len(self.entries[self.hl]) self.w.move(self.row, self.left+self.cursor) curses.curs_set(1) else: @@ -82,9 +88,47 @@ class bookForm: self.bt = min(self.bt,1) self.row+=1 else: + self.mvCursor(+len(self.entries[self.hl])) self.bt=-1 self.highlight() + def mvCursor(self,delta): + n = self.cursor + delta + n = max(n,0) + n = min(n,len(self.entries[self.hl])) + self.cursor = n + col = self.left + self.cursor - self.textStart + if col >= self.left and col < self.left+self.width: + self.w.move(self.row,col) + + def insert(self,ch): + c = self.cursor + self.entries[self.hl]=self.entries[self.hl][:c] +ch+ self.entries[self.hl][c:] + self.drawRow(self.hl) + self.highlight() + self.mvCursor(+1) + + def backspace(self): + if self.cursor>0: + self.entries[self.hl]=self.entries[self.hl][:self.cursor-1] + self.entries[self.hl][self.cursor:] + self.drawRow(self.hl) + self.highlight() + self.mvCursor(-1) + + def delete(self): + c = self.cursor + self.entries[self.hl]=self.entries[self.hl][:c] + self.entries[self.hl][c+1:] + self.drawRow(self.hl) + self.highlight() + + + def returnBook(): + book = {} + for k,v in zip(self.labels, self.entries): + if v!="" and k.lower()!="publish date": + book[k.lower()]=v + return book + def eventLoop(self): self.w.keypad(1) self.refresh() @@ -100,196 +144,44 @@ class bookForm: self.mvHighlight(+1) elif ch==curses.KEY_NPAGE: self.mvHighlight(+len(self.labels)) + + elif ch==curses.KEY_LEFT: + if self.bt==-1: + self.mvCursor(-1) + else: + self.bt=0 + elif ch==curses.KEY_HOME: + if self.bt==-1: + self.mvCursor(-len(self.entries[self.hl])) + elif ch==curses.KEY_RIGHT: + if self.bt==-1: + self.mvCursor(+1) + else: + self.bt=1 + elif ch==curses.KEY_END: + if self.bt==-1: + self.mvCursor(+len(self.entries[self.hl])) + + elif ch>=32 and ch<=126: + if self.bt==-1: + self.insert(curses.keyname(ch)) + elif ch==curses.KEY_BACKSPACE: + if self.bt==-1: + self.backspace() + elif ch==curses.KEY_DC: + if self.bt==-1: + self.delete() + + elif ch==10 or ch==curses.KEY_ENTER: + if self.hl==0: + book = self.lookup(self.entries[0]) + self.updateEntries(book) + self.refresh() + elif self.bt==0: + return {} + elif self.bt==1: + return self.returnBook() + self.mvHighlight(+1) self.w.refresh() ch = self.w.getch() - - -def test(stdscr): - w = curses.newwin(10,10,10,10) - bf = bookForm(w) - bf.eventLoop() - -curses.wrapper(test) - -# 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 - - 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 {}