From 486281e1d8070d9d47494a7b03a022c3d182394f Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 12 Mar 2026 22:08:28 +0000 Subject: [PATCH] Add from_file, from_bytes, and from_directory methods to BaseParser Extends the interop engine's parser base class with convenience methods for loading data from files, bytes, and directories. All methods delegate to the existing from_string() method. Closes #122 --- healthchain/interop/parsers/base.py | 54 +++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/healthchain/interop/parsers/base.py b/healthchain/interop/parsers/base.py index f97b226f..615d6eb0 100644 --- a/healthchain/interop/parsers/base.py +++ b/healthchain/interop/parsers/base.py @@ -1,4 +1,6 @@ from abc import ABC, abstractmethod +from pathlib import Path +from typing import List, Union from healthchain.interop.config_manager import InteropConfigManager @@ -23,3 +25,55 @@ def from_string(self, data: str) -> dict: A dictionary containing the parsed data structure """ pass + + def from_bytes(self, data: bytes, encoding: str = "utf-8") -> dict: + """Parse input data from bytes. + + Args: + data: The input data as bytes + encoding: Character encoding to use when decoding bytes + + Returns: + A dictionary containing the parsed data structure + """ + return self.from_string(data.decode(encoding)) + + def from_file(self, file_path: Union[str, Path]) -> dict: + """Parse input data from a file. + + Args: + file_path: Path to the file to parse + + Returns: + A dictionary containing the parsed data structure + + Raises: + FileNotFoundError: If the file does not exist + """ + path = Path(file_path) + return self.from_string(path.read_text(encoding="utf-8")) + + def from_directory( + self, directory_path: Union[str, Path], pattern: str = "*.xml" + ) -> List[dict]: + """Parse all matching files in a directory. + + Args: + directory_path: Path to the directory containing files to parse + pattern: Glob pattern to match files (default: "*.xml") + + Returns: + A list of dictionaries, one per parsed file + + Raises: + NotADirectoryError: If the path is not a directory + """ + path = Path(directory_path) + if not path.is_dir(): + raise NotADirectoryError(f"Not a directory: {path}") + + results = [] + for file_path in sorted(path.glob(pattern)): + if file_path.is_file(): + results.append(self.from_file(file_path)) + return results