Skip to content

Commit

Permalink
⚗️(ozi.render.load_environment): Add globals dict argument.
Browse files Browse the repository at this point in the history
Signed-off-by: rjdbcm <[email protected]>
  • Loading branch information
rjdbcm committed Apr 26, 2024
1 parent 4c8ee58 commit eaac863
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 15 deletions.
3 changes: 2 additions & 1 deletion ozi/fix/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from ozi.fix.parser import parser
from ozi.fix.rewrite_command import Rewriter
from ozi.render import load_environment
from ozi.spec import METADATA
from ozi.tap import TAP


Expand All @@ -31,7 +32,7 @@ def main() -> NoReturn: # pragma: no cover
project.add = list(set(project.add))
project.remove.remove('ozi.phony')
project.remove = list(set(project.remove))
env = load_environment(vars(project))
env = load_environment(vars(project), METADATA.asdict())

match [project.missing, project.strict]:
case [True, False]:
Expand Down
6 changes: 4 additions & 2 deletions ozi/new/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from pathlib import Path
from typing import TYPE_CHECKING

from ozi.spec import METADATA

if TYPE_CHECKING:
from argparse import Namespace
from typing import Callable
Expand Down Expand Up @@ -111,13 +113,13 @@ def project(project: Namespace) -> None:
project = postprocess_arguments(preprocess_arguments(project))
create_project_files(
project=project,
env=load_environment(vars(project)),
env=load_environment(vars(project), METADATA.asdict()),
)


def wrap(project: Namespace) -> None: # pragma: no cover
"""Create a new wrap file for publishing. Not a public function."""
env = load_environment(vars(project))
env = load_environment(vars(project), METADATA.asdict())
template = env.get_template('ozi.wrap.j2')
with open('ozi.wrap', 'w') as f:
f.write(template.render())
Expand Down
11 changes: 5 additions & 6 deletions ozi/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
"""Rendering utilities for the OZI project templates."""
from functools import _lru_cache_wrapper
from functools import lru_cache
from pathlib import Path
from types import FunctionType
from typing import Any
from warnings import warn

from git import InvalidGitRepositoryError
Expand Down Expand Up @@ -36,8 +36,7 @@
)


@lru_cache
def _init_environment() -> Environment:
def _init_environment(_globals: dict[str, Any]) -> Environment:
"""Initialize the rendering environment, set filters, and set global metadata."""
env = Environment(
loader=PackageLoader('ozi'),
Expand All @@ -53,17 +52,17 @@ def _init_environment() -> Environment:
env.filters.setdefault(f.__name__, f)
case _lru_cache_wrapper(): # pragma: defer to pyright,mypy
env.filters.setdefault(f.__wrapped__.__name__, f)
env.globals = env.globals | METADATA.asdict()
env.globals = env.globals | _globals
return env


def load_environment(project: dict[str, str]) -> Environment:
def load_environment(project: dict[str, str], _globals: dict[str, Any]) -> Environment:
"""Load the rendering environment for templates.
:return: jinja2 rendering environment for OZI
:rtype: Environment
"""
env = _init_environment()
env = _init_environment(_globals)
env.globals = env.globals | {'project': project}
return env

Expand Down
2 changes: 1 addition & 1 deletion tests/test_ozi_fix.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
missing=True,
)

env = load_environment(vars(bad_namespace))
env = load_environment(vars(bad_namespace), METADATA.asdict())


@pytest.fixture
Expand Down
10 changes: 5 additions & 5 deletions tests/test_ozi_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
fullmatch=True,
),
'author': st.lists(
st.text(st.characters(exclude_categories=['C']), min_size=1, max_size=16),
st.text(st.characters(exclude_categories=['C'], exclude_characters=['\\']), min_size=1, max_size=16),
min_size=1,
max_size=8,
unique=True,
Expand All @@ -51,7 +51,7 @@
max_size=8,
),
'maintainer': st.lists(
st.text(st.characters(exclude_categories=['C']), min_size=1, max_size=16),
st.text(st.characters(exclude_categories=['C'], exclude_characters=['\\']), min_size=1, max_size=16),
min_size=1,
max_size=8,
unique=True,
Expand All @@ -65,9 +65,9 @@
st.just('A, https://oziproject.dev'),
max_size=1,
),
'summary': st.text(st.characters(exclude_categories=['C']), max_size=255),
'summary': st.text(st.characters(exclude_categories=['C'], exclude_characters=['\\']), max_size=255),
'copyright_head': st.text(
st.characters(exclude_categories=['C']),
st.characters(exclude_categories=['C'], exclude_characters=['\\']),
max_size=255,
),
'license_file': st.just('LICENSE.txt'),
Expand Down Expand Up @@ -122,7 +122,7 @@ def test_fuzz_new_project_good_namespace( # noqa: DC102, RUF100
postprocessed = ozi.new.__main__.postprocess_arguments(preprocessed)
ozi.new.__main__.create_project_files(
postprocessed,
env=load_environment(vars(postprocessed)),
env=load_environment(vars(postprocessed), METADATA.asdict()),
)
name, _ = required_pkg_info(postprocessed.target)
required_files('root', postprocessed.target, postprocessed.name)
Expand Down

0 comments on commit eaac863

Please sign in to comment.