forked from hedyorg/hedy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
translating.py
140 lines (106 loc) · 3.49 KB
/
translating.py
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
"""
Helper routines for the translation page.
"""
class TranslatableFile:
def __init__(self, caption, file, strings):
self.caption = caption
self.file = file
self.strings = strings
def add_string(self, string):
self.strings.append(string)
class TranslatableString:
def __init__(self, path, original, translated):
self.is_header = False
self.path = path
self.original = original
self.translated = translated
self.key = path[-1] if path else ''
@property
def encoded_path(self):
return '.'.join(self.path)
@staticmethod
def decode_path(path):
return [try_int(x) for x in path.split('.')]
class TranslatableSection:
def __init__(self, caption):
self.is_header = True
self.caption = caption
def try_int(x):
try:
return int(x)
except ValueError:
return x
def struct_to_sections(struct1, struct2):
assert(isinstance(struct1, dict))
def recurse(x, y, path):
if isinstance(x, str):
strings.append(TranslatableString(path, str(x), str(y or '')))
return
if isinstance(x, list):
y = y if isinstance(y, list) else []
for i, el in enumerate(x):
if not isinstance(el, str):
strings.append(TranslatableSection(str(i + 1)))
recurse(el, y[i] if i < len(y) else None, path + ['a:' + str(i)])
return
if isinstance(x, dict):
y = y if isinstance(y, dict) else {}
for key, el in x.items():
if not isinstance(el, str):
strings.append(TranslatableSection(str(key)))
recurse(el, y.get(key, None), path + [str(key)])
strings = []
recurse(struct1, struct2, [])
return strings
def apply_form_change(data, encoded_path, value):
"""Apply a change submitted via the web form to the given data.
We need to make minimal mutations to the YAML in order
for the Ruamel Yaml parser to maintain the original
source locations, comments, etc.
"""
path = TranslatableString.decode_path(encoded_path)
return apply_change(data, path, value)
def apply_change(data, path, value):
if not path:
raise RuntimeError('Path cannot be empty')
key = path[-1]
container = value_at(data, path[:-1])
if str(key).startswith('a:'):
# Expecting index into a list
if not isinstance(container, list):
container = apply_change(data, path[:-1], [])
index = int(key[2:])
while len(container) < index + 1:
data.append({})
container[index] = value
return container
else:
# Expecting to index into a dict
if not isinstance(container, dict):
container = apply_change(data, path[:-1], {})
container[key] = value
return container
def value_at(data, path):
for p in path:
if str(p).startswith('a:'):
# Expecting to index into a list
index = int(key[2:])
if not isinstance(data, list): return None
if len(data) <= index: return None
data = data[index]
else:
# Expecting to index into a dict (but the index could still be a number)
if not isinstance(data, dict): return None
data = data.get(p, None)
return data
def render_caption(path):
return ' » '.join(path)
def str_presenter(dumper, data):
"""For use with yaml.dump to get mostly human-sensible results."""
try:
dlen = len(data.splitlines())
if (dlen > 1):
return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='|')
except TypeError as ex:
return dumper.represent_scalar('tag:yaml.org,2002:str', data)
return dumper.represent_scalar('tag:yaml.org,2002:str', data)