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 self.bk = open(self.tempname, mode)
15 def close(self, abort=False):
18 os.unlink(self.tempname)
20 os.rename(self.tempname, self.realname)
22 def read(self, sz=-1):
23 return self.bk.read(sz)
25 def write(self, data):
26 return self.bk.write(data)
31 def __exit__(self, *exc_info):
32 if exc_info[0] is not None:
37 def openwdir(nm, mode="r"):
42 if os.path.exists(nm):
45 d = os.path.dirname(nm)
46 if not os.path.isdir(d):
52 if c == "\\": return "\\"
53 elif c == '"': return '"'
54 elif c == " ": return " "
55 elif c == "n": return "\n"
76 elif c == "\\" and p < len(line):
86 elif c == "\\" and p < len(line):
102 def consline(*words):
105 if any((c == "\\" or c == '"' or c == "\n" for c in w)):
108 if c == "\\": wb += "\\\\"
109 elif c == '"': wb += '\\"'
110 elif c == "\n": wb += "\\n"
113 if w == "" or any((c.isspace() for c in w)):
121 def __init__(self, profile, libnm, id):
122 self.profile = profile
125 self.props = self.loadprops()
129 return lib.findlib(self.libnm).byid(self.id)
134 class memmanga(manga):
135 def __init__(self, profile, libnm, id):
136 super(memmanga, self).__init__(profile, libnm, id)
141 class tagview(object):
142 def __init__(self, manga):
144 self.profile = manga.profile
146 def add(self, *tags):
147 mt = self.getall(self.profile)
148 ctags = mt.setdefault((self.manga.libnm, self.manga.id), set())
150 self.save(self.profile, mt)
152 def remove(self, *tags):
153 mt = self.getall(self.profile)
154 ctags = mt.get((self.manga.libnm, self.manga.id), set())
158 del mt[self.manga.libnm, self.manga.id]
161 self.save(self.profile, mt)
164 return iter(self.getall(self.profile).get((self.manga.libnm, self.manga.id), set()))
170 with profile.file("tags") as fp:
171 for words in splitlines(fp):
172 libnm, id = words[0:2]
173 tags = set(words[2:])
174 ret[libnm, id] = tags
180 def save(profile, m):
181 with profile.file("tags", "W") as fp:
182 for (libnm, id), tags in m.items():
183 fp.write(consline(libnm, id, *tags) + "\n")
186 def bytag(profile, tag):
188 with profile.file("tags") as fp:
189 for words in splitlines(fp):
190 libnm, id = words[0:2]
193 yield profile.getmanga(libnm, id)
197 class filemanga(manga):
198 def __init__(self, profile, libnm, id, path):
200 super(filemanga, self).__init__(profile, libnm, id)
201 self.tags = tagview(self)
205 with openwdir(self.path) as f:
206 for words in splitlines(f):
207 if words[0] == "set" and len(words) > 2:
208 ret[words[1]] = words[2]
209 elif words[0] == "lset" and len(words) > 1:
210 ret[words[1]] = words[2:]
214 with openwdir(self.path, "W") as f:
215 for key, val in self.props.items():
216 if isinstance(val, str):
217 f.write(consline("set", key, val) + "\n")
219 f.write(consline("lset", key, *val) + "\n")
221 class profile(object):
222 def __init__(self, dir):
226 def getmapping(self):
229 if os.path.exists(pj(self.dir, "map")):
230 with openwdir(pj(self.dir, "map")) as f:
231 for words in splitlines(f):
232 if words[0] == "seq" and len(words) > 1:
237 elif words[0] == "manga" and len(words) > 3:
239 ret[words[1], words[2]] = int(words[3])
244 def savemapping(self, seq, m):
245 with openwdir(pj(self.dir, "map"), "W") as f:
246 f.write(consline("seq", str(seq)) + "\n")
247 for (libnm, id), num in m.items():
248 f.write(consline("manga", libnm, id, str(num)) + "\n")
250 def getmanga(self, libnm, id, creat=False):
251 seq, m = self.getmapping()
253 return filemanga(self, libnm, id, pj(self.dir, "%i.manga" % m[(libnm, id)]))
255 raise KeyError("no such manga: (%s, %s)" % (libnm, id))
258 fp = openwdir(pj(self.dir, "%i.manga" % seq), "wx")
265 self.savemapping(seq, m)
266 return filemanga(self, libnm, id, pj(self.dir, "%i.manga" % seq))
269 if self.name is None:
270 raise ValueError("profile at " + self.dir + " has no name")
271 with openwdir(pj(basedir, "last"), "W") as f:
272 f.write(self.name + "\n")
274 def getaliases(self):
276 if os.path.exists(pj(self.dir, "alias")):
277 with openwdir(pj(self.dir, "alias")) as f:
280 if len(ln) < 1: continue
281 if ln[0] == "alias" and len(ln) > 3:
282 ret[ln[1]] = ln[2], ln[3]
285 def savealiases(self, map):
286 with openwdir(pj(self.dir, "alias"), "W") as f:
287 for nm, (libnm, id) in map.items():
288 f.write(consline("alias", nm, libnm, id) + "\n")
290 def file(self, name, mode="r"):
291 return openwdir(pj(self.dir, name), mode)
293 def getalias(self, nm):
294 return self.getaliases()[nm]
296 def setalias(self, nm, libnm, id):
297 aliases = self.getaliases()
298 aliases[nm] = libnm, id
299 self.savealiases(aliases)
301 def bytag(self, tag):
302 return tagview.bytag(self, tag)
305 def byname(cls, name):
306 if not name or name == "last" or name[0] == '.':
307 raise KeyError("invalid profile name: " + name)
308 ret = cls(pj(basedir, name))
314 if not os.path.exists(pj(basedir, "last")):
315 raise KeyError("there is no last used profile")
316 with open(pj(basedir, "last")) as f:
317 return cls.byname(f.readline().strip())