Skip to content

Commit

Permalink
Clean up changed for 6.0
Browse files Browse the repository at this point in the history
New variable strength is now used, and minimum version must be defined
as a string.
  • Loading branch information
spapanik committed Jan 3, 2024
1 parent f76c766 commit b51ad62
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 65 deletions.
1 change: 0 additions & 1 deletion cookbook.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
$globals:
shell: bash
version: "5.2"
new_order: true
vars:
poetry_sync: "--sync"

Expand Down
5 changes: 5 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ The format is based on [Keep a Changelog], and this project adheres to [Semantic

## [Unreleased]

### Changed

- Min version is mandatory and must be a string
- New variable strength is now used

## [5.3.0] - 2023-11-03

### Added
Expand Down
3 changes: 1 addition & 2 deletions docs/usage/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ but they are not targets: Aliases and meta-targets.
A target starting with a single dollar sign is reserved by `yam` itself, for meta-targets.
To use a normal target that starts with a dollar sign, use one more dollar sign, than what
is needed, and `yam` will strip one of them and turn it into a normal target or an alias.
Currently the only meta-target is `$globals`, that has four valid keys:
Currently the only meta-target is `$globals`, that has 3 valid keys:

- `vars`, which is the same as the key with the same name in targets, but weaker
- `shell`, to override the default shell used to execute the commands
- `version`, which is the minimum version of `yam` needed for the cookbook.
- `new_order`, to adopt the new order of variable strength before it becomes default.

### Aliases

Expand Down
36 changes: 15 additions & 21 deletions src/yamk/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ def __init__(
extra: list[str],
original_regex: str | None = None,
*,
new_order: bool,
specified: bool = False,
):
self.new_order = new_order
self.extra = extra
self._specified = specified
self._raw_recipe = raw_recipe
Expand Down Expand Up @@ -116,7 +114,6 @@ def for_target(self, target: str, extra: list[str]) -> Recipe:
self.vars["global"],
self.vars["arg"],
extra,
new_order=self.new_order,
original_regex=self.target,
specified=True,
)
Expand All @@ -126,7 +123,7 @@ def _evaluate(
) -> Any:
if variables is None:
variables = self.vars
flat_vars = flatten_vars(variables, self.base_dir, new_order=self.new_order)
flat_vars = flatten_vars(variables, self.base_dir)
parser = Parser(flat_vars, self.base_dir)
return parser.evaluate(obj)

Expand Down Expand Up @@ -393,23 +390,20 @@ def print(self, cols: int) -> None: # noqa: A003


def flatten_vars(
variables: dict[str, dict[str, Any]], base_dir: Path, *, new_order: bool
variables: dict[str, dict[str, Any]], base_dir: Path
) -> dict[str, Any]:
if new_order:
order = [
"env",
"arg",
"global",
"local",
"global",
"implicit",
"regex",
"local",
"env",
"arg",
]
else:
order = ["env", "global", "regex", "local", "arg", "implicit"]
order = [
"env",
"arg",
"global",
"local",
"global",
"implicit",
"regex",
"local",
"env",
"arg",
]
output: dict[str, Any] = {}
strong_keys: set[str] = set()
for var_type in order:
Expand Down Expand Up @@ -457,5 +451,5 @@ def print_reports(reports: list[CommandReport]) -> None:
report.print(cols)


class RemovedIn60Warning(FutureWarning):
class RemovedIn70Warning(FutureWarning):
pass
38 changes: 1 addition & 37 deletions src/yamk/make.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import re
import subprocess
import sys
import warnings
from time import perf_counter_ns, sleep
from typing import Any, cast

Expand Down Expand Up @@ -116,7 +115,6 @@ def _parse_recipes(self, parsed_cookbook: dict[str, dict[str, Any]]) -> None:
self.globals.get("vars", {}),
self.arg_vars,
extra=[],
new_order=self._is_new_order,
)

if recipe.alias:
Expand Down Expand Up @@ -301,39 +299,5 @@ def _infer_timestamp(self, node: lib.Node) -> float:

return path.stat().st_mtime

@property
def _is_new_order(self) -> bool:
if (raw_order := self.globals.get("new_order")) is None:
warnings.warn(_msg, lib.RemovedIn60Warning, stacklevel=3)
return False
return bool(raw_order)

def _get_version(self) -> Version:
if (manual_version := self.globals.get("version")) is None:
warnings.warn(
"Min cookbook versions will become mandatory.",
lib.RemovedIn60Warning,
stacklevel=3,
)
return Version("4.0.0")
if not isinstance(manual_version, str):
warnings.warn(
"Min cookbook versions will be required to be a string",
lib.RemovedIn60Warning,
stacklevel=3,
)
return Version(str(manual_version))
return Version(manual_version)


_msg = """ The relative strength of variables will change.
Please set new_order to false in your cookbook to keep the old behaviour,
or to new to adopt the new one.
The new order is the following (from weakest to strongest):
- global variables
- implicit variables (e.g. ${.target})
- regex variables (the ones that come from the target name)
- local variables
- environment variables
- command line arguments
"""
return Version(self.globals["version"])
1 change: 0 additions & 1 deletion tests/data/mk.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
["$globals"]
shell = "bash"
version = "5.2"
new_order = true

["$globals".vars]
"[weak]prefix" = "/home/user/.config/"
Expand Down
6 changes: 3 additions & 3 deletions tests/yamk/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@


def test_recipe_to_str() -> None:
recipe = lib.Recipe("target", {}, pathlib.Path(), {}, {}, extra=[], new_order=True)
recipe = lib.Recipe("target", {}, pathlib.Path(), {}, {}, extra=[])
assert str(recipe) == "Generic recipe for target"
recipe = recipe.for_target("target", extra=[])
assert str(recipe) == "Specified recipe for target"


def test_recipe_for_target() -> None:
recipe = lib.Recipe("target", {}, pathlib.Path(), {}, {}, extra=[], new_order=True)
recipe = lib.Recipe("target", {}, pathlib.Path(), {}, {}, extra=[])
recipe_specified = recipe.for_target("target", extra=[])
recipe_specified_again = recipe_specified.for_target("target", extra=[])
assert recipe is not recipe_specified
Expand Down Expand Up @@ -48,7 +48,7 @@ def test_recipe_for_target() -> None:
def test_flatten_vars(
initial: dict[str, dict[str, str]], expected: dict[str, str]
) -> None:
assert lib.flatten_vars(initial, PATH, new_order=True) == expected
assert lib.flatten_vars(initial, PATH) == expected


def test_node_to_str() -> None:
Expand Down

0 comments on commit b51ad62

Please sign in to comment.