Skip to content

Commit 64fc233

Browse files
committed
Remove shortuuid and default branch value
1 parent a789f2b commit 64fc233

File tree

9 files changed

+51
-45
lines changed

9 files changed

+51
-45
lines changed

infrahub_sdk/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def _initialize(self) -> None:
281281
self.schema = InfrahubSchema(self)
282282
self.branch = InfrahubBranchManager(self)
283283
self.object_store = ObjectStore(self)
284-
self.store = NodeStore()
284+
self.store = NodeStore(default_branch=self.default_branch)
285285
self.task = InfrahubTaskManager(self)
286286
self.concurrent_execution_limit = asyncio.Semaphore(self.max_concurrent_execution)
287287
self._request_method: AsyncRequester = self.config.requester or self._default_request_method
@@ -1529,7 +1529,7 @@ def _initialize(self) -> None:
15291529
self.schema = InfrahubSchemaSync(self)
15301530
self.branch = InfrahubBranchManagerSync(self)
15311531
self.object_store = ObjectStoreSync(self)
1532-
self.store = NodeStoreSync()
1532+
self.store = NodeStoreSync(default_branch=self.default_branch)
15331533
self.task = InfrahubTaskManagerSync(self)
15341534
self._request_method: SyncRequester = self.config.sync_requester or self._default_request_method
15351535
self.group_context = InfrahubGroupContextSync(self)

infrahub_sdk/exceptions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ def __init__(self, message: str | None = None):
6969
class NodeNotFoundError(Error):
7070
def __init__(
7171
self,
72-
node_type: str,
7372
identifier: Mapping[str, list[str]],
7473
message: str = "Unable to find the node in the database.",
7574
branch_name: str | None = None,
75+
node_type: str | None = None,
7676
):
77-
self.node_type = node_type
77+
self.node_type = node_type or "unknown"
7878
self.identifier = identifier
7979
self.branch_name = branch_name
8080

infrahub_sdk/node.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
from copy import copy
77
from typing import TYPE_CHECKING, Any, Callable, Union, get_args
88

9-
import shortuuid
10-
119
from .constants import InfrahubClientMode
1210
from .exceptions import (
1311
Error,
@@ -17,7 +15,7 @@
1715
)
1816
from .graphql import Mutation, Query
1917
from .schema import GenericSchemaAPI, RelationshipCardinality, RelationshipKind
20-
from .utils import compare_lists, get_flat_value
18+
from .utils import compare_lists, generate_short_id, get_flat_value
2119
from .uuidt import UUIDT
2220

