2023-12-13 16:55:30 +00:00
|
|
|
from pathlib import Path
|
|
|
|
from typing import Dict
|
|
|
|
|
|
|
|
|
|
|
|
def find_and_replace(source: str, replacements: Dict[str, str]) -> str:
|
|
|
|
rtn = source
|
|
|
|
|
|
|
|
# replace keys in deterministic alphabetical order
|
|
|
|
finds = sorted(replacements.keys())
|
|
|
|
for find in finds:
|
|
|
|
replace = replacements[find]
|
|
|
|
rtn = rtn.replace(find, replace)
|
|
|
|
return rtn
|
|
|
|
|
|
|
|
|
|
|
|
def replace_file(source: Path, replacements: Dict[str, str]) -> None:
|
2023-12-13 19:14:51 +00:00
|
|
|
try:
|
|
|
|
content = source.read_text()
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
# binary file
|
|
|
|
return
|
|
|
|
new_content = find_and_replace(content, replacements)
|
|
|
|
if new_content != content:
|
|
|
|
source.write_text(new_content)
|
2023-12-13 16:55:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def replace_glob(parent: Path, glob: str, replacements: Dict[str, str]) -> None:
|
|
|
|
for file in parent.glob(glob):
|
|
|
|
if not file.is_file():
|
|
|
|
continue
|
|
|
|
replace_file(file, replacements)
|