-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcreateREADME.py
155 lines (130 loc) · 4.71 KB
/
createREADME.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
144
145
146
147
148
149
150
151
152
153
154
155
# -*- coding: utf-8 -*-
import os
import os.path as path
toc = []
def main ():
# recurMD(os.getcwd(), 1, "", 1)
license_img = r'<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/cn/"><img alt="知识共享许可协议" style="border-width:0" src="https://i.creativecommons.org/l/by-nc-sa/3.0/cn/88x31.png" /></a>' + '\n'
toc.append(license_img)
license_str = r'本作品采用<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/cn/">知识共享署名-非商业性使用-相同方式共享 3.0 中国大陆许可协议</a>进行许可' + '\n'
toc.append(license_str)
toc.append("---\n")
toc.append("# This README is generated by createREADME.py\n")
toc.append("---\n")
nonRecurs(os.getcwd(), toc)
with open('README.md', 'wt', encoding='utf-8') as f:
for one in toc:
one = one.replace("\\", "/") # github 的路径用 / 分隔
f.write(one + "\n")
print(one)
# toc.append("myNotes")
# toc.append("---")
pass
# str, int, str, int
def recurMD(baseDir, level, parentOrder, subOrder):
# def recurMD(toc, baseDir, level):
relPath = ""
link = ""
if level > 1:
# relPath = path.basename(baseDir) # get folder name
# relPath = path.split(baseDir)[-1]
# relPath = baseDir.split("\\")[-1]
parentOrder += "." + str(subOrder)
relPath = "#" * level
relPath += " " + parentOrder
relPath += " " + path.basename(baseDir) # get folder name
toc.append(relPath)
# relPath = path.relpath(baseDir)
# toc.append("#" * level + " " + str(level) + " " + relPath)
nodeList = os.listdir(baseDir)
forwardDir = []
for aNode in nodeList:
fullPath = path.join(baseDir, aNode)
if path.isfile(fullPath):
relPath = path.relpath(fullPath)
link = "- [" + aNode + "]" + \
"(" + relPath +")"
toc.append(link)
elif aNode == ".git":
continue
else :
forwardDir.append(fullPath)
_subOrder = 1
for aDir in forwardDir:
recurMD(aDir, level+1, parentOrder, _subOrder)
_subOrder += 1
return
def nonRecurs(baseDir, _toc):
# str dir, int level, str parentOrder, int _subOrder
stack = []
level = 2
parentOrder = ""
nodeList = os.listdir(baseDir)
forwardDir = []
# 遍历这个目录
for aNode in nodeList:
fullPath = path.join(baseDir, aNode)
if path.isfile(fullPath):
relPath = path.relpath(fullPath)
# 构造每个文件链接
link = "- [" + aNode + "]" + \
"(" + relPath +")"
_toc.append(link)
elif aNode == ".git":
continue
else :
forwardDir.append(fullPath)
_subOrder = len(forwardDir)
for aDir in reversed(forwardDir):
stack.append([aDir, level, "", _subOrder])
_subOrder -= 1
while len(stack) > 0:
baseDir, level, parentOrder, _subOrder = stack.pop()
# 构造 header
if level > 2:
thisOrder = parentOrder + "." + str(_subOrder)
else :
thisOrder = str(_subOrder)
header = "#" * level
header += " " + thisOrder
header += " " + path.basename(baseDir)
# 放入目录
_toc.append(header)
nodeList = os.listdir(baseDir)
forwardDir = []
# 遍历这个目录
for aNode in nodeList:
fullPath = path.join(baseDir, aNode)
if path.isfile(fullPath):
relPath = path.relpath(fullPath)
link = "- [" + aNode + "]" + \
"(" + relPath +")"
_toc.append(link)
else :
forwardDir.append(fullPath)
_subOrder = len(forwardDir)
# 反字母序压栈
for aDir in reversed(forwardDir):
stack.append([aDir, level+1, thisOrder, _subOrder])
_subOrder -= 1
continue
# end while len(stack) > 0
return
# def file_name(file_dir):
# for root, dirs, files in os.walk(file_dir):
# print(root) #当前目录路径
# print(files) #当前路径下所有非目录子文件
# print(dirs) #当前路径下所有子目录
# print()
# return
# 相对路径
def testRelpath(file):
ppp = os.path.relpath(file) # 相对.py 的路径
print(ppp)
return
if __name__=="__main__":
main()
# 先序遍历
# <http://blog.csdn.net/hyperbolechi/article/details/42913061>
# python3-cookbook
# <http://python3-cookbook.readthedocs.io/zh_CN/latest/c05/p01_read_write_text_data.html>