LSSTApplications  18.1.0
LSSTDataManagementBasePackage
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 builtins import object, super
24 
25 from lsst.daf.persistence import Policy
26 
27 import yaml
28 
29 
30 class AccessCfg(Policy, yaml.YAMLObject):
31  yaml_tag = u"!AccessCfg"
32 
33  def __init__(self, cls, storageCfg):
34  super().__init__({'storageCfg': storageCfg, 'cls': cls})
35 
36 
37 class Access(object):
38  """Implements an butler framework interface for Transport, Storage, and Registry
39 
40  .. warning::
41 
42  Access is 'wet paint' and very likely to change. Use of it in production
43  code other than via the 'old butler' API is strongly discouraged.
44 
45  """
46 
47  @classmethod
48  def cfg(cls, storageCfg):
49  """Helper func to create a properly formatted Policy to configure an Access instance.
50 
51  :param storageCfg: a cfg to instantiate a storage.
52  :return:
53  """
54  return AccessCfg(cls=cls, storageCfg=storageCfg)
55 
56  def __init__(self, cfg):
57  """Initializer
58 
59  :param cfg: a Policy that defines the configuration for this class. It is recommended that the cfg be
60  created by calling Access.cfg()
61  :return:
62  """
63  self.storage = cfg['storageCfg.cls'](cfg['storageCfg'])
64 
65  def __repr__(self):
66  return 'Access(storage=%s)' % self.storage
67 
68  def mapperClass(self):
69  """Get the mapper class associated with a repository root.
70 
71  :return: the mapper class
72  """
73  return self.storage.mapperClass()
74 
75  def root(self):
76  """Get the repository root as defined by the Storage class, this refers to the 'top' of a persisted
77  repository. The exact type of Root can vary based on Storage type.
78 
79  :return: the root of the persisted repository.
80  """
81 
82  return self.storage.root
83 
84  def locationWithRoot(self, location):
85  """Given a location, get a fully qualified handle to location including storage root.
86 
87  Note; at the time of this writing the only existing storage type is PosixStorage. This returns the
88  root+location.
89  :param location:
90  :return:
91  """
92  return self.storage.locationWithRoot(location)
93 
94  def setCfg(self, repoCfg):
95  """Writes the repository configuration to Storage.
96 
97  :param repoCfg: the Policy cfg to be written
98  :return: None
99  """
100  self.storage.setCfg(repoCfg)
101 
102  def loadCfg(self):
103  """Reads the repository configuration from Storage.
104 
105  :return: the Policy cfg
106  """
107  return self.storage.loadCfg()
108 
109  def write(self, butlerLocation, obj):
110  """Passes an object to Storage to be written into the repository.
111 
112  :param butlerLocation: the location & formatting for the object to be written.
113  :param obj: the object to be written.
114  :return: None
115  """
116  self.storage.write(butlerLocation, obj)
117 
118  def read(self, butlerLocation):
119  """Reads an object from storage
120 
121  :param butlerLocation: describes the location & how to load the object.
122  :return:
123  """
124  return self.storage.read(butlerLocation=butlerLocation)
125 
126  def exists(self, location):
127  """Query if a location exists.
128 
129  As of this writing the only storage type is PosixStorage, and it works to say that 'location' is a
130  simple locaiton descriptor. In the case of PosixStorage that's a path. If this needs to become more
131  complex it could be changed to be a butlerLocation, or something else, as needed.
132  :param location: a simple location descriptor, type is dependent on Storage.
133  :return: True if location exists, else False.
134  """
135  return self.storage.exists(location)
136 
137  def lookup(self, *args, **kwargs):
138  """Perform a lookup in the registry.
139 
140  Returns a list of dataId for each valid lookup (right? TODO VERIFY)"""
141  return self.storage.lookup(*args, **kwargs)
def exists(self, location)
Definition: access.py:126
def write(self, butlerLocation, obj)
Definition: access.py:109
def read(self, butlerLocation)
Definition: access.py:118
def cfg(cls, storageCfg)
Definition: access.py:48
def lookup(self, args, kwargs)
Definition: access.py:137
def __init__(self, cls, storageCfg)
Definition: access.py:33
def locationWithRoot(self, location)
Definition: access.py:84
def setCfg(self, repoCfg)
Definition: access.py:94