mirror of
https://github.com/snowykami/mbcp.git
synced 2026-01-26 05:32:03 +00:00
📝 测试文档部署
This commit is contained in:
@@ -121,29 +121,29 @@ class Docstring(BaseModel):
|
||||
|
||||
"""
|
||||
PREFIX = "" * indent
|
||||
if is_classmethod:
|
||||
PREFIX = " -"
|
||||
ret = ""
|
||||
ret += self.desc + "\n\n"
|
||||
# print(self.reduction())
|
||||
# print(self.desc, self.return_)
|
||||
if self.args:
|
||||
ret += PREFIX + f"{get_text(lang, 'docstring.args')}:\n\n"
|
||||
for arg in self.args:
|
||||
ret += PREFIX + f"{arg.name}: {arg.type} {arg.desc}\n\n"
|
||||
ret += PREFIX + f"- {arg.name}: {arg.type} {arg.desc}\n\n"
|
||||
if self.attrs:
|
||||
ret += PREFIX + f"{get_text(lang, 'docstring.attrs')}:\n\n"
|
||||
for attr in self.attrs:
|
||||
ret += PREFIX + f"{attr.name}: {attr.type} {attr.desc}\n\n"
|
||||
if self.return_:
|
||||
ret += PREFIX + f"- {attr.name}: {attr.type} {attr.desc}\n\n"
|
||||
if self.return_ is not None:
|
||||
ret += PREFIX + f"{get_text(lang, 'docstring.return')}:\n\n"
|
||||
ret += PREFIX + f"{self.return_.desc}\n\n"
|
||||
ret += PREFIX + f"- {self.return_.desc}\n\n"
|
||||
if self.raise_:
|
||||
ret += PREFIX + f"{get_text(lang, 'docstring.raises')}:\n\n"
|
||||
for exception in self.raise_:
|
||||
ret += PREFIX + f"{exception.name} {exception.desc}\n\n"
|
||||
ret += PREFIX + f"- {exception.name} {exception.desc}\n\n"
|
||||
if self.example:
|
||||
ret += PREFIX + f"{get_text(lang, 'docstring.example')}:\n\n"
|
||||
for example in self.example:
|
||||
ret += PREFIX + f"{example.desc}\n Input: {example.input}\n Output: {example.output}\n\n"
|
||||
ret += PREFIX + f"- {example.desc}\n Input: {example.input}\n Output: {example.output}\n\n"
|
||||
return ret
|
||||
|
||||
def __str__(self):
|
||||
|
||||
@@ -115,17 +115,17 @@ class GoogleDocstringParser(Parser):
|
||||
在一个子解析器中,解析下一行,直到缩进小于等于当前行的缩进
|
||||
Returns:
|
||||
"""
|
||||
while self.lineno + 1 < len(self.lines):
|
||||
while (self.lineno + 1) < len(self.lines):
|
||||
line = self.lines[self.lineno + 1]
|
||||
|
||||
if line.startswith(" " * self.indent):
|
||||
line = line[self.indent:]
|
||||
self.lineno += 1
|
||||
return line
|
||||
else:
|
||||
self.lineno += 1
|
||||
return None
|
||||
if not line:
|
||||
return None
|
||||
self.lineno += 1
|
||||
return line
|
||||
self.lineno += 1
|
||||
return None
|
||||
|
||||
def parse(self) -> Docstring:
|
||||
"""
|
||||
@@ -146,7 +146,6 @@ class GoogleDocstringParser(Parser):
|
||||
add_desc = False
|
||||
|
||||
match token:
|
||||
|
||||
case "args":
|
||||
self.parse_args()
|
||||
case "return":
|
||||
|
||||
@@ -26,7 +26,6 @@ def generate(parser: AstParser, lang: str, frontmatter: Optional[dict] = None) -
|
||||
Returns:
|
||||
markdown style document
|
||||
"""
|
||||
print(parser.variables)
|
||||
if frontmatter is not None:
|
||||
md = "---\n"
|
||||
for k, v in frontmatter.items():
|
||||
@@ -36,20 +35,13 @@ def generate(parser: AstParser, lang: str, frontmatter: Optional[dict] = None) -
|
||||
md = ""
|
||||
|
||||
# var > func > class
|
||||
for var in parser.variables:
|
||||
if var.type == TypeHint.NO_TYPEHINT:
|
||||
md += f"### ***var*** `{var.name} = {var.value}`\n\n"
|
||||
else:
|
||||
md += f"### ***var*** `{var.name}: {var.type} = {var.value}`\n\n"
|
||||
|
||||
for func in parser.functions:
|
||||
|
||||
md += func.markdown(lang)
|
||||
|
||||
for cls in parser.classes:
|
||||
md += f"### ***class*** `{cls.name}`\n\n"
|
||||
for mtd in cls.methods:
|
||||
|
||||
md += mtd.markdown(lang, 2, True)
|
||||
|
||||
for attr in cls.attrs:
|
||||
@@ -58,4 +50,10 @@ def generate(parser: AstParser, lang: str, frontmatter: Optional[dict] = None) -
|
||||
else:
|
||||
md += f"#### ***attr*** `{attr.name}: {attr.type} = {attr.value}`\n\n"
|
||||
|
||||
for var in parser.variables:
|
||||
if var.type == TypeHint.NO_TYPEHINT:
|
||||
md += f"### ***var*** `{var.name} = {var.value}`\n\n"
|
||||
else:
|
||||
md += f"### ***var*** `{var.name}: {var.type} = {var.value}`\n\n"
|
||||
|
||||
return md
|
||||
|
||||
@@ -139,18 +139,18 @@ class AstParser:
|
||||
)
|
||||
for default in node.args.defaults
|
||||
],
|
||||
return_=ast.unparse(node.returns).strip() if node.returns else TypeHint.NO_TYPEHINT,
|
||||
return_=ast.unparse(node.returns).strip() if node.returns else TypeHint.NO_RETURN,
|
||||
decorators=[ast.unparse(decorator).strip() for decorator in node.decorator_list],
|
||||
is_async=isinstance(node, ast.AsyncFunctionDef),
|
||||
src=ast.unparse(node).strip()
|
||||
))
|
||||
|
||||
elif isinstance(node, (ast.Assign, ast.AnnAssign)):
|
||||
if not self._is_module_level_variable(node):
|
||||
if not self._is_module_level_variable2(node):
|
||||
# print("变量不在模块级别", ast.unparse(node))
|
||||
continue
|
||||
else:
|
||||
print("变量在模块级别", ast.unparse(node))
|
||||
pass
|
||||
if isinstance(node, ast.Assign):
|
||||
for target in node.targets:
|
||||
if isinstance(target, ast.Name):
|
||||
@@ -180,7 +180,39 @@ class AstParser:
|
||||
return False
|
||||
return True
|
||||
|
||||
def _is_module_level_variable(self, node: ast.Assign):
|
||||
def _is_module_level_variable(self, node: ast.Assign | ast.AnnAssign):
|
||||
"""在类方法或函数内部的变量不会被记录"""
|
||||
|
||||
# for parent in ast.walk(self.tree):
|
||||
# if isinstance(parent, (ast.ClassDef, ast.FunctionDef, ast.AsyncFunctionDef)):
|
||||
# if node in parent.body:
|
||||
# return False
|
||||
# else:
|
||||
# for sub_node in parent.body:
|
||||
# if isinstance(sub_node, (ast.FunctionDef, ast.AsyncFunctionDef)):
|
||||
# if node in sub_node.body:
|
||||
# return False
|
||||
# return True
|
||||
# 递归检查
|
||||
def _check(_node, _parent):
|
||||
if isinstance(_parent, (ast.ClassDef, ast.FunctionDef, ast.AsyncFunctionDef)):
|
||||
if _node in _parent.body:
|
||||
return False
|
||||
else:
|
||||
for sub_node in _parent.body:
|
||||
if isinstance(sub_node, (ast.FunctionDef, ast.AsyncFunctionDef)):
|
||||
return _check(_node, sub_node)
|
||||
return True
|
||||
|
||||
for parent in ast.walk(self.tree):
|
||||
if not _check(node, parent):
|
||||
return False
|
||||
return True
|
||||
|
||||
def _is_module_level_variable2(self, node: ast.Assign | ast.AnnAssign) -> bool:
|
||||
"""
|
||||
检查变量是否在模块级别定义。
|
||||
"""
|
||||
for parent in ast.walk(self.tree):
|
||||
if isinstance(parent, (ast.ClassDef, ast.FunctionDef, ast.AsyncFunctionDef)):
|
||||
if node in parent.body:
|
||||
|
||||
@@ -119,7 +119,7 @@ class FunctionNode(BaseModel):
|
||||
kw_defaults: list[ConstantNode] = []
|
||||
defaults: list[ConstantNode] = []
|
||||
|
||||
return_: str = Field(TypeHint.NO_RETURN, alias="return")
|
||||
return_: str = TypeHint.NO_RETURN
|
||||
decorators: list[str] = []
|
||||
src: str
|
||||
is_async: bool = False
|
||||
@@ -153,8 +153,8 @@ class FunctionNode(BaseModel):
|
||||
"""
|
||||
self.complete_default_args()
|
||||
PREFIX = "" * indent
|
||||
if is_classmethod:
|
||||
PREFIX = "- #"
|
||||
# if is_classmethod:
|
||||
# PREFIX = "- #"
|
||||
|
||||
md = ""
|
||||
|
||||
@@ -211,11 +211,14 @@ class FunctionNode(BaseModel):
|
||||
|
||||
md += ", ".join(args) + ")"
|
||||
|
||||
if self.return_ != TypeHint.NO_RETURN:
|
||||
md += f" -> {self.return_}"
|
||||
|
||||
md += "`\n\n" # code end
|
||||
|
||||
"""此处预留docstring"""
|
||||
if self.docs is not None:
|
||||
md += f"\n{self.docs.markdown(lang, indent)}\n"
|
||||
md += f"\n{self.docs.markdown(lang, indent, is_classmethod)}\n"
|
||||
else:
|
||||
pass
|
||||
# 源码展示
|
||||
|
||||
Reference in New Issue
Block a user