-
Notifications
You must be signed in to change notification settings - Fork 146
/
generate_readme.py
146 lines (101 loc) · 3.32 KB
/
generate_readme.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
132
133
134
135
136
137
138
139
140
141
142
143
# -*-encoding:utf-8 -*-
import logging
import os
import re
HeaderLine = """
```
The book list mainly contains economics, technology and psychology.
I've read them more or less.
They gave me different insights to the world, and how something works. So I'd like to share with others.
I'm not the one who sells knowledge. Knowledge is dead and everywhere.
Since the future is already here, I just wanna it to be distributed.
Hope you can find interests in the books.
```
"""
FooterLine = """
## PR
Any pull requests or issues are welcome .
Use `python generate_readme.py` for generating readme.
"""
ignore_files = ['\..*',"generate_readme.py", "README.md"]
ignore_patterns = [re.compile(i) for i in ignore_files]
not_ignore = lambda fname: not any([p.match(fname) for p in ignore_patterns])
git_dir = os.path.dirname(os.path.abspath(__file__))
print "Git Directory :%s "%(git_dir)
PREFIX = len(git_dir)
class Tree:
def __init__(self, path, parent=None):
assert os.path.exists(path), "Path `{}` Not Found".format(path)
self.path = path
self.nodes = []
self.parent = parent
@property
def name(self):
fname = os.path.split(self.path)[-1]
slices = fname.split('.')
if len(slices) > 1:
return "".join(slices[:-1])
else:
return slices[0]
def add_node(self, node):
self.nodes.append(node)
def walk(self):
if os.path.isdir(self.path):
dir_list = os.listdir(self.path)
add_dir_list = filter(not_ignore, dir_list)
for d in add_dir_list:
fpath = os.path.join(self.path, d)
if os.path.isfile(fpath):
self.add_node(FileNode(fpath, parent=self))
elif os.path.isdir(fpath):
self.add_node(DirectoryNode(fpath, parent=self))
for node in self.nodes:
node.walk()
@property
def level(self):
me = self
level = 0
while me.parent:
me = me.parent
level += 1
return level
class FileNode(Tree):
pass
class DirectoryNode(Tree):
pass
class MarkDownRender:
render_level = {
0: "#",
1: "##",
2: "###",
3: "####",
}
def __init__(self, node):
self.node = node
self.lines = []
def walk(self, lines=None):
print self.node.name
if lines is None:
lines = []
if self.node.__class__.__name__ == 'FileNode':
lines.append("- [{}]({}) \n".format(self.node.name,"https://github.com/yowenter/books/blob/master%s"%self.node.path[PREFIX:]))
elif self.node.__class__.__name__ == 'DirectoryNode':
lines.append("{} {} \n".format(self.render_level.get(self.node.level," "),self.node.name))
if self.node.nodes:
for node in self.node.nodes:
MarkDownRender(node).walk(lines)
return lines
if __name__ == "__main__":
tree = DirectoryNode(git_dir)
tree.walk()
md = MarkDownRender(tree)
readme_path = os.path.join(git_dir,"README.md")
print readme_path
if os.path.exists(readme_path):
os.system("rm %s"%readme_path)
with open(readme_path,"w") as f:
f.write(HeaderLine)
for line in md.walk():
f.write(line)
f.write(" \n")
f.write(FooterLine)