X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=manga%2Fprofile.py;h=e8414a1afbd5548a1fdb23623413e5f99a513e7d;hb=5997ac775166eef814b46dc8d5df7f9961397c28;hp=87f02a9e5fffadebbe6c682f9c7ae87c2e97a77f;hpb=012c4cae7ca181ac6a6fbb85c8f808cc1259470c;p=automanga.git diff --git a/manga/profile.py b/manga/profile.py index 87f02a9..e8414a1 100644 --- a/manga/profile.py +++ b/manga/profile.py @@ -79,13 +79,31 @@ 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) + + def save(self): + pass + +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 +116,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 +124,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 +159,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 +172,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: @@ -182,6 +180,30 @@ class profile(object): with openwdir(pj(basedir, "last"), "w") as f: f.write(self.name + "\n") + def getaliases(self): + ret = {} + if os.path.exists(pj(self.dir, "alias")): + with openwdir(pj(self.dir, "alias")) as f: + for ln in f: + ln = splitline(ln) + if len(ln) < 1: continue + if ln[0] == "alias" and len(ln) > 3: + ret[ln[1]] = ln[2], ln[3] + return ret + + def savealiases(self, map): + with openwdir(pj(self.dir, "alias"), "w") as f: + for nm, (libnm, id) in map.iteritems(): + f.write(consline("alias", nm, libnm, id) + "\n") + + def getalias(self, nm): + return self.getaliases()[nm] + + def setalias(self, nm, libnm, id): + aliases = self.getaliases() + aliases[nm] = libnm, id + self.savealiases(aliases) + @classmethod def byname(cls, name): if not name or name == "last" or name[0] == '.':