skipcheck-tm1 0.1.0.dev0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- skipcheck_tm1/__init__.py +89 -0
- skipcheck_tm1/_calls.py +267 -0
- skipcheck_tm1/_compat.py +71 -0
- skipcheck_tm1/_generated/__init__.py +4 -0
- skipcheck_tm1/_generated/aio_api.py +4659 -0
- skipcheck_tm1/_generated/api.py +4659 -0
- skipcheck_tm1/_generated/index.py +605 -0
- skipcheck_tm1/_generated/model.py +5204 -0
- skipcheck_tm1/_generated/operations.py +127 -0
- skipcheck_tm1/_generated/patches.py +3814 -0
- skipcheck_tm1/_protocol.py +285 -0
- skipcheck_tm1/_resource_base.py +63 -0
- skipcheck_tm1/_sync/__init__.py +26 -0
- skipcheck_tm1/_sync/client.py +397 -0
- skipcheck_tm1/_sync/cognos.py +104 -0
- skipcheck_tm1/_sync/pool.py +295 -0
- skipcheck_tm1/_sync/resources.py +125 -0
- skipcheck_tm1/_sync/services/__init__.py +71 -0
- skipcheck_tm1/_sync/services/_base.py +34 -0
- skipcheck_tm1/_sync/services/annotation.py +186 -0
- skipcheck_tm1/_sync/services/attribute.py +143 -0
- skipcheck_tm1/_sync/services/content.py +175 -0
- skipcheck_tm1/_sync/services/cube.py +327 -0
- skipcheck_tm1/_sync/services/data.py +804 -0
- skipcheck_tm1/_sync/services/dimension.py +903 -0
- skipcheck_tm1/_sync/services/encryption.py +145 -0
- skipcheck_tm1/_sync/services/git.py +205 -0
- skipcheck_tm1/_sync/services/process.py +631 -0
- skipcheck_tm1/_sync/services/security.py +299 -0
- skipcheck_tm1/_sync/services/server.py +255 -0
- skipcheck_tm1/_sync/session.py +839 -0
- skipcheck_tm1/_views.py +89 -0
- skipcheck_tm1/aio/__init__.py +25 -0
- skipcheck_tm1/aio/client.py +396 -0
- skipcheck_tm1/aio/cognos.py +103 -0
- skipcheck_tm1/aio/pool.py +294 -0
- skipcheck_tm1/aio/resources.py +124 -0
- skipcheck_tm1/aio/services/__init__.py +70 -0
- skipcheck_tm1/aio/services/_base.py +33 -0
- skipcheck_tm1/aio/services/annotation.py +185 -0
- skipcheck_tm1/aio/services/attribute.py +142 -0
- skipcheck_tm1/aio/services/content.py +174 -0
- skipcheck_tm1/aio/services/cube.py +326 -0
- skipcheck_tm1/aio/services/data.py +803 -0
- skipcheck_tm1/aio/services/dimension.py +902 -0
- skipcheck_tm1/aio/services/encryption.py +144 -0
- skipcheck_tm1/aio/services/git.py +204 -0
- skipcheck_tm1/aio/services/process.py +630 -0
- skipcheck_tm1/aio/services/security.py +298 -0
- skipcheck_tm1/aio/services/server.py +254 -0
- skipcheck_tm1/aio/session.py +838 -0
- skipcheck_tm1/auth.py +119 -0
- skipcheck_tm1/base.py +269 -0
- skipcheck_tm1/client.py +5 -0
- skipcheck_tm1/errors.py +405 -0
- skipcheck_tm1/identity.py +114 -0
- skipcheck_tm1/model.py +7 -0
- skipcheck_tm1/odata.py +837 -0
- skipcheck_tm1/operation.py +74 -0
- skipcheck_tm1/request.py +252 -0
- skipcheck_tm1/resources.py +17 -0
- skipcheck_tm1/services.py +4 -0
- skipcheck_tm1/session.py +6 -0
- skipcheck_tm1/timeouts.py +91 -0
- skipcheck_tm1/values.py +429 -0
- skipcheck_tm1/version.py +369 -0
- skipcheck_tm1-0.1.0.dev0.dist-info/METADATA +101 -0
- skipcheck_tm1-0.1.0.dev0.dist-info/RECORD +69 -0
- skipcheck_tm1-0.1.0.dev0.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"""A Python client for the IBM Planning Analytics (TM1) REST API.
|
|
2
|
+
|
|
3
|
+
Generated from the same language-neutral API spec as the Java client (``spec/tm1-api.json``,
|
|
4
|
+
from TM1's ``$metadata``), so it covers every endpoint the spec lists::
|
|
5
|
+
|
|
6
|
+
from skipcheck_tm1 import Tm1Client
|
|
7
|
+
|
|
8
|
+
with Tm1Client.builder().base_url("https://tm1:8010/api/v1").basic_auth("admin", "apple").build() as tm1:
|
|
9
|
+
print(tm1.server.product_version())
|
|
10
|
+
|
|
11
|
+
Not affiliated with or endorsed by IBM.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from . import auth, model
|
|
15
|
+
from .base import Tm1Enum, Tm1Model
|
|
16
|
+
from .client import Tm1Batch, Tm1Client, Tm1ClientBuilder
|
|
17
|
+
from .errors import (
|
|
18
|
+
Tm1ApiError,
|
|
19
|
+
Tm1AuthenticationError,
|
|
20
|
+
Tm1BadRequestError,
|
|
21
|
+
Tm1ConflictError,
|
|
22
|
+
Tm1Error,
|
|
23
|
+
Tm1ErrorClassifier,
|
|
24
|
+
Tm1ErrorKind,
|
|
25
|
+
Tm1ForbiddenError,
|
|
26
|
+
Tm1NotFoundError,
|
|
27
|
+
Tm1NotImplementedError,
|
|
28
|
+
Tm1PreconditionFailedError,
|
|
29
|
+
Tm1ProtocolError,
|
|
30
|
+
Tm1ServerError,
|
|
31
|
+
Tm1SessionClosedError,
|
|
32
|
+
Tm1TransportError,
|
|
33
|
+
Tm1UnsupportedOperationError,
|
|
34
|
+
)
|
|
35
|
+
from .identity import Tm1Identity
|
|
36
|
+
from .odata import Bind, Filter, ODataPath, ODataProperty, ODataQuery, Raw
|
|
37
|
+
from .operation import Tm1Operation
|
|
38
|
+
from .request import Tm1Request
|
|
39
|
+
from .resources import UNSET
|
|
40
|
+
from .session import Tm1PoolStats, Tm1Session, Tm1SessionBatch, Tm1SessionBuilder, Tm1SessionPool, Tm1SessionPoolBuilder
|
|
41
|
+
from .timeouts import Tm1Timeouts
|
|
42
|
+
from .version import Tm1Capabilities, Tm1Capability, Tm1Dialect, Tm1Version
|
|
43
|
+
|
|
44
|
+
__all__ = [
|
|
45
|
+
"UNSET",
|
|
46
|
+
"Bind",
|
|
47
|
+
"Filter",
|
|
48
|
+
"ODataProperty",
|
|
49
|
+
"Tm1Batch",
|
|
50
|
+
"Tm1Capabilities",
|
|
51
|
+
"Tm1Capability",
|
|
52
|
+
"Tm1ClientBuilder",
|
|
53
|
+
"Tm1Dialect",
|
|
54
|
+
"Tm1ErrorClassifier",
|
|
55
|
+
"Tm1Operation",
|
|
56
|
+
"Tm1Request",
|
|
57
|
+
"Tm1Identity",
|
|
58
|
+
"Tm1PoolStats",
|
|
59
|
+
"Tm1SessionBatch",
|
|
60
|
+
"Tm1SessionBuilder",
|
|
61
|
+
"Tm1SessionClosedError",
|
|
62
|
+
"Tm1SessionPool",
|
|
63
|
+
"Tm1SessionPoolBuilder",
|
|
64
|
+
"Tm1Timeouts",
|
|
65
|
+
"Tm1Version",
|
|
66
|
+
"ODataPath",
|
|
67
|
+
"ODataQuery",
|
|
68
|
+
"Raw",
|
|
69
|
+
"Tm1ApiError",
|
|
70
|
+
"Tm1AuthenticationError",
|
|
71
|
+
"Tm1BadRequestError",
|
|
72
|
+
"Tm1Client",
|
|
73
|
+
"Tm1ConflictError",
|
|
74
|
+
"Tm1Enum",
|
|
75
|
+
"Tm1Error",
|
|
76
|
+
"Tm1ErrorKind",
|
|
77
|
+
"Tm1ForbiddenError",
|
|
78
|
+
"Tm1Model",
|
|
79
|
+
"Tm1NotFoundError",
|
|
80
|
+
"Tm1NotImplementedError",
|
|
81
|
+
"Tm1PreconditionFailedError",
|
|
82
|
+
"Tm1ProtocolError",
|
|
83
|
+
"Tm1ServerError",
|
|
84
|
+
"Tm1Session",
|
|
85
|
+
"Tm1TransportError",
|
|
86
|
+
"Tm1UnsupportedOperationError",
|
|
87
|
+
"auth",
|
|
88
|
+
"model",
|
|
89
|
+
]
|
skipcheck_tm1/_calls.py
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
"""The requests resources make, as :class:`~skipcheck_tm1._protocol.Call` values: built once,
|
|
2
|
+
sent by the sync session, the asyncio one, a batch, or as a TM1 async operation."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import functools
|
|
7
|
+
import json
|
|
8
|
+
from collections.abc import Mapping
|
|
9
|
+
from dataclasses import dataclass
|
|
10
|
+
from typing import Any, ClassVar
|
|
11
|
+
|
|
12
|
+
from pydantic import TypeAdapter
|
|
13
|
+
|
|
14
|
+
from ._protocol import TEXT, Call, Reply
|
|
15
|
+
from .base import Tm1Model, to_odata
|
|
16
|
+
from .errors import Tm1ProtocolError
|
|
17
|
+
from .odata import Alias, Bind, ODataPath, ODataQuery
|
|
18
|
+
|
|
19
|
+
__all__ = [
|
|
20
|
+
"UNSET",
|
|
21
|
+
"Page",
|
|
22
|
+
"Returns",
|
|
23
|
+
"Unset",
|
|
24
|
+
"endpoint",
|
|
25
|
+
"implemented_endpoints",
|
|
26
|
+
"query_of",
|
|
27
|
+
"register_endpoint",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
# Coverage: which code implements which endpoint of the spec (spec/tm1-api.json)
|
|
32
|
+
|
|
33
|
+
_IMPLEMENTED: dict[str, list[str]] = {}
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def register_endpoint(endpoint_id: str, where: str) -> None:
|
|
37
|
+
"""Records that ``where`` implements ``endpoint_id`` (a coverage-gate ID)."""
|
|
38
|
+
_IMPLEMENTED.setdefault(endpoint_id, []).append(where)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def endpoint(*ids: str) -> Any:
|
|
42
|
+
"""Marks a method as implementing endpoints, for the coverage gate (as ``@Tm1Endpoint`` does
|
|
43
|
+
in the Java client). Generated and hand-written code use it alike."""
|
|
44
|
+
|
|
45
|
+
def mark(f: Any) -> Any:
|
|
46
|
+
for endpoint_id in ids:
|
|
47
|
+
register_endpoint(endpoint_id, f"{f.__module__}.{f.__qualname__}")
|
|
48
|
+
f.__tm1_endpoints__ = ids
|
|
49
|
+
return f
|
|
50
|
+
|
|
51
|
+
return mark
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
FLAVORS = {
|
|
55
|
+
"sync": ("skipcheck_tm1._generated.api.", "skipcheck_tm1._generated.model.", "skipcheck_tm1._sync."),
|
|
56
|
+
"async": ("skipcheck_tm1._generated.aio_api.", "skipcheck_tm1._generated.model.", "skipcheck_tm1.aio."),
|
|
57
|
+
}
|
|
58
|
+
"""Where each flavor's implementations live: the model (complex types' navigation, read
|
|
59
|
+
through ``$expand``) is shared."""
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def implemented_endpoints(flavor: str | None = None) -> dict[str, list[str]]:
|
|
63
|
+
"""Every endpoint some code implements, with where; only the ``sync`` or ``async``
|
|
64
|
+
flavor's if given."""
|
|
65
|
+
# Importing them registers what they implement
|
|
66
|
+
from . import aio, client # noqa: F401
|
|
67
|
+
from ._generated import aio_api, api, model # noqa: F401
|
|
68
|
+
from ._sync import client as _sync_client # noqa: F401
|
|
69
|
+
|
|
70
|
+
prefixes = None if flavor is None else FLAVORS[flavor]
|
|
71
|
+
out: dict[str, list[str]] = {}
|
|
72
|
+
for k, where in _IMPLEMENTED.items():
|
|
73
|
+
kept = [w for w in where if prefixes is None or w.startswith(prefixes)]
|
|
74
|
+
if kept:
|
|
75
|
+
out[k] = kept
|
|
76
|
+
return out
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
class Unset:
|
|
80
|
+
"""The value of a parameter that wasn't passed: it's left out of the request, where
|
|
81
|
+
``None`` sends an explicit ``null``."""
|
|
82
|
+
|
|
83
|
+
_instance: ClassVar[Unset | None] = None
|
|
84
|
+
|
|
85
|
+
def __new__(cls) -> Unset:
|
|
86
|
+
if cls._instance is None:
|
|
87
|
+
cls._instance = super().__new__(cls)
|
|
88
|
+
return cls._instance
|
|
89
|
+
|
|
90
|
+
def __repr__(self) -> str:
|
|
91
|
+
return "UNSET"
|
|
92
|
+
|
|
93
|
+
def __bool__(self) -> bool:
|
|
94
|
+
return False
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
UNSET: Any = Unset()
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def query_of(query: ODataQuery | None, options: Mapping[str, Any]) -> ODataQuery | None:
|
|
101
|
+
"""A query from an :class:`ODataQuery`, keyword options (``select=``, ``filter=``, ``top=``…), or both."""
|
|
102
|
+
if not options:
|
|
103
|
+
return query
|
|
104
|
+
return ODataQuery.of(query, **options)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
@functools.cache
|
|
108
|
+
def _adapter(tp: Any) -> TypeAdapter[Any]:
|
|
109
|
+
return TypeAdapter(tp)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
@dataclass(frozen=True)
|
|
113
|
+
class Page:
|
|
114
|
+
"""One page of a collection, and the link to the next, if any."""
|
|
115
|
+
|
|
116
|
+
items: list[Any]
|
|
117
|
+
next_link: str | None
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def page_of(tp: Any, reply: Reply) -> Page:
|
|
121
|
+
doc = reply.json()
|
|
122
|
+
if not isinstance(doc, dict) or "value" not in doc:
|
|
123
|
+
raise Tm1ProtocolError(f"Expected a collection, got {str(doc)[:200]}")
|
|
124
|
+
return Page(_adapter(list[tp]).validate_python(doc["value"]), doc.get("@odata.nextLink"))
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
class Returns:
|
|
128
|
+
"""What an operation returns, and how it's read: a model, a primitive wrapped as
|
|
129
|
+
``{"value": …}``, a collection as ``{"value": […]}``, or nothing."""
|
|
130
|
+
|
|
131
|
+
__slots__ = ("type", "collection", "structured")
|
|
132
|
+
|
|
133
|
+
def __init__(self, tp: Any = None, *, collection: bool = False, structured: bool = False):
|
|
134
|
+
self.type = tp
|
|
135
|
+
self.collection = collection
|
|
136
|
+
self.structured = structured
|
|
137
|
+
|
|
138
|
+
@property
|
|
139
|
+
def pages(self) -> bool:
|
|
140
|
+
"""Whether the result is a collection of entities TM1 may send in pages."""
|
|
141
|
+
return self.collection and self.structured
|
|
142
|
+
|
|
143
|
+
def read(self, reply: Reply) -> Any:
|
|
144
|
+
if self.type is None:
|
|
145
|
+
return None
|
|
146
|
+
if self.pages:
|
|
147
|
+
return page_of(self.type, reply)
|
|
148
|
+
doc = reply.json()
|
|
149
|
+
if doc is None:
|
|
150
|
+
return [] if self.collection else None
|
|
151
|
+
if self.collection:
|
|
152
|
+
if not isinstance(doc, dict) or "value" not in doc:
|
|
153
|
+
raise Tm1ProtocolError(f"Expected a collection, got {str(doc)[:200]}")
|
|
154
|
+
return _adapter(list[self.type]).validate_python(doc["value"])
|
|
155
|
+
if self.structured:
|
|
156
|
+
return _adapter(self.type).validate_python(doc)
|
|
157
|
+
# TM1 wraps a primitive result as {"value": …}
|
|
158
|
+
value = doc.get("value") if isinstance(doc, dict) else doc
|
|
159
|
+
return _adapter(self.type | None).validate_python(value)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def _body(parameters: Mapping[str, Any]) -> dict[str, Any]:
|
|
163
|
+
body: dict[str, Any] = {}
|
|
164
|
+
for name, value in parameters.items():
|
|
165
|
+
if value is UNSET:
|
|
166
|
+
continue
|
|
167
|
+
if isinstance(value, Bind):
|
|
168
|
+
body[name + "@odata.bind"] = str(value)
|
|
169
|
+
elif isinstance(value, (list, tuple)) and value and all(isinstance(v, Bind) for v in value):
|
|
170
|
+
body[name + "@odata.bind"] = [str(v) for v in value]
|
|
171
|
+
else:
|
|
172
|
+
body[name] = to_odata(value)
|
|
173
|
+
return body
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def _structured(value: Any) -> bool:
|
|
177
|
+
return isinstance(value, (Tm1Model, Mapping)) or (isinstance(value, (list, tuple)) and any(isinstance(v, (Tm1Model, Mapping)) for v in value))
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
# The calls
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def _write(value: Any) -> Any:
|
|
184
|
+
"""A write's body: a model by what was set, a patch (``CubePatch.builder()…build()``), or a
|
|
185
|
+
mapping (``Bind`` values become ``@odata.bind``)."""
|
|
186
|
+
from .request import body_of
|
|
187
|
+
|
|
188
|
+
return body_of(value)
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def action(
|
|
192
|
+
path: ODataPath,
|
|
193
|
+
name: str,
|
|
194
|
+
parameters: Mapping[str, Any],
|
|
195
|
+
returns: Returns,
|
|
196
|
+
query: ODataQuery | None,
|
|
197
|
+
operation: str | None,
|
|
198
|
+
) -> Call:
|
|
199
|
+
target = path.action(name) if path.segments else ODataPath.of(name)
|
|
200
|
+
return Call("POST", str(target), query, json_body=_body(parameters), read=returns.read, operation=operation)
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def function(
|
|
204
|
+
path: ODataPath,
|
|
205
|
+
name: str,
|
|
206
|
+
parameters: Mapping[str, Any],
|
|
207
|
+
returns: Returns,
|
|
208
|
+
query: ODataQuery | None,
|
|
209
|
+
operation: str | None,
|
|
210
|
+
) -> Call:
|
|
211
|
+
arguments = {k: v for k, v in parameters.items() if v is not UNSET}
|
|
212
|
+
# A structured value can't be a literal: it's passed as JSON through an alias
|
|
213
|
+
aliases: dict[str, str] = {}
|
|
214
|
+
for parameter, value in list(arguments.items()):
|
|
215
|
+
if _structured(value):
|
|
216
|
+
aliases["@" + parameter] = json.dumps(to_odata(value), separators=(",", ":"), ensure_ascii=False)
|
|
217
|
+
arguments[parameter] = Alias("@" + parameter)
|
|
218
|
+
for alias, json_value in aliases.items():
|
|
219
|
+
query = (query or ODataQuery.none()).alias(alias, json_value)
|
|
220
|
+
target = path.function(name, arguments) if path.segments else ODataPath.function_import(name, arguments)
|
|
221
|
+
return Call("GET", str(target), query, read=returns.read, operation=operation)
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def get_entity(path: ODataPath, query: ODataQuery | None, model: type[Tm1Model]) -> Call:
|
|
225
|
+
return Call("GET", str(path), query, read=lambda r: model.model_validate(r.json()))
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def update(path: ODataPath, changes: Any, if_match: str | None) -> Call:
|
|
229
|
+
body = _write(changes)
|
|
230
|
+
return Call("PATCH", str(path), json_body=body, headers={"If-Match": if_match} if if_match else {})
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def delete(path: ODataPath, if_match: str | None) -> Call:
|
|
234
|
+
return Call("DELETE", str(path), headers={"If-Match": if_match} if if_match else {})
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
def page(path: ODataPath, query: ODataQuery | None, model: type[Tm1Model]) -> Call:
|
|
238
|
+
return Call("GET", str(path), query, read=lambda r: page_of(model, r))
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
def count(path: ODataPath, filter: str | None) -> Call:
|
|
242
|
+
query = None if filter is None else ODataQuery(filter=filter)
|
|
243
|
+
|
|
244
|
+
def read(reply: Reply) -> int:
|
|
245
|
+
return int(reply.text.strip().lstrip(""))
|
|
246
|
+
|
|
247
|
+
return Call("GET", str(path.count()), query, accept=TEXT, read=read)
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def create(path: ODataPath, entity: Any, model: type[Tm1Model]) -> Call:
|
|
251
|
+
body = _write(entity)
|
|
252
|
+
|
|
253
|
+
def read(reply: Reply) -> Any:
|
|
254
|
+
doc = reply.json()
|
|
255
|
+
return None if doc is None else model.model_validate(doc)
|
|
256
|
+
|
|
257
|
+
return Call("POST", str(path), json_body=body, read=read)
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
def read_stream(path: ODataPath, prop: str | None) -> Call:
|
|
261
|
+
target = path.value() if prop is None else path.nav(prop).value()
|
|
262
|
+
return Call("GET", str(target), accept="*/*", read=lambda r: r.content, raw=True)
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def write_stream(path: ODataPath, prop: str | None, data: bytes, content_type: str) -> Call:
|
|
266
|
+
target = path.value() if prop is None else path.nav(prop).value()
|
|
267
|
+
return Call("PUT", str(target), content=data, headers={"Content-Type": content_type}, raw=True)
|
skipcheck_tm1/_compat.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"""The few primitives the asyncio and sync flavors do differently.
|
|
2
|
+
|
|
3
|
+
The asyncio code (:mod:`skipcheck_tm1.aio`) is the source of truth; the sync flavor
|
|
4
|
+
(``skipcheck_tm1._sync``) is generated from it by ``tm1_pygen.unasync``, which turns
|
|
5
|
+
``AsyncIO`` into ``SyncIO``, drops ``async`` and ``await``, and renames ``Async*`` types.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import asyncio
|
|
11
|
+
import inspect
|
|
12
|
+
import threading
|
|
13
|
+
import time
|
|
14
|
+
from collections.abc import Awaitable
|
|
15
|
+
from typing import Any, TypeVar
|
|
16
|
+
|
|
17
|
+
import httpx
|
|
18
|
+
|
|
19
|
+
T = TypeVar("T")
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class AsyncIO:
|
|
23
|
+
Lock = asyncio.Lock
|
|
24
|
+
HttpClient = httpx.AsyncClient
|
|
25
|
+
sleep = staticmethod(asyncio.sleep)
|
|
26
|
+
|
|
27
|
+
@staticmethod
|
|
28
|
+
def shield(awaitable: Awaitable[T]) -> Awaitable[T]:
|
|
29
|
+
"""Runs ``awaitable`` to the end even if the caller is cancelled (cleanup)."""
|
|
30
|
+
return asyncio.shield(awaitable)
|
|
31
|
+
|
|
32
|
+
@staticmethod
|
|
33
|
+
def monotonic() -> float:
|
|
34
|
+
return time.monotonic()
|
|
35
|
+
|
|
36
|
+
@staticmethod
|
|
37
|
+
async def resolve(value: Any) -> Any:
|
|
38
|
+
"""What a caller's function returned: awaited, if it's awaitable."""
|
|
39
|
+
return await value if inspect.isawaitable(value) else value
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class _SyncLock:
|
|
43
|
+
"""A re-entrant-free lock used with ``with``, as ``asyncio.Lock`` is with ``async with``."""
|
|
44
|
+
|
|
45
|
+
def __init__(self) -> None:
|
|
46
|
+
self._lock = threading.Lock()
|
|
47
|
+
|
|
48
|
+
def __enter__(self) -> _SyncLock:
|
|
49
|
+
self._lock.acquire()
|
|
50
|
+
return self
|
|
51
|
+
|
|
52
|
+
def __exit__(self, *exc: Any) -> None:
|
|
53
|
+
self._lock.release()
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class SyncIO:
|
|
57
|
+
Lock = _SyncLock
|
|
58
|
+
HttpClient = httpx.Client
|
|
59
|
+
sleep = staticmethod(time.sleep)
|
|
60
|
+
|
|
61
|
+
@staticmethod
|
|
62
|
+
def shield(value: T) -> T:
|
|
63
|
+
return value
|
|
64
|
+
|
|
65
|
+
@staticmethod
|
|
66
|
+
def resolve(value: Any) -> Any:
|
|
67
|
+
return value
|
|
68
|
+
|
|
69
|
+
@staticmethod
|
|
70
|
+
def monotonic() -> float:
|
|
71
|
+
return time.monotonic()
|