11 while p < len(nm) and nm[p].isdigit():
17 while p < len(nm) and nm[p].isalpha():
34 def findname(names, files):
35 matches = list(names.keys())
37 matches = [pfx for pfx in matches if f.startswith(pfx)]
38 if len(matches) < 1: return None
39 matches.sort(key=len, reverse=True)
40 return names[matches[0]]
44 if not nmpath.exists():
47 with nmpath.open("r") as fp:
52 ret[line[:p]] = line[p + 1:]
55 class imgstream(lib.imgstream):
56 def __init__(self, path):
57 self.bk = path.open("rb")
58 self.clen = os.fstat(self.bk.fileno()).st_size
63 def read(self, sz=None):
64 return self.bk.read(sz)
67 def __init__(self, manga, path, name, id, stack):
75 return imgstream(self.path)
77 class interm(lib.pagelist):
78 def __init__(self, name, id, stack, direct):
85 return len(self.direct)
87 def __getitem__(self, n):
93 s = genstr(decode1(dent))
98 for p, n in zip(mx, s):
106 class manga(lib.manga):
107 exts = ["jpg", "jpeg", "png", "gif"]
109 def __init__(self, path):
110 path = path.resolve()
111 if not path.is_dir():
112 raise IOError("No such directory: " + path)
114 self.id = os.fspath(path)
116 if (self.path/"name").exists():
117 with (self.path/"name").open("r") as s:
118 self.name = s.readline().strip()
120 self.name = path.name
121 self.direct = self.destruct()
124 return len(self.direct)
126 def __getitem__(self, idx):
127 return self.direct[idx]
130 if (self.path/"order").exists():
131 with (self.path/"order").open("r") as s:
132 return True, [line.strip() for line in s if (self.path/line.strip()).exists()]
134 return False, [dent for dent in (dent.name for dent in self.path.iterdir()) if '.' in dent and dent[dent.rindex('.') + 1:] in self.exts]
136 def bakenames(self, files):
142 nm = nm[:nm.rindex('.')]
148 ordered, files = self.imglist()
149 pages, orig = self.bakenames(files)
150 mx = maxstruct(pages)
152 raise TypeError("could not figure out any structure")
153 var = [i for i, part in enumerate(mx) if part == int]
154 structs = [(nm, decode1(nm)) for nm in pages]
156 structs.sort(key=lambda o: "".join(o[1][len(mx):]))
157 for i in reversed(var):
158 structs.sort(key=lambda o: int(o[1][i]))
159 readnames = prefixes(self.path)
160 def constree(p, structs, idx):
163 for nm, st in structs:
164 id = "".join(st[len(mx):])
165 pages.append(page(self, self.path/orig[nm], id, id, p.stack + [(p, len(pages))]))
170 for nm, st in structs:
177 sub = [(nm, st) for nm, st in structs if st[var[idx]] == id]
180 id = "".join(st[var[idx]:])
181 ret.append(page(self, self.path/orig[nm], id, id, p.stack + [(p, len(ret))]))
183 name = findname(readnames, [nm for (nm, st) in sub]) or id
184 cur = interm(name, id, p.stack + [(p, len(ret))], [])
185 cur.direct = constree(cur, sub, idx + 1)
188 return constree(self, structs, 0)
190 class dumb(lib.library):
192 path = pathlib.Path(id)
193 if not path.is_dir():
197 class directory(dumb):
198 def __init__(self, path):
199 if not path.is_dir():
200 raise IOError("No such directory: " + path)
203 def byname(self, prefix):
205 prefix = prefix.lower()
206 for dent in self.path.iterdir():
207 if dent.name[:len(prefix)].lower() == prefix:
208 ret.append(manga(dent))
211 def search(self, expr):
213 return [manga(dent) for dent in self.path.iterdir() if expr in dent.name.lower()]
216 for dent in self.path.iterdir():