77 lines
1.9 KiB
Python
77 lines
1.9 KiB
Python
"""Parser interface for HTTP log formats."""
|
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
from http_log_explorer.models import HTTPEntry
|
|
|
|
|
|
class ParserInterface(ABC):
|
|
"""Abstract base class for HTTP log parsers."""
|
|
|
|
@abstractmethod
|
|
def parse(self, content: str | bytes, source_file: str | None = None) -> list[HTTPEntry]:
|
|
"""Parse content and return list of HTTP entries.
|
|
|
|
Args:
|
|
content: The content to parse (string or bytes)
|
|
source_file: Optional source file name for reference
|
|
|
|
Returns:
|
|
List of HTTPEntry objects
|
|
|
|
Raises:
|
|
ValueError: If content cannot be parsed
|
|
"""
|
|
pass
|
|
|
|
@abstractmethod
|
|
def can_parse(self, content: str | bytes) -> bool:
|
|
"""Check if this parser can handle the given content.
|
|
|
|
Args:
|
|
content: The content to check
|
|
|
|
Returns:
|
|
True if this parser can handle the content
|
|
"""
|
|
pass
|
|
|
|
@staticmethod
|
|
def get_parser_name() -> str:
|
|
"""Return the name of this parser."""
|
|
return "unknown"
|
|
|
|
|
|
def get_parser(content: str | bytes) -> ParserInterface:
|
|
"""Get the appropriate parser for the given content.
|
|
|
|
Args:
|
|
content: The content to parse
|
|
|
|
Returns:
|
|
An appropriate parser instance
|
|
|
|
Raises:
|
|
ValueError: If no suitable parser is found
|
|
"""
|
|
from http_log_explorer.parsers.curl_parser import CurlParser
|
|
from http_log_explorer.parsers.devtools_parser import DevToolsParser
|
|
from http_log_explorer.parsers.har_parser import HARParser
|
|
|
|
parsers: list[ParserInterface] = [
|
|
HARParser(),
|
|
CurlParser(),
|
|
DevToolsParser(),
|
|
]
|
|
|
|
for parser in parsers:
|
|
if parser.can_parse(content):
|
|
return parser
|
|
|
|
raise ValueError(
|
|
"Unsupported format. Supported formats are: HAR files, curl -v output, and Chrome DevTools network exports."
|
|
)
|
|
|
|
|
|
__all__ = ["ParserInterface", "get_parser"]
|