Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into nightly
Browse files Browse the repository at this point in the history
  • Loading branch information
Overhang.IO committed May 28, 2024
2 parents 9e0ff48 + d682b74 commit 2d4562b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- [Feature] Introduces the IS_FILE_RENDERED Filter, which allows developers to specify files that should be copied directly without rendering. This update empowers developers to manage special file types, ensuring that they are transferred intact without undergoing template processing. (by @Abdul-Muqadim-Arbisoft)
24 changes: 22 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,24 @@ 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:
"""
Determine whether a file should be rendered based on its binary nature.
This function checks if a file is binary and updates the rendering decision accordingly.
If the initial decision (`result`) is to render the file, but the file is detected as binary,
the function will override this decision to prevent rendering.
Parameters:
- result (bool): The initial decision on whether the file should be rendered.
- path (str): The file path to check for binary content.
Returns:
- bool: False if the file is binary and was initially set to be rendered, otherwise returns the initial decision.
"""
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 @@ -504,6 +504,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

0 comments on commit 2d4562b

Please sign in to comment.