Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
phil65 committed Sep 19, 2023
1 parent 74a6deb commit 0c7e960
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 43 deletions.
48 changes: 5 additions & 43 deletions mknodes/jinja/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,13 @@

from collections.abc import Mapping
import contextlib
import datetime
import functools
import os
import pathlib

from typing import TYPE_CHECKING, Any

import jinja2

from mknodes import paths
from mknodes.utils import (
helpers,
inspecthelpers,
jinjahelpers,
log,
mergehelpers,
yamlhelpers,
)
from mknodes.utils import jinjahelpers, log, mergehelpers


if TYPE_CHECKING:
Expand All @@ -29,33 +18,6 @@
logger = log.get_logger(__name__)


@functools.cache
def load_file(path: str | os.PathLike) -> str:
return pathlib.Path(path).read_text(encoding="utf-8")


ENVIRONMENT_GLOBALS = {
"log": log.log_stream.getvalue,
"now": datetime.datetime.now,
"str": str,
"inspecthelpers": inspecthelpers,
"resources_dir": paths.RESOURCES,
}
ENVIRONMENT_FILTERS = {
"dump_yaml": yamlhelpers.dump_yaml,
"styled": helpers.styled,
"rstrip": str.rstrip,
"lstrip": str.lstrip,
"removesuffix": str.removesuffix,
"removeprefix": str.removeprefix,
"issubclass": issubclass,
"isinstance": isinstance,
"hasattr": hasattr,
"load_file": load_file,
"path_join": os.path.join,
}


class Environment(jinja2.Environment):
"""Jinja environment."""

Expand All @@ -64,8 +26,8 @@ def __init__(self, *, undefined: str = "silent", load_templates: bool = False):
behavior = jinjahelpers.UNDEFINED_BEHAVIOR[undefined]
self.extra_files: set[str] = set()
super().__init__(undefined=behavior, loader=loader)
self.filters.update(ENVIRONMENT_FILTERS)
self.globals.update(ENVIRONMENT_GLOBALS)
self.filters.update(jinjahelpers.ENVIRONMENT_FILTERS)
self.globals.update(jinjahelpers.ENVIRONMENT_GLOBALS)

def set_mknodes_filters(self, parent: mk.MkNode | None = None):
"""Set our MkNode filters.
Expand Down Expand Up @@ -125,7 +87,7 @@ def add_template(self, file: str | os.PathLike):
if file in self.extra_files:
return
self.extra_files.add(file)
content = load_file(file)
content = jinjahelpers.load_file(file)
new_loader = jinja2.DictLoader({file: content})
match self.loader:
case jinja2.ChoiceLoader():
Expand All @@ -144,7 +106,7 @@ def render_file(self, file: str | os.PathLike, variables: dict | None = None) ->
file: Template file to load
variables: Extra variables for the environment
"""
content = load_file(str(file))
content = jinjahelpers.load_file(str(file))
return self.render_string(content, variables)

def render_template(
Expand Down
38 changes: 38 additions & 0 deletions mknodes/utils/jinjahelpers.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,56 @@
from __future__ import annotations

from collections.abc import Callable
import datetime
import functools

from importlib import util
import os
import pathlib
from typing import Any

import jinja2

from mknodes import paths
from mknodes.utils import (
helpers,
inspecthelpers,
log,
yamlhelpers,
)


resources_loader = jinja2.FileSystemLoader(searchpath="mknodes/resources")
docs_loader = jinja2.FileSystemLoader(searchpath="docs/")
resource_loader = jinja2.ChoiceLoader([resources_loader, docs_loader])


@functools.cache
def load_file(path: str | os.PathLike) -> str:
return pathlib.Path(path).read_text(encoding="utf-8")


ENVIRONMENT_GLOBALS = {
"log": log.log_stream.getvalue,
"now": datetime.datetime.now,
"str": str,
"inspecthelpers": inspecthelpers,
"resources_dir": paths.RESOURCES,
}
ENVIRONMENT_FILTERS = {
"dump_yaml": yamlhelpers.dump_yaml,
"styled": helpers.styled,
"rstrip": str.rstrip,
"lstrip": str.lstrip,
"removesuffix": str.removesuffix,
"removeprefix": str.removeprefix,
"issubclass": issubclass,
"isinstance": isinstance,
"hasattr": hasattr,
"load_file": load_file,
"path_join": os.path.join,
}

# material_partials_loader = jinja2.PackageLoader("material", "partials")


Expand Down

0 comments on commit 0c7e960

Please sign in to comment.