分析来自不同源文件的注释。
comment-parser的Python项目详细描述
注释分析器
python模块用于从各种类型的源代码文件中提取注释。
安装
先决条件
- libmagic
Linux/Unix
sudo pip3 install comment_parser
OSX和Windows
另外,完成 python-magic。
用法
要使用,只需运行:
>>>fromcomment_parserimportcomment_parser>>># Returns a list of comment_parser.parsers.common.Comments>>>comment_parser.extract_comments('/path/to/source_file')>>># Or>>>comment_parser.extract_comments_from_str('...')
提取评论签名
defextract_comments(filename,mime=None):"""Extracts and returns the comments from the given source file. Args: filename: String name of the file to extract comments from. mime: Optional MIME type for file (str). Note some MIME types accepted don't comply with RFC2045. If not given, an attempt to deduce the MIME type will occur. Returns: Python list of parsers.common.Comment in the order that they appear in the source file. Raises: UnsupportedError: If filename is of an unsupported MIME type. """passdefextract_comments_from_str(code,mime=None):"""Extracts and returns comments from the given source string. Args: code: String containing code to extract comments from. mime: Optional MIME type for code (str). Note some MIME types accepted don't comply with RFC2045. If not given, an attempt to deduce the MIME type will occur. Returns: Python list of parsers.common.Comment in the order that they appear in the source code. Raises: UnsupportedError: If code is of an unsupported MIME type. """pass
评论界面
classComment(object):"""Represents comments found in source files."""deftext(self):"""Returns the comment's text. Returns: String """passdefline_number(self):"""Returns the line number the comment was found on. Returns: Int """passdefis_multiline(self):"""Returns whether this comment was a multiline comment. Returns: True if comment was a multiline comment, False if not. """passdef__str__(self):passdef__eq__(self,other):pass
开发
本地运行
在仓库的底部启动python3。
fromcomment_parserimportcomment_parsercomment_parser.extract_comments('foo.c',mime='text/x-c')
运行测试
python3 setup.py test
运行Pylint
pylint comment_parser
正在运行格式化程序
yapf -rip --style=chromium .
部署到pypi
python3 setup.py sdist twine upload dist/*
支持的编程语言
Language | Mime String |
---|---|
C | text/x-c |
C++/C# | text/x-c++ |
Go | text/x-go |
HTML | text/html |
Java | text/x-java-source |
Javascript | application/javascript |
Ruby | text/x-ruby |
Shell | text/x-shellscript |
XML | text/xml |
还有更多!
检查comment\u parser.py中对应的mime类型。