| 1 |
#!/usr/bin/env python3 |
| 2 |
import os |
| 3 |
import re |
| 4 |
import sys |
| 5 |
|
| 6 |
root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 7 |
sys.path.insert(0, root) |
| 8 |
|
| 9 |
from DisplayCAL import jsondict |
| 10 |
from DisplayCAL.config import CONFIG_HOME |
| 11 |
from DisplayCAL.util_os import listdir_re |
| 12 |
|
| 13 |
|
| 14 |
def quote(obj): |
| 15 |
if isinstance(obj, str): |
| 16 |
return '"%s"' % obj.replace("\\", "\\\\").replace('"', '\\"').replace( |
| 17 |
"\n", "\\n" |
| 18 |
) |
| 19 |
else: |
| 20 |
return repr(obj) |
| 21 |
|
| 22 |
|
| 23 |
def find_potentially_unused_strings(filepath, keys): |
| 24 |
ldict = jsondict.JSONDict(filepath) |
| 25 |
|
| 26 |
merged = dict() |
| 27 |
merged["*"] = "" |
| 28 |
|
| 29 |
count = 0 |
| 30 |
for key in sorted(ldict.keys()): |
| 31 |
merged[key.encode("UTF-8")] = ldict[key].encode("UTF-8") |
| 32 |
if not key.startswith("*") and not key.startswith("!") and key not in keys: |
| 33 |
print( |
| 34 |
"Found potentially unused '%s' in '%s'" |
| 35 |
% (key, os.path.basename(filepath)) |
| 36 |
) |
| 37 |
count += 1 |
| 38 |
print( |
| 39 |
"Found %i potentially unused keys in '%s'" % (count, os.path.basename(filepath)) |
| 40 |
) |
| 41 |
|
| 42 |
|
| 43 |
def main(): |
| 44 |
keys = {} |
| 45 |
for (dirpath, _dirnames, filenames) in os.walk(os.path.join(root, "DisplayCAL")): |
| 46 |
for filename in filenames: |
| 47 |
ext = os.path.splitext(filename)[1][1:] |
| 48 |
if ext not in ("py", "pyw", "xrc"): |
| 49 |
continue |
| 50 |
filepath = os.path.join(dirpath, filename) |
| 51 |
with open(filepath, "rb") as py: |
| 52 |
code = py.read().decode() |
| 53 |
if ext == "xrc": |
| 54 |
pattern = r"<(?:label|title|tooltip)>([^>]+)</(?:label|title|tooltip)>" |
| 55 |
else: |
| 56 |
pattern = r'(?:getstr\(|(?:lstr|msg|msgid|msgstr|title)\s*=)\s*["\']([^"\']+)["\']' |
| 57 |
for match in re.findall(pattern, code): |
| 58 |
if match not in keys: |
| 59 |
keys[match.decode("UTF-8")] = 1 |
| 60 |
print(len(keys), "unique keys in py/pyw/xrc") |
| 61 |
usage_path = os.path.join(CONFIG_HOME, "localization_usage.json") |
| 62 |
if os.path.isfile(usage_path): |
| 63 |
usage = jsondict.JSONDict(usage_path) |
| 64 |
usage.load() |
| 65 |
keys.update(usage) |
| 66 |
print(len(keys), "unique keys after merging localization_usage.json") |
| 67 |
for langfile in listdir_re( |
| 68 |
os.path.join(root, "DisplayCAL", "lang"), r"^\w+\.json$" |
| 69 |
): |
| 70 |
if langfile != "en.json": |
| 71 |
find_potentially_unused_strings(os.path.join("lang", langfile), keys.keys()) |
| 72 |
input("Press any key to continue") |
| 73 |
print("") |
| 74 |
|
| 75 |
|
| 76 |
if __name__ == "__main__": |
| 77 |
if "-h" in sys.argv[1:] or "--help" in sys.argv[1:]: |
| 78 |
print("Usage: %s" % os.path.basename(sys.argv[0])) |
| 79 |
print("Finds potentially unused strings in localizations") |
| 80 |
else: |
| 81 |
main() |