1
|
|
|
from pathlib import Path |
|
|
|
|
2
|
|
|
from typing import Any, Callable, Mapping, Optional |
3
|
|
|
|
4
|
|
|
import decorateme |
|
|
|
|
5
|
|
|
import defusedxml.ElementTree as Xml |
|
|
|
|
6
|
|
|
import orjson |
|
|
|
|
7
|
|
|
from pocketutils.core.dot_dict import NestedDotDict |
|
|
|
|
8
|
|
|
from pocketutils.core.exceptions import LookupFailedError, XValueError |
|
|
|
|
9
|
|
|
from pocketutils.core.query_utils import QueryExecutor |
|
|
|
|
10
|
|
|
|
11
|
|
|
|
12
|
|
|
@decorateme.auto_obj() |
13
|
|
|
class _Source: |
14
|
|
|
def __init__(self, *getters: Callable[[], Optional[str]], name: str = "unknown"): |
15
|
|
|
self._getters = getters |
16
|
|
|
self._name = name |
17
|
|
|
|
18
|
|
|
def _get_txt(self) -> str: |
19
|
|
|
for getter in self._getters: |
20
|
|
|
txt = getter() |
21
|
|
|
if txt is not None: |
22
|
|
|
return txt |
23
|
|
|
raise LookupFailedError( |
24
|
|
|
f"Nothing found for {self._name} {self.__class__.__name__} source" |
25
|
|
|
) |
26
|
|
|
|
27
|
|
|
@classmethod |
28
|
|
|
def cached_from_url( |
|
|
|
|
29
|
|
|
cls, |
|
|
|
|
30
|
|
|
path: Optional[Path], |
|
|
|
|
31
|
|
|
url: Optional[str], |
|
|
|
|
32
|
|
|
*, |
|
|
|
|
33
|
|
|
query: QueryExecutor, |
|
|
|
|
34
|
|
|
query_args: Optional[Mapping[str, Any]] = None, |
|
|
|
|
35
|
|
|
): |
36
|
|
|
if path is not None and url is None and query is None: |
|
|
|
|
37
|
|
|
return cls.from_path(path) |
38
|
|
|
elif path is None and url is not None and query is not None: |
39
|
|
|
return cls.from_query(url, query, query_args) |
40
|
|
|
elif path is not None and url is not None and query is not None: |
41
|
|
|
path = Path(path) |
42
|
|
|
query_args = {} if query_args is None else query_args |
43
|
|
|
|
44
|
|
|
def load(): |
45
|
|
|
return path.read_text(encoding="utf8") if path.exists() else None |
46
|
|
|
|
47
|
|
|
def save(): |
48
|
|
|
data = query(url, encoding="utf8", **query_args) |
49
|
|
|
if data is not None: |
50
|
|
|
if not path.exists(): |
51
|
|
|
path.write_text(data, encoding="utf8") |
52
|
|
|
return data |
53
|
|
|
|
54
|
|
|
# noinspection PyArgumentList |
55
|
|
|
return cls(load, save, name=f"Cache({path}, {url})") |
56
|
|
|
raise XValueError(f"Cannot create source from path {path}, url {url}, and query {query}") |
57
|
|
|
|
58
|
|
|
@classmethod |
59
|
|
|
def from_path(cls, path: Path): |
|
|
|
|
60
|
|
|
path = Path(path) |
61
|
|
|
|
62
|
|
|
def one(): |
63
|
|
|
return path.read_text(encoding="utf8") if path.exists() else None |
64
|
|
|
|
65
|
|
|
# noinspection PyArgumentList |
66
|
|
|
return cls(one, name=f"Cache({path})") |
67
|
|
|
|
68
|
|
|
@classmethod |
69
|
|
|
def from_query( |
|
|
|
|
70
|
|
|
cls, url: str, query: QueryExecutor, query_args: Optional[Mapping[str, Any]] = None |
|
|
|
|
71
|
|
|
): |
72
|
|
|
def two(): |
73
|
|
|
return query(url, encoding="utf8", **query_args) |
74
|
|
|
|
75
|
|
|
# noinspection PyArgumentList |
76
|
|
|
return cls(two, name=f"Cache({url})") |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
class TextSource(_Source): |
|
|
|
|
80
|
|
|
def get(self) -> str: |
|
|
|
|
81
|
|
|
return self._get_txt() |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
class XmlSource(_Source): |
|
|
|
|
85
|
|
|
""" """ |
86
|
|
|
|
87
|
|
|
def get(self) -> Xml: |
|
|
|
|
88
|
|
|
return Xml.fromstring(self._get_txt()) |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
class JsonSource(_Source): |
|
|
|
|
92
|
|
|
""" """ |
93
|
|
|
|
94
|
|
|
def get(self) -> NestedDotDict: |
|
|
|
|
95
|
|
|
return NestedDotDict(orjson.loads(self._get_txt())) |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
__all__ = ["TextSource", "XmlSource", "JsonSource"] |
99
|
|
|
|