Syntax check `lang_en.txt`

Display correct line having issues
This commit is contained in:
3d-gussner 2022-01-10 09:52:43 +01:00
parent 687746a80a
commit db4859ce9a
1 changed files with 98 additions and 87 deletions

View File

@ -121,10 +121,11 @@ def parse_txt(lang, no_warning, warn_empty):
print(green("Start %s lang-check" % lang)) print(green("Start %s lang-check" % lang))
lines = 1 lines = 0
with open(file_path) as src: with open(file_path) as src:
while True: while True:
message = src.readline() message = src.readline()
lines += 1
#print(message) #Debug #print(message) #Debug
#check syntax 1st line starts with `#MSG` #check syntax 1st line starts with `#MSG`
if (message[0:4] != '#MSG'): if (message[0:4] != '#MSG'):
@ -151,7 +152,7 @@ def parse_txt(lang, no_warning, warn_empty):
if cols is None and rows is None: if cols is None and rows is None:
if not no_warning: if not no_warning:
print(yellow("[W]: No display definition on line %d" % lines)) print(yellow("[W]: No display definition on line %d" % lines))
cols = len(translation) # propably fullscreen cols = len(source) # propably fullscreen
if rows is None: if rows is None:
rows = 1 rows = 1
elif rows > 1 and cols != 20: elif rows > 1 and cols != 20:
@ -159,55 +160,61 @@ def parse_txt(lang, no_warning, warn_empty):
#Wrap text to 20 chars and rows #Wrap text to 20 chars and rows
source = src.readline()[:-1] #read whole line source = src.readline()[:-1] #read whole line
lines += 1
#check if 2nd line of origin message beginns and ends with " double quote #check if 2nd line of origin message beginns and ends with " double quote
if (source[0]!="\""): if (source[0]!="\""):
print(red('[E]: Critical syntax error: Missing " at beginning of message in source on line %d' % lines)) print(red('[E]: Critical syntax error: Missing " double quotes at beginning of message in source on line %d' % lines))
print(red(source)) print(red(source))
exit(1) exit(1)
if (source[-1]=="\""): if (source[-1]=="\""):
source = source.strip('"') #remove " double quotes from message source = source.strip('"') #remove " double quotes from message
else: else:
print(red('[E]: Critical syntax error: Missing " at end of message in source on line %d' % lines)) print(red('[E]: Critical syntax error: Missing " double quotes at end of message in source on line %d' % lines))
print(red(source)) print(red(source))
exit(1) exit(1)
#print (source) #Debug #print(source) #Debug
translation = src.readline()[:-1]#read whole line if lang != "en":
#check if 3rd line of translation message beginns and ends with " double quote translation = src.readline()[:-1]#read whole line
if (translation[0]!="\""): lines += 1
print(red('[E]: Critical syntax error: Missing " at beginning of message in translation on line %d' % lines)) #check if 3rd line of translation message beginns and ends with " double quote
print(red(translation)) if (translation[0]!="\""):
exit(1) print(red('[E]: Critical syntax error: Missing " double quotes at beginning of message in translation on line %d' % lines))
if (translation[-1]=="\""): print(red(translation))
#print ("End ok") exit(1)
translation = translation.strip('"') #remove " double quote from message if (translation[-1]=="\""):
else: #print ("End ok")
print(red('[E]: Critical syntax error: Missing " at end of message in translation on line %d' % lines)) translation = translation.strip('"') #remove " double quote from message
print(red(translation)) else:
exit(1) print(red('[E]: Critical syntax error: Missing " double quotes at end of message in translation on line %d' % lines))
#print (translation) print(red(translation))
if translation == '\\x00': exit(1)
# crude hack to handle intentionally-empty translations #print(translation) #Debug
translation = '' if translation == '\\x00':
#check if source is ascii only # crude hack to handle intentionally-empty translations
translation = ''
#check if source is ascii only
if source.isascii() == False: if source.isascii() == False:
print(red('[E]: Critical syntax: Non ascii chars found on line %d' % lines)) print(red('[E]: Critical syntax: Non ascii chars found on line %d' % lines))
print(red(source)) print(red(source))
exit(1) exit(1)
#check if translation is ascii only #check if translation is ascii only
if translation.isascii() == False: if lang != "en":
print(red('[E]: Critical syntax: Non ascii chars found on line %d' % lines)) if translation.isascii() == False:
print(red(translation)) print(red('[E]: Critical syntax: Non ascii chars found on line %d' % lines))
exit(1) print(red(translation))
exit(1)
# handle backslash sequences # handle backslash sequences
source = unescape(source) source = unescape(source)
translation = unescape(translation) if lang != "en":
translation = unescape(translation)
#print (translation) #Debug #print (translation) #Debug
wrapped_source = wrap_text(source, cols) wrapped_source = wrap_text(source, cols)
rows_count_source = len(wrapped_source) rows_count_source = len(wrapped_source)
wrapped_translation = wrap_text(translation, cols) if lang != "en":
rows_count_translation = len(wrapped_translation) wrapped_translation = wrap_text(translation, cols)
rows_count_translation = len(wrapped_translation)
# Check for potential errors in the definition # Check for potential errors in the definition
if not no_warning: if not no_warning:
@ -224,70 +231,74 @@ def parse_txt(lang, no_warning, warn_empty):
print() print()
# Missing translation # Missing translation
if len(translation) == 0 and (warn_empty or rows > 1): if lang != "en":
if rows == 1: if len(translation) == 0 and (warn_empty or rows > 1):
print(yellow("[W]: Empty translation for \"%s\" on line %d" % (source, lines))) if rows == 1:
else: print(yellow("[W]: Empty translation for \"%s\" on line %d" % (source, lines)))
print(yellow("[W]: Empty translation on line %d" % lines)) else:
print_ruler(6, cols); print(yellow("[W]: Empty translation on line %d" % lines))
print_wrapped(wrapped_source, rows, cols) print_ruler(6, cols);
print() print_wrapped(wrapped_source, rows, cols)
print()
# Check for translation lenght # Check for translation lenght
if (rows_count_translation > rows) or (rows == 1 and len(translation) > cols): if (rows_count_translation > rows) or (rows == 1 and len(translation) > cols):
print(red('[E]: Text is longer than definition on line %d: cols=%d rows=%d (rows diff=%d)' print(red('[E]: Text is longer than definition on line %d: cols=%d rows=%d (rows diff=%d)'
% (lines, cols, rows, rows_count_translation-rows))) % (lines, cols, rows, rows_count_translation-rows)))
print_source_translation(source, translation, print_source_translation(source, translation,
wrapped_source, wrapped_translation, wrapped_source, wrapped_translation,
rows, cols) rows, cols)
# Different count of % sequences # Different count of % sequences
if source.count('%') != translation.count('%') and len(translation) > 0: if source.count('%') != translation.count('%') and len(translation) > 0:
print(red('[E]: Unequal count of %% escapes on line %d:' % (lines))) print(red('[E]: Unequal count of %% escapes on line %d:' % (lines)))
print_source_translation(source, translation, print_source_translation(source, translation,
wrapped_source, wrapped_translation, wrapped_source, wrapped_translation,
rows, cols) rows, cols)
# Different first/last character # Different first/last character
if not no_warning and len(source) > 0 and len(translation) > 0: if not no_warning and len(source) > 0 and len(translation) > 0:
source_end = source.rstrip()[-1] source_end = source.rstrip()[-1]
translation_end = translation.rstrip()[-1] translation_end = translation.rstrip()[-1]
start_diff = not (ign_char_first(source[0]) and ign_char_first(translation[0])) and source[0] != translation[0] start_diff = not (ign_char_first(source[0]) and ign_char_first(translation[0])) and source[0] != translation[0]
end_diff = not (ign_char_last(source_end) and ign_char_last(translation_end)) and source_end != translation_end end_diff = not (ign_char_last(source_end) and ign_char_last(translation_end)) and source_end != translation_end
if start_diff or end_diff: if start_diff or end_diff:
if start_diff: if start_diff:
print(yellow('[W]: Differing first punctuation character (%s => %s) on line %d:' % (source[0], translation[0], lines))) print(yellow('[W]: Differing first punctuation character (%s => %s) on line %d:' % (source[0], translation[0], lines)))
if end_diff: if end_diff:
print(yellow('[W]: Differing last punctuation character (%s => %s) on line %d:' % (source[-1], translation[-1], lines))) print(yellow('[W]: Differing last punctuation character (%s => %s) on line %d:' % (source[-1], translation[-1], lines)))
print_source_translation(source, translation, print_source_translation(source, translation,
wrapped_source, wrapped_translation, wrapped_source, wrapped_translation,
rows, cols) rows, cols)
# Short translation # Short translation
if not no_warning and len(source) > 0 and len(translation) > 0: if not no_warning and len(source) > 0 and len(translation) > 0:
if len(translation.rstrip()) < len(source.rstrip()) / 2: if len(translation.rstrip()) < len(source.rstrip()) / 2:
print(yellow('[W]: Short translation on line %d:' % (lines))) print(yellow('[W]: Short translation on line %d:' % (lines)))
print_source_translation(source, translation, print_source_translation(source, translation,
wrapped_source, wrapped_translation, wrapped_source, wrapped_translation,
rows, cols) rows, cols)
# Incorrect trailing whitespace in translation # Incorrect trailing whitespace in translation
if not no_warning and len(translation) > 0 and \ if not no_warning and len(translation) > 0 and \
(source.rstrip() == source or (rows == 1 and len(source) == cols)) and \ (source.rstrip() == source or (rows == 1 and len(source) == cols)) and \
translation.rstrip() != translation and \ translation.rstrip() != translation and \
(rows > 1 or len(translation) != len(source)): (rows > 1 or len(translation) != len(source)):
print(yellow('[W]: Incorrect trailing whitespace for translation on line %d:' % (lines))) print(yellow('[W]: Incorrect trailing whitespace for translation on line %d:' % (lines)))
source = highlight_trailing_white(source) source = highlight_trailing_white(source)
translation = highlight_trailing_white(translation) translation = highlight_trailing_white(translation)
wrapped_translation = highlight_trailing_white(wrapped_translation) wrapped_translation = highlight_trailing_white(wrapped_translation)
print_source_translation(source, translation, print_source_translation(source, translation,
wrapped_source, wrapped_translation, wrapped_source, wrapped_translation,
rows, cols) rows, cols)
if len(src.readline()) != 1: # empty line delimiter = src.readline()
print(red('[E]: Critical Syntax error: Missing empty line between messages between lines: %d and %d' % (lines+3,lines+4))) lines += 1
if ("" == delimiter):
break
elif len(delimiter) != 1: # empty line
print(red('[E]: Critical Syntax error: Missing empty line between messages between lines: %d and %d' % (lines-1,lines)))
break break
lines += 4
print(green("End %s lang-check" % lang)) print(green("End %s lang-check" % lang))