-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoggle_template.py
More file actions
75 lines (59 loc) · 2.49 KB
/
Copy pathtoggle_template.py
File metadata and controls
75 lines (59 loc) · 2.49 KB
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
65
66
67
68
69
70
71
72
73
74
75
import os
import sys
def replace_text_in_file(selected_file, text_to_find, replace_text):
"""Replaces specified text with specified replace text in selected file
:param(str) selected_file: the selected file
:param(str) text_to_find: the selected text to find
:param(str) replace_text: the selected text to replace with
:returns(bool): True if file was modified, false otherwise
"""
print(selected_file)
if os.path.basename(selected_file) not in ["Procfile", ".env"]:
if os.path.splitext(selected_file)[1] not in [".py", ".iml", ".xml"]:
return None
print(f"Working on file: {selected_file}")
with open(selected_file, "r", encoding="UTF-8") as file:
data = file.read()
if data != "":
data = data.replace(text_to_find, replace_text)
with open(selected_file, "w", encoding="UTF-8") as writefile:
writefile.write(data)
return True
return False
def find_and_replace_in_directory(dir_name, text_to_find, replace_text):
"""Finds occurrences for a specified text in all files inside a directory
and its subdirectories and replaces it with a specified replace text.
:param(str) dir_name: Directory to navigate
:param(str) text_to_find: Text to find
:param(str) replace_text: Text to replace with
:returns(int): number of files modified
"""
# Get the list of all files in directory tree at given path
file_list = []
modified_files = 0
for (dirpath, dirnames, filenames) in os.walk( # noqa # pylint: disable=unused-variable
dir_name
):
file_list += [
os.path.join(dirpath, file)
for file in filenames
if os.path.join(dirpath, file) != __file__
]
# Print the files
for file in file_list:
if replace_text_in_file(file, text_to_find, replace_text):
modified_files += 1
return modified_files
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Incorrect usage. Use like: python toggle_template.py enable | disable")
sys.exit(-1)
if (x := sys.argv[1]) not in ["enable", "disable"]:
print('Incorrect argument. Allowed values: ["enable", "disable"]')
sys.exit(-1)
MODIFIED_FILES = find_and_replace_in_directory(
os.getcwd(),
r"{{project_name}}" if x == "disable" else "project_name",
r"project_name" if x == "disable" else "{{project_name}}",
)
print(f"Total files modified: {MODIFIED_FILES}")