|
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 from cone.public import persistence, exceptions, api, utils |
|
18 |
|
19 def dumps(obj): |
|
20 return DictWriter().dumps(obj) |
|
21 |
|
22 def loads(dictstr): |
|
23 # convert the dict string with eval to actual dict |
|
24 return DictReader().loads(eval(dictstr)) |
|
25 |
|
26 class DictWriter(persistence.ConeWriter): |
|
27 """ |
|
28 """ |
|
29 |
|
30 def dumps(self, obj): |
|
31 """ |
|
32 @param obj: The object |
|
33 """ |
|
34 writer = get_writer_for_class(obj.__class__.__name__) |
|
35 return {obj.__class__.__name__: writer.dumps(obj)} |
|
36 |
|
37 |
|
38 class DictReader(persistence.ConeReader): |
|
39 """ |
|
40 """ |
|
41 class_type = "Dict" |
|
42 |
|
43 def loads(self, dict): |
|
44 """ |
|
45 @param dict: The dictianary which to read. reads only the first object. |
|
46 """ |
|
47 classname = dict.keys()[0] |
|
48 reader = get_reader_for_elem(classname) |
|
49 return reader.loads(dict) |
|
50 |
|
51 |
|
52 class GenericWriter(DictWriter): |
|
53 """ |
|
54 """ |
|
55 |
|
56 def dumps(self, obj): |
|
57 """ |
|
58 @param obj: The Configuration object |
|
59 """ |
|
60 dict = {'dict': todict(obj)} |
|
61 for child in obj._objects(): |
|
62 writer = get_writer_for_class(child.__class__.__name__) |
|
63 chd = writer.dumps(child) |
|
64 if not dict.has_key('children'): |
|
65 dict['children'] = [] |
|
66 dict['children'].append({child.__class__.__name__: chd}) |
|
67 return dict |
|
68 |
|
69 @classmethod |
|
70 def supported_class(cls, classname): |
|
71 """ |
|
72 Class method to determine if this DictWriter supports writing |
|
73 of the given class name |
|
74 """ |
|
75 try: |
|
76 cls = get_class(classname) |
|
77 return True |
|
78 except exceptions.IncorrectClassError: |
|
79 return False |
|
80 |
|
81 |
|
82 class GenericReader(DictReader): |
|
83 """ |
|
84 """ |
|
85 |
|
86 @classmethod |
|
87 def supported_elem(cls, elemname, parent=None): |
|
88 """ |
|
89 Class method to determine if this DictWriter supports reading |
|
90 of the given elem name |
|
91 """ |
|
92 try: |
|
93 cls = get_class(elemname) |
|
94 return True |
|
95 except exceptions.IncorrectClassError: |
|
96 return False |
|
97 |
|
98 def __init__(self): |
|
99 pass |
|
100 |
|
101 def loads(self, dict): |
|
102 """ |
|
103 @param obj: The Configuration object |
|
104 """ |
|
105 |
|
106 (classname,objdata) = dict.popitem() |
|
107 obj = get_class(classname)() |
|
108 objdict = objdata.get('dict',{}) |
|
109 for membername in objdict.keys(): |
|
110 try: |
|
111 setattr(obj, membername, objdict[membername]) |
|
112 except AttributeError: |
|
113 # read only attributes cannot be set |
|
114 pass |
|
115 obj._name = utils.resourceref.to_objref(obj.ref) |
|
116 |
|
117 #container.ObjectContainer.__init__(obj,utils.resourceref.to_dottedref(obj.ref)) |
|
118 for child in objdata.get('children',[]): |
|
119 classname = child.keys()[0] |
|
120 reader = get_reader_for_elem(classname) |
|
121 childobj = reader.loads(child) |
|
122 obj.add(childobj) |
|
123 return obj |
|
124 |
|
125 |
|
126 class ConfigurationProxyWriter(DictWriter): |
|
127 """ |
|
128 """ |
|
129 def dumps(self, obj): |
|
130 """ |
|
131 @param obj: The Configuration object |
|
132 """ |
|
133 dict = {'dict': todict(obj)} |
|
134 return dict |
|
135 |
|
136 @classmethod |
|
137 def supported_class(cls, classname): |
|
138 """ |
|
139 Class method to determine if this DictWriter supports writing |
|
140 of the given class name |
|
141 """ |
|
142 if classname=="ConfigurationProxy": |
|
143 return True |
|
144 else: |
|
145 return False |
|
146 |
|
147 |
|
148 class ConfigurationProxyReader(DictReader): |
|
149 """ |
|
150 """ |
|
151 def loads(self, dict): |
|
152 """ |
|
153 @param obj: The Configuration object |
|
154 """ |
|
155 (classname,objdata) = dict.popitem() |
|
156 obj = api.ConfigurationProxy("") |
|
157 obj.__dict__.update(objdata.get('dict',{})) |
|
158 obj.set('_name',utils.resourceref.to_objref(obj.path)) |
|
159 return obj |
|
160 |
|
161 @classmethod |
|
162 def supported_elem(cls, elemname, parent=None): |
|
163 """ |
|
164 Class method to determine if this DictWriter supports reading |
|
165 of the given elem name |
|
166 """ |
|
167 if elemname=="ConfigurationProxy": |
|
168 return True |
|
169 else: |
|
170 return False |
|
171 |
|
172 |
|
173 def get_class(elemname): |
|
174 """ |
|
175 Get a correct class from a element name. |
|
176 """ |
|
177 if elemname=="Configuration": |
|
178 return api.Configuration |
|
179 elif elemname=="CompositeConfiguration": |
|
180 return api.CompositeConfiguration |
|
181 elif elemname=="Feature": |
|
182 return api.Feature |
|
183 elif elemname=="Data": |
|
184 return api.Data |
|
185 elif elemname=="DataContainer": |
|
186 return api.DataContainer |
|
187 elif elemname=="Base": |
|
188 return api.Base |
|
189 else: |
|
190 raise exceptions.IncorrectClassError("Could not find a class for name %s!" % elemname) |
|
191 |
|
192 def get_reader_for_elem(classname): |
|
193 for reader in DictReader.__subclasses__(): |
|
194 if reader.supported_elem(classname): |
|
195 return reader() |
|
196 raise exceptions.ConePersistenceError("No reader for given class found!") |
|
197 |
|
198 def get_writer_for_class(classname): |
|
199 for writer in DictWriter.__subclasses__(): |
|
200 if writer.supported_class(classname): |
|
201 return writer () |
|
202 raise exceptions.ConePersistenceError("No writer for given class found! %s" % classname) |
|
203 |
|
204 def todict(obj): |
|
205 """ |
|
206 Helper function to push all non internal data to a dictionary |
|
207 """ |
|
208 dict = {} |
|
209 fromdict = obj._dict() |
|
210 for member in fromdict: |
|
211 if member.startswith("_"): |
|
212 # skip internals |
|
213 continue |
|
214 value = getattr(obj,member) |
|
215 if isinstance(value,str): |
|
216 dict[member] = value |
|
217 continue |
|
218 if isinstance(value,int): |
|
219 dict[member] = value |
|
220 continue |
|
221 return dict |
|
222 |