def open_resource(self,path,mode="r"): strio = None path = utils.resourceref.remove_begin_slash(path) fullpath = utils.resourceref.join_refs([self.get_current_path(),path]) try: if self.get_mode(mode) == self.MODE_READ: if not self.is_resource(fullpath): raise exceptions.NotResource("Resource is not found %s" % fullpath) bytes = self.zipfile.read(fullpath) strio = StringIO.StringIO(bytes) elif self.get_mode(mode) == self.MODE_APPEND: if not self.is_resource(fullpath): raise exceptions.NotResource("Resource is not found %s" % fullpath) bytes = self.zipfile.read(fullpath) # delete the "old" resource self.delete_resource(fullpath) strio = StringIO.StringIO(bytes) strio.seek(0, os.SEEK_END) elif self.get_mode(mode) == self.MODE_WRITE: if self.is_resource(fullpath): # delete the "old" resource self.delete_resource(fullpath) # Create a new string buffer because the resource is overwritten strio = StringIO.StringIO() else: raise ZipException("Unrecognized mode %s" % mode) res = ZipFileResource(self,fullpath,mode,strio) self.__opened__(res) return res except KeyError: raise exceptions.NotResource(path)
def delete_resource(self, path): if self.is_resource(path): try: path = utils.resourceref.remove_begin_slash(path) path = utils.resourceref.join_refs( [self.get_path(), self.get_current_path(), path]) for res in self.__get_open__(path): res.close() self.__closed__(res) os.unlink(path) except IOError: raise exceptions.NotResource(path) else: raise exceptions.NotResource(path)
def save_resource(self, res): """ Flush the changes of a given resource instance. Normally this is called by the Resource object in its own save. @param res: the resource to the resource to save. """ if not self.__has_resource__(res): raise exceptions.NotResource("No such %s open resource!" % res.path) else: self.zipfile.writestr(res.path,res.getvalue())
def open_resource(self, path, mode="r"): # make sure that path exists if we are creating a file path = utils.resourceref.remove_end_slash(path) path = utils.resourceref.remove_begin_slash(path) path = utils.resourceref.join_refs([self.get_current_path(), path]) fullpath = utils.resourceref.join_refs([self.get_path(), path]) if mode.find("w") != -1: dirpath = os.path.dirname(fullpath) if not os.path.exists(dirpath): os.makedirs(dirpath) try: res = FileResource(self, path, mode, open(fullpath, mode)) self.__opened__(res) return res except IOError, e: raise exceptions.NotResource("%s, %s" % (path, e))
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) else: raise exceptions.NotResource("No such %s resource!" % path) class ZipFileResource(Resource): def __init__(self,storage,path,mode,handle): Resource.__init__(self,storage,path,mode) self.handle = handle def read(self,bytes=0): if bytes == 0: return self.handle.read() else: return self.handle.read(bytes) def write(self,string): if self.get_mode() == api.Storage.MODE_READ: