-
Notifications
You must be signed in to change notification settings - Fork 0
/
apply_skin.py
131 lines (104 loc) · 3.7 KB
/
apply_skin.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import os
def upper_camel(key):
return key.replace("-", " ").title().replace(" ", "")
def camel(key):
return key.lower()[0] + upper_camel(key)[1:]
def upper_snake(key):
return key.replace("-", "_").upper()
def upper_spaces(key):
return key.replace("-", " ").upper()
def snake(key):
return key.replace("-", "_")
def spaces(key):
return key.replace("-", " ")
def upper_train(key):
words = key.split("-")
return "-".join(" ".join(words).title().split(" "))
def train(key):
return key
# If the key is a single word, we can't tell if it
# should be translated to snake, camel, train, or spaces
# so we do our best to pick the right one first and then
# manual edits will have to be made
TRANSFORMATIONS = [
upper_camel,
upper_snake,
upper_spaces,
upper_train,
snake,
camel,
train,
spaces,
]
def get_config(names_file):
config = {}
with open(names_file, "r") as names:
for line in names:
if line[0] == "#":
continue
if line == "\n":
continue
old, new = line.strip().split(":")
if not new:
continue
config[old] = new
return config
def get_file_list(directory):
file_list = []
for root, _, files in os.walk(directory):
if root.find("node_modules") == -1:
for name in files:
file_list.append(os.path.join(root, name))
return [ f for f in file_list if f.endswith( ('.js', '.py', '.css') ) ]
def get_rename_file_list(directory, repo_root):
file_list = []
for root, _, files in os.walk(directory):
if root.find("node_modules") == -1:
for name in files:
file_list.append(os.path.relpath(os.path.join(root, name), repo_root))
return [ f for f in file_list if f.endswith( ('.js', '.py', '.css', '.png', '.svg') ) ]
def modify_line(line, config):
for old in reversed(sorted(config, key=str.casefold)):
new = config[old]
for transformation in TRANSFORMATIONS:
if transformation(old) in line:
line = line.replace(transformation(old), transformation(new))
return line
def execute_reskin_on_file(file_path, config):
all_lines = []
with open(file_path, "r") as reskin_file:
for line in reskin_file:
all_lines.append(line)
with open(file_path, "w") as reskin_file:
for line in all_lines:
reskin_file.write(modify_line(line, config))
def execute_rename_on_file(file_path, config):
new_file_path = file_path
for old in reversed(sorted(config, key=str.casefold)):
new = config[old]
for transformation in TRANSFORMATIONS:
if transformation(old) in new_file_path:
new_file_path = new_file_path.replace(transformation(old), transformation(new))
if new_file_path != file_path:
os.rename(file_path, new_file_path)
def execute_reskin(rename_dir, config):
file_list = get_file_list(rename_dir)
for file in file_list:
execute_reskin_on_file(file, config)
def execute_rename(rename_dir, config, repo_root):
rename_file_list = get_rename_file_list(rename_dir, repo_root)
for file_path in rename_file_list:
execute_rename_on_file(file_path, config)
def main(skin_file):
repo_root = os.path.dirname(os.path.abspath(__file__))
config = get_config(skin_file)
for directory in [
os.path.join(repo_root, "boardzorg"),
os.path.join(repo_root, "client"),
os.path.join(repo_root, "server"),
]:
execute_reskin(directory, config)
execute_rename(directory, config, repo_root)
if __name__ == "__main__":
skin_file = "pooh.skin"
main(skin_file)