#!/usr/bin/python3 # SPDX-License-Identifier: WTFPL """ Database localization helper tool Parse enums and sets from sql schema script for translatable strings """ import os import sys import argparse import re __author__ = "Thomas Hooge" __copyright__ = "Copyright 2026" __version__ = "0.1" __email__ = "thomas@hoogi.de" __status__ = "Development" COLUMN_RE = re.compile( r"^\s*(?P\w+)\s+(?Penum|set)\s*" r"\(\s*(?P'[a-z]+'(?:\s*,\s*'[a-z]+')*)\s*\)", re.IGNORECASE ) TABLE_RE = re.compile( r"CREATE TABLE (?P\w+)", re.IGNORECASE ) def load_msgids(sql): current_table = None for lineno, line in enumerate(sql.splitlines(), 1): table_match = TABLE_RE.search(line) if table_match: current_table = table_match.group("table") column_match = COLUMN_RE.search(line) if current_table and column_match: column = column_match.group("column") values = re.findall( r"'([a-z]+)'", column_match.group("values"), ) context = f"{current_table}.{column}" for value in values: yield context, value, lineno def write_pot(msgids, output, filename): output.write( 'msgid ""\n' 'msgstr ""\n' '"Content-Type: text/plain; charset=UTF-8\\n"\n' '"Content-Transfer-Encoding: 8bit\\n"\n' '"MIME-Version: 1.0\\n"\n' '\n' ) for context, msgid, lineno in sorted(set(msgids)): output.write(f"#: {filename}:{lineno}\n") output.write(f'msgctxt "{context}"\n') output.write(f'msgid "{msgid}"\n') output.write('msgstr ""\n\n') if __name__ == "__main__": parser = argparse.ArgumentParser( description=( "Database localization helper tool\n" "Parse enums and sets from SQL schema script " "for translatable strings" ), formatter_class=argparse.RawDescriptionHelpFormatter, ) parser.add_argument( "sql_file", help="SQL schema file to parse", ) parser.add_argument( "--version", action="version", version=f"%(prog)s {__version__}", ) args = parser.parse_args() try: with open(args.sql_file, "r", encoding="utf-8") as f: sql = f.read() except OSError as e: parser.error(str(e)) write_pot(load_msgids(sql), sys.stdout, args.sql_file)