-
Notifications
You must be signed in to change notification settings - Fork 0
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
Comments
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! |
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. |
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
),
) |
Rather than writing many files to disk and then parsing and zipping them, add an option to
generate_algorithm_template
that takes an openzip_file
handle and write the generated files directly there. It should be exclusive fromoutput_path
and exactly one of those two variables should be defined.The text was updated successfully, but these errors were encountered: