def __setitem__(self, idx, doc): assert isinstance(idx, int) assert doc is None or isinstance(doc, Document) path = self.file_path_maker(idx) path = path + self.ext if doc is None: if self.store_none: if os.path.exists(path): os.remove(path) else: doc = Document.save(os.path.join(self.dirpath, path), fmt=self.fmt)
def append(self, doc): """ Add a document to the destination. Args: doc: the document or None, if None, no action is performed. """ if doc is None: return assert isinstance(doc, Document) path = self.file_path_maker(doc=doc, idx=self.idx) path = os.path.normpath( path ) # convert forward slashes to backslashes on windows path = os.path.join(self.dirpath, path) + self.ext # check if we need to create the directories. For this we first need to get the directories part of the path, # which is everything left of the last slash if os.path.sep in path: dirs = path[: path.rindex(os.path.sep)] if not os.path.exists(os.path.normpath(dirs)): os.makedirs(dirs) Document.save(doc, path, fmt=self.fmt) self.idx += 1