-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from nicholasjng/shelf-context
Unify serde interface by introducing `Shelf.Context`
- Loading branch information
Showing
9 changed files
with
207 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ | |
|
||
from .core import Shelf | ||
from .registry import deregister_type, lookup, register_type | ||
from .types import Context |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from __future__ import annotations | ||
|
||
from collections import deque | ||
from dataclasses import dataclass | ||
from os import PathLike | ||
from pathlib import Path | ||
from typing import IO, Any, Callable, Generic, Literal, TypeVar | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
# TODO: Consider splitting these into ReadContext and WriteContext | ||
class Context: | ||
def __init__(self, tmpdir: str | PathLike[str], filenames: list[str] | None = None): | ||
self._tmpdir = Path(tmpdir) | ||
self._fds: deque[IO] = deque() | ||
self._filenames: list[str] = filenames or [] | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
# Unwind the file queue by closing | ||
while self._fds: | ||
file = self._fds.pop() | ||
file.close() | ||
|
||
@property | ||
def files(self): | ||
return self._fds | ||
|
||
@property | ||
def filenames(self): | ||
return self._filenames | ||
|
||
@property | ||
def tmpdir(self): | ||
return self._tmpdir | ||
|
||
def directory(self, name: str) -> PathLike[str]: | ||
return self.tmpdir / name | ||
|
||
def file(self, name: str, **openkwargs: Any) -> IO: | ||
# TODO: Assert name is not absolute | ||
fp = self.tmpdir / name | ||
desc = open(fp, **openkwargs) | ||
self._fds.append(desc) | ||
self._filenames.append(str(fp)) | ||
return desc | ||
|
||
|
||
@dataclass(frozen=True) | ||
class IOPair(Generic[T]): | ||
serializer: Callable[[T, Context], None] | ||
deserializer: Callable[[Context], T] | ||
|
||
|
||
@dataclass(frozen=True) | ||
class CacheOptions: | ||
directory: PathLike[str] | ||
type: Literal["blockcache", "filecache", "simplecache"] = "filecache" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,41 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
from typing import Any | ||
|
||
from fsspec import AbstractFileSystem, filesystem | ||
from fsspec.utils import get_protocol, stringify_path | ||
|
||
from shelf.types import CacheOptions | ||
|
||
|
||
def filesystem_from_uri( | ||
uri: str, | ||
cache_options: CacheOptions | None = None, | ||
storage_options: dict[str, Any] | None = None, | ||
) -> AbstractFileSystem: | ||
protocol = get_protocol(uri) | ||
storage_options = storage_options or {} | ||
if cache_options is not None: | ||
protocol = cache_options.type | ||
kwargs = { | ||
"target_protocol": cache_options.type, | ||
"target_options": storage_options, | ||
"cache_storage": cache_options.directory, | ||
} | ||
else: | ||
kwargs = storage_options | ||
|
||
def is_fully_qualified(path: str) -> bool: | ||
fs: AbstractFileSystem = filesystem(protocol, **kwargs) | ||
return fs | ||
|
||
|
||
def is_fully_qualified(path: str | os.PathLike[str]) -> bool: | ||
path = stringify_path(path) | ||
protocol = get_protocol(path) | ||
return any(path.startswith(protocol + sep) for sep in ("::", "://")) | ||
|
||
|
||
def with_trailing_sep(path: str) -> str: | ||
def with_trailing_sep(path: str | os.PathLike[str]) -> str: | ||
path = stringify_path(path) | ||
return path if path.endswith(os.sep) else path + os.sep |
Oops, something went wrong.