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, path):
83 self.profile = profile
87 self.props = self.loadprops()
91 with openwdir(self.path) as f:
93 words = splitline(line)
94 if len(words) < 1: continue
95 if words[0] == "set" and len(words) > 2:
96 ret[words[1]] = words[2]
97 elif words[0] == "lset" and len(words) > 1:
98 ret[words[1]] = words[2:]
101 def prop(self, key, default=KeyError):
102 if key not in self.props:
103 if default is KeyError:
106 return self.props[key]
108 def __getitem__(self, key):
109 return self.props[key]
111 def __contains__(self, key):
112 return key in self.props
114 def setprop(self, key, val):
115 self.props[key] = val
118 with openwdir(self.path, "w") as f:
119 for key, val in self.props.iteritems():
120 if isinstance(val, str):
121 f.write(consline("set", key, val) + "\n")
123 f.write(consline("lset", key, *val) + "\n")
127 return lib.findlib(self.libnm).byid(self.id)
129 class profile(object):
130 def __init__(self, dir):
134 def getmapping(self):
137 if os.path.exists(pj(self.dir, "map")):
138 with openwdir(pj(self.dir, "map")) as f:
140 words = splitline(ln)
143 if words[0] == "seq" and len(words) > 1:
148 elif words[0] == "manga" and len(words) > 3:
150 ret[words[1], words[2]] = int(words[3])
155 def savemapping(self, seq, m):
156 with openwdir(pj(self.dir, "map"), "w") as f:
157 f.write(consline("seq", str(seq)) + "\n")
158 for (libnm, id), num in m.iteritems():
159 f.write(consline("manga", libnm, id, str(num)) + "\n")
161 def getmanga(self, libnm, id, creat=False):
162 seq, m = self.getmapping()
164 return manga(self, libnm, id, pj(self.dir, "%i.manga" % m[(libnm, id)]))
166 raise KeyError("no such manga: (%s, %s)" % (libnm, id))
169 fp = openwdir(pj(self.dir, "%i.manga" % seq), "wx")
176 self.savemapping(seq, m)
177 return manga(self, libnm, id, pj(self.dir, "%i.manga" % seq))
180 if self.name is None:
181 raise ValueError("profile at " + self.dir + " has no name")
182 with openwdir(pj(basedir, "last"), "w") as f:
183 f.write(self.name + "\n")
186 def byname(cls, name):
187 if not name or name == "last" or name[0] == '.':
188 raise KeyError("invalid profile name: " + name)
189 ret = cls(pj(basedir, name))
195 if not os.path.exists(pj(basedir, "last")):
196 raise KeyError("there is no last used profile")
197 with open(pj(basedir, "last")) as f:
198 return cls.byname(f.readline().strip())