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

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

#!/usr/bin/env python 

 

# 

# LSST Data Management System 

# Copyright 2017 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/>. 

# 

from abc import ABCMeta, abstractmethod 

 

 

class NoRepositroyAtRoot(RuntimeError): 

pass 

 

 

class StorageInterface: 

"""Defines the interface for a connection to a Storage location. 

 

Parameters 

---------- 

uri : string 

URI or path that is used as the storage location. 

create : bool 

If True The StorageInterface subclass should create a new 

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

will not be created. 

 

Raises 

------ 

NoRepositroyAtRoot 

If create is False and a repository does not exist at the root 

specified by uri then NoRepositroyAtRoot is raised. 

""" 

__metaclass__ = ABCMeta 

 

def __init__(self, uri, create): 

"""initialzer""" 

pass 

 

@classmethod 

def _readFormatters(cls): 

"""Getter for the container of read formatters of a StorageInterface subclass. 

 

Returns 

------- 

dict 

The read formatters container belonging to the class type. 

""" 

try: 

return cls._readFormattersDict 

except AttributeError: 

cls._readFormattersDict = {} 

return cls._readFormattersDict 

 

@classmethod 

def _writeFormatters(cls): 

"""Getter for the container of write formatters of a StorageInterface subclass. 

 

Returns 

------- 

dict 

The write formatters container belonging to the class type. 

""" 

try: 

return cls._writeFormattersDict 

except AttributeError: 

cls._writeFormattersDict = {} 

return cls._writeFormattersDict 

 

@classmethod 

def getReadFormatter(cls, objType): 

"""Search in the registered formatters for the objType read formatter. 

 

Parameters 

---------- 

objType : class type 

The type of class to find a formatter for. 

 

Returns 

------- 

formatter callable 

The formatter callable used to read the object from the storageInterface. 

""" 

return cls._readFormatters().get(objType, None) 

 

@classmethod 

def getWriteFormatter(cls, objType): 

"""Search in the registered formatters for the objType write formatter. 

 

Parameters 

---------- 

objType : class type 

The type of class to find a formatter for. 

 

Returns 

------- 

formatter callable 

The formatter callable used to write the object to the storageInterface. 

""" 

return cls._writeFormatters().get(objType, None) 

 

@classmethod 

def registerFormatters(cls, formatable, readFormatter=None, writeFormatter=None): 

"""Register read and/or write formatters for a storageInterface subclass 

 

Parameters 

---------- 

cls : StorageInterface subclass 

The type of StorageInterface the formatter is being registered for. 

formatable : class object 

The class object whose instances can be formatted by the formatter. 

readFormatter : a read formatter callable 

The formatter function that can be used by the StorageInterface instance to read the object from 

the storage. 

writeFormatter : a write formatter callable 

The formatter function that can be used by the StorageInterface instance to write the object to 

the storage. 

 

Raises 

------ 

RuntimeError 

For each object type and StorageInterface subclass the read and write formatters should only be 

registered once. If a second registration occurs for either a RuntimeError is raised. 

""" 

def register(formatable, formatter, formatters, storageInterface): 

141 ↛ 142line 141 didn't jump to line 142, because the condition on line 141 was never true if formatable in formatters: 

raise RuntimeError(("Registration of second formatter {} for formattable {} in " + 

" storageInterface {}").format(formatter, formatable, storageInterface)) 

formatters[formatable] = formatter 

 

146 ↛ 149line 146 didn't jump to line 149, because the condition on line 146 was never false if readFormatter: 

formatters = cls._readFormatters() 

register(formatable, readFormatter, formatters, cls) 

if writeFormatter: 

formatters = cls._writeFormatters() 

register(formatable, writeFormatter, formatters, cls) 

 

@abstractmethod 

def write(self, butlerLocation, obj): 

"""Writes an object to a location and persistence format specified by ButlerLocation 

 

Parameters 

---------- 

butlerLocation : ButlerLocation 

The location & formatting for the object to be written. 

obj : object instance 

The object to be written. 

""" 

 

@abstractmethod 

def read(self, butlerLocation): 

"""Read from a butlerLocation. 

 

Parameters 

---------- 

butlerLocation : ButlerLocation 

The location & formatting for the object(s) to be read. 

 

Returns 

------- 

A list of objects as described by the butler location. One item for 

each location in butlerLocation.getLocations() 

""" 

 

@abstractmethod 

