24 from __future__
import absolute_import
26 from builtins
import object
27 from future
import standard_library
29 from .
import NoRepositroyAtRoot
30 standard_library.install_aliases()
34 """Base class for storages"""
43 """Register derived classes for lookup by URI scheme.
45 A scheme is a name that describes the form a resource at the beginning of a URI
46 e.g. 'http' indicates HTML and related code, such as is found in http://www.lsst.org
48 The only currently supported schemes are:
49 * 'file' where the portion of the URI after the // indicates an absolute locaiton on disk.
50 for example: file:/my_repository_folder/
51 * '' (no scheme) where the entire string is a relative path on the local system
52 for example "my_repository_folder" will indicate a folder in the current working directory with the
55 See documentation for the urlparse python library for more information.
59 Storage is 'wet paint' and very likely to change during factorization of Butler back end and
60 storage formats (DM-6225). Use of it in production code other than via the 'old butler' API is
66 Name of the `scheme` the class is being registered for, which appears at the beginning of a URI.
68 A class object that should be used for a given scheme.
70 if scheme
in Storage.storages:
71 raise RuntimeError(
"Scheme '%s' already registered:%s" % (scheme, Storage.storages[scheme]))
72 Storage.storages[scheme] = cls
75 """Get a RepositoryCfg from a location specified by uri.
77 If a cfg is found then it is cached by the uri, so that multiple lookups
78 are not performed on storages that might be remote.
80 RepositoryCfgs are not supposed to change once they are created so this
81 should not lead to stale data.
83 cfg = self.repositoryCfgs.get(uri,
None)
86 parseRes = urllib.parse.urlparse(uri)
87 if parseRes.scheme
in Storage.storages:
92 raise RuntimeError(
"No storage registered for scheme %s" % parseRes.scheme)
97 """Write a RepositoryCfg object to a location described by uri"""
99 parseRes = urllib.parse.urlparse(uri)
100 if parseRes.scheme
in Storage.storages:
103 raise RuntimeError(
"No storage registered for scheme %s" % parseRes.scheme)
108 """Get a mapper class cfg value from location described by uri.
110 Note that in legacy repositories the mapper may be specified by a file called _mapper at the uri
111 location, and in newer repositories the mapper would be specified by a RepositoryCfg stored at the uri
116 Storage is 'wet paint' and very likely to change during factorization of Butler back end and
117 storage formats (DM-6225). Use of it in production code other than via the 'old butler' API is
118 strongly discouraged.
122 parseRes = urllib.parse.urlparse(uri)
123 if parseRes.scheme
in Storage.storages:
126 raise RuntimeError(
"No storage registered for scheme %s" % parseRes.scheme)
131 '''Instantiate a StorageInterface sublcass from a URI.
135 Storage is 'wet paint' and very likely to change during factorization of Butler back end and
136 storage formats (DM-6225). Use of it in production code other than via the 'old butler' API is
137 strongly discouraged.
142 The uri to the root location of a repository.
143 create : bool, optional
144 If True The StorageInterface subclass should create a new
145 repository at the root location. If False then a new repository
150 A Storage subclass instance, or if create is False and a repository
151 does not exist at the root location then returns None.
156 When a StorageInterface subclass does not exist for the scheme
157 indicated by the uri.
160 parseRes = urllib.parse.urlparse(uri)
161 if parseRes.scheme
in Storage.storages:
162 theClass = Storage.storages[parseRes.scheme]
164 ret = theClass(uri=uri, create=create)
165 except NoRepositroyAtRoot:
168 raise RuntimeError(
"No storage registered for scheme %s" % parseRes.scheme)
173 """Test if a URI is for a local filesystem storage.
175 This is mostly for backward compatibility; Butler V1 repositories were only ever on the local
176 filesystem. They may exist but not have a RepositoryCfg class. This enables conditional checking for a
179 This function treats 'file' and '' (no scheme) as posix storages, see
180 the class docstring for more details.
185 URI to the root of a Repository.
190 True if the URI is associated with a posix storage, else false.
192 parseRes = urllib.parse.urlparse(uri)
193 if parseRes.scheme
in (
'file',
''):
199 """Get a relative path from a location to a location, if a relative path for these 2 locations exists.
204 A URI that describes a location at which to start.
206 A URI that describes a target location.
211 A relative path that describes the path from fromUri to toUri, provided one exists. If a relative
212 path between the two URIs does not exist then the entire toUri path is returned.
214 fromUriParseRes = urllib.parse.urlparse(fromUri)
215 toUriParseRes = urllib.parse.urlparse(toUri)
216 if fromUriParseRes.scheme != toUriParseRes.scheme:
218 storage = Storage.storages.get(fromUriParseRes.scheme,
None)
221 return storage.relativePath(fromUri, toUri)
225 """Get an absolute path for the path from fromUri to toUri
229 fromUri : the starting location
231 toUri : the location relative to fromUri
237 URI that is absolutepath fromUri + toUri, if one exists. If toUri is absolute or if fromUri is not
238 related to toUri (e.g. are of different storage types) then toUri will be returned.
240 fromUriParseRes = urllib.parse.urlparse(fromUri)
241 toUriParseRes = urllib.parse.urlparse(toUri)
242 if fromUriParseRes.scheme != toUriParseRes.scheme:
244 storage = Storage.storages.get(fromUriParseRes.scheme,
None)
247 return storage.absolutePath(fromUri, toUri)
251 """Look for the given path in a storage root at URI; return None if it can't be found.
253 If the path contains an HDU indicator (a number in brackets before the
254 dot, e.g. 'foo.fits[1]', this will be stripped when searching and so
255 will match filenames without the HDU indicator, e.g. 'foo.fits'. The
256 path returned WILL contain the indicator though, e.g. ['foo.fits[1]'].
262 URI to the the root location to search
264 A filename (and optionally prefix path) to search for within root.
269 The location that was found, or None if no location was found.
271 parseRes = urllib.parse.urlparse(uri)
272 storage = Storage.storages.get(parseRes.scheme,
None)
274 return storage.search(uri, path)
279 """Ask if a storage at the location described by uri exists
284 URI to the the root location of the storage
289 True if the storage exists, false if not
291 parseRes = urllib.parse.urlparse(uri)
292 storage = Storage.storages.get(parseRes.scheme,
None)
294 return storage.storageExists(uri)