|
| 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 | +importargparse |
| 9 | +importre |
| 10 | +importsubprocess |
| 11 | +importsys |
| 12 | +frompathlibimportPath |
| 13 | + |
| 14 | +defreset_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 | +defpopulate_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 | +ifresult.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 | +defpatch_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 | +forlineincontent: |
| 69 | +ifline.startswith(("source_file","source_lang")): |
| 70 | +continue |
| 71 | + |
| 72 | +ifline.startswith("file_filter"): |
| 73 | +line=line.replace("<lang>/","") |
| 74 | +line=line.replace("--","/") |
| 75 | + |
| 76 | +forpattern,replacementintext_to_replace.items(): |
| 77 | +ifpatterninline: |
| 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 | +defparse_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 | +returnparser.parse_args() |
| 102 | + |
| 103 | + |
| 104 | +defmain(): |
| 105 | +args=parse_args() |
| 106 | +TX_CONFIG=Path(".tx/config") |
| 107 | +ifargs.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() |