Coverage for python / lsst / images / json / formatters.py: 0%

23 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-30 09:06 +0000

1# This file is part of lsst-images. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# Use of this source code is governed by a 3-clause BSD-style 

10# license that can be found in the LICENSE file. 

11 

12from __future__ import annotations 

13 

14__all__ = ("GenericFormatter",) 

15 

16from typing import Any, ClassVar 

17 

18from lsst.daf.butler import DatasetProvenance, FormatterV2 

19from lsst.resources import ResourcePath 

20 

21from ..serialization import ButlerInfo 

22from ._input_archive import read 

23from ._output_archive import write 

24 

25 

26class GenericFormatter(FormatterV2): 

27 """The butler interface to JSON archive serialization. 

28 

29 Serialized types must meet all the requirements of the `.read` and 

30 `.write` functions. 

31 

32 Notes 

33 ----- 

34 This formatter does not support any parameters or components. 

35 """ 

36 

37 default_extension: ClassVar[str] = ".json" 

38 can_read_from_uri: ClassVar[bool] = True 

39 unsupported_parameters: ClassVar[None] = None 

40 butler_provenance: DatasetProvenance | None = None 

41 

42 def read_from_uri(self, uri: ResourcePath, component: str | None = None, expected_size: int = -1) -> Any: 

43 pytype = self.dataset_ref.datasetType.storageClass.pytype 

44 kwargs = self.file_descriptor.parameters or {} 

45 return read(pytype, uri, **kwargs).deserialized 

46 

47 def write_local_file(self, in_memory_dataset: Any, uri: ResourcePath) -> None: 

48 butler_info = ButlerInfo( 

49 dataset=self.dataset_ref.to_simple(), 

50 provenance=self.butler_provenance if self.butler_provenance is not None else DatasetProvenance(), 

51 ) 

52 write(in_memory_dataset, uri.ospath, butler_info=butler_info) 

53 

54 def add_provenance( 

55 self, in_memory_dataset: Any, /, *, provenance: DatasetProvenance | None = None 

56 ) -> Any: 

57 # Instead of attaching the provenance to the object we remember it on 

58 # the formatter, since a Formatter instance is only used once. 

59 self.butler_provenance = provenance 

60 return in_memory_dataset