Skip to content

Commit 7ad4051

Browse files
authored
use jinja2 (#26)
* use jinja2 * use jinja2
1 parent 267fc27 commit 7ad4051

File tree

7 files changed

+438
-296
lines changed

7 files changed

+438
-296
lines changed

.dev/install

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/usr/bin/env bash
22

33
pip install -U pip poetry
4-
poetry install
4+
poetry install --all-extras

metablock/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from .spaces import Block, Service, Space, SpaceExtension
66
from .user import User
77

8-
__version__ = "1.0.2"
8+
__version__ = "1.1.0"
99

1010
__all__ = [
1111
"Metablock",

metablock/cli.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from pathlib import Path
44

55
import click
6+
import jinja2
67
import yaml
78

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

1718

18-
def manifest(file_path: Path) -> dict:
19-
return yaml.safe_load(file_path.read_text())
19+
def manifest(file_path: Path, params: dict) -> str:
20+
env = jinja2.Environment()
21+
template = env.from_string(file_path.read_text())
22+
return template.render(**params)
2023

2124

2225
@click.group()
@@ -39,13 +42,19 @@ def main() -> None:
3942
help="metablock API token",
4043
default=METABLOCK_API_TOKEN,
4144
)
42-
def apply(path: str, space_name: str, token: str) -> None:
45+
@click.option(
46+
"--dry-run",
47+
is_flag=True,
48+
help="Do not apply changes, just show what would be done",
49+
)
50+
def apply(path: str, space_name: str, token: str, dry_run: bool) -> None:
4351
"""Apply metablock manifest to a metablock space"""
44-
asyncio.get_event_loop().run_until_complete(
52+
asyncio.run(
4553
_apply(
4654
path,
4755
space_name or METABLOCK_SPACE,
4856
token or METABLOCK_API_TOKEN,
57+
dry_run=dry_run,
4958
)
5059
)
5160

@@ -84,7 +93,7 @@ def ship(
8493
token: str,
8594
) -> None:
8695
"""Deploy a new version of html block"""
87-
asyncio.get_event_loop().run_until_complete(
96+
asyncio.run(
8897
_ship(
8998
path,
9099
env or METABLOCK_ENV,
@@ -95,7 +104,7 @@ def ship(
95104
)
96105

97106

98-
async def _apply(path: str, space_name: str, token: str) -> None:
107+
async def _apply(path: str, space_name: str, token: str, dry_run: bool) -> None:
99108
if not token:
100109
click.echo("metablock API token is required", err=True)
101110
raise click.Abort()
@@ -106,7 +115,14 @@ async def _apply(path: str, space_name: str, token: str) -> None:
106115
blocks = []
107116
for file_path in p.glob("*.yaml"):
108117
name = file_path.name.split(".")[0]
109-
blocks.append((name, manifest(file_path)))
118+
text = manifest(file_path, dict(space=space_name, block=name))
119+
if dry_run:
120+
click.echo(text)
121+
else:
122+
blocks.append((name, yaml.safe_load(text)))
123+
if not blocks:
124+
click.echo("nothing to do")
125+
raise click.Abort()
110126
async with Metablock(auth_key=token) as mb:
111127
space = await mb.spaces.get(space_name)
112128
svc = await space.blocks.get_list()

poetry.lock

Lines changed: 391 additions & 273 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1-
[tool.poetry]
1+
[project]
22
name = "metablock"
3-
version = "1.0.2"
43
description = "Metablock cloud python client"
5-
authors = ["Luca <[email protected]>"]
4+
version = "1.1.0"
65
license = "BSD"
76
readme = "readme.md"
8-
9-
[tool.poetry.dependencies]
10-
python = ">=3.11"
11-
click = "^8.1.7"
12-
pyyaml = "^6.0.2"
13-
httpx = "^0.28.1"
14-
multidict = "^6.4.3"
7+
authors = [
8+
{ name = "Luca Sbardella", email="[email protected]" }
9+
]
10+
requires-python = ">=3.11"
11+
dependencies = [
12+
"httpx >= 0.28.1",
13+
"multidict >= 6.4.3"
14+
]
15+
16+
[project.optional-dependencies]
17+
cli = [
18+
"click >= 8.1.7",
19+
"pyyaml >= 6.0.2",
20+
"jinja2 >= 3.1.6",
21+
]
1522

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

31-
[tool.poetry.scripts]
38+
[project.scripts]
3239
metablock = "metablock.cli:main"
3340

3441

tests/blocks/test.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
upstream: https://mb.test.test
1+
upstream: https://{{ block }}.test
22
tags:
33
- test
44
routes:
@@ -8,4 +8,5 @@ routes:
88
paths:
99
- /test
1010
tags:
11-
- test
11+
- {{ space }}
12+
- {{ block }}

tests/test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
pytestmark = pytest.mark.asyncio(loop_scope="module")
66

77

8-
def test_cli(cli: Metablock):
8+
async def test_cli(cli: Metablock):
99
assert str(cli) == cli.url
1010

1111

0 commit comments

Comments
 (0)