forked from david-abel/simple_rl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix_underscores.py
78 lines (54 loc) · 1.97 KB
/
fix_underscores.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
# Other imports
import os
this_dir = os.path.dirname(os.path.realpath(__file__))
dir_fixes = {"_modules":"modules", "_static":"static", "_sources":"sources"}
def rename_directories():
# Get all subdirectories.
sub_dirs = dir_fixes.keys() #[f for f in os.listdir(this_dir) if os.path.isdir(f)]
# Rename them.
for subdir in sub_dirs:
if os.path.exists(subdir):
no_underscore_dir_name = subdir.replace("_", "")
os.rename(os.path.join(this_dir, subdir), os.path.join(this_dir, no_underscore_dir_name))
def get_all_html_file_names():
'''
Returns:
(list): Contains any file that ends in .html in the simple_rl/docs folder (excluding simple_rl/docs/sphinx).
'''
# Get the non sphinx sub directories.
all_non_sphinx_sub_dirs = [x for x in os.walk(this_dir) if "sphinx" not in x[0]]
# Loop through and find the html files.
all_html_full_paths = []
for sub_dir, root, files in all_non_sphinx_sub_dirs:
for f in files:
if ".html" in f:
all_html_full_paths.append(os.path.join(sub_dir, f))
return all_html_full_paths
def replace_underscored_module_names(path_to_file):
'''
Args:
paths_to_files (str)
Summary:
Opens the file @path_to_file and replaces all uses of the _static with static,
for all key-val pairs in the global dictionary @dir_fixes.
'''
html_file = open(path_to_file, "r")
all_words = html_file.read()
# Replace all uses of wrong directory name.
result_text = all_words
for dir_to_fix in dir_fixes.keys():
result_text = result_text.replace(dir_to_fix, dir_fixes[dir_to_fix])
# Close the file, open it for reading, overwrite with the newly replaced text.
html_file.close()
html_file = open(path_to_file, "w+")
html_file.write(result_text)
html_file.close()
def main():
# Rename the underscored directories.
rename_directories()
# Grab all relevant html files.
all_html_files_to_fix = get_all_html_file_names()
for html_file in all_html_files_to_fix:
replace_underscored_module_names(html_file)
if __name__ == "__main__":
main()