22 from __future__
import annotations
24 __all__ = (
"PexConfigFormatter", )
35 from lsst.daf.butler.formatters.file
import FileFormatter
39 """Interface for reading and writing pex.config.Config objects from disk.
43 def _readFile(self, path: str, pytype: Optional[Type[Any]] =
None) -> Any:
44 """Read a pex.config.Config instance from the given file.
49 Path to use to open the file.
51 Class to use to read the config file.
55 data : `lsst.pex.config.Config`
56 Instance of class ``pytype`` read from config file. `None`
57 if the file could not be opened.
60 raise RuntimeError(
"A python type is always required for reading pex_config Config files")
62 if not os.path.exists(path):
75 except AssertionError
as err:
77 if not msg.startswith(
"config is of type"):
78 raise RuntimeError(
"Unexpected assertion; cannot infer Config class type.")
from err
79 actualPyTypeStr = msg.split()[-1]
80 actualPyType = doImport(actualPyTypeStr)
81 instance = actualPyType()
85 def _writeFile(self, inMemoryDataset: Any) ->
None:
86 """Write the in memory dataset to file on disk.
90 inMemoryDataset : `object`
96 The file could not be written.
98 inMemoryDataset.save(self.fileDescriptor.location.path)