|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import bz2 |
| 4 | +import datetime |
| 5 | +import gzip |
| 6 | +import importlib |
| 7 | +import json |
| 8 | +from enum import Enum |
| 9 | +from pathlib import Path |
| 10 | +from typing import Any, BinaryIO, TextIO, cast |
| 11 | +from uuid import UUID |
| 12 | + |
| 13 | +import numpy as np |
| 14 | + |
| 15 | + |
| 16 | +def _detect_format(filename: str | Path, fmt: str | None = None) -> str: |
| 17 | + if fmt is not None: |
| 18 | + return fmt |
| 19 | + basename = Path(filename).name.lower() |
| 20 | + if ".mpk" in basename: |
| 21 | + return "mpk" |
| 22 | + if ".yaml" in basename or ".yml" in basename: |
| 23 | + return "yaml" |
| 24 | + return "json" |
| 25 | + |
| 26 | + |
| 27 | +def _open_text(filename: str | Path, mode: str) -> TextIO: |
| 28 | + path = str(filename) |
| 29 | + lower_path = path.lower() |
| 30 | + if lower_path.endswith((".gz", ".z")): |
| 31 | + return cast("TextIO", gzip.open(path, mode, encoding="utf-8")) |
| 32 | + if lower_path.endswith(".bz2"): |
| 33 | + return cast("TextIO", bz2.open(path, mode, encoding="utf-8")) |
| 34 | + return cast("TextIO", open(path, mode, encoding="utf-8")) |
| 35 | + |
| 36 | + |
| 37 | +def _open_binary(filename: str | Path, mode: str) -> BinaryIO: |
| 38 | + path = str(filename) |
| 39 | + lower_path = path.lower() |
| 40 | + if lower_path.endswith((".gz", ".z")): |
| 41 | + return cast("BinaryIO", gzip.open(path, mode)) |
| 42 | + if lower_path.endswith(".bz2"): |
| 43 | + return cast("BinaryIO", bz2.open(path, mode)) |
| 44 | + return cast("BinaryIO", open(path, mode)) |
| 45 | + |
| 46 | + |
| 47 | +def _yaml_dump(obj: Any, fp, *args: Any, **kwargs: Any) -> None: |
| 48 | + try: |
| 49 | + yaml = importlib.import_module("yaml") |
| 50 | + |
| 51 | + if "sort_keys" not in kwargs: |
| 52 | + kwargs["sort_keys"] = False |
| 53 | + getattr(yaml, "safe_dump")(obj, fp, *args, **kwargs) |
| 54 | + except ModuleNotFoundError: |
| 55 | + try: |
| 56 | + ruamel_yaml = importlib.import_module("ruamel.yaml") |
| 57 | + except ModuleNotFoundError as e: |
| 58 | + raise RuntimeError( |
| 59 | + "Dumping YAML files requires PyYAML or ruamel.yaml." |
| 60 | + ) from e |
| 61 | + yaml = getattr(ruamel_yaml, "YAML")() |
| 62 | + if "indent" in kwargs: |
| 63 | + indent = kwargs.pop("indent") |
| 64 | + yaml.indent(mapping=indent, sequence=indent, offset=2) |
| 65 | + yaml.dump(obj, fp, *args, **kwargs) |
| 66 | + |
| 67 | + |
| 68 | +def _yaml_load(fp, *args: Any, **kwargs: Any) -> Any: |
| 69 | + try: |
| 70 | + yaml = importlib.import_module("yaml") |
| 71 | + |
| 72 | + return getattr(yaml, "safe_load")(fp, *args, **kwargs) |
| 73 | + except ModuleNotFoundError: |
| 74 | + try: |
| 75 | + ruamel_yaml = importlib.import_module("ruamel.yaml") |
| 76 | + except ModuleNotFoundError as e: |
| 77 | + raise RuntimeError( |
| 78 | + "Loading YAML files requires PyYAML or ruamel.yaml." |
| 79 | + ) from e |
| 80 | + yaml = getattr(ruamel_yaml, "YAML")(typ="safe") |
| 81 | + return yaml.load(fp, *args, **kwargs) |
| 82 | + |
| 83 | + |
| 84 | +def _encode_ndarray(obj: np.ndarray) -> dict[str, Any]: |
| 85 | + if str(obj.dtype).startswith("complex"): |
| 86 | + data = [obj.real.tolist(), obj.imag.tolist()] |
| 87 | + else: |
| 88 | + data = obj.tolist() |
| 89 | + return { |
| 90 | + "@module": "numpy", |
| 91 | + "@class": "array", |
| 92 | + "dtype": str(obj.dtype), |
| 93 | + "data": data, |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | +def to_serializable(obj: Any) -> Any: |
| 98 | + """Convert common dpdata objects to monty-compatible plain data.""" |
| 99 | + if isinstance(obj, dict): |
| 100 | + return {to_serializable(k): to_serializable(v) for k, v in obj.items()} |
| 101 | + if isinstance(obj, (list, tuple)): |
| 102 | + return [to_serializable(v) for v in obj] |
| 103 | + if isinstance(obj, np.ndarray): |
| 104 | + return _encode_ndarray(obj) |
| 105 | + if isinstance(obj, np.generic): |
| 106 | + return obj.item() |
| 107 | + if isinstance(obj, datetime.datetime): |
| 108 | + return { |
| 109 | + "@module": "datetime", |
| 110 | + "@class": "datetime", |
| 111 | + "string": str(obj), |
| 112 | + } |
| 113 | + if isinstance(obj, UUID): |
| 114 | + return {"@module": "uuid", "@class": "UUID", "string": str(obj)} |
| 115 | + if isinstance(obj, Path): |
| 116 | + return {"@module": "pathlib", "@class": "Path", "string": str(obj)} |
| 117 | + if isinstance(obj, Enum): |
| 118 | + return { |
| 119 | + "@module": obj.__class__.__module__, |
| 120 | + "@class": obj.__class__.__name__, |
| 121 | + "value": to_serializable(obj.value), |
| 122 | + } |
| 123 | + if hasattr(obj, "as_dict"): |
| 124 | + data = obj.as_dict() |
| 125 | + if "@module" not in data: |
| 126 | + data["@module"] = obj.__class__.__module__ |
| 127 | + if "@class" not in data: |
| 128 | + data["@class"] = obj.__class__.__name__ |
| 129 | + return to_serializable(data) |
| 130 | + return obj |
| 131 | + |
| 132 | + |
| 133 | +def _decode_ndarray(data: dict[str, Any]) -> np.ndarray: |
| 134 | + dtype = data["dtype"] |
| 135 | + if dtype.startswith("complex"): |
| 136 | + real, imag = data["data"] |
| 137 | + return np.array(real, dtype=dtype) + np.array(imag, dtype=dtype) * 1j |
| 138 | + return np.array(data["data"], dtype=dtype) |
| 139 | + |
| 140 | + |
| 141 | +def process_decoded(obj: Any) -> Any: |
| 142 | + """Decode monty-style dictionaries used by existing dpdata JSON files.""" |
| 143 | + if isinstance(obj, dict): |
| 144 | + if "@module" in obj and "@class" in obj: |
| 145 | + module_name = obj["@module"] |
| 146 | + class_name = obj["@class"] |
| 147 | + if module_name == "numpy" and class_name == "array": |
| 148 | + return _decode_ndarray(obj) |
| 149 | + if module_name == "datetime" and class_name == "datetime": |
| 150 | + value = obj["string"].split("+")[0] |
| 151 | + try: |
| 152 | + return datetime.datetime.strptime(value, "%Y-%m-%d %H:%M:%S.%f") |
| 153 | + except ValueError: |
| 154 | + return datetime.datetime.strptime(value, "%Y-%m-%d %H:%M:%S") |
| 155 | + if module_name == "uuid" and class_name == "UUID": |
| 156 | + return UUID(obj["string"]) |
| 157 | + if module_name == "pathlib" and class_name == "Path": |
| 158 | + return Path(obj["string"]) |
| 159 | + try: |
| 160 | + module = importlib.import_module(module_name) |
| 161 | + cls = getattr(module, class_name) |
| 162 | + except (AttributeError, ImportError, ModuleNotFoundError): |
| 163 | + cls = None |
| 164 | + if cls is not None: |
| 165 | + data = {k: v for k, v in obj.items() if not k.startswith("@")} |
| 166 | + if hasattr(cls, "from_dict"): |
| 167 | + return cls.from_dict(data) |
| 168 | + if isinstance(cls, type) and issubclass(cls, Enum): |
| 169 | + return cls(process_decoded(data["value"])) |
| 170 | + return {process_decoded(k): process_decoded(v) for k, v in obj.items()} |
| 171 | + if isinstance(obj, list): |
| 172 | + return [process_decoded(v) for v in obj] |
| 173 | + return obj |
| 174 | + |
| 175 | + |
| 176 | +def dumpfn( |
| 177 | + obj: Any, |
| 178 | + filename: str | Path, |
| 179 | + *args: Any, |
| 180 | + fmt: str | None = None, |
| 181 | + **kwargs: Any, |
| 182 | +) -> None: |
| 183 | + """Dump an object to JSON, YAML, or msgpack without requiring monty.""" |
| 184 | + fmt = _detect_format(filename, fmt) |
| 185 | + obj = to_serializable(obj) |
| 186 | + if fmt == "json": |
| 187 | + with _open_text(filename, "wt") as fp: |
| 188 | + json.dump(obj, fp, *args, **kwargs) |
| 189 | + return |
| 190 | + if fmt == "yaml": |
| 191 | + with _open_text(filename, "wt") as fp: |
| 192 | + _yaml_dump(obj, fp, *args, **kwargs) |
| 193 | + return |
| 194 | + if fmt == "mpk": |
| 195 | + try: |
| 196 | + import msgpack |
| 197 | + except ModuleNotFoundError as e: |
| 198 | + raise RuntimeError("Dumping msgpack files requires msgpack.") from e |
| 199 | + with _open_binary(filename, "wb") as fp: |
| 200 | + msgpack.dump(obj, fp, *args, **kwargs) |
| 201 | + return |
| 202 | + raise TypeError(f"Invalid format: {fmt}") |
| 203 | + |
| 204 | + |
| 205 | +def loadfn( |
| 206 | + filename: str | Path, |
| 207 | + *args: Any, |
| 208 | + fmt: str | None = None, |
| 209 | + **kwargs: Any, |
| 210 | +) -> Any: |
| 211 | + """Load JSON, YAML, or msgpack data and decode monty-style objects.""" |
| 212 | + fmt = _detect_format(filename, fmt) |
| 213 | + if fmt == "json": |
| 214 | + with _open_text(filename, "rt") as fp: |
| 215 | + return process_decoded(json.load(fp, *args, **kwargs)) |
| 216 | + if fmt == "yaml": |
| 217 | + with _open_text(filename, "rt") as fp: |
| 218 | + return process_decoded(_yaml_load(fp, *args, **kwargs)) |
| 219 | + if fmt == "mpk": |
| 220 | + try: |
| 221 | + import msgpack |
| 222 | + except ModuleNotFoundError as e: |
| 223 | + raise RuntimeError("Loading msgpack files requires msgpack.") from e |
| 224 | + with _open_binary(filename, "rb") as fp: |
| 225 | + return process_decoded(msgpack.load(fp, *args, **kwargs)) |
| 226 | + raise TypeError(f"Invalid format: {fmt}") |
0 commit comments