Skip to content

Commit

Permalink
Refactor Format classes so that the base has no implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
reweeden committed Jun 12, 2024
1 parent cfd909f commit 347c4c7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
37 changes: 23 additions & 14 deletions mandible/metadata_mapper/format/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,30 @@ def __init_subclass__(cls, register: bool = True, **kwargs):
super().__init_subclass__(**kwargs)

# Begin class definition
@abstractmethod
def get_values(
self,
file: IO[bytes],
keys: Iterable[str],
) -> Dict[str, Any]:
"""Get a list of values from a file"""
pass

@abstractmethod
def get_value(self, file: IO[bytes], key: str) -> Any:
"""Convenience function for getting a single value"""
pass


@dataclass
class _SimpleFormat(Format, ABC, register=False):
def get_values(
self,
file: IO[bytes],
keys: Iterable[str],
) -> Dict[str, Any]:
"""Get a list of values from a file"""

with self._parse_data(file) as data:
return {
key: self._eval_key_wrapper(data, key)
Expand Down Expand Up @@ -69,7 +88,7 @@ def _eval_key(data, key: str) -> Any:
# Define placeholders for when extras are not installed

@dataclass
class _PlaceholderBase(Format, register=False):
class _PlaceholderBase(_SimpleFormat, register=False):
"""
Base class for defining placeholder implementations for classes that
require extra dependencies to be installed
Expand Down Expand Up @@ -104,7 +123,7 @@ def __init__(self):
# Define formats that don't require extra dependencies

@dataclass
class Json(Format):
class Json(_SimpleFormat):
@staticmethod
@contextlib.contextmanager
def _parse_data(file: IO[bytes]):
Expand All @@ -128,6 +147,8 @@ def __post_init__(self):
}

def get_values(self, file: IO[bytes], keys: Iterable[str]):
"""Get a list of values from a file"""

with zipfile.ZipFile(file, "r") as zf:
file = self._get_file_from_archive(zf)
return self.format.get_values(file, keys)
Expand Down Expand Up @@ -174,15 +195,3 @@ def _matches_filters(self, zipinfo: zipfile.ZipInfo) -> bool:
return False

return True

@staticmethod
def _parse_data(file: IO[bytes]):
raise NotImplementedError(
f"{Zip.__name__}._parse_data should not be called!"
)

@staticmethod
def _eval_key(data: dict, key: str):
raise NotImplementedError(
f"{Zip.__name__}._eval_key should not be called!"
)
4 changes: 2 additions & 2 deletions mandible/metadata_mapper/format/h5.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

from mandible.h5_parser import normalize

from .format import Format
from .format import _SimpleFormat


@dataclass
class H5(Format):
class H5(_SimpleFormat):
@staticmethod
def _parse_data(file: IO[bytes]) -> ContextManager[Any]:
return h5py.File(file, "r")
Expand Down
4 changes: 2 additions & 2 deletions mandible/metadata_mapper/format/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

from lxml import etree

from .format import Format
from .format import _SimpleFormat


@dataclass
class Xml(Format):
class Xml(_SimpleFormat):
@staticmethod
@contextlib.contextmanager
def _parse_data(file: IO[bytes]) -> Any:
Expand Down

0 comments on commit 347c4c7

Please sign in to comment.