LSSTApplications  11.0-13-gbb96280,12.1+18,12.1+7,12.1-1-g14f38d3+72,12.1-1-g16c0db7+5,12.1-1-g5961e7a+84,12.1-1-ge22e12b+23,12.1-11-g06625e2+4,12.1-11-g0d7f63b+4,12.1-19-gd507bfc,12.1-2-g7dda0ab+38,12.1-2-gc0bc6ab+81,12.1-21-g6ffe579+2,12.1-21-gbdb6c2a+4,12.1-24-g941c398+5,12.1-3-g57f6835+7,12.1-3-gf0736f3,12.1-37-g3ddd237,12.1-4-gf46015e+5,12.1-5-g06c326c+20,12.1-5-g648ee80+3,12.1-5-gc2189d7+4,12.1-6-ga608fc0+1,12.1-7-g3349e2a+5,12.1-7-gfd75620+9,12.1-9-g577b946+5,12.1-9-gc4df26a+10
LSSTDataManagementBasePackage
butlerLocation.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 #
4 # LSST Data Management System
5 # Copyright 2008, 2009, 2010 LSST Corporation.
6 #
7 # This product includes software developed by the
8 # LSST Project (http://www.lsst.org/).
9 #
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the LSST License Statement and
21 # the GNU General Public License along with this program. If not,
22 # see <http://www.lsstcorp.org/LegalNotices/>.
23 #
24 
25 
26 """This module defines the ButlerLocation class."""
27 
28 import lsst.daf.base as dafBase
29 
30 from collections import namedtuple
31 from past.builtins import basestring
32 import yaml
33 
34 from . import iterify, doImport
35 
36 
37 class ButlerComposite(object):
38  """Initializer
39 
40  Parameters
41  ----------
42  assembler : function object
43  Function object or importable string to a function object that can be called with the assembler
44  signature: (dataId, componentDict, cls).
45  disassembler : function object
46  Function object or importable string to a function object that can be called with the disassembler
47  signature: (object, dataId, componentDict).
48  python : class object
49  A python class object or importable string to a class object that can be used by the assembler to
50  instantiate an object to be returned.
51  dataId : dict or DataId
52  The dataId that is used to look up components.
53  mapper : Mapper instance
54  A reference to the mapper that created this ButlerComposite object.
55  """
56 
57 
58  class ComponentInfo():
59  """Information about a butler composite object. Some details come from the policy and some are filled
60  in by the butler. Component info is used while assembling and disassembling a composite object in
61  butler. It is used as an input to assemblers and disassemblers (which are part of the butler public
62  API).
63 
64  Parameters
65  ----------
66  datasetType : string
67  The datasetType of the component.
68  obj : object instance
69  The python object instance that is this component.
70  setter : string
71  The name of the function in the parent object to set this component.
72  Optional - may be None
73  getter : string
74  The name of the function in the parent object to get this component.
75  Optional - may be None
76  subset : bool
77  If true, indicates that the obj should be a list of objects found via butlerSubset.
78  inputOnly : bool
79  If true, indicates that the obj should not be serialized when performing a butler.put.
80  """
81  def __init__(self, datasetType, obj, setter, getter, subset, inputOnly):
82  self.datasetType = datasetType
83  self.obj = obj
84  self.setter = setter
85  self.getter = getter
86  self.subset = subset
87  self.inputOnly = inputOnly
88 
89  def __repr__(self):
90  return 'ComponentInfo(datasetType:%s, obj:%s, setter:%s, getter:%s, subset:%s)' % \
91  (self.datasetType, self.obj, self.setter, self.getter, self.subset)
92 
93 
94  def __repr__(self):
95  return 'ButlerComposite(assembler:%s, disassembler:%s, python:%s, dataId:%s, mapper:%s, componentInfo:%s, repository:%s)' % \
96  (self.assembler,
97  self.disassembler,
98  self.python,
99  self.dataId,
100  self.mapper,
101  self.componentInfo,
102  self.repository)
103 
104  def __repr__(self):
105  return "ComponentInfo(datasetType=%s, obj=%s, setter=%s, getter=%s)" % (
106  self.datasetType, self.obj, self.setter, self.getter)
107 
108 
109  def __init__(self, assembler, disassembler, python, dataId, mapper):
110  self.assembler = doImport(assembler) if isinstance(assembler, basestring) else assembler
111  self.disassembler = doImport(disassembler) if isinstance(disassembler, basestring) else disassembler
112  self.python = doImport(python) if isinstance(python, basestring) else python
113  self.dataId = dataId
114  self.mapper = mapper
115  self.componentInfo = {}
116  self.repository = None
117 
118  def add(self, id, datasetType, setter, getter, subset, inputOnly):
119  """Add a description of a component needed to fetch the composite dataset.
120 
121  Parameters
122  ----------
123  id : string
124  The name of the component in the policy definition.
125  datasetType : string
126  The name of the datasetType of the component.
127  setter : string or None
128  The name of the function used to set this component into the python type that contains it.
129  Specifying a setter is optional, use None if the setter won't be specified or used.
130  getter : string or None
131  The name of the function used to get this component from the python type that contains it.
132  Specifying a setter is optional, use None if the setter won't be specified or used.
133  subset : bool
134  If true, indicates that the obj should be a list of objects found via butlerSubset.
135  inputOnly : bool
136  If true, indicates that the obj should not be serialized when performing a butler.put.
137  """
138  self.componentInfo[id] = ButlerComposite.ComponentInfo(datasetType=datasetType,
139  obj = None,
140  setter=setter,
141  getter=getter,
142  subset=subset,
143  inputOnly=inputOnly)
144 
145  def __repr__(self):
146  return "ButlerComposite(assembler=%s, disassembler=%s, python=%s, dataId=%s, components=%s)" % (
147  self.assembler, self.disassembler, self.python, self.dataId, self.componentInfo)
148 
149  def setRepository(self, repository):
150  self.repository = repository
151 
152  def getRepository(self):
153  return self.repository
154 
155 
156 class ButlerLocation(yaml.YAMLObject):
157  """ButlerLocation is a struct-like class that holds information needed to
158  persist and retrieve an object using the LSST Persistence Framework.
159 
160  Mappers should create and return ButlerLocations from their
161  map_{datasetType} methods.
162 
163  Parameters
164  ----------
165  pythonType - string or class instance
166  This is the type of python object that should be created when reading the location.
167 
168  cppType - string or None
169  The type of cpp object represented by the location (optional, may be None)
170 
171  storageName - string
172  The type of storage the object is in or should be place into.
173 
174  locationList - list of string
175  A list of URI to place the object or where the object might be found. (Typically when reading the
176  length is expected to be exactly 1).
177 
178  dataId - dict
179  The dataId that was passed in when mapping the location. This may include keys that were not used for
180  mapping this location.
181 
182  mapper - mapper class instance
183  The mapper object that mapped this location.
184 
185  storage - storage class instance
186  The storage interface that can be used to read or write this location.
187 
188  usedDataId - dict
189  The dataId components that were used to map this location. If the mapper had to look up keys those
190  will be in this dict (even though they may not appear in the dataId parameter). If the dataId
191  parameter contained keys that were not required to map this item then those keys will NOT be in this
192  parameter.
193 
194  datasetType - string
195  The datasetType that this location represents.
196  """
197 
198  yaml_tag = u"!ButlerLocation"
199  yaml_loader = yaml.Loader
200  yaml_dumper = yaml.Dumper
201 
202  def __repr__(self):
203  return \
204  'ButlerLocation(pythonType=%r, cppType=%r, storageName=%r, locationList=%r,' \
205  ' additionalData=%r, mapper=%r, dataId=%r)' % \
206  (self.pythonType, self.cppType, self.storageName, self.locationList,
207  self.additionalData, self.mapper, self.dataId)
208 
209  def __init__(self, pythonType, cppType, storageName, locationList, dataId, mapper, storage=None,
210  usedDataId=None, datasetType=None):
211  self.pythonType = pythonType
212  self.cppType = cppType
213  self.storageName = storageName
214  self.mapper = mapper
215  self.storage = storage
216  self.locationList = iterify(locationList)
218  for k, v in dataId.items():
219  self.additionalData.set(k, v)
220  self.dataId = dataId
221  self.usedDataId = usedDataId
222  self.datasetType = datasetType
223 
224  def __str__(self):
225  s = "%s at %s(%s)" % (self.pythonType, self.storageName,
226  ", ".join(self.locationList))
227  return s
228 
229  @staticmethod
230  def to_yaml(dumper, obj):
231  """Representer for dumping to YAML
232  :param dumper:
233  :param obj:
234  :return:
235  """
236  return dumper.represent_mapping(ButlerLocation.yaml_tag,
237  {'pythonType': obj.pythonType, 'cppType': obj.cppType,
238  'storageName': obj.storageName,
239  'locationList': obj.locationList, 'mapper': obj.mapper,
240  'storage': obj.storage, 'dataId': obj.dataId})
241 
242  @staticmethod
243  def from_yaml(loader, node):
244  obj = loader.construct_mapping(node)
245  return ButlerLocation(**obj)
246 
247  def setRepository(self, repository):
248  self.repository = repository
249 
250  def getRepository(self):
251  return self.repository
252 
253  def getPythonType(self):
254  return self.pythonType
255 
256  def getCppType(self):
257  return self.cppType
258 
259  def getStorageName(self):
260  return self.storageName
261 
262  def getLocations(self):
263  return self.locationList
264 
265  def getAdditionalData(self):
266  return self.additionalData
Class for storing generic metadata.
Definition: PropertySet.h:82