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

# This file is part of daf_butler. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

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

# See the COPYRIGHT file at the top-level directory of this distribution 

# for details of code ownership. 

# 

# 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 GNU General Public License 

# along with this program. If not, see <http://www.gnu.org/licenses/>. 

 

from __future__ import annotations 

 

__all__ = ["DatasetType"] 

 

from copy import deepcopy 

import re 

 

from types import MappingProxyType 

from ..storageClass import StorageClass, StorageClassFactory 

from ..dimensions import DimensionGraph 

from ..configSupport import LookupKey 

 

 

def _safeMakeMappingProxyType(data): 

if data is None: 

data = {} 

return MappingProxyType(data) 

 

 

class DatasetType: 

r"""A named category of Datasets that defines how they are organized, 

related, and stored. 

 

A concrete, final class whose instances represent `DatasetType`\ s. 

`DatasetType` instances may be constructed without a `Registry`, 

but they must be registered 

via `Registry.registerDatasetType()` before corresponding Datasets 

may be added. 

`DatasetType` instances are immutable. 

 

Parameters 

---------- 

name : `str` 

A string name for the Dataset; must correspond to the same 

`DatasetType` across all Registries. Names must start with an 

upper or lowercase letter, and may contain only letters, numbers, 

and underscores. Component dataset types should contain a single 

period separating the base dataset type name from the component name 

(and may be recursive). 

dimensions : `DimensionGraph` or iterable of `Dimension` 

Dimensions used to label and relate instances of this `DatasetType`. 

If not a `DimensionGraph`, ``universe`` must be provided as well. 

storageClass : `StorageClass` or `str` 

Instance of a `StorageClass` or name of `StorageClass` that defines 

how this `DatasetType` is persisted. 

universe : `DimensionUniverse`, optional 

Set of all known dimensions, used to normalize ``dimensions`` if it 

is not already a `DimensionGraph`. 

""" 

 

__slots__ = ("_name", "_dimensions", "_storageClass", "_storageClassName") 

 

VALID_NAME_REGEX = re.compile("^[a-zA-Z][a-zA-Z0-9_]*(\\.[a-zA-Z][a-zA-Z0-9_]*)*$") 

 

@staticmethod 

def nameWithComponent(datasetTypeName, componentName): 

"""Form a valid DatasetTypeName from a parent and component. 

 

No validation is performed. 

 

Parameters 

---------- 

datasetTypeName : `str` 

Base type name. 

componentName : `str` 

Name of component. 

 

Returns 

------- 

compTypeName : `str` 

Name to use for component DatasetType. 

""" 

return "{}.{}".format(datasetTypeName, componentName) 

 

def __init__(self, name, dimensions, storageClass, *, universe=None): 

if self.VALID_NAME_REGEX.match(name) is None: 

raise ValueError(f"DatasetType name '{name}' is invalid.") 

self._name = name 

if not isinstance(dimensions, DimensionGraph): 

if universe is None: 

raise ValueError("If dimensions is not a normalized DimensionGraph, " 

"a universe must be provided.") 

dimensions = universe.extract(dimensions) 

self._dimensions = dimensions 

assert isinstance(storageClass, (StorageClass, str)) 

if isinstance(storageClass, StorageClass): 

self._storageClass = storageClass 

self._storageClassName = storageClass.name 

else: 

self._storageClass = None 

self._storageClassName = storageClass 

 

def __repr__(self): 

return "DatasetType({}, {}, {})".format(self.name, self.dimensions, self._storageClassName) 

 

def __eq__(self, other): 

if self._name != other._name: 

return False 

if self._dimensions != other._dimensions: 

return False 

if self._storageClass is not None and other._storageClass is not None: 

return self._storageClass == other._storageClass 

else: 

return self._storageClassName == other._storageClassName 

 

def __hash__(self): 

"""Hash DatasetType instance. 

 

This only uses StorageClass name which is it consistent with the 

implementation of StorageClass hash method. 

""" 

return hash((self._name, self._dimensions, self._storageClassName)) 

 

@property 

def name(self): 

"""A string name for the Dataset; must correspond to the same 

`DatasetType` across all Registries. 

""" 

return self._name 

 

@property 

def dimensions(self): 

r"""The `Dimension`\ s that label and relate instances of this 

`DatasetType` (`DimensionGraph`). 

""" 

return self._dimensions 

 

@property 

def storageClass(self): 

"""`StorageClass` instance that defines how this `DatasetType` 

is persisted. Note that if DatasetType was constructed with a name 

of a StorageClass then Butler has to be initialized before using 

this property. 

""" 

if self._storageClass is None: 