2321
if TYPE_CHECKING:
@@ -693,7 +691,7 @@ def __init__(self, schema: MainSchemaTypesAPI, branch: str, data: dict | None =
693691
# Generate a unique ID only to be used inside the SDK
694692
# The format if this ID is purposely different from the ID used by the API
695693
# This is done to avoid confusion and potential conflicts between the IDs
696-
self._internal_id = shortuuid.uuid()
694+
self._internal_id = generate_short_id()
697695

698696
self.id = data.get("id", None) if isinstance(data, dict) else None
699697
self.display_label: str | None = data.get("display_label", None) if isinstance(data, dict) else None

infrahub_sdk/store.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import warnings
4-
from collections import defaultdict
54
from typing import TYPE_CHECKING, Literal, overload
65

76
from .exceptions import NodeInvalidError, NodeNotFoundError
@@ -28,7 +27,7 @@ def __init__(self, name: str) -> None:
2827
self.branch_name = name
2928

3029
self._objs: dict[str, InfrahubNode | InfrahubNodeSync | CoreNode | CoreNodeSync] = {}
31-
self._hfids: dict[str, dict[tuple, str]] = defaultdict(dict)
30+
self._hfids: dict[str, dict[tuple, str]] = {}
3231
self._keys: dict[str, str] = {}
3332
self._uuids: dict[str, str] = {}
3433

@@ -45,9 +44,12 @@ def set(self, node: InfrahubNode | InfrahubNodeSync | CoreNode | CoreNodeSync, k
4544
self._uuids[node.id] = node._internal_id
4645

4746
if hfid := node.get_human_friendly_id():
48-
self._hfids[node.get_kind()][tuple(hfid)] = node._internal_id
47+
for kind in node.get_all_kinds():
48+
if kind not in self._hfids:
49+
self._hfids[kind] = {}
50+
self._hfids[kind][tuple(hfid)] = node._internal_id
4951

50-
def get( # type: ignore[no-untyped-def]
52+
def get(
5153
self,
5254
key: str,
5355
kind: type[SchemaType | SchemaTypeSync] | str | None = None,
@@ -88,13 +90,11 @@ def get( # type: ignore[no-untyped-def]
8890

8991
if kind and found_invalid:
9092
raise NodeInvalidError(
91-
node_type="n/a",
9293
identifier={"key": [key]},
93-
message=f"Found a node of a differentkind instead of {kind} for key {key!r} in the store ({self.branch_name})",
94+
message=f"Found a node of a different kind instead of {kind} for key {key!r} in the store ({self.branch_name})",
9495
)
9596

9697
raise NodeNotFoundError(
97-
node_type="n/a",
9898
identifier={"key": [key]},
9999
message=f"Unable to find the node {key!r} in the store ({self.branch_name})",
100100
)
@@ -104,7 +104,6 @@ def _get_by_internal_id(
104104
) -> InfrahubNode | InfrahubNodeSync | CoreNode | CoreNodeSync:
105105
if internal_id not in self._objs:
106106
raise NodeNotFoundError(
107-
node_type="n/a",
108107
identifier={"internal_id": [internal_id]},
109108
message=f"Unable to find the node {internal_id!r} in the store ({self.branch_name})",
110109
)
@@ -124,7 +123,6 @@ def _get_by_key(
124123
) -> InfrahubNode | InfrahubNodeSync | CoreNode | CoreNodeSync:
125124
if key not in self._keys:
126125
raise NodeNotFoundError(
127-
node_type="n/a",
128126
identifier={"key": [key]},
129127
message=f"Unable to find the node {key!r} in the store ({self.branch_name})",
130128
)
@@ -143,7 +141,6 @@ def _get_by_key(
143141
def _get_by_id(self, id: str, kind: str | None = None) -> InfrahubNode | InfrahubNodeSync | CoreNode | CoreNodeSync:
144142
if id not in self._uuids:
145143
raise NodeNotFoundError(
146-
node_type="n/a",
147144
identifier={"id": [id]},
148145
message=f"Unable to find the node {id!r} in the store ({self.branch_name})",
149146
)
@@ -170,7 +167,7 @@ def _get_by_hfid(
170167
node_hfid = [hfid]
171168

172169
exception_to_raise_if_not_found = NodeNotFoundError(
173-
node_type=node_kind or "unknown",
170+
node_type=node_kind,
174171
identifier={"hfid": node_hfid},
175172
message=f"Unable to find the node {hfid!r} in the store ({self.branch_name})",
176173
)
@@ -192,17 +189,27 @@ class NodeStoreBase:
192189
we need to save them in order to reuse them later to associate them with another node for example.
193190
"""
194191

195-
def __init__(self, default_branch: str = "main") -> None:
192+
def __init__(self, default_branch: str | None = None) -> None:
196193
self._branches: dict[str, NodeStoreBranch] = {}
197194
self._default_branch = default_branch
198195

196+
def _get_branch(self, branch: str | None = None) -> str:
197+
branch = branch or self._default_branch
198+
199+
if branch is None:
200+
raise ValueError(
201+
"A Branch must be provided to use the store, either as a parameter or by setting the default branch on the store"
202+
)
203+
204+
return branch
205+
199206
def _set(
200207
self,
201208
node: InfrahubNode | InfrahubNodeSync | SchemaType | SchemaTypeSync,
202209
key: str | None = None,
203210
branch: str | None = None,
204211
) -> None:
205-
branch = branch or node.get_branch() or self._default_branch
212+
branch = self._get_branch(branch or node.get_branch())
206213

207214
if branch not in self._branches:
208215
self._branches[branch] = NodeStoreBranch(name=branch)
@@ -216,15 +223,15 @@ def _get( # type: ignore[no-untyped-def]
216223
raise_when_missing: bool = True,
217224
branch: str | None = None,
218225
):
219-
branch = branch or self._default_branch
226+
branch = self._get_branch(branch)
220227

221228
if branch not in self._branches:
222229
self._branches[branch] = NodeStoreBranch(name=branch)
223230

224231
return self._branches[branch].get(key=key, kind=kind, raise_when_missing=raise_when_missing)
225232

226233
def count(self, branch: str | None = None) -> int:
227-
branch = branch or self._default_branch
234+
branch = self._get_branch(branch)
228235

229236
if branch not in self._branches:
230237
return 0

infrahub_sdk/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from __future__ import annotations
22

3+
import base64
34
import hashlib
45
import json
6+
import uuid
57
from itertools import groupby
68
from pathlib import Path
79
from typing import TYPE_CHECKING, Any
@@ -25,6 +27,11 @@
2527
from whenever import TimeDelta
2628

2729

30+
def generate_short_id() -> str:
31+
"""Generate a short unique ID"""
32+
return base64.urlsafe_b64encode(uuid.uuid4().bytes).rstrip(b"=").decode("ascii").lower()
33+
34+
2835
def base36encode(number: int) -> str:
2936
if not isinstance(number, (int)):
3037
raise TypeError("number must be an integer")

poetry.lock

Lines changed: 1 addition & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ pyyaml = { version = "^6", optional = true }
4343
eval-type-backport = { version = "^0.2.2", python = "~3.9" }
4444
dulwich = "^0.21.4"
4545
whenever = "0.7.2"
46-
shortuuid = "^1.0.13"
4746

4847
[tool.poetry.group.dev.dependencies]
4948
pytest = "*"

tests/unit/sdk/test_store.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
def test_node_store_set(client_type, clients, schema_with_hfid):
1212
if client_type == "standard":
1313
client = clients.standard
14-
store = NodeStore()
14+
store = NodeStore(default_branch="main")
1515
node_class = InfrahubNode
1616
else:
1717
client = clients.sync
18-
store = NodeStoreSync()
18+
store = NodeStoreSync(default_branch="main")
1919
node_class = InfrahubNodeSync
2020

2121
data = {
@@ -36,11 +36,11 @@ def test_node_store_set(client_type, clients, schema_with_hfid):
3636
def test_node_store_set_no_hfid(client_type, clients, location_schema):
3737
if client_type == "standard":
3838
client = clients.standard
39-
store = NodeStore()
39+
store = NodeStore(default_branch="main")
4040
node_class = InfrahubNode
4141
else:
4242
client = clients.sync
43-
store = NodeStoreSync()
43+
store = NodeStoreSync(default_branch="main")
4444
node_class = InfrahubNodeSync
4545

4646
data = {
@@ -70,11 +70,11 @@ def test_node_store_set_no_hfid(client_type, clients, location_schema):
7070
def test_node_store_get(client_type, clients, location_schema):
7171
if client_type == "standard":
7272
client = clients.standard
73-
store = NodeStore()
73+
store = NodeStore(default_branch="main")
7474
node_class = InfrahubNode
7575
else:
7676
client = clients.sync
77-
store = NodeStoreSync()
77+
store = NodeStoreSync(default_branch="main")
7878
node_class = InfrahubNodeSync
7979

8080
data = {
@@ -114,11 +114,11 @@ def test_node_store_get(client_type, clients, location_schema):
114114
def test_node_store_get_with_hfid(client_type, clients, schema_with_hfid):
115115
if client_type == "standard":
116116
client = clients.standard
117-
store = NodeStore()
117+
store = NodeStore(default_branch="main")
118118
node_class = InfrahubNode
119119
else:
120120
client = clients.sync
121-
store = NodeStoreSync()
121+
store = NodeStoreSync(default_branch="main")
122122
node_class = InfrahubNodeSync
123123

124124
data = {

tests/unit/sdk/test_utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
dict_hash,
1919
duplicates,
2020
extract_fields,
21+
generate_short_id,
2122
get_flat_value,
2223
is_valid_url,
2324
is_valid_uuid,
@@ -26,6 +27,12 @@
2627
)
2728

2829

30+
def test_generate_short_id():
31+
assert len(generate_short_id()) == 22
32+
assert isinstance(generate_short_id(), str)
33+
assert generate_short_id() != generate_short_id()
34+
35+
2936
def test_is_valid_uuid():
3037
assert is_valid_uuid(uuid.uuid4()) is True
3138
assert is_valid_uuid(uuid.UUID("ba0aecd9-546a-4d77-9187-23e17a20633e")) is True

0 commit comments

Comments
 (0)