python3 conversion

This commit is contained in:
John Ladan 2013-10-21 19:38:21 -04:00
parent 69ade368bc
commit 73c35979c3
5 changed files with 37 additions and 28 deletions

View File

@ -1,5 +1,11 @@
from urllib2 import urlopen,URLError
from json import load,dumps
try:
# For Python 3.0 and later
from urllib.request import urlopen,URLError
except ImportError:
# Fall back to Python 2's urllib2
from urllib2 import urlopen,URLError
from json import loads,dumps
import sys
""" Library Book Type Description:
The book is a dictionary of the form { string : a, ... }
@ -33,12 +39,12 @@ def openLibrary_isbn(ISBN):
jsondata = urlopen("http://openlibrary.org/api/books?format=json&jscmd=data&bibkeys=ISBN:"+isbn, timeout=3)
except URLError:
return {}
openBook = load(jsondata)
openBook = loads(jsondata.read().decode('utf-8'))
if "ISBN:"+isbn not in 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}
book = dict({"isbn" : isbn})
book["title"]=openBook["title"]
book["authors"]=""
if "authors" in openBook:

View File

@ -7,7 +7,7 @@ class browserWindow:
hl=0
topline = 0
entries = []
selected = []
selected = list()
commands = [(' /', 'search'), (' n', 'find next'), (' N', 'find previous'), (' q', 'quit')]
cs = []
# column definitions are in (label, weight, specified width) triples
@ -26,13 +26,13 @@ class browserWindow:
def sortByColumn(self, col):
self.entries.sort(key=lambda k: k.get(col)) # key=dict.get(col))
self.selected = map(lambda x: False, self.selected)
self.selected = list(map(lambda x: False, self.selected))
def updateGeometry(self):
(self.my,self.mx)=self.w.getmaxyx()
(y,x) = self.w.getbegyx()
self.cx = x + self.mx/2
self.cy = y + self.my/2
self.cx = x + self.mx//2
self.cy = y + self.my//2
self.pageSize = self.my-4
self.calcColWidths()
@ -50,7 +50,7 @@ class browserWindow:
if value!=None:
cols.append((label,value))
else:
cols.append((label,available_space*weight/total_weights))
cols.append((label,available_space*weight//total_weights))
self.columns=cols
def refresh(self):
@ -69,7 +69,7 @@ class browserWindow:
def centreChild(self,child):
(y,x)=child.getmaxyx()
child.mvwin(self.cy-y/2,self.cx-x/2)
child.mvwin(self.cy-y//2,self.cx-x//2)
def displayHeader(self):
@ -84,6 +84,7 @@ class browserWindow:
entry = self.entries[self.topline+row]
cursor = 2
self.w.addnstr(row+3, 1, " "*self.mx,self.mx-2)
sys.stderr.write(str(type(self.selected)))
if self.selected[self.topline+row]:
self.w.addstr(row+3, 1, "*")
else:
@ -187,11 +188,11 @@ class browserWindow:
def handleInput(self,ch):
if ch == curses.KEY_UP or ch == 107 or ch == 16:
if self.hl == self.topline:
self.scroll(-self.pageSize/2-1)
self.scroll(-self.pageSize//2-1)
self.mvHighlight(-1)
elif ch == curses.KEY_DOWN or ch == 106 or ch == 14:
if self.hl == self.topline+self.pageSize-1:
self.scroll(+self.pageSize/2+1)
self.scroll(+self.pageSize//2+1)
self.mvHighlight(+1)
elif ch == curses.KEY_PPAGE:
self.scroll(-self.pageSize)
@ -276,7 +277,7 @@ class trashBrowser(browserWindow):
def refreshBooks(self):
self.entries = db.getRemovedBooks()
self.selected = map(lambda x:False, self.entries)
self.selected = list(map(lambda x:False, self.entries))
def handleInput(self,ch):
browserWindow.handleInput(self,ch)
@ -356,11 +357,11 @@ class bookBrowser(browserWindow):
def refreshBooks(self):
self.entries = db.getBooks()
self.selected = map(lambda x:False, self.entries)
self.selected = list(map(lambda x:False, self.entries))
def refreshBooksInCategory(self,cat):
self.entries = db.getBooksByCategory(cat)
self.selected = map(lambda x:False, self.entries)
self.selected = list(map(lambda x:False, self.entries))
def handleInput(self,ch):
browserWindow.handleInput(self,ch)
@ -397,7 +398,7 @@ class categoryBrowser(browserWindow):
def refreshCategories(self):
self.entries = db.getCategories()
self.sortByColumn('category')
self.selected = map(lambda x:False, self.entries)
self.selected = list(map(lambda x:False, self.entries))
def addCategory(self):
w = curses.newwin(1,1,10,10)
@ -457,7 +458,7 @@ class categorySelector(browserWindow):
self.refreshSelected()
def refreshSelected(self):
self.original = map(lambda x:False, self.entries)
self.original = list(map(lambda x:False, self.entries))
cats = db.getBookCategories(self.book)
cats.sort()
cats.sort(key=lambda k: k.get('category')) # key=dict.get(col))

View File

@ -1,4 +1,5 @@
import curses
import sys
class formWindow:
mx = my = 0
@ -52,7 +53,7 @@ class formWindow:
self.hb.refresh()
self.updateGeometry()
self.w.box()
self.w.addstr(0,(self.mx-len(self.caption))/2,self.caption)
self.w.addstr(0,(self.mx-len(self.caption))//2,self.caption)
r=self.top
for l in self.labels:
c = self.left-len(l)-2
@ -105,7 +106,8 @@ class formWindow:
def insert(self,ch):
c = self.cursor
self.entries[self.hl]=self.entries[self.hl][:c] +ch+ self.entries[self.hl][c:]
#sys.stderr.write(str(type(ch)))
self.entries[self.hl]=self.entries[self.hl][:c] +ch.decode('utf-8')+ self.entries[self.hl][c:]
self.drawRow(self.hl)
self.mvCursor(+1)
self.highlight()

View File

@ -14,8 +14,8 @@ class helpBar:
def updateGeometry(self):
(self.my, self.mx) = self.w.getmaxyx()
(self.y, self.x) = self.w.getbegyx()
self.numCols = self.mx/self.colWidth
numRows = len(self.commands)/self.numCols +1
self.numCols = self.mx//self.colWidth
numRows = len(self.commands)//self.numCols +1
self.y += self.my - numRows
self.my = numRows
self.w.mvwin(0,0)

View File

@ -1,9 +1,9 @@
#!/usr/bin/env python
#!/usr/bin/env python3
import curses
import dbLayer as db
import browser
import form
import form as form
import helpBar
import bookData
@ -24,7 +24,7 @@ def menutest(s, l):
hb = helpBar.helpBar(bar)
hb.command=menu_commands
hb.refresh()
w = curses.newwin(10,40,(rows-10)/2, (cols-40)/2)
w = curses.newwin(10,40,(rows-10)//2, (cols-40)//2)
menu(w, l)
@ -84,7 +84,7 @@ def addForm():
(my,mx)=stdscr.getmaxyx()
bf = form.bookForm(w,hb)
(r,c)=w.getmaxyx()
w.mvwin((my-r)/2,(mx-c)/2)
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'
@ -101,7 +101,7 @@ def updateMenu():
def trashMenu():
(my,mx)=stdscr.getmaxyx()
w=curses.newwin(20,80,(my-20)/2,(mx-80)/2)
w=curses.newwin(20,80,(my-20)//2,(mx-80)//2)
b = browser.trashBrowser(w,hb)
b.refreshBooks()
b.eventLoop()
@ -109,7 +109,7 @@ def trashMenu():
def browseMenu():
(my,mx)=stdscr.getmaxyx()
w=curses.newwin(20,80,(my-20)/2,(mx-80)/2)
w=curses.newwin(20,80,(my-20)//2,(mx-80)//2)
b = browser.bookBrowser(w,hb)
b.refreshBooks()
b.eventLoop()
@ -117,7 +117,7 @@ def browseMenu():
def catMenu():
(my,mx)=stdscr.getmaxyx()
w=curses.newwin(10,40,(my-10)/2,(mx-40)/2)
w=curses.newwin(10,40,(my-10)//2,(mx-40)//2)
c = browser.categoryBrowser(w,hb)
c.refreshCategories()
c.sortByColumn('category')