lsst.daf.persistence  13.0-30-gd2bda26+1
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
access.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # LSST Data Management System
4 # Copyright 2016 LSST Corporation.
5 #
6 # This product includes software developed by the
7 # LSST Project (http://www.lsst.org/).
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the LSST License Statement and
20 # the GNU General Public License along with this program. If not,
21 # see <http://www.lsstcorp.org/LegalNotices/>.
22 #
23 from future import standard_library
24 standard_library.install_aliases()
25 from builtins import object
26 
27 from lsst.daf.persistence import Policy
28 
29 import yaml
30 
31 
32 class AccessCfg(Policy, yaml.YAMLObject):
33  yaml_tag = u"!AccessCfg"
34 
35  def __init__(self, cls, storageCfg):
36  super(AccessCfg, self).__init__({'storageCfg': storageCfg, 'cls': cls})
37 
38 
39 class Access(object):
40  """Implements an butler framework interface for Transport, Storage, and Registry
41 
42  .. warning::
43 
44  Access is 'wet paint' and very likely to change. Use of it in production
45  code other than via the 'old butler' API is strongly discouraged.
46 
47  """
48 
49  @classmethod
50  def cfg(cls, storageCfg):
51  """Helper func to create a properly formatted Policy to configure an Access instance.
52 
53  :param storageCfg: a cfg to instantiate a storage.
54  :return:
55  """
56  return AccessCfg(cls=cls, storageCfg=storageCfg)
57 
58  def __init__(self, cfg):
59  """Initializer
60 
61  :param cfg: a Policy that defines the configuration for this class. It is recommended that the cfg be
62  created by calling Access.cfg()
63  :return:
64  """
65  self.storage = cfg['storageCfg.cls'](cfg['storageCfg'])
66 
67  def __repr__(self):
68  return 'Access(storage=%s)' % self.storage
69 
70  def mapperClass(self):
71  """Get the mapper class associated with a repository root.
72 
73  :return: the mapper class
74  """
75  return self.storage.mapperClass()
76 
77  def root(self):
78  """Get the repository root as defined by the Storage class, this refers to the 'top' of a persisted
79  repository. The exact type of Root can vary based on Storage type.
80 
81  :return: the root of the persisted repository.
82  """
83 
84  return self.storage.root
85 
86  def locationWithRoot(self, location):
87  """Given a location, get a fully qualified handle to location including storage root.
88 
89  Note; at the time of this writing the only existing storage type is PosixStorage. This returns the
90  root+location.
91  :param location:
92  :return:
93  """
94  return self.storage.locationWithRoot(location)
95 
96  def setCfg(self, repoCfg):
97  """Writes the repository configuration to Storage.
98 
99  :param repoCfg: the Policy cfg to be written
100  :return: None
101  """
102  self.storage.setCfg(repoCfg)
103 
104  def loadCfg(self):
105  """Reads the repository configuration from Storage.
106 
107  :return: the Policy cfg
108  """
109  return self.storage.loadCfg()
110 
111  def write(self, butlerLocation, obj):
112  """Passes an object to Storage to be written into the repository.
113 
114  :param butlerLocation: the location & formatting for the object to be written.
115  :param obj: the object to be written.
116  :return: None
117  """
118  self.storage.write(butlerLocation, obj)
119 
120  def read(self, butlerLocation):
121  """Reads an object from storage
122 
123  :param butlerLocation: describes the location & how to load the object.
124  :return:
125  """
126  return self.storage.read(butlerLocation=butlerLocation)
127 
128  def exists(self, location):
129  """Query if a location exists.
130 
131  As of this writing the only storage type is PosixStorage, and it works to say that 'location' is a
132  simple locaiton descriptor. In the case of PosixStorage that's a path. If this needs to become more
133  complex it could be changed to be a butlerLocation, or something else, as needed.
134  :param location: a simple location descriptor, type is dependent on Storage.
135  :return: True if location exists, else False.
136  """
137  return self.storage.exists(location)
138 
139  def lookup(self, *args, **kwargs):
140  """Perform a lookup in the registry.
141 
142  Returns a list of dataId for each valid lookup (right? TODO VERIFY)"""
143  return self.storage.lookup(*args, **kwargs)