Skip to content

Commit

Permalink
Enable reading polymorphic pex_config types (DM-32062)
Browse files Browse the repository at this point in the history
The `readConfigStorage()` method now uses special Config method which
can read arbitrary types from serialized data. We still limit allowed
types to the sub-classes of the type defined in policy.
  • Loading branch information
andy-slac committed Oct 6, 2021
1 parent aaec9c0 commit 6d2f5c6
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions python/lsst/daf/persistence/posixStorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
StorageInterface, Storage, ButlerLocation,
NoRepositroyAtRoot, RepositoryCfg, doImport)
from lsst.log import Log
import lsst.pex.config as pexConfig
from .safeFileIo import SafeFilename, safeMakeDir


Expand Down Expand Up @@ -527,13 +528,20 @@ def readConfigStorage(butlerLocation):
logLoc = LogicalLocation(locStringWithRoot, butlerLocation.getAdditionalData())
if not os.path.exists(logLoc.locString()):
raise RuntimeError("No such config file: " + logLoc.locString())

# Automatically determine the Config class from the serialized form
with open(logLoc.locString(), "r") as fd:
config_py = fd.read()
config = pexConfig.Config._fromPython(config_py)

pythonType = butlerLocation.getPythonType()
if pythonType is not None:
if isinstance(pythonType, str):
pythonType = doImport(pythonType)
finalItem = pythonType()
finalItem.load(logLoc.locString())
results.append(finalItem)
if not isinstance(config, pythonType):
raise TypeError(f"Unexpected type of config: {type(config)}, expect {pythonType}")

results.append(config)
return results


Expand Down

0 comments on commit 6d2f5c6

Please sign in to comment.