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

# 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 .utils import getInstanceOf 

 

__all__ = ("MappingFactory", ) 

 

 

class MappingFactory: 

""" 

Register the mapping of some key to a python type and retrieve instances. 

 

Enables instances of these classes to be retrieved from the factory later. 

The class can be specified as an object, class or string. 

If the key is an object it is converted to a string by accessing 

a ``name`` attribute. 

 

Parameters 

---------- 

refType : `type` 

Python reference `type` to use to ensure that items stored in the 

registry create instance objects of the correct class. Subclasses 

of this type are allowed. Using `None` disables the check. 

 

""" 

 

def __init__(self, refType): 

self._registry = {} 

self.refType = refType 

 

def getFromRegistry(self, *targetClasses): 

"""Get a new instance of the object stored in the registry. 

 

Parameters 

---------- 

*targetClasses : `str` or objects supporting ``name`` attribute 

Each item is tested in turn until a match is found in the registry. 

Items with `None` value are skipped. 

 

Returns 

------- 

instance : `object` 

Instance of class stored in registry associated with the first 

matching target class. 

 

Raises 

------ 

KeyError 

None of the supplied target classes match an item in the registry. 

""" 

attempts = [] 

for t in (targetClasses): 

if t is None: 

attempts.append(t) 

else: 

key = self._getName(t) 

attempts.append(key) 

try: 

typeName = self._registry[key] 

except KeyError: 

pass 

else: 

return getInstanceOf(typeName) 

raise KeyError("Unable to find item in registry with keys: {}".format(attempts)) 

 

def placeInRegistry(self, registryKey, typeName): 

"""Register a class name with the associated type. 

 

Parameters 

---------- 

registryKey : `str` or object supporting ``name`` attribute. 

Item to associate with the provided type. 

typeName : `str` or Python type 

Identifies a class to associate with the provided key. 

 

Raises 

------ 

KeyError 

If item is already registered and has different value. 

""" 

keyString = self._getName(registryKey) 

if keyString in self._registry: 

# Compare the class strings since dynamic classes can be the 

# same thing but be different. 

if str(self._registry[keyString]) == str(typeName): 

return 

 

raise KeyError("Item with key {} already registered with different value" 

" ({} != {})".format(keyString, self._registry[keyString], typeName)) 

 

self._registry[keyString] = typeName 

 

@staticmethod 

def _getName(typeOrName): 

"""Extract name of supplied object as string. 

 

Parameters 

---------- 

typeOrName : `str` or object supporting ``name`` attribute. 

Item from which to extract a name. 

 

Returns 

------- 

name : `str` 

Extracted name as a string. 

""" 

if isinstance(typeOrName, str): 

return typeOrName 

elif hasattr(typeOrName, "name"): 

return typeOrName.name 

else: 

raise ValueError("Cannot extract name from type")