Skip to content

Commit

Permalink
Merge pull request #47 from ha36d/output-class
Browse files Browse the repository at this point in the history
fix: translation of object to JSON only when we are in cli

Reviewed-by: kaushlendrapratap.9837@gmail.com, mishra.gaurav@siemens.com
Tested-by: mishra.gaurav@siemens.com
  • Loading branch information
GMishx authored Oct 26, 2021
2 parents 728394a + 0d97ebf commit c310d96
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 30 deletions.
12 changes: 7 additions & 5 deletions nirjas/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class LanguageMapper:
'.php': 'php',
'.pl': 'perl',
'.r': 'r',
'.R':'r',
'.R': 'r',
'.rb': 'ruby',
'.rs': 'rust',
'.sh': 'shell',
Expand All @@ -82,7 +82,7 @@ class LanguageMapper:
'.lic': 'text',
'.install': 'text',
'.OSS': 'text',
'.gl': 'text'
'.gl': 'text',
}

@staticmethod
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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


Expand Down
1 change: 1 addition & 0 deletions nirjas/output/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@
from .scan_output import ScanOutput
from .single_line import SingleLine
from .multi_line import MultiLine
from .output import Output
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 c310d96

Please sign in to comment.