| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import collections.abc as c |
| 4 |
import typing as t |
| 5 |
|
| 6 |
from flask import Flask |
| 7 |
from jinja2 import Environment |
| 8 |
from werkzeug import Request |
| 9 |
from werkzeug import Response |
| 10 |
|
| 11 |
|
| 12 |
class DebugPanel: |
| 13 |
"""Base class for debug panels.""" |
| 14 |
|
| 15 |
name: str |
| 16 |
|
| 17 |
# If content returns something, set to true in subclass |
| 18 |
has_content = False |
| 19 |
|
| 20 |
# If the client is able to activate/de-activate the panel |
| 21 |
user_enable = False |
| 22 |
|
| 23 |
# We'll maintain a local context instance so we can expose our template |
| 24 |
# context variables to panels which need them: |
| 25 |
context: dict[str, t.Any] = {} |
| 26 |
|
| 27 |
# Panel methods |
| 28 |
def __init__( |
| 29 |
self, jinja_env: Environment, context: dict[str, t.Any] | None = None |
| 30 |
) -> None: |
| 31 |
if context is not None: |
| 32 |
self.context.update(context) |
| 33 |
|
| 34 |
self.jinja_env = jinja_env |
| 35 |
# If the client enabled the panel |
| 36 |
self.is_active = False |
| 37 |
|
| 38 |
@classmethod |
| 39 |
def init_app(cls, app: Flask) -> None: |
| 40 |
"""Method that can be overridden by child classes. |
| 41 |
Can be used for setting up additional URL-rules/routes. |
| 42 |
|
| 43 |
Example:: |
| 44 |
|
| 45 |
class UMLDiagramPanel(DebugPanel): |
| 46 |
|
| 47 |
@classmethod |
| 48 |
def init_app(cls, app): |
| 49 |
app.add_url_rule( |
| 50 |
'/_flask_debugtoolbar_umldiagram/<path:filename>', |
| 51 |
'_flask_debugtoolbar_umldiagram.serve_generated_image', |
| 52 |
cls.serve_generated_image |
| 53 |
) |
| 54 |
|
| 55 |
@classmethod |
| 56 |
def serve_generated_image(cls, app): |
| 57 |
return Response(...) |
| 58 |
""" |
| 59 |
pass |
| 60 |
|
| 61 |
def render(self, template_name: str, context: dict[str, t.Any]) -> str: |
| 62 |
template = self.jinja_env.get_template(template_name) |
| 63 |
return template.render(**context) |
| 64 |
|
| 65 |
def dom_id(self) -> str: |
| 66 |
return f"flDebug{self.name.replace(' ', '')}Panel" |
| 67 |
|
| 68 |
def nav_title(self) -> str: |
| 69 |
"""Title showing in toolbar""" |
| 70 |
raise NotImplementedError |
| 71 |
|
| 72 |
def nav_subtitle(self) -> str: |
| 73 |
"""Subtitle showing until title in toolbar""" |
| 74 |
return "" |
| 75 |
|
| 76 |
def title(self) -> str: |
| 77 |
"""Title showing in panel""" |
| 78 |
raise NotImplementedError |
| 79 |
|
| 80 |
def url(self) -> str: |
| 81 |
raise NotImplementedError |
| 82 |
|
| 83 |
def content(self) -> str: |
| 84 |
raise NotImplementedError |
| 85 |
|
| 86 |
# Standard middleware methods |
| 87 |
def process_request(self, request: Request) -> None: |
| 88 |
pass |
| 89 |
|
| 90 |
def process_view( |
| 91 |
self, |
| 92 |
request: Request, |
| 93 |
view_func: c.Callable[..., t.Any], |
| 94 |
view_kwargs: dict[str, t.Any], |
| 95 |
) -> c.Callable[..., t.Any] | None: |
| 96 |
pass |
| 97 |
|
| 98 |
def process_response(self, request: Request, response: Response) -> None: |
| 99 |
pass |