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