Coverage for python/lsst/daf/butler/formatters/logs.py: 52%
19 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-18 09:54 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-18 09:54 +0000
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 software is dual licensed under the GNU General Public License and also
10# under a 3-clause BSD license. Recipients may choose which of these licenses
11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
12# respectively. If you choose the GPL option then the following text applies
13# (but note that there is still no warranty even if you opt for BSD instead):
14#
15# This program is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 3 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program. If not, see <http://www.gnu.org/licenses/>.
28__all__ = ("ButlerLogRecordsFormatter",)
30from typing import Any
32from lsst.daf.butler.logging import ButlerLogRecords
34from .json import JsonFormatter
37class ButlerLogRecordsFormatter(JsonFormatter):
38 """Read and write log records in JSON format.
40 This is a naive implementation that treats everything as a pydantic.
41 model. In the future this may be changed to be able to read
42 `ButlerLogRecord` one at time from the file and return a subset
43 of records given some filtering parameters.
44 """
46 def _readFile(self, path: str, pytype: type[Any] | None = None) -> Any:
47 """Read a file from the path in JSON format.
49 Parameters
50 ----------
51 path : `str`
52 Path to use to open JSON format file.
53 pytype : `class`, optional
54 Python type being read. Should be a `ButlerLogRecords` or
55 subclass.
57 Returns
58 -------
59 data : `object`
60 Data as Python object read from JSON file.
62 Notes
63 -----
64 Can read two forms of JSON log file. It can read a full JSON
65 document created from `ButlerLogRecords`, or a stream of standalone
66 JSON documents with a log record per line.
67 """
68 if pytype is None:
69 pytype = ButlerLogRecords
70 elif not issubclass(pytype, ButlerLogRecords):
71 raise RuntimeError(f"Python type {pytype} does not seem to be a ButlerLogRecords type")
73 return pytype.from_file(path)
75 def _fromBytes(self, serializedDataset: bytes, pytype: type[Any] | None = None) -> Any:
76 """Read the bytes object as a python object.
78 Parameters
79 ----------
80 serializedDataset : `bytes`
81 Bytes object to unserialize.
82 pytype : `class`, optional
83 Python type being read. Should be a `ButlerLogRecords` or
84 subclass.
86 Returns
87 -------
88 inMemoryDataset : `object`
89 The requested data as a Python object or None if the string could
90 not be read.
91 """
92 # Duplicates some of the logic from ButlerLogRecords.from_file
93 if pytype is None: 93 ↛ 94line 93 didn't jump to line 94, because the condition on line 93 was never true
94 pytype = ButlerLogRecords
95 elif not issubclass(pytype, ButlerLogRecords): 95 ↛ 96line 95 didn't jump to line 96, because the condition on line 95 was never true
96 raise RuntimeError(f"Python type {pytype} does not seem to be a ButlerLogRecords type")
98 return pytype.from_raw(serializedDataset)
100 def _toBytes(self, inMemoryDataset: Any) -> bytes:
101 """Write the in memory dataset to a bytestring.
103 Parameters
104 ----------
105 inMemoryDataset : `object`
106 Object to serialize
108 Returns
109 -------
110 serializedDataset : `bytes`
111 bytes representing the serialized dataset.
113 Raises
114 ------
115 Exception
116 The object could not be serialized.
117 """
118 return inMemoryDataset.model_dump_json(exclude_unset=True, exclude_defaults=True).encode()