4 home = os.getenv("HOME")
5 if home is None or not os.path.isdir(home):
6 raise Exception("Could not find home directory for profile keeping")
7 basedir = pj(home, ".manga", "profiles")
10 def __init__(self, name, mode):
12 self.tempname = name + ".new"
13 super(txfile, self).__init__(self.tempname, mode)
15 def close(self, abort=False):
16 super(txfile, self).close()
18 os.unlink(self.tempname)
20 os.rename(self.tempname, self.realname)
25 def __exit__(self, *exc_info):
26 if exc_info[0] is not None:
31 def openwdir(nm, mode="r"):
36 if os.path.exists(nm):
39 d = os.path.dirname(nm)
40 if not os.path.isdir(d):
46 if c == "\\": return "\\"
47 elif c == '"': return '"'
48 elif c == " ": return " "
49 elif c == "n": return "\n"
70 elif c == "\\" and p < len(line):
80 elif c == "\\" and p < len(line):
99 if any((c == "\\" or c == '"' or c == "\n" for c in w)):
102 if c == "\\": wb += "\\\\"
103 elif c == '"': wb += '\\"'
104 elif c == "\n": wb += "\\n"
107 if w == "" or any((c.isspace() for c in w)):
115 def __init__(self, profile, libnm, id):
116 self.profile = profile
119 self.props = self.loadprops()
123 return lib.findlib(self.libnm).byid(self.id)
128 class memmanga(manga):
129 def __init__(self, profile, libnm, id):
130 super(memmanga, self).__init__(profile, libnm, id)
135 class tagview(object):
136 def __init__(self, manga):
138 self.profile = manga.profile
140 def add(self, *tags):
141 mt = self.getall(self.profile)
142 ctags = mt.setdefault((self.manga.libnm, self.manga.id), set())
144 self.save(self.profile, mt)
146 def remove(self, *tags):
147 mt = self.getall(self.profile)
148 ctags = mt.get((self.manga.libnm, self.manga.id), set())
152 del mt[self.manga.libnm, self.manga.id]
155 self.save(self.profile, mt)
158 return iter(self.getall(self.profile).get((self.manga.libnm, self.manga.id), set()))
164 with profile.file("tags") as fp:
165 for words in splitlines(fp):
166 libnm, id = words[0:2]
167 tags = set(words[2:])
168 ret[libnm, id] = tags
174 def save(profile, m):
175 with profile.file("tags", "W") as fp:
176 for (libnm, id), tags in m.iteritems():
177 fp.write(consline(libnm, id, *tags) + "\n")
180 def bytag(profile, tag):
182 with profile.file("tags") as fp:
183 for words in splitlines(fp):
184 libnm, id = words[0:2]
187 yield profile.getmanga(libnm, id)
191 class filemanga(manga):
192 def __init__(self, profile, libnm, id, path):
194 super(filemanga, self).__init__(profile, libnm, id)
195 self.tags = tagview(self)
199 with openwdir(self.path) as f:
200 for words in splitlines(f):
201 if words[0] == "set" and len(words) > 2:
202 ret[words[1]] = words[2]
203 elif words[0] == "lset" and len(words) > 1:
204 ret[words[1]] = words[2:]
208 with openwdir(self.path, "W") as f:
209 for key, val in self.props.iteritems():
210 if isinstance(val, str):
211 f.write(consline("set", key, val) + "\n")
213 f.write(consline("lset", key, *val) + "\n")
215 class profile(object):
216 def __init__(self, dir):
220 def getmapping(self):
223 if os.path.exists(pj(self.dir, "map")):
224 with openwdir(pj(self.dir, "map")) as f:
225 for words in splitlines(f):
226 if words[0] == "seq" and len(words) > 1:
231 elif words[0] == "manga" and len(words) > 3:
233 ret[words[1], words[2]] = int(words[3])
238 def savemapping(self, seq, m):
239 with openwdir(pj(self.dir, "map"), "W") as f:
240 f.write(consline("seq", str(seq)) + "\n")
241 for (libnm, id), num in m.iteritems():
242 f.write(consline("manga", libnm, id, str(num)) + "\n")
244 def getmanga(self, libnm, id, creat=False):
245 seq, m = self.getmapping()
247 return filemanga(self, libnm, id, pj(self.dir, "%i.manga" % m[(libnm, id)]))
249 raise KeyError("no such manga: (%s, %s)" % (libnm, id))
252 fp = openwdir(pj(self.dir, "%i.manga" % seq), "wx")
259 self.savemapping(seq, m)
260 return filemanga(self, libnm, id, pj(self.dir, "%i.manga" % seq))
263 if self.name is None:
264 raise ValueError("profile at " + self.dir + " has no name")
265 with openwdir(pj(basedir, "last"), "W") as f:
266 f.write(self.name + "\n")
268 def getaliases(self):
270 if os.path.exists(pj(self.dir, "alias")):
271 with openwdir(pj(self.dir, "alias")) as f:
274 if len(ln) < 1: continue
275 if ln[0] == "alias" and len(ln) > 3:
276 ret[ln[1]] = ln[2], ln[3]
279 def savealiases(self, map):
280 with openwdir(pj(self.dir, "alias"), "W") as f:
281 for nm, (libnm, id) in map.iteritems():
282 f.write(consline("alias", nm, libnm, id) + "\n")
284 def file(self, name, mode="r"):
285 return openwdir(pj(self.dir, name), mode)
287 def getalias(self, nm):
288 return self.getaliases()[nm]
290 def setalias(self, nm, libnm, id):
291 aliases = self.getaliases()
292 aliases[nm] = libnm, id
293 self.savealiases(aliases)
295 def bytag(self, tag):
296 return tagview.bytag(self, tag)
299 def byname(cls, name):
300 if not name or name == "last" or name[0] == '.':
301 raise KeyError("invalid profile name: " + name)
302 ret = cls(pj(basedir, name))
308 if not os.path.exists(pj(basedir, "last")):
309 raise KeyError("there is no last used profile")
310 with open(pj(basedir, "last")) as f:
311 return cls.byname(f.readline().strip())