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

Pyta Template File Now Resolves Relative To Cwd #1140

Merged
Show file tree
Hide file tree
Changes from 3 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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Simon Chen,
Freeman Cheng,
Ivan Chepelev,
Yianni Culmone,
Daniel Dervishi,
Nigel Fong,
Adam Gleizer,
Ibrahim Hasan,
Expand Down
4 changes: 2 additions & 2 deletions docs/usage/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`)

Expand Down
5 changes: 4 additions & 1 deletion python_ta/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = "",
Expand Down Expand Up @@ -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 = (
(
Expand All @@ -296,7 +299,7 @@ def reset_linter(
(
"pyta-template-file",
{
"default": "template.html.jinja",
"default": os.path.join(TEMPLATES_DIR, "template.html.jinja"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not very common to have default values that are dynamically-set (in this case, based on the installation location for PythonTA). A more conventional approach is to use a default value like '', and then in the code which uses the value the dynamic TEMPLATES_DIR location is computed and the default template is used.

"type": "string",
"metavar": "<pyta_reporter>",
"help": "HTML template file for the HTMLReporter.",
Expand Down
5 changes: 3 additions & 2 deletions python_ta/config/.pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions python_ta/reporters/html_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions tests/test.pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down