Coverage for python/lsst/daf/butler/formatters/pexConfigFormatter.py : 29%

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