Skip to content

use jinja2 #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .dev/install
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash

pip install -U pip poetry
poetry install
poetry install --all-extras
2 changes: 1 addition & 1 deletion metablock/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .spaces import Block, Service, Space, SpaceExtension
from .user import User

__version__ = "1.0.2"
__version__ = "1.1.0"

__all__ = [
"Metablock",
Expand Down
30 changes: 23 additions & 7 deletions metablock/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pathlib import Path

import click
import jinja2
import yaml

from metablock import Metablock, __version__
Expand All @@ -15,8 +16,10 @@
METABLOCK_API_TOKEN = os.environ.get("METABLOCK_API_TOKEN", "")


def manifest(file_path: Path) -> dict:
return yaml.safe_load(file_path.read_text())
def manifest(file_path: Path, params: dict) -> str:
env = jinja2.Environment()
template = env.from_string(file_path.read_text())
return template.render(**params)


@click.group()
Expand All @@ -39,13 +42,19 @@ def main() -> None:
help="metablock API token",
default=METABLOCK_API_TOKEN,
)
def apply(path: str, space_name: str, token: str) -> None:
@click.option(
"--dry-run",
is_flag=True,
help="Do not apply changes, just show what would be done",
)
def apply(path: str, space_name: str, token: str, dry_run: bool) -> None:
"""Apply metablock manifest to a metablock space"""
asyncio.get_event_loop().run_until_complete(
asyncio.run(
_apply(
path,
space_name or METABLOCK_SPACE,
token or METABLOCK_API_TOKEN,
dry_run=dry_run,
)
)

Expand Down Expand Up @@ -84,7 +93,7 @@ def ship(
token: str,
) -> None:
"""Deploy a new version of html block"""
asyncio.get_event_loop().run_until_complete(
asyncio.run(
_ship(
path,
env or METABLOCK_ENV,
Expand All @@ -95,7 +104,7 @@ def ship(
)


async def _apply(path: str, space_name: str, token: str) -> None:
async def _apply(path: str, space_name: str, token: str, dry_run: bool) -> None:
if not token:
click.echo("metablock API token is required", err=True)
raise click.Abort()
Expand All @@ -106,7 +115,14 @@ async def _apply(path: str, space_name: str, token: str) -> None:
blocks = []
for file_path in p.glob("*.yaml"):
name = file_path.name.split(".")[0]
blocks.append((name, manifest(file_path)))
text = manifest(file_path, dict(space=space_name, block=name))
if dry_run:
click.echo(text)
else:
blocks.append((name, yaml.safe_load(text)))
if not blocks:
click.echo("nothing to do")
raise click.Abort()
async with Metablock(auth_key=token) as mb:
space = await mb.spaces.get(space_name)
svc = await space.blocks.get_list()
Expand Down
664 changes: 391 additions & 273 deletions poetry.lock

Large diffs are not rendered by default.

29 changes: 18 additions & 11 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
[tool.poetry]
[project]
name = "metablock"
version = "1.0.2"
description = "Metablock cloud python client"
authors = ["Luca <[email protected]>"]
version = "1.1.0"
license = "BSD"
readme = "readme.md"

[tool.poetry.dependencies]
python = ">=3.11"
click = "^8.1.7"
pyyaml = "^6.0.2"
httpx = "^0.28.1"
multidict = "^6.4.3"
authors = [
{ name = "Luca Sbardella", email="[email protected]" }
]
requires-python = ">=3.11"
dependencies = [
"httpx >= 0.28.1",
"multidict >= 6.4.3"
]

[project.optional-dependencies]
cli = [
"click >= 8.1.7",
"pyyaml >= 6.0.2",
"jinja2 >= 3.1.6",
]

[tool.poetry.group.dev.dependencies]
pytest-asyncio = "^0.26.0"
Expand All @@ -28,7 +35,7 @@ ruff = "^0.11.5"
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.poetry.scripts]
[project.scripts]
metablock = "metablock.cli:main"


Expand Down
5 changes: 3 additions & 2 deletions tests/blocks/test.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
upstream: https://mb.test.test
upstream: https://{{ block }}.test
tags:
- test
routes:
Expand All @@ -8,4 +8,5 @@ routes:
paths:
- /test
tags:
- test
- {{ space }}
- {{ block }}
2 changes: 1 addition & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
pytestmark = pytest.mark.asyncio(loop_scope="module")


def test_cli(cli: Metablock):
async def test_cli(cli: Metablock):
assert str(cli) == cli.url


Expand Down