am-freescan/scripts/sync_scanner_titles.py

33 lines
1.6 KiB
Python
Raw Permalink Normal View History

"""Synchronize supplier operation names by method/path, never by editable node name."""
from pathlib import Path
import re
ROOT=Path(__file__).resolve().parents[1]
REGISTRY=ROOT/'am-backend/yudao-module-cmvr/src/main/java/com/cmvr/scanner/ScannerOperation.java'
DOCUMENT=ROOT/'docs/integrations/scanner-api.md'
def documented_titles(source):
result={}
for block in re.split(r'(?m)(?=^## (?:GET|POST) )',source):
heading=re.match(r'^## (GET|POST) (.+)',block)
route=re.search(r'(?m)^(GET|POST) (/[^\s]*)\s*$',block)
if not heading or not route:continue
method,path=route.groups()
path=re.sub(r'^/api/v1/tasks/[a-fA-F0-9]{32}$','/api/v1/tasks/{taskId}',path)
if heading[1]!=method or (method,path) in result:raise ValueError('Duplicate or inconsistent supplier operation')
result[(method,path)]=heading[2].strip()
return result
def main():
titles=documented_titles(DOCUMENT.read_text(encoding='utf-8-sig'))
original=REGISTRY.read_text(encoding='utf-8')
pattern=r'(?m)^( \w+\("(GET|POST)", "([^"]+)", )"([^"]+)"'
rows=list(re.finditer(pattern,original))
if len(titles)!=len(rows) or set(titles)!={(r[2],r[3]) for r in rows}:raise ValueError('Supplier operation inventory differs; no files written')
changed=sum(titles[(r[2],r[3])]!=r[4] for r in rows)
result=re.sub(pattern,lambda r:r[1]+'"'+titles[(r[2],r[3])]+'"',original)
REGISTRY.write_text(result,encoding='utf-8')
print(f'Synchronized {len(rows)} supplier operation names; {changed} changed')
if __name__=='__main__':main()