def retrieve_content(jobID, documents=None): """ Retrieves data from a single or a list of documents. Returns binary data, for retrieving unicode text use retrieve_text(). Args: jobID (unicode): Identifier of the bin documents (tuple or list of tuples): Documents to read in Returns: Dictionary: A dictionary mapping file identifiers to their contents. Raises: NidabaNoSuchStorageBin if the job identifer is not known. """ if not is_valid_job(jobID): raise NidabaNoSuchStorageBin('ID ' + jobID + ' not known.') if documents: if isinstance(documents, basestring): documents = [documents] fdict = {} dpath = _sanitize_path(nidaba_cfg['storage_path'], jobID) locks = [lock(_sanitize_path(dpath, doc)) for doc in documents] map(lambda x: x.acquire(), locks) for doc in documents: with open(_sanitize_path(dpath, doc), 'rb') as f: fdict[doc] = f.read() map(lambda x: x.release(), locks) return fdict
def write_content(jobID, dest, data): """ Writes data to a document at a destination beneath a jobID. Writes bytes, does not accept unicode objects; use write_text() for that. Args: jobID (unicode): Identifier of the bin. dest (tuple): Documents to write to. data (str): Data to write. Returns: int: Length of data written """ if not is_valid_job(jobID): raise NidabaNoSuchStorageBin('ID ' + jobID + ' not known.') if not isinstance(data, basestring): raise NidabaStorageViolationException('data is not string') with open( _sanitize_path(nidaba_cfg['storage_path'], os.path.join(jobID, dest)), 'wb') as f: l = lock(f.name) l.acquire() f.write(data) l.release() return len(data)
def write_content(jobID, dest, data): """ Writes data to a document at a destination beneath a jobID. Writes bytes, does not accept unicode objects; use write_text() for that. Args: jobID (unicode): Identifier of the bin. dest (tuple): Documents to write to. data (str): Data to write. Returns: int: Length of data written None: Failure """ if not is_valid_job(jobID): return None if not isinstance(data, basestring): return None try: with open(_sanitize_path(nidaba_cfg['storage_path'], os.path.join(jobID, dest)), 'wb') as f: l = lock(f.name) l.acquire() f.write(data) l.release() except: return None return len(data)
def __init__(self, jobID, path, *args, **kwargs): self.path = get_abs_path(jobID, path) self.lock = lock(self.path) self.lock.acquire() try: self.fd = io.OpenWrapper(self.path, *args, **kwargs) except: self.lock.release() raise
def write_content(jobID, dest, data): """ Writes data to a document at a destination beneath a jobID. Writes bytes, does not accept unicode objects; use write_text() for that. Args: jobID (unicode): Identifier of the bin. dest (tuple): Documents to write to. data (str): Data to write. Returns: int: Length of data written """ if not is_valid_job(jobID): raise NidabaNoSuchStorageBin('ID ' + jobID + ' not known.') if not isinstance(data, basestring): raise NidabaStorageViolationException('data is not string') with open(_sanitize_path(nidaba_cfg['storage_path'], os.path.join(jobID, dest)), 'wb') as f: l = lock(f.name) l.acquire() f.write(data) l.release() return len(data)