Skip to content

Commit

Permalink
use replacement cookiecutter to render files
Browse files Browse the repository at this point in the history
  • Loading branch information
mcflugen committed Mar 23, 2024
1 parent 205c14e commit 6e23709
Showing 1 changed file with 46 additions and 17 deletions.
63 changes: 46 additions & 17 deletions babelizer/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

from __future__ import annotations

import contextlib
import datetime
import os
import sys
from collections.abc import Generator
from typing import Any

import git
Expand Down Expand Up @@ -44,6 +43,51 @@ def render(
clobber: bool = False,
version: str = "0.1",
make_pretty: bool = False,
) -> str:
from babelizer._cookiecutter import cookiecutter

if template is None:
template = get_datadir()

context = {
"cookiecutter": {
"files": {
"_bmi.py": render_bmi(plugin_metadata),
"__init__.py": render_init(plugin_metadata),
"lib/__init__.py": render_lib_init(plugin_metadata),
".gitignore": render_gitignore(plugin_metadata),
"LICENSE.rst": render_license(plugin_metadata),
},
"now": datetime.datetime.now(),
}
| plugin_metadata.as_cookiecutter_context()
}

cookiecutter(
template,
extra_context=context,
output_dir=output,
no_input=True,
overwrite_if_exists=clobber,
)

path = os.path.realpath(output)

with open(os.path.join(path, "babel.toml"), "w") as fp:
plugin_metadata.dump(fp, fmt="toml")

git.Repo.init(path)

return path


def render_with_cookiecutter(
plugin_metadata: BabelMetadata,
output: str,
template: str | None = None,
clobber: bool = False,
version: str = "0.1",
make_pretty: bool = False,
) -> str:
"""Generate a babelized library.
Expand Down Expand Up @@ -153,21 +197,6 @@ def render_plugin_repo(
return path


@contextlib.contextmanager
def as_cwd(path: str) -> Generator[None, None, None]:
"""Change directory context.
Parameters
----------
path : str
Path-like object to a directory.
"""
prev_cwd = os.getcwd()
os.chdir(path)
yield
os.chdir(prev_cwd)


def blacken_file(filepath: str) -> None:
"""Format a Python file with ``black``.
Expand Down

0 comments on commit 6e23709

Please sign in to comment.