def qute_resource(url: QUrl) -> _HandlerRet: """Handler for qute://resource.""" path = url.path().lstrip('/') mimetype = utils.guess_mimetype(path, fallback=True) try: data = utils.read_file_binary(path) except FileNotFoundError as e: raise NotFoundError(str(e)) return mimetype, data
def qute_help(url: QUrl) -> _HandlerRet: """Handler for qute://help.""" urlpath = url.path() if not urlpath or urlpath == '/': urlpath = 'index.html' else: urlpath = urlpath.lstrip('/') if not docutils.docs_up_to_date(urlpath): message.error("Your documentation is outdated! Please re-run " "scripts/asciidoc2html.py.") path = 'html/doc/{}'.format(urlpath) if not urlpath.endswith('.html'): try: bdata = utils.read_file_binary(path) except OSError as e: raise SchemeOSError(e) mimetype = utils.guess_mimetype(urlpath) return mimetype, bdata try: data = utils.read_file(path) except OSError: asciidoc = _asciidoc_fallback_path(path) if asciidoc is None: raise preamble = textwrap.dedent(""" There was an error loading the documentation! This most likely means the documentation was not generated properly. If you are running qutebrowser from the git repository, please (re)run scripts/asciidoc2html.py and reload this page. If you're running a released version this is a bug, please use :report to report it. Falling back to the plaintext version. --------------------------------------------------------------- """) return 'text/plain', (preamble + asciidoc).encode('utf-8') else: return 'text/html', data
def get_pdfjs_res_and_path(path): """Get a pdf.js resource in binary format. Returns a (content, path) tuple, where content is the file content and path is the path where the file was found. If path is None, the bundled version was used. Args: path: The path inside the pdfjs directory. """ path = path.lstrip('/') content = None file_path = None system_paths = _SYSTEM_PATHS + [ # fallback os.path.join(standarddir.data(), 'pdfjs'), # hardcoded fallback for --temp-basedir os.path.expanduser('~/.local/share/qutebrowser/pdfjs/'), ] # First try a system wide installation # System installations might strip off the 'build/' or 'web/' prefixes. # qute expects them, so we need to adjust for it. names_to_try = [path, _remove_prefix(path)] for system_path in system_paths: content, file_path = _read_from_system(system_path, names_to_try) if content is not None: break # Fallback to bundled pdf.js if content is None: res_path = '3rdparty/pdfjs/{}'.format(path) try: content = utils.read_file_binary(res_path) except FileNotFoundError: raise PDFJSNotFound(path) from None except OSError as e: log.misc.warning("OSError while reading PDF.js file: {}".format(e)) raise PDFJSNotFound(path) from None return content, file_path
def _data_url(self, path: str) -> str: """Get a data: url for the broken qutebrowser logo.""" data = utils.read_file_binary(path) mimetype = utils.guess_mimetype(path) return urlutils.data_url(mimetype, data).toString()