Esempio n. 1
0
    def _open_next(self):
        """Proceed to next volume."""

        # is the file split over archives?
        if (self._cur.flags & rarfile.RAR_FILE_SPLIT_AFTER) == 0:
            return False

        if self._fd:
            self._fd.close()
            self._fd = None

        # open next part
        self._volfile = self._parser._next_volname(self._volfile)
        fd = rarfile.XFile(self._volfile)
        self._fd = fd
        sig = fd.read(len(self._parser._expect_sig))
        if sig != self._parser._expect_sig:
            raise rarfile.BadRarFile("Invalid signature")

        # loop until first file header
        while 1:
            cur = self._parser._parse_header(fd)
            if not cur:
                raise rarfile.BadRarFile("Unexpected EOF")
            if cur.type in (rarfile.RAR_BLOCK_MARK, rarfile.RAR_BLOCK_MAIN):
                if cur.add_size:
                    fd.seek(cur.add_size, 1)
                continue
            if cur.orig_filename != self._inf.orig_filename:
                raise rarfile.BadRarFile("Did not found file entry")
            self._cur = cur
            self._cur_avail = cur.add_size
            return True
Esempio n. 2
0
    def _parse(self):
        rarfile_xfile = rarfile.XFile(self._rarfile)
        ver = _get_rar_version(rarfile_xfile)
        rarfile_xfile.seek(0)
        if ver == 3 and self._rar3parser:
            p3 = self._rar3parser(self._filesystem, rarfile_xfile,
                                  self._rarfile, self._password,
                                  self._crc_check, self._charset, self._strict,
                                  self._info_callback)
            self._file_parser = p3  # noqa
        elif ver == 5 and self._rar5parser:
            p5 = self._rar5parser(self._filesystem, rarfile_xfile,
                                  self._rarfile, self._password,
                                  self._crc_check, self._charset, self._strict,
                                  self._info_callback)
            self._file_parser = p5  # noqa
        else:
            raise rarfile.BadRarFile("Not a RAR file")

        self._file_parser.parse()
        self.comment = self._file_parser.comment
Esempio n. 3
0
def extract_render(
    path: str,
    version: str,
    input_title: str,
    doc_template_path: str,
    page_template_path: str,
    boot_template_path: str,
    asset_paths: Iterable[str],
    img_types: Iterable[str],
    dark: bool,
    horizontal: bool,
    outpath: Path = Path(tempfile.gettempdir()) / 'html-mangareader',
) -> Path:
    """Main controller procedure. Handles opening of archive, image, or directory and renders the images
    appropriately for each, then opens the document in the user's default browser.

    Parameters:
    * `path`: path to image, directory, or archive.
    * `version`: version of the app to display to user.
    * `doc_template_path`: path to HTML template for the main document.
    * `page_template_path`: path to HTML template for individual comic page elements.
    * `boot_template_path`: path to HTML template for bootstrap document.
    * `asset_paths`: paths of static assets to copy.
    * `image_types`: list of recognized image file extensions.
    * `outpath`: directory to write temporary files in. Defaults to OS temp directory.

    Returns: Path to the bootstrap document, which can be opened in a web browser.

    Throws:
    * `BadZipFile`: opened file was a zip file, but could not be read.
    * `BadRarFile`: opened file was a rar file, but could not be read.
    * `Bad7zFile`: opened file was a 7z file, but could not be read.
    * `ImagesNotFound`: if no images could be found in an opened directory or archive.
    """
    start = 0
    pPath = Path(path).resolve()
    rmtree(os.path.dirname(os.path.join(tempfile.gettempdir(), 'html-mangareader', 'render.html')), ignore_errors=True)
    doc_template, page_template, boot_template = (
        resolve_template(p) for p in (doc_template_path, page_template_path, boot_template_path)
    )
    try:
        if pPath.is_file():
            if pPath.suffix.lower()[1:] in img_types:
                imgpaths = scan_directory(pPath.parent, img_types)
                start = imgpaths.index(pPath)
                title = pPath.parent.name
            else:
                try:
                    imgpaths = extract_zip(pPath, img_types, str(outpath))
                    title = pPath.name
                except zipfile.BadZipFile as e:
                    raise zipfile.BadZipfile(
                        f'"{path}" does not appear to be a valid zip/cbz file.'
                    ).with_traceback(e.__traceback__)
                except rarfile.BadRarFile as e:
                    raise rarfile.BadRarFile(
                        f'"{path}" does not appear to be a valid rar/cbr file.'
                    ).with_traceback(e.__traceback__)
                except py7zr.Bad7zFile as e:
                    raise py7zr.Bad7zFile(
                        f'"{path}" does not appear to be a valid 7z/cb7 file.'
                    ).with_traceback(e.__traceback__)
        else:
            imgpaths = scan_directory(path, img_types)
            title = pPath.name
            
        title = input_title
        
        create_out_path(outpath)
        render_copy(asset_paths, outpath)
        renderfile = render_from_template(
            paths=imgpaths,
            version=version,
            title=title,
            doc_template=doc_template,
            page_template=page_template,
            outfile=str(outpath / 'index.html'),
        )
        bootfile = render_bootstrap(
            outfile=str(outpath / 'boot.html'),
            render=Path(renderfile).as_uri(),
            index=start,
            boot_template=boot_template,
        )
        return Path(bootfile)

    except ImagesNotFound:
        raise
    return
Esempio n. 4
0
 def _check(self):  # TODO: fix?
     """Do not check final CRC."""
     if self._returncode:
         rarfile.check_returncode(self, '')
     if self._remain != 0:
         raise rarfile.BadRarFile("Failed the read enough data")