# !/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
@contact: JHC000abc@gmail.com
@file: make_doc.py
@time: 2023/1/25 0:40
@desc:批量,根据python代码中注释生成文档,注释必须用""" """(三双引号)
"""
import os
import re
class MakeDoc():
def __init__(self):
pass
def read(self,file):
with open(file,"r",encoding="utf-8")as fp:
# num = 0
# for i in fp:
# num += 1
# yield num,i.strip()
return fp.read()
def get_detail(self,text):
text1 = re.sub("@contact","\n@作者",text)
text2 = re.sub("@file", "\n@文件名", text1)
text3 = re.sub("@time", "\n@创建时间", text2)
text4 = re.sub("@desc", "\n@描述", text3)
# print("text4",text4)
return text4
def numberToWords(self,num):
to19='one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen seventeen eighteen nineteen'.split()
tens="twenty thirty forty fifty sixty seventy eighty ninety".split()
def words(n): #
if n < 20: return to19[n - 1:n]
if n < 100: return [tens[n // 10 - 2]] + words(n % 10)
if n < 1000:
s = [to19[n // 100 - 1]] + ["hundred"] if n % 100 == 0 else [to19[n // 100 - 1]] + ["hundred"] + [
"and"] + words(n % 100)
return s
# return [to19[n//100-1]]+["hundred"]+["and"] +words(n%100)
for p, w in enumerate(('thousand', "million", "billion"), 1): # 1万 10000
if n < 1000 ** (p + 1): # p,1,2,3 1000 000 1000 000 000 1000 000 000 000
return words(n // 1000 ** p) + [w] + words(n % 1000 ** p)
return " ".join(words(num)) or "Zero"
def get_exegesis(self,text):
num = 0
pattern = re.compile(r'\"{3}(.*?)\"{3}|def (.*?)\:',re.S)
# print("pattern",pattern)
result = pattern.findall(text)
for index,value in enumerate(result):
if index == 0:
header = value[0].strip()
# print("header",header)
yield self.get_detail(header)+"\n\n\n"
else:
if value[0] != "" and result[index-1][1] != "":
num += 1
function = result[index-1][1].strip()
exegesis = value[0].strip()
Function_description = """
Function {}:
""".format(self.numberToWords(num).capitalize())+'def {}:\n\n"""\n\n{}\n\n"""\n'.format(function,exegesis)
# print(Function_description)
yield Function_description
def get_files_under_path(self,path):
"""
获取路径下所有文件的绝对路径
:param path: 根路径
:return: 路径下文件绝对路径列表
"""
try:
if os.path.exists(path) and os.path.isabs(path):
files_path_lis = []
for path, dir_lis, file_lis in os.walk(path):
if len(file_lis) > 0:
for file in file_lis:
if file.endswith(".py"):
files_path_lis.append(os.path.join(path, file))
return files_path_lis
except Exception as e:
print(e, e.__traceback__.tb_lineno)
def main(self):
parent_path = R"F:\Python_Project\Project3"
for file in self.get_files_under_path(parent_path):
# file = R"F:\Python_Project\Project3\work\work1.py"
try:
doc = md.read(file)
ret_res_lis = self.get_exegesis(doc)
SPLIT_LINE = "<hr></hr>"
with open("./doc.md", "a", encoding="utf-8")as fp:
fp.write("<h2>代码文档:{}</h2>\n".format(file.replace(parent_path, "")))
fp.write(SPLIT_LINE)
for index, document in enumerate(ret_res_lis):
fp.write(document)
fp.write(SPLIT_LINE + "</br>")
except Exception as e:
print("file error", file)
if __name__ == '__main__':
md = MakeDoc()
md.main()
批量,根据python代码中注释生成文档
于 2023-01-25 02:46:03 首次发布