Skip to content

Commit 2a9c05a

Browse files
authored
Merge pull request #409 from AGhafaryy/lazyloaded-args
allowing lazy loaded arguments as arguments
2 parents acc6c95 + 75eed37 commit 2a9c05a

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

nodestream/project/storage.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from dataclasses import dataclass, field
2-
from typing import Any, Dict, Optional
2+
from typing import Any, Dict, Optional, Union
3+
4+
from nodestream.file_io import LazyLoadedArgument
35

46
from ..pipeline.object_storage import ObjectStore, Signer
57

@@ -9,14 +11,15 @@ class StoreConfiguration:
911
name: str
1012
storage_type: str
1113
arguments: Dict[str, Any]
12-
hmac_key: Optional[any] = None
14+
hmac_key: Optional[Union[LazyLoadedArgument, str]] = None
1315

1416
def initialize(self) -> ObjectStore:
15-
store = ObjectStore.from_file_arguments(self.storage_type, **self.arguments)
17+
resolved_args = LazyLoadedArgument.resolve_if_needed(self.arguments)
18+
store = ObjectStore.from_file_arguments(self.storage_type, **resolved_args)
1619
if self.hmac_key:
17-
return store.signed(Signer.hmac(self.hmac_key))
18-
else:
19-
return store
20+
resolved_key = LazyLoadedArgument.resolve_if_needed(self.hmac_key)
21+
return store.signed(Signer.hmac(resolved_key))
22+
return store
2023

2124
def to_file_data(self):
2225
return dict(
@@ -37,10 +40,15 @@ def from_file_data(data):
3740

3841
@staticmethod
3942
def describe_yaml_schema():
40-
from schema import Optional, Schema
43+
from schema import Optional, Or, Schema
4144

4245
return Schema(
43-
{"name": str, "type": str, Optional("hmac_key"): str, Optional(str): object}
46+
{
47+
"name": str,
48+
"type": str,
49+
Optional("hmac_key"): Or(LazyLoadedArgument, str, only_one=True),
50+
Optional(str): object,
51+
}
4452
)
4553

4654

tests/unit/project/test_storage.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
import base64
2+
from unittest.mock import Mock
3+
14
import pytest
25
from hamcrest import assert_that, contains, equal_to, instance_of
36

7+
from nodestream.file_io import LazyLoadedArgument
48
from nodestream.pipeline.object_storage import (
59
DirectoryObjectStore,
610
NullObjectStore,
@@ -88,3 +92,24 @@ def test_storage_configuration_to_file_data(store_config):
8892
file_data = storage_config.to_file_data()
8993
expected_data = {"stores": [("test_store", store_config)]}
9094
assert_that(file_data, equal_to(expected_data))
95+
96+
97+
def test_store_configuration_initialize_with_lazy_hmac():
98+
expected_hmac = "dvHdCrVbRPp1HcmWX78Ryw=="
99+
mock_lazy_hmac = Mock(spec=LazyLoadedArgument)
100+
mock_lazy_hmac.get_value.return_value = expected_hmac
101+
expected_arg = "testarglol"
102+
mock_lazy_arg = Mock(spec=LazyLoadedArgument)
103+
mock_lazy_arg.get_value.return_value = expected_arg
104+
105+
store_config = StoreConfiguration(
106+
name="test-store",
107+
storage_type="local",
108+
arguments={"root": mock_lazy_arg},
109+
hmac_key=mock_lazy_hmac,
110+
)
111+
112+
store = store_config.initialize()
113+
assert_that(base64.b64encode(store.signer.key).decode(), equal_to(expected_hmac))
114+
assert_that(store.store.root, equal_to(expected_arg))
115+
mock_lazy_hmac.get_value.assert_called_once()

0 commit comments

Comments
 (0)