|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Generate the .tx/config file based on the existing projects |
| 4 | +in Python docs Transifex project. Takes an project slug as |
| 5 | +positional argument, e.g. python-newest or python-313 |
| 6 | +""" |
| 7 | + |
| 8 | +import argparse |
| 9 | +import re |
| 10 | +import subprocess |
| 11 | +import sys |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +def reset_tx_config(txconfig: Path): |
| 15 | + """Create or reset the .tx/config file with basic header.""" |
| 16 | + txconfig.parent.mkdir(exist_ok=True) |
| 17 | + txconfig.write_text("[main]\nhost = https://www.transifex.com\n", encoding="utf-8") |
| 18 | + print("Initialized .tx/config.") |
| 19 | + |
| 20 | + |
| 21 | +def populate_resources_from_remote(config_file: Path, tx_project: str): |
| 22 | + """Add the remote resources from the Transifex project to .tx/config.""" |
| 23 | + result = subprocess.run([ |
| 24 | + "tx", "--config", str(config_file), "add", "remote", |
| 25 | + "--file-filter", "<lang>/<resource_slug>.<ext>", |
| 26 | + f"https://app.transifex.com/python-doc/{tx_project}/" |
| 27 | + ], check=True) |
| 28 | + if result.returncode != 0: |
| 29 | + print("Failed to add the resources from remote:") |
| 30 | + print(result.stderr.strip()) |
| 31 | + sys.exit(result.returncode) |
| 32 | + print("Added remote resources to Transifex.") |
| 33 | + |
| 34 | + |
| 35 | +def patch_config(txconfig: Path): |
| 36 | + """Patch .tx/config to fixing PO filenames to match the expected.""" |
| 37 | + content = txconfig.read_text(encoding="utf-8").splitlines() |
| 38 | + new_lines = [] |
| 39 | + |
| 40 | + text_to_replace = { |
| 41 | + "2_": "2.", |
| 42 | + "3_": "3.", |
| 43 | + "glossary_": "glossary", |
| 44 | + "collections_": "collections.", |
| 45 | + "compression_": "compression.", |
| 46 | + "concurrent_": "concurrent.", |
| 47 | + "curses_": "curses.", |
| 48 | + "email_": "email.", |
| 49 | + "html_": "html.", |
| 50 | + "http_": "http.", |
| 51 | + "importlib_resources_": "importlib.resources.", |
| 52 | + "importlib_": "importlib.", |
| 53 | + "logging_": "logging.", |
| 54 | + "multiprocessing_": "multiprocessing.", |
| 55 | + "os_": "os.", |
| 56 | + "string_": "string.", |
| 57 | + "sys_monitoring": "sys.monitoring", |
| 58 | + "tkinter_": "tkinter.", |
| 59 | + "unittest_": "unittest.", |
| 60 | + "urllib_": "urllib.", |
| 61 | + "xml_dom_": "xml.dom.", |
| 62 | + "xml_etree_": "xml.etree.", |
| 63 | + "xmlrpc_": "xmlrpc.", |
| 64 | + "xml_sax_": "xml.sax.", |
| 65 | + "xml_": "xml." |
| 66 | + } |
| 67 | + |
| 68 | + for line in content: |
| 69 | + if line.startswith(("source_file", "source_lang")): |
| 70 | + continue |
| 71 | + |
| 72 | + if line.startswith("file_filter"): |
| 73 | + line = line.replace("<lang>/", "") |
| 74 | + line = line.replace("--", "/") |
| 75 | + |
| 76 | + for pattern, replacement in text_to_replace.items(): |
| 77 | + if pattern in line: |
| 78 | + line = line.replace(pattern, replacement) |
| 79 | + break |
| 80 | + |
| 81 | + new_lines.append(line) |
| 82 | + |
| 83 | + text = "\n".join(new_lines) |
| 84 | + |
| 85 | + txconfig.write_text(text + "\n", encoding="utf-8") |
| 86 | + print("Updated .tx/config with character substitutions") |
| 87 | + |
| 88 | + |
| 89 | +def parse_args(): |
| 90 | + parser = argparse.ArgumentParser(description=__doc__) |
| 91 | + parser.add_argument( |
| 92 | + "--root-path", |
| 93 | + "-p", |
| 94 | + default=Path("."), |
| 95 | + help="Path to the translation files, and also the .tx/config" |
| 96 | + ) |
| 97 | + parser.add_argument( |
| 98 | + "tx_project", |
| 99 | + help="Name of the Transifex project to query resources from (e.g. python-newest or python-313)" |
| 100 | + ) |
| 101 | + return parser.parse_args() |
| 102 | + |
| 103 | + |
| 104 | +def main(): |
| 105 | + args = parse_args() |
| 106 | + TX_CONFIG = Path(".tx/config") |
| 107 | + if args.root_path: |
| 108 | + TX_CONFIG = args.root_path / TX_CONFIG |
| 109 | + reset_tx_config(TX_CONFIG) |
| 110 | + populate_resources_from_remote(TX_CONFIG, args.tx_project) |
| 111 | + patch_config(TX_CONFIG) |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + main() |
0 commit comments