From 0d299ac79ff30e3d6ed288d58e3f11fda6891c60 Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Sat, 20 Jun 2020 15:47:54 +0200 Subject: [PATCH] local: Read and use prefix names. --- manga/local.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/manga/local.py b/manga/local.py index 7e21a9b..9e6fabc 100644 --- a/manga/local.py +++ b/manga/local.py @@ -32,6 +32,27 @@ def genstr(s): ret += [part] return ret +def findname(names, files): + matches = list(names.keys()) + for f in files: + matches = [pfx for pfx in matches if f.startswith(pfx)] + if len(matches) < 1: return None + matches.sort(key=len, reverse=True) + return names[matches[0]] + +def prefixes(path): + nmpath = pj(path, "names") + if not os.path.exists(nmpath): + return {} + ret = {} + with open(nmpath, "r") as fp: + for line in fp: + line = line.strip() + p = line.find(' ') + if p < 0: continue + ret[line[:p]] = line[p + 1:] + return ret + class imgstream(lib.imgstream): def __init__(self, path): self.bk = open(path, 'rb') @@ -136,6 +157,7 @@ class manga(lib.manga): structs.sort(key=lambda o: "".join(o[1][len(mx):])) for i in reversed(var): structs.sort(key=lambda o: int(o[1][i])) + readnames = prefixes(self.path) def constree(p, structs, idx): if idx == len(var): pages = [] @@ -159,7 +181,8 @@ class manga(lib.manga): id = "".join(st[var[idx]:]) ret.append(page(self, pj(self.path, orig[nm]), id, id, p.stack + [(p, len(ret))])) else: - cur = interm(id, id, p.stack + [(p, len(ret))], []) + name = findname(readnames, [nm for (nm, st) in sub]) or id + cur = interm(name, id, p.stack + [(p, len(ret))], []) cur.direct = constree(cur, sub, idx + 1) ret.append(cur) return ret -- 2.11.0