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)
95 class memmanga(manga):
96 def __init__(self, profile, libnm, id):
97 super(memmanga, self).__init__(profile, libnm, id)
102 class filemanga(manga):
103 def __init__(self, profile, libnm, id, path):
105 super(filemanga, self).__init__(profile, libnm, id)
109 with openwdir(self.path) as f:
111 words = splitline(line)
112 if len(words) < 1: continue
113 if words[0] == "set" and len(words) > 2:
114 ret[words[1]] = words[2]
115 elif words[0] == "lset" and len(words) > 1:
116 ret[words[1]] = words[2:]
120 with openwdir(self.path, "w") as f:
121 for key, val in self.props.iteritems():
122 if isinstance(val, str):
123 f.write(consline("set", key, val) + "\n")
125 f.write(consline("lset", key, *val) + "\n")
127 class profile(object):
128 def __init__(self, dir):
132 def getmapping(self):
135 if os.path.exists(pj(self.dir, "map")):
136 with openwdir(pj(self.dir, "map")) as f:
138 words = splitline(ln)
141 if words[0] == "seq" and len(words) > 1:
146 elif words[0] == "manga" and len(words) > 3:
148 ret[words[1], words[2]] = int(words[3])
153 def savemapping(self, seq, m):
154 with openwdir(pj(self.dir, "map"), "w") as f:
155 f.write(consline("seq", str(seq)) + "\n")
156 for (libnm, id), num in m.iteritems():
157 f.write(consline("manga", libnm, id, str(num)) + "\n")
159 def getmanga(self, libnm, id, creat=False):
160 seq, m = self.getmapping()
162 return filemanga(self, libnm, id, pj(self.dir, "%i.manga" % m[(libnm, id)]))
164 raise KeyError("no such manga: (%s, %s)" % (libnm, id))
167 fp = openwdir(pj(self.dir, "%i.manga" % seq), "wx")
174 self.savemapping(seq, m)
175 return filemanga(self, libnm, id, pj(self.dir, "%i.manga" % seq))
178 if self.name is None:
179 raise ValueError("profile at " + self.dir + " has no name")
180 with openwdir(pj(basedir, "last"), "w") as f:
181 f.write(self.name + "\n")
183 def getaliases(self):
185 if os.path.exists(pj(self.dir, "alias")):
186 with openwdir(pj(self.dir, "alias")) as f:
189 if len(ln) < 1: continue
190 if ln[0] == "alias" and len(ln) > 3:
191 ret[ln[1]] = ln[2], ln[3]
194 def savealiases(self, map):
195 with openwdir(pj(self.dir, "alias"), "w") as f:
196 for nm, (libnm, id) in map.iteritems():
197 f.write(consline("alias", nm, libnm, id) + "\n")
199 def file(self, name, mode="r"):
200 return openwdir(pj(self.dir, name), mode)
202 def getalias(self, nm):
203 return self.getaliases()[nm]
205 def setalias(self, nm, libnm, id):
206 aliases = self.getaliases()
207 aliases[nm] = libnm, id
208 self.savealiases(aliases)
211 def byname(cls, name):
212 if not name or name == "last" or name[0] == '.':
213 raise KeyError("invalid profile name: " + name)
214 ret = cls(pj(basedir, name))
220 if not os.path.exists(pj(basedir, "last")):
221 raise KeyError("there is no last used profile")
222 with open(pj(basedir, "last")) as f:
223 return cls.byname(f.readline().strip())