codekingpro/portable-devtools
114k
1from __future__ import annotations2 3import json4import pickle5import warnings6from enum import Enum7from pathlib import Path8from typing import TYPE_CHECKING, Any, Callable9 10from typing_extensions import deprecated11 12from ..warnings import PydanticDeprecatedSince2013 14if not TYPE_CHECKING:15 # See PyCharm issues https://youtrack.jetbrains.com/issue/PY-2191516 # and https://youtrack.jetbrains.com/issue/PY-5142817 DeprecationWarning = PydanticDeprecatedSince2018 19 20class Protocol(str, Enum):21 json = 'json'22 pickle = 'pickle'23 24 25@deprecated('`load_str_bytes` is deprecated.', category=None)26def load_str_bytes(27 b: str | bytes,28 *,29 content_type: str | None = None,30 encoding: str = 'utf8',31 proto: Protocol | None = None,32 allow_pickle: bool = False,33 json_loads: Callable[[str], Any] = json.loads,34) -> Any:35 warnings.warn('`load_str_bytes` is deprecated.', category=PydanticDeprecatedSince20, stacklevel=2)36 if proto is None and content_type:37 if content_type.endswith(('json', 'javascript')):38 pass39 elif allow_pickle and content_type.endswith('pickle'):40 proto = Protocol.pickle41 else:42 raise TypeError(f'Unknown content-type: {content_type}')43 44 proto = proto or Protocol.json45 46 if proto == Protocol.json:47 if isinstance(b, bytes):48 b = b.decode(encoding)49 return json_loads(b) # type: ignore50 elif proto == Protocol.pickle:51 if not allow_pickle:52 raise RuntimeError('Trying to decode with pickle with allow_pickle=False')53 bb = b if isinstance(b, bytes) else b.encode() # type: ignore54 return pickle.loads(bb)55 else:56 raise TypeError(f'Unknown protocol: {proto}')57 58 59@deprecated('`load_file` is deprecated.', category=None)60def load_file(61 path: str | Path,62 *,63 content_type: str | None = None,64 encoding: str = 'utf8',65 proto: Protocol | None = None,66 allow_pickle: bool = False,67 json_loads: Callable[[str], Any] = json.loads,68) -> Any:69 warnings.warn('`load_file` is deprecated.', category=PydanticDeprecatedSince20, stacklevel=2)70 path = Path(path)71 b = path.read_bytes()72 if content_type is None:73 if path.suffix in ('.js', '.json'):74 proto = Protocol.json75 elif path.suffix == '.pkl':76 proto = Protocol.pickle77 78 return load_str_bytes(79 b, proto=proto, content_type=content_type, encoding=encoding, allow_pickle=allow_pickle, json_loads=json_loads80 )81 