MCPcopy Create free account
hub / github.com/ddnet/ddnet / decode

Function decode

scripts/languages/twlang.py:13–54  ·  view source on GitHub ↗
(fileobj, elements_per_key, allow_context=True)

Source from the content-addressed store, hash-verified

11
12
13def decode(fileobj, elements_per_key, allow_context=True):
14 data = {}
15 current_context = ""
16 current_key = None
17 index = -1
18 for index, line in enumerate(fileobj):
19 line = line.encode("utf-8").decode("utf-8-sig").rstrip("\r\n")
20 if not line or line[:1] == "#":
21 current_context = ""
22 continue
23
24 if line[0] == "[":
25 if line[-1] != "]":
26 raise LanguageDecodeError("Invalid context string", fileobj.name, index)
27 if not allow_context:
28 raise LanguageDecodeError("Context not allowed in this file", fileobj.name, index)
29 current_context = line[1:-1]
30 elif line[:3] == "== ":
31 if len(data[current_key]) >= 1 + elements_per_key:
32 raise LanguageDecodeError("Wrong number of elements per key", fileobj.name, index)
33 if current_key:
34 translation = line[3:]
35 data[current_key].extend([translation])
36 else:
37 raise LanguageDecodeError("Element before key given", fileobj.name, index)
38 else:
39 if current_key:
40 if len(data[current_key]) != 1 + elements_per_key:
41 raise LanguageDecodeError("Wrong number of elements per key", fileobj.name, index)
42 data[current_key].append(index - 1 if current_context else index)
43 if line in data:
44 raise LanguageDecodeError("Key defined multiple times: " + line, fileobj.name, index)
45 data[(line, current_context)] = [index - 1 if current_context else index]
46 current_key = (line, current_context)
47 if len(data[current_key]) != 1 + elements_per_key:
48 raise LanguageDecodeError("Wrong number of elements per key", fileobj.name, index)
49 data[current_key].append(index + 1)
50 new_data = {}
51 for key, value in data.items():
52 if key[0]:
53 new_data[key] = value
54 return new_data
55
56
57def check_file(path):

Callers 2

language_indexFunction · 0.85
translationsFunction · 0.85

Calls 1

LanguageDecodeErrorClass · 0.85

Tested by

no test coverage detected