diff --git a/nirjas/main.py b/nirjas/main.py index e040e20..a3d0588 100644 --- a/nirjas/main.py +++ b/nirjas/main.py @@ -69,7 +69,7 @@ class LanguageMapper: '.php': 'php', '.pl': 'perl', '.r': 'r', - '.R':'r', + '.R': 'r', '.rb': 'ruby', '.rs': 'rust', '.sh': 'shell', @@ -82,7 +82,7 @@ class LanguageMapper: '.lic': 'text', '.install': 'text', '.OSS': 'text', - '.gl': 'text' + '.gl': 'text', } @staticmethod @@ -121,7 +121,7 @@ def run_cli(): out_file = args.outFile try: if file is not None: - return file_runner(file) + return file_runner(file, 'json') return inputfile_runner(inputfile, out_file) except NotSupportedExtension as e: print(e, file=os.sys.stderr) @@ -140,7 +140,8 @@ def scan_the_file(file): func = langname + '.' + langname + 'Extractor' return eval(func)(file) -def file_runner(file): + +def file_runner(file, type='dictionary'): ''' Check if the input is a file or a directory and iterate with scan_the_file() @@ -161,7 +162,8 @@ def file_runner(file): result.append(scan_the_file(file_to_scan).get_dict()) except Exception: continue - result = json.dumps(result, sort_keys=False, indent=4) + if type == 'json': + return json.dumps(result, sort_keys=False, indent=4) return result diff --git a/nirjas/output/__init__.py b/nirjas/output/__init__.py index 5bb320b..c98f360 100644 --- a/nirjas/output/__init__.py +++ b/nirjas/output/__init__.py @@ -26,3 +26,4 @@ from .scan_output import ScanOutput from .single_line import SingleLine from .multi_line import MultiLine +from .output import Output diff --git a/nirjas/output/multi_line.py b/nirjas/output/multi_line.py index 3e9d67f..eb2ddcf 100644 --- a/nirjas/output/multi_line.py +++ b/nirjas/output/multi_line.py @@ -21,6 +21,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ''' +from .output import Output + class MultiLine: ''' @@ -36,8 +38,6 @@ def get_dict(self): ''' Get the output as dictionary ''' - return { - "start_line": self.start_line, - "end_line": self.end_line, - "comment": self.comment - } + return Output( + start_line=self.start_line, end_line=self.end_line, comment=self.comment + ).output diff --git a/nirjas/output/output.py b/nirjas/output/output.py new file mode 100644 index 0000000..4f87290 --- /dev/null +++ b/nirjas/output/output.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +''' +Copyright (C) 2021 Hamed Faramarzi +Author: Hamed Faramarzi + +SPDX-License-Identifier: LGPL-2.1 + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +''' + + +class Output(object): + ''' + return results + ''' + + def __init__(self, **kwargs): + self.result = {} + for key, value in kwargs.items(): + self.result.setdefault(key, value) + + @property + def output(self): + ''' + return results + ''' + return self.result diff --git a/nirjas/output/scan_output.py b/nirjas/output/scan_output.py index b06da33..4a771b4 100644 --- a/nirjas/output/scan_output.py +++ b/nirjas/output/scan_output.py @@ -21,6 +21,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ''' +from .output import Output + class ScanOutput: ''' @@ -33,24 +35,27 @@ def __init__(self): self.total_lines = None self.total_lines_of_comments = None self.blank_lines = None - self.single_line_comment = list() - self.cont_single_line_comment = list() - self.multi_line_comment = list() + self.single_line_comment = [] + self.cont_single_line_comment = [] + self.multi_line_comment = [] def get_dict(self): ''' Get the output as dictionary ''' - return { - "metadata": { - "filename": self.filename, - "lang": self.lang, - "total_lines": self.total_lines, - "total_lines_of_comments": self.total_lines_of_comments, - "blank_lines": self.blank_lines, - "sloc": self.total_lines - (self.total_lines_of_comments + self.blank_lines) - }, - "single_line_comment": [c.get_dict() for c in self.single_line_comment], - "cont_single_line_comment": [c.get_dict() for c in self.cont_single_line_comment], - "multi_line_comment": [c.get_dict() for c in self.multi_line_comment] - } + return Output( + metadata=Output( + filename=self.filename, + lang=self.lang, + total_lines=self.total_lines, + total_lines_of_comments=self.total_lines_of_comments, + blank_lines=self.blank_lines, + sloc=self.total_lines + - (self.total_lines_of_comments + self.blank_lines), + ).output, + single_line_comment=[c.get_dict() for c in self.single_line_comment], + cont_single_line_comment=[ + c.get_dict() for c in self.cont_single_line_comment + ], + multi_line_comment=[c.get_dict() for c in self.multi_line_comment], + ).output diff --git a/nirjas/output/single_line.py b/nirjas/output/single_line.py index 956c30c..01ea4b7 100644 --- a/nirjas/output/single_line.py +++ b/nirjas/output/single_line.py @@ -21,6 +21,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ''' +from .output import Output + class SingleLine: ''' @@ -35,7 +37,4 @@ def get_dict(self): ''' Get the output as dictionary ''' - return { - "line_number": self.line_number, - "comment": self.comment - } + return Output(line_number=self.line_number, comment=self.comment).output