File: validate.py

package info (click to toggle)
ddnet 19.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 68,960 kB
  • sloc: cpp: 195,050; ansic: 58,572; python: 5,568; asm: 946; sh: 941; java: 366; xml: 206; makefile: 31
file content (54 lines) | stat: -rwxr-xr-x 1,737 bytes parent folder | download
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
#!/usr/bin/env python3
import os
import sys
import re
import twlang

os.chdir(os.path.dirname(__file__) + "/../..")

# Taken from https://stackoverflow.com/questions/30011379/how-can-i-parse-a-c-format-string-in-python
cfmt = '''
(                                  # start of capture group 1
%                                  # literal "%"
(?:                                # first option
(?:[-+0 #]{0,5})                   # optional flags
(?:\\d+|\\*)?                      # width
(?:\\.(?:\\d+|\\*))?               # precision
(?:h|l|ll|w|I|I32|I64)?            # size
[cCdiouxXeEfgGaAnpsSZ]             # type
) |                                # OR
%%)                                # literal "%%"
'''

total_errors = 0

def print_validation_error(error, filename, error_line):
	print(f"Invalid: {translated}")
	print(f"- {error} in {filename}:{error_line + 1}\n")
	global total_errors
	total_errors += 1

if len(sys.argv) > 1:
	languages = sys.argv[1:]
else:
	languages = twlang.languages()
local = twlang.localizes()

for language in languages:
	translations = twlang.translations(language)

	for (english, _), (line, translated, _) in translations.items():
		if not translated:
			continue

		# Validate c format strings. Strings that move the formatters are not validated.
		if re.findall(cfmt, english, flags=re.X) != re.findall(cfmt, translated, flags=re.X) and not "1$" in translated:
			print_validation_error("Non-matching formatting", language, line)

		# Check for elipisis
		if "…" in english and "..." in translated:
			print_validation_error("Usage of ... instead of the … character", language, line)

if total_errors:
	print(f"Found {total_errors} {'error' if total_errors == 1 else 'errors'} ")
	sys.exit(1)