-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvert.py
More file actions
executable file
·88 lines (63 loc) · 2.39 KB
/
convert.py
File metadata and controls
executable file
·88 lines (63 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# -*- coding: utf-8 -*-
'''libhangul mapping convertor
'''
import argparse
import os
import pathlib
import sys
import xml.etree.ElementTree as ET
import yaml
def get_args():
"""parsing arguments
"""
parser = argparse.ArgumentParser(
"Keyboard mapping convertor for libhangul")
parser.add_argument('--config', '-c',
# type=argparse.FileType('r'),
default='config.yaml',
help="mapping confiiguration")
parser.add_argument('--in_path' , '-i',
type=pathlib.Path,
required=True,
help="path for input files")
parser.add_argument('--out_path', '-o',
type=pathlib.Path,
default='./',
help="path for output files. Default is current directory.")
return parser.parse_args()
def _expand_eliments(eliments):
eliments = list(eliments)
eliments.extend([elem.upper() for elem in eliments])
return eliments
def _update_mapping(mapping):
keys = _expand_eliments(mapping.keys())
values = _expand_eliments(mapping.values())
return dict(zip([hex(ord(k)) for k in keys],
[hex(ord(v)) for v in values]))
_CONVERTED = 'converted'
def convert(name, mapping, targets, in_path, out_path):
"""convert mappings"""
mapping = _update_mapping(mapping)
for xml_file in targets:
in_file = os.path.join(in_path, xml_file)
tree = ET.parse(in_file)
root = tree.getroot()
if _CONVERTED in root.keys():
print("'{}' is already converted for '{}'".format(
in_file, root.get(_CONVERTED)))
continue
root.set(_CONVERTED, name)
for item in root.iter('item'):
key = item.attrib['key']
if key in mapping.keys():
item.attrib['key'] = mapping[key]
tree.write(os.path.join(out_path, xml_file), encoding='utf-8',
xml_declaration=True)
def main(name, conversions, in_path, out_path):
for conv in conversions:
convert(name, conv['mapping'], conv['targets'], in_path, out_path)
if __name__ == '__main__':
ARGS = get_args()
with open(ARGS.config) as yml:
config = yaml.safe_load(yml)
main(config['name'], config['conversions'], ARGS.in_path, ARGS.out_path)