sleepy-launcher/fluentscan.py

153 lines
3.8 KiB
Python
Raw Normal View History

#!/usr/bin/python
# Author: @xstraok
# Modified by Observer KRypt0n_ <https://github.com/krypt0nn>
import os
import sys
import glob
2023-04-18 02:52:56 +03:00
import re
2023-04-18 17:55:06 +03:00
valid_commands = ["diff", "unused", "missing"]
if len(sys.argv) < 3:
print(f"Command format: ./fluentscan.py [command] [locale]\nAvailable commands: {valid_commands}")
sys.exit()
2023-04-18 17:55:06 +03:00
if sys.argv[1] not in valid_commands:
print(f"Invalid command \"{sys.argv[1]}\". Available commands: {valid_commands}")
sys.exit()
2023-04-18 17:55:06 +03:00
path = "assets/locales/" + sys.argv[2] + "/"
try:
2023-04-18 17:55:06 +03:00
open(path + "/main.ftl", "r").close()
except:
print(f"{path} does not exist")
2023-04-18 17:55:06 +03:00
sys.exit()
2023-04-18 17:55:06 +03:00
all_entries = {}
2023-04-18 02:52:56 +03:00
def dict_compare(d1, d2):
d1_keys = set(d1.keys())
d2_keys = set(d2.keys())
2023-04-18 17:55:06 +03:00
2023-04-18 02:52:56 +03:00
shared_keys = d1_keys.intersection(d2_keys)
2023-04-18 02:52:56 +03:00
added = d1_keys - d2_keys
removed = d2_keys - d1_keys
2023-04-18 02:52:56 +03:00
same = set(o for o in shared_keys if d1[o] == d2[o])
2023-04-18 02:52:56 +03:00
return added, removed, same
2023-04-18 02:52:56 +03:00
def to_dict(text):
result={}
2023-04-18 02:52:56 +03:00
for i in text:
if " =" in i:
try:
result[i.split()[0]] = ' '.join(i.split()[2:])
2023-04-18 02:52:56 +03:00
except:
pass
2023-04-18 02:52:56 +03:00
elif i:
result[list(result.keys())[-1]] += i
2023-04-18 02:52:56 +03:00
return result
2023-04-18 02:52:56 +03:00
def get_line_num(text,pattern):
2023-04-18 17:55:06 +03:00
line = 1
2023-04-18 02:52:56 +03:00
for i in text.split("\n"):
if pattern in i:
return line
2023-04-18 17:55:06 +03:00
2023-04-18 02:52:56 +03:00
line += 1
2023-04-18 02:52:56 +03:00
for filename in os.listdir("assets/locales/en"):
with open(os.path.join("assets/locales/en", filename), 'r') as locale_file:
created_locale = open(path + filename)
2023-04-18 17:55:06 +03:00
expected = to_dict(locale_file)
expected2 = to_dict(created_locale)
2023-04-18 02:52:56 +03:00
all_entries.update(expected)
2023-04-18 02:52:56 +03:00
added, removed, same = dict_compare(expected, expected2)
2023-04-18 17:55:06 +03:00
if sys.argv[1] == "unused" or sys.argv[1] == "missing":
files = glob.glob("src/**/*.rs", recursive=True)
used = []
vars = {}
for i in files:
2023-04-18 17:55:06 +03:00
with open(i, "r") as script:
text = script.read()
if sys.argv[1] == "unused":
for j in expected:
2023-04-18 17:55:06 +03:00
if f"\"{j}\"" in text:
used.append(j)
2023-04-18 17:55:06 +03:00
elif sys.argv[1] == "missing":
2023-04-18 02:52:56 +03:00
for j in text.split():
# TODO: ignore comments
2023-04-18 02:52:56 +03:00
if 'tr("' in j:
2023-04-18 17:55:06 +03:00
index = j.find('tr("')
var_name = re.sub('[^\\w-]+', '', j[index:].replace('tr("', '').replace("Some", ""))
# TODO: index multiple matches
2023-04-18 02:52:56 +03:00
vars[var_name] = [script.name, get_line_num(text,var_name)]
2023-04-18 17:55:06 +03:00
if sys.argv[1] == "unused":
2023-04-18 02:52:56 +03:00
for i in expected:
if i not in used:
print(f"[{locale_file.name}]\n"
2023-04-18 17:55:06 +03:00
" [Unused]\n"
f" {i}")
continue
2023-04-18 17:55:06 +03:00
if (added or removed or same) and sys.argv[1] == "diff":
print(f"[{created_locale.name[15:]}]")
if added:
print(" [Added]")
for i in added:
print(f" {i} = {expected[i]}")
if removed:
print(" [Removed]")
for i in removed:
print(f" {i} = {expected2[i]}")
#workaround
if same and same != "set()":
print(" [Untranslated]")
for i in same:
print(f" {i} = {expected[i]}")
print("")
2023-04-18 17:55:06 +03:00
if sys.argv[1] == "missing":
2023-04-18 02:52:56 +03:00
added, removed, same = dict_compare(vars, all_entries)
2023-04-18 17:55:06 +03:00
2023-04-18 02:52:56 +03:00
if not added:
2023-04-18 17:55:06 +03:00
print("Nothing is missing")
2023-04-18 02:52:56 +03:00
for i in added:
print(f"[{vars[i][0]}, line {vars[i][1]}]\n"
" [Missing]\n"
f" {i}")