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