Skip to main content

Finds comments in source code in different programming languages

Project description

rtmigo / commie.python

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

Find comments in a file

from pathlib import Path
import commie

for comment in commie.iter_comments_file(Path("/path/to/source.cpp")):

  # something like "/* sample */"
  print("Comment code:", comment.code)
  print("Comment code location:", comment.code_span.start, comment.code_span.end)

  # something like " sample " 
  print("Comment inner text:", comment.text)
  print("Comment text location:", comment.text_span.start, comment.text_span.end)

Find comments in a string

Method Works for
commie.iter_comments_c C99+, C++, Objective-C, C#, Java
commie.iter_comments_js JavaScript, Dart, TypeScript
commie.iter_comments_go Go
commie.iter_comments_ruby Ruby
commie.iter_comments_python Python
commie.iter_comments_shell Bash, Sh
commie.iter_comments_html HTML, XML, SGML
commie.iter_comments_css CSS
commie.iter_comments_sass SASS
import commie

source_code_in_golang:str = ...

for comment in commie.iter_comments_go(source_code_in_golang):
  # ... process comment ...
  pass

Find comments in a string with a known filename

Method commie.iter_comments will try to guess the file format from the provided filename

from pathlib import Path
import commie

filename:str = "/path/to/mycode.go"
source_code:str = Path(filename).read_text()

for comment in commie.iter_comments(source_code, filename):
  # ... process comment ...
  pass

This project was forked from comment_parser. Motivation:

comment_parser commie
Returns only a line number Returns positions where the comment starts and ends. Just like regular string search
Returns only the text of a comment Respects markup as well, making it possible to remove or replace the entire comment
Depends on python-magic requiring an optional installation of binaries Does not have this dependency

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

commie-0.0.3.tar.gz (11.8 kB view hashes)

Uploaded Source

Built Distribution

commie-0.0.3-py3-none-any.whl (21.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page