Parse content using the first parser that succeeds. Args: content: Raw bytes content to be parsed Returns: Document: Parsed document from the first successful parser, or an empty Document if all parsers fail
(self, content: bytes)
| 46 | self._parsers.append(parser) |
| 47 | |
| 48 | def parse_into_text(self, content: bytes) -> Document: |
| 49 | """Parse content using the first parser that succeeds. |
| 50 | |
| 51 | Args: |
| 52 | content: Raw bytes content to be parsed |
| 53 | |
| 54 | Returns: |
| 55 | Document: Parsed document from the first successful parser, |
| 56 | or an empty Document if all parsers fail |
| 57 | """ |
| 58 | for p in self._parsers: |
| 59 | logger.info(f"FirstParser: using parser {p.__class__.__name__}") |
| 60 | try: |
| 61 | document = p.parse_into_text(content) |
| 62 | except Exception: |
| 63 | logger.exception( |
| 64 | "FirstParser: parser %s raised exception; trying next parser", |
| 65 | p.__class__.__name__, |
| 66 | ) |
| 67 | continue |
| 68 | |
| 69 | if document.is_valid(): |
| 70 | logger.info(f"FirstParser: parser {p.__class__.__name__} succeeded") |
| 71 | return document |
| 72 | return Document() |
| 73 | |
| 74 | @classmethod |
| 75 | def create(cls, *parser_classes: Type["BaseParser"]) -> Type["FirstParser"]: |
no test coverage detected