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