-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadme_maker.py
More file actions
187 lines (151 loc) · 5.75 KB
/
readme_maker.py
File metadata and controls
187 lines (151 loc) · 5.75 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import os
import re
from pprint import pprint
import copy
import sys
PRINT_LOG = False
class DirTree:
sep = ";"
default_struct = {
"files": [],
"dirs": {},
"base_path": ""
}
link_prefix = "https://github.com/gnithin/Notes/tree/master/Notes/"
def __init__(self, base_path):
self.dir_tree = copy.deepcopy(self.default_struct)
self.base_path = base_path
def __get_obj_from_working_path(self, working_path):
current_obj = self.dir_tree
if working_path is not "":
try:
for key in working_path.split(self.sep):
if key not in current_obj["dirs"]:
current_obj["dirs"][key] = \
copy.deepcopy(self.default_struct)
current_obj = current_obj["dirs"][key]
except Exception:
if PRINT_LOG:
print(sys.exc_info()[0])
current_obj = None
return current_obj
def add_values_to_tree(self, working_base_path, dirs, files):
add_success = False
if len(dirs) or len(files):
try:
working_path = self.get_path_from_base_path(working_base_path)
current_obj = self.__get_obj_from_working_path(working_path)
if current_obj:
if len(dirs):
for d in dirs:
current_obj["dirs"][d] = \
copy.deepcopy(self.default_struct)
if len(files):
for file_name in files:
# Ignore hidden files
if file_name.startswith("."):
continue
current_obj["files"].append(file_name)
current_obj["base_path"] = working_path.replace(
self.sep,
"/"
) + "/"
except:
print(sys.exc_info()[0])
else:
add_success = True
return add_success
def get_path_from_base_path(self, working_base_path):
matched = (re.findall(self.base_path + r'(.+)', working_base_path))
if len(matched):
return matched[0].replace(os.sep, self.sep)
return ""
def print_tree(self):
if PRINT_LOG:
pprint(self.dir_tree)
def get_text(self):
return self.__text_from_tree(self.dir_tree)
def __text_from_tree(self, current_obj, level=0,
final_text="", dir_name=""):
current_text = ""
tab = " " * 2
files = current_obj.get("files", None)
base_path = current_obj.get("base_path", "")
if files and len(files):
if dir_name != "" and (level - 1) >= 0:
current_text += "{0}- **{1}**\n".format(
tab*(level-1),
dir_name
)
for f in sorted(files):
current_text += "{0}- [{1}]({2})\n".format(
tab*level,
self.format_file_name(f),
self.link_prefix + base_path + f
)
dirs = current_obj.get("dirs", None)
if dirs and len(dirs):
for d in sorted(dirs):
current_text += self.__text_from_tree(
current_obj["dirs"][d],
level+1,
current_text,
d
)
return current_text
def format_file_name(self, filename):
return filename
class ReadmeMaker:
START_LABEL = "<!-- LABEL_BEGIN -->\n"
END_LABEL = "\n<!-- LABEL_END -->"
def __init__(self, notes_path, readme_file_path):
self.path = notes_path
self.dir_tree = DirTree(notes_path)
self.readme_file_path = readme_file_path
self.get_all_files_dirs_from_path()
def get_all_files_dirs_from_path(self):
for (dirpath, dirnames, filenames) in os.walk(self.path):
self.dir_tree.add_values_to_tree(dirpath, dirnames, filenames)
def print_tree(self):
self.dir_tree.print_tree()
def get_text_from_tree(self):
return self.dir_tree.get_text()
def update_readme(self):
did_update = False
# Fetch the readme.
with open(self.readme_file_path, 'r') as fp:
readme_contents = (fp.read()).strip()
if readme_contents != "":
# Fetch the location between the thing is supposed to be written.
start_label = self.START_LABEL
end_label = self.END_LABEL
start_index = readme_contents.find(start_label)
end_index = readme_contents.find(end_label)
# Get markdown string to be printed
print_text = self.get_text_from_tree()
# Insert markdown between labels
new_contents = (readme_contents[:start_index + len(start_label)] +
print_text +
readme_contents[end_index:])
# Writing the contents to the file
try:
with open(self.readme_file_path, 'w') as fw:
fw.write(new_contents)
except:
if PRINT_LOG:
print("Could not write to file")
print(sys.exc_info()[0])
else:
did_update = True
return did_update
if __name__ == "__main__":
notes_path = ".{0}Notes{0}".format(os.sep)
readme_path = "README.md"
rd_maker = ReadmeMaker(notes_path, readme_path)
# rd_maker.print_tree()
did_update = rd_maker.update_readme()
if did_update:
print("Updated :)")
print(readme_path)
else:
print("Failed!")