File: replace_words.py

package info (click to toggle)
python-inline-snapshot 0.31.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,708 kB
  • sloc: python: 9,261; makefile: 39; sh: 32
file content (31 lines) | stat: -rw-r--r-- 838 bytes parent folder | download
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
import re
import sys


def replace_words(file_path, replacements):
    with open(file_path) as file:
        content = original_content = file.read()

    for old_word, new_word in replacements.items():
        content = re.sub(rf"\b{re.escape(old_word)}\b", new_word, content)

    content = re.sub(
        rf"\(#([0-9]+)\)",
        lambda m: f"([#{m[1]}](https://github.com/15r10nk/inline-snapshot/issues/{m[1]}))",
        content,
    )

    if original_content != content:
        with open(file_path, "w") as file:
            print("change:", file_path)
            file.write(content)


if __name__ == "__main__":

    replacements = {
        "http://localhost:8000/inline-snapshot/": "https://15r10nk.github.io/inline-snapshot/latest/",
    }

    for file_path in sys.argv[1:]:
        replace_words(file_path, replacements)