Created
March 5, 2013 11:58
-
-
Save ospalh/5089835 to your computer and use it in GitHub Desktop.
Similar to kvgswap.py
Swap the actual vector data of two strokes around in a kanjivg kanki stroke order svg file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python2 | |
# -*- coding: utf-8 ; mode: python -*- | |
# © Copyright 2013 ospalh@gmail.com | |
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html | |
import re | |
import sys | |
__version__ = '0.0.1' | |
stroke_re = '^\s.*-s{0}" kvg:type=".*" d="(.*)"/>' | |
def swap_stroke_data(kanji, a, b): | |
"""Swap the stroke data in a kanjivg file""" | |
# We do hardly any checking at all. If something is wrong, just | |
# blow up. | |
with open(kanji) as kf: | |
lines = kf.readlines() | |
num_a = -1 | |
num_b = -1 | |
line_a_match = None | |
line_b_match = None | |
line_a_re = stroke_re.format(a) | |
line_b_re = stroke_re.format(b) | |
for n, l in enumerate(lines): | |
m = re.search(line_a_re, l) | |
if m: | |
num_a = n | |
line_a_match = m | |
m = re.search(line_b_re, l) | |
if m: | |
num_b = n | |
line_b_match = m | |
if num_a < 0 or num_b < 0: | |
raise RuntimeError("Did not find both lines") | |
lines[num_a] = lines[num_a].replace(line_a_match.group(1), | |
line_b_match.group(1)) | |
lines[num_b] = lines[num_b].replace(line_b_match.group(1), | |
line_a_match.group(1)) | |
with open(kanji, 'w') as kf: | |
for l in lines: | |
kf.write(l) | |
if __name__ == '__main__': | |
helpmsg = u"""Usage: kvgstrokedataswap FILE STROKE_A STROKE_B | |
Swaps the data (path d="..." values) of strokes A and B in a kanjivg svg file. | |
""" | |
if len(sys.argv) != 4: | |
print helpmsg | |
sys.exit(0) | |
swap_stroke_data(sys.argv[1], sys.argv[2], sys.argv[3]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment