|
1 # |
|
2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 # All rights reserved. |
|
4 # This component and the accompanying materials are made available |
|
5 # under the terms of "Eclipse Public License v1.0" |
|
6 # which accompanies this distribution, and is available |
|
7 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 # |
|
9 # Initial Contributors: |
|
10 # Nokia Corporation - initial contribution. |
|
11 # |
|
12 # Contributors: |
|
13 # |
|
14 # Description: |
|
15 # |
|
16 |
|
17 class _CrmlObjectBase(object): |
|
18 """ |
|
19 Common utility base class for all CRML objects for implementing |
|
20 operations like repr(), copy() etc. |
|
21 """ |
|
22 |
|
23 # Variable names used in simple object equality comparison |
|
24 SIMPLE_EQ_VARNAMES = [] |
|
25 |
|
26 def __repr__(self): |
|
27 return "%s(**%r)" % (self.__class__.__name__, vars(self)) |
|
28 |
|
29 def __get_filtered_dict(self, source_dict, allowed_keys): |
|
30 result = {} |
|
31 for key in allowed_keys: |
|
32 if key in source_dict: |
|
33 result[key] = source_dict[key] |
|
34 return result |
|
35 |
|
36 def __eq__(self, other): |
|
37 # 1. Compare type |
|
38 if type(self) is not type(other): |
|
39 return False |
|
40 |
|
41 # 2. Compare all members that can be simply compared using == |
|
42 varnames = self.__class__.SIMPLE_EQ_VARNAMES |
|
43 dict_self = self.__get_filtered_dict(vars(self), varnames) |
|
44 dict_other = self.__get_filtered_dict(vars(other), varnames) |
|
45 if dict_self != dict_other: |
|
46 return False |
|
47 |
|
48 # 3. Do any extra comparing |
|
49 if not self._do_extra_eq_handling(other): |
|
50 return False |
|
51 |
|
52 return True |
|
53 |
|
54 def __ne__(self, other): |
|
55 return not (self == other) |
|
56 |
|
57 def _do_extra_eq_handling(self, other): |
|
58 """ |
|
59 If sub-classes need to do any extra handling in __eq__(), they |
|
60 should implement this method and do it here. |
|
61 @return: True if self is equal to other, False if not. |
|
62 """ |
|
63 return True |
|
64 |
|
65 def copy(self): |
|
66 """ |
|
67 Return a deep copy of this object. |
|
68 """ |
|
69 new_obj = self.__class__(**vars(self)) |
|
70 self._do_extra_copy_handling(new_obj) |
|
71 return new_obj |
|
72 |
|
73 def _do_extra_copy_handling(self, new_object): |
|
74 """ |
|
75 If sub-classes need to do any extra handling in copy(), they |
|
76 should implement this method and do it here. |
|
77 @param new_object: The new copied object, a shallow copy at this point. |
|
78 """ |
|
79 return |
|
80 |
|
81 class CrmlAccess(_CrmlObjectBase): |
|
82 SIMPLE_EQ_VARNAMES = ['cap_rd', 'cap_wr', 'sid_rd', 'sid_wr'] |
|
83 |
|
84 def __init__(self, **kwargs): |
|
85 self.cap_rd = kwargs.get('cap_rd') |
|
86 self.cap_wr = kwargs.get('cap_wr') |
|
87 self.sid_rd = kwargs.get('sid_rd') |
|
88 self.sid_wr = kwargs.get('sid_wr') |
|
89 |
|
90 |
|
91 class CrmlRepository(_CrmlObjectBase): |
|
92 SIMPLE_EQ_VARNAMES = ['uid_value', 'uid_name', 'owner', 'backup', 'rfs', 'access', 'keys', 'version'] |
|
93 |
|
94 def __init__(self, **kwargs): |
|
95 self.uid_value = kwargs.get('uid_value') |
|
96 self.uid_name = kwargs.get('uid_name') |
|
97 self.owner = kwargs.get('owner') |
|
98 self.backup = kwargs.get('backup', False) |
|
99 self.rfs = kwargs.get('rfs', False) |
|
100 self.access = kwargs.get('access', CrmlAccess()) |
|
101 self.keys = kwargs.get('keys', []) |
|
102 self.version = kwargs.get('version', '1') |
|
103 |
|
104 def _do_extra_copy_handling(self, new_object): |
|
105 new_keys = [] |
|
106 for key in new_object.keys: |
|
107 new_keys.append(key.copy()) |
|
108 new_object.keys = new_keys |
|
109 |
|
110 self.access = new_object.access.copy() |
|
111 |
|
112 def get_refs(self): |
|
113 result = [] |
|
114 for key in self.keys: |
|
115 result.extend(key.get_refs()) |
|
116 return result |
|
117 |
|
118 class CrmlKeyBase(object): |
|
119 def __init__(self, **kwargs): |
|
120 self.ref = kwargs.get('ref') |
|
121 self.name = kwargs.get('name') |
|
122 self.backup = kwargs.get('backup', False) |
|
123 self.read_only = kwargs.get('read_only', False) |
|
124 self.access = kwargs.get('access', CrmlAccess()) |
|
125 |
|
126 class CrmlSimpleKey(CrmlKeyBase, _CrmlObjectBase): |
|
127 SIMPLE_EQ_VARNAMES = ['ref', 'name', 'int', 'type', 'backup', 'read_only', 'access'] |
|
128 |
|
129 def __init__(self, **kwargs): |
|
130 CrmlKeyBase.__init__(self, **kwargs) |
|
131 try: |
|
132 self.ref = kwargs['ref'] |
|
133 self.int = kwargs['int'] |
|
134 self.type = kwargs.get('type', 'int') |
|
135 except KeyError, e: |
|
136 raise ValueError("Mandatory argument '%s' not given!" % e.message) |
|
137 |
|
138 def _do_extra_copy_handling(self, new_object): |
|
139 self.access = new_object.access.copy() |
|
140 |
|
141 def get_refs(self): |
|
142 return [self.ref] |
|
143 |
|
144 class CrmlBitmaskKey(CrmlKeyBase, _CrmlObjectBase): |
|
145 SIMPLE_EQ_VARNAMES = ['int', 'type', 'backup', 'read_only', 'access', 'name', 'bits'] |
|
146 |
|
147 def __init__(self, **kwargs): |
|
148 CrmlKeyBase.__init__(self, **kwargs) |
|
149 try: |
|
150 self.int = kwargs['int'] |
|
151 self.type = kwargs.get('type', 'int') |
|
152 self.bits = kwargs.get('bits', []) |
|
153 except KeyError, e: |
|
154 raise ValueError("Mandatory argument '%s' not given!" % e.message) |
|
155 |
|
156 def _do_extra_copy_handling(self, new_object): |
|
157 new_bits = [] |
|
158 for bit in new_object.bits: |
|
159 new_bits.append(bit.copy()) |
|
160 new_object.bits = new_bits |
|
161 |
|
162 self.access = new_object.access.copy() |
|
163 |
|
164 def get_refs(self): |
|
165 return [bit.ref for bit in self.bits] |
|
166 |
|
167 class CrmlBit(_CrmlObjectBase): |
|
168 SIMPLE_EQ_VARNAMES = ['ref', 'index', 'invert'] |
|
169 |
|
170 def __init__(self, **kwargs): |
|
171 try: |
|
172 self.ref = kwargs['ref'] |
|
173 self.index = kwargs['index'] |
|
174 self.invert = kwargs.get('invert', False) |
|
175 except KeyError, e: |
|
176 raise ValueError("Mandatory argument '%s' not given!" % e.message) |
|
177 |
|
178 class CrmlKeyRange(CrmlKeyBase, _CrmlObjectBase): |
|
179 SIMPLE_EQ_VARNAMES = ['ref', 'name', 'first_int', 'last_int', 'first_index', 'index_bits', 'count_int', 'backup', 'read_only', 'access', 'subkeys'] |
|
180 |
|
181 def __init__(self, **kwargs): |
|
182 CrmlKeyBase.__init__(self, **kwargs) |
|
183 try: |
|
184 self.first_int = kwargs['first_int'] |
|
185 self.last_int = kwargs['last_int'] |
|
186 self.first_index = kwargs.get('first_index', 0) |
|
187 self.index_bits = kwargs.get('index_bits') |
|
188 self.count_int = kwargs.get('count_int') |
|
189 self.subkeys = kwargs.get('subkeys', []) |
|
190 except KeyError, e: |
|
191 raise ValueError("Mandatory argument '%s' not given!" % e.message) |
|
192 |
|
193 def _do_extra_copy_handling(self, new_object): |
|
194 new_subkeys = [] |
|
195 for subkey in new_object.subkeys: |
|
196 new_subkeys.append(subkey.copy()) |
|
197 new_object.subkeys = new_subkeys |
|
198 |
|
199 self.access = new_object.access.copy() |
|
200 |
|
201 def get_refs(self): |
|
202 if self.ref is not None: |
|
203 refs = [self.ref] |
|
204 for sk in self.subkeys: |
|
205 refs.append(self.ref + '.' + sk.ref) |
|
206 return refs |
|
207 else: |
|
208 return [] |
|
209 |
|
210 |
|
211 class CrmlKeyRangeSubKey(_CrmlObjectBase): |
|
212 SIMPLE_EQ_VARNAMES = ['ref', 'name', 'type', 'int'] |
|
213 |
|
214 def __init__(self, **kwargs): |
|
215 try: |
|
216 self.ref = kwargs['ref'] |
|
217 self.type = kwargs['type'] |
|
218 self.int = kwargs['int'] |
|
219 self.name = kwargs.get('name') |
|
220 except KeyError, e: |
|
221 raise ValueError("Mandatory argument '%s' not given!" % e.message) |
|
222 |