Skip to content

Commit

Permalink
Merge pull request #49 from soham4abc/workflows
Browse files Browse the repository at this point in the history
Flake8 Linter added

Reviewed-by: kaushlendrapratap.9837@gmail.com
Tested-by: kaushlendrapratap.9837@gmail.com
  • Loading branch information
Kaushl2208 authored Nov 3, 2021
2 parents c310d96 + c715677 commit e3e2596
Show file tree
Hide file tree
Showing 58 changed files with 2,061 additions and 1,624 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Lint
on: [push, pull_request]
jobs:
CI-Linter:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: 3.8
- name: Install flake8
run: pip install flake8
- name: Run flake8
run: flake8 --ignore=E501 ./nirjas
16 changes: 9 additions & 7 deletions nirjas/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3

'''
"""
nirjas module which can be imported by other tools
SPDX-License-Identifier: LGPL-2.1
Expand All @@ -16,17 +16,19 @@
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
'''
"""

from nirjas.main import file_runner # noqa

from nirjas.main import *

def extract(file):
'''
"""
Extract the information from the given file.
:param file: File or directory to get information from
:type file: string
:return: Returns comments and other meta information about the given file.
'''
return file_runner(file)
"""
return file_runner(file) # noqa


__all__ = ['file_runner', 'extract', 'LanguageMapper']
__all__ = ["file_runner", "extract", "LanguageMapper"]
148 changes: 77 additions & 71 deletions nirjas/binder.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
"""
Copyright (C) 2020 Ayush Bhardwaj (classicayush@gmail.com),
Kaushlendra Pratap (kaushlendrapratap.9837@gmail.com)
Expand All @@ -19,18 +19,18 @@
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
'''
"""

import re
from itertools import groupby
from operator import itemgetter


def readSingleLine(file, regex):
'''
"""
Read file line by line and match the given regex to get comment.
Return comments, lines read, blank lines, and lines with comments.
'''
"""
content = []
total_lines, line_of_comments, blank_lines = 0, 0, 0
with open(file) as f:
Expand All @@ -39,23 +39,23 @@ def readSingleLine(file, regex):
output = re.findall(regex, line, re.I)
if len(output) > 0:
line_of_comments += 1
output = ''.join(output)
output = "".join(output)

if output:
content.append([line_number, output.strip()])

line = line.strip()

if line == '':
if line == "":
blank_lines += 1

return content, total_lines, blank_lines, line_of_comments


def contSingleLines(data):
'''
"""
Merge consecutive single line comments as cont_single_line_comment
'''
"""
lines, start_line, end_line, output = [], [], [], []
content = ""
for i in data[0]:
Expand All @@ -73,15 +73,15 @@ def contSingleLines(data):
for index, x in enumerate(data[0]):
if x[0] == i:
del data[0][index]
content = content + ' ' + comment[0]
content = content + " " + comment[0]
output.append(content)
return data, start_line, end_line, output


def readMultiLineSame(file, syntax: str):
'''
"""
Read multiline comments where starting and ending symbols are same.
'''
"""
start_line, end_line, output = [], [], []
content = ""
if '"' in syntax:
Expand All @@ -92,14 +92,13 @@ def readMultiLineSame(file, syntax: str):
copy = False
with open(file) as f:
for line_number, line in enumerate(f, start=1):
if syntax in line and \
syntax_in_string not in line:
if syntax in line and syntax_in_string not in line:
closingCount += 1
copy = True
if line.count(syntax) == 2:
# Start and end on same line
closingCount = 2
content = line.replace('\n', ' ')
content = line.replace("\n", " ")
start_line.append(line_number)
if closingCount % 2 == 0 and closingCount != 0:
copy = False
Expand All @@ -111,16 +110,16 @@ def readMultiLineSame(file, syntax: str):

if copy:
lines_of_comment += 1
content = content + line.replace('\n', ' ')
content = content + line.replace("\n", " ")

output = [s.strip(syntax).strip() for s in output]
return start_line, end_line, output, lines_of_comment


def readMultiLineDiff(file, startSyntax: str, endSyntax: str):
'''
"""
Read multiline comments where starting and ending symbols are different.
'''
"""
output, startLine, endLine = [], [], []
content = ""
total_lines, line_of_comments, blank_lines = 0, 0, 0
Expand All @@ -134,50 +133,57 @@ def readMultiLineDiff(file, startSyntax: str, endSyntax: str):
line = line[line.find(startSyntax) + len(startSyntax):]
if endSyntax in line:
copy = False
line = line[:line.rfind(endSyntax) + len(endSyntax)]
content = content + line.replace('\n', ' ')
line = line[: line.rfind(endSyntax) + len(endSyntax)]
content = content + line.replace("\n", " ")
content = content.strip(startSyntax).strip(endSyntax).strip()
output.append(content)
content = ""
endLine.append(lineNumber)
continue
if copy:
content = content + (line.replace('\n', ' ')).strip()
if line.strip() == '':
content = content + (line.replace("\n", " ")).strip()
if line.strip() == "":
blank_lines += 1
for idx, _ in enumerate(endLine):
line_of_comments = line_of_comments + (endLine[idx] - startLine[idx]) + 1
line_of_comments += len(output)
output = [s.strip(startSyntax).strip(endSyntax).strip()for s in output]
return startLine, endLine, output, line_of_comments, total_lines, blank_lines
output = [s.strip(startSyntax).strip(endSyntax).strip() for s in output]
return (
startLine,
endLine,
output,
line_of_comments,
total_lines,
blank_lines,
) # noqa


def extractAssignedString(file):
'''
"""
Read file line by line and match string type variable to get string.
Return the content of the string.
'''
"""
content = []
regex = r'(?<=(=\s*[\'\"]))(.*?)(?=[\'\"])'
regex = r"(?<=(=\s*[\'\"]))(.*?)(?=[\'\"])"
total_lines, line_of_assignedString = 0, 0
with open(file) as f:
for line_number, line in enumerate(f, start=1):
total_lines += 1
output = re.findall(regex, line, re.S)
if len(output) >= 2:
line_of_assignedString += 1
output = ''.join(output)
output = "".join(output)
if output and len(output) > 1:
content.append([line_number, output.strip()])
line = line.strip()
return content, line_of_assignedString


class CommentSyntax:
'''
"""
Class to hold various regex and helper functions based on comment format
used by a language.
'''
"""

def __init__(self):
self.sign = None
Expand All @@ -186,43 +192,43 @@ def __init__(self):
self.end = None

def hash(self, file):
'''
"""
sign: #
'''
self.sign = '#'
self.pattern = r'''(?<!["'`])#+\s*(.*)'''
"""
self.sign = "#"
self.pattern = r"""(?<!["'`])#+\s*(.*)"""
return readSingleLine(file, self.pattern)

def hashNoCurl(self, file):
'''
"""
sign: #
'''
self.sign = '#'
self.pattern = r'''(?<!["'`])#+(?!\{)\s*(.*)'''
"""
self.sign = "#"
self.pattern = r"""(?<!["'`])#+(?!\{)\s*(.*)"""
return readSingleLine(file, self.pattern)

def percentage(self, file):
'''
"""
sign: %
'''
self.sign = '%'
self.pattern = r'''(?<!["'`])\%\s*(.*)'''
"""
self.sign = "%"
self.pattern = r"""(?<!["'`])\%\s*(.*)"""
return readSingleLine(file, self.pattern)

def doubleSlash(self, file):
'''
"""
sign: //
'''
self.sign = '//'
self.pattern = r'''(?<![pst'"`]:)\/\/\s*(.*)'''
"""
self.sign = "//"
self.pattern = r"""(?<![pst'"`]:)\/\/\s*(.*)"""
return readSingleLine(file, self.pattern)

def doubleNotTripleSlash(self, file):
'''
"""
sign: //
'''
self.sign = '//'
self.pattern = r'''(?<!\/)\/\/(?!\/)\s*(.*)'''
"""
self.sign = "//"
self.pattern = r"""(?<!\/)\/\/(?!\/)\s*(.*)"""
return readSingleLine(file, self.pattern)

def singleQuotes(self, file):
Expand All @@ -240,73 +246,73 @@ def doubleQuotes(self, file):
return readMultiLineSame(file, self.sign)

def doubleDash(self, file):
'''
"""
sign: --
'''
self.sign = '--'
self.pattern = r'''(?<!["'`])\-\-\s*(.*)'''
"""
self.sign = "--"
self.pattern = r"""(?<!["'`])\-\-\s*(.*)"""
return readSingleLine(file, self.pattern)

def slashStar(self, file):
'''
"""
sign: /* ~ */
'''
"""
self.start = "/*"
self.end = "*/"
return readMultiLineDiff(file, self.start, self.end)

def gtExclamationDash(self, file):
'''
"""
sign : <!-- ~ -->
'''
"""
self.start = "<!--"
self.end = "-->"
return readMultiLineDiff(file, self.start, self.end)

def beginCut(self, file):
'''
"""
sign: =begin ~ =cut
'''
"""
self.start = "=begin"
self.end = "=cut"
return readMultiLineDiff(file, self.start, self.end)

def beginEnd(self, file):
'''
"""
sign: =begin ~ =end
'''
"""
self.start = "=begin"
self.end = "=end"
return readMultiLineDiff(file, self.start, self.end)

def curlybracesDash(self, file):
'''
"""
sign: {- ~ -}
'''
"""
self.start = "{-"
self.end = "-}"
return readMultiLineDiff(file, self.start, self.end)

def percentageCurlybraces(self, file):
'''
"""
sign: %{ ~ %}
'''
"""
self.start = "%{"
self.end = "%}"
return readMultiLineDiff(file, self.start, self.end)

def tripleSlash(self, file):
'''
"""
sign: ///
'''
self.sign = '///'
self.pattern = r'''(?<!["'`])\/\/\/\s*(.*)'''
"""
self.sign = "///"
self.pattern = r"""(?<!["'`])\/\/\/\s*(.*)"""
return readSingleLine(file, self.pattern)

def slashDoubleStar(self, file):
'''
"""
sign: /** ~ */
'''
"""
self.start = "/**"
self.end = "*/"
return readMultiLineDiff(file, self.start, self.end)
Loading

0 comments on commit e3e2596

Please sign in to comment.