python3 conversion
This commit is contained in:
parent
69ade368bc
commit
73c35979c3
14
bookData.py
14
bookData.py
|
@ -1,5 +1,11 @@
|
||||||
from urllib2 import urlopen,URLError
|
try:
|
||||||
from json import load,dumps
|
# 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:
|
""" Library Book Type Description:
|
||||||
The book is a dictionary of the form { string : a, ... }
|
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)
|
jsondata = urlopen("http://openlibrary.org/api/books?format=json&jscmd=data&bibkeys=ISBN:"+isbn, timeout=3)
|
||||||
except URLError:
|
except URLError:
|
||||||
return {}
|
return {}
|
||||||
openBook = load(jsondata)
|
openBook = loads(jsondata.read().decode('utf-8'))
|
||||||
if "ISBN:"+isbn not in openBook:
|
if "ISBN:"+isbn not in openBook:
|
||||||
return {'isbn':isbn,'title':'Book not found'}
|
return {'isbn':isbn,'title':'Book not found'}
|
||||||
openBook = openBook["ISBN:"+isbn]
|
openBook = openBook["ISBN:"+isbn]
|
||||||
# create my custom dict for books with the info we want.
|
# create my custom dict for books with the info we want.
|
||||||
book = {"isbn" : isbn}
|
book = dict({"isbn" : isbn})
|
||||||
book["title"]=openBook["title"]
|
book["title"]=openBook["title"]
|
||||||
book["authors"]=""
|
book["authors"]=""
|
||||||
if "authors" in openBook:
|
if "authors" in openBook:
|
||||||
|
|
27
browser.py
27
browser.py
|
@ -7,7 +7,7 @@ class browserWindow:
|
||||||
hl=0
|
hl=0
|
||||||
topline = 0
|
topline = 0
|
||||||
entries = []
|
entries = []
|
||||||
selected = []
|
selected = list()
|
||||||
commands = [(' /', 'search'), (' n', 'find next'), (' N', 'find previous'), (' q', 'quit')]
|
commands = [(' /', 'search'), (' n', 'find next'), (' N', 'find previous'), (' q', 'quit')]
|
||||||
cs = []
|
cs = []
|
||||||
# column definitions are in (label, weight, specified width) triples
|
# column definitions are in (label, weight, specified width) triples
|
||||||
|
@ -26,13 +26,13 @@ class browserWindow:
|
||||||
|
|
||||||
def sortByColumn(self, col):
|
def sortByColumn(self, col):
|
||||||
self.entries.sort(key=lambda k: k.get(col)) # key=dict.get(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):
|
def updateGeometry(self):
|
||||||
(self.my,self.mx)=self.w.getmaxyx()
|
(self.my,self.mx)=self.w.getmaxyx()
|
||||||
(y,x) = self.w.getbegyx()
|
(y,x) = self.w.getbegyx()
|
||||||
self.cx = x + self.mx/2
|
self.cx = x + self.mx//2
|
||||||
self.cy = y + self.my/2
|
self.cy = y + self.my//2
|
||||||
self.pageSize = self.my-4
|
self.pageSize = self.my-4
|
||||||
self.calcColWidths()
|
self.calcColWidths()
|
||||||
|
|
||||||
|
@ -50,7 +50,7 @@ class browserWindow:
|
||||||
if value!=None:
|
if value!=None:
|
||||||
cols.append((label,value))
|
cols.append((label,value))
|
||||||
else:
|
else:
|
||||||
cols.append((label,available_space*weight/total_weights))
|
cols.append((label,available_space*weight//total_weights))
|
||||||
self.columns=cols
|
self.columns=cols
|
||||||
|
|
||||||
def refresh(self):
|
def refresh(self):
|
||||||
|
@ -69,7 +69,7 @@ class browserWindow:
|
||||||
|
|
||||||
def centreChild(self,child):
|
def centreChild(self,child):
|
||||||
(y,x)=child.getmaxyx()
|
(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):
|
def displayHeader(self):
|
||||||
|
@ -84,6 +84,7 @@ class browserWindow:
|
||||||
entry = self.entries[self.topline+row]
|
entry = self.entries[self.topline+row]
|
||||||
cursor = 2
|
cursor = 2
|
||||||
self.w.addnstr(row+3, 1, " "*self.mx,self.mx-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]:
|
if self.selected[self.topline+row]:
|
||||||
self.w.addstr(row+3, 1, "*")
|
self.w.addstr(row+3, 1, "*")
|
||||||
else:
|
else:
|
||||||
|
@ -187,11 +188,11 @@ class browserWindow:
|
||||||
def handleInput(self,ch):
|
def handleInput(self,ch):
|
||||||
if ch == curses.KEY_UP or ch == 107 or ch == 16:
|
if ch == curses.KEY_UP or ch == 107 or ch == 16:
|
||||||
if self.hl == self.topline:
|
if self.hl == self.topline:
|
||||||
self.scroll(-self.pageSize/2-1)
|
self.scroll(-self.pageSize//2-1)
|
||||||
self.mvHighlight(-1)
|
self.mvHighlight(-1)
|
||||||
elif ch == curses.KEY_DOWN or ch == 106 or ch == 14:
|
elif ch == curses.KEY_DOWN or ch == 106 or ch == 14:
|
||||||
if self.hl == self.topline+self.pageSize-1:
|
if self.hl == self.topline+self.pageSize-1:
|
||||||
self.scroll(+self.pageSize/2+1)
|
self.scroll(+self.pageSize//2+1)
|
||||||
self.mvHighlight(+1)
|
self.mvHighlight(+1)
|
||||||
elif ch == curses.KEY_PPAGE:
|
elif ch == curses.KEY_PPAGE:
|
||||||
self.scroll(-self.pageSize)
|
self.scroll(-self.pageSize)
|
||||||
|
@ -276,7 +277,7 @@ class trashBrowser(browserWindow):
|
||||||
|
|
||||||
def refreshBooks(self):
|
def refreshBooks(self):
|
||||||
self.entries = db.getRemovedBooks()
|
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):
|
def handleInput(self,ch):
|
||||||
browserWindow.handleInput(self,ch)
|
browserWindow.handleInput(self,ch)
|
||||||
|
@ -356,11 +357,11 @@ class bookBrowser(browserWindow):
|
||||||
|
|
||||||
def refreshBooks(self):
|
def refreshBooks(self):
|
||||||
self.entries = db.getBooks()
|
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):
|
def refreshBooksInCategory(self,cat):
|
||||||
self.entries = db.getBooksByCategory(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):
|
def handleInput(self,ch):
|
||||||
browserWindow.handleInput(self,ch)
|
browserWindow.handleInput(self,ch)
|
||||||
|
@ -397,7 +398,7 @@ class categoryBrowser(browserWindow):
|
||||||
def refreshCategories(self):
|
def refreshCategories(self):
|
||||||
self.entries = db.getCategories()
|
self.entries = db.getCategories()
|
||||||
self.sortByColumn('category')
|
self.sortByColumn('category')
|
||||||
self.selected = map(lambda x:False, self.entries)
|
self.selected = list(map(lambda x:False, self.entries))
|
||||||
|
|
||||||
def addCategory(self):
|
def addCategory(self):
|
||||||
w = curses.newwin(1,1,10,10)
|
w = curses.newwin(1,1,10,10)
|
||||||
|
@ -457,7 +458,7 @@ class categorySelector(browserWindow):
|
||||||
self.refreshSelected()
|
self.refreshSelected()
|
||||||
|
|
||||||
def refreshSelected(self):
|
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 = db.getBookCategories(self.book)
|
||||||
cats.sort()
|
cats.sort()
|
||||||
cats.sort(key=lambda k: k.get('category')) # key=dict.get(col))
|
cats.sort(key=lambda k: k.get('category')) # key=dict.get(col))
|
||||||
|
|
6
form.py
6
form.py
|
@ -1,4 +1,5 @@
|
||||||
import curses
|
import curses
|
||||||
|
import sys
|
||||||
|
|
||||||
class formWindow:
|
class formWindow:
|
||||||
mx = my = 0
|
mx = my = 0
|
||||||
|
@ -52,7 +53,7 @@ class formWindow:
|
||||||
self.hb.refresh()
|
self.hb.refresh()
|
||||||
self.updateGeometry()
|
self.updateGeometry()
|
||||||
self.w.box()
|
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
|
r=self.top
|
||||||
for l in self.labels:
|
for l in self.labels:
|
||||||
c = self.left-len(l)-2
|
c = self.left-len(l)-2
|
||||||
|
@ -105,7 +106,8 @@ class formWindow:
|
||||||
|
|
||||||
def insert(self,ch):
|
def insert(self,ch):
|
||||||
c = self.cursor
|
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.drawRow(self.hl)
|
||||||
self.mvCursor(+1)
|
self.mvCursor(+1)
|
||||||
self.highlight()
|
self.highlight()
|
||||||
|
|
|
@ -14,8 +14,8 @@ class helpBar:
|
||||||
def updateGeometry(self):
|
def updateGeometry(self):
|
||||||
(self.my, self.mx) = self.w.getmaxyx()
|
(self.my, self.mx) = self.w.getmaxyx()
|
||||||
(self.y, self.x) = self.w.getbegyx()
|
(self.y, self.x) = self.w.getbegyx()
|
||||||
self.numCols = self.mx/self.colWidth
|
self.numCols = self.mx//self.colWidth
|
||||||
numRows = len(self.commands)/self.numCols +1
|
numRows = len(self.commands)//self.numCols +1
|
||||||
self.y += self.my - numRows
|
self.y += self.my - numRows
|
||||||
self.my = numRows
|
self.my = numRows
|
||||||
self.w.mvwin(0,0)
|
self.w.mvwin(0,0)
|
||||||
|
|
14
librarian.py
14
librarian.py
|
@ -1,9 +1,9 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import curses
|
import curses
|
||||||
import dbLayer as db
|
import dbLayer as db
|
||||||
import browser
|
import browser
|
||||||
import form
|
import form as form
|
||||||
import helpBar
|
import helpBar
|
||||||
|
|
||||||
import bookData
|
import bookData
|
||||||
|
@ -24,7 +24,7 @@ def menutest(s, l):
|
||||||
hb = helpBar.helpBar(bar)
|
hb = helpBar.helpBar(bar)
|
||||||
hb.command=menu_commands
|
hb.command=menu_commands
|
||||||
hb.refresh()
|
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)
|
menu(w, l)
|
||||||
|
|
||||||
|
@ -84,7 +84,7 @@ def addForm():
|
||||||
(my,mx)=stdscr.getmaxyx()
|
(my,mx)=stdscr.getmaxyx()
|
||||||
bf = form.bookForm(w,hb)
|
bf = form.bookForm(w,hb)
|
||||||
(r,c)=w.getmaxyx()
|
(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_isbn=bookData.openLibrary_isbn
|
||||||
bf.lookup_lccn=bookData.openLibrary_lccn
|
bf.lookup_lccn=bookData.openLibrary_lccn
|
||||||
bf.caption='Add a Book'
|
bf.caption='Add a Book'
|
||||||
|
@ -101,7 +101,7 @@ def updateMenu():
|
||||||
|
|
||||||
def trashMenu():
|
def trashMenu():
|
||||||
(my,mx)=stdscr.getmaxyx()
|
(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 = browser.trashBrowser(w,hb)
|
||||||
b.refreshBooks()
|
b.refreshBooks()
|
||||||
b.eventLoop()
|
b.eventLoop()
|
||||||
|
@ -109,7 +109,7 @@ def trashMenu():
|
||||||
|
|
||||||
def browseMenu():
|
def browseMenu():
|
||||||
(my,mx)=stdscr.getmaxyx()
|
(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 = browser.bookBrowser(w,hb)
|
||||||
b.refreshBooks()
|
b.refreshBooks()
|
||||||
b.eventLoop()
|
b.eventLoop()
|
||||||
|
@ -117,7 +117,7 @@ def browseMenu():
|
||||||
|
|
||||||
def catMenu():
|
def catMenu():
|
||||||
(my,mx)=stdscr.getmaxyx()
|
(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 = browser.categoryBrowser(w,hb)
|
||||||
c.refreshCategories()
|
c.refreshCategories()
|
||||||
c.sortByColumn('category')
|
c.sortByColumn('category')
|
||||||
|
|
Loading…
Reference in New Issue