def convert_to_cbz(self): ''' Converts a cbr-comic to a cbz-comic ''' from calibre.utils.unrar import RARFile, extract with TemporaryDirectory('_cbr2cbz') as tdir: # extract the rar file ffile = self.db.format(self.book_id, "cbr", as_path=True) extract(ffile, tdir) # get the comment with open(ffile, 'rb') as stream: zr = RARFile(stream, get_comment=True) comment = zr.comment delete_temp_file(ffile) # make the cbz file with TemporaryFile("comic.cbz") as tf: zf = ZipFile(tf, "w") zf.add_dir(tdir) zf.close() # write comment if comment: writeZipComment(tf, comment) # add the cbz format to calibres library self.db.add_format(self.book_id, "cbz", tf) self.format = "cbz"
def extract(source): tdir = tempfile.mkdtemp(suffix='_archive', dir=self.tdir) if source.lower().endswith('.zip'): from calibre.utils.zipfile import ZipFile try: with ZipFile(source) as zf: zf.extractall(tdir) except Exception: prints('Corrupt ZIP file, trying to use local headers') from calibre.utils.localunzip import extractall extractall(source, tdir) elif source.lower().endswith('.rar'): from calibre.utils.unrar import extract extract(source, tdir) return tdir
def extract(self): if self.path.lower().endswith('.zip'): from calibre.utils.zipfile import ZipFile try: with ZipFile(self.path) as zf: zf.extractall(self.tdir) except Exception: prints('Corrupt ZIP file, trying to use local headers') from calibre.utils.localunzip import extractall extractall(self.path, self.tdir) elif self.path.lower().endswith('.rar'): from calibre.utils.unrar import extract extract(self.path, self.tdir) else: raise ValueError('Can only process ZIP or RAR archives')
def scan(self): try: compiled_rules = tuple(map(compile_rule, gprefs.get('add_filter_rules', ()))) except Exception: compiled_rules = () import traceback traceback.print_exc() def find_files(root): for dirpath, dirnames, filenames in os.walk(root): for files in find_books_in_directory(dirpath, self.single_book_per_directory, compiled_rules=compiled_rules): if self.abort_scan: return self.file_groups[len(self.file_groups)] = files def extract(source): tdir = tempfile.mkdtemp(suffix='_archive', dir=self.tdir) if source.lower().endswith('.zip'): from calibre.utils.zipfile import ZipFile try: with ZipFile(source) as zf: zf.extractall(tdir) except Exception: prints('Corrupt ZIP file, trying to use local headers') from calibre.utils.localunzip import extractall extractall(source, tdir) elif source.lower().endswith('.rar'): from calibre.utils.unrar import extract extract(source, tdir) return tdir try: if isinstance(self.source, basestring): find_files(self.source) self.ignore_opf = True else: unreadable_files = [] for path in self.source: if self.abort_scan: return if os.access(path, os.R_OK): if self.list_of_archives: find_files(extract(path)) self.ignore_opf = True else: self.file_groups[len(self.file_groups)] = [path] else: unreadable_files.append(path) if unreadable_files: if not self.file_groups: self.scan_error = _('You do not have permission to read the selected file(s).') + '\n' self.scan_error += '\n'.join(unreadable_files) else: a = self.report.append for f in unreadable_files: a(_('Could not add %s as you do not have permission to read the file' % f)) a('') except Exception: self.scan_error = traceback.format_exc()
def scan(self): def find_files(root): for dirpath, dirnames, filenames in os.walk(root): for files in find_books_in_directory(dirpath, self.single_book_per_directory): if self.abort_scan: return self.file_groups[len(self.file_groups)] = files def extract(source): tdir = tempfile.mkdtemp(suffix="_archive", dir=self.tdir) if source.lower().endswith(".zip"): from calibre.utils.zipfile import ZipFile try: with ZipFile(source) as zf: zf.extractall(tdir) except Exception: prints("Corrupt ZIP file, trying to use local headers") from calibre.utils.localunzip import extractall extractall(source, tdir) elif source.lower().endswith(".rar"): from calibre.utils.unrar import extract extract(source, tdir) return tdir try: if isinstance(self.source, basestring): find_files(self.source) self.ignore_opf = True else: unreadable_files = [] for path in self.source: if self.abort_scan: return if os.access(path, os.R_OK): if self.list_of_archives: find_files(extract(path)) self.ignore_opf = True else: self.file_groups[len(self.file_groups)] = [path] else: unreadable_files.append(path) if unreadable_files: if not self.file_groups: self.scan_error = _("You do not have permission to read the selected file(s).") + "\n" self.scan_error += "\n".join(unreadable_files) else: a = self.report.append for f in unreadable_files: a(_("Could not add %s as you do not have permission to read the file" % f)) a("") except Exception: self.scan_error = traceback.format_exc()
def convert_cbr_to_cbz(self): ''' Converts a rar or cbr-comic to a cbz-comic ''' from calibre.utils.unrar import extract, comment with TemporaryDirectory('_cbr2cbz') as tdir: # extract the rar file ffile = self.db.format(self.book_id, self.format, as_path=True) extract(ffile, tdir) comments = comment(ffile) delete_temp_file(ffile) # make the cbz file with TemporaryFile("comic.cbz") as tf: zf = ZipFile(tf, "w") zf.add_dir(tdir) if comments: zf.comment = comments.encode("utf-8") zf.close() # add the cbz format to calibres library self.db.add_format(self.book_id, "cbz", tf) self.format = "cbz"
def scan(self): try: compiled_rules = tuple( map(compile_rule, gprefs.get('add_filter_rules', ()))) except Exception: compiled_rules = () import traceback traceback.print_exc() if iswindows or ismacos: def find_files(root): for dirpath, dirnames, filenames in os.walk(root): for files in find_books_in_directory( dirpath, self.single_book_per_directory, compiled_rules=compiled_rules): if self.abort_scan: return if iswindows: files = list( resolve_windows_links(files, hwnd=self.win_id)) if files: self.file_groups[len(self.file_groups)] = files else: def find_files(root): if isinstance(root, unicode_type): root = root.encode(filesystem_encoding) for dirpath, dirnames, filenames in os.walk(root): try: dirpath = dirpath.decode(filesystem_encoding) except UnicodeDecodeError: prints('Ignoring non-decodable directory:', dirpath) continue for files in find_books_in_directory( dirpath, self.single_book_per_directory, compiled_rules=compiled_rules): if self.abort_scan: return self.file_groups[len(self.file_groups)] = files def extract(source): tdir = tempfile.mkdtemp(suffix='_archive', dir=self.tdir) if source.lower().endswith('.zip'): from calibre.utils.zipfile import ZipFile try: with ZipFile(source) as zf: zf.extractall(tdir) except Exception: prints('Corrupt ZIP file, trying to use local headers') from calibre.utils.localunzip import extractall extractall(source, tdir) elif source.lower().endswith('.rar'): from calibre.utils.unrar import extract extract(source, tdir) return tdir try: if isinstance(self.source, string_or_bytes): find_files(self.source) self.ignore_opf = True else: unreadable_files = [] for path in self.source: if self.abort_scan: return if os.access(path, os.R_OK): if self.list_of_archives: find_files(extract(path)) self.ignore_opf = True else: x = [path] if iswindows: x = list( resolve_windows_links(x, hwnd=self.win_id)) if x: self.file_groups[len(self.file_groups)] = x else: unreadable_files.append(path) else: unreadable_files.append(path) if unreadable_files: if not self.file_groups: m = ngettext( 'You do not have permission to read the selected file.', 'You do not have permission to read the selected files.', len(unreadable_files)) self.scan_error = m + '\n' + '\n'.join( unreadable_files) else: a = self.report.append for f in unreadable_files: a( _('Could not add %s as you do not have permission to read the file' % f)) a('') except Exception: self.scan_error = traceback.format_exc()