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 datetime import datetime
4 from functools import partial
6 from multiprocessing import freeze_support
7 from typing import Set, Tuple
9 from aiohttp import web
14 from _black_version import version as __version__
16 # This is used internally by tests to shut down the server prematurely
17 _stop_signal = asyncio.Event()
20 PROTOCOL_VERSION_HEADER = "X-Protocol-Version"
21 LINE_LENGTH_HEADER = "X-Line-Length"
22 PYTHON_VARIANT_HEADER = "X-Python-Variant"
23 SKIP_STRING_NORMALIZATION_HEADER = "X-Skip-String-Normalization"
24 FAST_OR_SAFE_HEADER = "X-Fast-Or-Safe"
25 DIFF_HEADER = "X-Diff"
28 PROTOCOL_VERSION_HEADER,
30 PYTHON_VARIANT_HEADER,
31 SKIP_STRING_NORMALIZATION_HEADER,
37 BLACK_VERSION_HEADER = "X-Black-Version"
40 class InvalidVariantHeader(Exception):
44 @click.command(context_settings={"help_option_names": ["-h", "--help"]})
46 "--bind-host", type=str, help="Address to bind the server to.", default="localhost"
48 @click.option("--bind-port", type=int, help="Port to listen on", default=45484)
49 @click.version_option(version=black.__version__)
50 def main(bind_host: str, bind_port: int) -> None:
51 logging.basicConfig(level=logging.INFO)
53 ver = black.__version__
54 black.out(f"blackd version {ver} listening on {bind_host} port {bind_port}")
55 web.run_app(app, host=bind_host, port=bind_port, handle_signals=True, print=None)
58 def make_app() -> web.Application:
59 app = web.Application()
60 executor = ProcessPoolExecutor()
62 cors = aiohttp_cors.setup(app)
63 resource = cors.add(app.router.add_resource("/"))
65 resource.add_route("POST", partial(handle, executor=executor)),
67 "*": aiohttp_cors.ResourceOptions(
68 allow_headers=(*BLACK_HEADERS, "Content-Type"), expose_headers="*"
76 async def handle(request: web.Request, executor: Executor) -> web.Response:
77 headers = {BLACK_VERSION_HEADER: __version__}
79 if request.headers.get(PROTOCOL_VERSION_HEADER, "1") != "1":
81 status=501, text="This server only supports protocol version 1"
85 request.headers.get(LINE_LENGTH_HEADER, black.DEFAULT_LINE_LENGTH)
88 return web.Response(status=400, text="Invalid line length header value")
90 if PYTHON_VARIANT_HEADER in request.headers:
91 value = request.headers[PYTHON_VARIANT_HEADER]
93 pyi, versions = parse_python_variant_header(value)
94 except InvalidVariantHeader as e:
97 text=f"Invalid value for {PYTHON_VARIANT_HEADER}: {e.args[0]}",
103 skip_string_normalization = bool(
104 request.headers.get(SKIP_STRING_NORMALIZATION_HEADER, False)
107 if request.headers.get(FAST_OR_SAFE_HEADER, "safe") == "fast":
109 mode = black.FileMode(
110 target_versions=versions,
112 line_length=line_length,
113 string_normalization=not skip_string_normalization,
115 req_bytes = await request.content.read()
116 charset = request.charset if request.charset is not None else "utf8"
117 req_str = req_bytes.decode(charset)
118 then = datetime.utcnow()
120 loop = asyncio.get_event_loop()
121 formatted_str = await loop.run_in_executor(
122 executor, partial(black.format_file_contents, req_str, fast=fast, mode=mode)
125 # Only output the diff in the HTTP response
126 only_diff = bool(request.headers.get(DIFF_HEADER, False))
128 now = datetime.utcnow()
129 src_name = f"In\t{then} +0000"
130 dst_name = f"Out\t{now} +0000"
131 loop = asyncio.get_event_loop()
132 formatted_str = await loop.run_in_executor(
134 partial(black.diff, req_str, formatted_str, src_name, dst_name),
138 content_type=request.content_type,
143 except black.NothingChanged:
144 return web.Response(status=204, headers=headers)
145 except black.InvalidInput as e:
146 return web.Response(status=400, headers=headers, text=str(e))
147 except Exception as e:
148 logging.exception("Exception during handling a request")
149 return web.Response(status=500, headers=headers, text=str(e))
152 def parse_python_variant_header(value: str) -> Tuple[bool, Set[black.TargetVersion]]:
157 for version in value.split(","):
158 if version.startswith("py"):
159 version = version[len("py") :]
161 major_str, *rest = version.split(".")
163 major_str = version[0]
164 rest = [version[1:]] if len(version) > 1 else []
166 major = int(major_str)
167 if major not in (2, 3):
168 raise InvalidVariantHeader("major version must be 2 or 3")
171 if major == 2 and minor != 7:
172 raise InvalidVariantHeader(
173 "minor version must be 7 for Python 2"
176 # Default to lowest supported minor version.
177 minor = 7 if major == 2 else 3
178 version_str = f"PY{major}{minor}"
179 if major == 3 and not hasattr(black.TargetVersion, version_str):
180 raise InvalidVariantHeader(f"3.{minor} is not supported")
181 versions.add(black.TargetVersion[version_str])
182 except (KeyError, ValueError):
183 raise InvalidVariantHeader("expected e.g. '3.7', 'py3.5'")
184 return False, versions
187 def patched_main() -> None:
193 if __name__ == "__main__":