60 class StringStorage(api.Storage, container.ObjectContainer): |
60 class StringStorage(api.Storage, container.ObjectContainer): |
61 """ |
61 """ |
62 A general base class for all storage type classes |
62 A general base class for all storage type classes |
63 @param path : the reference to the root of the storage. |
63 @param path : the reference to the root of the storage. |
64 """ |
64 """ |
65 def __init__(self, path): |
65 def __init__(self, path, mode='r'): |
66 container.ObjectContainer.__init__(self,"") |
66 container.ObjectContainer.__init__(self,"") |
67 api.Storage.__init__(self,path) |
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) |
68 |
71 |
69 def __getstate__(self): |
72 def __getstate__(self): |
70 dict = self.__dict__.copy() |
73 dict = self.__dict__.copy() |
71 del dict['__opened_res__'] |
74 del dict['__opened_res__'] |
72 return dict |
75 return dict |
187 try: |
190 try: |
188 self.__closed__(res) |
191 self.__closed__(res) |
189 if not res.get_mode() == api.Storage.MODE_READ: |
192 if not res.get_mode() == api.Storage.MODE_READ: |
190 self._get(utils.resourceref.to_dref(res.path)).data = res.getvalue() |
193 self._get(utils.resourceref.to_dref(res.path)).data = res.getvalue() |
191 except KeyError,e: |
194 except KeyError,e: |
192 raise StorageException("No such %s open resource! %s" % (res.path,e)) |
195 raise exceptions.StorageException("No such %s open resource! %s" % (res.path,e)) |
193 |
196 |
194 |
197 |
195 def save_resource(self, res): |
198 def save_resource(self, res): |
196 """ |
199 """ |
197 Flush the changes of a given resource instance. Normally this is called by the Resource object |
200 Flush the changes of a given resource instance. Normally this is called by the Resource object |
211 """ |
214 """ |
212 # Add the current path in front of the given path |
215 # Add the current path in front of the given path |
213 path = utils.resourceref.join_refs([self.get_current_path(), path]) |
216 path = utils.resourceref.join_refs([self.get_current_path(), path]) |
214 return self._has(utils.resourceref.to_dref(path)) |
217 return self._has(utils.resourceref.to_dref(path)) |
215 |
218 |
216 def list_resources(self,path,recurse=False,empty_folders=False): |
219 def list_resources(self, path, **kwargs): |
217 """ |
220 """ |
218 find the resources under certain path/path |
221 find the resources under certain path/path |
219 @param path : reference to path where resources are searched |
222 @param path : reference to path where resources are searched |
220 @param recurse : defines whether to return resources directly under the path or does the listing recurse to subfolders. |
223 @param recurse : defines whether to return resources directly under the path or does the listing recurse to subfolders. |
221 Default value is False. Set to True to enable recursion. |
224 Default value is False. Set to True to enable recursion. |
222 """ |
225 """ |
223 """ Get the given curpath element """ |
226 """ Get the given curpath element """ |
224 try: |
227 try: |
225 curelem = self._get(utils.resourceref.to_dref(self.get_current_path())) |
228 curelem = self._get(utils.resourceref.to_dref(self.get_current_path())) |
226 dref = utils.resourceref.to_dref(path) |
229 dref = utils.resourceref.to_dref(path) |
227 if recurse: |
230 elems = sorted([child.path_to_elem(curelem) for child in curelem._get(dref)._objects(type=_StringStorageObject)]) |
228 return sorted([child.path_to_elem(curelem) for child in curelem._get(dref)._traverse(type=_StringStorageObject)]) |
231 if kwargs.get('recurse', False): |
229 else: |
232 # Recursively call list_resources to subelements that are of 'folder' type |
230 return sorted([child.path_to_elem(curelem) for child in curelem._get(dref)._objects(type=_StringStorageObject)]) |
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 |
231 except exceptions.NotFound: |
237 except exceptions.NotFound: |
232 return [] |
238 return [] |
233 |
239 |
234 def import_resources(self,paths,storage,empty_folders=False): |
240 def import_resources(self, paths, storage, **kwargs): |
235 for path in paths: |
241 for path in paths: |
236 if not storage.is_resource(path): |
242 if not storage.is_resource(path): |
237 logging.getLogger('cone').warning("The given path is not a Resource in the storage %s! Ignoring from export!" % path) |
243 logging.getLogger('cone').warning("The given path is not a Resource in the storage %s! Ignoring from export!" % path) |
238 continue |
244 continue |
239 wres = self.open_resource(path,'wb') |
245 wres = self.open_resource(path,'wb') |
326 if self.get_mode() == api.Storage.MODE_READ: |
332 if self.get_mode() == api.Storage.MODE_READ: |
327 raise exceptions.StorageException("Writing attempted to %s in read-only mode." % self.path) |
333 raise exceptions.StorageException("Writing attempted to %s in read-only mode." % self.path) |
328 else: |
334 else: |
329 self.handle.write(string) |
335 self.handle.write(string) |
330 |
336 |
331 def read(self, bytes=0): |
337 # def read(self, length=0): |
332 if self.get_mode() == api.Storage.MODE_WRITE: |
338 # if self.get_mode() == api.Storage.MODE_WRITE: |
333 raise exceptions.StorageException("Reading attempted to %s in write-only mode." % self.path) |
339 # raise exceptions.StorageException("Reading attempted to %s in write-only mode." % self.path) |
334 else: |
340 # else: |
335 self.handle.read(string) |
341 # self.handle.read(length) |
336 |
342 |
337 def save(self): |
343 def save(self): |
338 self.storage.save_resource(self) |
344 self.storage.save_resource(self) |
339 |
345 |
340 def close(self): |
346 def close(self): |
346 raise exceptions.StorageException("Reading resource size attempted to %s in write-only mode." % self.path) |
352 raise exceptions.StorageException("Reading resource size attempted to %s in write-only mode." % self.path) |
347 return len(self.handle.getvalue()) |
353 return len(self.handle.getvalue()) |
348 |
354 |
349 def get_content_info(self): |
355 def get_content_info(self): |
350 if self.content_info == None: |
356 if self.content_info == None: |
351 self.content_info = utils.make_content_info(self, self.handle.getvalue()) |
357 self.content_info = api.make_content_info(self, self.handle.getvalue()) |
352 |
358 |
353 return self.content_info |
359 return self.content_info |