Raise slightly more helpful error on idpageget failure.
[automanga.git] / manga / reader.py
index 442f1f8..0940a6c 100644 (file)
@@ -1,6 +1,6 @@
 import threading
 from gi.repository import Gtk as gtk, GdkPixbuf as gdkpix, Gdk as gdk, GObject as gobject
 import threading
 from gi.repository import Gtk as gtk, GdkPixbuf as gdkpix, Gdk as gdk, GObject as gobject
-import lib, profile
+from . import lib, profile
 
 class notdone(Exception): pass
 
 
 class notdone(Exception): pass
 
@@ -12,13 +12,13 @@ class future(threading.Thread):
         self._val = None
         self._exc = None
         self._notlist = []
         self._val = None
         self._exc = None
         self._notlist = []
-        self._started = False
+        self._tstarted = False
         self.setDaemon(True)
 
     def start(self):
         self.setDaemon(True)
 
     def start(self):
-        if not self._started:
+        if not self._tstarted:
             super(future, self).start()
             super(future, self).start()
-            self._started = True
+            self._tstarted = True
 
     def run(self):
         try:
 
     def run(self):
         try:
@@ -77,20 +77,26 @@ class imgload(future):
 
     def value(self):
         buf = gdkpix.PixbufLoader()
 
     def value(self):
         buf = gdkpix.PixbufLoader()
+        done = False
         try:
             with self.page.open() as st:
                 self.p = 0
                 self.st = st
                 while True:
                     read = st.read(1024)
         try:
             with self.page.open() as st:
                 self.p = 0
                 self.st = st
                 while True:
                     read = st.read(1024)
-                    if read == "":
+                    if read == b"":
                         break
                     self.p += len(read)
                     buf.write(read)
                     self.progcb()
             self.st = None
                         break
                     self.p += len(read)
                     buf.write(read)
                     self.progcb()
             self.st = None
+            done = True
         finally:
         finally:
-            buf.close()
+            try:
+                buf.close()
+            except:
+                if done:
+                    raise
         return buf.get_pixbuf()
 
     @property
         return buf.get_pixbuf()
 
     @property
@@ -151,7 +157,10 @@ class idpageget(future):
         self.idlist = idlist
 
     def value(self):
         self.idlist = idlist
 
     def value(self):
-        return lib.cursor(self.bnode.byidlist(self.idlist)).cur
+        try:
+            return lib.cursor(self.bnode.byidlist(self.idlist)).cur
+        except KeyError:
+            raise KeyError("could not find last read page: " + repr(self.idlist))
 
 class pageget(future):
     def __init__(self, fnode):
 
 class pageget(future):
     def __init__(self, fnode):
@@ -295,7 +304,7 @@ class pageview(gtk.Widget):
 class msgproc(object):
     def attach(self, reader):
         self.rd = reader
 class msgproc(object):
     def attach(self, reader):
         self.rd = reader
-        self.msg = gtk.Alignment(yalign=0.5)
+        self.msg = gtk.Alignment(xalign=0.0, yalign=0.5, xscale=0.0, yscale=0.0)
         self.hlay = gtk.HBox()
         self.lbl = gtk.Label("")
         self.hlay.pack_start(self.lbl, True, True, 0)
         self.hlay = gtk.HBox()
         self.lbl = gtk.Label("")
         self.hlay.pack_start(self.lbl, True, True, 0)
@@ -513,8 +522,8 @@ class reader(gtk.Window):
         vlay.pack_start(self.pfr, True, True, 0)
         self.pfr.show()
         self.sboxbar = gtk.HBox()
         vlay.pack_start(self.pfr, True, True, 0)
         self.pfr.show()
         self.sboxbar = gtk.HBox()
-        algn = gtk.Alignment(yalign=0.5)
-        sboxlbl = gtk.Label(self.manga.name + u": ")
+        algn = gtk.Alignment(xalign=0.0, yalign=0.5, xscale=0.0, yscale=0.0)
+        sboxlbl = gtk.Label(self.manga.name + ": ")
         algn.add(sboxlbl)
         sboxlbl.show()
         self.sboxbar.pack_start(algn, False, True, 0)
         algn.add(sboxlbl)
         sboxlbl.show()
         self.sboxbar.pack_start(algn, False, True, 0)
@@ -523,7 +532,7 @@ class reader(gtk.Window):
         self.sboxbar.show()
         self.sbar = gtk.HBox()
         self.pagelbl = gtk.Label("")
         self.sboxbar.show()
         self.sbar = gtk.HBox()
         self.pagelbl = gtk.Label("")
-        algn = gtk.Alignment(yalign=0.5)
+        algn = gtk.Alignment(xalign=0.0, yalign=0.5, xscale=0.0, yscale=0.0)
         algn.add(self.pagelbl)
         self.pagelbl.show()
         self.sbar.pack_start(algn, True, True, 0)
         algn.add(self.pagelbl)
         self.pagelbl.show()
         self.sbar.pack_start(algn, True, True, 0)
@@ -547,7 +556,7 @@ class reader(gtk.Window):
             self.pagelbl.set_text("")
         else:
             w, h = self.page.get_osize()
             self.pagelbl.set_text("")
         else:
             w, h = self.page.get_osize()
-            self.pagelbl.set_text(u"%s\u00d7%s (%d%%)" % (w, h, int(self.page.zoom * 100)))
+            self.pagelbl.set_text("%s\u00d7%s (%d%%)" % (w, h, int(self.page.zoom * 100)))
 
     def updsboxes(self, page):
         nodes = [node for node, idx in page.stack[1:]] + [page]
 
     def updsboxes(self, page):
         nodes = [node for node, idx in page.stack[1:]] + [page]
@@ -556,10 +565,10 @@ class reader(gtk.Window):
             if pbox.node != node:
                 l = i
                 break
             if pbox.node != node:
                 l = i
                 break
-        for i in xrange(l, len(self.sboxes)):
+        for i in range(l, len(self.sboxes)):
             self.sboxbar.remove(self.sboxes[i])
         self.sboxes = self.sboxes[:l]
             self.sboxbar.remove(self.sboxes[i])
         self.sboxes = self.sboxes[:l]
-        for i in xrange(l, len(nodes)):
+        for i in range(l, len(nodes)):
             new = sbox(self, nodes[i])
             self.sboxbar.pack_start(new, False, True, 5)
             self.sboxes.append(new)
             new = sbox(self, nodes[i])
             self.sboxbar.pack_start(new, False, True, 5)
             self.sboxes.append(new)
@@ -596,7 +605,7 @@ class reader(gtk.Window):
         return proc
 
     def updtitle(self):
         return proc
 
     def updtitle(self):
-        self.set_title(u"Automanga \u2013 " + self.manga.name)
+        self.set_title("Automanga \u2013 " + self.manga.name)
 
     @property
     def zoom(self):
 
     @property
     def zoom(self):