From 3cc7937cd91ec6d3cfb7eebcd4c1afd85c5a615a Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Mon, 16 Nov 2015 02:57:51 +0100 Subject: [PATCH] Initial port of core code to Python3. --- automanga | 2 +- checkmanga | 2 +- manga/htcache.py | 23 ++++++++++------------- manga/lib.py | 11 ++++++----- manga/local.py | 6 +++--- manga/profile.py | 24 +++++++++++++++--------- manga/reader.py | 20 ++++++++++---------- 7 files changed, 46 insertions(+), 42 deletions(-) diff --git a/automanga b/automanga index b483bc0..bb6c4d5 100755 --- a/automanga +++ b/automanga @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 import sys, getopt import manga.lib, manga.reader, manga.profile, manga.local diff --git a/checkmanga b/checkmanga index 298cf4c..e174e3a 100755 --- a/checkmanga +++ b/checkmanga @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 import sys, getopt import manga.lib, manga.profile diff --git a/manga/htcache.py b/manga/htcache.py index 4212db3..45ede5d 100644 --- a/manga/htcache.py +++ b/manga/htcache.py @@ -1,4 +1,4 @@ -import os, md5, urllib, time +import os, hashlib, urllib.request, time pj = os.path.join class cache(object): @@ -6,30 +6,27 @@ class cache(object): self.dir = dir def mangle(self, url): - n = md5.new() - n.update(url) + n = hashlib.md5() + n.update(url.encode("ascii")) return n.hexdigest() def miss(self, url): - s = urllib.urlopen(url) - try: + with urllib.request.urlopen(url) as s: if s.headers.get("content-encoding") == "gzip": - import gzip, StringIO - return gzip.GzipFile(fileobj=StringIO.StringIO(s.read()), mode="r").read() + import gzip, io + return gzip.GzipFile(fileobj=io.BytesIO(s.read()), mode="r").read() return s.read() - finally: - s.close() - def fetch(self, url, expire = 3600): + def fetch(self, url, expire=3600): path = pj(self.dir, self.mangle(url)) if os.path.exists(path): if time.time() - os.stat(path).st_mtime < expire: - with open(path) as f: + with open(path, "rb") as f: return f.read() data = self.miss(url) if not os.path.isdir(self.dir): os.makedirs(self.dir) - with open(path, "w") as f: + with open(path, "wb") as f: f.write(data) return data @@ -38,5 +35,5 @@ if home is None or not os.path.isdir(home): raise Exception("Could not find home directory for HTTP caching") default = cache(pj(home, ".manga", "htcache")) -def fetch(url, expire = 3600): +def fetch(url, expire=3600): return default.fetch(url, expire) diff --git a/manga/lib.py b/manga/lib.py index 70b3ff9..c0a9f30 100644 --- a/manga/lib.py +++ b/manga/lib.py @@ -149,7 +149,7 @@ class imgstream(object): """Close this stream.""" raise NotImplementedError() - def read(self, sz = None): + def read(self, sz=None): """Read SZ bytes from the stream, or the entire rest of the stream of SZ is not given.""" raise NotImplementedError() @@ -159,8 +159,8 @@ class stdimgstream(imgstream): have no particular implementation requirements.""" def __init__(self, url): - import urllib - self.bk = urllib.urlopen(url) + import urllib.request + self.bk = urllib.request.urlopen(url) ok = False try: if self.bk.getcode() != 200: @@ -178,7 +178,7 @@ class stdimgstream(imgstream): def close(self): self.bk.close() - def read(self, sz = None): + def read(self, sz=None): if sz is None: return self.bk.read() else: @@ -218,7 +218,8 @@ class cursor(object): loaded = {} def findlib(name): def load(name): - mod = __import__(name, fromlist=["dummy"]) + import importlib + mod = importlib.import_module(name) if not hasattr(mod, "library"): raise ImportError("module " + name + " is not a manga library") return mod.library() diff --git a/manga/local.py b/manga/local.py index 2d79b20..7e21a9b 100644 --- a/manga/local.py +++ b/manga/local.py @@ -1,5 +1,5 @@ import os -import lib +from . import lib pj = os.path.join def decode1(nm): @@ -95,9 +95,9 @@ class manga(lib.manga): self.stack = [] if os.path.exists(pj(self.path, "name")): with open(pj(self.path, "name")) as s: - self.name = s.readline().strip().decode("utf-8") + self.name = s.readline().strip() else: - self.name = os.path.basename(path).decode("utf-8") + self.name = os.path.basename(path) self.direct = self.destruct() def __len__(self): diff --git a/manga/profile.py b/manga/profile.py index f304252..081cd6c 100644 --- a/manga/profile.py +++ b/manga/profile.py @@ -6,19 +6,25 @@ if home is None or not os.path.isdir(home): raise Exception("Could not find home directory for profile keeping") basedir = pj(home, ".manga", "profiles") -class txfile(file): +class txfile(object): def __init__(self, name, mode): self.realname = name self.tempname = name + ".new" - super(txfile, self).__init__(self.tempname, mode) + self.bk = open(self.tempname, mode) def close(self, abort=False): - super(txfile, self).close() + self.bk.close() if abort: os.unlink(self.tempname) else: os.rename(self.tempname, self.realname) + def read(self, sz=-1): + return self.bk.read(sz) + + def write(self, data): + return self.bk.write(data) + def __enter__(self): return self @@ -29,7 +35,7 @@ class txfile(file): self.close(False) def openwdir(nm, mode="r"): - ft = file + ft = open if mode == "W": mode = "w" ft = txfile @@ -119,7 +125,7 @@ class manga(object): self.props = self.loadprops() def open(self): - import lib + from . import lib return lib.findlib(self.libnm).byid(self.id) def save(self): @@ -173,7 +179,7 @@ class tagview(object): @staticmethod def save(profile, m): with profile.file("tags", "W") as fp: - for (libnm, id), tags in m.iteritems(): + for (libnm, id), tags in m.items(): fp.write(consline(libnm, id, *tags) + "\n") @staticmethod @@ -206,7 +212,7 @@ class filemanga(manga): def save(self): with openwdir(self.path, "W") as f: - for key, val in self.props.iteritems(): + for key, val in self.props.items(): if isinstance(val, str): f.write(consline("set", key, val) + "\n") else: @@ -238,7 +244,7 @@ class profile(object): def savemapping(self, seq, m): with openwdir(pj(self.dir, "map"), "W") as f: f.write(consline("seq", str(seq)) + "\n") - for (libnm, id), num in m.iteritems(): + for (libnm, id), num in m.items(): f.write(consline("manga", libnm, id, str(num)) + "\n") def getmanga(self, libnm, id, creat=False): @@ -278,7 +284,7 @@ class profile(object): def savealiases(self, map): with openwdir(pj(self.dir, "alias"), "W") as f: - for nm, (libnm, id) in map.iteritems(): + for nm, (libnm, id) in map.items(): f.write(consline("alias", nm, libnm, id) + "\n") def file(self, name, mode="r"): diff --git a/manga/reader.py b/manga/reader.py index 0e870eb..c957b13 100644 --- a/manga/reader.py +++ b/manga/reader.py @@ -1,6 +1,6 @@ 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 @@ -12,13 +12,13 @@ class future(threading.Thread): self._val = None self._exc = None self._notlist = [] - self._started = False + self._tstarted = False self.setDaemon(True) def start(self): - if not self._started: + if not self._tstarted: super(future, self).start() - self._started = True + self._tstarted = True def run(self): try: @@ -84,7 +84,7 @@ class imgload(future): self.st = st while True: read = st.read(1024) - if read == "": + if read == b"": break self.p += len(read) buf.write(read) @@ -520,7 +520,7 @@ class reader(gtk.Window): self.pfr.show() self.sboxbar = gtk.HBox() algn = gtk.Alignment(xalign=0.0, yalign=0.5, xscale=0.0, yscale=0.0) - sboxlbl = gtk.Label(self.manga.name + u": ") + sboxlbl = gtk.Label(self.manga.name + ": ") algn.add(sboxlbl) sboxlbl.show() self.sboxbar.pack_start(algn, False, True, 0) @@ -553,7 +553,7 @@ class reader(gtk.Window): 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] @@ -562,10 +562,10 @@ class reader(gtk.Window): 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] - 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) @@ -602,7 +602,7 @@ class reader(gtk.Window): 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): -- 2.11.0