src/blackd/__init__.py
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | import asyncio
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | import logging
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from concurrent.futures import Executor, ProcessPoolExecutor
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from datetime import datetime, timezone
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from functools import partial
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from multiprocessing import freeze_support
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from typing import Set, Tuple
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | try:
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from aiohttp import web
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from .middlewares import cors
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | except ImportError as ie:
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | raise ImportError(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | f"aiohttp dependency is not installed: {ie}. "
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | + "Please re-install black with the '[d]' extra install "
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | + "to obtain aiohttp_cors: `pip install black[d]`"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ) from None
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | import click
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | import black
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from _black_version import version as __version__
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from black.concurrency import maybe_install_uvloop
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | # This is used internally by tests to shut down the server prematurely
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | _stop_signal = asyncio.Event()
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | # Request headers
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | PROTOCOL_VERSION_HEADER = "X-Protocol-Version"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | LINE_LENGTH_HEADER = "X-Line-Length"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | PYTHON_VARIANT_HEADER = "X-Python-Variant"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | SKIP_SOURCE_FIRST_LINE = "X-Skip-Source-First-Line"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | SKIP_STRING_NORMALIZATION_HEADER = "X-Skip-String-Normalization"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | SKIP_MAGIC_TRAILING_COMMA = "X-Skip-Magic-Trailing-Comma"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | PREVIEW = "X-Preview"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | FAST_OR_SAFE_HEADER = "X-Fast-Or-Safe"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | DIFF_HEADER = "X-Diff"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | BLACK_HEADERS = [
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | PROTOCOL_VERSION_HEADER,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | LINE_LENGTH_HEADER,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | PYTHON_VARIANT_HEADER,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | SKIP_SOURCE_FIRST_LINE,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | SKIP_STRING_NORMALIZATION_HEADER,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | SKIP_MAGIC_TRAILING_COMMA,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | PREVIEW,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | FAST_OR_SAFE_HEADER,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | DIFF_HEADER,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | ]
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | # Response headers
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | BLACK_VERSION_HEADER = "X-Black-Version"
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- 01 -- --- --- --- --- --- --- ------- ------- ------- | class InvalidVariantHeader(Exception):
---- ---- ---- ---- ---- ---- ---- 01 -- --- --- --- --- --- --- ------- ------- ------- | pass
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.command(context_settings={"help_option_names": ["-h", "--help"]})
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.option(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "--bind-host",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | type=str,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | help="Address to bind the server to.",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | default="localhost",
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | show_default=True,
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.option(
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | "--bind-port", type=int, help="Port to listen on", default=45484, show_default=True
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | )
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | @click.version_option(version=black.__version__)
0018 0011 0018 0000 0000 0000 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def main(bind_host: str, bind_port: int) -> None:
0018 0011 0018 0000 0000 0000 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | logging.basicConfig(level=logging.INFO)
0018 0011 0018 0000 0000 0000 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | app = make_app()
0018 0011 0018 0000 0000 0000 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | ver = black.__version__
0018 0011 0018 0000 0000 0000 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | black.out(f"blackd version {ver} listening on {bind_host} port {bind_port}")
0018 0011 0018 0000 0000 0000 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | web.run_app(app, host=bind_host, port=bind_port, handle_signals=True, print=None)
0018 0011 0018 0000 0000 0000 0000 -- -- --- --- --- --- --- --- ------- ------- ------- |
0018 0011 0018 0000 0000 0000 0000 -- -- --- --- --- --- --- --- ------- ------- ------- |
0007 0005 0007 0000 0000 0000 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def make_app() -> web.Application:
0007 0005 0007 0000 0000 0000 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | app = web.Application(
0007 0005 0007 0000 0000 0000 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | middlewares=[cors(allow_headers=(*BLACK_HEADERS, "Content-Type"))]
0007 0005 0007 0000 0000 0000 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | )
0007 0005 0007 0000 0000 0000 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | executor = ProcessPoolExecutor()
0007 0005 0007 0000 0000 0000 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | app.add_routes([web.post("/", partial(handle, executor=executor))])
0007 0005 0007 0000 0000 0000 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | return app
0018 0011 0018 0000 0000 0000 0000 -- -- --- --- --- --- --- --- ------- ------- ------- |
0018 0011 0018 0000 0000 0000 0000 -- -- --- --- --- --- --- --- ------- ------- ------- |
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | async def handle(request: web.Request, executor: Executor) -> web.Response:
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | headers = {BLACK_VERSION_HEADER: __version__}
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | try:
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | if request.headers.get(PROTOCOL_VERSION_HEADER, "1") != "1":
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | return web.Response(
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | status=501, text="This server only supports protocol version 1"
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | )
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | try:
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | line_length = int(
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | request.headers.get(LINE_LENGTH_HEADER, black.DEFAULT_LINE_LENGTH)
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | )
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | except ValueError:
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | return web.Response(status=400, text="Invalid line length header value")
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 |
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | if PYTHON_VARIANT_HEADER in request.headers:
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | value = request.headers[PYTHON_VARIANT_HEADER]
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | try:
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | pyi, versions = parse_python_variant_header(value)
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | except InvalidVariantHeader as e:
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | return web.Response(
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | status=400,
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | text=f"Invalid value for {PYTHON_VARIANT_HEADER}: {e.args[0]}",
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | )
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | else:
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | pyi = False
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | versions = set()
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 |
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | skip_string_normalization = bool(
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | request.headers.get(SKIP_STRING_NORMALIZATION_HEADER, False)
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | )
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | skip_magic_trailing_comma = bool(
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | request.headers.get(SKIP_MAGIC_TRAILING_COMMA, False)
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | )
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | skip_source_first_line = bool(
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | request.headers.get(SKIP_SOURCE_FIRST_LINE, False)
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | )
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | preview = bool(request.headers.get(PREVIEW, False))
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | fast = False
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | if request.headers.get(FAST_OR_SAFE_HEADER, "safe") == "fast":
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | fast = True
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | mode = black.FileMode(
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | target_versions=versions,
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | is_pyi=pyi,
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | line_length=line_length,
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | skip_source_first_line=skip_source_first_line,
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | string_normalization=not skip_string_normalization,
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | magic_trailing_comma=not skip_magic_trailing_comma,
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | preview=preview,
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | )
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | req_bytes = await request.content.read()
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | charset = request.charset if request.charset is not None else "utf8"
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | req_str = req_bytes.decode(charset)
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | then = datetime.now(timezone.utc)
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 |
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | header = ""
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | if skip_source_first_line:
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | first_newline_position: int = req_str.find("\n") + 1
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | header = req_str[:first_newline_position]
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | req_str = req_str[first_newline_position:]
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 |
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | loop = asyncio.get_event_loop()
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | formatted_str = await loop.run_in_executor(
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | executor, partial(black.format_file_contents, req_str, fast=fast, mode=mode)
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | )
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 |
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | # Preserve CRLF line endings
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | nl = req_str.find("\n")
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | if nl > 0 and req_str[nl - 1] == "\r":
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | formatted_str = formatted_str.replace("\n", "\r\n")
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | # If, after swapping line endings, nothing changed, then say so
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | if formatted_str == req_str:
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | raise black.NothingChanged
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 |
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | # Put the source first line back
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | req_str = header + req_str
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | formatted_str = header + formatted_str
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 |
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | # Only output the diff in the HTTP response
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | only_diff = bool(request.headers.get(DIFF_HEADER, False))
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | if only_diff:
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | now = datetime.now(timezone.utc)
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | src_name = f"In\t{then}"
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | dst_name = f"Out\t{now}"
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | loop = asyncio.get_event_loop()
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | formatted_str = await loop.run_in_executor(
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | executor,
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | partial(black.diff, req_str, formatted_str, src_name, dst_name),
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | )
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 |
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | return web.Response(
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | content_type=request.content_type,
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | charset=charset,
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | headers=headers,
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | text=formatted_str,
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | )
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | except black.NothingChanged:
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | return web.Response(status=204, headers=headers)
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | except black.InvalidInput as e:
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | return web.Response(status=400, headers=headers, text=str(e))
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | except Exception as e:
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | logging.exception("Exception during handling a request")
0102 0063 0090 0004 0000 0008 0004 -- 15 009 021 014 026 030 040 0196.28 1093.54 0005.57 | return web.Response(status=500, headers=headers, text=str(e))
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0031 0032 0030 0001 0000 0000 0001 -- 13 006 015 010 019 021 029 0127.38 0484.03 0003.80 | def parse_python_variant_header(value: str) -> Tuple[bool, Set[black.TargetVersion]]:
0031 0032 0030 0001 0000 0000 0001 -- 13 006 015 010 019 021 029 0127.38 0484.03 0003.80 | if value == "pyi":
0031 0032 0030 0001 0000 0000 0001 -- 13 006 015 010 019 021 029 0127.38 0484.03 0003.80 | return True, set()
0031 0032 0030 0001 0000 0000 0001 -- 13 006 015 010 019 021 029 0127.38 0484.03 0003.80 | else:
0031 0032 0030 0001 0000 0000 0001 -- 13 006 015 010 019 021 029 0127.38 0484.03 0003.80 | versions = set()
0031 0032 0030 0001 0000 0000 0001 -- 13 006 015 010 019 021 029 0127.38 0484.03 0003.80 | for version in value.split(","):
0031 0032 0030 0001 0000 0000 0001 -- 13 006 015 010 019 021 029 0127.38 0484.03 0003.80 | if version.startswith("py"):
0031 0032 0030 0001 0000 0000 0001 -- 13 006 015 010 019 021 029 0127.38 0484.03 0003.80 | version = version[len("py") :]
0031 0032 0030 0001 0000 0000 0001 -- 13 006 015 010 019 021 029 0127.38 0484.03 0003.80 | if "." in version:
0031 0032 0030 0001 0000 0000 0001 -- 13 006 015 010 019 021 029 0127.38 0484.03 0003.80 | major_str, *rest = version.split(".")
0031 0032 0030 0001 0000 0000 0001 -- 13 006 015 010 019 021 029 0127.38 0484.03 0003.80 | else:
0031 0032 0030 0001 0000 0000 0001 -- 13 006 015 010 019 021 029 0127.38 0484.03 0003.80 | major_str = version[0]
0031 0032 0030 0001 0000 0000 0001 -- 13 006 015 010 019 021 029 0127.38 0484.03 0003.80 | rest = [version[1:]] if len(version) > 1 else []
0031 0032 0030 0001 0000 0000 0001 -- 13 006 015 010 019 021 029 0127.38 0484.03 0003.80 | try:
0031 0032 0030 0001 0000 0000 0001 -- 13 006 015 010 019 021 029 0127.38 0484.03 0003.80 | major = int(major_str)
0031 0032 0030 0001 0000 0000 0001 -- 13 006 015 010 019 021 029 0127.38 0484.03 0003.80 | if major not in (2, 3):
0031 0032 0030 0001 0000 0000 0001 -- 13 006 015 010 019 021 029 0127.38 0484.03 0003.80 | raise InvalidVariantHeader("major version must be 2 or 3")
0031 0032 0030 0001 0000 0000 0001 -- 13 006 015 010 019 021 029 0127.38 0484.03 0003.80 | if len(rest) > 0:
0031 0032 0030 0001 0000 0000 0001 -- 13 006 015 010 019 021 029 0127.38 0484.03 0003.80 | minor = int(rest[0])
0031 0032 0030 0001 0000 0000 0001 -- 13 006 015 010 019 021 029 0127.38 0484.03 0003.80 | if major == 2:
0031 0032 0030 0001 0000 0000 0001 -- 13 006 015 010 019 021 029 0127.38 0484.03 0003.80 | raise InvalidVariantHeader("Python 2 is not supported")
0031 0032 0030 0001 0000 0000 0001 -- 13 006 015 010 019 021 029 0127.38 0484.03 0003.80 | else:
0031 0032 0030 0001 0000 0000 0001 -- 13 006 015 010 019 021 029 0127.38 0484.03 0003.80 | # Default to lowest supported minor version.
0031 0032 0030 0001 0000 0000 0001 -- 13 006 015 010 019 021 029 0127.38 0484.03 0003.80 | minor = 7 if major == 2 else 3
0031 0032 0030 0001 0000 0000 0001 -- 13 006 015 010 019 021 029 0127.38 0484.03 0003.80 | version_str = f"PY{major}{minor}"
0031 0032 0030 0001 0000 0000 0001 -- 13 006 015 010 019 021 029 0127.38 0484.03 0003.80 | if major == 3 and not hasattr(black.TargetVersion, version_str):
0031 0032 0030 0001 0000 0000 0001 -- 13 006 015 010 019 021 029 0127.38 0484.03 0003.80 | raise InvalidVariantHeader(f"3.{minor} is not supported")
0031 0032 0030 0001 0000 0000 0001 -- 13 006 015 010 019 021 029 0127.38 0484.03 0003.80 | versions.add(black.TargetVersion[version_str])
0031 0032 0030 0001 0000 0000 0001 -- 13 006 015 010 019 021 029 0127.38 0484.03 0003.80 | except (KeyError, ValueError):
0031 0032 0030 0001 0000 0000 0001 -- 13 006 015 010 019 021 029 0127.38 0484.03 0003.80 | raise InvalidVariantHeader("expected e.g. '3.7', 'py3.5'") from None
0031 0032 0030 0001 0000 0000 0001 -- 13 006 015 010 019 021 029 0127.38 0484.03 0003.80 | return False, versions
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0004 0004 0004 0000 0000 0000 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | def patched_main() -> None:
0004 0004 0004 0000 0000 0000 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | maybe_install_uvloop()
0004 0004 0004 0000 0000 0000 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | freeze_support()
0004 0004 0004 0000 0000 0000 0000 -- 01 000 000 000 000 000 000 0000.00 0000.00 0000.00 | main()
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | if __name__ == "__main__":
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | patched_main()