(kanji, a, b)
Swap the stroke data in a kanjivg file
Swap the stroke data in a kanjivg file
Swap the stroke data in a kanjivg file
def swap_stroke_data(kanji, a, b):
    """Swap the stroke data in a kanjivg file"""
    # We do hardly any checking. 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)
def swap_stroke_data ( kanji , a , b ) : # We do hardly any checking. 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 )