From 434236682e0d553965b2912d1d307e3b1473c491 Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Wed, 15 May 2013 04:15:15 +0200 Subject: [PATCH] Changed profile interface a bit. --- manga/profile.py | 45 ++++++++++++++++++++------------------------- manga/reader.py | 15 +++++++-------- 2 files changed, 27 insertions(+), 33 deletions(-) diff --git a/manga/profile.py b/manga/profile.py index 2ff6be5..2620a98 100644 --- a/manga/profile.py +++ b/manga/profile.py @@ -79,13 +79,28 @@ def consline(*words): return buf class manga(object): - def __init__(self, profile, libnm, id, path): + def __init__(self, profile, libnm, id): self.profile = profile self.libnm = libnm self.id = id - self.path = path self.props = self.loadprops() + def open(self): + import lib + return lib.findlib(self.libnm).byid(self.id) + +class memmanga(manga): + def __init__(self, profile, libnm, id): + super(memmanga, self).__init__(profile, libnm, id) + + def loadprops(self): + return {} + +class filemanga(manga): + def __init__(self, profile, libnm, id, path): + self.path = path + super(filemanga, self).__init__(profile, libnm, id) + def loadprops(self): ret = {} with openwdir(self.path) as f: @@ -98,23 +113,7 @@ class manga(object): ret[words[1]] = words[2:] return ret - def prop(self, key, default=KeyError): - if key not in self.props: - if default is KeyError: - raise KeyError(key) - return default - return self.props[key] - - def __getitem__(self, key): - return self.props[key] - - def __contains__(self, key): - return key in self.props - - def setprop(self, key, val): - self.props[key] = val - - def saveprops(self): + def save(self): with openwdir(self.path, "w") as f: for key, val in self.props.iteritems(): if isinstance(val, str): @@ -122,10 +121,6 @@ class manga(object): else: f.write(consline("lset", key, *val) + "\n") - def open(self): - import lib - return lib.findlib(self.libnm).byid(self.id) - class profile(object): def __init__(self, dir): self.dir = dir @@ -161,7 +156,7 @@ class profile(object): def getmanga(self, libnm, id, creat=False): seq, m = self.getmapping() if (libnm, id) in m: - return manga(self, libnm, id, pj(self.dir, "%i.manga" % m[(libnm, id)])) + return filemanga(self, libnm, id, pj(self.dir, "%i.manga" % m[(libnm, id)])) if not creat: raise KeyError("no such manga: (%s, %s)" % (libnm, id)) while True: @@ -174,7 +169,7 @@ class profile(object): fp.close() m[(libnm, id)] = seq self.savemapping(seq, m) - return manga(self, libnm, id, pj(self.dir, "%i.manga" % seq)) + return filemanga(self, libnm, id, pj(self.dir, "%i.manga" % seq)) def setlast(self): if self.name is None: diff --git a/manga/reader.py b/manga/reader.py index 12089e7..c5507ee 100644 --- a/manga/reader.py +++ b/manga/reader.py @@ -1,5 +1,5 @@ import threading, gtk, gio, gobject -import lib +import lib, profile class notdone(Exception): pass @@ -463,7 +463,7 @@ class sbox(gtk.ComboBox): self.rd.fetchpage(pageget(self.pnode[self.get_active()])) class reader(gtk.Window): - def __init__(self, manga, profile=None): + def __init__(self, manga, prof=None): super(reader, self).__init__(gtk.WINDOW_TOPLEVEL) self.connect("delete_event", lambda wdg, ev, data=None: False) self.connect("destroy", lambda wdg, data=None: self.quit()) @@ -472,7 +472,7 @@ class reader(gtk.Window): self.pagefetch = procslot(self) self.imgfetch = procslot(self) self.preload = procslot(self) - self.profile = profile + self.profile = prof if prof else profile.memmanga(None, None, manga.id) self.manga = manga self.page = None @@ -505,8 +505,8 @@ class reader(gtk.Window): self.add(vlay) vlay.show() - if self.profile and "curpage" in self.profile: - self.fetchpage(idpageget(self.manga, self.profile["curpage"])) + if "curpage" in self.profile.props: + self.fetchpage(idpageget(self.manga, self.profile.props["curpage"])) else: self.fetchpage(pageget(self.manga)) self.updtitle() @@ -548,9 +548,8 @@ class reader(gtk.Window): if self.point is not None: self.point = None if page is not None: - if self.profile: - self.profile.setprop("curpage", page.idlist()) - self.profile.saveprops() + self.profile.props["curpage"] = page.idlist() + self.profile.save() self.point = ccursor(page, self.cache) self.imgfetch.set(imgfetch(self.cache[page])) else: -- 2.11.0