Coverage for python/lsst/daf/butler/formatters/logs.py: 52%
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# This file is part of daf_butler.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
22__all__ = ("ButlerLogRecordsFormatter", )
24from typing import (
25 Any,
26 Optional,
27 Type,
28)
30from lsst.daf.butler.core.logging import ButlerLogRecords
31from .json import JsonFormatter
34class ButlerLogRecordsFormatter(JsonFormatter):
35 """Read and write log records in JSON format.
37 This is a naive implementation that treats everything as a pydantic.
38 model. In the future this may be changed to be able to read
39 `ButlerLogRecord` one at time from the file and return a subset
40 of records given some filtering parameters.
41 """
43 def _readFile(self, path: str, pytype: Optional[Type[Any]] = None) -> Any:
44 """Read a file from the path in JSON format.
46 Parameters
47 ----------
48 path : `str`
49 Path to use to open JSON format file.
50 pytype : `class`, optional
51 Python type being read. Should be a `ButlerLogRecords` or
52 subclass.
54 Returns
55 -------
56 data : `object`
57 Data as Python object read from JSON file.
59 Notes
60 -----
61 Can read two forms of JSON log file. It can read a full JSON
62 document created from `ButlerLogRecords`, or a stream of standalone
63 JSON documents with a log record per line.
64 """
65 if pytype is None:
66 pytype = ButlerLogRecords
67 elif not issubclass(pytype, ButlerLogRecords):
68 raise RuntimeError(f"Python type {pytype} does not seem to be a ButlerLogRecords type")
70 return pytype.from_file(path)
72 def _fromBytes(self, serializedDataset: bytes, pytype: Optional[Type[Any]] = None) -> Any:
73 """Read the bytes object as a python object.
75 Parameters
76 ----------
77 serializedDataset : `bytes`
78 Bytes object to unserialize.
79 pytype : `class`, optional
80 Python type being read. Should be a `ButlerLogRecords` or
81 subclass.
83 Returns
84 -------
85 inMemoryDataset : `object`
86 The requested data as a Python object or None if the string could
87 not be read.
88 """
89 # Duplicates some of the logic from ButlerLogRecords.from_file
90 if pytype is None: 90 ↛ 91line 90 didn't jump to line 91, because the condition on line 90 was never true
91 pytype = ButlerLogRecords
92 elif not issubclass(pytype, ButlerLogRecords): 92 ↛ 93line 92 didn't jump to line 93, because the condition on line 92 was never true
93 raise RuntimeError(f"Python type {pytype} does not seem to be a ButlerLogRecords type")
95 return pytype.from_raw(serializedDataset)
97 def _toBytes(self, inMemoryDataset: Any) -> bytes:
98 """Write the in memory dataset to a bytestring.
100 Parameters
101 ----------
102 inMemoryDataset : `object`
103 Object to serialize
105 Returns
106 -------
107 serializedDataset : `bytes`
108 bytes representing the serialized dataset.
110 Raises
111 ------
112 Exception
113 The object could not be serialized.
114 """
115 return inMemoryDataset.json(exclude_unset=True, exclude_defaults=True).encode()