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):
66 if any((c == "\\" or c == '"' or c == "\n" for c in w)):
69 if c == "\\": wb += "\\\\"
70 elif c == '"': wb += '\\"'
71 elif c == "\n": wb += "\\n"
74 if w == "" or any((c.isspace() for c in w)):
82 def __init__(self, profile, libnm, id):
83 self.profile = profile
86 self.props = self.loadprops()
90 return lib.findlib(self.libnm).byid(self.id)
92 class memmanga(manga):
93 def __init__(self, profile, libnm, id):
94 super(memmanga, self).__init__(profile, libnm, id)
99 class filemanga(manga):
100 def __init__(self, profile, libnm, id, path):
102 super(filemanga, self).__init__(profile, libnm, id)
106 with openwdir(self.path) as f:
108 words = splitline(line)
109 if len(words) < 1: continue
110 if words[0] == "set" and len(words) > 2:
111 ret[words[1]] = words[2]
112 elif words[0] == "lset" and len(words) > 1:
113 ret[words[1]] = words[2:]
117 with openwdir(self.path, "w") as f:
118 for key, val in self.props.iteritems():
119 if isinstance(val, str):
120 f.write(consline("set", key, val) + "\n")
122 f.write(consline("lset", key, *val) + "\n")
124 class profile(object):
125 def __init__(self, dir):
129 def getmapping(self):
132 if os.path.exists(pj(self.dir, "map")):
133 with openwdir(pj(self.dir, "map")) as f:
135 words = splitline(ln)
138 if words[0] == "seq" and len(words) > 1:
143 elif words[0] == "manga" and len(words) > 3:
145 ret[words[1], words[2]] = int(words[3])
150 def savemapping(self, seq, m):
151 with openwdir(pj(self.dir, "map"), "w") as f:
152 f.write(consline("seq", str(seq)) + "\n")
153 for (libnm, id), num in m.iteritems():
154 f.write(consline("manga", libnm, id, str(num)) + "\n")
156 def getmanga(self, libnm, id, creat=False):
157 seq, m = self.getmapping()
159 return filemanga(self, libnm, id, pj(self.dir, "%i.manga" % m[(libnm, id)]))
161 raise KeyError("no such manga: (%s, %s)" % (libnm, id))
164 fp = openwdir(pj(self.dir, "%i.manga" % seq), "wx")
171 self.savemapping(seq, m)
172 return filemanga(self, libnm, id, pj(self.dir, "%i.manga" % seq))
175 if self.name is None:
176 raise ValueError("profile at " + self.dir + " has no name")
177 with openwdir(pj(basedir, "last"), "w") as f:
178 f.write(self.name + "\n")
180 def getaliases(self):
182 if os.path.exists(pj(self.dir, "alias")):
183 with openwdir(pj(self.dir, "alias")) as f:
186 if len(ln) < 1: continue
187 if ln[0] == "alias" and len(ln) > 3:
188 ret[ln[1]] = ln[2], ln[3]
191 def savealiases(self, map):
192 with openwdir(pj(self.dir, "alias"), "w") as f:
193 for nm, (libnm, id) in map.iteritems():
194 f.write(consline("alias", nm, libnm, id) + "\n")
196 def getalias(self, nm):
197 return self.getaliases()[nm]
199 def setalias(self, nm, libnm, id):
200 aliases = self.getaliases()
201 aliases[nm] = libnm, id
202 self.savealiases(aliases)
205 def byname(cls, name):
206 if not name or name == "last" or name[0] == '.':
207 raise KeyError("invalid profile name: " + name)
208 ret = cls(pj(basedir, name))
214 if not os.path.exists(pj(basedir, "last")):
215 raise KeyError("there is no last used profile")
216 with open(pj(basedir, "last")) as f:
217 return cls.byname(f.readline().strip())