| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import collections.abc as c |
| 4 |
import gzip |
| 5 |
import io |
| 6 |
import itertools |
| 7 |
import os.path |
| 8 |
import sys |
| 9 |
from types import ModuleType |
| 10 |
|
| 11 |
from flask import current_app |
| 12 |
from markupsafe import Markup |
| 13 |
|
| 14 |
try: |
| 15 |
from pygments import highlight |
| 16 |
from pygments.formatters import HtmlFormatter |
| 17 |
from pygments.lexers import SqlLexer |
| 18 |
from pygments.styles import get_style_by_name |
| 19 |
|
| 20 |
PYGMENT_STYLE = get_style_by_name("colorful") |
| 21 |
HAVE_PYGMENTS = True |
| 22 |
except ImportError: |
| 23 |
HAVE_PYGMENTS = False |
| 24 |
|
| 25 |
try: |
| 26 |
import sqlparse # pyright: ignore |
| 27 |
|
| 28 |
HAVE_SQLPARSE = True |
| 29 |
except ImportError: |
| 30 |
HAVE_SQLPARSE = False |
| 31 |
|
| 32 |
|
| 33 |
def format_fname(value: str) -> str: |
| 34 |
# If the value has a builtin prefix, return it unchanged |
| 35 |
if value.startswith(("{", "<")): |
| 36 |
return value |
| 37 |
|
| 38 |
value = os.path.normpath(value) |
| 39 |
|
| 40 |
# If the file is absolute, try normalizing it relative to the project root |
| 41 |
# to handle it as a project file |
| 42 |
if os.path.isabs(value): |
| 43 |
value = _shortest_relative_path(value, [current_app.root_path], os.path) |
| 44 |
|
| 45 |
# If the value is a relative path, it is a project file |
| 46 |
if not os.path.isabs(value): |
| 47 |
return os.path.join(".", value) |
| 48 |
|
| 49 |
# Otherwise, normalize other paths relative to sys.path |
| 50 |
return f"<{_shortest_relative_path(value, sys.path, os.path)}>" |
| 51 |
|
| 52 |
|
| 53 |
def _shortest_relative_path( |
| 54 |
value: str, paths: list[str], path_module: ModuleType |
| 55 |
) -> str: |
| 56 |
relpaths = _relative_paths(value, paths, path_module) |
| 57 |
return min(itertools.chain(relpaths, [value]), key=len) |
| 58 |
|
| 59 |
|
| 60 |
def _relative_paths( |
| 61 |
value: str, paths: list[str], path_module: ModuleType |
| 62 |
) -> c.Iterator[str]: |
| 63 |
for path in paths: |
| 64 |
try: |
| 65 |
relval = path_module.relpath(value, path) |
| 66 |
except ValueError: |
| 67 |
# on Windows, relpath throws a ValueError for |
| 68 |
# paths with different drives |
| 69 |
continue |
| 70 |
|
| 71 |
if not relval.startswith(path_module.pardir): |
| 72 |
yield relval |
| 73 |
|
| 74 |
|
| 75 |
def decode_text(value: str | bytes) -> str: |
| 76 |
""" |
| 77 |
Decode a text-like value for display. |
| 78 |
|
| 79 |
Unicode values are returned unchanged. Byte strings will be decoded |
| 80 |
with a text-safe replacement for unrecognized characters. |
| 81 |
""" |
| 82 |
if isinstance(value, bytes): |
| 83 |
return value.decode("ascii", "replace") |
| 84 |
|
| 85 |
return value # pyright: ignore |
| 86 |
|
| 87 |
|
| 88 |
def format_sql(query: str | bytes, args: object) -> str: |
| 89 |
if HAVE_SQLPARSE: |
| 90 |
query = sqlparse.format(query, reindent=True, keyword_case="upper") |
| 91 |
|
| 92 |
if not HAVE_PYGMENTS: |
| 93 |
return decode_text(query) |
| 94 |
|
| 95 |
return Markup( |
| 96 |
highlight(query, SqlLexer(), HtmlFormatter(noclasses=True, style=PYGMENT_STYLE)) |
| 97 |
) |
| 98 |
|
| 99 |
|
| 100 |
def gzip_compress(data: bytes, compresslevel: int = 6) -> bytes: |
| 101 |
buff = io.BytesIO() |
| 102 |
|
| 103 |
with gzip.GzipFile(fileobj=buff, mode="wb", compresslevel=compresslevel) as f: |
| 104 |
f.write(data) |
| 105 |
|
| 106 |
return buff.getvalue() |
| 107 |
|
| 108 |
|
| 109 |
def gzip_decompress(data: bytes) -> bytes: |
| 110 |
with gzip.GzipFile(fileobj=io.BytesIO(data), mode="rb") as f: |
| 111 |
return f.read() |