Skip to content

Commit a00e671

Browse files
committed
tools/update: add a typed inventory
Signed-off-by: Olivier Hoareau <olivier.hoareau@vates.tech>
1 parent 76e5037 commit a00e671

1 file changed

Lines changed: 18 additions & 12 deletions

File tree

lib/tools/inventory.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
1-
"""Inventory for tools scripts.
2-
"""
1+
"""Inventory for tools scripts."""
2+
33
from __future__ import annotations
44

55
import tomllib
66
from pathlib import Path
77

88
from lib.common import HostAddress
99

10-
def load_inventory(inventory_path: Path) -> dict[str, dict[str, list[str]]]:
10+
from typing import TypeAlias, TypedDict
11+
12+
class Server(TypedDict):
13+
enablerepos: list[str]
14+
15+
16+
Inventory: TypeAlias = dict[HostAddress, Server]
17+
18+
19+
def load_inventory(inventory_path: Path) -> Inventory:
1120
"""Create an inventory object from loaded inventory file."""
12-
inventory: dict[str, dict[str, list[str]]] = {}
21+
inventory: Inventory = {}
1322

1423
with open(inventory_path, "rb") as f:
1524
data = tomllib.load(f)
@@ -19,24 +28,21 @@ def load_inventory(inventory_path: Path) -> dict[str, dict[str, list[str]]]:
1928

2029
for server, config in servers.items():
2130
repos = config.get("enablerepos", [])
22-
host = {
23-
"enablerepos": repos or all.get("enablerepos", [])
24-
}
31+
host: Server = {"enablerepos": repos or all.get("enablerepos", [])}
2532
inventory[server] = host
2633

2734
return inventory
2835

29-
def into_inventory(hosts: list[HostAddress], enablerepos: list[str]) -> dict[HostAddress, dict[str, list[str]]]:
36+
37+
def into_inventory(hosts: list[HostAddress], enablerepos: list[str]) -> Inventory:
3038
"""Create an inventory object from arguments.
3139
3240
Basically, it is used as compatibility when we don't want inventory from file.
3341
"""
34-
inventory: dict[HostAddress, dict[str, list[str]]] = {}
42+
inventory: Inventory = {}
3543

3644
for h in hosts:
37-
host = {
38-
"enablerepos": enablerepos or [],
39-
}
45+
host: Server = {"enablerepos": enablerepos or []}
4046
inventory[h] = host
4147

4248
return inventory

0 commit comments

Comments
 (0)