Add arguments validation to parse_tr

This commit is contained in:
rubenwardy 2024-02-26 00:41:22 +00:00
parent e86d9a8e88
commit 3e6d6864b3
4 changed files with 36 additions and 3 deletions

@ -29,7 +29,6 @@ class Translation:
self.entries = entries
def parse_tr(filepath: str) -> Translation:
entries = {}
filename = os.path.basename(filepath)
@ -40,13 +39,13 @@ def parse_tr(filepath: str) -> Translation:
language = filename_parts[-2]
textdomain = ".".join(filename_parts[0:-2])
with open(filepath, "r", encoding='utf-8') as existing_file:
with open(filepath, "r", encoding="utf-8") as existing_file:
lines = existing_file.readlines()
line_index = 0
while line_index < len(lines):
line = lines[line_index].rstrip('\n')
if line == "":
if line.strip() == "":
pass
# Comment lines
@ -63,6 +62,7 @@ def parse_tr(filepath: str) -> Translation:
had_equals = False
source = ""
current_part = ""
next_variable = 1
while i < len(line):
if line[i] == "@":
if i + 1 < len(line):
@ -76,6 +76,17 @@ def parse_tr(filepath: str) -> Translation:
current_part += "\n"
elif code.isdigit():
current_part += "@" + code
if had_equals:
if int(code) >= next_variable:
raise SyntaxError(
f"Line {line_index + 1}: Unknown argument @{code} in translated string")
else:
if int(code) != next_variable:
raise SyntaxError(
f"Line {line_index + 1}: Arguments out of order in source, found @{code} and expected @{next_variable}." +
"Arguments in source must be in increasing order, without gaps or repetitions, starting from 1")
next_variable += 1
else:
raise SyntaxError(f"Line {line_index + 1}: Unknown escape character: {code}")

@ -0,0 +1 @@
Some @1 args @5=Some @1 args @5

@ -59,3 +59,23 @@ def test_parses_tr_error_on_bad_escape():
parse_tr(filepath)
assert str(e.value) == "Line 1: Unknown escape character: x"
def test_parses_tr_error_on_bad_args():
dirname = os.path.dirname(__file__)
filepath = os.path.join(dirname, "bad_args.fr.tr")
with pytest.raises(SyntaxError) as e:
parse_tr(filepath)
assert "Line 1: Arguments out of order in source, found @5 and expected @2." in str(e.value)
def test_parses_tr_error_on_unknown_arg():
dirname = os.path.dirname(__file__)
filepath = os.path.join(dirname, "unknown_arg.fr.tr")
with pytest.raises(SyntaxError) as e:
parse_tr(filepath)
assert str(e.value) == "Line 1: Unknown argument @2 in translated string"

@ -0,0 +1 @@
Some @1 arg=Some @2 arg