Skip to content

Commit

Permalink
Move composites logic shared by pickle and JSON to composites.py
Browse files Browse the repository at this point in the history
  • Loading branch information
timj committed Feb 8, 2018
1 parent 1725289 commit eda4e70
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 22 deletions.
31 changes: 31 additions & 0 deletions python/lsst/daf/butler/core/composites.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

"""Support for reading and writing composite objects."""

import collections


class DatasetComponent:

Expand Down Expand Up @@ -124,6 +126,35 @@ def genericAssembler(storageClass, components, pytype=None):
return obj


def validComponents(composite, storageClass):
"""Extract requested components from a composite.
Parameters
----------
composite : `object`
Composite from which to extract components.
storageClass : `StorageClass`
`StorageClass` associated with this composite object.
If None, it is assumed this is not to be treated as a composite.
Returns
-------
comps : `dict`
Non-None components extracted from the composite, indexed by the component
name as derived from the `StorageClass`.
"""
components = {}
if storageClass is not None and storageClass.components:
for c in storageClass.components:
if isinstance(composite, collections.Mapping):
comp = composite[c]
else:
comp = genericGetter(composite, c)
if comp is not None:
components[c] = comp
return components


def hasComponent(composite, componentName):
"""Determine if it seems likely that the composite has the component.
Expand Down
15 changes: 4 additions & 11 deletions python/lsst/daf/butler/formatters/jsonFormatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import json
import collections

from lsst.daf.butler.core.composites import genericAssembler, hasComponent
from lsst.daf.butler.core.composites import genericAssembler, validComponents
from lsst.daf.butler.core.formatter import Formatter


Expand Down Expand Up @@ -156,15 +156,8 @@ def write(self, inMemoryDataset, fileDescriptor):
inMemoryDataset = inMemoryDataset._asdict()
json.dump(inMemoryDataset, fd)

# The reference components come from the StorageClass when determining
# whether this object can be a composite.
sc = fileDescriptor.storageClass
comps = []
if sc is not None and sc.components:
for c in sc.components:
if (hasComponent(inMemoryDataset, c) is not None or
(isinstance(inMemoryDataset, collections.Mapping) and c in inMemoryDataset)):
comps.append(c)
# Get the list of valid components so we can build URIs
components = validComponents(inMemoryDataset, fileDescriptor.storageClass)

return (fileDescriptor.location.uri,
{c: fileDescriptor.location.componentUri(c) for c in comps})
{c: fileDescriptor.location.componentUri(c) for c in components})
15 changes: 4 additions & 11 deletions python/lsst/daf/butler/formatters/pickleFormatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import pickle
import collections

from lsst.daf.butler.core.composites import genericGetter, hasComponent
from lsst.daf.butler.core.composites import genericGetter, validComponents
from lsst.daf.butler.core.formatter import Formatter


Expand Down Expand Up @@ -151,15 +151,8 @@ def write(self, inMemoryDataset, fileDescriptor):
with open(self._getPath(fileDescriptor), "wb") as fd:
pickle.dump(inMemoryDataset, fd, protocol=-1)

# The reference components come from the StorageClass when determining
# whether this object can be a composite.
sc = fileDescriptor.storageClass
comps = []
if sc is not None and sc.components:
for c in sc.components:
if (hasComponent(inMemoryDataset, c) is not None or
(isinstance(inMemoryDataset, collections.Mapping) and c in inMemoryDataset)):
comps.append(c)
# Get the list of valid components so we can build URIs
components = validComponents(inMemoryDataset, fileDescriptor.storageClass)

return (fileDescriptor.location.uri,
{c: fileDescriptor.location.componentUri(c) for c in comps})
{c: fileDescriptor.location.componentUri(c) for c in components})

0 comments on commit eda4e70

Please sign in to comment.