Skip to main content

Comment Parser

Build Status PyPI status PyPI version shields.io PyPI license PyPI pyversions

Python module used to extract comments from source code files of various types.

Installation

Prerequisites

  • libmagic

Linux/Unix

sudo pip3 install comment_parser

OSX and Windows

Additionally, complete the special installation requirements for python-magic.

Usage

To use, simply run:

>>> from comment_parser import comment_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('...')

extract_comments signatures

def extract_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.
    """
    pass


def extract_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

Comments Interface

class Comment(object):
    """Represents comments found in source files."""
    def text(self):
        """Returns the comment's text.
        Returns:
            String
        """
        pass

    def line_number(self):
        """Returns the line number the comment was found on.
        Returns:
            Int
        """
        pass

    def is_multiline(self):
        """Returns whether this comment was a multiline comment.
        Returns:
            True if comment was a multiline comment, False if not.
        """
       pass

    def __str__(self):
        pass

    def __eq__(self, other):
        pass

Development

Running locally

Start python3 in the base of repository.

from comment_parser import comment_parser
comment_parser.extract_comments('foo.c', mime='text/x-c')

Running tests

python3 setup.py test

Running pylint

pylint comment_parser

Running formatter

yapf -rip --style=chromium .

Deploying to PyPi

python3 setup.py sdist
twine upload dist/*

Supported Programming Languages

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
Python text/x-python
Ruby text/x-ruby
Shell text/x-shellscript
XML text/xml

And more to come!

Check comment_parser.py for corresponding MIME types.

Release files for comment-parser 1.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for comment-parser 1.2.2
File Size Uploaded
comment_parser-1.2.2.tar.gz 7.7 kB Details

Release files / comment_parser-1.2.2.tar.gz

Download URL comment_parser-1.2.2.tar.gz
Size 7.7 kB
Tags Source
SHA-256 checksum
How to use checksums
17e1a7f93f0844f4616b711c28b2592face505f45df2987a5867626a0082a9a3
BLAKE2b-256 checksum
How to use checksums
15cdb7191d2be662537d1499912995cd57a5ca97d2a304b0981ce88a6584a532
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.3

Release history Release notifications | RSS feed

1.2.5

2 release files

1.2.4

1 release file

1.2.3

1 release file

This release

1.2.2 This release

1 release file

1.2.1

1 release file

1.2.0

1 release file

1.1.6

1 release file

1.1.5

1 release file

1.1.4

1 release file

1.1.3

1 release file

1.1.2

1 release file

1.1.1

1 release file

1.1.0

1 release file

1.0.9

2 release files

1.0.8

1 release file

1.0.7

1 release file

1.0.6

1 release file

1.0.5

1 release file

1.0.4

1 release file

1.0.3

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page