From 5df2c0dbf49859eadb74351435d6a8e9ee5d3660 Mon Sep 17 00:00:00 2001 From: Daniel Dervishi Date: Tue, 4 Feb 2025 15:17:25 -0500 Subject: [PATCH 1/4] Task Completed --- README.md | 1 + docs/usage/configuration.md | 4 ++-- python_ta/__init__.py | 5 ++++- python_ta/config/.pylintrc | 5 +++-- python_ta/reporters/html_reporter.py | 10 +++++++--- tests/test.pylintrc | 5 +++-- 6 files changed, 20 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 94a89353e..2173c2334 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,7 @@ Simon Chen, Freeman Cheng, Ivan Chepelev, Yianni Culmone, +Daniel Dervishi, Nigel Fong, Adam Gleizer, Ibrahim Hasan, diff --git a/docs/usage/configuration.md b/docs/usage/configuration.md index 6443951e1..a1b07437b 100644 --- a/docs/usage/configuration.md +++ b/docs/usage/configuration.md @@ -118,9 +118,9 @@ The maximum number of occurrences of each check to report. This option can be used to limit the size of the output report. If set to 0 (the default), all occurrences are shown. -### `pyta-template-file` (default: `"template.html.jinja"`) +### `pyta-template-file` (default: Path to PythonTA's `template.html.jinja`) -HTML template file for the HTMLReporter. +Path to HTML template file for the HTMLReporter. ### `allow-pylint-comments` (default: `false`) diff --git a/python_ta/__init__.py b/python_ta/__init__.py index c238ef441..976916252 100644 --- a/python_ta/__init__.py +++ b/python_ta/__init__.py @@ -63,6 +63,8 @@ # Flag to determine if we've previously patched pylint PYLINT_PATCHED = False +TEMPLATES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "reporters/templates") + def check_errors( module_name: Union[list[str], str] = "", @@ -282,6 +284,7 @@ def reset_linter( - If the config argument is a dictionary, apply those options afterward. Do not re-use a linter object. Returns a new linter. """ + # Tuple of custom options. Note: 'type' must map to a value equal a key in the pylint/config/option.py `VALIDATORS` dict. new_checker_options = ( ( @@ -296,7 +299,7 @@ def reset_linter( ( "pyta-template-file", { - "default": "template.html.jinja", + "default": os.path.join(TEMPLATES_DIR, "template.html.jinja"), "type": "string", "metavar": "", "help": "HTML template file for the HTMLReporter.", diff --git a/python_ta/config/.pylintrc b/python_ta/config/.pylintrc index 3828cf223..fc06b7b2f 100644 --- a/python_ta/config/.pylintrc +++ b/python_ta/config/.pylintrc @@ -8,8 +8,9 @@ pyta-number-of-messages = 0 # (DEPRECATED: Use output-format option below.) Set the [REPORTS] output-format option instead. # pyta-reporter = HTMLReporter -# Set the location of the template for HTMLReporter. -pyta-template-file = template.html.jinja +# Set the path of the template for HTMLReporter. Ex: +# pyta-template-file = ../../templates/template.html.jinja +# pyta-template-file = /Users/user/pyta/python_ta/reporters/templates/template.html.jinja #Set whether you wish to opt-in to anonymous data collection of errors reported when you run PyTA. pyta-error-permission = no diff --git a/python_ta/reporters/html_reporter.py b/python_ta/reporters/html_reporter.py index bd5d11a78..47d95f876 100644 --- a/python_ta/reporters/html_reporter.py +++ b/python_ta/reporters/html_reporter.py @@ -12,8 +12,6 @@ from .core import PythonTaReporter -TEMPLATES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "templates") - class HTMLReporter(PythonTaReporter): """Reporter that displays results in HTML form. @@ -62,9 +60,15 @@ def display_messages(self, layout: BaseLayout) -> None: grouped_messages = {path: self.group_messages(msgs) for path, msgs in self.messages.items()} template_f = self.linter.config.pyta_template_file - template = Environment(loader=FileSystemLoader(TEMPLATES_DIR)).get_template(template_f) + path = os.path.abspath(template_f) + filename, file_parent_directory = os.path.basename(path), os.path.dirname(path) + + template = Environment(loader=FileSystemLoader(file_parent_directory)).get_template( + filename + ) # Embed resources so the output html can go anywhere, independent of assets. + # TEMPLATES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "templates") # with open(os.path.join(TEMPLATES_DIR, 'pyta_logo_markdown.png'), 'rb+') as image_file: # # Encode img binary to base64 (+33% size), decode to remove the "b'" # pyta_logo_base64_encoded = b64encode(image_file.read()).decode() diff --git a/tests/test.pylintrc b/tests/test.pylintrc index 565ead9d0..76ecf4bad 100644 --- a/tests/test.pylintrc +++ b/tests/test.pylintrc @@ -8,8 +8,9 @@ pyta-number-of-messages = 0 # (DEPRECATED: Use output-format option below.) Set the [REPORTS] output-format option instead. # pyta-reporter = HTMLReporter -# Set the location of the template for HTMLReporter. -pyta-template-file = template.html.jinja +# Set the path of the template for HTMLReporter. Ex: +# pyta-template-file = ../../templates/template.html.jinja +# pyta-template-file = /Users/user/pyta/python_ta/reporters/templates/template.html.jinja #Set whether you wish to opt-in to anonymous data collection of errors reported when you run PyTA. pyta-error-permission = no From f71e45d4a1bf272d19f91584929d862826389a2f Mon Sep 17 00:00:00 2001 From: Daniel Dervishi Date: Tue, 4 Feb 2025 15:41:45 -0500 Subject: [PATCH 2/4] Updated Changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba04163fd..4d2a07ec4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ### ✨ Enhancements - Added custom error message for `comparison-with-callable` +- Changed `pyta-template-file` argument to now resolve the file path relative to the CWD. ### 💫 New checkers From 79a9dd4f643c521158e0d37d724b239db54aefbc Mon Sep 17 00:00:00 2001 From: Daniel Dervishi Date: Fri, 7 Feb 2025 18:57:39 -0500 Subject: [PATCH 3/4] Addressed Comments --- python_ta/__init__.py | 4 +--- python_ta/reporters/html_reporter.py | 5 +++++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/python_ta/__init__.py b/python_ta/__init__.py index 976916252..74c646486 100644 --- a/python_ta/__init__.py +++ b/python_ta/__init__.py @@ -63,8 +63,6 @@ # Flag to determine if we've previously patched pylint PYLINT_PATCHED = False -TEMPLATES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "reporters/templates") - def check_errors( module_name: Union[list[str], str] = "", @@ -299,7 +297,7 @@ def reset_linter( ( "pyta-template-file", { - "default": os.path.join(TEMPLATES_DIR, "template.html.jinja"), + "default": "", "type": "string", "metavar": "", "help": "HTML template file for the HTMLReporter.", diff --git a/python_ta/reporters/html_reporter.py b/python_ta/reporters/html_reporter.py index 47d95f876..c9f561764 100644 --- a/python_ta/reporters/html_reporter.py +++ b/python_ta/reporters/html_reporter.py @@ -12,6 +12,8 @@ from .core import PythonTaReporter +TEMPLATES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "templates") + class HTMLReporter(PythonTaReporter): """Reporter that displays results in HTML form. @@ -60,6 +62,9 @@ def display_messages(self, layout: BaseLayout) -> None: grouped_messages = {path: self.group_messages(msgs) for path, msgs in self.messages.items()} template_f = self.linter.config.pyta_template_file + template_f = ( + template_f if template_f != "" else os.path.join(TEMPLATES_DIR, "template.html.jinja") + ) path = os.path.abspath(template_f) filename, file_parent_directory = os.path.basename(path), os.path.dirname(path) From 1175995fdb5fd0ed13970ccd25d5d0c2db5cbf36 Mon Sep 17 00:00:00 2001 From: Daniel Dervishi Date: Fri, 7 Feb 2025 19:01:13 -0500 Subject: [PATCH 4/4] remove redundant comment --- python_ta/reporters/html_reporter.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python_ta/reporters/html_reporter.py b/python_ta/reporters/html_reporter.py index c9f561764..93a1cdbb0 100644 --- a/python_ta/reporters/html_reporter.py +++ b/python_ta/reporters/html_reporter.py @@ -73,7 +73,6 @@ def display_messages(self, layout: BaseLayout) -> None: ) # Embed resources so the output html can go anywhere, independent of assets. - # TEMPLATES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "templates") # with open(os.path.join(TEMPLATES_DIR, 'pyta_logo_markdown.png'), 'rb+') as image_file: # # Encode img binary to base64 (+33% size), decode to remove the "b'" # pyta_logo_base64_encoded = b64encode(image_file.read()).decode()