#!/usr/bin/env python # -*- coding: utf-8 -*- # # Copyright (C) 2011-2013 Alexandre Courbot # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . import sys, os, re, datetime from kanjivg import licenseString from utils import open verbose = False pathre = re.compile(r'') helpString = """Usage: %s [ kanji files ] Recognized commands: split file1 [ file2 ... ] extract path data into a -paths suffixed file merge file1 [ file2 ... ] merge path data from -paths suffixed file release create single release file""" % (sys.argv[0],) def createPathsSVG(f): s = open(f, "r", encoding="utf-8").read() paths = pathre.findall(s) out = open(f[:-4] + "-paths.svg", "w", encoding="utf-8") out.write(""" \n""") i = 1 for path in paths: out.write('\n' % (i, path)) i += 1 out.write("") def mergePathsSVG(f): pFile = f[:-4] + "-paths.svg" if not os.path.exists(pFile): print("%s does not exist!" % (pFile,)) return s = open(pFile, "r", encoding="utf-8").read() paths = pathre.findall(s) s = open(f, "r", encoding="utf-8").read() pos = 0 while True: match = pathre.search(s[pos:]) if match and len(paths) == 0 or not match and len(paths) > 0: print("Paths count mismatch for %s" % (f,)) return if not match and len(paths) == 0: break s = s[:pos + match.start(1)] + paths[0] + s[pos + match.end(1):] pos += match.start(1) + len(paths[0]) del paths[0] open(f, "w", encoding="utf-8").write(s) def release(): datadir = "kanji" idMatchString = "\n') out.write("\n") out.write("\n") for f in files: data = open(os.path.join(datadir, f), encoding='utf8').read() data = data.replace("\r\n", "\n") data = data[data.find("" % (data[:kidend],) + data[data.find("\n"):data.find('\n" out.write(data) out.write("\n") out.close() if verbose: print("%d kanji emitted" % len(files)) actions = { "split": (createPathsSVG, 2), "merge": (mergePathsSVG, 2), "release": (release, 1) } if __name__ == "__main__": if len(sys.argv) < 2 or sys.argv[1] not in actions.keys() or \ len(sys.argv) <= actions[sys.argv[1]][1]: print(helpString) sys.exit(0) action = actions[sys.argv[1]][0] files = sys.argv[2:] if len(files) == 0: action() else: for f in files: if not os.path.exists(f): print("%s does not exist!" % (f,)) continue action(f)