|
1 | | -#!/usr/bin/env python3 |
| 1 | +"""Please note that this script requires a Transifex API token to run.""" |
2 | 2 | importglob |
3 | | -importjson |
4 | | -importos |
| 3 | +importsubprocess |
| 4 | +fromfunctoolsimportpartial |
| 5 | +frompathlibimportPath |
5 | 6 | importre |
6 | | -importsys |
7 | | -importurllib.request |
| 7 | +importos |
| 8 | + |
| 9 | +run=partial(subprocess.run,check=True) |
| 10 | + |
8 | 11 |
|
| 12 | +definit_project(): |
| 13 | +run(["tx","init"]) |
9 | 14 |
|
10 | | -deflist_resources(token,project): |
11 | | -auth_handler=urllib.request.HTTPBasicAuthHandler() |
12 | | -auth_handler.add_password( |
13 | | -realm="api",uri="https://api.transifex.com/",user="api",passwd=token |
| 15 | + |
| 16 | +defadd_files(project_name:str): |
| 17 | +run( |
| 18 | + [ |
| 19 | +"tx", |
| 20 | +"add", |
| 21 | +"remote", |
| 22 | +"--file-filter", |
| 23 | +"trans/<lang>/<resource_slug>.<ext>", |
| 24 | +f"https://www.transifex.com/python-doc/{project_name}/dashboard/", |
| 25 | + ] |
14 | 26 | ) |
15 | | -opener=urllib.request.build_opener(auth_handler) |
16 | | -urllib.request.install_opener(opener) |
17 | | -next_= ( |
18 | | -"https://api.transifex.com/organizations/python-doc/projects/" |
19 | | -+project |
20 | | -+"/resources/" |
| 27 | + |
| 28 | + |
| 29 | +FILTER_PATTERN=re.compile( |
| 30 | +r"^(?P<prefix>file_filter( *)=( *))(?P<resource>.+)$",re.MULTILINE |
| 31 | +) |
| 32 | + |
| 33 | + |
| 34 | +defname_replacer(match:re.Match[str]): |
| 35 | +prefix,resource=match.group("prefix","resource") |
| 36 | +override_prefix=prefix.replace("file_filter","trans.zh_CN") |
| 37 | +pattern= ( |
| 38 | +resource.replace("trans/<lang>/","") |
| 39 | + .replace("glossary_","glossary") |
| 40 | + .replace("--","/") |
| 41 | + .replace("_","?") |
21 | 42 | ) |
22 | | -resources= [] |
23 | | -whileTrue: |
24 | | -resp=urllib.request.urlopen(next_) |
25 | | -result=json.loads(resp.read().decode("utf-8")) |
26 | | -resources.extend([i["slug"]foriinresult]) |
27 | | -link=re.findall('<([^<]*)>; rel="next"',resp.getheader("Link")or"") |
28 | | -ifnotlink: |
29 | | -break |
30 | | -next_=link[0] |
31 | | -returnresources |
32 | | - |
33 | | - |
34 | | -defrender_config(doc_dir,project,resources): |
35 | | -os.chdir(doc_dir) |
36 | | -tpl=""" |
37 | | -
|
38 | | -[{project}.{resource}] |
39 | | -trans.zh_CN = {filename} |
40 | | -source_lang = en |
41 | | -type = PO""" |
42 | | -conf="""[main] |
43 | | -host = https://www.transifex.com""" |
44 | | -forresourceinsorted(resources): |
45 | | -ifresource=="glossary_": |
46 | | -filename="glossary.po" |
47 | | -elifresource=="sphinx": |
48 | | -filename="sphinx.po" |
49 | | -else: |
50 | | -pattern=resource.replace("--","/").replace("_","?") |
51 | | -matches=glob.glob(pattern+".rst") |
52 | | -iflen(matches)==0: |
53 | | -print("missing",resource,file=sys.stderr) |
54 | | -continue |
55 | | -eliflen(matches)==1: |
56 | | -filename=matches[0].replace(".rst",".po") |
57 | | -else: |
58 | | -print("multi match",resource,pattern,matches,file=sys.stderr) |
59 | | -conf+=tpl.format(project=project,resource=resource,filename=filename) |
60 | | -returnconf |
| 43 | +matches=list(glob.glob(pattern.replace(".po",".rst"))) |
| 44 | +ifnotmatches: |
| 45 | +print("missing",pattern) |
| 46 | +returnf"{prefix}{resource}\n{override_prefix}{pattern.replace('?','_')}" |
| 47 | +eliflen(matches)==1: |
| 48 | +filename=matches[0].replace(".rst",".po").replace("\\","/") |
| 49 | +else: |
| 50 | +raiseValueError("multi match",resource,pattern,matches) |
| 51 | +returnf"{prefix}{resource}\n{override_prefix}{filename}" |
| 52 | + |
| 53 | + |
| 54 | +defpatch_config(path:str): |
| 55 | +tx_config_path=Path(".tx","config") |
| 56 | + |
| 57 | +config_content=tx_config_path.read_text("utf-8") |
| 58 | + |
| 59 | +cwd=os.getcwd() |
| 60 | +os.chdir(path) |
| 61 | +config_content=FILTER_PATTERN.sub(name_replacer,config_content) |
| 62 | +os.chdir(cwd) |
| 63 | + |
| 64 | +tx_config_path.write_text(config_content,"utf-8") |
61 | 65 |
|
62 | 66 |
|
63 | 67 | if__name__=="__main__": |
64 | | -importargparse |
| 68 | +fromargparseimportArgumentParser |
| 69 | + |
| 70 | +parser=ArgumentParser() |
| 71 | + |
| 72 | +parser.add_argument("--token",default="") |
| 73 | +parser.add_argument("--project-name",required=True) |
| 74 | +parser.add_argument("--doc-path",required=True) |
65 | 75 |
|
66 | | -parser=argparse.ArgumentParser() |
67 | | -parser.add_argument("--token") |
68 | | -parser.add_argument("--project") |
69 | | -parser.add_argument("--doc-dir") |
70 | | -args=parser.parse_args() |
| 76 | +params=parser.parse_args() |
71 | 77 |
|
72 | | -resources=list_resources(args.token,args.project) |
73 | | -conf=render_config(args.doc_dir,args.project,resources) |
74 | | -print(conf) |
| 78 | +ifparams.token: |
| 79 | +os.environ["TX_TOKEN"]=params.token |
75 | 80 |
|
76 | | -# vim: set et ts=4 sw=4 sts=4: |
| 81 | +init_project() |
| 82 | +add_files(params.project_name) |
| 83 | +patch_config(params.doc_path) |