-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(download): add an option to archive and download (#6)
- Loading branch information
Jeff Yang
committed
Mar 11, 2021
1 parent
25fdd92
commit 3f87b20
Showing
4 changed files
with
50 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,39 @@ | ||
"""Code Generator base module. | ||
""" | ||
import shutil | ||
from pathlib import Path | ||
|
||
from jinja2 import Environment, FileSystemLoader | ||
|
||
|
||
class CodeGenerator: | ||
def __init__(self, templates_dir=None): | ||
templates_dir = templates_dir or "./templates" | ||
self.template_list = [p.stem for p in Path(templates_dir).iterdir() if p.is_dir()] | ||
self.env = Environment( | ||
loader=FileSystemLoader(templates_dir), trim_blocks=True, lstrip_blocks=True | ||
) | ||
self.env = Environment(loader=FileSystemLoader(templates_dir), trim_blocks=True, lstrip_blocks=True) | ||
|
||
def render_templates(self, template_name: str, config: dict): | ||
"""Renders all the templates from template folder for the given config. | ||
""" | ||
file_template_list = ( | ||
template | ||
for template in self.env.list_templates(".jinja") | ||
if template.startswith(template_name) | ||
template for template in self.env.list_templates(".jinja") if template.startswith(template_name) | ||
) | ||
for fname in file_template_list: | ||
# Get template | ||
template = self.env.get_template(fname) | ||
# Render template | ||
code = template.render(**config) | ||
# Write python file | ||
fname = fname.strip(f"{template_name}/").strip(".jinja") | ||
fname = fname.replace(f"{template_name}/", "").replace(".jinja", "") | ||
self.generate(template_name, fname, code) | ||
yield fname, code | ||
|
||
def generate(self, template_name: str, fname: str, code: str) -> None: | ||
"""Generates `fname` with content `code` in `path`. | ||
""" | ||
path = Path(f"dist/{template_name}") | ||
path.mkdir(parents=True, exist_ok=True) | ||
(path / fname).write_text(code) | ||
self.path = Path(f"./dist/{template_name}") | ||
self.path.mkdir(parents=True, exist_ok=True) | ||
(self.path / fname).write_text(code) | ||
|
||
def make_archive(self): | ||
raise NotImplementedError | ||
def make_archive(self, format_): | ||
return shutil.make_archive(base_name=str(self.path), format=format_, base_dir=self.path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters