Enhanced parser with comprehensive validation and error handling
| 434 | |
| 435 | |
| 436 | class PackageIndexParser: |
| 437 | """Enhanced parser with comprehensive validation and error handling""" |
| 438 | |
| 439 | def __init__(self, timeout: int = 30): |
| 440 | """Initialize parser with timeout configuration""" |
| 441 | self.timeout = timeout |
| 442 | |
| 443 | def parse_package_index(self, json_str: str) -> PackageIndex: |
| 444 | """Parse and validate package index JSON""" |
| 445 | try: |
| 446 | raw_data = json.loads(json_str) |
| 447 | return PackageIndex(**raw_data) |
| 448 | except ValidationError as e: |
| 449 | raise PackageParsingError(f"Invalid package index format: {e}") |
| 450 | except json.JSONDecodeError as e: |
| 451 | raise PackageParsingError(f"Invalid JSON format: {e}") |
| 452 | |
| 453 | def parse_from_url(self, url: str) -> PackageIndex: |
| 454 | """Fetch and parse package index from URL with validation""" |
| 455 | try: |
| 456 | print(f"Fetching package index from: {url}") |
| 457 | with urlopen(url, timeout=self.timeout) as response: |
| 458 | content = response.read() |
| 459 | json_str = content.decode("utf-8") |
| 460 | |
| 461 | return self.parse_package_index(json_str) |
| 462 | |
| 463 | except KeyboardInterrupt as ki: |
| 464 | handle_keyboard_interrupt(ki) |
| 465 | raise |
| 466 | except Exception as e: |
| 467 | raise PackageParsingError(f"Error fetching package index from {url}: {e}") |
| 468 | |
| 469 | async def parse_from_url_async(self, url: str) -> PackageIndex: |
| 470 | """Async version of URL parsing""" |
| 471 | try: |
| 472 | print(f"Fetching package index from: {url}") |
| 473 | async with httpx.AsyncClient(timeout=self.timeout) as client: |
| 474 | response = await client.get(url) |
| 475 | response.raise_for_status() |
| 476 | json_str = response.text |
| 477 | |
| 478 | return self.parse_package_index(json_str) |
| 479 | |
| 480 | except KeyboardInterrupt as ki: |
| 481 | handle_keyboard_interrupt(ki) |
| 482 | raise |
| 483 | except Exception as e: |
| 484 | raise PackageParsingError(f"Error fetching package index from {url}: {e}") |
| 485 | |
| 486 | |
| 487 | # Package Manager Configuration |
no outgoing calls