-
Notifications
You must be signed in to change notification settings - Fork 372
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from EmbeddedDevops1/5-indentation-requirement-…
…for-python-classes Preprocessor Script for Indents
- Loading branch information
Showing
8 changed files
with
167 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import ast | ||
import textwrap | ||
|
||
|
||
class FilePreprocessor: | ||
def __init__(self, path_to_file): | ||
self.path_to_file = path_to_file | ||
|
||
# List of rules/action key pair. | ||
# Add your new rule and how to process the text (function) here | ||
self.rules = [(self._is_python_file, self._process_if_python)] | ||
|
||
def process_file(self, text: str) -> str: | ||
""" | ||
Process the text based on the internal rules. | ||
""" | ||
for condition, action in self.rules: | ||
if condition(): | ||
return action(text) | ||
return text # Return the text unchanged if no rules apply | ||
|
||
def _is_python_file(self) -> bool: | ||
""" | ||
Rule to check if the file is a Python file. | ||
""" | ||
return self.path_to_file.endswith(".py") | ||
|
||
def _process_if_python(self, text: str) -> str: | ||
""" | ||
Action to process Python files by checking for class definitions and indenting if found. | ||
""" | ||
if self._contains_class_definition(): | ||
return textwrap.indent(text, " ") | ||
return text | ||
|
||
def _contains_class_definition(self) -> bool: | ||
""" | ||
Check if the file contains a Python class definition using the ast module. | ||
""" | ||
try: | ||
with open(self.path_to_file, "r") as file: | ||
content = file.read() | ||
parsed_ast = ast.parse(content) | ||
for node in ast.walk(parsed_ast): | ||
if isinstance(node, ast.ClassDef): | ||
return True | ||
except SyntaxError as e: | ||
print(f"Syntax error when parsing the file: {e}") | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.1.31 | ||
0.1.32 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import pytest | ||
import tempfile | ||
import textwrap | ||
from cover_agent.FilePreprocessor import FilePreprocessor | ||
|
||
|
||
class TestFilePreprocessor: | ||
# Test for a C file | ||
def test_c_file(self): | ||
with tempfile.NamedTemporaryFile(delete=False, suffix=".c") as tmp: | ||
preprocessor = FilePreprocessor(tmp.name) | ||
input_text = "Lorem ipsum dolor sit amet,\nconsectetur adipiscing elit,\nsed do eiusmod tempor incididunt." | ||
processed_text = preprocessor.process_file(input_text) | ||
assert ( | ||
processed_text == input_text | ||
), "C file processing should not alter the text." | ||
|
||
# Test for a Python file with only a function | ||
def test_py_file_with_function_only(self): | ||
with tempfile.NamedTemporaryFile(delete=False, suffix=".py") as tmp: | ||
tmp.write(b"def function():\n pass\n") | ||
tmp.close() | ||
preprocessor = FilePreprocessor(tmp.name) | ||
input_text = "Lorem ipsum dolor sit amet,\nconsectetur adipiscing elit,\nsed do eiusmod tempor incididunt." | ||
processed_text = preprocessor.process_file(input_text) | ||
assert ( | ||
processed_text == input_text | ||
), "Python file without class should not alter the text." | ||
|
||
# Test for a Python file with a comment that looks like a class definition | ||
def test_py_file_with_commented_class(self): | ||
with tempfile.NamedTemporaryFile(delete=False, suffix=".py") as tmp: | ||
tmp.write(b"# class myPythonFile:\n pass\n") | ||
tmp.close() | ||
preprocessor = FilePreprocessor(tmp.name) | ||
input_text = "Lorem ipsum dolor sit amet,\nconsectetur adipiscing elit,\nsed do eiusmod tempor incididunt." | ||
processed_text = preprocessor.process_file(input_text) | ||
assert ( | ||
processed_text == input_text | ||
), "Commented class definition should not trigger processing." | ||
|
||
# Test for a Python file with an actual class definition | ||
def test_py_file_with_class(self): | ||
with tempfile.NamedTemporaryFile(delete=False, suffix=".py") as tmp: | ||
tmp.write(b"class MyClass:\n def method(self):\n pass\n") | ||
tmp.close() | ||
preprocessor = FilePreprocessor(tmp.name) | ||
input_text = "Lorem ipsum dolor sit amet,\nconsectetur adipiscing elit,\nsed do eiusmod tempor incididunt." | ||
processed_text = preprocessor.process_file(input_text) | ||
expected_output = textwrap.indent(input_text, " ") | ||
assert ( | ||
processed_text == expected_output | ||
), "Python file with class should indent the text." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters