]> jfr.im git - dlqueue.git/blob - venv/lib/python3.11/site-packages/werkzeug/testapp.py
init: venv aand flask
[dlqueue.git] / venv / lib / python3.11 / site-packages / werkzeug / testapp.py
1 """A small application that can be used to test a WSGI server and check
2 it for WSGI compliance.
3 """
4 from __future__ import annotations
5
6 import os
7 import sys
8 import typing as t
9 from textwrap import wrap
10
11 from markupsafe import escape
12
13 from . import __version__ as _werkzeug_version
14 from .wrappers.request import Request
15 from .wrappers.response import Response
16
17 TEMPLATE = """\
18 <!doctype html>
19 <html lang=en>
20 <title>WSGI Information</title>
21 <style type="text/css">
22 @import url(https://fonts.googleapis.com/css?family=Ubuntu);
23
24 body { font-family: 'Lucida Grande', 'Lucida Sans Unicode', 'Geneva',
25 'Verdana', sans-serif; background-color: white; color: #000;
26 font-size: 15px; text-align: center; }
27 div.box { text-align: left; width: 45em; margin: auto; padding: 50px 0;
28 background-color: white; }
29 h1, h2 { font-family: 'Ubuntu', 'Lucida Grande', 'Lucida Sans Unicode',
30 'Geneva', 'Verdana', sans-serif; font-weight: normal; }
31 h1 { margin: 0 0 30px 0; }
32 h2 { font-size: 1.4em; margin: 1em 0 0.5em 0; }
33 table { width: 100%%; border-collapse: collapse; border: 1px solid #AFC5C9 }
34 table th { background-color: #AFC1C4; color: white; font-size: 0.72em;
35 font-weight: normal; width: 18em; vertical-align: top;
36 padding: 0.5em 0 0.1em 0.5em; }
37 table td { border: 1px solid #AFC5C9; padding: 0.1em 0 0.1em 0.5em; }
38 code { font-family: 'Consolas', 'Monaco', 'Bitstream Vera Sans Mono',
39 monospace; font-size: 0.7em; }
40 ul li { line-height: 1.5em; }
41 ul.path { font-size: 0.7em; margin: 0 -30px; padding: 8px 30px;
42 list-style: none; background: #E8EFF0; }
43 ul.path li { line-height: 1.6em; }
44 li.virtual { color: #999; text-decoration: underline; }
45 li.exp { background: white; }
46 </style>
47 <div class="box">
48 <h1>WSGI Information</h1>
49 <p>
50 This page displays all available information about the WSGI server and
51 the underlying Python interpreter.
52 <h2 id="python-interpreter">Python Interpreter</h2>
53 <table>
54 <tr>
55 <th>Python Version
56 <td>%(python_version)s
57 <tr>
58 <th>Platform
59 <td>%(platform)s [%(os)s]
60 <tr>
61 <th>API Version
62 <td>%(api_version)s
63 <tr>
64 <th>Byteorder
65 <td>%(byteorder)s
66 <tr>
67 <th>Werkzeug Version
68 <td>%(werkzeug_version)s
69 </table>
70 <h2 id="wsgi-environment">WSGI Environment</h2>
71 <table>%(wsgi_env)s</table>
72 <h2 id="installed-eggs">Installed Eggs</h2>
73 <p>
74 The following python packages were installed on the system as
75 Python eggs:
76 <ul>%(python_eggs)s</ul>
77 <h2 id="sys-path">System Path</h2>
78 <p>
79 The following paths are the current contents of the load path. The
80 following entries are looked up for Python packages. Note that not
81 all items in this path are folders. Gray and underlined items are
82 entries pointing to invalid resources or used by custom import hooks
83 such as the zip importer.
84 <p>
85 Items with a bright background were expanded for display from a relative
86 path. If you encounter such paths in the output you might want to check
87 your setup as relative paths are usually problematic in multithreaded
88 environments.
89 <ul class="path">%(sys_path)s</ul>
90 </div>
91 """
92
93
94 def iter_sys_path() -> t.Iterator[tuple[str, bool, bool]]:
95 if os.name == "posix":
96
97 def strip(x: str) -> str:
98 prefix = os.path.expanduser("~")
99 if x.startswith(prefix):
100 x = f"~{x[len(prefix) :]}"
101 return x
102
103 else:
104
105 def strip(x: str) -> str:
106 return x
107
108 cwd = os.path.abspath(os.getcwd())
109 for item in sys.path:
110 path = os.path.join(cwd, item or os.path.curdir)
111 yield strip(os.path.normpath(path)), not os.path.isdir(path), path != item
112
113
114 @Request.application
115 def test_app(req: Request) -> Response:
116 """Simple test application that dumps the environment. You can use
117 it to check if Werkzeug is working properly:
118
119 .. sourcecode:: pycon
120
121 >>> from werkzeug.serving import run_simple
122 >>> from werkzeug.testapp import test_app
123 >>> run_simple('localhost', 3000, test_app)
124 * Running on http://localhost:3000/
125
126 The application displays important information from the WSGI environment,
127 the Python interpreter and the installed libraries.
128 """
129 try:
130 import pkg_resources
131 except ImportError:
132 eggs: t.Iterable[t.Any] = ()
133 else:
134 eggs = sorted(
135 pkg_resources.working_set,
136 key=lambda x: x.project_name.lower(),
137 )
138 python_eggs = []
139 for egg in eggs:
140 try:
141 version = egg.version
142 except (ValueError, AttributeError):
143 version = "unknown"
144 python_eggs.append(
145 f"<li>{escape(egg.project_name)} <small>[{escape(version)}]</small>"
146 )
147
148 wsgi_env = []
149 sorted_environ = sorted(req.environ.items(), key=lambda x: repr(x[0]).lower())
150 for key, value in sorted_environ:
151 value = "".join(wrap(str(escape(repr(value)))))
152 wsgi_env.append(f"<tr><th>{escape(key)}<td><code>{value}</code>")
153
154 sys_path = []
155 for item, virtual, expanded in iter_sys_path():
156 class_ = []
157 if virtual:
158 class_.append("virtual")
159 if expanded:
160 class_.append("exp")
161 class_ = f' class="{" ".join(class_)}"' if class_ else ""
162 sys_path.append(f"<li{class_}>{escape(item)}")
163
164 context = {
165 "python_version": "<br>".join(escape(sys.version).splitlines()),
166 "platform": escape(sys.platform),
167 "os": escape(os.name),
168 "api_version": sys.api_version,
169 "byteorder": sys.byteorder,
170 "werkzeug_version": _werkzeug_version,
171 "python_eggs": "\n".join(python_eggs),
172 "wsgi_env": "\n".join(wsgi_env),
173 "sys_path": "\n".join(sys_path),
174 }
175 return Response(TEMPLATE % context, mimetype="text/html")
176
177
178 if __name__ == "__main__":
179 from .serving import run_simple
180
181 run_simple("localhost", 5000, test_app, use_reloader=True)