class StoreEngineWeedFs(StoreBase): """docstring for StoreEngineWeedFs""" def __init__(self, section): StoreBase.__init__(self, section) from weedfs import WeedClient self.client = WeedClient() def _get(self, id): print '_get {}'.format(id) item = id if isinstance(id, StoreItem) else self.get_meta(id) if not item.has_key(WEED_HOST) or not item.has_key(WEED_HOST): raise ValueError('the entry has no special value ' + WEED_HOST + ' and ' + WEED_FID) volume_host, fid = item[WEED_HOST], item[WEED_FID] ctype, size, content = self.client.retrieve(volume_host, fid) print 'weed retrieved: %s %s' % (ctype, size) if content: from StringIO import StringIO return StringIO(content) raise ValueError('weed client.retrieve error: invalid response') def delete(self, id): raise NotImplemented() def _put(self, data, **spec): volume_host, fid = self.client.assign() ret = self.client.store(volume_host, fid, content=data, name=spec['filename'], content_type=spec['mime']) if isinstance(ret, int) and ret > 0: print 'saved {}/{} size {} bytes'.format(volume_host, fid, ret) spec[WEED_HOST] = volume_host spec[WEED_FID] = fid self._save_meta(spec['_id'], spec) return spec['_id'] print 'store error: %s' % ret def _store_exists(self, id=None, *args, **kwargs): if hasattr(kwargs, WEED_HOST) and hasattr(kwargs, WEED_FID): ctype, size = self.client.retrieve(kwargs[WEED_HOST], kwargs[WEED_FID], head=True) print 'exists %s %s' % (ctype, size) return True return False
class StoreEngineWeedFs(StoreBase): """docstring for StoreEngineWeedFs""" def __init__(self, section): StoreBase.__init__(self, section) from weedfs import WeedClient self.client = WeedClient() def _get(self, id): print '_get {}'.format(id) item = id if isinstance(id, StoreItem) else self.get_meta(id) if not item.has_key(WEED_HOST) or not item.has_key(WEED_HOST): raise ValueError('the entry has no special value ' + WEED_HOST + ' and ' + WEED_FID) volume_host, fid = item[WEED_HOST], item[WEED_FID] ctype, size, content = self.client.retrieve(volume_host, fid) print 'weed retrieved: %s %s' % (ctype, size) if content: from StringIO import StringIO return StringIO(content) raise ValueError('weed client.retrieve error: invalid response') def delete(self, id): raise NotImplemented() def _put(self, data, **spec): volume_host, fid = self.client.assign() ret = self.client.store(volume_host, fid, content=data, name=spec['filename'], content_type=spec['mime']) if isinstance(ret, int) and ret > 0: print 'saved {}/{} size {} bytes'.format(volume_host, fid, ret) spec[WEED_HOST] = volume_host spec[WEED_FID] = fid self._save_meta(spec['_id'], spec) return spec['_id'] print 'store error: %s' % ret def _store_exists(self, id=None, *args, **kwargs): if hasattr(kwargs, WEED_HOST) and hasattr(kwargs, WEED_FID): ctype, size = self.client.retrieve(kwargs[WEED_HOST],kwargs[WEED_FID], head=True) print 'exists %s %s' % (ctype, size) return True return False
def __init__(self, section): StoreBase.__init__(self, section) from weedfs import WeedClient self.client = WeedClient()