Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Python language support and fix processors intialization #5

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,5 @@ ENV/

# Ramile project ignores
test-output*
.idea
.DS_Store
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Ramile automatically loads the config file `.ramileconfig.json` from the project
| CSS | .css, .less, .sass |
| Swift | .swift |
| Objective-C | .m |
| Python | .py |

## Test:

Expand Down
5 changes: 4 additions & 1 deletion ramile/file_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class FileInfo(object):
file_extension = ''
file_path = ''
is_in_comment_block = False
comment_block_end_sign = None
blank_lines = 0
comment_lines = 0
extracted_lines = 0
Expand All @@ -28,10 +29,12 @@ def found_comment_line(self):
self.comment_lines += 1
return

def mark_comment_block_start(self):
def mark_comment_block_start(self, block_end_sign):
self.is_in_comment_block = True
self.comment_block_end_sign = block_end_sign
return

def mark_comment_block_end(self):
self.is_in_comment_block = False
self.comment_block_end_sign = None
return
Empty file added ramile/filters/__init__.py
Empty file.
63 changes: 63 additions & 0 deletions ramile/filters/comment_block_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from ramile.processors import LineFilterBase


class CommentBlockFilterBase(LineFilterBase):
""" Filtering out comment block """
block_signs = {}

def filter(self, file, line):
if file.is_in_comment_block:
file.found_comment_line()
if self.close_comment_block(file, line):
file.mark_comment_block_end()
return line, True
else:
is_comment_block, comment_block_start_sign, comment_block_end_sign = self.is_comment_block(line)
if is_comment_block:
file.mark_comment_block_start(comment_block_end_sign)
file.found_comment_line()
line_tail = line[len(comment_block_start_sign):]
if self.close_comment_block(file, line_tail):
file.mark_comment_block_end()
return line, True
return line, False

def is_comment_block(self, line):
""" determine whether current line starts a comment block

:param line: current line
:return is_comment_block: whether current line starts a comment block
:return comment_block_end_sign: sign to close the comment block
"""
for sign_start in self.block_signs.keys():
if line.startswith(sign_start):
return True, sign_start, self.block_signs[sign_start]
return False, None, None

def close_comment_block(self, file, line):
return line.endswith(file.comment_block_end_sign)


class PythonCommentBlockFilter(CommentBlockFilterBase):
""" Filtering out Python multi-line docstrings with \"\"\" and '''
"""
block_signs = {
'"""': '"""',
"'''": "'''"
}


class CStyleCommentBlockFilter(CommentBlockFilterBase):
""" Filters out C-style comment blocks with '/*' and '*/'
"""
block_signs = {
'/*': '*/'
}


class HtmlCommentBlockFilter(CommentBlockFilterBase):
""" Filters out html comment blocks with '<!--' and '-->'
"""
block_signs = {
'<!--': '-->'
}
38 changes: 38 additions & 0 deletions ramile/filters/comment_line_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from ramile.processors import LineFilterBase


class CommentFilterBase(LineFilterBase):
""" Filters out single line comments which start with '//'
"""
comment_sign = None
strip_endings = False

def filter(self, file, line):
if line.startswith(self.comment_sign):
file.found_comment_line()
return line, True
if not line:
return line, False
if self.strip_endings:
if self.comment_sign in line:
return line[:line.index(self.comment_sign)], False
return line, False


class DoubleSlashCommentFilter(CommentFilterBase):
""" Filters out single line comments which start with '//'
"""
comment_sign = '//'
strip_endings = True


class SharpCommentFilter(CommentFilterBase):
""" Filters out single line comments which start with '#'
"""
comment_sign = '#'


class ColonCommentFilter(CommentFilterBase):
""" Filters out single line comments which start with ':'
"""
comment_sign = ':'
15 changes: 11 additions & 4 deletions ramile/processors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ class FileProcessorBase(object):
""" Base class for file processors. The processor for each lanuage should inherit from this class.
"""
expected_extensions = []
filters = []

def __init__(self):
# by default, processors of all languages will always starts with a blank line filter
self.filters = []
self.filters.append(BlankLineFilter())
return

Expand Down Expand Up @@ -49,7 +49,7 @@ class LineFilterBase(object):
""" A filter will process each line, and determine whether each line should be dropped. A filter can also perform any neccessary process on the line and replace the original line.
"""

def filter(self, line):
def filter(self, file, line):
""" Filters a line of code, outputs the filtered content, and a flag whether the line should be dropped.
"""
return line, False
Expand Down Expand Up @@ -83,6 +83,10 @@ def __build_processors(self):
self.__cache_processor(CssProcessor())
self.__cache_processor(SwiftProcessor())
self.__cache_processor(OCProcessor())
self.__cache_processor(PythonProcessor())
self.__cache_processor(CppProcessor())
self.__cache_processor(JsonProcessor())
self.__cache_processor(BatchProcessor())
self.__cache_processor(GoProcessor())
return

Expand All @@ -96,13 +100,16 @@ def __cache_processor(self, processor):
return


from ramile.processors.blank_line_filter import BlankLineFilter
from ramile.processors.comment_block_filter import CommentBlockFilter
from ramile.filters.blank_line_filter import BlankLineFilter
from ramile.processors.js_processor import JsProcessor
from ramile.processors.java_processor import JavaProcessor
from ramile.processors.php_processor import PhpProcessor
from ramile.processors.html_processor import HtmlProcessor
from ramile.processors.css_processor import CssProcessor
from ramile.processors.swift_processor import SwiftProcessor
from ramile.processors.oc_processor import OCProcessor
from ramile.processors.python_processor import PythonProcessor
from ramile.processors.cpp_processor import CppProcessor
from ramile.processors.json_processor import JsonProcessor
from ramile.processors.bat_processor import BatchProcessor
from ramile.processors.go_processor import GoProcessor
20 changes: 20 additions & 0 deletions ramile/processors/bat_processor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#! /usr/bin/env python
#
# Copyright 2021 eVision.ai Inc. All Rights Reserved.
#
# @author: Chen Shijiang([email protected])
# @date: 2021-01-04 19:53
# @version: 1.0
#
from ramile.filters.comment_line_filter import ColonCommentFilter, SharpCommentFilter
from ramile.processors import FileProcessorBase


