48 lines
1.2 KiB
Python
48 lines
1.2 KiB
Python
"""Parser factory for creating appropriate parsers."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
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
|
|
|
|
if TYPE_CHECKING:
|
|
from http_log_explorer.parsers import ParserInterface
|
|
|
|
|
|
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
|
|
"""
|
|
parsers = [
|
|
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."
|
|
)
|
|
|
|
|
|
def get_all_parsers() -> list[ParserInterface]:
|
|
"""Get all available parser instances."""
|
|
return [HARParser(), CurlParser(), DevToolsParser()]
|
|
|
|
|
|
__all__ = ["get_parser", "get_all_parsers"]
|