Skip to content

Commit

Permalink
feat: using Output class for serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ha36d authored and Hamed Faramarzi committed Oct 24, 2021
1 parent e818bb2 commit 4d0020a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 25 deletions.
10 changes: 5 additions & 5 deletions nirjas/output/multi_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''

from .output import Output


class MultiLine:
'''
Expand All @@ -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
40 changes: 40 additions & 0 deletions nirjas/output/output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
Copyright (C) 2021 Hamed Faramarzi
Author: Hamed Faramarzi <hamed.faramarzi@gmail.com>
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
37 changes: 21 additions & 16 deletions nirjas/output/scan_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''

from .output import Output


class ScanOutput:
'''
Expand All @@ -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
7 changes: 3 additions & 4 deletions nirjas/output/single_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''

from .output import Output


class SingleLine:
'''
Expand All @@ -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

0 comments on commit 4d0020a

Please sign in to comment.