Skip to content

Commit e0d37b3

Browse files
committed
run pre-commit
1 parent 3a33fbd commit e0d37b3

File tree

10 files changed

+86
-57
lines changed

10 files changed

+86
-57
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .build_number import update_build_number
2+
from .version import update_version
3+
4+
__all__ = ["update_build_number", "update_version"]

conda_forge_tick/update_recipe/v2/build_number.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import logging
44
from typing import TYPE_CHECKING, Any, Literal
5-
from conda_forge_tick.update_recipe.v2.yaml import _load_yaml, _dump_yaml_to_str
5+
6+
from conda_forge_tick.update_recipe.v2.yaml import _dump_yaml_to_str, _load_yaml
7+
68
if TYPE_CHECKING:
79
from pathlib import Path
810

@@ -11,15 +13,19 @@
1113
HashType = Literal["md5", "sha256"]
1214

1315

14-
def _update_build_number_in_context(recipe: dict[str, Any], new_build_number: int) -> bool:
16+
def _update_build_number_in_context(
17+
recipe: dict[str, Any], new_build_number: int
18+
) -> bool:
1519
for key in recipe.get("context", {}):
1620
if key.startswith("build_") or key == "build":
1721
recipe["context"][key] = new_build_number
1822
return True
1923
return False
2024

2125

22-
def _update_build_number_in_recipe(recipe: dict[str, Any], new_build_number: int) -> bool:
26+
def _update_build_number_in_recipe(
27+
recipe: dict[str, Any], new_build_number: int
28+
) -> bool:
2329
is_modified = False
2430
if "build" in recipe and "number" in recipe["build"]:
2531
recipe["build"]["number"] = new_build_number

conda_forge_tick/update_recipe/v2/context.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import jinja2
22

3-
def load_recipe_context(context: dict[str, str], jinja_env: jinja2.Environment) -> dict[str, str]:
3+
4+
def load_recipe_context(
5+
context: dict[str, str], jinja_env: jinja2.Environment
6+
) -> dict[str, str]:
47
"""
58
Load all string values from the context dictionary as Jinja2 templates.
69
Use linux-64 as default target_platform, build_platform, and mpi.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .jinja import jinja_env
2+
3+
__all__ = ["jinja_env"]

conda_forge_tick/update_recipe/v2/jinja/filters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from conda_forge_tick.jinja.utils import _MissingUndefined
3+
from conda_forge_tick.update_recipe.v2.jinja.utils import _MissingUndefined
44

55

66
def _version_to_build_string(some_string: str | _MissingUndefined) -> str:

conda_forge_tick/update_recipe/v2/jinja/jinja.py

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
from __future__ import annotations
22

3-
from typing import Any, TypedDict
3+
from typing import TypedDict
44

55
import jinja2
6-
import yaml
6+
from jinja2.sandbox import SandboxedEnvironment
77

8-
from conda_forge_tick.jinja.filters import _bool, _split, _version_to_build_string
9-
from conda_forge_tick.jinja.objects import (
8+
from conda_forge_tick.update_recipe.v2.jinja.filters import (
9+
_bool,
10+
_split,
11+
_version_to_build_string,
12+
)
13+
from conda_forge_tick.update_recipe.v2.jinja.objects import (
1014
_stub_compatible_pin,
1115
_stub_is_linux,
1216
_stub_is_unix,
@@ -15,20 +19,21 @@
1519
_stub_subpackage_pin,
1620
_StubEnv,
1721
)
18-
from conda_forge_tick.jinja.utils import _MissingUndefined
19-
from conda_forge_tick.loader import load_yaml
22+
from conda_forge_tick.update_recipe.v2.jinja.utils import _MissingUndefined
23+
24+
# from conda_forge_tick.update_recipe.v2.loader import load_yaml
2025

2126

2227
class RecipeWithContext(TypedDict, total=False):
2328
context: dict[str, str]
2429

2530

26-
def jinja_env() -> jinja2.Environment:
31+
def jinja_env() -> SandboxedEnvironment:
2732
"""
2833
Create a `rattler-build` specific Jinja2 environment with modified syntax.
2934
Target platform, build platform, and mpi are set to linux-64 by default.
3035
"""
31-
env = jinja2.sandbox.SandboxedEnvironment(
36+
env = SandboxedEnvironment(
3237
variable_start_string="${{",
3338
variable_end_string="}}",
3439
trim_blocks=True,
@@ -71,7 +76,9 @@ def jinja_env() -> jinja2.Environment:
7176
return env
7277

7378

74-
def load_recipe_context(context: dict[str, str], jinja_env: jinja2.Environment) -> dict[str, str]:
79+
def load_recipe_context(
80+
context: dict[str, str], jinja_env: jinja2.Environment
81+
) -> dict[str, str]:
7582
"""
7683
Load all string values from the context dictionary as Jinja2 templates.
7784
Use linux-64 as default target_platform, build_platform, and mpi.
@@ -87,30 +94,30 @@ def load_recipe_context(context: dict[str, str], jinja_env: jinja2.Environment)
8794
return context
8895

8996

