-
Notifications
You must be signed in to change notification settings - Fork 7
/
meta.py
64 lines (47 loc) · 2.32 KB
/
meta.py
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
from typing import Callable, TypeVar, Tuple
import os
import re
import sys
T = TypeVar("T")
def update_variables(file_path: str, patterns: Tuple[Callable[[T], str], ...], updated_values: Tuple[T, ...]):
"""
Updates the variables inside the given file to the given updated values.
**Each variable should occur exactly once in the file!**
:param file_path: Python file
:param patterns: For each variable, function that takes a value and returns the string of the form `name = value` that defines the variable
:param updated_values: For each variable, new value for the variable
"""
if not os.path.exists(file_path):
raise ValueError(f"{file_path} does not exist. Run this script from the root directory of this repo.")
if len(patterns) != len(updated_values):
raise ValueError("Need as many patterns as updated values")
updates = [patterns[i](updated_values[i]) for i in range(len(patterns))]
apply = input(f"Do you want to apply the update to {file_path}? [y,n,q]: ").lower()
if apply == "y":
with open(file_path, "r") as f:
file_data = f.read()
for i in range(len(patterns)):
regex = re.compile(re.escape(patterns[i]("できれば現れない文字")).replace("できれば現れない文字", ".*"))
if not regex.search(file_data):
print(f"Variable not found in {file_path}. Wrong file name?")
file_data = regex.sub(updates[i], file_data)
with open(file_path, "w") as f:
f.write(file_data)
print(f"Successfully updated {file_path}")
elif apply == "q":
sys.exit(0)
else:
do_print = input("Print updated values? [y,n,q]: ")
if do_print.lower() == "y":
for update in updates:
print(update)
print("No file update")
def update_variable(file_path: str, pattern: Callable[[T], str], updated_value: T):
"""
Updates the variable inside the given file to the given updated value.
**The variable should occur exactly once in the file!**
:param file_path: Python file
:param pattern: Function that takes a value and returns the string of the form `name = value` that defines the variable
:param updated_value: New value for the variable
"""
update_variables(file_path, (pattern,), (updated_value,))