Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

#!/usr/bin/env python 

 

# 

# LSST Data Management System 

# Copyright 2016 LSST Corporation. 

# 

# This product includes software developed by the 

# LSST Project (http://www.lsst.org/). 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the LSST License Statement and 

# the GNU General Public License along with this program. If not, 

# see <http://www.lsstcorp.org/LegalNotices/>. 

# 

 

import urllib.parse 

from . import NoRepositroyAtRoot 

 

 

class Storage: 

"""Base class for storages""" 

 

storages = {} 

 

def __init__(self): 

self.repositoryCfgs = {} 

 

@staticmethod 

def registerStorageClass(scheme, cls): 

"""Register derived classes for lookup by URI scheme. 

 

A scheme is a name that describes the form a resource at the beginning of a URI 

e.g. 'http' indicates HTML and related code, such as is found in http://www.lsst.org 

 

The only currently supported schemes are: 

* 'file' where the portion of the URI after the // indicates an absolute locaiton on disk. 

for example: file:/my_repository_folder/ 

* '' (no scheme) where the entire string is a relative path on the local system 

for example "my_repository_folder" will indicate a folder in the current working directory with the 

same name. 

 

See documentation for the urlparse python library for more information. 

 

.. warning:: 

 

Storage is 'wet paint' and very likely to change during factorization of Butler back end and 

storage formats (DM-6225). Use of it in production code other than via the 'old butler' API is 

strongly discouraged. 

 

Parameters 

---------- 

scheme : str 

Name of the `scheme` the class is being registered for, which appears at the beginning of a URI. 

cls : class object 

A class object that should be used for a given scheme. 

""" 

66 ↛ 67line 66 didn't jump to line 67, because the condition on line 66 was never true if scheme in Storage.storages: 

raise RuntimeError("Scheme '%s' already registered:%s" % (scheme, Storage.storages[scheme])) 

Storage.storages[scheme] = cls 

 

def getRepositoryCfg(self, uri): 

"""Get a RepositoryCfg from a location specified by uri. 

 

If a cfg is found then it is cached by the uri, so that multiple lookups 

are not performed on storages that might be remote. 

 

RepositoryCfgs are not supposed to change once they are created so this 

should not lead to stale data. 

""" 

cfg = self.repositoryCfgs.get(uri, None) 

if cfg: 

return cfg 

parseRes = urllib.parse.urlparse(uri) 

if parseRes.scheme in Storage.storages: 

cfg = Storage.storages[parseRes.scheme].getRepositoryCfg(uri) 

if cfg: 

self.repositoryCfgs[uri] = cfg 

else: 

raise RuntimeError("No storage registered for scheme %s" % parseRes.scheme) 

return cfg 

 

@staticmethod 

def putRepositoryCfg(cfg, uri): 

"""Write a RepositoryCfg object to a location described by uri""" 

ret = None 

parseRes = urllib.parse.urlparse(uri) 

if parseRes.scheme in Storage.storages: 

ret = Storage.storages[parseRes.scheme].putRepositoryCfg(cfg, uri) 

else: 

raise RuntimeError("No storage registered for scheme %s" % parseRes.scheme) 

return ret 

 

@staticmethod 

def getMapperClass(uri): 

"""Get a mapper class cfg value from location described by uri. 

 

Note that in legacy repositories the mapper may be specified by a file called _mapper at the uri 

location, and in newer repositories the mapper would be specified by a RepositoryCfg stored at the uri 

location. 

 

.. warning:: 

 

Storage is 'wet paint' and very likely to change during factorization of Butler back end and 

storage formats (DM-6225). Use of it in production code other than via the 'old butler' API is 

strongly discouraged. 

 

""" 

ret = None 

parseRes = urllib.parse.urlparse(uri) 

if parseRes.scheme in Storage.storages: 

ret = Storage.storages[parseRes.scheme].getMapperClass(uri) 

else: 

raise RuntimeError("No storage registered for scheme %s" % parseRes.scheme) 

return ret 

 

@staticmethod 

def makeFromURI(uri, create=True): 

'''Instantiate a StorageInterface sublcass from a URI. 

 

.. warning:: 

 

Storage is 'wet paint' and very likely to change during factorization of Butler back end and 

storage formats (DM-6225). Use of it in production code other than via the 'old butler' API is 

strongly discouraged. 

 

Parameters 

---------- 

uri : string 

The uri to the root location of a repository. 

create : bool, optional 

If True The StorageInterface subclass should create a new 

repository at the root location. If False then a new repository 

will not be created. 

 

Returns 

------- 

A Storage subclass instance, or if create is False and a repository 

does not exist at the root location then returns None. 

 

Raises 

------ 

RuntimeError 

When a StorageInterface subclass does not exist for the scheme 

indicated by the uri. 

''' 

