add detection of missing entries

This commit is contained in:
xstraok 2023-04-17 19:52:56 -04:00 committed by GitHub
parent 2739006b52
commit 3cea26c072
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -6,28 +6,38 @@
import os import os
import sys import sys
import glob import glob
import re
valid_args=["diff","unused"] valid_args=["diff","unused","missing"]
if len(sys.argv)<3 or len(sys.argv) == 0: if len(sys.argv)<3 or len(sys.argv) == 0:
print("missing arguments") print("missing arguments")
sys.exit() sys.exit()
if sys.argv[2] not in valid_args: if sys.argv[2] not in valid_args:
print(f"invalid argument:{sys.argv[2]}") print(f"invalid argument:{sys.argv[2]}\n Valid arguments: {valid_args}")
sys.exit() sys.exit()
path = "assets/locales/" + sys.argv[1] + "/" path = "assets/locales/" + sys.argv[1] + "/"
all_entries={}
for filename in os.listdir("assets/locales/en"): def dict_compare(d1, d2):
with open(os.path.join("assets/locales/en", filename), 'r') as locale_file: d1_keys = set(d1.keys())
created_locale = open(path + filename) d2_keys = set(d2.keys())
shared_keys = d1_keys.intersection(d2_keys)
def to_dict(text): added = d1_keys - d2_keys
removed = d2_keys - d1_keys
same = set(o for o in shared_keys if d1[o] == d2[o])
return added, removed, same
def to_dict(text):
result={} result={}
for i in text: for i in text:
if " = " in i: if " =" in i:
try: try:
result[i.split()[0]] = ' '.join(i.split()[2:]) result[i.split()[0]] = ' '.join(i.split()[2:])
@ -39,29 +49,26 @@ for filename in os.listdir("assets/locales/en"):
return result return result
def dict_compare(d1, d2): def get_line_num(text,pattern):
d1_keys = set(d1.keys()) line=1
d2_keys = set(d2.keys()) for i in text.split("\n"):
if pattern in i:
return line
line += 1
shared_keys = d1_keys.intersection(d2_keys) for filename in os.listdir("assets/locales/en"):
with open(os.path.join("assets/locales/en", filename), 'r') as locale_file:
added = d1_keys - d2_keys created_locale = open(path + filename)
removed = d2_keys - d1_keys
modified = {o : (d1[o], d2[o]) for o in shared_keys if d1[o] != d2[o]}
same = set(o for o in shared_keys if d1[o] == d2[o])
return added, removed, modified, same
expected=to_dict(locale_file) expected=to_dict(locale_file)
all_entries.update(expected)
expected2=to_dict(created_locale) expected2=to_dict(created_locale)
# TODO: why modified is not used? added, removed, same = dict_compare(expected, expected2)
added, removed, modified, same = dict_compare(expected, expected2)
if sys.argv[2] == "unused" or sys.argv[2] == "missing": if sys.argv[2] == "unused" or sys.argv[2] == "missing":
files = glob.glob("src/" + '/**/*.rs', recursive=True) files = glob.glob("src/" + '/**/*.rs', recursive=True)
used=[] used=[]
vars={}
for i in files: for i in files:
with open(i,"r") as script: with open(i,"r") as script:
text=script.read() text=script.read()
@ -69,10 +76,17 @@ for filename in os.listdir("assets/locales/en"):
for j in expected: for j in expected:
if '"'+j+'"' in text: if '"'+j+'"' in text:
used.append(j) used.append(j)
#elif sys.argv[2] == "missing": elif sys.argv[2] == "missing":
#for j in text.split(): for j in text.split():
#find all cases of tr( if 'tr("' in j:
index=j.find('tr("')
var_name=re.sub('[^\\w-]+', '',
j[index:].replace('tr("','')
.replace("Some",""))
# TODO: search multiple lines
vars[var_name] = [script.name, get_line_num(text,var_name)]
if sys.argv[2] == "unused":
for i in expected: for i in expected:
if i not in used: if i not in used:
print(f"{i} is not used ({locale_file.name})") print(f"{i} is not used ({locale_file.name})")
@ -103,4 +117,9 @@ for filename in os.listdir("assets/locales/en"):
print("") print("")
if sys.argv[2] == "missing":
added, removed, same = dict_compare(vars, all_entries)
if not added:
print("nothing is missing")
for i in added:
print(f"missing entry: {i} ({vars[i][0]}), line {vars[i][1]}")