Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for reading and writing data as simple "bytes" #570

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions tiled/adapters/bytes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from ..structures.core import StructureFamily


class BytesAdapter:
"""TBD"""

structure_family = StructureFamily.bytes

def __init__(
self,
buffer: bytes,
*,
structure=None,
metadata=None,
specs=None,
access_policy=None,
):
self._buffer = buffer
self._structure = structure
self._metadata = metadata or {}
self.specs = specs or []

@classmethod
def from_datasource(
cls,
datasource,
*,
structure=None,
metadata=None,
specs=None,
access_policy=None,
):
assets = datasource.assets
assert assets
if len(assets) > 1:
buffer = cls._combine_asset_bytes(assets)
else:
buffer = cls._asset_bytes(assets[0])

return cls(
buffer,
structure=structure,
metadata=metadata,
specs=specs,
access_policy=access_policy,
)

@classmethod
def from_asset(
cls,
asset,
*,
metadata=None,
specs=None,
access_policy=None,
):
# TODO: Do we need this constructor?
asset_uri = asset.data_uri
raise NotImplementedError

def __repr__(self):
return f"{type(self).__name__}({self._buffer!r})"

def metadata(self):
return self._metadata

def structure(self):
return self._structure

def read(self, slice=None):
# TODO: `slice` parameter is ignored for now

# TODO: Do we open a file here or offer a classmethod constructor for assets?
return self._buffer

def write(self, data):
raise NotImplementedError
return self._buffer
1 change: 1 addition & 0 deletions tiled/structures/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class StructureFamily(str, enum.Enum):
array = "array"
sparse = "sparse"
table = "table"
bytes = "bytes"


@dataclass(frozen=True)
Expand Down
Loading