Jinja2 utilities, loaders & fsspec integration.
Project description
jinjarope
How to install
pip
The latest released version is available at the Python package index.
pip install jinjarope
Quick guide
Jinjarope contains a range of Jinja2 loaders (including fsspec-based ones) as well as a jinja2.Environment
subclass with added functionality.
For debugging purposes, an FsSpec filesystem implementation for jinja2 loaders is also included.
FsSpecFileSystemLoader
This loader can be used like a FileSystemLoader, but also works on any fsspec-supported
remote path.
Using the dir::
prefix, any folder can be set as root.
# protocol path
loader = jinjarope.FsSpecFileSystemLoader("dir::github://phil65:jinjarope@main/tests/testresources")
env = jinjarope.Environment(loader=loader)
env.get_template("testfile.jinja").render()
# protocol and storage options
loader = jinjarope.FsSpecFileSystemLoader("github", org="phil65", repo="jinjarope")
env = jinjarope.Environment(loader=loader)
env.get_template("README.md").render()
# fsspec filesystem
fs = fsspec.filesystem("github", org="phil65", repo="jinjarope")
loader = jinjarope.FsSpecFileSystemLoader(fs)
env = jinjarope.Environment(loader=loader)
env.get_template("README.md").render()
FsSpecProtocolPathLoader
This loader accepts any FsSpec protocol path to be used directly. A complete protocol URL to the template file is required.
loader = jinjarope.FsSpecProtocolPathLoader()
env = jinjarope.Environment(loader=loader)
env.get_template("github://phil65:jinjarope@main/tests/testresources/testfile.jinja").render()
NestedDictLoader
[example]
template = "{{ something }}"
content = tomllib.load(toml_file)
loader = jinjarope.NestedDictLoader(content)
env = jinjarope.Environment(loader=loader)
env.get_template("example/template")
General loader information
jinjarope also contains subclasses for all default jinja2 loaders. These loaders
have implementations for some magic methods (__eq__
, __hash__
, __repr__
, , __getitem__
).
loader = jinjarope.FileSystemLoader(...)
template_source = loader["path/to/template.jinja"]
The loaders can also get ORed to return a ChoiceLoader.
choice_loader = jinjarope.FileSystemLoader(..) | jinjarope.PackageLoader(...)
Prefix loaders can get created using pathlib-style string concatenations
prefix_loader = "path_prefix" / jinjarope.FileSystemLoader(...)
Additional filters
pformat
: pprint.pformatrepr
: reprrstrip
: str.rstriplstrip
: str.lstripremovesuffix
: str.removesuffixremoveprefix
: str.removeprefixadd
: Optionally add a prefix / suffix to a value if value is truth-yissubclass
: issubclassisinstance
: isinstanceimport_module
: importlib.import_modulehasattr
: hasattrevaluate
: Evaluate text as python code and return last statementpartial
: functools.partialdump_json
: json.dumpsload_json
: json.loadsload_toml
: tomllib.loadsload_file
: Accepts a path or a fsspec protocol URL and returns content as strpath_join
: os.path.joinformat_js_map
: format_js_map (format a dictionary as JavaScript map)format_css_rule
: format_css_rule (format a dictionary of shape {".a": {"b": "c"}} as CSS rule)check_output
: subprocess.check_output (as str)getenv
: os.getenv
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
Built Distribution
Hashes for jinjarope-0.5.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cbc025d72622b4ba6de3cd91c965ffb5f6060a974a6056ccdcbefd50f2800efd |
|
MD5 | 8dd48f2250c1aeb5a0009908bd07a79a |
|
BLAKE2b-256 | e1cc848818732a7232d94d73cdbd879ac91b125c920dc7556be80f577d56e1d5 |