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

Generate the output directly in a zip file #69

Open
jmsmkn opened this issue Oct 31, 2024 · 3 comments
Open

Generate the output directly in a zip file #69

jmsmkn opened this issue Oct 31, 2024 · 3 comments

Comments

@jmsmkn
Copy link
Member

jmsmkn commented Oct 31, 2024

Rather than writing many files to disk and then parsing and zipping them, add an option to generate_algorithm_template that takes an open zip_file handle and write the generated files directly there. It should be exclusive from output_path and exactly one of those two variables should be defined.

@chrisvanrun
Copy link
Collaborator

Huh, that inverts things nicely. Instead of generating things towards disk and then streaming it into a memory zip_file it would allow directly pushing things into a ZIP. Smart!

@jmsmkn
Copy link
Member Author

jmsmkn commented Oct 31, 2024

I've had a look at this but writing files to disk is unsurprisingly done everywhere. I think the core might be better if it wrote everything to a zip file, then the CLI can handle writing that to disk as necessary.

@jmsmkn
Copy link
Member Author

jmsmkn commented Oct 31, 2024

Copy and render would then look something like:

def copy_and_render(
    *,
    templates_dir_name,
    output_zip_file,
    context,
    prefix=None,
):  
    source_path = PARTIALS_PATH / templates_dir_name

    if not source_path.exists():
        raise TemplateNotFound(source_path)

    env = get_jinja2_environment(searchpath=source_path)

    for source_file in source_path.rglob("*"):
        if not source_file.is_file():
            continue

        check_allowed_source(path=source_file)

        output_file = source_file.relative_to(source_path)

        if prefix:
            output_file = prefix / output_file

        if source_file.endswith(".j2"):
            # Render Jinja2 template
            template = env.get_template(
                name=str(source_file.relative_to(source_path))
            )
            rendered_content = template.render(**context)

            # Write rendered content to output file (without .j2 extension)
            output_file = output_file.with_suffix("")
        else:
            with source_file.open("r") as f:
                rendered_content = f.read()

        if output_file.suffix == ".py":
            rendered_content = black.format_str(rendered_content, fast=False, mode=black.Mode(), write_back=black.WriteBack.YES)
        
        output_zip_file.writestr(
            str(output_file),
            rendered_content,
            zipfile.ZipInfo(str(output_file)).from_file(
                str(source_file), strict_timestamps=False
            ),
        )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants