Files
WinDirStat-Export/tools/gen_langs.py
T
Lucas WintherandClaude Opus 5.5 e28fbeb0b3 Add wdsexport: Linux directory scan to WinDirStat 2.x saved-results CSV
Single-file, stdlib-only CLI that writes the CSV format WinDirStat's
CsvLoader accepts (parent-before-child rows, subtree totals, localized
headers for all 25 UI languages). Includes install.sh, pyproject entry
point, unit tests and a generator for the language table.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 22:01:02 +01:00

54 lines
2.1 KiB
Python
Executable File

#!/usr/bin/env python3
"""Regenerate the LANGS table in wdsexport.py from WinDirStat's language files.
Usage: python3 tools/gen_langs.py [REF] (REF defaults to master)
"""
import json
import os
import re
import sys
import urllib.request
REPO = "windirstat/windirstat"
KEYS = ["IDS_COL_NAME", "IDS_COL_FILES", "IDS_COL_FOLDERS", "IDS_COL_SIZE_LOGICAL",
"IDS_COL_SIZE_PHYSICAL", "IDS_COL_ATTRIBUTES", "IDS_COL_LAST_CHANGE", "IDS_COL_INDEX"]
TARGET = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "wdsexport.py")
def fetch(url):
req = urllib.request.Request(url, headers={"User-Agent": "wdsexport-gen-langs"})
with urllib.request.urlopen(req, timeout=30) as r:
return r.read()
def main():
ref = sys.argv[1] if len(sys.argv) > 1 else "master"
tree = json.loads(fetch(f"https://api.github.com/repos/{REPO}/git/trees/{ref}?recursive=1"))
paths = sorted(t["path"] for t in tree["tree"]
if re.fullmatch(r"windirstat/res/langs/lang_[\w-]+\.txt", t["path"]))
langs = {}
for path in paths:
code = re.search(r"lang_([\w-]+)\.txt$", path).group(1)
text = fetch(f"https://raw.githubusercontent.com/{REPO}/{ref}/{path}").decode("utf-8-sig")
values = dict(line.split("=", 1) for line in text.splitlines() if "=" in line)
missing = [k for k in KEYS if k not in values]
if missing:
print(f"skipping {code}: missing {missing}", file=sys.stderr)
continue
row = [values[k] for k in KEYS]
if any('"' in v for v in row):
print(f"skipping {code}: column name contains a double quote", file=sys.stderr)
continue
langs[code] = row
body = "LANGS = {\n" + "".join(f" {c!r}: {v!r},\n" for c, v in langs.items()) + "}\n"
src = open(TARGET, encoding="utf-8").read()
new = re.sub(r"(# @@LANGS-BEGIN@@\n).*?(# @@LANGS-END@@)",
lambda m: m.group(1) + body + m.group(2), src, flags=re.S)
open(TARGET, "w", encoding="utf-8").write(new)
print(f"wrote {len(langs)} languages to {os.path.normpath(TARGET)}", file=sys.stderr)
if __name__ == "__main__":
main()