Skip to main content

Rybak

/ˈrɨ.bak/

Directory-tree generator library.

Synopsis

Rybak is an extension of template engines, which instead of rendering a single file or string, renders a whole directory.

In a way it's similar to Cookiecutter or Copier, but while these programs are designed specifically for generating software projects scaffolding, Rybak is a library for generating arbitrary directory tree structures.

Rybak template is a directory consisting of template files. Jinja, and Mako are supported, though one project can only use a single template engine.

Goals:

  • generate directory-tree based on a user-provided template and data,
  • keep track and remove files that aren't produced by the template,
  • allow for multiple files to be generated from a single template file,
  • support but don't require templates to be git or other DCVS repositories.

Non-goals:

  • command line interface,
  • prompting users for template data.

Usage

from pathlib import Path
from rybak import TreeTemplate
from rybak.jinja import JinjaAdapter
from jinja2.loaders import FileSystemLoader

TreeTemplate(
    JinjaAdapter(loader=FileSystemLoader('template_root')),
).render(
    {'likes': {
        'Alice': 'Bob',
        'Bob': 'Charlie',
        'Charlie': 'cats',
    }},
    Path('target_root'),
)
  • template_root: is the directory containing template files, in this case Jinja templates. Templates can be used in file content, file names and directory names.

Multiple template roots

With Jinja Adapter you can use ChoiceLoader with a collection of FileSystemLoader and PackageLoader instances.

Mako adapter accepts a collection of directories.

Single template multiple data

Template files can be applied to collections of items. In order to do so, a special function is made available, loop_over that iterates over a passed collection. The function can only be used in templates in file names.

loop_over returns the element of the collection, so that it can be used to render name.

model:

names = [
    'Alice',
    'Bob',
    'Charlie',
]

file name: {{loop_over(names)}} would produce three files: Alice, Bob and Charlie.

The current item is also available during rendering the file contents, as model variable item.

Of course, we might want different value in the file name and its content. For that we can manipulate the loop_overs result:

model:

likes = {
    'Alice': 'Bob',
    'Bob': 'Charlie',
    'Charlie': 'cats',
}

file name: {{loop_over(likes.items())[0]}}

file content: {{item[1]}}

loop_over(likes.items()) produces a key-value tuple for each file, and [0] accesses the key.

Similarly, {{items}} would produce the same key-value pair, and [1] accesses the value.

Alternatively, the model could be a list of complex objects:

model:

likes = [
    {'name': 'Alice', 'likes': 'Bob'},
    {'name': 'Bob', 'likes': 'Charlie'},
    {'name': 'Charlie', 'likes': 'cats'},
]

In this case the file name template simplifies to {{loop_over(likes).name}} and the content template to {{item.likes}}.

Installation

Rybak templates can work with either of Jinja, Mako or Torado; so typically you need to install Rybak and one of those libraries.

installing rybak[jinja] or rybak[mako] will handle this.

Name

'y' is pronounced like in 'sit', 'a' like in 'father', just shorter.

Rybak is a Polish word for fisherman.

The original idea was to only support Mako templates, which is named by a species of a shark.

Functionality of this library is provided by the template engine, so picture a fisherman pulled by a shark.

Release files for rybak 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rybak 0.5.0
File Size Uploaded
rybak-0.5.0.tar.gz 22.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for rybak 0.5.0
File Interpreter ABI Platform
rybak-0.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 31.3 kB

Release files / rybak-0.5.0.tar.gz

Download URL rybak-0.5.0.tar.gz
Size 22.3 kB
Tags Source
SHA-256 checksum
How to use checksums
9733f6dcb1969092b7ceb0d65454d921ff7f33f24f4da5390e0b1674bfbc26bd
BLAKE2b-256 checksum
How to use checksums
8f7681fd4eb5bb606426319e0eb81a48146c6e7848798e696bb33b9dd31bf062
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.4.27

Release files / rybak-0.5.0-py3-none-any.whl

Download URL rybak-0.5.0-py3-none-any.whl
Size 9.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
34d377b01b0e37ade633589526863428782018d8f46100577452f4181583faae
BLAKE2b-256 checksum
How to use checksums
95a6b09acdb6667a9f574a1c124dbecbec1136167ce6fc1bbd1ee3ea08832a7e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.4.27

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page