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 IS_FILE_RENDERED filter to enhance file processing flexibility #1062

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- [Feature] Introduces the IS_FILE_RENDERED Filter, enabling developers to specify files that should be copied without rendering. (by @Abdul-Muqadim-Arbisoft)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: having a bit more context here can be helpful for the devs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

concern addressed

11 changes: 9 additions & 2 deletions tutor/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,7 @@ def render_template(self, template_name: str) -> t.Union[str, bytes]:
The template_name *always* uses "/" separators, and is not os-dependent. Do not pass the result of
os.path.join(...) to this function.
"""
if is_binary_file(template_name):
# Don't try to render binary files
if not hooks.Filters.IS_FILE_RENDERED.apply(True, template_name):
return self.environment.read_bytes(template_name)

try:
Expand Down Expand Up @@ -551,3 +550,11 @@ def _delete_plugin_templates(plugin: str, root: str, _config: Config) -> None:
raise exceptions.TutorError(
f"Could not delete file {e.filename} from plugin {plugin} in folder {path}"
)


@hooks.Filters.IS_FILE_RENDERED.add()
def _do_not_render_binary_files(result: bool, path: str) -> bool:
"""Return bianry file"""
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: the docstring does not make sense. Please update it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

concern addressed

if result and is_binary_file(path):
result = False
return result
9 changes: 9 additions & 0 deletions tutor/hooks/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,15 @@ def your_filter_callback(some_data):
#: filter themselves, but they can apply it to check whether other plugins are enabled.
PLUGINS_LOADED: Filter[list[str], []] = Filter()

#: Use this filter to determine whether a file should be rendered. This can be useful in scenarios where
#: certain types of files need special handling, such as binary files, which should not be rendered as text.
#:
#: This filter expects a boolean return value that indicates whether the file should be rendered.
#:
#: :param bool should_render: Initial decision on rendering the file, typically set to True.
#: :param str file_path: The path to the file being checked.
IS_FILE_RENDERED: Filter[bool, [str]] = Filter()


class Contexts:
"""
Expand Down
Loading