ret = None 

parseRes = urllib.parse.urlparse(uri) 

if parseRes.scheme in Storage.storages: 

theClass = Storage.storages[parseRes.scheme] 

try: 

ret = theClass(uri=uri, create=create) 

except NoRepositroyAtRoot: 

pass 

else: 

raise RuntimeError("No storage registered for scheme %s" % parseRes.scheme) 

return ret 

 

@staticmethod 

def isPosix(uri): 

"""Test if a URI is for a local filesystem storage. 

 

This is mostly for backward compatibility; Butler V1 repositories were only ever on the local 

filesystem. They may exist but not have a RepositoryCfg class. This enables conditional checking for a 

V1 Repository. 

 

This function treats 'file' and '' (no scheme) as posix storages, see 

the class docstring for more details. 

 

Parameters 

---------- 

uri : string 

URI to the root of a Repository. 

 

Returns 

------- 

Bool 

True if the URI is associated with a posix storage, else false. 

""" 

parseRes = urllib.parse.urlparse(uri) 

if parseRes.scheme in ('file', ''): 

return True 

return False 

 

@staticmethod 

def relativePath(fromUri, toUri): 

"""Get a relative path from a location to a location, if a relative path for these 2 locations exists. 

 

Parameters 

---------- 

fromPath : string 

A URI that describes a location at which to start. 

toPath : string 

A URI that describes a target location. 

 

Returns 

------- 

string 

A relative path that describes the path from fromUri to toUri, provided one exists. If a relative 

path between the two URIs does not exist then the entire toUri path is returned. 

""" 

fromUriParseRes = urllib.parse.urlparse(fromUri) 

toUriParseRes = urllib.parse.urlparse(toUri) 

if fromUriParseRes.scheme != toUriParseRes.scheme: 

return toUri 

storage = Storage.storages.get(fromUriParseRes.scheme, None) 

if not storage: 

return toUri 

return storage.relativePath(fromUri, toUri) 

 

@staticmethod 

def absolutePath(fromUri, toUri): 

"""Get an absolute path for the path from fromUri to toUri 

 

Parameters 

---------- 

fromUri : the starting location 

Description 

toUri : the location relative to fromUri 

Description 

 

Returns 

------- 

string 

URI that is absolutepath fromUri + toUri, if one exists. If toUri is absolute or if fromUri is not 

related to toUri (e.g. are of different storage types) then toUri will be returned. 

""" 

fromUriParseRes = urllib.parse.urlparse(fromUri) 

toUriParseRes = urllib.parse.urlparse(toUri) 

if fromUriParseRes.scheme != toUriParseRes.scheme: 

return toUri 

storage = Storage.storages.get(fromUriParseRes.scheme, None) 

if not storage: 

return toUri 

return storage.absolutePath(fromUri, toUri) 

 

@staticmethod 

def search(uri, path): 

"""Look for the given path in a storage root at URI; return None if it can't be found. 

 

If the path contains an HDU indicator (a number in brackets before the 

dot, e.g. 'foo.fits[1]', this will be stripped when searching and so 

will match filenames without the HDU indicator, e.g. 'foo.fits'. The 

path returned WILL contain the indicator though, e.g. ['foo.fits[1]']. 

 

 

Parameters 

---------- 

root : string 

URI to the the root location to search 

path : string 

A filename (and optionally prefix path) to search for within root. 

 

Returns 

------- 

string or None 

The location that was found, or None if no location was found. 

""" 

parseRes = urllib.parse.urlparse(uri) 

storage = Storage.storages.get(parseRes.scheme, None) 

if storage: 

return storage.search(uri, path) 

return None 

 

@staticmethod 

def storageExists(uri): 

"""Ask if a storage at the location described by uri exists 

 

Parameters 

---------- 

root : string 

URI to the the root location of the storage 

 

Returns 

------- 

bool 

True if the storage exists, false if not 

""" 

parseRes = urllib.parse.urlparse(uri) 

storage = Storage.storages.get(parseRes.scheme, None) 

if storage: 

return storage.storageExists(uri) 

return None