No project description provided
Project description
templa
Templa is a Python library that offers a flexible and typed building system using Jinja2 templates for your code. It allows you to easily render, parse, process, and build. The following are the assumed build processes in Templa:
- Define the required context with dataclass for rendering a Jinja2 formatted template.
- Render the template with Jinja2 library using the provided context.
- Extract relevant information and elements from the rendered template for further processing.
- Perform necessary processing on the parsed rendered template.
- Utilize the processed rendered template to generate desired outputs or objects.
from dataclasses import dataclass
from typing import List
from typing import Optional
import jinja2
import yaml
from templa.builder import Builder
from templa.config import Config
from templa.config import ConfigData
def parse_rendered_template_yaml_stub(rendered_str: str) -> Optional[List[str]]:
parsed_rendered_template = yaml.safe_load(rendered_str)
assert (
isinstance(parsed_rendered_template, list) or parsed_rendered_template is None
)
return parsed_rendered_template
def build_parsed_rendered_template_yaml_stub(
parsed_rendered_template: Optional[List[str]],
) -> Optional[str]:
built = yaml.dump(parsed_rendered_template, Dumper=yaml.SafeDumper)
assert isinstance(built, str)
return built
class SampleTemplateGetter:
def get_template(self) -> jinja2.Template:
environment = jinja2.Environment(
loader=jinja2.DictLoader(
{"template.txt": "---\n- {{ foo }}\n- {{ bar }}\n- {{ zoo }}"}
)
)
return environment.get_template("template.txt")
@dataclass(frozen=True)
class SampleConfigData(ConfigData):
foo: str
bar: str
class SampleBuilder(Builder[List[str], str]):
def __init__(
self,
) -> None:
super().__init__(
config=Config[SampleConfigData]({"foo": "FOO", "bar": 5}, SampleConfigData),
template_getter=SampleTemplateGetter(),
parse_rendered_template=parse_rendered_template_yaml_stub,
build_processed=build_parsed_rendered_template_yaml_stub,
)
def _build(self, processed: Optional[List[str]]) -> Optional[str]:
if processed is not None:
processed[2] = "ZOO_Updated"
return super()._build(processed)
def build_sample_template() -> None:
builder = SampleBuilder()
initialized_target = builder.init_builder_target()
# It will be typing error because the builder variable is in a
# state of an unfinished type before the build process is
# completed, causing a type error.
# builder.build(initialized_target)
# -> Argument 1 to "build" of "Builder" has incompatible type
# "BuilderTargetInitialized"; expected "BuilderTargetProcessed" [arg-type]
processed_target = builder.process_template(initialized_target)
built_target = builder.build(processed_target)
print(builder.fetch_built(built_target))
if __name__ == "__main__":
build_sample_template()
Install
$ pip install 'templa@git+https://github.com/yuxki/templa.git'
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
templa-0.1.0.tar.gz
(7.8 kB
view details)
Built Distribution
File details
Details for the file templa-0.1.0.tar.gz
.
File metadata
- Download URL: templa-0.1.0.tar.gz
- Upload date:
- Size: 7.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.1 CPython/3.11.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a8fd95f654bdc2f0d7135273925f7c2c791d8cc935d6e9c2de1a33057085c6d0 |
|
MD5 | ac44d4a949b36f8816032c9d4fd11824 |
|
BLAKE2b-256 | a4bf8b0110ff5d59d476f8aa7ee1e9d6a2c45f959bc382abf39234afaac75618 |
File details
Details for the file templa-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: templa-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.1 CPython/3.11.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 139db911231f86d62d816fdfdef273bff056bcee1ec3828daf9040987634634a |
|
MD5 | e760a73ba36c0502259a8ef42103a0be |
|
BLAKE2b-256 | 6973d8e034e6a45cd372a730e225676a31a10961a10371e92b0591e25d3fef19 |