def handle_request(self) -> Optional[str]: """Actually handle a request. Called by `do_HEAD` and `do_GET`.""" path = self.path.split("?", 1)[0] if path == "/": out = render.html_index(self.server.all_modules) else: module = removesuffix(path.lstrip("/"), ".html").replace("/", ".") if module not in self.server.all_modules: self.send_response(404) self.send_header("content-type", "text/html") self.end_headers() return render.html_error(error=f"Module {module!r} not found") mtime = "" t = extract.module_mtime(module) if t: mtime = f"{t:.1f}" if "mtime=1" in self.path: self.send_response(200) self.send_header("content-type", "text/plain") self.end_headers() return mtime try: extract.invalidate_caches(module) mod = doc.Module(extract.load_module(module)) except Exception: self.send_response(500) self.send_header("content-type", "text/html") self.end_headers() return render.html_error( error=f"Error importing {module!r}", details=traceback.format_exc(), ) out = render.html_module( module=mod, all_modules=self.server.all_modules, mtime=mtime, ) self.send_response(200) self.send_header("content-type", "text/html") self.end_headers() return out
def r(mod: doc.Module) -> str: return render.html_module(module=mod, all_modules=all_modules)