def delete(self, key, glob=False): # if glob is true will delete all with filename prefix if glob == True: for f in os.listdir(self._root_dir): if f.startswith(safefilename.encode(key)): os.remove(os.path.join(self._root_dir,f)) else: os.remove( os.path.join(self._root_dir, safefilename.encode(key)))
def delete(self, key, glob=False): # if glob is true will delete all with filename prefix if glob == True: for f in os.listdir(self._root_dir): if f.startswith(safefilename.encode(key)): os.remove(os.path.join(self._root_dir, f)) else: os.remove(os.path.join(self._root_dir, safefilename.encode(key)))
def put(self, key, headers, src): """ Add a file to the store, overwriting an existing file with the same key. :arg key: unique key that identifies the file. :arg headers: list of (name, value) pairs that will be associated with the file. :arg src: readable file-like object """ # XXX We should only allow strings as headers keys and values. # Open the file with minimal permissions.. filename = os.path.join(self._root_dir, safefilename.encode(key)) fd = os.open(filename, os.O_RDWR | os.O_CREAT, self._mode) dest = os.fdopen(fd, 'wb') try: if isinstance(headers, dict): headers = headers.items() for name, value in headers: if isinstance(value, unicode): value = value.encode('utf-8') dest.write('%s: %s\n' % (name, value)) dest.write('\n') _copyfile.copyfileobj(src, dest) finally: dest.close()
def put(self, key, headers, src): """ Add a file to the store, overwriting an existing file with the same key. :arg key: unique key that identifies the file. :arg headers: list of (name, value) pairs that will be associated with the file. :arg src: readable file-like object """ # XXX We should only allow strings as headers keys and values. # Open the file with minimal permissions.. filename = os.path.join(self._root_dir, safefilename.encode(key)) fd = os.open(filename, os.O_RDWR|os.O_CREAT, self._mode) dest = os.fdopen(fd, 'wb') try: if isinstance(headers, dict): headers = headers.items() for name, value in headers: if isinstance(value, unicode): value = value.encode('utf-8') dest.write('%s: %s\n' % (name, value)) dest.write('\n') _copyfile.copyfileobj(src, dest) finally: dest.close()
def get(self, key): """ Get the file stored for the given key. :arg key: unique key that identifies the file. :returns: tuple of (header, f) where headers is a list of (name, value) pairs and f is a readable file-like object. :raises KeyError: not found """ try: f = open(os.path.join(self._root_dir, safefilename.encode(key)), 'rb') except IOError, AttributeError: raise KeyError(key)
def get(self, key): """ Get the file stored for the given key. :arg key: unique key that identifies the file. :returns: tuple of (header, f) where headers is a list of (name, value) pairs and f is a readable file-like object. :raises KeyError: not found """ try: f = open(os.path.join(self._root_dir, safefilename.encode(key)), 'rb') except (IOError, AttributeError): raise KeyError(key) headers = [] while True: line = f.readline().strip() if not line: break name, value = line.split(': ', 1) headers.append((name, value.decode('utf-8'))) return headers, f