Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork90
Expand file tree
/
Copy pathgenerate_tx_config.py
More file actions
executable file
·87 lines (66 loc) · 2.43 KB
/
generate_tx_config.py
File metadata and controls
executable file
·87 lines (66 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""Please note that this script requires a Transifex API token to run."""
importglob
importsubprocess
fromfunctoolsimportpartial
frompathlibimportPath
importre
importos
run=partial(subprocess.run,check=True)
definit_project():
run(["tx","init"])
defadd_files(project_name:str):
run(
[
"tx",
"add",
"remote",
"--file-filter",
"trans/<lang>/<resource_slug>.<ext>",
f"https://www.transifex.com/python-doc/{project_name}/dashboard/",
]
)
FILTER_PATTERN=re.compile(
r"^(?P<prefix>file_filter( *)=( *))(?P<resource>.+)$",re.MULTILINE
)
defname_replacer(match:re.Match[str]):
prefix,resource=match.group("prefix","resource")
override_prefix=prefix.replace("file_filter","trans.zh_CN")
pattern= (
resource.replace("trans/<lang>/","")
.replace("glossary_","glossary")
.replace("--","/")
.replace("_","?")
)
matches=list(glob.glob(pattern.replace(".po",".rst")))
ifnotmatches:
print("missing",pattern)
returnf"{prefix}{resource}\n{override_prefix}{pattern.replace('?','_')}"
eliflen(matches)==1:
filename=matches[0].replace(".rst",".po").replace("\\","/")
else:
raiseValueError("multi match",resource,pattern,matches)
returnf"{prefix}{resource}\n{override_prefix}{filename}"
defpatch_config(path:str):
tx_config_path=Path(".tx","config")
config_content=tx_config_path.read_text("utf-8")
cwd=os.getcwd()
os.chdir(path)
config_content=FILTER_PATTERN.sub(name_replacer,config_content)
config_content=re.sub(r'replace_edited_strings.*\n','',config_content)
config_content=re.sub(r'keep_translations.*\n','',config_content)
config_content=re.sub(r'0\ntrans\.zh_CN.*\n','0\n',config_content)
config_content=config_content.replace(' =','=')
os.chdir(cwd)
tx_config_path.write_text(config_content,"utf-8")
if__name__=="__main__":
fromargparseimportArgumentParser
parser=ArgumentParser()
parser.add_argument("--token",default="")
parser.add_argument("--project-name",required=True)
parser.add_argument("--doc-path",required=True)
params=parser.parse_args()
ifparams.token:
os.environ["TX_TOKEN"]=params.token
init_project()
add_files(params.project_name)
patch_config(params.doc_path)