Coverage for python/lsst/daf/butler/formatters/astropyTable.py: 84%

19 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-10-25 15:13 +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/>. 

21 

22__all__ = ("AstropyTableFormatter",) 

23 

24import os.path 

25from typing import Any 

26 

27from .file import FileFormatter 

28 

29 

30class AstropyTableFormatter(FileFormatter): 

31 """Interface for reading and writing astropy.Table objects 

32 in either ECSV or FITS format. 

33 """ 

34 

35 supportedWriteParameters = frozenset({"format"}) 

36 # Ideally we'd also support fits, but that doesn't 

37 # round trip string columns correctly, so things 

38 # need to be fixed up on read. 

39 supportedExtensions = frozenset( 

40 { 

41 ".ecsv", 

42 } 

43 ) 

44 

45 @property 

46 def extension(self) -> str: # type: ignore 

47 # Typing is ignored above since this is a property and the 

48 # parent class has a class attribute 

49 

50 # Default to ECSV but allow configuration via write parameter 

51 format = self.writeParameters.get("format", "ecsv") 

52 if format == "ecsv": 52 ↛ 55line 52 didn't jump to line 55, because the condition on line 52 was never false

53 return ".ecsv" 

54 # Other supported formats can be added here 

55 raise RuntimeError(f"Requested file format '{format}' is not supported for Table") 

56 

57 def _readFile(self, path: str, pytype: type[Any] | None = None) -> Any: 

58 """Read a file from the path in a supported format format. 

59 

60 Parameters 

61 ---------- 

62 path : `str` 

63 Path to use to open the file. 

64 pytype : `type` 

65 Class to use to read the serialized file. 

66 

67 Returns 

68 ------- 

69 data : `object` 

70 Instance of class ``pytype`` read from serialized file. None 

71 if the file could not be opened. 

72 """ 

73 if not os.path.exists(path) or pytype is None: 73 ↛ 74line 73 didn't jump to line 74, because the condition on line 73 was never true

74 return None 

75 

76 return pytype.read(path) 

77 

78 def _writeFile(self, inMemoryDataset: Any) -> None: 

79 """Write the in memory dataset to file on disk. 

80 

81 Parameters 

82 ---------- 

83 inMemoryDataset : `object` 

84 Object to serialize. 

85 

86 Raises 

87 ------ 

88 Exception 

89 The file could not be written. 

90 """ 

91 inMemoryDataset.write(self.fileDescriptor.location.path)