self._storageClass = StorageClassFactory().getStorageClass(self._storageClassName) 

return self._storageClass 

 

@staticmethod 

def splitDatasetTypeName(datasetTypeName): 

"""Given a dataset type name, return the root name and the component 

name. 

 

Parameters 

---------- 

datasetTypeName : `str` 

The name of the dataset type, can include a component using 

a "."-separator. 

 

Returns 

------- 

rootName : `str` 

Root name without any components. 

componentName : `str` 

The component if it has been specified, else `None`. 

 

Notes 

----- 

If the dataset type name is ``a.b.c`` this method will return a 

root name of ``a`` and a component name of ``b.c``. 

""" 

comp = None 

root = datasetTypeName 

if "." in root: 

# If there is doubt, the component is after the first "." 

root, comp = root.split(".", maxsplit=1) 

return root, comp 

 

def nameAndComponent(self): 

"""Return the root name of this dataset type and the component 

name (if defined). 

 

Returns 

------- 

rootName : `str` 

Root name for this `DatasetType` without any components. 

componentName : `str` 

The component if it has been specified, else `None`. 

""" 

return self.splitDatasetTypeName(self.name) 

 

def component(self): 

"""Component name (if defined) 

 

Returns 

------- 

comp : `str` 

Name of component part of DatasetType name. `None` if this 

`DatasetType` is not associated with a component. 

""" 

_, comp = self.nameAndComponent() 

return comp 

 

def componentTypeName(self, component): 

"""Given a component name, derive the datasetTypeName of that component 

 

Parameters 

---------- 

component : `str` 

Name of component 

 

Returns 

------- 

derived : `str` 

Compound name of this `DatasetType` and the component. 

 

Raises 

------ 

KeyError 

Requested component is not supported by this `DatasetType`. 

""" 

if component in self.storageClass.components: 

return self.nameWithComponent(self.name, component) 

raise KeyError("Requested component ({}) not understood by this DatasetType".format(component)) 

 

def makeComponentDatasetType(self, component: str) -> DatasetType: 

"""Return a DatasetType suitable for the given component, assuming the 

same dimensions as the parent. 

 

Parameters 

---------- 

component : `str` 

Name of component 

 

Returns 

------- 

datasetType : `DatasetType` 

A new DatasetType instance. 

""" 

return DatasetType(self.componentTypeName(component), dimensions=self.dimensions, 

storageClass=self.storageClass.components[component]) 

 

def isComponent(self): 

"""Boolean indicating whether this `DatasetType` refers to a 

component of a composite. 

 

Returns 

------- 

isComponent : `bool` 

`True` if this `DatasetType` is a component, `False` otherwise. 

""" 

if self.component(): 

return True 

return False 

 

def isComposite(self): 

"""Boolean indicating whether this `DatasetType` is a composite type. 

 

Returns 

------- 

isComposite : `bool` 

`True` if this `DatasetType` is a composite type, `False` 

otherwise. 

""" 

return self.storageClass.isComposite() 

 

def _lookupNames(self): 

"""Name keys to use when looking up this datasetType in a 

configuration. 

 

The names are returned in order of priority. 

 

Returns 

------- 

names : `tuple` of `LookupKey` 

Tuple of the `DatasetType` name and the `StorageClass` name. 

If the name includes a component the name with the component 

is first, then the name without the component and finally 

the storage class name. 

""" 

rootName, componentName = self.nameAndComponent() 

lookups = (LookupKey(name=self.name),) 

if componentName is not None: 

lookups = lookups + (LookupKey(name=rootName),) 

 

if self.dimensions: 

# Dimensions are a lower priority than dataset type name 

lookups = lookups + (LookupKey(dimensions=self.dimensions),) 

 

return lookups + self.storageClass._lookupNames() 

 

def __reduce__(self): 

"""Support pickling. 

 

StorageClass instances can not normally be pickled, so we pickle 

StorageClass name instead of instance. 

""" 

return (DatasetType, (self.name, self.dimensions, self._storageClassName)) 

 

def __deepcopy__(self, memo): 

"""Support for deep copy method. 

 

Normally ``deepcopy`` will use pickle mechanism to make copies. 

We want to avoid that to support (possibly degenerate) use case when 

DatasetType is constructed with StorageClass instance which is not 

registered with StorageClassFactory (this happens in unit tests). 

Instead we re-implement ``__deepcopy__`` method. 

""" 

return DatasetType(name=deepcopy(self.name, memo), 

dimensions=deepcopy(self.dimensions, memo), 

storageClass=deepcopy(self._storageClass or self._storageClassName, memo))