Skip to content

Commit

Permalink
🚸(ozi.render): add RenderedContent class
Browse files Browse the repository at this point in the history
Signed-off-by: rjdbcm <[email protected]>
  • Loading branch information
rjdbcm committed Jul 9, 2024
1 parent cd0fca7 commit 707d9bc
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 41 deletions.
2 changes: 1 addition & 1 deletion ozi/fix/build_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,6 @@ def walk(
),
)
if rel_path == Path('.') and project_name:
children += [Path('.')]
children += [Path('.')] # pragma: no cover
for child in children:
walk(target, child, found_files=found_files) # pragma: no cover
34 changes: 9 additions & 25 deletions ozi/new/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,16 @@
from ozi.new.validate import valid_project_url
from ozi.new.validate import valid_spdx
from ozi.new.validate import valid_summary
from ozi.render import render_ci_files_set_user
from ozi.render import render_project_files
from ozi.render import RenderedContent

if TYPE_CHECKING:
from argparse import Namespace
from typing import Callable
from typing import TypeAlias

from jinja2 import Environment

Composable: TypeAlias = Callable[[Namespace], Namespace]


def create_project_files(
project: Namespace,
env: Environment,
) -> None:
"""Create the actual project."""
project.allow_file = set(map(Path, project.allow_file))
project.ci_user = render_ci_files_set_user(env, project.target, project.ci_provider)
render_project_files(env, project.target, project.name)
if project.ci_provider == 'github':
Path(
project.target,
f'README.{project.long_description_content_type}',
).symlink_to(Path(project.target, 'README'))
else: # pragma: no cover
pass


def _valid_project(project: Namespace) -> Namespace:
"""Validate a project namespace."""
valid_project_name(name=project.name)
Expand Down Expand Up @@ -123,16 +103,20 @@ def postprocess_arguments(project: Namespace) -> Namespace:
TAP.not_ok(
f'--ci-provider "{project.ci_provider}" unrecognized. ci_user will not be set.',
)
project.allow_file = set(map(Path, project.allow_file))
return project


def project(project: Namespace) -> None:
"""Create a new project in a target directory."""
project = postprocess_arguments(preprocess_arguments(project))
create_project_files(
project=project,
env=load_environment(vars(project), METADATA.asdict()),
)
RenderedContent(
load_environment(vars(project), METADATA.asdict()),
project.target,
project.name,
project.ci_provider,
project.long_description_content_type,
).render()


def wrap(project: Namespace) -> None: # pragma: no cover
Expand Down
44 changes: 44 additions & 0 deletions ozi/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
from tap_producer import TAP

if TYPE_CHECKING:
from typing import Self

from jinja2 import Environment


Expand Down Expand Up @@ -168,6 +170,48 @@ def build_child(env: Environment, parent: str, child: Path) -> None:
)


class RenderedContent:
def __init__(
self: Self, # pyright: ignore
env: Environment,
target: Path,
name: str,
ci_provider: str,
readme_type: str,
) -> None:
"""OZI new project content to render.
.. versionadded:: 1.15.2
:param target: project root path
:type target: Path
:param name: project name
:type name: str
:param ci_provider: :term:`CI` provider setting
:type ci_provider: str
:param readme_type: the README file extension
:type readme_type: str
"""
self.env = env
self.target = target
self.name = name
self.ci_provider = ci_provider
self.readme_type = readme_type
self.ci_user = ''

def render(self: Self) -> None:
"""Render the project."""
self.ci_user = render_ci_files_set_user(self.env, self.target, self.ci_provider)
render_project_files(self.env, self.target, self.name)
if self.ci_provider == 'github':
Path(
self.target,
f'README.{self.readme_type}',
).symlink_to(Path(self.target, 'README'))
else: # pragma: no cover
pass


def render_ci_files_set_user(env: Environment, target: Path, ci_provider: str) -> str:
"""Render :term:`CI` files based on the ci_provider for target in env.
Expand Down
21 changes: 6 additions & 15 deletions tests/test_ozi_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
from hypothesis import strategies as st
from ozi_spec import METADATA # pyright: ignore
from ozi_templates import load_environment # pyright: ignore
from tap_producer import TAP # pyright: ignore

import ozi.actions # pyright: ignore
import ozi.new.__main__ # pyright: ignore
from ozi.fix.missing import required_files # pyright: ignore
from ozi.fix.missing import required_pkg_info # pyright: ignore
import ozi.render # pyright: ignore


@settings(
Expand Down Expand Up @@ -132,20 +130,13 @@ def test_fuzz_new_project_good_namespace( # noqa: DC102, RUF100
namespace = argparse.Namespace(**project)
preprocessed = ozi.new.__main__.preprocess_arguments(namespace)
postprocessed = ozi.new.__main__.postprocess_arguments(preprocessed)
ozi.new.__main__.create_project_files(
postprocessed,
env=load_environment(vars(postprocessed), METADATA.asdict()),
)
name, _ = required_pkg_info(postprocessed.target)
required_files('root', postprocessed.target, postprocessed.name)
required_files('test', postprocessed.target, postprocessed.name)
required_files(
'source',
ozi.render.RenderedContent(
load_environment(vars(postprocessed), METADATA.asdict()),
postprocessed.target,
postprocessed.name,
)
with pytest.raises(SystemExit):
TAP.end()
postprocessed.ci_provider,
postprocessed.long_description_content_type,
).render()


@pytest.mark.parametrize(
Expand Down

0 comments on commit 707d9bc

Please sign in to comment.