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

# coding=utf-8 

# 

# Copyright (C) 2010-2012 Platform Computing 

# 

# This library is free software; you can redistribute it and/or 

# modify it under the terms of the GNU Lesser General Public 

# License as published by the Free Software Foundation; either 

# version 2.1 of the License, or (at your option) any later version. 

# 

# This library 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 

# Lesser General Public License for more details. 

# 

# You should have received a copy of the GNU Lesser General Public 

# License along with this library; if not, write to the Free Software 

# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA 

# 

''' 

Backends should derive from this class. 

 

Created on Jun 27, 2011 

 

@author: tmetsch 

''' 

 

 

def is_related_valid(link): 

    ''' 

    Verifies that a link is consistent. 

 

    link -- The link which should be verified. 

    ''' 

    if link.target.kind == link.kind.related: 

        return True 

    else: 

        return False 

 

 

def is_attr_mutable(category, attribute_name): 

    ''' 

    Checks if an attribute is mutable. Return True if so. 

 

    category -- The category which defines the attribute. 

    attribute_name -- The name of the attribute. 

    ''' 

    if category.attributes[attribute_name] == 'mutable': 

        return True 

    else: 

        return False 

 

 

def is_attr_required(category, attribute_name): 

    ''' 

    Checks if an attribute is required. Return True if so. 

 

    category -- The category which defines the attribute. 

    attribute_name -- The name of the attribute. 

    ''' 

    if category.attributes[attribute_name] == 'required': 

        return True 

    else: 

        return False 

 

 

def is_action_applicable(entity, action): 

    ''' 

    Returns true if the action is currently applicable. 

 

    entity -- The current entity. 

    action -- The action to test for. 

    ''' 

    if action in entity.actions and action in entity.kind.actions: 

        return True 

    else: 

        return False 

 

 

class KindBackend(object): 

    ''' 

    A prototype backend which essentially does nothing. 

 

    Use this Backend for your Resource and Link types. 

 

    Note that it can happen that an entity has more than one backend assigned 

    (this is the case when it has a couple of mixins). But you can assign 

    multiple kinds to one backend. 

    ''' 

 

    def create(self, entity, extras): 

        ''' 

        Call the Resource Management and create this entity. 

 

        entity -- The entity which is to be created. 

        extras -- Any extra arguments which are defined by the user. 

        ''' 

        pass 

 

    def retrieve(self, entity, extras): 

        ''' 

        Call the Resource Management and refresh this entity so the client gets 

        up to date information. 

 

        entity -- The entity which is to be retrieved. 

        extras -- Any extra arguments which are defined by the user. 

        ''' 

        pass 

 

    def update(self, old, new, extras): 

        ''' 

        Call the Resource Management and update this entity. 

 

        It is up to the backend implementation to decide which information from 

        new if copied into old. 

 

        Note that the new entity might no have all the information the old one 

        had. 

 

        old -- The old entity which is to be updated. 

        new -- The new entity holding the updated information. 

        extras -- Any extra arguments which are defined by the user. 

        ''' 

        pass 

 

    def replace(self, old, new, extras): 

        ''' 

        Call the Resource Management and update this entity. This is 

        essentially a full update (Which allows removal of attributes for 

        example). 

 

        It is up to the backend implementation to decide which information from 

        new if copied into old. So if you really want to replace old with new 

        you need to have an old.* = new.* somewhere here (RECOMMENDED). 

 

        old -- The old entity which is to be updated. 

        new -- The new entity holding the updated information. 

        extras -- Any extra arguments which are defined by the user. 

        ''' 

        pass 

 

    def delete(self, entity, extras): 

        ''' 

        Call the Resource Management and delete this entity. 

 

        entity -- The entity which is to be deleted. 

        extras -- Any extra arguments which are defined by the user. 

        ''' 

        pass 

 

 

class ActionBackend(object): 

    ''' 

    A prototype backend which essentially does nothing. 

 

    Use this Backend for Action types. 

    ''' 

 

    # disabling 'Too few public...' pylint check (this is for extension) 

    # pylint: disable=R0903 

 

    def action(self, entity, action, attributes, extras): 

        ''' 

        Call the Resource Management and perform this action. 

 

        entity -- The entity on which the action is going to be performed. 

        action -- The action category definition. 

        attributes -- The acctributes for this action. 

        extras -- Any extra arguments which are defined by the user. 

        ''' 

        pass 

 

 

class MixinBackend(KindBackend): 

    ''' 

    A prototype backend which essentially does nothing. 

 

    Use this Backend for Mixin types. 

    ''' 

 

    pass 

 

 

class UserDefinedMixinBackend(MixinBackend): 

    ''' 

    A Backend for mixins defined by users. 

 

    DO NOT USE THIS CLASS IN YOUR IMPLEMENTATION! 

    ''' 

 

    pass