24 from __future__
import absolute_import
28 from future
import standard_library
29 standard_library.install_aliases()
30 from builtins
import object
33 from .storage
import Storage
35 from lsst.utils
import continueClass
39 """Base class for storages"""
45 """Register derived classes for lookup by URI scheme.
47 A scheme is a name that describes the form a resource at the beginning of a URI
48 e.g. 'http' indicates HTML and related code, such as is found in http://www.lsst.org
50 The only currently supported schemes are:
51 * 'file' where the portion of the URI after the // indicates an absolute locaiton on disk.
52 for example: file:/my_repository_folder/
53 * '' (no scheme) where the entire string is a relative path on the local system
54 for example "my_repository_folder" will indicate a folder in the current working directory with the
57 See documentation for the urlparse python library for more information.
61 Storage is 'wet paint' and very likely to change during factorization of Butler back end and
62 storage formats (DM-6225). Use of it in production code other than via the 'old butler' API is
68 Name of the `scheme` the class is being registered for, which appears at the beginning of a URI.
70 A class object that should be used for a given scheme.
72 if scheme
in Storage.storages:
73 raise RuntimeError(
"Scheme '%s' already registered:%s" % (scheme, Storage.storages[scheme]))
74 Storage.storages[scheme] = cls
78 """Get a RepositoryCfg from a location specified by uri."""
80 parseRes = urllib.parse.urlparse(uri)
81 if parseRes.scheme
in Storage.storages:
84 raise RuntimeError(
"No storage registered for scheme %s" % parseRes.scheme)
89 """Write a RepositoryCfg object to a location described by uri"""
91 parseRes = urllib.parse.urlparse(uri)
92 if parseRes.scheme
in Storage.storages:
95 raise RuntimeError(
"No storage registered for scheme %s" % parseRes.scheme)
100 """Get a mapper class cfg value from location described by uri.
102 Note that in legacy repositories the mapper may be specified by a file called _mapper at the uri
103 location, and in newer repositories the mapper would be specified by a RepositoryCfg stored at the uri
108 Storage is 'wet paint' and very likely to change during factorization of Butler back end and
109 storage formats (DM-6225). Use of it in production code other than via the 'old butler' API is
110 strongly discouraged.
114 parseRes = urllib.parse.urlparse(uri)
115 if parseRes.scheme
in Storage.storages:
118 raise RuntimeError(
"No storage registered for scheme %s" % parseRes.scheme)
123 '''Instantiate a storage sublcass from a URI.
127 Storage is 'wet paint' and very likely to change during factorization of Butler back end and
128 storage formats (DM-6225). Use of it in production code other than via the 'old butler' API is
129 strongly discouraged.
134 The uri to the root location of a repository.
138 A Storage subclass instance.
141 parseRes = urllib.parse.urlparse(uri)
142 if parseRes.scheme
in Storage.storages:
143 theClass = Storage.storages[parseRes.scheme]
144 ret = theClass(uri=uri)
146 raise RuntimeError(
"No storage registered for scheme %s" % parseRes.scheme)
151 """Test if a URI is for a local filesystem storage.
153 This is mostly for backward compatibility; Butler V1 repositories were only ever on the local
154 filesystem. They may exist but not have a RepositoryCfg class. This enables conditional checking for a
157 This function treats 'file' and '' (no scheme) as posix storages, see
158 the class docstring for more details.
163 URI to the root of a Repository.
168 True if the URI is associated with a posix storage, else false.
170 parseRes = urllib.parse.urlparse(uri)
171 if parseRes.scheme
in (
'file',
''):
177 """Get a relative path from a location to a location, if a relative path for these 2 locations exists.
182 A URI that describes a location at which to start.
184 A URI that describes a target location.
189 A relative path that describes the path from fromUri to toUri, provided one exists. If a relative
190 path between the two URIs does not exist then the entire toUri path is returned.
192 fromUriParseRes = urllib.parse.urlparse(fromUri)
193 toUriParseRes = urllib.parse.urlparse(toUri)
194 if fromUriParseRes.scheme != toUriParseRes.scheme:
196 storage = Storage.storages.get(fromUriParseRes.scheme,
None)
199 return storage.relativePath(fromUri, toUri)
203 """Get an absolute path for the path from fromUri to toUri
207 fromUri : the starting location
209 toUri : the location relative to fromUri
215 URI that is absolutepath fromUri + toUri, if one exists. If toUri is absolute or if fromUri is not
216 related to toUri (e.g. are of different storage types) then toUri will be returned.
218 fromUriParseRes = urllib.parse.urlparse(fromUri)
219 toUriParseRes = urllib.parse.urlparse(toUri)
220 if fromUriParseRes.scheme != toUriParseRes.scheme:
222 storage = Storage.storages.get(fromUriParseRes.scheme,
None)
225 return storage.absolutePath(fromUri, toUri)
229 """Look for the given path in a storage root at URI; return None if it can't be found.
231 If the path contains an HDU indicator (a number in brackets before the
232 dot, e.g. 'foo.fits[1]', this will be stripped when searching and so
233 will match filenames without the HDU indicator, e.g. 'foo.fits'. The
234 path returned WILL contain the indicator though, e.g. ['foo.fits[1]'].
240 URI to the the root location to search
242 A filename (and optionally prefix path) to search for within root.
247 The location that was found, or None if no location was found.
249 parseRes = urllib.parse.urlparse(uri)
250 storage = Storage.storages.get(parseRes.scheme,
None)
252 return storage.search(uri, path)
257 """Ask if a storage at the location described by uri exists
262 URI to the the root location of the storage
267 True if the storage exists, false if not
269 parseRes = urllib.parse.urlparse(uri)
270 storage = Storage.storages.get(parseRes.scheme,
None)
272 return storage.storageExists(uri)