def close(self):
        if self.zipfile:
            super(ZipStorage,self).close()
            self.zipfile.close()
            # Recreate the zip file if the zip has been modified to make a zip without 
            # duplicate local file entries
        else:
            raise exceptions.StorageException('Storage %s has been already closed!' % self.path)

        if self.modified or self.cache:
            logging.getLogger('cone').debug("Recreating the ZIP output file")
            oldfile = None
            newzipfile = None
            fh, tmp_path = tempfile.mkstemp(suffix='.zip')
            shutil.move(self.path, tmp_path)
            oldfile = zipfile.ZipFile(tmp_path,"r")
            newzipfile = zipfile.ZipFile(self.path,"w",self.compression)
            for fileinfo in oldfile.infolist():
                if fileinfo.filename not in self.cache.keys():
                    newzipfile.writestr(fileinfo, oldfile.read(fileinfo.filename))
            for filename in sorted(self.cache.keys()):
                logging.getLogger('cone').debug("Adding pre-cached file %s." % filename)
                newzipfile.write(self.cache[filename], arcname=filename)
            if oldfile: oldfile.close()
            if newzipfile: newzipfile.close()
            os.close(fh)
            os.unlink(tmp_path)
            logging.getLogger('cone').debug("Recreating the ZIP output file completed.")
        
        self.zipfile = None
 def __init__(self, path, mode="r", **kwargs):
     super(FileStorage, self).__init__(path, mode)
     logging.getLogger('cone').debug("FileStorage path %s" %
                                     self.get_path())
     self.persistentmodule = persistentconfml
     if mode.find("a") != -1 or mode.find("r") != -1:
         # check that the given folder exists and is a folder
         if not os.path.isdir(self.get_path()):
             raise exceptions.StorageException(
                 "The given data folder for storage does not exist! %s" %
                 self.get_path())
     elif mode.find("w") != -1:
         # check if the given folder exists and create it if it does not
         if not os.path.exists(os.path.abspath(self.get_path())):
             os.makedirs(self.get_path())
     else:
         raise exceptions.StorageException(
             "Unsupported creation mode given! %s" % mode)
 def get_size(self):
     if self.get_mode() == api.Storage.MODE_WRITE:
         raise exceptions.StorageException(
             "Reading size attempted to %s in write-only mode." % self.path)
     orig_pos = self.handle.tell()
     self.handle.seek(0, os.SEEK_END)
     try:
         return self.handle.tell()
     finally:
         self.handle.seek(orig_pos, os.SEEK_SET)
 def delete_folder(self, path):
     """
     Delete a folder entry from a path. The path must be empty.
     @param path : path to the folder
     """
     path = utils.resourceref.join_refs(
         [self.get_path(), self.get_current_path(), path])
     if os.path.isdir(path):
         os.rmdir(path)
     else:
         raise exceptions.StorageException("Not a folder %s" % path)
 def close_resource(self, res):
     """
     Close the given resource instance. Normally this is called by the Resource object 
     in its own close.
     @param res: the resource object to close. 
     """
     try:
         self.__closed__(res)
         if self.get_mode(self.mode) != api.Storage.MODE_READ and \
            (res.get_mode() == api.Storage.MODE_WRITE or res.get_mode() == api.Storage.MODE_APPEND):
             self.zipfile.writestr(res.path,res.getvalue())
     except KeyError,e:
         raise exceptions.StorageException("No such %s open resource! %s" % (res.path,e))
 def close_resource(self, res):
     """
     Close the given resource instance. Normally this is called by the Resource object 
     in its own close.
     @param res: the resource object to close. 
     """
     try:
         self.__closed__(res)
         #if not res.get_mode() == api.Storage.MODE_READ:
         #    self._get(utils.resourceref.to_dref(res.path)).data = res.getvalue()
     except KeyError, e:
         raise exceptions.StorageException("No such %s open resource! %s" %
                                           (res.path, e))
 def unload(self, path, object):
     """
     Dump a given object to the storage (reference is fetched from the object)
     @param object: The object to dump to the storage, which is expected to be an instance 
     of Base class.
     """
     # Add the current path in front of the given path
     path = utils.resourceref.join_refs([self.get_current_path(), path])
     if not isinstance(object, api.Configuration):
         raise exceptions.StorageException("Cannot dump object type %s" % object.__class__)
     if self.get_mode(self.mode) != api.Storage.MODE_READ:
         res = self.open_resource(path,"wb")
         data = "%s" % self.persistentmodule.dumps(object)
         res.write(data)
         res.close()
     return
 def load(self, path):
     """
     Load an from a reference.
     """
     # Add the current path in front of the given path
     path = utils.resourceref.join_refs([self.get_current_path(), path])
     if not utils.resourceref.get_ext(path) == "confml":
         raise exceptions.StorageException("Cannot load reference type %s" % utils.resourceref.get_ext(path))
     if self.is_resource(path):
         res = self.open_resource(path,"r")
         # read the resource with persistentmodule
         parsecontext.get_confml_context().current_file = path
         try:
             obj = self.persistentmodule.loads(res.read())
             obj.set_path(path)
             res.close()
             return obj
         except exceptions.ParseError,e:
             parsecontext.get_confml_context().handle_exception(e)
             #logging.getLogger('cone').error("Resource %s parsing failed with exception: %s" % (path,e))
             # returning an empty config in case of xml parsing failure.
             return api.Configuration(path)
 def get_size(self):
     if self.get_mode() == api.Storage.MODE_WRITE:
         raise exceptions.StorageException("Reading resource size attempted to %s in write-only mode." % self.path)
     return len(self.handle.getvalue())
 def write(self,string):
     if self.get_mode() == api.Storage.MODE_READ:
         raise exceptions.StorageException("Writing attempted to %s in read-only mode." % self.path)
     else:
         self.handle.write(string)