12 while p < len(nm) and nm[p].isdigit():
18 while p < len(nm) and nm[p].isalpha():
35 def findname(names, files):
36 matches = list(names.keys())
38 matches = [pfx for pfx in matches if f.startswith(pfx)]
39 if len(matches) < 1: return None
40 matches.sort(key=len, reverse=True)
41 return names[matches[0]]
44 nmpath = pj(path, "names")
45 if not os.path.exists(nmpath):
48 with open(nmpath, "r") as fp:
53 ret[line[:p]] = line[p + 1:]
56 class imgstream(lib.imgstream):
57 def __init__(self, path):
58 self.bk = open(path, 'rb')
59 self.clen = os.fstat(self.bk.fileno()).st_size
64 def read(self, sz=None):
65 return self.bk.read(sz)
68 def __init__(self, manga, path, name, id, stack):
76 return imgstream(self.path)
78 class interm(lib.pagelist):
79 def __init__(self, name, id, stack, direct):
86 return len(self.direct)
88 def __getitem__(self, n):
94 s = genstr(decode1(dent))
99 for p, n in zip(mx, s):
107 class manga(lib.manga):
108 exts = ["jpg", "jpeg", "png", "gif"]
110 def __init__(self, path):
111 path = os.path.abspath(path)
112 if not os.path.isdir(path):
113 raise IOError("No such directory: " + path)
117 if os.path.exists(pj(self.path, "name")):
118 with open(pj(self.path, "name")) as s:
119 self.name = s.readline().strip()
121 self.name = os.path.basename(path)
122 self.direct = self.destruct()
125 return len(self.direct)
127 def __getitem__(self, idx):
128 return self.direct[idx]
131 if os.path.exists(pj(self.path, "order")):
132 with open(pj(self.path, "order")) as s:
133 return True, [line.strip() for line in s if os.path.exists(pj(self.path, line.strip()))]
135 return False, [dent for dent in os.listdir(self.path) if '.' in dent and dent[dent.rindex('.') + 1:] in self.exts]
137 def bakenames(self, files):
143 nm = nm[:nm.rindex('.')]
149 ordered, files = self.imglist()
150 pages, orig = self.bakenames(files)
151 mx = maxstruct(pages)
153 raise TypeError("could not figure out any structure")
154 var = [i for i, part in enumerate(mx) if part == int]
155 structs = [(nm, decode1(nm)) for nm in pages]
157 structs.sort(key=lambda o: "".join(o[1][len(mx):]))
158 for i in reversed(var):
159 structs.sort(key=lambda o: int(o[1][i]))
160 readnames = prefixes(self.path)
161 def constree(p, structs, idx):
164 for nm, st in structs:
165 id = "".join(st[len(mx):])
166 pages.append(page(self, pj(self.path, orig[nm]), id, id, p.stack + [(p, len(pages))]))
171 for nm, st in structs:
178 sub = [(nm, st) for nm, st in structs if st[var[idx]] == id]
181 id = "".join(st[var[idx]:])
182 ret.append(page(self, pj(self.path, orig[nm]), id, id, p.stack + [(p, len(ret))]))
184 name = findname(readnames, [nm for (nm, st) in sub]) or id
185 cur = interm(name, id, p.stack + [(p, len(ret))], [])
186 cur.direct = constree(cur, sub, idx + 1)
189 return constree(self, structs, 0)
191 class dumb(lib.library):
193 if not os.path.isdir(id):
197 class directory(dumb):
198 def __init__(self, path):
199 if not os.path.isdir(path):
200 raise IOError("No such directory: " + path)
203 def byname(self, prefix):
205 prefix = prefix.lower()
206 for dent in os.listdir(self.path):
207 if dent[:len(prefix)].lower() == prefix:
208 ret.append(manga(pj(self.path, dent)))
211 def search(self, expr):
213 return [manga(pj(self.path, dent)) for dent in os.listdir(self.path) if expr in dent.lower()]
216 for dent in os.listdir(self.path):
217 yield manga(pj(self.path, dent))