X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=manga%2Fprofile.py;h=081cd6c58ff0af1de389fde728c79a8fba725ff5;hb=531e4473ccfc214cb30582905b7dea23d011d501;hp=85e47b390cfd756c7810b28cbdca02e6973bafd0;hpb=f03018e9fefd3eb81cf3b59f973e6c94b3fcf0e0;p=automanga.git diff --git a/manga/profile.py b/manga/profile.py index 85e47b3..081cd6c 100644 --- a/manga/profile.py +++ b/manga/profile.py @@ -6,14 +6,46 @@ 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(object): + def __init__(self, name, mode): + self.realname = name + self.tempname = name + ".new" + self.bk = open(self.tempname, mode) + + def close(self, abort=False): + 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 + + def __exit__(self, *exc_info): + if exc_info[0] is not None: + self.close(True) + else: + self.close(False) + def openwdir(nm, mode="r"): + ft = open + if mode == "W": + mode = "w" + ft = txfile if os.path.exists(nm): - return open(nm, mode) + return ft(nm, mode) if mode != "r": d = os.path.dirname(nm) if not os.path.isdir(d): os.makedirs(d) - return open(nm, mode) + return ft(nm, mode) def splitline(line): def bsq(c): @@ -93,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): @@ -146,8 +178,8 @@ class tagview(object): @staticmethod def save(profile, m): - with profile.file("tags", "w") as fp: - for (libnm, id), tags in m.iteritems(): + with profile.file("tags", "W") as fp: + for (libnm, id), tags in m.items(): fp.write(consline(libnm, id, *tags) + "\n") @staticmethod @@ -179,8 +211,8 @@ class filemanga(manga): return ret def save(self): - with openwdir(self.path, "w") as f: - for key, val in self.props.iteritems(): + with openwdir(self.path, "W") as f: + for key, val in self.props.items(): if isinstance(val, str): f.write(consline("set", key, val) + "\n") else: @@ -210,9 +242,9 @@ class profile(object): return seq, ret def savemapping(self, seq, m): - with openwdir(pj(self.dir, "map"), "w") as f: + 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): @@ -236,7 +268,7 @@ class profile(object): def setlast(self): if self.name is None: raise ValueError("profile at " + self.dir + " has no name") - with openwdir(pj(basedir, "last"), "w") as f: + with openwdir(pj(basedir, "last"), "W") as f: f.write(self.name + "\n") def getaliases(self): @@ -251,8 +283,8 @@ class profile(object): return ret def savealiases(self, map): - with openwdir(pj(self.dir, "alias"), "w") as f: - for nm, (libnm, id) in map.iteritems(): + with openwdir(pj(self.dir, "alias"), "W") as f: + for nm, (libnm, id) in map.items(): f.write(consline("alias", nm, libnm, id) + "\n") def file(self, name, mode="r"):