Skip to content

Commit

Permalink
Typesafe accessor for Storage
Browse files Browse the repository at this point in the history
  • Loading branch information
danielballan committed Dec 13, 2024
1 parent 9da38f1 commit d838f77
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion tiled/adapters/arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def init_storage(
The list of assets.
"""
data_source = copy.deepcopy(data_source) # Do not mutate caller input.
data_uri = str(storage.filesystem) + "".join(
data_uri = str(storage.get("filesystem")) + "".join(
f"/{quote_plus(segment)}" for segment in path_parts
)
directory = path_from_uri(data_uri)
Expand Down
2 changes: 1 addition & 1 deletion tiled/adapters/awkward_buffers.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def init_storage(
"""
data_source = copy.deepcopy(data_source) # Do not mutate caller input.
data_uri = str(storage.filesystem) + "".join(
data_uri = str(storage.get("filesystem")) + "".join(
f"/{quote_plus(segment)}" for segment in path_parts
)
directory: Path = path_from_uri(data_uri)
Expand Down
2 changes: 1 addition & 1 deletion tiled/adapters/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def init_storage(
"""
data_source = copy.deepcopy(data_source) # Do not mutate caller input.
data_uri = str(storage.filesystem) + "".join(
data_uri = str(storage.get("filesystem")) + "".join(
f"/{quote_plus(segment)}" for segment in path_parts
)
directory = path_from_uri(data_uri)
Expand Down
2 changes: 1 addition & 1 deletion tiled/adapters/parquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def init_storage(
"""
data_source = copy.deepcopy(data_source) # Do not mutate caller input.
data_uri = str(storage.filesystem) + "".join(
data_uri = str(storage.get("filesystem")) + "".join(
f"/{quote_plus(segment)}" for segment in path_parts
)
directory = path_from_uri(data_uri)
Expand Down
2 changes: 1 addition & 1 deletion tiled/adapters/sparse_blocks_parquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def init_storage(
"""
data_source = copy.deepcopy(data_source) # Do not mutate caller input.
data_uri = str(storage.filesystem) + "".join(
data_uri = str(storage.get("filesystem")) + "".join(
f"/{quote_plus(segment)}" for segment in path_parts
)
directory = path_from_uri(data_uri)
Expand Down
3 changes: 2 additions & 1 deletion tiled/adapters/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def init_storage(
) -> DataSource[TableStructure]:
"""
Class to initialize the list of assets for given uri. In SQL Adapter we hve single partition.
Parameters
----------
data_uri : the uri of the data
Expand All @@ -96,7 +97,7 @@ def init_storage(
default_table_name = ... # based on hash of Arrow schema
data_source.parameters.setdefault("table_name", default_table_name)
data_source.parameters["dataset_id"] = uuid.uuid4().int
data_uri = storage.sql # TODO scrub credentials
data_uri = storage.get("sql") # TODO scrub credentials
data_source.assets.append(
Asset(
data_uri=data_uri,
Expand Down
6 changes: 3 additions & 3 deletions tiled/adapters/zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def init_storage(
"""
data_source = copy.deepcopy(data_source) # Do not mutate caller input.
data_uri = str(storage.filesystem) + "".join(
data_uri = str(storage.get("filesystem")) + "".join(
f"/{quote_plus(segment)}" for segment in path_parts
)
# Zarr requires evenly-sized chunks within each dimension.
Expand All @@ -85,9 +85,9 @@ def init_storage(
shape = tuple(dim[0] * len(dim) for dim in data_source.structure.chunks)
directory = path_from_uri(data_uri)
directory.mkdir(parents=True, exist_ok=True)
storage = zarr.storage.DirectoryStore(str(directory))
store = zarr.storage.DirectoryStore(str(directory))
zarr.storage.init_array(
storage,
store,
shape=shape,
chunks=zarr_chunks,
dtype=data_source.structure.data_type.to_numpy_dtype(),
Expand Down
6 changes: 6 additions & 0 deletions tiled/structures/data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,9 @@ def from_json(cls, d):
class Storage:
filesystem: Optional[str]
sql: Optional[str]

def get(self, storage: str) -> str:
uri = getattr(self, storage)
if isinstance(uri, str):
return uri
raise TypeError(f"{storage} is not set")

0 comments on commit d838f77

Please sign in to comment.