90-
def render_recipe_with_context(recipe_content: RecipeWithContext) -> dict[str, Any]:
91-
"""
92-
Render the recipe using known values from context section.
93-
Unknown values are not evaluated and are kept as it is.
94-
Target platform, build platform, and mpi are set to linux-64 by default.
95-
96-
Examples:
97-
---
98-
```python
99-
>>> from pathlib import Path
100-
>>> from conda_forge_tick.loader import load_yaml
101-
>>> recipe_content = load_yaml((Path().resolve() / "tests" / "data" / "eval_recipe_using_context.yaml").read_text())
102-
>>> evaluated_context = render_recipe_with_context(recipe_content)
103-
>>> assert "my_value-${{ not_present_value }}" == evaluated_context["build"]["string"]
104-
>>>
105-
```
106-
"""
107-
env = jinja_env()
108-
context = recipe_content.get("context", {})
109-
# render out the context section and retrieve dictionary
110-
context_variables = load_recipe_context(context, env)
111-
112-
# render the rest of the document with the values from the context
113-
# and keep undefined expressions _as is_.
114-
template = env.from_string(yaml.dump(recipe_content))
115-
rendered_content = template.render(context_variables)
116-
return load_yaml(rendered_content)
97+
# def render_recipe_with_context(recipe_content: RecipeWithContext) -> dict[str, Any]:
98+
# """
99+
# Render the recipe using known values from context section.
100+
# Unknown values are not evaluated and are kept as it is.
101+
# Target platform, build platform, and mpi are set to linux-64 by default.
102+
103+
# Examples:
104+
# ---
105+
# ```python
106+
# >>> from pathlib import Path
107+
# >>> from conda_forge_tick.loader import load_yaml
108+
# >>> recipe_content = load_yaml((Path().resolve() / "tests" / "data" / "eval_recipe_using_context.yaml").read_text())
109+
# >>> evaluated_context = render_recipe_with_context(recipe_content)
110+
# >>> assert "my_value-${{ not_present_value }}" == evaluated_context["build"]["string"]
111+
# >>>
112+
# ```
113+
# """
114+
# env = jinja_env()
115+
# context = recipe_content.get("context", {})
116+
# # render out the context section and retrieve dictionary
117+
# context_variables = load_recipe_context(context, env)
118+
119+
# # render the rest of the document with the values from the context
120+
# # and keep undefined expressions _as is_.
121+
# template = env.from_string(yaml.dump(recipe_content))
122+
# rendered_content = template.render(context_variables)
123+
# return load_yaml(rendered_content)

conda_forge_tick/update_recipe/v2/source.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import typing
2-
from typing import Union, List, TypedDict, Any, Iterator, NotRequired, Mapping
3-
from conda_forge_tick.update_recipe.v2.conditional_list import visit_conditional_list, ConditionalList
2+
from typing import Any, Iterator, List, Mapping, NotRequired, TypedDict, Union
3+
4+
from conda_forge_tick.update_recipe.v2.conditional_list import (
5+
ConditionalList,
6+
visit_conditional_list,
7+
)
48

59
OptionalUrlList = Union[str, List[str], None]
610

11+
712
class Source(TypedDict):
813
url: NotRequired[str | list[str]]
914
sha256: NotRequired[str]
@@ -29,8 +34,7 @@ def get_all_sources(recipe: Mapping[Any, Any]) -> Iterator[Source]:
2934
# Try getting all url top-level sources
3035
if sources is not None:
3136
source_list = visit_conditional_list(sources, None)
32-
for source in source_list:
33-
yield source
37+
yield from source_list
3438

3539
outputs = recipe.get("outputs", None)
3640
if outputs is None:
@@ -43,5 +47,4 @@ def get_all_sources(recipe: Mapping[Any, Any]) -> Iterator[Source]:
4347
if sources is None:
4448
continue
4549
source_list = visit_conditional_list(sources, None)
46-
for source in source_list:
47-
yield source
50+
yield from source_list

conda_forge_tick/update_recipe/v2/version.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
import re
77
from typing import TYPE_CHECKING, Literal
88

9+
import requests
10+
911
from conda_forge_tick.update_recipe.v2.context import load_recipe_context
1012
from conda_forge_tick.update_recipe.v2.jinja import jinja_env
1113
from conda_forge_tick.update_recipe.v2.source import Source, get_all_sources
12-
from conda_forge_tick.update_recipe.v2.yaml import _load_yaml, _dump_yaml_to_str
13-
14-
import requests
14+
from conda_forge_tick.update_recipe.v2.yaml import _dump_yaml_to_str, _load_yaml
1515

1616
if TYPE_CHECKING:
1717
from pathlib import Path
@@ -20,6 +20,7 @@
2020

2121
HashType = Literal["md5", "sha256"]
2222

23+
2324
class CouldNotUpdateVersionError(Exception):
2425
NO_CONTEXT = "Could not find context in recipe"
2526
NO_VERSION = "Could not find version in recipe context"
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import io
2-
from ruamel.yaml import YAML
3-
from typing import TYPE_CHECKING
2+
from pathlib import Path
43

5-
if TYPE_CHECKING:
6-
from pathlib import Path
4+
from ruamel.yaml import YAML
75

86
yaml = YAML()
97
yaml.preserve_quotes = True
108
yaml.width = 4096
119
yaml.indent(mapping=2, sequence=4, offset=2)
1210

11+
1312
def _load_yaml(file: Path) -> dict:
1413
"""Load a YAML file."""
1514
with file.open("r") as f:
1615
return yaml.load(f)
1716

17+
1818
def _dump_yaml_to_str(data: dict) -> str:
1919
"""Dump a dictionary to a YAML string."""
2020
with io.StringIO() as f:
2121
yaml.dump(data, f)
22-
return f.getvalue()
22+
return f.getvalue()

tests/test_recipe_editing_v2.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
import pytest
21
from pathlib import Path
32

4-
from conda_forge_tick.recipe_editing_v2 import update_build_number, update_version
3+
import pytest
4+
5+
from conda_forge_tick.update_recipe.v2 import update_build_number, update_version
56

67

78
@pytest.fixture
89
def data_dir() -> Path:
910
return Path(__file__).parent / "recipe_v2"
1011

12+
1113
def test_build_number_mod(data_dir: Path) -> None:
1214
tests = data_dir / "build_number"
1315
result = update_build_number(tests / "test_1/recipe.yaml", 0)

0 commit comments

Comments
 (0)