def extract_zip( path: Path, img_types: Iterable[str], outpath: str = os.path.join(tempfile.gettempdir(), 'html-mangareader'), ) -> List[Path]: """Extract image files found in an archive file. Parameters: * `path`: path to archive. * `img_types`: list of recognized image file extensions. * `outpath`: directory to extract images to. Defaults to OS temp directory. Returns: list of absolute paths to extracted image files. Throws: * `ImagesNotFound` if no images were found in the archive. * `BadZipFile` if CBZ/ZIP archive could not be read. * `BadRarFile` if CBR/RAR archive could not be read. * `Bad7zFile` if CB7/7Z archive could not be read. """ file_ext = path.suffix.lower()[1:] try: if file_ext in ZIP_TYPES: with zipfile.ZipFile(path, mode='r') as zip_file: return extract_archive(img_types, zip_file) elif file_ext in RAR_TYPES: with rarfile.RarFile(path, mode='r') as rar_file: return extract_archive(img_types, rar_file) elif file_ext in _7Z_TYPES: with SevenZipAdapter(path, mode='r') as _7z_file: return extract_archive(img_types, _7z_file) else: raise ImagesNotFound(f'Unknown archive format: {path}') except ImagesNotFound: raise ImagesNotFound(f'No image files were found in archive: {path}')
def extract_archive( img_types: Iterable[str], archive: Union[zipfile.ZipFile, rarfile.RarFile], outpath: str = os.path.join(tempfile.gettempdir(), 'html-mangareader'), ) -> List[Path]: """Extract image files in archive to the outpath.""" imagefiles = list(filter(lambda f: f.split('.')[-1].lower() in img_types, archive.namelist())) if not imagefiles: raise ImagesNotFound() archive.extractall(outpath, imagefiles) return [Path(outpath) / image for image in sorted(imagefiles, key=filename_comparator)]
def scan_directory(path: Union[str, Path], img_types: Iterable[str]) -> List[Path]: """Get a list of image file paths from a directory. Parameters: * `path`: directory to scan for images. * `img_types`: list of recognized image file extensions. Returns: list of absolute paths to image files. Throws: `ImagesNotFound` if no images were found in the directory. """ files = filter(lambda f: f.is_file(), Path(path).iterdir()) imagefiles = list(filter(lambda f: f.suffix.lower()[1:] in img_types, files)) if not imagefiles: raise ImagesNotFound(f'No image files were found in directory "{Path(path).resolve()}"') return [ p if p.is_absolute() else p.resolve() for p in sorted(imagefiles, key=filename_comparator) ]
def render_from_template( paths: Iterable[Union[Path, str]], version: str, title: str, doc_template: str, page_template: str, outfile: str = os.path.join(tempfile.gettempdir(), 'html-mangareader', 'render.html'), ) -> str: """Render a list of image paths to the finished HTML document. Parameters: * `paths`: full file:// paths to images to render on the page. * `version`: version number to render on page. * `title`: title of the page. * `doc_template`: HTML template for the overall document. * `page_template`: HTML template for each comic page element. * `outfile`: path to write the rendered document to. Defaults to OS temp directory. Returns: path to rendered HTML document. Throws: `ImagesNotFound` if the image list is empty. """ if not paths: raise ImagesNotFound('No images were sent to the renderer.') imagepaths = [Path(p).as_uri() for p in paths] os.makedirs(os.path.dirname(outfile), exist_ok=True) with open(outfile, 'w', encoding='utf-8', newline='\r\n') as renderfd: html_template = Template(doc_template) img_template = Template(page_template) img_list = [ img_template.substitute( img=imagepaths[i], id=str(i), previd=str(i - 1) if i > 0 else 'none', nextid=str(i + 1) if i < len(imagepaths) - 1 else 'none', ) for i in range(0, len(imagepaths)) ] doc_string = html_template.substitute(pages=''.join(img_list), version=version, title=title) renderfd.write(doc_string) # print("view saved to " + outfile) return outfile