Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
_api.py117 linesDownload Raw Back to contentviews
1from __future__ import annotations2 3import logging4import typing5from abc import abstractmethod6from dataclasses import dataclass7from pathlib import Path8from typing import Literal9 10from mitmproxy import http11from mitmproxy import tcp12from mitmproxy import udp13from mitmproxy.dns import DNSMessage14from mitmproxy.flow import Flow15from mitmproxy.websocket import WebSocketMessage16 17logger = logging.getLogger(__name__)18 19 20type SyntaxHighlight = Literal["css", "javascript", "xml", "yaml", "none", "error"]21 22 23@typing.runtime_checkable24class Contentview(typing.Protocol):25    """26    Base class for all contentviews.27    """28 29    @property30    def name(self) -> str:31        """32        The name of this contentview, e.g. "XML/HTML".33        Inferred from the class name by default.34        """35        return type(self).__name__.removesuffix("Contentview")36 37    @property38    def syntax_highlight(self) -> SyntaxHighlight:39        """Optional syntax highlighting that should be applied to the prettified output."""40        return "none"41 42    @abstractmethod43    def prettify(44        self,45        data: bytes,46        metadata: Metadata,47    ) -> str:48        """49        Transform raw data into human-readable output.50        May raise an exception (e.g. `ValueError`) if data cannot be prettified.51        """52 53    def render_priority(54        self,55        data: bytes,56        metadata: Metadata,57    ) -> float:58        """59        Return the priority of this view for rendering `data`.60        If no particular view is chosen by the user, the view with the highest priority is selected.61        If this view does not support the given data, return a float < 0.62        """63        return 064 65    def __lt__(self, other):66        return self.name.__lt__(other.name)67 68 69@typing.runtime_checkable70class InteractiveContentview(Contentview, typing.Protocol):71    """A contentview that prettifies raw data and allows for interactive editing."""72 73    @abstractmethod74    def reencode(75        self,76        prettified: str,77        metadata: Metadata,78    ) -> bytes:79        """80        Reencode the given (modified) `prettified` output into the original data format.81        May raise an exception (e.g. `ValueError`) if reencoding failed.82        """83 84 85@dataclass86class Metadata:87    """88    Metadata about the data that is being prettified.89 90    Do not rely on any given attribute to be present.91    """92 93    flow: Flow | None = None94    """The flow that the data belongs to, if any."""95 96    content_type: str | None = None97    """The HTTP content type of the data, if any."""98    http_message: http.Message | None = None99    """The HTTP message that the data belongs to, if any."""100    tcp_message: tcp.TCPMessage | None = None101    """The TCP message that the data belongs to, if any."""102    udp_message: udp.UDPMessage | None = None103    """The UDP message that the data belongs to, if any."""104    websocket_message: WebSocketMessage | None = None105    """The websocket message that the data belongs to, if any."""106    dns_message: DNSMessage | None = None107    """The DNS message that the data belongs to, if any."""108 109    protobuf_definitions: Path | None = None110    """Path to a .proto file that's used to resolve Protobuf field names."""111 112    original_data: bytes | None = None113    """When reencoding: The original data that was prettified."""114 115 116Metadata.__init__.__doc__ = "@private"117 
codekingpro/portable-devtools · Team Ai