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")
9 def openwdir(nm, mode="r"):
10 if os.path.exists(nm):
13 d = os.path.dirname(nm)
14 if not os.path.isdir(d):
20 if c == "\\": return "\\"
21 elif c == '"': return '"'
22 elif c == " ": return " "
23 elif c == "n": return "\n"
44 elif c == "\\" and p < len(line):
54 elif c == "\\" and p < len(line):
73 if any((c == "\\" or c == '"' or c == "\n" for c in w)):
76 if c == "\\": wb += "\\\\"
77 elif c == '"': wb += '\\"'
78 elif c == "\n": wb += "\\n"
81 if w == "" or any((c.isspace() for c in w)):
89 def __init__(self, profile, libnm, id):
90 self.profile = profile
93 self.props = self.loadprops()
97 return lib.findlib(self.libnm).byid(self.id)
102 class memmanga(manga):
103 def __init__(self, profile, libnm, id):
104 super(memmanga, self).__init__(profile, libnm, id)
109 class tagview(object):
110 def __init__(self, manga):
112 self.profile = manga.profile
114 def add(self, *tags):
115 mt = self.getall(self.profile)
116 ctags = mt.setdefault((self.manga.libnm, self.manga.id), set())
118 self.save(self.profile, mt)
120 def remove(self, *tags):
121 mt = self.getall(self.profile)
122 ctags = mt.get((self.manga.libnm, self.manga.id), set())
126 del mt[self.manga.libnm, self.manga.id]
129 self.save(self.profile, mt)
132 return iter(self.getall(self.profile).get((self.manga.libnm, self.manga.id), set()))
138 with profile.file("tags") as fp:
139 for words in splitlines(fp):
140 libnm, id = words[0:2]
141 tags = set(words[2:])
142 ret[libnm, id] = tags
148 def save(profile, m):
149 with profile.file("tags", "w") as fp:
150 for (libnm, id), tags in m.iteritems():
151 fp.write(consline(libnm, id, *tags) + "\n")
154 def bytag(profile, tag):
156 with profile.file("tags") as fp:
157 for words in splitlines(fp):
158 libnm, id = words[0:2]
161 yield profile.getmanga(libnm, id)
165 class filemanga(manga):
166 def __init__(self, profile, libnm, id, path):
168 super(filemanga, self).__init__(profile, libnm, id)
169 self.tags = tagview(self)
173 with openwdir(self.path) as f:
174 for words in splitlines(f):
175 if words[0] == "set" and len(words) > 2:
176 ret[words[1]] = words[2]
177 elif words[0] == "lset" and len(words) > 1:
178 ret[words[1]] = words[2:]
182 with openwdir(self.path, "w") as f:
183 for key, val in self.props.iteritems():
184 if isinstance(val, str):
185 f.write(consline("set", key, val) + "\n")
187 f.write(consline("lset", key, *val) + "\n")
189 class profile(object):
190 def __init__(self, dir):
194 def getmapping(self):
197 if os.path.exists(pj(self.dir, "map")):
198 with openwdir(pj(self.dir, "map")) as f:
199 for words in splitlines(f):
200 if words[0] == "seq" and len(words) > 1:
205 elif words[0] == "manga" and len(words) > 3:
207 ret[words[1], words[2]] = int(words[3])
212 def savemapping(self, seq, m):
213 with openwdir(pj(self.dir, "map"), "w") as f:
214 f.write(consline("seq", str(seq)) + "\n")
215 for (libnm, id), num in m.iteritems():
216 f.write(consline("manga", libnm, id, str(num)) + "\n")
218 def getmanga(self, libnm, id, creat=False):
219 seq, m = self.getmapping()
221 return filemanga(self, libnm, id, pj(self.dir, "%i.manga" % m[(libnm, id)]))
223 raise KeyError("no such manga: (%s, %s)" % (libnm, id))
226 fp = openwdir(pj(self.dir, "%i.manga" % seq), "wx")
233 self.savemapping(seq, m)
234 return filemanga(self, libnm, id, pj(self.dir, "%i.manga" % seq))
237 if self.name is None:
238 raise ValueError("profile at " + self.dir + " has no name")
239 with openwdir(pj(basedir, "last"), "w") as f:
240 f.write(self.name + "\n")
242 def getaliases(self):
244 if os.path.exists(pj(self.dir, "alias")):
245 with openwdir(pj(self.dir, "alias")) as f:
248 if len(ln) < 1: continue
249 if ln[0] == "alias" and len(ln) > 3:
250 ret[ln[1]] = ln[2], ln[3]
253 def savealiases(self, map):
254 with openwdir(pj(self.dir, "alias"), "w") as f:
255 for nm, (libnm, id) in map.iteritems():
256 f.write(consline("alias", nm, libnm, id) + "\n")
258 def file(self, name, mode="r"):
259 return openwdir(pj(self.dir, name), mode)
261 def getalias(self, nm):
262 return self.getaliases()[nm]
264 def setalias(self, nm, libnm, id):
265 aliases = self.getaliases()
266 aliases[nm] = libnm, id
267 self.savealiases(aliases)
269 def bytag(self, tag):
270 return tagview.bytag(self, tag)
273 def byname(cls, name):
274 if not name or name == "last" or name[0] == '.':
275 raise KeyError("invalid profile name: " + name)
276 ret = cls(pj(basedir, name))
282 if not os.path.exists(pj(basedir, "last")):
283 raise KeyError("there is no last used profile")
284 with open(pj(basedir, "last")) as f:
285 return cls.byname(f.readline().strip())