2022-06-01 15:37:39 +00:00
|
|
|
"""Test that the "translated" javascript files to not contain template tags which need to be determined at "run time".
|
2021-07-28 23:23:24 +00:00
|
|
|
|
|
|
|
This is because the "translated" javascript files are compiled into the "static" directory.
|
|
|
|
They should only contain template tags that render static information.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import pathlib
|
2022-05-20 15:24:51 +00:00
|
|
|
import re
|
|
|
|
import sys
|
2021-07-28 23:23:24 +00:00
|
|
|
|
|
|
|
here = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
template_dir = os.path.abspath(os.path.join(here, '..', 'InvenTree', 'templates'))
|
|
|
|
|
|
|
|
# We only care about the 'translated' files
|
|
|
|
js_i18n_dir = os.path.join(template_dir, 'js', 'translated')
|
|
|
|
js_dynamic_dir = os.path.join(template_dir, 'js', 'dynamic')
|
|
|
|
|
|
|
|
errors = 0
|
|
|
|
|
2024-01-11 00:28:58 +00:00
|
|
|
print('=================================')
|
|
|
|
print('Checking static javascript files:')
|
|
|
|
print('=================================')
|
2021-07-28 23:23:24 +00:00
|
|
|
|
2021-11-19 20:50:41 +00:00
|
|
|
|
2021-07-28 23:23:24 +00:00
|
|
|
def check_invalid_tag(data):
|
2022-06-05 22:56:52 +00:00
|
|
|
"""Check for invalid tags."""
|
2024-01-11 00:28:58 +00:00
|
|
|
pattern = r'{%(\w+)'
|
2021-07-28 23:23:24 +00:00
|
|
|
|
|
|
|
err_count = 0
|
|
|
|
|
|
|
|
for idx, line in enumerate(data):
|
|
|
|
results = re.findall(pattern, line)
|
|
|
|
|
|
|
|
for result in results:
|
|
|
|
err_count += 1
|
|
|
|
|
2024-02-19 23:47:57 +00:00
|
|
|
print(f' - Error on line {idx + 1}: %{{{result[0]}')
|
2021-07-28 23:23:24 +00:00
|
|
|
|
|
|
|
return err_count
|
|
|
|
|
2021-11-19 20:50:41 +00:00
|
|
|
|
2021-07-28 23:23:24 +00:00
|
|
|
def check_prohibited_tags(data):
|
2022-06-05 22:56:52 +00:00
|
|
|
"""Check for prohibited tags."""
|
2021-07-28 23:23:24 +00:00
|
|
|
allowed_tags = [
|
|
|
|
'if',
|
|
|
|
'elif',
|
|
|
|
'else',
|
|
|
|
'endif',
|
|
|
|
'for',
|
|
|
|
'endfor',
|
|
|
|
'trans',
|
|
|
|
'load',
|
|
|
|
'include',
|
|
|
|
'url',
|
|
|
|
]
|
|
|
|
|
2024-01-11 00:28:58 +00:00
|
|
|
pattern = r'{% (\w+)\s'
|
2021-07-28 23:23:24 +00:00
|
|
|
|
|
|
|
err_count = 0
|
|
|
|
|
|
|
|
for idx, line in enumerate(data):
|
|
|
|
for tag in re.findall(pattern, line):
|
|
|
|
if tag not in allowed_tags:
|
2024-02-19 23:47:57 +00:00
|
|
|
print(f" > Line {idx + 1} contains prohibited template tag '{tag}'")
|
2021-07-28 23:23:24 +00:00
|
|
|
err_count += 1
|
|
|
|
|
|
|
|
return err_count
|
|
|
|
|
|
|
|
|
|
|
|
for filename in pathlib.Path(js_i18n_dir).rglob('*.js'):
|
|
|
|
print(f"Checking file 'translated/{os.path.basename(filename)}':")
|
|
|
|
|
|
|
|
with open(filename, 'r') as js_file:
|
|
|
|
data = js_file.readlines()
|
|
|
|
|
|
|
|
errors += check_invalid_tag(data)
|
|
|
|
errors += check_prohibited_tags(data)
|
|
|
|
|
|
|
|
for filename in pathlib.Path(js_dynamic_dir).rglob('*.js'):
|
|
|
|
print(f"Checking file 'dynamic/{os.path.basename(filename)}':")
|
|
|
|
|
|
|
|
# Check that the 'dynamic' files do not contains any translated strings
|
|
|
|
with open(filename, 'r') as js_file:
|
|
|
|
data = js_file.readlines()
|
|
|
|
|
2024-01-21 11:47:47 +00:00
|
|
|
invalid_tags = ['blocktrans', 'blocktranslate', 'trans', 'translate']
|
2021-11-22 23:28:23 +00:00
|
|
|
|
2021-07-28 23:23:24 +00:00
|
|
|
err_count = 0
|
|
|
|
|
|
|
|
for idx, line in enumerate(data):
|
2024-01-19 01:16:17 +00:00
|
|
|
for tag in invalid_tags:
|
|
|
|
tag = '{% ' + tag
|
|
|
|
if tag in line:
|
|
|
|
err_count += 1
|
2021-07-28 23:23:24 +00:00
|
|
|
|
2024-02-19 23:47:57 +00:00
|
|
|
print(f" > Error on line {idx + 1}: Prohibited tag '{tag}' found")
|
2021-07-28 23:23:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
if errors > 0:
|
2024-01-11 00:28:58 +00:00
|
|
|
print(f'Found {errors} incorrect template tags')
|
2021-07-28 23:23:24 +00:00
|
|
|
|
|
|
|
sys.exit(errors)
|