Skip to content

Instantly share code, notes, and snippets.

@ospalh
Created February 28, 2013 15:00
Show Gist options
  • Save ospalh/5057354 to your computer and use it in GitHub Desktop.
Save ospalh/5057354 to your computer and use it in GitHub Desktop.
Swap two stroke numbers in a [KanjiVG](https://github.com/KanjiVG/kanjivg) stroke order svg.
#! /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 sys
__version__ = '0.0.1'
stroke_text_pattern = '>{0}</text>'
def swap_strokes(kanji, a, b):
"""Swap strokes in a kanjivg file"""
# We do no 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 = u''
line_b = u''
line_a_pattern = stroke_text_pattern.format(a)
line_b_pattern = stroke_text_pattern.format(b)
for n, l in enumerate(lines):
if line_a_pattern in l:
num_a = n
line_a = l
if line_b_pattern in l:
num_b = n
line_b = l
if num_a < 0 or num_b < 0:
raise RuntimeError("Did not find both lines")
lines[num_a] = line_b.replace(line_b_pattern, line_a_pattern)
lines[num_b] = line_a.replace(line_a_pattern, line_b_pattern)
with open(kanji, 'w') as kf:
for l in lines:
kf.write(l)
if __name__ == '__main__':
helpmsg = u"""Usage: kvgswap FILE STROKE_A STROKE_B
Swaps strokes A and B in a kanjivg svg file.
"""
if len(sys.argv) != 4:
print helpmsg
sys.exit(0)
swap_strokes(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