class BatchProcessor(FileProcessorBase):
expected_extensions = ['.bat', '.cmd']

def __init__(self):
super().__init__()
self.filters.append(SharpCommentFilter())
self.filters.append(ColonCommentFilter())
return
21 changes: 0 additions & 21 deletions ramile/processors/c_style_comment_block_filter.py

This file was deleted.

12 changes: 0 additions & 12 deletions ramile/processors/comment_block_filter.py

This file was deleted.

12 changes: 12 additions & 0 deletions ramile/processors/cpp_processor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from ramile.filters.comment_block_filter import CStyleCommentBlockFilter
from ramile.filters.comment_line_filter import DoubleSlashCommentFilter
from ramile.processors import FileProcessorBase


class CppProcessor(FileProcessorBase):
expected_extensions = ['.cpp', '.c', 'h']

def __init__(self):
super().__init__()
self.filters.append(DoubleSlashCommentFilter())
self.filters.append(CStyleCommentBlockFilter())
8 changes: 3 additions & 5 deletions ramile/processors/css_processor.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
from ramile.filters.comment_block_filter import CStyleCommentBlockFilter
from ramile.filters.comment_line_filter import DoubleSlashCommentFilter
from ramile.processors import FileProcessorBase
from ramile.processors import BlankLineFilter
from ramile.processors.c_style_comment_block_filter import CStyleCommentBlockFilter
from ramile.processors.double_slash_comment_filter import DoubleSlashCommentFilter
from ramile.processors.html_comment_block_filter import HtmlCommentBlockFilter


class CssProcessor(FileProcessorBase):
expected_extensions = ['.css', '.less', '.sass']

def __init__(self):
self.filters.append(BlankLineFilter())
super().__init__()
self.filters.append(CStyleCommentBlockFilter())
self.filters.append(DoubleSlashCommentFilter())
return
12 changes: 0 additions & 12 deletions ramile/processors/double_slash_comment_filter.py

This file was deleted.

21 changes: 0 additions & 21 deletions ramile/processors/html_comment_block_filter.py

This file was deleted.

8 changes: 3 additions & 5 deletions ramile/processors/html_processor.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
from ramile.filters.comment_block_filter import CStyleCommentBlockFilter, HtmlCommentBlockFilter
from ramile.filters.comment_line_filter import DoubleSlashCommentFilter
from ramile.processors import FileProcessorBase
from ramile.processors import BlankLineFilter
from ramile.processors.c_style_comment_block_filter import CStyleCommentBlockFilter
from ramile.processors.double_slash_comment_filter import DoubleSlashCommentFilter
from ramile.processors.html_comment_block_filter import HtmlCommentBlockFilter


class HtmlProcessor(FileProcessorBase):
expected_extensions = ['.html', '.htm']

def __init__(self):
self.filters.append(BlankLineFilter())
super().__init__()
self.filters.append(CStyleCommentBlockFilter())
self.filters.append(DoubleSlashCommentFilter())
self.filters.append(HtmlCommentBlockFilter())
Expand Down
7 changes: 3 additions & 4 deletions ramile/processors/java_processor.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from ramile.filters.comment_block_filter import CStyleCommentBlockFilter
from ramile.filters.comment_line_filter import DoubleSlashCommentFilter
from ramile.processors import FileProcessorBase
from ramile.processors import BlankLineFilter
from ramile.processors.c_style_comment_block_filter import CStyleCommentBlockFilter
from ramile.processors.double_slash_comment_filter import DoubleSlashCommentFilter


class JavaProcessor(FileProcessorBase):
expected_extensions = ['.java']

def __init__(self):
self.filters.append(BlankLineFilter())
super().__init__()
self.filters.append(CStyleCommentBlockFilter())
self.filters.append(DoubleSlashCommentFilter())
return
7 changes: 3 additions & 4 deletions ramile/processors/js_processor.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from ramile.filters.comment_block_filter import CStyleCommentBlockFilter
from ramile.filters.comment_line_filter import DoubleSlashCommentFilter
from ramile.processors import FileProcessorBase
from ramile.processors import BlankLineFilter
from ramile.processors.c_style_comment_block_filter import CStyleCommentBlockFilter
from ramile.processors.double_slash_comment_filter import DoubleSlashCommentFilter


class JsProcessor(FileProcessorBase):
expected_extensions = ['.js', '.jsx', '.vue', '.wpy']

def __init__(self):
self.filters.append(BlankLineFilter())
super().__init__()
self.filters.append(CStyleCommentBlockFilter())
self.filters.append(DoubleSlashCommentFilter())
return
19 changes: 19 additions & 0 deletions ramile/processors/json_processor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#! /usr/bin/env python
#
# Copyright 2021 eVision.ai Inc. All Rights Reserved.
#
# @author: Chen Shijiang([email protected])
# @date: 2021-01-04 19:53
# @version: 1.0
#
from ramile.filters.comment_line_filter import DoubleSlashCommentFilter
from ramile.processors import FileProcessorBase


class JsonProcessor(FileProcessorBase):
expected_extensions = ['.json', '.gradle']

def __init__(self):
super().__init__()
self.filters.append(DoubleSlashCommentFilter())
return
Loading