def save(cls, items, path): path = pathlib.Path(path) head = EBMLHead(docType="filters", docTypeReadVersion=1, docTypeVersion=1, ebmlMaxIDLength=4, ebmlMaxSizeLength=8, ebmlReadVersion=1, ebmlVersion=1) self = cls.fromObj(items, environ={"module": filters}) if lzma and path.suffix.upper() == ".XZ": file = lzma.LZMAFile(path, "w", preset=9 | lzma.PRESET_EXTREME) elif bz2 and path.suffix.upper() == ".BZ2": file = bz2.BZ2File(path, "w", compresslevel=9) elif gzip and path.suffix.upper() == ".GZ": file = gzip.GzipFile(path, "w", compresslevel=9) else: file = open(path, "wb") self.source = None objIDs = {filter.objID for filter in self.items} for filter in self.items: if isinstance(filter.options, ebml.serialization.StateDict): for item in list(filter.options.items): if (isinstance(item.items[1], ebml.serialization.Ref) and item.items[1].data not in objIDs): filter.options.items.remove(item) head.toFile(file) self.toFile(file)
def save(cls, filter, path): path = pathlib.Path(path) head = EBMLHead(docType="filterchain", docTypeReadVersion=1, docTypeVersion=1, ebmlMaxIDLength=4, ebmlMaxSizeLength=8, ebmlReadVersion=1, ebmlVersion=1) self = cls.fromObj(filter, environ={"module": filters}) if lzma and path.suffix.upper() == ".XZ": file = lzma.LZMAFile(path, "w", preset=9 | lzma.PRESET_EXTREME) elif bz2 and path.suffix.upper() == ".BZ2": file = bz2.BZ2File(path, "w", compresslevel=9) elif gzip and path.suffix.upper() == ".GZ": file = gzip.GzipFile(path, "w", compresslevel=9) else: file = open(path, "wb") self.source = None head.toFile(file) self.toFile(file)
def save(cls, filter, path): if isinstance(filter, FilterChain): return FilterChainElement.save(filter, path) path = pathlib.Path(path) head = EBMLHead(docType="filter", docTypeReadVersion=1, docTypeVersion=1, ebmlMaxIDLength=4, ebmlMaxSizeLength=8, ebmlReadVersion=1, ebmlVersion=1) self = cls.fromObj(filter, environ={"module": filters}) if lzma and path.suffix.upper() == ".XZ": file = lzma.LZMAFile(path, "w", preset=9 | lzma.PRESET_EXTREME) elif bz2 and path.suffix.upper() == ".BZ2": file = bz2.BZ2File(path, "w", compresslevel=9) elif gzip and path.suffix.upper() == ".GZ": file = gzip.GzipFile(path, "w", compresslevel=9) else: file = open(path, "wb") self.source = None if isinstance(self.options, ebml.serialization.StateDict): for item in list(self.options.items): if isinstance(item.items[1], ebml.serialization.Ref): self.options.items.remove(item) head.toFile(file) self.toFile(file)
def load(cls, path): path = pathlib.Path(path) if lzma and path.suffix.upper() == ".XZ": file = lzma.LZMAFile(path, "r") elif bz2 and path.suffix.upper() == ".BZ2": file = bz2.BZ2File(path, "r") elif gzip and path.suffix.upper() == ".GZ": file = gzip.GzipFile(path, "r") else: file = open(path, "rb") head = EBMLHead.fromFile(file) if head.docType == "filterchain": self = FilterChainElement.fromFile(file) elif head.docType == "filter": self = cls.fromFile(file) else: raise ValueError("Not a valid filter or filterchain file.") return self.toObj({"path": path, "module": filters})
def _init_read(self): head = EBMLHead.fromFile(self._file) if head.docType != "matroska": raise ReadError("Not a matroska file.") self.head = head self.body = self._bodycls(self._file)
def _init_write(self): head = EBMLHead(docType="matroska", docTypeReadVersion=2, docTypeVersion=4, ebmlMaxIDLength=4, ebmlMaxSizeLength=8, ebmlReadVersion=1, ebmlVersion=1) self.writeEBMLHead(head) self.beginWriteEBMLBody()
def _init_read(self): """ This should be overridden in subclasses if you are looking to handle specific document types. """ self.head = EBMLHead.fromFile(self._file) self.body = self._bodycls(self._file)