Skip to content

Output Format

Ayush Bhardwaj edited this page Jul 7, 2020 · 1 revision

Nirjas follows the json output structure.

All the comments (single line or block) or source code along with the required metadata are structured in an accessible json format so that it can be easy to parse the output as per the requirements.

Output Structure

1. Extract Comments from a Single File

$ nirjas -p setup.py
  • Output:
[
    {
        "metadata": [
            {
                "blank_lines": 7,
                "filename": "setup.py",
                "lang": "Python",
                "sloc": 23,
                "total_lines": 68,
                "total_lines_of_comments": 38
            }
        ],
        "multi_line_comment": [
            {
                "comment": "Example Comment 1",
                "end_line": 2,
                "start_line": 1
            },
            {
                "comment": "Example Comment 2",
                "end_line": 2,
                "start_line": 3
            }
        ],
        "single_line_comment": [
            {
                "comment": "Single line 1",
                "line_number": 22
            }
        ]
    }
]

2. Extract Comments from all files in a directory

nirjas -p <path to directory>
  • Output:
[
    {
        "metadata": [
            {
                "blank_lines": 13,
                "filename": "file1.py",
                "lang": "Python",
                "sloc": 46,
                "total_lines": 79,
                "total_lines_of_comments": 20
            }
        ],
        "multi_line_comment": [
            {
                "comment": "Example Multiline Comment",
                "end_line": 21,
                "start_line": 3
            }
        ],
        "single_line_comment": [
            {
                "comment": "Example Comment",
                "line_number": 1
            },
            {
                "comment": "Example Comment",
                "line_number": 75
            }
        ]
    },
    {
        "metadata": [
            {
                "blank_lines": 13,
                "filename": "file2.py",
                "lang": "Python",
                "sloc": 46,
                "total_lines": 79,
                "total_lines_of_comments": 20
            }
        ],
        "multi_line_comment": [
            {
                "comment": "Example Multiline Comment",
                "end_line": 21,
                "start_line": 3
            }
        ],
        "single_line_comment": [
            {
                "comment": "Example Comment",
                "line_number": 1
            },
            {
                "comment": "Example Comment",
                "line_number": 75
            }
        ]
    },
    {
        "metadata": [
            {
                "blank_lines": 13,
                "filename": "fileN.py",
                "lang": "Python",
                "sloc": 46,
                "total_lines": 79,
                "total_lines_of_comments": 20
            }
        ],
        "multi_line_comment": [
            {
                "comment": "Example Multiline Comment",
                "end_line": 21,
                "start_line": 3
            }
        ],
        "single_line_comment": [
            {
                "comment": "Example Comment",
                "line_number": 1
            },
            {
                "comment": "Example Comment",
                "line_number": 75
            }
        ]
    }
]

3. Extract only source code (exclude commented part) out of a file

nirjas -i <target file> -s <new file name including extension>
  • Output:
from setuptools import setup, find_packages
from os import path
from io import open

here = path.abspath(path.dirname(__file__))
with open(path.join(here, 'README.md'), encoding='utf-8') as f:
    long_description = f.read()


CLASSIFIERS = """\
Development Status :: 5 - Production/Stable
Intended Audience :: Developers
License :: OSI Approved :: LGPL-2.1
Programming Language :: Python :: 3
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Topic :: Software Development
"""

setup(
    name='Nirjas', 
    version='0.0.1',  
    description='A Python library to extract comments and source code out of your file(s)',  
    long_description=long_description,  
    long_description_content_type='text/markdown',  
    url='https://github.com/fossology/nirjas',  
    author='Ayush Bhardwaj, Kaushlendra Pratap',  
    author_email='[email protected], [email protected]',  

    classifiers=[_f for _f in CLASSIFIERS.split('\n') if _f],
    keywords='Comment Extractor, Code Comment Extractor, Source Code Extractor, Source Extractor',  
    packages=find_packages(),  
    python_requires = ">=3",
    entry_points = {
    'console_scripts': [
      'nirjas = extractor.main:main'
    ]
  },
)