refactor(parse): add base html and json parsers

This commit is contained in:
2026-09-21 22:17:50 +03:00
parent 5d025998b9
commit 0baa1277ef
8 changed files with 186 additions and 98 deletions
+42 -6
View File
@@ -1,13 +1,49 @@
from typing import Generic, TypeVar
D = TypeVar("D")
R = TypeVar("R")
from abc import ABC, abstractmethod
from typing import Generic, NamedTuple, Protocol, TypeVar
class ParseError(Exception):
pass
class Parser(Generic[D, R]):
D = TypeVar("D")
R = TypeVar("R")
class Parser[D, R](Protocol):
def parse(self, data: D) -> R: ...
P = TypeVar("P")
class PrepareParser(ABC, Parser[D, R], Generic[D, P, R]):
@abstractmethod
def prepare(self, data: D) -> P: ...
@abstractmethod
def extract(self, data: P) -> R: ...
def parse(self, data: D) -> R:
raise NotImplementedError
return self.extract(self.prepare(data))
class Context(Protocol):
data: str
C = TypeVar("C", bound=Context)
class ParsedContext(NamedTuple, Generic[C, D]):
context: C
data: D
class ContextParser(PrepareParser[C, ParsedContext[C, P], R], Generic[C, P, R]):
@abstractmethod
def prepare_context(self, data: str) -> P: ...
def prepare(self, data: C) -> ParsedContext[C, P]:
return ParsedContext(context=data, data=self.prepare_context(data.data))