xtr-web-profiler 7.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- xtr_web_profiler/.agents/skills/xtr-web-profiler/SKILL.md +122 -0
- xtr_web_profiler/__init__.py +40 -0
- xtr_web_profiler/bundle/__init__.py +14 -0
- xtr_web_profiler/bundle/web_debug_toolbar_middleware_factory.py +41 -0
- xtr_web_profiler/bundle/web_profiler_bundle.py +147 -0
- xtr_web_profiler/bundle/web_profiler_config.py +57 -0
- xtr_web_profiler/controller/__init__.py +7 -0
- xtr_web_profiler/controller/profiler_controller.py +371 -0
- xtr_web_profiler/csp/__init__.py +19 -0
- xtr_web_profiler/csp/content_security_policy_handler.py +126 -0
- xtr_web_profiler/csp/nonce_generator.py +21 -0
- xtr_web_profiler/exception/__init__.py +9 -0
- xtr_web_profiler/exception/invalid_argument_error.py +14 -0
- xtr_web_profiler/exception/template_not_found_error.py +32 -0
- xtr_web_profiler/exception/web_profiler_error.py +13 -0
- xtr_web_profiler/profiler/__init__.py +7 -0
- xtr_web_profiler/profiler/collector_view.py +25 -0
- xtr_web_profiler/profiler/template_manager.py +72 -0
- xtr_web_profiler/py.typed +0 -0
- xtr_web_profiler/recipe/__init__.py +15 -0
- xtr_web_profiler/recipe/files/config/routes/dev/web_profiler.py.tmpl +26 -0
- xtr_web_profiler/recipe/manifest.toml +27 -0
- xtr_web_profiler/routing/__init__.py +7 -0
- xtr_web_profiler/routing/_endpoints.py +123 -0
- xtr_web_profiler/templates/collector/ajax.html.j2 +71 -0
- xtr_web_profiler/templates/collector/config.html.j2 +167 -0
- xtr_web_profiler/templates/collector/events.html.j2 +142 -0
- xtr_web_profiler/templates/collector/exception.html.j2 +135 -0
- xtr_web_profiler/templates/collector/logger.html.j2 +151 -0
- xtr_web_profiler/templates/collector/memory.html.j2 +60 -0
- xtr_web_profiler/templates/collector/request.html.j2 +155 -0
- xtr_web_profiler/templates/collector/router.html.j2 +147 -0
- xtr_web_profiler/templates/collector/time.html.j2 +388 -0
- xtr_web_profiler/templates/profiler/base.html.j2 +1462 -0
- xtr_web_profiler/templates/profiler/info.html.j2 +33 -0
- xtr_web_profiler/templates/profiler/layout.html.j2 +65 -0
- xtr_web_profiler/templates/profiler/open.html.j2 +59 -0
- xtr_web_profiler/templates/profiler/results.html.j2 +75 -0
- xtr_web_profiler/templates/profiler/toolbar.css.j2 +584 -0
- xtr_web_profiler/templates/profiler/toolbar.html.j2 +64 -0
- xtr_web_profiler/templates/profiler/toolbar_item.html.j2 +27 -0
- xtr_web_profiler/templates/profiler/toolbar_js.html.j2 +454 -0
- xtr_web_profiler/templates/profiler/toolbar_redirect.html.j2 +167 -0
- xtr_web_profiler/templating/__init__.py +16 -0
- xtr_web_profiler/templating/dump.py +91 -0
- xtr_web_profiler/templating/environment_factory.py +86 -0
- xtr_web_profiler/templating/template_loader.py +65 -0
- xtr_web_profiler/web_debug_toolbar_middleware.py +281 -0
- xtr_web_profiler-7.0.0.dist-info/METADATA +322 -0
- xtr_web_profiler-7.0.0.dist-info/RECORD +53 -0
- xtr_web_profiler-7.0.0.dist-info/WHEEL +4 -0
- xtr_web_profiler-7.0.0.dist-info/entry_points.txt +6 -0
- xtr_web_profiler-7.0.0.dist-info/licenses/LICENSE +22 -0
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: xtr-web-profiler
|
|
3
|
+
description: How to use xtr-web-profiler, the development toolbar drawn into every HTML page and the pages showing each request's profile (request, timeline, memory, logs, events, routing, exception, background requests). Use when adding the toolbar or the profiler to an application in dev, reading a request's profile from X-Debug-Token or /_profiler, writing a panel for a library's own data collector, pointing frame links at an editor, intercepting redirects, a toolbar missing from a page, or WebProfilerBundle being added to an application on xtr-dependency-injection.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# xtr-web-profiler
|
|
7
|
+
|
|
8
|
+
In `dev`, every HTML page gets a toolbar at its foot, and every request a profile under
|
|
9
|
+
`/_profiler/<token>`. The profiles are collected by the http kernel's profiler; this package draws
|
|
10
|
+
them and the panels that show them.
|
|
11
|
+
|
|
12
|
+
## Quick reference
|
|
13
|
+
|
|
14
|
+
- Add it: `uv run xtr-recipes recipes:add --dev xtr-web-profiler` — lists `WebProfilerBundle` for
|
|
15
|
+
`dev` and writes `config/routes/dev/web_profiler.py`.
|
|
16
|
+
- Find a profile: the response's `X-Debug-Token` names it, `X-Debug-Token-Link` is its page;
|
|
17
|
+
`/_profiler/latest` is the newest, `/_profiler/` the search.
|
|
18
|
+
- Configure: `WebProfilerConfig(toolbar=True, ajax_replace=False, intercept_redirects=False,
|
|
19
|
+
excluded_ajax_paths="^/(_wdt|_profiler)", file_link_format=None)`, from
|
|
20
|
+
`xtr_web_profiler.bundle`.
|
|
21
|
+
- What is profiled is the http kernel's business: `HttpKernelConfig.profiler` — see the
|
|
22
|
+
`xtr-http-kernel` skill.
|
|
23
|
+
- Template names: `"@<import package>/<path under templates/>"`.
|
|
24
|
+
|
|
25
|
+
## Open frame links in the editor
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
# app/config/packages/dev/web_profiler.py
|
|
29
|
+
from xtr_dependency_injection import configure
|
|
30
|
+
|
|
31
|
+
from xtr_web_profiler.bundle import WebProfilerConfig
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@configure
|
|
35
|
+
def web_profiler() -> WebProfilerConfig:
|
|
36
|
+
return WebProfilerConfig(file_link_format="pycharm://open?file={file}&line={line}")
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
`{file}` and `{line}` are the placeholders — never `%f`, which the container reads as a parameter.
|
|
40
|
+
|
|
41
|
+
## Write a panel for a collector
|
|
42
|
+
|
|
43
|
+
Register the collector in the library's bundle with the template it is shown with (see the
|
|
44
|
+
`xtr-http-kernel` skill for the collector itself), and ship the template under the library's
|
|
45
|
+
`templates/` folder:
|
|
46
|
+
|
|
47
|
+
```jinja
|
|
48
|
+
{# src/xtr_orm/templates/collector/orm.html.j2 #}
|
|
49
|
+
{% block toolbar %}
|
|
50
|
+
{% from "@xtr_web_profiler/profiler/toolbar_item.html.j2" import toolbar_item %}
|
|
51
|
+
{% set icon %}<span class="xtr-toolbar-value">{{ collector.data.count }} queries</span>{% endset %}
|
|
52
|
+
{% set text %}<b>Time</b> {{ collector.data.duration_ms | format_ms }}{% endset %}
|
|
53
|
+
{{ toolbar_item(collector.name, icon, text, link=panel_url(collector.name)) }}
|
|
54
|
+
{% endblock %}
|
|
55
|
+
{% block menu %}<span class="xtr-label">Database</span>{% endblock %}
|
|
56
|
+
{% block panel %}
|
|
57
|
+
{% for query in collector.data.queries %}<pre>{{ query.sql }}</pre>{% endfor %}
|
|
58
|
+
{% endblock %}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
- Each block is rendered on its own: do not `extends`, and import inside the block.
|
|
62
|
+
- A block sees `collector` (`.name`, `.data`), `profile`, `token`, `url`, `panel_url`,
|
|
63
|
+
`open_file_url`, `csp_script_nonce`, `csp_style_nonce`; any other name fails.
|
|
64
|
+
- `toolbar_item(name, icon, text, link=None, status="normal")`, `status` one of `normal`, `green`,
|
|
65
|
+
`yellow`, `red`.
|
|
66
|
+
- Filters: `profiler_dump` (a `clone_var` tree), `format_bytes`, `format_ms`, `format_time`,
|
|
67
|
+
`file_link(file, line)`.
|
|
68
|
+
- An inline `<script>` or `<style>` carries `nonce="{{ csp_script_nonce }}"` /
|
|
69
|
+
`nonce="{{ csp_style_nonce }}"`.
|
|
70
|
+
|
|
71
|
+
## Read a profile in a test
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
from xtr_http_kernel.profiler import FileProfilerStorage
|
|
75
|
+
|
|
76
|
+
response = await client.get("/books")
|
|
77
|
+
profile = FileProfilerStorage(folder).read(response.headers["x-debug-token"])
|
|
78
|
+
assert profile is not None
|
|
79
|
+
assert profile.get_collector("router")["name"] == "show_book"
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Point `HttpKernelConfig.profiler.storage_path` at the test's folder to read its profiles.
|
|
83
|
+
|
|
84
|
+
## Testing
|
|
85
|
+
|
|
86
|
+
- Every page is served through the http kernel, so drive the application with
|
|
87
|
+
`httpx.ASGITransport` inside its lifespan, as the `xtr-http-kernel` skill shows.
|
|
88
|
+
- A test environment other than `dev` has no profiler routes: the response then says so in
|
|
89
|
+
`X-Debug-Error`, and no toolbar is drawn.
|
|
90
|
+
- `assert_zero_config(WebProfilerBundle)` from `xtr_dependency_injection.testing` holds.
|
|
91
|
+
|
|
92
|
+
## Use in an application
|
|
93
|
+
|
|
94
|
+
1. `uv run xtr-recipes recipes:add --dev xtr-web-profiler`.
|
|
95
|
+
2. The recipe lists `WebProfilerBundle: {"dev": True}` in `config/bundles.py` and writes
|
|
96
|
+
`config/routes/dev/web_profiler.py`, serving `/_wdt` and `/_profiler`.
|
|
97
|
+
3. Call `setup(app, kernel)` where the application is built, if it does not already.
|
|
98
|
+
4. Install `xtr-logging` beside it for the log panel.
|
|
99
|
+
5. Check: `debug:bundles` shows `web_profiler` active in `dev`; an HTML page shows the toolbar;
|
|
100
|
+
`GET /_profiler/latest` answers.
|
|
101
|
+
6. Remove: `uv run xtr-recipes recipes:remove xtr-web-profiler`.
|
|
102
|
+
|
|
103
|
+
## Errors
|
|
104
|
+
|
|
105
|
+
| Error | Raised when |
|
|
106
|
+
|---|---|
|
|
107
|
+
| `InvalidArgumentError` | `excluded_ajax_paths` is no regular expression, or `file_link_format` lacks `{file}`; also a `ValueError` |
|
|
108
|
+
| `TemplateNotFoundError` | a collector's tag names a template that does not exist; also a `LookupError` |
|
|
109
|
+
|
|
110
|
+
A toolbar missing from a page: the response is not HTML, is an attachment, is compressed, is a
|
|
111
|
+
redirect, carries no `X-Debug-Token` (the path is excluded from profiling, or profiling is off), or
|
|
112
|
+
the request was a background one.
|
|
113
|
+
|
|
114
|
+
## Do not
|
|
115
|
+
|
|
116
|
+
- Do not list `WebProfilerBundle` outside `dev`, and do not move its routes file out of
|
|
117
|
+
`config/routes/dev/`: a profile holds every request's headers, cookies and state.
|
|
118
|
+
- Do not install it as a regular dependency; it is `--dev`.
|
|
119
|
+
- Do not `extends` in a collector template, nor use `|safe` on collector data.
|
|
120
|
+
- Do not raise `HttpKernelConfig.middleware_priority` above `1024`: the toolbar middleware must
|
|
121
|
+
stay outside the lifecycle to see the profile's token.
|
|
122
|
+
- Do not write `%f`/`%l` in `file_link_format`.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"""A development toolbar on every page, and a profile of every request.
|
|
2
|
+
|
|
3
|
+
In the development environment, each HTML page an application serves gets a
|
|
4
|
+
toolbar at its foot: the status, the time it took, the memory, the log, the
|
|
5
|
+
events, the route, the background requests the page made since. Each item
|
|
6
|
+
opens the request's full profile — request and response, a timeline of where
|
|
7
|
+
the time went, every log record, every listener, the exception with its
|
|
8
|
+
frames — and every profile stays searchable for two days.
|
|
9
|
+
|
|
10
|
+
The profiles are collected by the http kernel's profiler; this package draws
|
|
11
|
+
them. A library adds a panel by registering a data collector with a template.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
17
|
+
|
|
18
|
+
from .controller.profiler_controller import PROFILER_PAGE_HEADER, ProfilerController
|
|
19
|
+
from .exception import InvalidArgumentError, TemplateNotFoundError, WebProfilerError
|
|
20
|
+
from .profiler.collector_view import CollectorView
|
|
21
|
+
from .profiler.template_manager import TemplateManager
|
|
22
|
+
from .web_debug_toolbar_middleware import WebDebugToolbarMiddleware
|
|
23
|
+
|
|
24
|
+
try:
|
|
25
|
+
__version__ = version("xtr-web-profiler")
|
|
26
|
+
except PackageNotFoundError: # pragma: no cover
|
|
27
|
+
# Running from a source tree with no installed metadata to read.
|
|
28
|
+
__version__ = "0+unknown"
|
|
29
|
+
|
|
30
|
+
__all__ = [
|
|
31
|
+
"PROFILER_PAGE_HEADER",
|
|
32
|
+
"CollectorView",
|
|
33
|
+
"InvalidArgumentError",
|
|
34
|
+
"ProfilerController",
|
|
35
|
+
"TemplateManager",
|
|
36
|
+
"TemplateNotFoundError",
|
|
37
|
+
"WebDebugToolbarMiddleware",
|
|
38
|
+
"WebProfilerError",
|
|
39
|
+
"__version__",
|
|
40
|
+
]
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"""The xtr-dependency-injection bundle for xtr-web-profiler."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from .web_debug_toolbar_middleware_factory import WebDebugToolbarMiddlewareFactory
|
|
6
|
+
from .web_profiler_bundle import WEB_DEBUG_TOOLBAR_PRIORITY, WebProfilerBundle
|
|
7
|
+
from .web_profiler_config import WebProfilerConfig
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"WEB_DEBUG_TOOLBAR_PRIORITY",
|
|
11
|
+
"WebDebugToolbarMiddlewareFactory",
|
|
12
|
+
"WebProfilerBundle",
|
|
13
|
+
"WebProfilerConfig",
|
|
14
|
+
]
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"""The middleware factory the bundle tags for the kernel to order into the stack."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import TYPE_CHECKING, final
|
|
6
|
+
|
|
7
|
+
from xtr_web_profiler.web_debug_toolbar_middleware import WebDebugToolbarMiddleware
|
|
8
|
+
|
|
9
|
+
if TYPE_CHECKING:
|
|
10
|
+
from starlette.types import ASGIApp
|
|
11
|
+
|
|
12
|
+
from xtr_web_profiler.csp.content_security_policy_handler import ContentSecurityPolicyHandler
|
|
13
|
+
from xtr_web_profiler.profiler.template_manager import TemplateManager
|
|
14
|
+
|
|
15
|
+
from .web_profiler_config import WebProfilerConfig
|
|
16
|
+
|
|
17
|
+
__all__ = ["WebDebugToolbarMiddlewareFactory"]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@final
|
|
21
|
+
class WebDebugToolbarMiddlewareFactory:
|
|
22
|
+
"""Wraps a downstream app in the toolbar middleware; tagged for the kernel's stack."""
|
|
23
|
+
|
|
24
|
+
__slots__ = ("_config", "_csp", "_manager")
|
|
25
|
+
|
|
26
|
+
def __init__(
|
|
27
|
+
self,
|
|
28
|
+
config: WebProfilerConfig,
|
|
29
|
+
manager: TemplateManager,
|
|
30
|
+
csp: ContentSecurityPolicyHandler,
|
|
31
|
+
) -> None:
|
|
32
|
+
"""Hand every middleware built ``config``, ``manager`` and ``csp``."""
|
|
33
|
+
self._config = config
|
|
34
|
+
self._manager = manager
|
|
35
|
+
self._csp = csp
|
|
36
|
+
|
|
37
|
+
def __call__(self, app: ASGIApp) -> ASGIApp:
|
|
38
|
+
"""Return ``app`` wrapped in the toolbar middleware."""
|
|
39
|
+
return WebDebugToolbarMiddleware(
|
|
40
|
+
app, config=self._config, manager=self._manager, csp=self._csp
|
|
41
|
+
)
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"""The xtr-web-profiler bundle: the toolbar on every page, and the pages showing each profile.
|
|
2
|
+
|
|
3
|
+
Listed for the development environment, it turns the http kernel's profiler
|
|
4
|
+
on there, puts the toolbar middleware just outside the request lifecycle,
|
|
5
|
+
and contributes two route resources — ``web_profiler_wdt`` and
|
|
6
|
+
``web_profiler`` — that a routes file of the development environment
|
|
7
|
+
includes.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import warnings
|
|
13
|
+
from dataclasses import replace
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
from typing import TYPE_CHECKING, Final, final, override
|
|
16
|
+
|
|
17
|
+
from jinja2 import Environment # noqa: TC002 — read at runtime by the container
|
|
18
|
+
from xtr_dependency_injection import Bundle, as_bundle, required_bundle
|
|
19
|
+
from xtr_http_kernel import MIDDLEWARE_TAG
|
|
20
|
+
from xtr_http_kernel.bundle import HttpKernelBundle, HttpKernelConfig
|
|
21
|
+
from xtr_http_kernel.data_collector import DataCollectorTemplates
|
|
22
|
+
from xtr_http_kernel.profiler import Profiler # noqa: TC002 — read at runtime by the container
|
|
23
|
+
|
|
24
|
+
from xtr_web_profiler.controller.profiler_controller import ProfilerController
|
|
25
|
+
from xtr_web_profiler.csp.content_security_policy_handler import ContentSecurityPolicyHandler
|
|
26
|
+
from xtr_web_profiler.csp.nonce_generator import NonceGenerator
|
|
27
|
+
from xtr_web_profiler.profiler.template_manager import TemplateManager
|
|
28
|
+
from xtr_web_profiler.templating.environment_factory import create_environment
|
|
29
|
+
|
|
30
|
+
from .web_debug_toolbar_middleware_factory import WebDebugToolbarMiddlewareFactory
|
|
31
|
+
from .web_profiler_config import WebProfilerConfig
|
|
32
|
+
|
|
33
|
+
if TYPE_CHECKING:
|
|
34
|
+
from collections.abc import Callable
|
|
35
|
+
|
|
36
|
+
from xtr_dependency_injection import ContainerBuilder, ServiceConfigurator
|
|
37
|
+
|
|
38
|
+
__all__ = ["WEB_DEBUG_TOOLBAR_PRIORITY", "WebProfilerBundle"]
|
|
39
|
+
|
|
40
|
+
WEB_DEBUG_TOOLBAR_PRIORITY: Final = 1024
|
|
41
|
+
"""Where the toolbar middleware sits: outside the request lifecycle's, at its default 0.
|
|
42
|
+
|
|
43
|
+
It must stay outside — above ``HttpKernelConfig.middleware_priority`` — to
|
|
44
|
+
see the ``X-Debug-Token`` the lifecycle's listeners put on a response.
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
_PRODUCTION: Final = "prod"
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
@final
|
|
51
|
+
@required_bundle(HttpKernelBundle)
|
|
52
|
+
@as_bundle("web_profiler", config=WebProfilerConfig)
|
|
53
|
+
class WebProfilerBundle(Bundle[WebProfilerConfig]):
|
|
54
|
+
"""Registers the toolbar middleware, the profiler's pages and their route resources."""
|
|
55
|
+
|
|
56
|
+
@override
|
|
57
|
+
def prepend_extension(self, builder: ContainerBuilder) -> None:
|
|
58
|
+
"""Turn the http kernel's profiler on, wherever this bundle is listed.
|
|
59
|
+
|
|
60
|
+
An application turning it off again — with a transform of the http
|
|
61
|
+
kernel's configuration — keeps the pages, which then answer 404.
|
|
62
|
+
"""
|
|
63
|
+
builder.prepend_extension_config(HttpKernelConfig, _with_profiler)
|
|
64
|
+
|
|
65
|
+
@override
|
|
66
|
+
def load_extension(
|
|
67
|
+
self,
|
|
68
|
+
config: WebProfilerConfig,
|
|
69
|
+
services: ServiceConfigurator,
|
|
70
|
+
builder: ContainerBuilder,
|
|
71
|
+
) -> None:
|
|
72
|
+
"""Register the pages, the template environment and the toolbar middleware.
|
|
73
|
+
|
|
74
|
+
Warns when the kernel runs in production: every profile holds every
|
|
75
|
+
request's headers, cookies and state, readable by whoever reaches the
|
|
76
|
+
pages.
|
|
77
|
+
"""
|
|
78
|
+
if builder.get_parameter("kernel.environment") == _PRODUCTION:
|
|
79
|
+
warnings.warn(
|
|
80
|
+
"The web profiler is active in the prod environment: it exposes the data of "
|
|
81
|
+
"every request. List WebProfilerBundle for the dev environment only.",
|
|
82
|
+
UserWarning,
|
|
83
|
+
stacklevel=2,
|
|
84
|
+
)
|
|
85
|
+
profiling = builder.get_extension_config(HttpKernelConfig).profiler.enabled
|
|
86
|
+
project_dir = Path(str(builder.get_parameter("kernel.project_dir")))
|
|
87
|
+
_ = services.set(_environment(config.file_link_format))
|
|
88
|
+
_ = services.set(NonceGenerator)
|
|
89
|
+
_ = services.set(_csp_handler)
|
|
90
|
+
if profiling:
|
|
91
|
+
_ = services.set(_template_manager)
|
|
92
|
+
_ = services.set(_controller(project_dir))
|
|
93
|
+
if config.toolbar or config.intercept_redirects:
|
|
94
|
+
_ = services.set(_middleware_factory(config)).add_tag(
|
|
95
|
+
MIDDLEWARE_TAG, priority=WEB_DEBUG_TOOLBAR_PRIORITY
|
|
96
|
+
)
|
|
97
|
+
else:
|
|
98
|
+
_ = services.set(_unprofiled_controller(project_dir))
|
|
99
|
+
services.load("xtr_web_profiler.routing")
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def _with_profiler(config: HttpKernelConfig) -> HttpKernelConfig:
|
|
103
|
+
return replace(config, profiler=replace(config.profiler, enabled=True))
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def _environment(file_link_format: str | None) -> Callable[[], Environment]:
|
|
107
|
+
def environment() -> Environment:
|
|
108
|
+
return create_environment(file_link_format)
|
|
109
|
+
|
|
110
|
+
return environment
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _csp_handler(generator: NonceGenerator) -> ContentSecurityPolicyHandler:
|
|
114
|
+
return ContentSecurityPolicyHandler(generator)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def _template_manager(
|
|
118
|
+
environment: Environment, templates: DataCollectorTemplates
|
|
119
|
+
) -> TemplateManager:
|
|
120
|
+
return TemplateManager(environment, templates)
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def _controller(project_dir: Path) -> Callable[[Profiler, TemplateManager], ProfilerController]:
|
|
124
|
+
def controller(profiler: Profiler, manager: TemplateManager) -> ProfilerController:
|
|
125
|
+
return ProfilerController(profiler, manager, project_dir)
|
|
126
|
+
|
|
127
|
+
return controller
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def _unprofiled_controller(project_dir: Path) -> Callable[[Environment], ProfilerController]:
|
|
131
|
+
def controller(environment: Environment) -> ProfilerController:
|
|
132
|
+
return ProfilerController(
|
|
133
|
+
None, TemplateManager(environment, DataCollectorTemplates()), project_dir
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
return controller
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def _middleware_factory(
|
|
140
|
+
config: WebProfilerConfig,
|
|
141
|
+
) -> Callable[[TemplateManager, ContentSecurityPolicyHandler], WebDebugToolbarMiddlewareFactory]:
|
|
142
|
+
def middleware_factory(
|
|
143
|
+
manager: TemplateManager, csp: ContentSecurityPolicyHandler
|
|
144
|
+
) -> WebDebugToolbarMiddlewareFactory:
|
|
145
|
+
return WebDebugToolbarMiddlewareFactory(config, manager, csp)
|
|
146
|
+
|
|
147
|
+
return middleware_factory
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"""Configuration for :class:`~xtr_web_profiler.bundle.WebProfilerBundle`."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
|
|
8
|
+
from xtr_web_profiler.exception.invalid_argument_error import InvalidArgumentError
|
|
9
|
+
|
|
10
|
+
__all__ = ["WebProfilerConfig"]
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@dataclass(frozen=True, slots=True)
|
|
14
|
+
class WebProfilerConfig:
|
|
15
|
+
"""What an application may change about the toolbar and the profiler pages.
|
|
16
|
+
|
|
17
|
+
Attributes:
|
|
18
|
+
toolbar: Whether HTML pages get the debug toolbar.
|
|
19
|
+
ajax_replace: Whether a background request whose response names a
|
|
20
|
+
profile reloads the toolbar with that profile.
|
|
21
|
+
intercept_redirects: Whether a redirect answering a page is shown as a
|
|
22
|
+
page of its own — with the toolbar, and a link to follow it — so
|
|
23
|
+
the request that redirected can be inspected before it is left.
|
|
24
|
+
excluded_ajax_paths: A regular expression; background requests to a
|
|
25
|
+
matching path are left out of the toolbar's list.
|
|
26
|
+
file_link_format: How a file and line open in an editor — ``{file}``
|
|
27
|
+
is the file, ``{line}`` the line, as in
|
|
28
|
+
``"pycharm://open?file={file}&line={line}"`` or
|
|
29
|
+
``"vscode://file/{file}:{line}"``. ``None`` opens them in the
|
|
30
|
+
profiler's own source view.
|
|
31
|
+
|
|
32
|
+
Raises:
|
|
33
|
+
InvalidArgumentError: When ``excluded_ajax_paths`` is not a regular
|
|
34
|
+
expression, or ``file_link_format`` does not name the file.
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
toolbar: bool = True
|
|
38
|
+
ajax_replace: bool = False
|
|
39
|
+
intercept_redirects: bool = False
|
|
40
|
+
excluded_ajax_paths: str = "^/(_wdt|_profiler)"
|
|
41
|
+
file_link_format: str | None = None
|
|
42
|
+
|
|
43
|
+
def __post_init__(self) -> None:
|
|
44
|
+
"""Refuse values the toolbar would misread."""
|
|
45
|
+
try:
|
|
46
|
+
_ = re.compile(self.excluded_ajax_paths)
|
|
47
|
+
except re.error as error:
|
|
48
|
+
message = (
|
|
49
|
+
f"excluded_ajax_paths is {self.excluded_ajax_paths!r}, "
|
|
50
|
+
"which is not a regular expression"
|
|
51
|
+
)
|
|
52
|
+
raise InvalidArgumentError(message) from error
|
|
53
|
+
if self.file_link_format is not None and "{file}" not in self.file_link_format:
|
|
54
|
+
message = (
|
|
55
|
+
f"file_link_format must name the file with {{file}}: {self.file_link_format!r}"
|
|
56
|
+
)
|
|
57
|
+
raise InvalidArgumentError(message)
|