codekingpro/portable-devtools
115k
1# Default view cutoff *in lines*2import sys3from abc import ABC4from abc import abstractmethod5from collections.abc import Iterable6from collections.abc import Iterator7from collections.abc import Mapping8from typing import ClassVar9from typing import Union10 11from mitmproxy import flow12from mitmproxy import http13 14if sys.version_info < (3, 13): # pragma: no cover15 from typing_extensions import deprecated16else:17 from warnings import deprecated18 19KEY_MAX = 3020 21TTextType = Union[str, bytes] # FIXME: This should be either bytes or str ultimately.22TViewLine = list[tuple[str, TTextType]]23TViewResult = tuple[str, Iterator[TViewLine]]24 25 26@deprecated("Use `mitmproxy.contentviews.Contentview` instead.")27class View(ABC):28 """29 Deprecated, do not use.30 """31 32 name: ClassVar[str]33 34 @abstractmethod35 def __call__(36 self,37 data: bytes,38 *,39 content_type: str | None = None,40 flow: flow.Flow | None = None,41 http_message: http.Message | None = None,42 **unknown_metadata,43 ) -> TViewResult:44 """45 Transform raw data into human-readable output.46 47 Returns a (description, content generator) tuple.48 The content generator yields lists of (style, text) tuples, where each list represents49 a single line. ``text`` is a unfiltered string which may need to be escaped,50 depending on the used output. For example, it may contain terminal control sequences51 or unfiltered HTML.52 53 Except for `data`, implementations must not rely on any given argument to be present.54 To ensure compatibility with future mitmproxy versions, unknown keyword arguments should be ignored.55 56 The content generator must not yield tuples of tuples, because urwid cannot process that.57 You have to yield a *list* of tuples per line.58 """59 raise NotImplementedError() # pragma: no cover60 61 def render_priority(62 self,63 data: bytes,64 *,65 content_type: str | None = None,66 flow: flow.Flow | None = None,67 http_message: http.Message | None = None,68 **unknown_metadata,69 ) -> float:70 """71 Return the priority of this view for rendering `data`.72 If no particular view is chosen by the user, the view with the highest priority is selected.73 74 Except for `data`, implementations must not rely on any given argument to be present.75 To ensure compatibility with future mitmproxy versions, unknown keyword arguments should be ignored.76 """77 return 078 79 def __lt__(self, other):80 assert isinstance(other, View)81 return self.name.__lt__(other.name)82 83 84@deprecated("Use `mitmproxy.contentviews.Contentview` instead.")85def format_pairs(items: Iterable[tuple[TTextType, TTextType]]) -> Iterator[TViewLine]:86 """87 Helper function that accepts a list of (k,v) pairs into a list of88 [89 ("key", key )90 ("value", value)91 ]92 where key is padded to a uniform width93 """94 95 max_key_len = max((len(k[0]) for k in items), default=0)96 max_key_len = min((max_key_len, KEY_MAX), default=0)97 98 for key, value in items:99 if isinstance(key, bytes):100 key += b":"101 else:102 key += ":"103 104 key = key.ljust(max_key_len + 2)105 106 yield [("header", key), ("text", value)]107 108 109@deprecated("Use `mitmproxy.contentviews.Contentview` instead.")110def format_dict(d: Mapping[TTextType, TTextType]) -> Iterator[TViewLine]:111 """112 Helper function that transforms the given dictionary into a list of113 [114 ("key", key )115 ("value", value)116 ]117 entries, where key is padded to a uniform width.118 """119 120 return format_pairs(d.items())121 122 123@deprecated("Use `mitmproxy.contentviews.Contentview` instead.")124def format_text(text: TTextType) -> Iterator[TViewLine]:125 """126 Helper function that transforms bytes into the view output format.127 """128 for line in text.splitlines():129 yield [("text", line)]130 