|
| 1 | +# pycsamt/_session.py |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# Author: LKouadio <[email protected]> |
| 4 | +# License: LGPL-3.0-or-later |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +from pathlib import Path |
| 9 | +from typing import Any, Optional |
| 10 | + |
| 11 | +from .core.transformers import AVGtoEDI, JtoEDI |
| 12 | +from .core.registry import RegistryAPI |
| 13 | +from .core.base import to_edi |
| 14 | + |
| 15 | +from .jones.j import JFile |
| 16 | +from .zonge.avg import AVG |
| 17 | +from .seg.edi import EDIFile |
| 18 | +from .seg.collection import EDICollection |
| 19 | + |
| 20 | + |
| 21 | +__all__ = [ |
| 22 | + "Session", |
| 23 | + "work_session", |
| 24 | + "Normalize", |
| 25 | + "normalize_session" |
| 26 | + |
| 27 | +] |
| 28 | + |
| 29 | + |
| 30 | +class Session: |
| 31 | + def __init__( |
| 32 | + self, |
| 33 | + root: Path | str, |
| 34 | + *, |
| 35 | + manifest_name: str = "manifest.json", |
| 36 | + auto_capture: bool = True, |
| 37 | + capture_children: bool = False, |
| 38 | + max_children: int = 256, |
| 39 | + ) -> None: |
| 40 | + self.root = Path(root) |
| 41 | + self.auto_capture = bool(auto_capture) |
| 42 | + self.capture_children = bool(capture_children) |
| 43 | + self.max_children = int(max_children) |
| 44 | + self.reg = RegistryAPI( |
| 45 | + self.root, |
| 46 | + manifest_name=manifest_name, |
| 47 | + ) |
| 48 | + self._orig_to_edi = None |
| 49 | + self._orig_t_x = None |
| 50 | + self._orig_t_j = None |
| 51 | + |
| 52 | + |
| 53 | + def _record(self, obj: Any, *, tag: str) -> None: |
| 54 | + try: |
| 55 | + self.reg.add_object(obj, tags=[tag]) |
| 56 | + except Exception: |
| 57 | + pass |
| 58 | + if not self.capture_children: |
| 59 | + return |
| 60 | + try: |
| 61 | + if hasattr(obj, "__iter__") and not hasattr(obj, "Z"): |
| 62 | + n = 0 |
| 63 | + for it in obj: # type: ignore |
| 64 | + self.reg.add_object(it, tags=[tag]) |
| 65 | + n += 1 |
| 66 | + if n >= self.max_children: |
| 67 | + break |
| 68 | + except Exception: |
| 69 | + pass |
| 70 | + |
| 71 | + def _wrap_to_edi(self) -> None: |
| 72 | + |
| 73 | + from .core import base as b |
| 74 | + |
| 75 | + if not self.auto_capture: |
| 76 | + return |
| 77 | + |
| 78 | + if self._orig_to_edi is None: |
| 79 | + self._orig_to_edi = to_edi |
| 80 | + |
| 81 | + def _wrapped(source: Any, *a: Any, **k: Any) -> Any: |
| 82 | + out = self._orig_to_edi(source, *a, **k) |
| 83 | + self._record(out, tag="to_edi") |
| 84 | + return out |
| 85 | + |
| 86 | + b.to_edi = _wrapped # type: ignore |
| 87 | + |
| 88 | + def _wrap_transformers(self) -> None: |
| 89 | + from .core import transformers as tr |
| 90 | + |
| 91 | + if not self.auto_capture: |
| 92 | + return |
| 93 | + |
| 94 | + def _wrap_method(func): |
| 95 | + def inner(self_, *a, **k): # noqa: ANN001 |
| 96 | + out = func(self_, *a, **k) |
| 97 | + try: |
| 98 | + tag = f"{self_.__class__.__name__}.transform" |
| 99 | + except Exception: |
| 100 | + tag = "transform" |
| 101 | + self._record(out, tag=tag) |
| 102 | + return out |
| 103 | + return inner |
| 104 | + |
| 105 | + if self._orig_t_x is None and hasattr(tr, "AVGtoEDI"): |
| 106 | + self._orig_t_x = tr.AVGtoEDI.transform |
| 107 | + tr.AVGtoEDI.transform = _wrap_method(self._orig_t_x) # type: ignore |
| 108 | + if self._orig_t_j is None and hasattr(tr, "JtoEDI"): |
| 109 | + self._orig_t_j = tr.JtoEDI.transform |
| 110 | + tr.JtoEDI.transform = _wrap_method(self._orig_t_j) # type: ignore |
| 111 | + |
| 112 | + def _restore(self) -> None: |
| 113 | + from .core import base as b |
| 114 | + from .core import transformers as tr |
| 115 | + try: |
| 116 | + if self._orig_to_edi is not None: |
| 117 | + b.to_edi = self._orig_to_edi # type: ignore |
| 118 | + except Exception: |
| 119 | + pass |
| 120 | + try: |
| 121 | + |
| 122 | + if self._orig_t_x is not None: |
| 123 | + tr.AVGtoEDI.transform = self._orig_t_x # type: ignore |
| 124 | + if self._orig_t_j is not None: |
| 125 | + tr.JtoEDI.transform = self._orig_t_j # type: ignore |
| 126 | + except Exception: |
| 127 | + pass |
| 128 | + |
| 129 | + def __enter__(self) -> "Session": |
| 130 | + self._wrap_to_edi() |
| 131 | + self._wrap_transformers() |
| 132 | + return self |
| 133 | + |
| 134 | + def __exit__( |
| 135 | + self, |
| 136 | + exc_type, |
| 137 | + exc, |
| 138 | + tb, |
| 139 | + ) -> Optional[bool]: |
| 140 | + self._restore() |
| 141 | + try: |
| 142 | + self.reg.save() |
| 143 | + except Exception: |
| 144 | + pass |
| 145 | + return None |
| 146 | + |
| 147 | + |
| 148 | +def work_session( |
| 149 | + root: Path | str, |
| 150 | + *, |
| 151 | + manifest_name: str = "manifest.json", |
| 152 | + auto_capture: bool = True, |
| 153 | + capture_children: bool = False, |
| 154 | + max_children: int = 256, |
| 155 | +) -> Session: |
| 156 | + return Session( |
| 157 | + root, |
| 158 | + manifest_name=manifest_name, |
| 159 | + auto_capture=auto_capture, |
| 160 | + capture_children=capture_children, |
| 161 | + max_children=max_children, |
| 162 | + ) |
| 163 | + |
| 164 | + |
| 165 | +class Normalize: |
| 166 | + def __init__( |
| 167 | + self, |
| 168 | + root: Path | str, |
| 169 | + *, |
| 170 | + manifest_name: str = "manifest.json", |
| 171 | + topo_src: Any | None = None, |
| 172 | + auto_register: bool = True, |
| 173 | + ) -> None: |
| 174 | + self.root = Path(root) |
| 175 | + self.reg = RegistryAPI( |
| 176 | + self.root, |
| 177 | + manifest_name=manifest_name, |
| 178 | + ) |
| 179 | + self.topo_src = topo_src |
| 180 | + self.auto_register = bool(auto_register) |
| 181 | + |
| 182 | + def _as_edi_coll(self, src: Any) -> Any: |
| 183 | + |
| 184 | + if isinstance(src, EDICollection): |
| 185 | + return src |
| 186 | + if isinstance(src, EDIFile): |
| 187 | + return EDICollection(items=[src], verbose=0) |
| 188 | + return None |
| 189 | + |
| 190 | + def _try_topo(self, avg: Any) -> None: |
| 191 | + if self.topo_src is None: |
| 192 | + return |
| 193 | + try: |
| 194 | + if hasattr(avg, "add_topography"): |
| 195 | + avg.add_topography(self.topo_src) |
| 196 | + return |
| 197 | + except Exception: |
| 198 | + pass |
| 199 | + try: |
| 200 | + if hasattr(self.topo_src, "frame"): |
| 201 | + avg.topo = self.topo_src |
| 202 | + except Exception: |
| 203 | + pass |
| 204 | + |
| 205 | + def _to_avg(self, src: Any) -> Any: |
| 206 | + |
| 207 | + |
| 208 | + |
| 209 | + if isinstance(src, AVG): |
| 210 | + return src |
| 211 | + if isinstance(src, (str, Path)): |
| 212 | + return AVG.from_file(src) |
| 213 | + return None |
| 214 | + |
| 215 | + def _to_j(self, src: Any) -> Any: |
| 216 | + |
| 217 | + |
| 218 | + |
| 219 | + if isinstance(src, JFile): |
| 220 | + return src |
| 221 | + if isinstance(src, (str, Path)): |
| 222 | + return JFile.from_file(src) |
| 223 | + return None |
| 224 | + |
| 225 | + def _normalize(self, source: Any) -> Any: |
| 226 | + out = self._as_edi_coll(source) |
| 227 | + if out is not None: |
| 228 | + return out |
| 229 | + a = self._to_avg(source) |
| 230 | + if a is not None: |
| 231 | + self._try_topo(a) |
| 232 | + |
| 233 | + return AVGtoEDI().transform(a) |
| 234 | + j = self._to_j(source) |
| 235 | + if j is not None: |
| 236 | + return JtoEDI().transform(j) |
| 237 | + try: |
| 238 | + |
| 239 | + |
| 240 | + if isinstance(source, (str, Path)): |
| 241 | + ed = EDIFile.from_file(source) |
| 242 | + return EDICollection(items=[ed], verbose=0) |
| 243 | + except Exception: |
| 244 | + pass |
| 245 | + |
| 246 | + obj = to_edi(source) |
| 247 | + return self._as_edi_coll(obj) or obj |
| 248 | + |
| 249 | + def __enter__(self) -> "Normalize": |
| 250 | + return self |
| 251 | + |
| 252 | + def __exit__( |
| 253 | + self, |
| 254 | + exc_type, |
| 255 | + exc, |
| 256 | + tb, |
| 257 | + ) -> bool | None: |
| 258 | + try: |
| 259 | + self.reg.save() |
| 260 | + except Exception: |
| 261 | + pass |
| 262 | + return None |
| 263 | + |
| 264 | + def load(self, source: Any) -> Any: |
| 265 | + out = self._normalize(source) |
| 266 | + if self.auto_register: |
| 267 | + try: |
| 268 | + self.reg.add_object(out, tags=["normalized"]) |
| 269 | + except Exception: |
| 270 | + pass |
| 271 | + return out |
| 272 | + |
| 273 | +def normalize_session( |
| 274 | + root: Path | str, |
| 275 | + *, |
| 276 | + manifest_name: str = "manifest.json", |
| 277 | + topo_src: Any | None = None, |
| 278 | + auto_register: bool = True, |
| 279 | +): |
| 280 | + return Normalize( |
| 281 | + root, |
| 282 | + manifest_name=manifest_name, |
| 283 | + topo_src=topo_src, |
| 284 | + auto_register=auto_register, |
| 285 | + ) |
0 commit comments