11"""Please note that this script requires a Transifex API token to run."""
2+ import glob
23import subprocess
34from functools import partial
45from pathlib import Path
@@ -32,20 +33,33 @@ def add_files(version: str):
3233def name_replacer (match :re .Match [str ]):
3334prefix ,resource = match .group ("prefix" ,"resource" )
3435override_prefix = prefix .replace ("file_filter" ,"trans.zh_CN" )
35- override_resource = (
36+ pattern = (
3637resource .replace ("trans/<lang>/" ,"" )
37- .replace ("--" ,"/" )
3838 .replace ("glossary_" ,"glossary" )
39- .replace ("_" ,"." )
39+ .replace ("--" ,"/" )
40+ .replace ("_" ,"?" )
4041 )
41- return f"{ prefix } { resource } \n { override_prefix } { override_resource } "
42-
43-
44- def patch_config ():
42+ matches = list (glob .glob (pattern .replace (".po" ,".rst" )))
43+ if not matches :
44+ print ("missing" ,pattern )
45+ return f"{ prefix } { resource } \n { override_prefix } { pattern } "
46+ elif len (matches )== 1 :
47+ filename = matches [0 ].replace (".rst" ,".po" ).replace ("\\ " ,"/" )
48+ else :
49+ raise ValueError ("multi match" ,resource ,pattern ,matches )
50+ return f"{ prefix } { resource } \n { override_prefix } { filename } "
51+
52+
53+ def patch_config (path :str ):
4554tx_config_path = Path (".tx" ,"config" )
4655
4756config_content = tx_config_path .read_text ("utf-8" )
57+
58+ cwd = os .getcwd ()
59+ os .chdir (path )
4860config_content = FILTER_PATTERN .sub (name_replacer ,config_content )
61+ os .chdir (cwd )
62+
4963tx_config_path .write_text (config_content ,"utf-8" )
5064
5165
@@ -55,13 +69,14 @@ def patch_config():
5569
5670parser = ArgumentParser ()
5771
58- parser .add_argument ("--token" )
59- parser .add_argument ("--version" )
72+ parser .add_argument ("--token" ,required = True )
73+ parser .add_argument ("--version" ,required = True )
74+ parser .add_argument ("--doc-path" ,required = True )
6075
6176params = parser .parse_args ()
6277
6378os .environ ["TX_TOKEN" ]= params .token
6479
6580init_project ()
6681add_files (params .version )
67- patch_config ()
82+ patch_config (params . doc_path )