Skip to content

Commit

Permalink
Merge pull request #425 from lsst/tickets/DM-26658
Browse files Browse the repository at this point in the history
DM-26658: Use FormatterV2 for configs
  • Loading branch information
timj authored Jul 25, 2024
2 parents 0272154 + 759a513 commit db3495b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 43 deletions.
53 changes: 12 additions & 41 deletions python/lsst/pipe/base/formatters/pexConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,55 +29,26 @@

__all__ = ("PexConfigFormatter",)

import os.path
from typing import Any
from typing import Any, BinaryIO

from lsst.daf.butler.formatters.file import FileFormatter
from lsst.daf.butler import FormatterV2
from lsst.pex.config import Config
from lsst.resources import ResourceHandleProtocol, ResourcePath


class PexConfigFormatter(FileFormatter):
class PexConfigFormatter(FormatterV2):
"""Formatter implementation for reading and writing
`lsst.pex.config.Config` instances.
"""

extension = ".py"

def _readFile(self, path: str, pytype: type[Any] | None = None) -> Any:
"""Read a pex.config.Config instance from the given file.
Parameters
----------
path : `str`
Path to use to open the file.
pytype : `type`, optional
Class to use to read the config file.
Returns
-------
data : `lsst.pex.config.Config`
Instance of class ``pytype`` read from config file. `None`
if the file could not be opened.
"""
if not os.path.exists(path):
return None
default_extension = ".py"
can_read_from_stream = True

def read_from_stream(
self, stream: BinaryIO | ResourceHandleProtocol, component: str | None = None, expected_size: int = -1
) -> Any:
# Automatically determine the Config class from the serialized form
with open(path, "r") as fd:
config_py = fd.read()
return Config._fromPython(config_py)

def _writeFile(self, inMemoryDataset: Any) -> None:
"""Write the in memory dataset to file on disk.
Parameters
----------
inMemoryDataset : `object`
Object to serialize.
return Config._fromPython(stream.read().decode())

Raises
------
Exception
The file could not be written.
"""
inMemoryDataset.save(self.fileDescriptor.location.path)
def write_local_file(self, in_memory_dataset: Any, uri: ResourcePath) -> None:
in_memory_dataset.save(uri.path)
5 changes: 3 additions & 2 deletions python/lsst/pipe/base/tests/mocks/_storage_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
DatasetType,
Formatter,
FormatterFactory,
FormatterV2,
LookupKey,
SerializedDatasetType,
StorageClass,
Expand Down Expand Up @@ -606,7 +607,7 @@ def new_get_storage_class(self: StorageClassFactory, storageClassName: str) -> S

def new_get_formatter_class_with_match(
self: FormatterFactory, entity: Any
) -> tuple[LookupKey, type[Formatter], dict[str, Any]]:
) -> tuple[LookupKey, type[Formatter | FormatterV2], dict[str, Any]]:
try:
return original_get_formatter_class_with_match(self, entity)
except KeyError:
Expand All @@ -629,7 +630,7 @@ def new_get_formatter_class_with_match(

def new_get_formatter_with_match(
self: FormatterFactory, entity: Any, *args: Any, **kwargs: Any
) -> tuple[LookupKey, Formatter]:
) -> tuple[LookupKey, Formatter | FormatterV2]:
try:
return original_get_formatter_with_match(self, entity, *args, **kwargs)
except KeyError:
Expand Down

0 comments on commit db3495b

Please sign in to comment.