Coverage for python/lsst/obs/base/formatters/pexConfig.py : 79%

Hot-keys 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/>.
22from __future__ import annotations
24__all__ = ("PexConfigFormatter", )
26import os.path
28from typing import (
29 Any,
30 Optional,
31 Type,
32)
34from lsst.utils import doImport
35from lsst.daf.butler.formatters.file import FileFormatter
38class PexConfigFormatter(FileFormatter):
39 """Interface for reading and writing pex.config.Config objects from disk.
40 """
41 extension = ".py"
43 def _readFile(self, path: str, pytype: Optional[Type[Any]] = None) -> Any:
44 """Read a pex.config.Config instance from the given file.
46 Parameters
47 ----------
48 path : `str`
49 Path to use to open the file.
50 pytype : `type`
51 Class to use to read the config file.
53 Returns
54 -------
55 data : `lsst.pex.config.Config`
56 Instance of class ``pytype`` read from config file. `None`
57 if the file could not be opened.
58 """
59 if pytype is None: 59 ↛ 60line 59 didn't jump to line 60, because the condition on line 59 was never true
60 raise RuntimeError("A python type is always required for reading pex_config Config files")
62 if not os.path.exists(path): 62 ↛ 63line 62 didn't jump to line 63, because the condition on line 62 was never true
63 return None
64 instance = pytype()
65 # Configs can only be loaded if you use the correct derived type,
66 # but we can't really store the correct derive type in the StorageClass
67 # because that'd be a huge proliferation of StorageClasses.
68 # Instead, we use a bit of a hack: try to load using the base-class
69 # Config, inspect the exception message to obtain the class we should
70 # have used, import that, and try it instead.
71 # TODO: clean this up, somehow.
72 try:
73 instance.load(path)
74 return instance
75 except AssertionError as err:
76 msg = str(err)
77 if not msg.startswith("config is of type"): 77 ↛ 78line 77 didn't jump to line 78, because the condition on line 77 was never true
78 raise RuntimeError("Unexpected assertion; cannot infer Config class type.") from err
79 actualPyTypeStr = msg.split()[-1]
80 actualPyType = doImport(actualPyTypeStr)
81 instance = actualPyType()
82 instance.load(path)
83 return instance
85 def _writeFile(self, inMemoryDataset: Any) -> None:
86 """Write the in memory dataset to file on disk.
88 Parameters
89 ----------
90 inMemoryDataset : `object`
91 Object to serialize.
93 Raises
94 ------
95 Exception
96 The file could not be written.
97 """
98 inMemoryDataset.save(self.fileDescriptor.location.path)