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 b59a25e
Show file tree
Hide file tree
Showing 2 changed files with 12 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)}, expected {pythonType}")

results.append(config)
return results


Expand Down
1 change: 1 addition & 0 deletions ups/daf_persistence.table
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
setupRequired(utils)
setupRequired(log)
setupRequired(daf_base)
setupRequired(pex_config)

envPrepend(LD_LIBRARY_PATH, ${PRODUCT_DIR}/lib)
envPrepend(DYLD_LIBRARY_PATH, ${PRODUCT_DIR}/lib)
Expand Down

0 comments on commit b59a25e

Please sign in to comment.