From d682b740539b6c9f1bb0f1e87fc4a9ec3327f8cd Mon Sep 17 00:00:00 2001 From: Abdul-Muqadim-Arbisoft <139064778+Abdul-Muqadim-Arbisoft@users.noreply.github.com> Date: Tue, 28 May 2024 11:06:51 +0500 Subject: [PATCH] feat: add IS_FILE_RENDERED filter to enhance file processing flexibility (#1062) --- ...8_abdul.muqadim_is_file_rendered_filter.md | 1 + tutor/env.py | 24 +++++++++++++++++-- tutor/hooks/catalog.py | 9 +++++++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 changelog.d/20240508_111948_abdul.muqadim_is_file_rendered_filter.md diff --git a/changelog.d/20240508_111948_abdul.muqadim_is_file_rendered_filter.md b/changelog.d/20240508_111948_abdul.muqadim_is_file_rendered_filter.md new file mode 100644 index 0000000..4ccec0c --- /dev/null +++ b/changelog.d/20240508_111948_abdul.muqadim_is_file_rendered_filter.md @@ -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) diff --git a/tutor/env.py b/tutor/env.py index 470f9dc..9d71447 100644 --- a/tutor/env.py +++ b/tutor/env.py @@ -194,8 +194,7 @@ class Renderer: 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: @@ -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 diff --git a/tutor/hooks/catalog.py b/tutor/hooks/catalog.py index d4a4fa9..60ec819 100644 --- a/tutor/hooks/catalog.py +++ b/tutor/hooks/catalog.py @@ -504,6 +504,15 @@ class Filters: #: 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: """