def load_html(path, view, codec='utf-8', mime_type=None, pre_load_callback=lambda x:None, path_is_html=False, force_as_html=False, loading_url=None): from qt.core import QUrl, QByteArray if mime_type is None: mime_type = guess_type(path)[0] if not mime_type: mime_type = 'text/html' if path_is_html: html = path else: with open(path, 'rb') as f: html = f.read().decode(codec, 'replace') html = cleanup_html(html) loading_url = loading_url or QUrl.fromLocalFile(path) pre_load_callback(loading_url) if force_as_html or load_as_html(html): view.setHtml(html, loading_url) else: view.setContent(QByteArray(html.encode(codec)), mime_type, loading_url) mf = view.page().mainFrame() elem = mf.findFirstElement('parsererror') if not elem.isNull(): return False return True
def convert_html_file(self, path, page_layout, settle_time=0, wait_for_title=None): self.working = True self.load_complete = False self.wait_for_title = wait_for_title self.settle_time = settle_time self.page_layout = page_layout self.setUrl(QUrl.fromLocalFile(path))
def main(): must_use_qt() load_builtin_fonts() renderer = Renderer() renderer.setUrl(QUrl.fromLocalFile(sys.argv[-1])) renderer.loadFinished.connect(renderer.do_print) QApplication.instance().exec_() print('Output written to:', OUTPUT)
def parse_link(self, link): link = link.strip() if link and os.path.exists(link): return QUrl.fromLocalFile(link) has_schema = re.match(r'^[a-zA-Z]+:', link) if has_schema is not None: url = QUrl(link, QUrl.ParsingMode.TolerantMode) if url.isValid(): return url if os.path.exists(link): return QUrl.fromLocalFile(link) if has_schema is None: first, _, rest = link.partition('.') prefix = 'http' if first == 'ftp': prefix = 'ftp' url = QUrl(prefix +'://'+link, QUrl.ParsingMode.TolerantMode) if url.isValid(): return url return QUrl(link, QUrl.ParsingMode.TolerantMode)
def view_image(self, name): path = get_path_for_name(name) if path: pmap = QPixmap() if pmap.load(path): self.image_popup.current_img = pmap self.image_popup.current_url = QUrl.fromLocalFile(path) self.image_popup() else: error_dialog(self, _('Invalid image'), _( "Failed to load the image {}").format(name), show=True) else: error_dialog(self, _('Image not found'), _( "Failed to find the image {}").format(name), show=True)
def copy_image(self, name): path = get_path_for_name(name) if not path: return error_dialog(self, _('Image not found'), _( "Failed to find the image {}").format(name), show=True) try: img = image_from_path(path) except Exception: return error_dialog(self, _('Invalid image'), _( "Failed to load the image {}").format(name), show=True) url = QUrl.fromLocalFile(path) md = QMimeData() md.setImageData(img) md.setUrls([url]) QApplication.instance().clipboard().setMimeData(md)
def url_for_id(i): try: ans = db.format_path(i, fmt, index_is_id=True) except: ans = None if ans is None: fmts = db.formats(i, index_is_id=True) if fmts: fmts = fmts.split(',') else: fmts = [] for f in fmts: try: ans = db.format_path(i, f, index_is_id=True) except: ans = None if ans is None: ans = db.abspath(i, index_is_id=True) return QUrl.fromLocalFile(ans)
def bd_open_cover_with(self, book_id, entry): cpath = self.current_db.new_api.format_abspath(book_id, '__COVER_INTERNAL__') if cpath: if entry is None: pm = QPixmap() pm.load(cpath) pm.setDevicePixelRatio(self.devicePixelRatioF()) if pm.isNull(): open_local_file(cpath) else: from calibre.gui2.image_popup import ImageView iv = ImageView(QApplication.instance().focusWindow(), pm, QUrl.fromLocalFile(cpath), geom_name='book_details_image_view') iv(use_exec=True) return from calibre.gui2.open_with import run_program run_program(entry, cpath, self)
def test_dom_load(self): overseer = Overseer() for f in ('book', 'nav'): path = P(f'templates/new_{f}.html', allow_user_override=False) url = QUrl.fromLocalFile(path) html = overseer.fetch_url(url, 'test') def c(a): ans = tostring(fromstring(a.encode('utf-8')), pretty_print=True, encoding='unicode') return re.sub(r'\s+', ' ', ans) with open(path, 'rb') as f: raw = f.read().decode('utf-8') self.assertEqual(c(html), c(raw)) self.assertRaises(ValueError, overseer.fetch_url, 'file:///does-not-exist.html', 'test') w = overseer.workers self.assertEqual(len(w), 1) del overseer self.assertFalse(w)
def path_to_url(path): return QUrl.fromLocalFile(path).toString()
def start_load(self, path_to_html): self.load(QUrl.fromLocalFile(path_to_html)) self.start_time = monotonic() self.hang_timer.start()
self.current_url = QUrl() self.parent = parent self.dialogs = [] def __call__(self): if self.current_img.isNull(): return d = ImageView(self.parent, self.current_img, self.current_url) self.dialogs.append(d) d.finished.connect(self.cleanup, type=Qt.ConnectionType.QueuedConnection) d() def cleanup(self): for d in tuple(self.dialogs): if not d.isVisible(): self.dialogs.remove(d) if __name__ == '__main__': import sys from calibre.gui2 import Application app = Application([]) p = QPixmap() p.load(sys.argv[-1]) u = QUrl.fromLocalFile(sys.argv[-1]) d = ImageView(None, p, u) d() app.exec_()
def load_path(self, path, frag=None): self._page.current_frag = frag self.setUrl(QUrl.fromLocalFile(path))
def show_book_folder(self): path = os.path.dirname(os.path.abspath(set_book_path.pathtoebook)) safe_open_url(QUrl.fromLocalFile(path))