|
| 1 | +from typing import Protocol, Callable, Any |
| 2 | +import json |
| 3 | +from pathlib import Path |
| 4 | +import os |
| 5 | +import re |
| 6 | +import logging |
| 7 | +import pandas as pd |
| 8 | +from pandas import DataFrame |
| 9 | + |
| 10 | +type Data = list[dict[str, Any]] |
| 11 | + |
| 12 | + |
| 13 | +# === Interfaces === |
| 14 | +class DataLoader(Protocol): |
| 15 | + def load(self) -> Data: ... |
| 16 | + |
| 17 | + |
| 18 | +class Transformer(Protocol): |
| 19 | + def transform(self, data: Data) -> Data: ... |
| 20 | + |
| 21 | + |
| 22 | +class Exporter(Protocol): |
| 23 | + def export(self, data: Data) -> None: ... |
| 24 | + |
| 25 | + |
| 26 | +# === Concrete implementations === |
| 27 | +class InMemoryLoader: |
| 28 | + def load_nl_dataset(self, dir_path: Path) -> dict[str, DataFrame]: |
| 29 | + datasets = {} |
| 30 | + for file_name in sorted(os.listdir(dir_path)): |
| 31 | + file_path = dir_path / file_name |
| 32 | + try: |
| 33 | + if file_name.endswith(".jsonl"): |
| 34 | + df = pd.read_json(str(file_path), lines=True) |
| 35 | + datasets[file_name] = df |
| 36 | + logging.info(f"Loaded {file_name} with shape: {df.shape}") |
| 37 | + else: |
| 38 | + logging.warning( |
| 39 | + f"Unsupported file format for {file_name}, skipping." |
| 40 | + ) |
| 41 | + continue |
| 42 | + except Exception as e: |
| 43 | + logging.error(f"Error loading {file_name}: {e}") |
| 44 | + return datasets |
| 45 | + |
| 46 | + def load_xacml_dataset(self, dir_path: Path) -> dict[str, DataFrame]: |
| 47 | + datasets = {} |
| 48 | + for file_name in sorted(os.listdir(dir_path)): |
| 49 | + file_path = dir_path / file_name |
| 50 | + try: |
| 51 | + if file_name.endswith(".xml"): |
| 52 | + with open(file_path, "r", encoding="utf-8") as file: |
| 53 | + policy_pattern = re.compile( |
| 54 | + r"<Policy\s[^>]*>[\s\S]*?<\/Policy>" |
| 55 | + ) |
| 56 | + xacml_content = file.read() |
| 57 | + policies = policy_pattern.findall(xacml_content) |
| 58 | + datasets[file_name] = pd.DataFrame({"policy": policies}) |
| 59 | + logging.info( |
| 60 | + f"Loaded {file_name} with {len(policies)} policies." |
| 61 | + ) |
| 62 | + else: |
| 63 | + logging.warning( |
| 64 | + f"Unsupported file format for {file_name}, skipping." |
| 65 | + ) |
| 66 | + continue |
| 67 | + except Exception as e: |
| 68 | + logging.error(f"Error loading {file_name}: {e}") |
| 69 | + return datasets |
| 70 | + |
| 71 | + |
| 72 | +class CleanMissingFields: |
| 73 | + def transform(self, data: Data) -> Data: |
| 74 | + return [row for row in data if row["age"] is not None] |
| 75 | + |
| 76 | + |
| 77 | +class JSONExporter: |
| 78 | + def __init__(self, filename: str): |
| 79 | + self.filename = filename |
| 80 | + |
| 81 | + def export(self, data: Data) -> None: |
| 82 | + with open(self.filename, "w") as f: |
| 83 | + json.dump(data, f, indent=2) |
| 84 | + |
| 85 | + |
| 86 | +# === Pipeline === |
| 87 | +class DataPipeline: |
| 88 | + def __init__( |
| 89 | + self, loader: DataLoader, transformer: Transformer, exporter: Exporter |
| 90 | + ): |
| 91 | + self.loader = loader |
| 92 | + self.transformer = transformer |
| 93 | + self.exporter = exporter |
| 94 | + |
| 95 | + def run(self) -> None: |
| 96 | + data = self.loader.load() |
| 97 | + clean = self.transformer.transform(data) |
| 98 | + self.exporter.export(clean) |
| 99 | + |
| 100 | + |
| 101 | +# === Simple DI container === |
| 102 | +class Container: |
| 103 | + def __init__(self) -> None: |
| 104 | + self._providers: dict[str, tuple[Callable[[], Any], bool]] = {} |
| 105 | + self._singletons: dict[str, Any] = {} |
| 106 | + |
| 107 | + def register( |
| 108 | + self, name: str, provider: Callable[[], Any], singleton: bool = False |
| 109 | + ) -> None: |
| 110 | + self._providers[name] = (provider, singleton) |
| 111 | + |
| 112 | + def resolve(self, name: str) -> Any: |
| 113 | + if name in self._singletons: |
| 114 | + return self._singletons[name] |
| 115 | + |
| 116 | + if name not in self._providers: |
| 117 | + raise ValueError(f"No provider registered for '{name}'") |
| 118 | + |
| 119 | + provider, singleton = self._providers[name] |
| 120 | + instance = provider() |
| 121 | + |
| 122 | + if singleton: |
| 123 | + self._singletons[name] = instance |
| 124 | + |
| 125 | + return instance |
| 126 | + |
| 127 | + |
| 128 | +# === Main runner === |
| 129 | +def main() -> None: |
| 130 | + container = Container() |
| 131 | + |
| 132 | + container.register("loader", lambda: InMemoryLoader(), singleton=True) |
| 133 | + container.register("transformer", lambda: CleanMissingFields()) |
| 134 | + container.register("exporter", lambda: JSONExporter("output.json")) |
| 135 | + |
| 136 | + container.register( |
| 137 | + "pipeline", |
| 138 | + lambda: DataPipeline( |
| 139 | + loader=container.resolve("loader"), |
| 140 | + transformer=container.resolve("transformer"), |
| 141 | + exporter=container.resolve("exporter"), |
| 142 | + ), |
| 143 | + ) |
| 144 | + |
| 145 | + pipeline: DataPipeline = container.resolve("pipeline") |
| 146 | + pipeline.run() |
| 147 | + print("Pipeline finished. Output written to output.json") |
| 148 | + |
| 149 | + |
| 150 | +if __name__ == "__main__": |
| 151 | + main() |
0 commit comments