26 """This module defines the ButlerLocation class."""
30 from collections
import namedtuple
32 from past.builtins
import basestring
35 from .
import iterify, doImport
43 assembler : function object
44 Function object or importable string to a function object that can be called with the assembler
45 signature: (dataId, componentDict, cls).
46 disassembler : function object
47 Function object or importable string to a function object that can be called with the disassembler
48 signature: (object, dataId, componentDict).
50 A python class object or importable string to a class object that can be used by the assembler to
51 instantiate an object to be returned.
52 dataId : dict or DataId
53 The dataId that is used to look up components.
54 mapper : Mapper instance
55 A reference to the mapper that created this ButlerComposite object.
60 """Information about a butler composite object. Some details come from the policy and some are filled
61 in by the butler. Component info is used while assembling and disassembling a composite object in
62 butler. It is used as an input to assemblers and disassemblers (which are part of the butler public
68 The datasetType of the component.
70 The python object instance that is this component.
72 The name of the function in the parent object to set this component.
73 Optional - may be None
75 The name of the function in the parent object to get this component.
76 Optional - may be None
78 If true, indicates that the obj should be a list of objects found via butlerSubset.
80 If true, indicates that the obj should not be serialized when performing a butler.put.
82 def __init__(self, datasetType, obj, setter, getter, subset, inputOnly):
91 return 'ComponentInfo(datasetType:%s, obj:%s, setter:%s, getter:%s, subset:%s)' % \
96 return 'ButlerComposite(assembler:%s, disassembler:%s, python:%s, dataId:%s, mapper:%s, componentInfo:%s, repository:%s)' % \
106 return "ComponentInfo(datasetType=%s, obj=%s, setter=%s, getter=%s)" % (
107 self.datasetType, self.obj, self.setter, self.getter)
110 def __init__(self, assembler, disassembler, python, dataId, mapper):
119 def add(self, id, datasetType, setter, getter, subset, inputOnly):
120 """Add a description of a component needed to fetch the composite dataset.
125 The name of the component in the policy definition.
127 The name of the datasetType of the component.
128 setter : string or None
129 The name of the function used to set this component into the python type that contains it.
130 Specifying a setter is optional, use None if the setter won't be specified or used.
131 getter : string or None
132 The name of the function used to get this component from the python type that contains it.
133 Specifying a setter is optional, use None if the setter won't be specified or used.
135 If true, indicates that the obj should be a list of objects found via butlerSubset.
137 If true, indicates that the obj should not be serialized when performing a butler.put.
147 return "ButlerComposite(assembler=%s, disassembler=%s, python=%s, dataId=%s, components=%s)" % (
158 """ButlerLocation is a struct-like class that holds information needed to
159 persist and retrieve an object using the LSST Persistence Framework.
161 Mappers should create and return ButlerLocations from their
162 map_{datasetType} methods.
166 pythonType - string or class instance
167 This is the type of python object that should be created when reading the location.
169 cppType - string or None
170 The type of cpp object represented by the location (optional, may be None)
173 The type of storage the object is in or should be place into.
175 locationList - list of string
176 A list of URI to place the object or where the object might be found. (Typically when reading the
177 length is expected to be exactly 1).
180 The dataId that was passed in when mapping the location. This may include keys that were not used for
181 mapping this location.
183 mapper - mapper class instance
184 The mapper object that mapped this location.
186 storage - storage class instance
187 The storage interface that can be used to read or write this location.
190 The dataId components that were used to map this location. If the mapper had to look up keys those
191 will be in this dict (even though they may not appear in the dataId parameter). If the dataId
192 parameter contained keys that were not required to map this item then those keys will NOT be in this
196 The datasetType that this location represents.
199 yaml_tag =
u"!ButlerLocation"
200 yaml_loader = yaml.Loader
201 yaml_dumper = yaml.Dumper
205 'ButlerLocation(pythonType=%r, cppType=%r, storageName=%r, storage=%r, locationList=%r,' \
206 ' additionalData=%r, mapper=%r, dataId=%r)' % \
210 def __init__(self, pythonType, cppType, storageName, locationList, dataId, mapper, storage,
211 usedDataId=
None, datasetType=
None):
213 self.
pythonType = str(pythonType)
if isinstance(pythonType, basestring)
else pythonType
220 for k, v
in dataId.items():
221 self.additionalData.set(k, v)
233 """Representer for dumping to YAML
238 return dumper.represent_mapping(ButlerLocation.yaml_tag,
239 {
'pythonType': obj.pythonType,
'cppType': obj.cppType,
240 'storageName': obj.storageName,
241 'locationList': obj.locationList,
'mapper': obj.mapper,
242 'storage': obj.storage,
'dataId': obj.dataId})
246 obj = loader.construct_mapping(node)
268 return [os.path.join(self.storage.root, l)
for l
in self.
getLocations()]