0
|
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 |
import StringIO
|
|
18 |
import os
|
|
19 |
import pickle
|
|
20 |
import copy
|
|
21 |
import logging
|
|
22 |
|
|
23 |
from cone.public import *
|
|
24 |
from cone.storage import persistentdictionary
|
|
25 |
|
|
26 |
|
|
27 |
class _StringStorageObject(container.ObjectContainer):
|
|
28 |
def __init__(self, name):
|
|
29 |
container.ObjectContainer.__init__(self, utils.resourceref.to_dottedref(name))
|
|
30 |
self.path = name
|
|
31 |
self.data = ""
|
|
32 |
|
|
33 |
def get_path(self):
|
|
34 |
"""
|
|
35 |
Return the path of the configuration resource
|
|
36 |
"""
|
|
37 |
return self.path
|
|
38 |
|
|
39 |
def set_path(self,path):
|
|
40 |
"""
|
|
41 |
Set the path of the configuration resource
|
|
42 |
"""
|
|
43 |
self.path = path
|
|
44 |
|
|
45 |
def path_to_elem(self, toparent=None):
|
|
46 |
parent_path = ""
|
|
47 |
# check if the parent is found at all from this hierarchy
|
|
48 |
if toparent and not self._find_parent_or_default(match=toparent):
|
|
49 |
toparent = self._find_parent_or_default(container=True)
|
|
50 |
if self._find_parent():
|
|
51 |
parent_path = self._find_parent()._path(toparent).replace(".","/")
|
|
52 |
return utils.resourceref.join_refs([parent_path,self.get_path()])
|
|
53 |
|
|
54 |
def __getstate__(self):
|
|
55 |
return self.__dict__.copy()
|
|
56 |
|
|
57 |
def __setstate__(self,dict):
|
|
58 |
self.__dict__ = dict.copy()
|
|
59 |
|
|
60 |
class StringStorage(api.Storage, container.ObjectContainer):
|
|
61 |
"""
|
|
62 |
A general base class for all storage type classes
|
|
63 |
@param path : the reference to the root of the storage.
|
|
64 |
"""
|
3
|
65 |
def __init__(self, path, mode='r'):
|
0
|
66 |
container.ObjectContainer.__init__(self,"")
|
3
|
67 |
api.Storage.__init__(self, path, mode)
|
|
68 |
|
|
69 |
def __reduce_ex__(self, protocol_version):
|
|
70 |
return super(container.ObjectContainer, self).__reduce_ex__(protocol_version)
|
0
|
71 |
|
|
72 |
def __getstate__(self):
|
|
73 |
dict = self.__dict__.copy()
|
|
74 |
del dict['__opened_res__']
|
|
75 |
return dict
|
|
76 |
|
|
77 |
def __setstate__(self,dict):
|
|
78 |
self.__dict__ = dict.copy()
|
|
79 |
self.__dict__['__opened_res__'] = {}
|
|
80 |
|
|
81 |
def __dump__(self):
|
|
82 |
"""
|
|
83 |
Dump the storage to the reference file
|
|
84 |
"""
|
|
85 |
file = open(self.get_path(),"w")
|
|
86 |
pickle.dump(self,file)
|
|
87 |
file.close()
|
|
88 |
|
|
89 |
@classmethod
|
|
90 |
def __open__(cls,path, mode="r"):
|
|
91 |
if mode.find("a")!=-1 or mode.find("r")!=-1:
|
|
92 |
if os.path.exists(path) and os.path.isfile(path):
|
|
93 |
file = open(path,"r")
|
|
94 |
obj = pickle.load(file)
|
|
95 |
file.close()
|
|
96 |
else:
|
|
97 |
raise exceptions.StorageException("The given data file for storage does not exist! %s" % path)
|
|
98 |
elif mode.find("w")!=-1:
|
|
99 |
# check if the given storage path exists and delete it if it does
|
|
100 |
if os.path.dirname(path) != '' and not os.path.exists(os.path.dirname(path)):
|
|
101 |
os.makedirs(os.path.dirname(path))
|
|
102 |
obj = StringStorage(path)
|
|
103 |
""" key value pairs of data. Key path = datastring """
|
|
104 |
else:
|
|
105 |
raise exceptions.StorageException("Unsupported creation mode given! %s" % mode)
|
|
106 |
return obj
|
|
107 |
|
|
108 |
@classmethod
|
|
109 |
def supported_storage(cls,path):
|
|
110 |
"""
|
|
111 |
Class method for determing if the given clas supports a storage by given path.
|
|
112 |
E.g. foo.zip, foo.cpd, foo/bar, http://foo.com/
|
|
113 |
@param path:
|
|
114 |
@return: Boolean value. True if the storage of the path is supported. False if not.
|
|
115 |
"""
|
|
116 |
if utils.resourceref.get_ext(path) == "pk":
|
|
117 |
return True
|
|
118 |
else:
|
|
119 |
return False
|
|
120 |
|
|
121 |
def close(self):
|
|
122 |
"""
|
|
123 |
Close the repository, which will save and close all open resources.
|
|
124 |
"""
|
|
125 |
super(StringStorage,self).close()
|
|
126 |
self.__dump__()
|
|
127 |
|
|
128 |
def save(self):
|
|
129 |
"""
|
|
130 |
Save changes from all resources to the repository.
|
|
131 |
"""
|
|
132 |
super(StringStorage,self).save()
|
|
133 |
self.__dump__()
|
|
134 |
|
|
135 |
def open_resource(self,path,mode="r"):
|
|
136 |
"""
|
|
137 |
Open the given resource and return a File object.
|
|
138 |
@param path : reference to the resource
|
|
139 |
@param mode : the mode in which to open. Can be one of r = read, w = write, a = append.
|
|
140 |
raises a NotResource exception if the path item is not a resource.
|
|
141 |
"""
|
|
142 |
res = None
|
|
143 |
# Add the current path in front of the given path
|
|
144 |
path = utils.resourceref.join_refs([self.get_current_path(), path])
|
|
145 |
dottedref = utils.resourceref.to_dref(path)
|
|
146 |
(pathto,name)= utils.resourceref.psplit_ref(path)
|
|
147 |
(dpath, dref) = utils.dottedref.psplit_ref(dottedref)
|
|
148 |
|
|
149 |
# check for existence
|
|
150 |
if self.get_mode(mode) == self.MODE_READ:
|
|
151 |
try:
|
|
152 |
# Try to create a new StringResource in any case
|
|
153 |
|
|
154 |
res = StringResource(self, path, self._get(dottedref).data,mode)
|
|
155 |
except exceptions.NotFound,e:
|
|
156 |
raise exceptions.NotResource("Not found %s" % path)
|
|
157 |
elif self.get_mode(mode) == self.MODE_WRITE:
|
|
158 |
# Create a new StringResource in any case
|
|
159 |
self._add_to_path(dpath,_StringStorageObject(name))
|
|
160 |
res = StringResource(self, path, self._get(dottedref).data,mode)
|
|
161 |
elif self.get_mode(mode) == self.MODE_APPEND:
|
|
162 |
# Append case, create the data reference if it is not existing
|
|
163 |
if not self._has(dottedref):
|
|
164 |
self._add_to_path(dpath,_StringStorageObject(name))
|
|
165 |
# Create a new StringResource in any case
|
|
166 |
res = StringResource(self, path, self._get(dottedref).data,mode)
|
|
167 |
res.seek(0, os.SEEK_END)
|
|
168 |
self.__opened__(res)
|
|
169 |
return res
|
|
170 |
|
|
171 |
def delete_resource(self,path):
|
|
172 |
"""
|
|
173 |
Delete the given resource from storage
|
|
174 |
@param res : Resource objcet to the resource
|
|
175 |
raises a NotSupportedException exception if delete operation is not supported by the storage
|
|
176 |
"""
|
|
177 |
# First close all open resources
|
|
178 |
# Add the current path in front of the given path
|
|
179 |
path = utils.resourceref.join_refs([self.curpath, path])
|
|
180 |
for res in self.__get_open__(path):
|
|
181 |
self.__closed__(res)
|
|
182 |
self._remove(utils.resourceref.to_dref(path))
|
|
183 |
|
|
184 |
def close_resource(self, res):
|
|
185 |
"""
|
|
186 |
Close the given resource instance. Normally this is called by the Resource object
|
|
187 |
in its own close.
|
|
188 |
@param res: the resource object to close.
|
|
189 |
"""
|
|
190 |
try:
|
|
191 |
self.__closed__(res)
|
|
192 |
if not res.get_mode() == api.Storage.MODE_READ:
|
|
193 |
self._get(utils.resourceref.to_dref(res.path)).data = res.getvalue()
|
|
194 |
except KeyError,e:
|
3
|
195 |
raise exceptions.StorageException("No such %s open resource! %s" % (res.path,e))
|
0
|
196 |
|
|
197 |
|
|
198 |
def save_resource(self, res):
|
|
199 |
"""
|
|
200 |
Flush the changes of a given resource instance. Normally this is called by the Resource object
|
|
201 |
in its own save.
|
|
202 |
@param res: the resource to the resource to save.
|
|
203 |
"""
|
|
204 |
if not self.__has_resource__(res):
|
|
205 |
raise exceptions.NotResource("No such %s open resource!" % res.path)
|
|
206 |
else:
|
|
207 |
if not res.get_mode() == api.Storage.MODE_READ:
|
|
208 |
self._get(utils.resourceref.to_dref(res.path)).data = res.getvalue()
|
|
209 |
|
|
210 |
def is_resource(self,path):
|
|
211 |
"""
|
|
212 |
Return true if the path is a resource
|
|
213 |
@param path : reference to path where resources are searched
|
|
214 |
"""
|
|
215 |
# Add the current path in front of the given path
|
|
216 |
path = utils.resourceref.join_refs([self.get_current_path(), path])
|
|
217 |
return self._has(utils.resourceref.to_dref(path))
|
|
218 |
|
3
|
219 |
def list_resources(self, path, **kwargs):
|
0
|
220 |
"""
|
|
221 |
find the resources under certain path/path
|
|
222 |
@param path : reference to path where resources are searched
|
|
223 |
@param recurse : defines whether to return resources directly under the path or does the listing recurse to subfolders.
|
|
224 |
Default value is False. Set to True to enable recursion.
|
|
225 |
"""
|
|
226 |
""" Get the given curpath element """
|
|
227 |
try:
|
|
228 |
curelem = self._get(utils.resourceref.to_dref(self.get_current_path()))
|
|
229 |
dref = utils.resourceref.to_dref(path)
|
3
|
230 |
elems = sorted([child.path_to_elem(curelem) for child in curelem._get(dref)._objects(type=_StringStorageObject)])
|
|
231 |
if kwargs.get('recurse', False):
|
|
232 |
# Recursively call list_resources to subelements that are of 'folder' type
|
|
233 |
folders = [child._name for child in curelem._get(dref)._objects() if child.__class__ == container.ObjectContainer]
|
|
234 |
for folderpath in sorted(folders):
|
|
235 |
elems += self.list_resources(utils.resourceref.join_refs([path,folderpath]), **kwargs)
|
|
236 |
return elems
|
0
|
237 |
except exceptions.NotFound:
|
|
238 |
return []
|
|
239 |
|
3
|
240 |
def import_resources(self, paths, storage, **kwargs):
|
0
|
241 |
for path in paths:
|
|
242 |
if not storage.is_resource(path):
|
|
243 |
logging.getLogger('cone').warning("The given path is not a Resource in the storage %s! Ignoring from export!" % path)
|
|
244 |
continue
|
|
245 |
wres = self.open_resource(path,'wb')
|
|
246 |
res = storage.open_resource(path,"rb")
|
|
247 |
wres.write(res.read())
|
|
248 |
wres.close()
|
|
249 |
res.close()
|
|
250 |
|
|
251 |
|
|
252 |
def create_folder(self,path):
|
|
253 |
"""
|
|
254 |
Create a folder entry to a path
|
|
255 |
@param path : path to the folder
|
|
256 |
"""
|
|
257 |
if not self._has(utils.resourceref.to_dref(path)):
|
|
258 |
(dpath,name) = utils.dottedref.psplit_ref(utils.resourceref.to_dref(path))
|
|
259 |
self._add_to_path(dpath, self._default_object(name))
|
|
260 |
|
|
261 |
def delete_folder(self,path):
|
|
262 |
"""
|
|
263 |
Delete a folder entry from a path. The path must be empty.
|
|
264 |
@param path : path to the folder
|
|
265 |
"""
|
|
266 |
self._remove(utils.resourceref.to_dref(path))
|
|
267 |
|
|
268 |
def is_folder(self,path):
|
|
269 |
"""
|
|
270 |
Check if the given path is an existing folder in the storage
|
|
271 |
@param path : path to the folder
|
|
272 |
"""
|
|
273 |
return self._has(utils.resourceref.to_dref(path))
|
|
274 |
|
|
275 |
def export_resources(self,refs,storage,empty_folders=False):
|
|
276 |
"""
|
|
277 |
export resources from this storage based on a list of reference to this storage
|
|
278 |
@param refs : a list of resource names in this storage (references).
|
|
279 |
@param storage : the external storage where to export.
|
|
280 |
"""
|
|
281 |
storage.import_resources(refs, self, empty_folders=empty_folders)
|
|
282 |
|
|
283 |
def unload(self, path, object):
|
|
284 |
"""
|
|
285 |
Dump a given object to the storage (reference is fetched from the object)
|
|
286 |
@param object: The object to dump to the storage, which is expected to be an instance
|
|
287 |
of Base class.
|
|
288 |
"""
|
|
289 |
# Add the current path in front of the given path
|
|
290 |
path = utils.resourceref.join_refs([self.get_current_path(), path])
|
|
291 |
if not isinstance(object, api.Configuration):
|
|
292 |
raise exceptions.StorageException("Cannot dump object type %s" % object.__class__)
|
|
293 |
res = self.open_resource(path,"w")
|
|
294 |
data = persistentdictionary.dumps(object)
|
|
295 |
res.write(data)
|
|
296 |
res.close()
|
|
297 |
return
|
|
298 |
|
|
299 |
def load(self, path):
|
|
300 |
"""
|
|
301 |
Load an from a reference.
|
|
302 |
"""
|
|
303 |
# Add the current path in front of the given path
|
|
304 |
path = utils.resourceref.join_refs([self.get_current_path(), path])
|
|
305 |
if not utils.resourceref.get_ext(path) == "confml":
|
|
306 |
raise exceptions.StorageException("Cannot load object from given path = %s!" % path)
|
|
307 |
if self.is_resource(path):
|
|
308 |
res = self.open_resource(path,"r")
|
|
309 |
# read the dictionary from the resource with eval
|
|
310 |
obj = persistentdictionary.loads(res.read())
|
|
311 |
res.close()
|
|
312 |
return obj
|
|
313 |
else:
|
|
314 |
raise exceptions.NotResource("No such %s resource!" % path)
|
|
315 |
|
|
316 |
class StringResource(api.Resource):
|
|
317 |
"""
|
|
318 |
A StringResource class that works on top of StringIO buffer. This class in
|
|
319 |
intended mainly for testing purposes.
|
|
320 |
"""
|
|
321 |
def __init__(self,storage,path,stringdata, mode=api.Storage.MODE_READ):
|
|
322 |
strio = StringIO.StringIO(stringdata)
|
|
323 |
api.Resource.__init__(self,storage,path, mode)
|
|
324 |
self.handle = strio
|
|
325 |
self.read = self.handle.read
|
|
326 |
self.tell = self.handle.tell
|
|
327 |
self.seek = self.handle.seek
|
|
328 |
self.readline = self.handle.readline
|
|
329 |
self.getvalue = self.handle.getvalue
|
|
330 |
|
|
331 |
def write(self, string):
|
|
332 |
if self.get_mode() == api.Storage.MODE_READ:
|
|
333 |
raise exceptions.StorageException("Writing attempted to %s in read-only mode." % self.path)
|
|
334 |
else:
|
|
335 |
self.handle.write(string)
|
|
336 |
|
3
|
337 |
# def read(self, length=0):
|
|
338 |
# if self.get_mode() == api.Storage.MODE_WRITE:
|
|
339 |
# raise exceptions.StorageException("Reading attempted to %s in write-only mode." % self.path)
|
|
340 |
# else:
|
|
341 |
# self.handle.read(length)
|
0
|
342 |
|
|
343 |
def save(self):
|
|
344 |
self.storage.save_resource(self)
|
|
345 |
|
|
346 |
def close(self):
|
|
347 |
self.storage.close_resource(self)
|
|
348 |
self.handle.close()
|
|
349 |
|
|
350 |
def get_size(self):
|
|
351 |
if self.get_mode() == api.Storage.MODE_WRITE:
|
|
352 |
raise exceptions.StorageException("Reading resource size attempted to %s in write-only mode." % self.path)
|
|
353 |
return len(self.handle.getvalue())
|
|
354 |
|
|
355 |
def get_content_info(self):
|
|
356 |
if self.content_info == None:
|
3
|
357 |
self.content_info = api.make_content_info(self, self.handle.getvalue())
|
0
|
358 |
|
|
359 |
return self.content_info
|