All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
2 from concurrent.futures import Executor, ProcessPoolExecutor
3 from functools import partial
5 from multiprocessing import freeze_support
6 from typing import Set, Tuple
8 from aiohttp import web
13 from _version import version as __version__
15 # This is used internally by tests to shut down the server prematurely
16 _stop_signal = asyncio.Event()
19 PROTOCOL_VERSION_HEADER = "X-Protocol-Version"
20 LINE_LENGTH_HEADER = "X-Line-Length"
21 PYTHON_VARIANT_HEADER = "X-Python-Variant"
22 SKIP_STRING_NORMALIZATION_HEADER = "X-Skip-String-Normalization"
23 FAST_OR_SAFE_HEADER = "X-Fast-Or-Safe"
26 PROTOCOL_VERSION_HEADER,
28 PYTHON_VARIANT_HEADER,
29 SKIP_STRING_NORMALIZATION_HEADER,
34 BLACK_VERSION_HEADER = "X-Black-Version"
37 class InvalidVariantHeader(Exception):
41 @click.command(context_settings={"help_option_names": ["-h", "--help"]})
43 "--bind-host", type=str, help="Address to bind the server to.", default="localhost"
45 @click.option("--bind-port", type=int, help="Port to listen on", default=45484)
46 @click.version_option(version=black.__version__)
47 def main(bind_host: str, bind_port: int) -> None:
48 logging.basicConfig(level=logging.INFO)
50 ver = black.__version__
51 black.out(f"blackd version {ver} listening on {bind_host} port {bind_port}")
52 web.run_app(app, host=bind_host, port=bind_port, handle_signals=True, print=None)
55 def make_app() -> web.Application:
56 app = web.Application()
57 executor = ProcessPoolExecutor()
59 cors = aiohttp_cors.setup(app)
60 resource = cors.add(app.router.add_resource("/"))
62 resource.add_route("POST", partial(handle, executor=executor)),
64 "*": aiohttp_cors.ResourceOptions(
65 allow_headers=(*BLACK_HEADERS, "Content-Type"), expose_headers="*"
73 async def handle(request: web.Request, executor: Executor) -> web.Response:
74 headers = {BLACK_VERSION_HEADER: __version__}
76 if request.headers.get(PROTOCOL_VERSION_HEADER, "1") != "1":
78 status=501, text="This server only supports protocol version 1"
82 request.headers.get(LINE_LENGTH_HEADER, black.DEFAULT_LINE_LENGTH)
85 return web.Response(status=400, text="Invalid line length header value")
87 if PYTHON_VARIANT_HEADER in request.headers:
88 value = request.headers[PYTHON_VARIANT_HEADER]
90 pyi, versions = parse_python_variant_header(value)
91 except InvalidVariantHeader as e:
94 text=f"Invalid value for {PYTHON_VARIANT_HEADER}: {e.args[0]}",
100 skip_string_normalization = bool(
101 request.headers.get(SKIP_STRING_NORMALIZATION_HEADER, False)
104 if request.headers.get(FAST_OR_SAFE_HEADER, "safe") == "fast":
106 mode = black.FileMode(
107 target_versions=versions,
109 line_length=line_length,
110 string_normalization=not skip_string_normalization,
112 req_bytes = await request.content.read()
113 charset = request.charset if request.charset is not None else "utf8"
114 req_str = req_bytes.decode(charset)
115 loop = asyncio.get_event_loop()
116 formatted_str = await loop.run_in_executor(
117 executor, partial(black.format_file_contents, req_str, fast=fast, mode=mode)
120 content_type=request.content_type,
125 except black.NothingChanged:
126 return web.Response(status=204, headers=headers)
127 except black.InvalidInput as e:
128 return web.Response(status=400, headers=headers, text=str(e))
129 except Exception as e:
130 logging.exception("Exception during handling a request")
131 return web.Response(status=500, headers=headers, text=str(e))
134 def parse_python_variant_header(value: str) -> Tuple[bool, Set[black.TargetVersion]]:
139 for version in value.split(","):
140 if version.startswith("py"):
141 version = version[len("py") :]
143 major_str, *rest = version.split(".")
145 major_str = version[0]
146 rest = [version[1:]] if len(version) > 1 else []
148 major = int(major_str)
149 if major not in (2, 3):
150 raise InvalidVariantHeader("major version must be 2 or 3")
153 if major == 2 and minor != 7:
154 raise InvalidVariantHeader(
155 "minor version must be 7 for Python 2"
158 # Default to lowest supported minor version.
159 minor = 7 if major == 2 else 3
160 version_str = f"PY{major}{minor}"
161 if major == 3 and not hasattr(black.TargetVersion, version_str):
162 raise InvalidVariantHeader(f"3.{minor} is not supported")
163 versions.add(black.TargetVersion[version_str])
164 except (KeyError, ValueError):
165 raise InvalidVariantHeader("expected e.g. '3.7', 'py3.5'")
166 return False, versions
169 def patched_main() -> None:
175 if __name__ == "__main__":