| 1 |
import aiohttp |
| 2 |
from aiohttp.typedefs import StrOrURL |
| 3 |
|
| 4 |
import black |
| 5 |
|
| 6 |
_DEFAULT_HEADERS = {"Content-Type": "text/plain; charset=utf-8"} |
| 7 |
|
| 8 |
|
| 9 |
class BlackDClient: |
| 10 |
def __init__( |
| 11 |
self, |
| 12 |
url: StrOrURL = "http://localhost:9090", |
| 13 |
line_length: int | None = None, |
| 14 |
skip_source_first_line: bool = False, |
| 15 |
skip_string_normalization: bool = False, |
| 16 |
skip_magic_trailing_comma: bool = False, |
| 17 |
preview: bool = False, |
| 18 |
fast: bool = False, |
| 19 |
python_variant: str | None = None, |
| 20 |
diff: bool = False, |
| 21 |
headers: dict[str, str] | None = None, |
| 22 |
): |
| 23 |
""" |
| 24 |
Initialize a BlackDClient object. |
| 25 |
:param url: The URL of the BlackD server. |
| 26 |
:param line_length: The maximum line length. |
| 27 |
Corresponds to the ``--line-length`` CLI option. |
| 28 |
:param skip_source_first_line: True to skip the first line of the source. |
| 29 |
Corresponds to the ``--skip-source-first-line`` CLI option. |
| 30 |
:param skip_string_normalization: True to skip string normalization. |
| 31 |
Corresponds to the ``--skip-string-normalization`` CLI option. |
| 32 |
:param skip_magic_trailing_comma: True to skip magic trailing comma. |
| 33 |
Corresponds to the ``--skip-magic-trailing-comma`` CLI option. |
| 34 |
:param preview: True to enable experimental preview mode. |
| 35 |
Corresponds to the ``--preview`` CLI option. |
| 36 |
:param fast: True to enable fast mode. |
| 37 |
Corresponds to the ``--fast`` CLI option. |
| 38 |
:param python_variant: The Python variant to use. |
| 39 |
Corresponds to the ``--pyi`` CLI option if this is "pyi". |
| 40 |
Otherwise, corresponds to the ``--target-version`` CLI option. |
| 41 |
:param diff: True to enable diff mode. |
| 42 |
Corresponds to the ``--diff`` CLI option. |
| 43 |
:param headers: A dictionary of additional custom headers to send with |
| 44 |
the request. |
| 45 |
""" |
| 46 |
self.url = url |
| 47 |
self.headers = _DEFAULT_HEADERS.copy() |
| 48 |
|
| 49 |
if line_length is not None: |
| 50 |
self.headers["X-Line-Length"] = str(line_length) |
| 51 |
if skip_source_first_line: |
| 52 |
self.headers["X-Skip-Source-First-Line"] = "yes" |
| 53 |
if skip_string_normalization: |
| 54 |
self.headers["X-Skip-String-Normalization"] = "yes" |
| 55 |
if skip_magic_trailing_comma: |
| 56 |
self.headers["X-Skip-Magic-Trailing-Comma"] = "yes" |
| 57 |
if preview: |
| 58 |
self.headers["X-Preview"] = "yes" |
| 59 |
if fast: |
| 60 |
self.headers["X-Fast-Or-Safe"] = "fast" |
| 61 |
if python_variant is not None: |
| 62 |
self.headers["X-Python-Variant"] = python_variant |
| 63 |
if diff: |
| 64 |
self.headers["X-Diff"] = "yes" |
| 65 |
|
| 66 |
if headers is not None: |
| 67 |
self.headers.update(headers) |
| 68 |
|
| 69 |
async def format_code(self, unformatted_code: str) -> str: |
| 70 |
async with aiohttp.ClientSession() as session: |
| 71 |
async with session.post( |
| 72 |
self.url, headers=self.headers, data=unformatted_code.encode("utf-8") |
| 73 |
) as response: |
| 74 |
if response.status == 204: |
| 75 |
# Input is already well-formatted |
| 76 |
return unformatted_code |
| 77 |
elif response.status == 200: |
| 78 |
# Formatting was needed |
| 79 |
return await response.text() |
| 80 |
elif response.status == 400: |
| 81 |
# Input contains a syntax error |
| 82 |
error_message = await response.text() |
| 83 |
raise black.InvalidInput(error_message) |
| 84 |
elif response.status == 500: |
| 85 |
# Other kind of error while formatting |
| 86 |
error_message = await response.text() |
| 87 |
raise RuntimeError(f"Error while formatting: {error_message}") |
| 88 |
else: |
| 89 |
# Unexpected response status code |
| 90 |
raise RuntimeError( |
| 91 |
f"Unexpected response status code: {response.status}" |
| 92 |
) |