Coverage for python/lsst/daf/butler/formatters/astropyTable.py: 84%
19 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-26 02:47 -0700
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-26 02:47 -0700
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__ = ("AstropyTableFormatter",)
30import os.path
31from typing import Any
33from .file import FileFormatter
36class AstropyTableFormatter(FileFormatter):
37 """Interface for reading and writing astropy.Table objects
38 in either ECSV or FITS format.
39 """
41 supportedWriteParameters = frozenset({"format"})
42 # Ideally we'd also support fits, but that doesn't
43 # round trip string columns correctly, so things
44 # need to be fixed up on read.
45 supportedExtensions = frozenset(
46 {
47 ".ecsv",
48 }
49 )
51 @property
52 def extension(self) -> str: # type: ignore
53 # Typing is ignored above since this is a property and the
54 # parent class has a class attribute
56 # Default to ECSV but allow configuration via write parameter
57 format = self.writeParameters.get("format", "ecsv")
58 if format == "ecsv": 58 ↛ 61line 58 didn't jump to line 61, because the condition on line 58 was never false
59 return ".ecsv"
60 # Other supported formats can be added here
61 raise RuntimeError(f"Requested file format '{format}' is not supported for Table")
63 def _readFile(self, path: str, pytype: type[Any] | None = None) -> Any:
64 """Read a file from the path in a supported format format.
66 Parameters
67 ----------
68 path : `str`
69 Path to use to open the file.
70 pytype : `type`
71 Class to use to read the serialized file.
73 Returns
74 -------
75 data : `object`
76 Instance of class ``pytype`` read from serialized file. None
77 if the file could not be opened.
78 """
79 if not os.path.exists(path) or pytype is None: 79 ↛ 80line 79 didn't jump to line 80, because the condition on line 79 was never true
80 return None
82 return pytype.read(path)
84 def _writeFile(self, inMemoryDataset: Any) -> None:
85 """Write the in memory dataset to file on disk.
87 Parameters
88 ----------
89 inMemoryDataset : `object`
90 Object to serialize.
92 Raises
93 ------
94 Exception
95 The file could not be written.
96 """
97 inMemoryDataset.write(self.fileDescriptor.location.path)