def getLocalFile(self, path): 

"""Get a handle to a local copy of the file, downloading it to a 

temporary if needed. 

 

Parameters 

---------- 

path : string 

A path to the the file in storage, relative to root. 

 

Returns 

------- 

A handle to a local copy of the file. If storage is remote it will be 

a temporary file. If storage is local it may be the original file or 

a temporary file. The file name can be gotten via the 'name' property 

of the returned object. 

""" 

 

@abstractmethod 

def exists(self, location): 

"""Check if location exists. 

 

Parameters 

---------- 

location : ButlerLocation or string 

A a string or a ButlerLocation that describes the location of an 

object in this storage. 

 

Returns 

------- 

bool 

True if exists, else False. 

""" 

 

@abstractmethod 

def instanceSearch(self, path): 

"""Search for the given path in this storage instance. 

 

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 

---------- 

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. 

""" 

 

@classmethod 

@abstractmethod 

def search(cls, root, path): 

"""Look for the given path in the current root. 

 

Also supports searching for the path in Butler v1 repositories by 

following the Butler v1 _parent symlink 

 

If the path contains an HDU indicator (a number in brackets, 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 

The path to the root directory. 

path : string 

The path to the file within the root directory. 

 

Returns 

------- 

string or None 

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

""" 

 

@abstractmethod 

def copyFile(self, fromLocation, toLocation): 

"""Copy a file from one location to another on the local filesystem. 

 

Parameters 

---------- 

fromLocation : string 

Path and name of existing file. 

toLocation : string 

Path and name of new file. 

 

Returns 

------- 

None 

""" 

 

@abstractmethod 

def locationWithRoot(self, location): 

"""Get the full path to the location. 

 

Parameters 

---------- 

location : string 

Path to a location within the repository relative to repository 

root. 

 

Returns 

------- 

string 

Absolute path to to the locaiton within the repository. 

""" 

 

@classmethod 

@abstractmethod 

def getRepositoryCfg(cls, uri): 

"""Get a persisted RepositoryCfg 

 

Parameters 

---------- 

uri : URI or path to a RepositoryCfg 

Description 

 

Returns 

------- 

A RepositoryCfg instance or None 

""" 

 

@classmethod 

@abstractmethod 

def putRepositoryCfg(cls, cfg, loc=None): 

"""Serialize a RepositoryCfg to a location. 

 

When loc == cfg.root, the RepositoryCfg is to be written at the root 

location of the repository. In that case, root is not written, it is 

implicit in the location of the cfg. This allows the cfg to move from 

machine to machine without modification. 

 

Parameters 

---------- 

cfg : RepositoryCfg instance 

The RepositoryCfg to be serailized. 

loc : string, optional 

The URI location (can be relative path) to write the RepositoryCfg. 

If loc is None, the location will be read from the root parameter 

of loc. 

 

Returns 

------- 

None 

""" 

 

@classmethod 

@abstractmethod 

def getMapperClass(cls, root): 

"""Get the mapper class associated with a repository root. 

 

Parameters 

---------- 

root : string 

The location of a persisted RepositoryCfg is (new style repos). 

 

Returns 

------- 

A class object or a class instance, depending on the state of the 

mapper when the repository was created. 

""" 

 

# Optional: Only needs to work if relative paths are sensical on this 

# storage type and for the case where fromPath and toPath are of the same 

# storage type. 

@classmethod 

def relativePath(cls, fromPath, toPath): 

"""Get a relative path from a location to a location. 

 

Parameters 

---------- 

fromPath : string 

A path at which to start. It can be a relative path or an 

absolute path. 

toPath : string 

A target location. It can be a relative path or an absolute path. 

 

Returns 

------- 

string 

A relative path that describes the path from fromPath to toPath. 

""" 

return toPath 

 

# Optional: Only needs to work if relative paths and absolute paths are 

# sensical on this storage type and for the case where fromPath and toPath 

# are of the same storage type. 

@classmethod 

def absolutePath(cls, fromPath, relativePath): 

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

 

Parameters 

---------- 

fromPath : the starting location 

A location at which to start. It can be a relative path or an 

absolute path. 

relativePath : the location relative to fromPath 

A relative path. 

 

Returns 

------- 

string 

Path that is an absolute path representation of fromPath + 

relativePath, if one exists. If relativePath is absolute or if 

fromPath is not related to relativePath then relativePath will be 

returned. 

""" 

return relativePath