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
mapper.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 from builtins import object
25 
26 import yaml
27 
28 from . import Policy
29 
30 """This module defines the Mapper base class."""
31 
32 
33 class Mapper(object):
34  """Mapper is a base class for all mappers.
35 
36  Subclasses may define the following methods:
37 
38  map_{datasetType}(self, dataId, write)
39  Map a dataset id for the given dataset type into a ButlerLocation.
40  If write=True, this mapping is for an output dataset.
41 
42  query_{datasetType}(self, key, format, dataId)
43  Return the possible values for the format fields that would produce
44  datasets at the granularity of key in combination with the provided
45  partial dataId.
46 
47  std_{datasetType}(self, item)
48  Standardize an object of the given data set type.
49 
50  Methods that must be overridden:
51 
52  keys(self)
53  Return a list of the keys that can be used in data ids.
54 
55  Other public methods:
56 
57  __init__(self)
58 
59  getDatasetTypes(self)
60 
61  map(self, datasetType, dataId, write=False)
62 
63  queryMetadata(self, datasetType, key, format, dataId)
64 
65  canStandardize(self, datasetType)
66 
67  standardize(self, datasetType, item, dataId)
68 
69  validate(self, dataId)
70  """
71 
72  @staticmethod
73  def Mapper(cfg):
74  '''Instantiate a Mapper from a configuration.
75  In come cases the cfg may have already been instantiated into a Mapper, this is allowed and
76  the input var is simply returned.
77 
78  :param cfg: the cfg for this mapper. It is recommended this be created by calling
79  Mapper.cfg()
80  :return: a Mapper instance
81  '''
82  if isinstance(cfg, Policy):
83  return cfg['cls'](cfg)
84  return cfg
85 
86  def __new__(cls, *args, **kwargs):
87  """Create a new Mapper, saving arguments for pickling.
88 
89  This is in __new__ instead of __init__ to save the user
90  from having to save the arguments themselves (either explicitly,
91  or by calling the super's __init__ with all their
92  *args,**kwargs. The resulting pickling system (of __new__,
93  __getstate__ and __setstate__ is similar to how __reduce__
94  is usually used, except that we save the user from any
95  responsibility (except when overriding __new__, but that
96  is not common).
97  """
98  self = super(Mapper, cls).__new__(cls)
99  self._arguments = (args, kwargs)
100  return self
101 
102  def __init__(self):
103  pass
104 
105  def __getstate__(self):
106  return self._arguments
107 
108  def __setstate__(self, state):
109  self._arguments = state
110  args, kwargs = state
111  self.__init__(*args, **kwargs)
112 
113  def keys(self):
114  raise NotImplementedError("keys() unimplemented")
115 
116  def queryMetadata(self, datasetType, format, dataId):
117  """Get possible values for keys given a partial data id.
118 
119  :param datasetType: see documentation about the use of datasetType
120  :param key: this is used as the 'level' parameter
121  :param format:
122  :param dataId: see documentation about the use of dataId
123  :return:
124  """
125 
126  func = getattr(self, 'query_' + datasetType)
127 
128  val = func(format, self.validate(dataId))
129  return val
130 
131  def getDatasetTypes(self):
132  """Return a list of the mappable dataset types."""
133 
134  list = []
135  for attr in dir(self):
136  if attr.startswith("map_"):
137  list.append(attr[4:])
138  return list
139 
140  def map(self, datasetType, dataId, write=False):
141  """Map a data id using the mapping method for its dataset type."""
142 
143  func = getattr(self, 'map_' + datasetType)
144  return func(self.validate(dataId), write)
145 
146  def canStandardize(self, datasetType):
147  """Return true if this mapper can standardize an object of the given
148  dataset type."""
149 
150  return hasattr(self, 'std_' + datasetType)
151 
152  def standardize(self, datasetType, item, dataId):
153  """Standardize an object using the standardization method for its data
154  set type, if it exists."""
155 
156  if hasattr(self, 'std_' + datasetType):
157  func = getattr(self, 'std_' + datasetType)
158  return func(item, self.validate(dataId))
159  return item
160 
161  def validate(self, dataId):
162  """Validate a dataId's contents.
163 
164  If the dataId is valid, return it. If an invalid component can be
165  transformed into a valid one, copy the dataId, fix the component, and
166  return the copy. Otherwise, raise an exception."""
167 
168  return dataId
169 
170  def backup(self, datasetType, dataId):
171  """Rename any existing object with the given type and dataId.
172 
173  Not implemented in the base mapper.
174  """
175  raise NotImplementedError("Base-class Mapper does not implement backups")