Add syntax checks

This commit is contained in:
3d-gussner 2022-01-07 14:52:13 +01:00
parent eb25e5f0da
commit 687746a80a

View file

@ -1,7 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# #
# Version 1.0.1 # Version 1.0.2 - Build 37
#
############################################################################# #############################################################################
# Change log: # Change log:
# 7 May 2019, Ondrej Tuma, Initial # 7 May 2019, Ondrej Tuma, Initial
@ -13,12 +12,26 @@
# 23 Apr. 2021, wavexx , improve # 23 Apr. 2021, wavexx , improve
# 24 Apr. 2021, wavexx , improve # 24 Apr. 2021, wavexx , improve
# 26 Apr. 2021, 3d-gussner, add character ruler # 26 Apr. 2021, 3d-gussner, add character ruler
# 07 Jan. 2022, 3d-gussner, Check for Syntax errors and exit with error
# , add Build number 'git rev-list --count HEAD lang-check.py'
############################################################################# #############################################################################
# #
# Expected syntax of the files, which other scripts depend on
# 'lang_en.txt'
# 1st line: '#MSG_'<some text>' c='<max chars in a column>' r='<max rows> ; '#MSG' is mandentory while 'c=' and 'r=' aren't but should be there
# 2nd line: '"'<origin message used in the source code>'"' ; '"' double quotes at the beginning and end of message are mandentory
# 3rd line: LF ; Line feed is mandantory between messages
#
# 'lang_en_??.txt'
# 1st line: '#MSG_'<some text>' c='<max chars in a column>' r='<max rows> ; '#MSG' is mandentory while 'c=' and 'r=' aren't but should be there
# 2nd line: '"'<origin message used in the source code>'"' ; '"' double quotes at the beginning and end of message are mandentory
# 3rd line: '"'<translated message>'"' ; '"' double quotes at the beginning and end of message are mandentory
# 4th line: LF ; Line feed is mandantory between messages
#
"""Check lang files.""" """Check lang files."""
from argparse import ArgumentParser from argparse import ArgumentParser
from traceback import print_exc from traceback import print_exc
from sys import stdout, stderr from sys import stdout, stderr, exit
import textwrap import textwrap
import re import re
@ -99,7 +112,6 @@ def ign_char_first(c):
def ign_char_last(c): def ign_char_last(c):
return c.isalnum() or c in {'.', "'"} return c.isalnum() or c in {'.', "'"}
def parse_txt(lang, no_warning, warn_empty): def parse_txt(lang, no_warning, warn_empty):
"""Parse txt file and check strings to display definition.""" """Parse txt file and check strings to display definition."""
if lang == "en": if lang == "en":
@ -112,9 +124,15 @@ def parse_txt(lang, no_warning, warn_empty):
lines = 1 lines = 1
with open(file_path) as src: with open(file_path) as src:
while True: while True:
comment = src.readline().split(' ') message = src.readline()
#print (comment) #Debug #print(message) #Debug
#check syntax 1st line starts with `#MSG`
if (message[0:4] != '#MSG'):
print(red("[E]: Critical syntax error: 1st line doesn't start with #MSG on line %d" % lines))
print(red(message))
exit(1)
#Check if columns and rows are defined
comment = message.split(' ')
#Check if columns and rows are defined #Check if columns and rows are defined
cols = None cols = None
rows = None rows = None
@ -140,12 +158,46 @@ def parse_txt(lang, no_warning, warn_empty):
print(yellow("[W]: Multiple rows with odd number of columns on line %d" % lines)) print(yellow("[W]: Multiple rows with odd number of columns on line %d" % lines))
#Wrap text to 20 chars and rows #Wrap text to 20 chars and rows
source = src.readline()[:-1].strip('"') source = src.readline()[:-1] #read whole line
#check if 2nd line of origin message beginns and ends with " double quote
if (source[0]!="\""):
print(red('[E]: Critical syntax error: Missing " at beginning of message in source on line %d' % lines))
print(red(source))
exit(1)
if (source[-1]=="\""):
source = source.strip('"') #remove " double quotes from message
else:
print(red('[E]: Critical syntax error: Missing " at end of message in source on line %d' % lines))
print(red(source))
exit(1)
#print (source) #Debug #print (source) #Debug
translation = src.readline()[:-1].strip('"') translation = src.readline()[:-1]#read whole line
#check if 3rd line of translation message beginns and ends with " double quote
if (translation[0]!="\""):
print(red('[E]: Critical syntax error: Missing " at beginning of message in translation on line %d' % lines))
print(red(translation))
exit(1)
if (translation[-1]=="\""):
#print ("End ok")
translation = translation.strip('"') #remove " double quote from message
else:
print(red('[E]: Critical syntax error: Missing " at end of message in translation on line %d' % lines))
print(red(translation))
exit(1)
#print (translation)
if translation == '\\x00': if translation == '\\x00':
# crude hack to handle intentionally-empty translations # crude hack to handle intentionally-empty translations
translation = '' translation = ''
#check if source is ascii only
if source.isascii() == False:
print(red('[E]: Critical syntax: Non ascii chars found on line %d' % lines))
print(red(source))
exit(1)
#check if translation is ascii only
if translation.isascii() == False:
print(red('[E]: Critical syntax: Non ascii chars found on line %d' % lines))
print(red(translation))
exit(1)
# handle backslash sequences # handle backslash sequences
source = unescape(source) source = unescape(source)
@ -232,8 +284,8 @@ def parse_txt(lang, no_warning, warn_empty):
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 if len(src.readline()) != 1: # empty line
print(red('[E]: Critical Syntax error: Missing empty line between messages between lines: %d and %d' % (lines+3,lines+4)))
break break
lines += 4 lines += 4
print(green("End %s lang-check" % lang)) print(green("End %s lang-check" % lang))