Add syntax checks

This commit is contained in:
3d-gussner 2022-01-07 14:52:13 +01:00
parent 3b82333fcb
commit 86235259a3

View file

@ -1,7 +1,6 @@
#!/usr/bin/env python3
#
# Version 1.0.1
#
# Version 1.0.2 - Build 37
#############################################################################
# Change log:
# 7 May 2019, Ondrej Tuma, Initial
@ -13,12 +12,26 @@
# 23 Apr. 2021, wavexx , improve
# 24 Apr. 2021, wavexx , improve
# 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."""
from argparse import ArgumentParser
from traceback import print_exc
from sys import stdout, stderr
from sys import stdout, stderr, exit
import textwrap
import re
@ -99,7 +112,6 @@ def ign_char_first(c):
def ign_char_last(c):
return c.isalnum() or c in {'.', "'"}
def parse_txt(lang, no_warning, warn_empty):
"""Parse txt file and check strings to display definition."""
if lang == "en":
@ -112,9 +124,15 @@ def parse_txt(lang, no_warning, warn_empty):
lines = 1
with open(file_path) as src:
while True:
comment = src.readline().split(' ')
#print (comment) #Debug
message = src.readline()
#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
cols = 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))
#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
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':
# crude hack to handle intentionally-empty translations
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
source = unescape(source)
@ -232,8 +284,8 @@ def parse_txt(lang, no_warning, warn_empty):
print_source_translation(source, translation,
wrapped_source, wrapped_translation,
rows, cols)
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
lines += 4
print(green("End %s lang-check" % lang))