Skip to content

Commit

Permalink
Add yaml formatter parameter to allow unsafe dump
Browse files Browse the repository at this point in the history
This works fine for things like PropertyList where safe_load
will read it back because of the registered representers.
If an unscrupulous user enables it for something that has not
been properly setup for yaml serialization then the subsequent
safe_load will fail on read.
  • Loading branch information
timj committed Nov 5, 2020
1 parent 9876259 commit 477dcae
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
15 changes: 12 additions & 3 deletions python/lsst/daf/butler/configs/datastores/formatters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,20 @@ Packages:
formatter: lsst.obs.base.formatters.packages.PackagesFormatter
parameters:
format: yaml
PropertyList: lsst.daf.butler.formatters.yaml.YamlFormatter
PropertySet: lsst.daf.butler.formatters.yaml.YamlFormatter
PropertyList:
formatter: lsst.daf.butler.formatters.yaml.YamlFormatter
parameters:
unsafe_dump: true
PropertySet:
formatter: lsst.daf.butler.formatters.yaml.YamlFormatter
parameters:
unsafe_dump: true
NumpyArray: lsst.daf.butler.formatters.pickle.PickleFormatter
Plot: lsst.daf.butler.formatters.matplotlib.MatplotlibFormatter
MetricValue: lsst.daf.butler.formatters.yaml.YamlFormatter
MetricValue:
formatter: lsst.daf.butler.formatters.yaml.YamlFormatter
parameters:
unsafe_dump: true
BrighterFatterKernel: lsst.daf.butler.formatters.pickle.PickleFormatter
StructuredDataDict: lsst.daf.butler.formatters.yaml.YamlFormatter
Filter: lsst.obs.base.formatters.filter.FilterFormatter
11 changes: 10 additions & 1 deletion python/lsst/daf/butler/formatters/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ class YamlFormatter(FileFormatter):
unsupportedParameters = None
"""This formatter does not support any parameters"""

supportedWriteParameters = frozenset({"unsafe_dump"})
"""Allow the normal yaml.dump to be used to write the YAML. Use this
if you know that your class has registered representers."""

def _readFile(self, path: str, pytype: Type[Any] = None) -> Any:
"""Read a file from the path in YAML format.
Expand Down Expand Up @@ -159,7 +163,12 @@ def _toBytes(self, inMemoryDataset: Any) -> bytes:
"""
if hasattr(inMemoryDataset, "_asdict"):
inMemoryDataset = inMemoryDataset._asdict()
return yaml.safe_dump(inMemoryDataset).encode()
unsafe_dump = self.writeParameters.get("unsafe_dump", False)
if unsafe_dump:
serialized = yaml.dump(inMemoryDataset)
else:
serialized = yaml.safe_dump(inMemoryDataset)
return serialized.encode()

def _coerceType(self, inMemoryDataset: Any, storageClass: StorageClass,
pytype: Optional[Type[Any]] = None) -> Any:
Expand Down

0 comments on commit 477dcae

Please sign in to comment.