ommlds 0.0.0.dev516__py3-none-any.whl → 0.0.0.dev518__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.
@@ -689,8 +689,8 @@ with _lang.auto_proxy_init(
689
689
  from .resources import ( # noqa
690
690
  ResourcesRef,
691
691
  ResourcesRefNotRegisteredError,
692
- Resources,
693
692
 
693
+ Resources,
694
694
  ResourceManaged,
695
695
 
696
696
  ResourcesOption,
@@ -1,13 +1,8 @@
1
- """
2
- TODO:
3
- - unify with omlish.sql.api.resources -> omlish.resources
4
- """
5
1
  import contextlib
6
2
  import typing as ta
7
3
 
8
- from omlish import check
9
- from omlish import collections as col
10
4
  from omlish import lang
5
+ from omlish import resources as _resources
11
6
  from omlish import typedvalues as tv
12
7
  from omlish.logs import all as logs
13
8
 
@@ -23,191 +18,14 @@ log = logs.get_module_logger(globals())
23
18
  ##
24
19
 
25
20
 
26
- class ResourcesRef(lang.Abstract):
27
- pass
28
-
29
-
30
- class ResourcesRefNotRegisteredError(Exception):
31
- pass
32
-
33
-
34
- @ta.final
35
- class Resources(lang.Final, lang.NotPicklable):
36
- """
37
- Essentially a reference-tracked AsyncContextManager.
38
- """
39
-
40
- def __init__(
41
- self,
42
- *,
43
- init_ref: ResourcesRef | None = None,
44
- no_autoclose: bool = False,
45
- ) -> None:
46
- super().__init__()
47
-
48
- self._no_autoclose = no_autoclose
49
-
50
- self._closed = False
51
-
52
- self._refs: ta.MutableSet[ResourcesRef] = col.IdentitySet()
53
-
54
- self._aes = contextlib.AsyncExitStack()
55
-
56
- if init_ref is not None:
57
- self.add_ref(init_ref)
58
-
59
- async def init(self) -> None:
60
- await self._aes.__aenter__()
61
-
62
- @property
63
- def autoclose(self) -> bool:
64
- return not self._no_autoclose
65
-
66
- @property
67
- def num_refs(self) -> int:
68
- return len(self._refs)
69
-
70
- @property
71
- def closed(self) -> bool:
72
- return self._closed
73
-
74
- def __repr__(self) -> str:
75
- return lang.attr_repr(self, 'closed', 'num_refs', with_id=True)
76
-
77
- #
78
-
79
- class _InitRef(ResourcesRef):
80
- pass
81
-
82
- @classmethod
83
- def new(cls, **kwargs: ta.Any) -> ta.AsyncContextManager['Resources']:
84
- @contextlib.asynccontextmanager
85
- async def inner():
86
- init_ref = Resources._InitRef()
87
-
88
- res = Resources(init_ref=init_ref, **kwargs)
89
-
90
- await res.init()
91
-
92
- try:
93
- yield res
94
-
95
- finally:
96
- await res.remove_ref(init_ref)
97
-
98
- return inner()
99
-
100
- #
101
-
102
- def add_ref(self, ref: ResourcesRef) -> None:
103
- check.isinstance(ref, ResourcesRef)
104
- check.state(not self._closed)
105
-
106
- self._refs.add(ref)
107
-
108
- def has_ref(self, ref: ResourcesRef) -> bool:
109
- return ref in self._refs
110
-
111
- async def remove_ref(self, ref: ResourcesRef) -> None:
112
- check.isinstance(ref, ResourcesRef)
113
-
114
- try:
115
- self._refs.remove(ref)
116
-
117
- except KeyError:
118
- raise ResourcesRefNotRegisteredError(ref) from None
119
-
120
- if not self._no_autoclose and not self._refs:
121
- await self.aclose()
122
-
123
- #
124
-
125
- def enter_context(self, cm: ta.ContextManager[T]) -> T:
126
- check.state(not self._closed)
127
-
128
- return self._aes.enter_context(cm)
129
-
130
- async def enter_async_context(self, cm: ta.AsyncContextManager[T]) -> T:
131
- check.state(not self._closed)
132
-
133
- return await self._aes.enter_async_context(cm)
134
-
135
- #
136
-
137
- def new_managed(self, v: T) -> 'ResourceManaged[T]':
138
- return ResourceManaged(v, self)
139
-
140
- #
141
-
142
- async def aclose(self) -> None:
143
- try:
144
- await self._aes.__aexit__(None, None, None)
145
- finally:
146
- self._closed = True
147
-
148
- def __del__(self) -> None:
149
- if not self._closed:
150
- ref_lst = list(self._refs)
151
- log.error(
152
- f'{__package__}.{self.__class__.__name__}.__del__: ' # noqa
153
- f'%r deleted without being closed! '
154
- f'refs: %s',
155
- repr(self),
156
- ref_lst,
157
- )
158
-
159
-
160
- ##
161
-
162
-
163
- @ta.final
164
- class ResourceManaged(ResourcesRef, lang.Final, lang.NotPicklable, ta.Generic[T]):
165
- """
166
- A class to 'handoff' a ref to a `Resources`, allowing the `Resources` to temporarily survive being passed from
167
- instantiation within a callee.
168
-
169
- This class wraps an arbitrary value, likely an object referencing resources managed by the `Resources`, which is
170
- accessed by `__aenter__`'ing. However, as the point of this class is handoff of a `Resources`, not necessarily some
171
- arbitrary value, the value needn't necessarily be related to the `Resources`, or may even be `None`.
172
-
173
- The ref to the `Resources` is allocated in the ctor, so the contract is that an instance of this must be immediately
174
- `__aenter__`'d before doing anything else with the return value of the call. Failure to do so leaks the `Resources`.
175
- """
176
-
177
- def __init__(self, v: T, resources: Resources) -> None:
178
- super().__init__()
179
-
180
- self.__v = v
181
- self.__resources = resources
182
-
183
- resources.add_ref(self)
184
-
185
- __state: ta.Literal['new', 'entered', 'exited'] = 'new'
186
-
187
- def __repr__(self) -> str:
188
- return f'{self.__class__.__name__}<{self.__v!r}, {self.__state}>'
189
-
190
- async def __aenter__(self) -> T:
191
- check.state(self.__state == 'new')
192
- self.__state = 'entered'
193
-
194
- return self.__v
21
+ ResourcesRef: ta.TypeAlias = _resources.ResourceManagerRef
22
+ ResourcesRefNotRegisteredError: ta.TypeAlias = _resources.ResourceManagerRefNotRegisteredError
195
23
 
196
- async def __aexit__(self, exc_type, exc_val, exc_tb):
197
- check.state(self.__state == 'entered')
198
- self.__state = 'exited'
24
+ Resources: ta.TypeAlias = _resources.AsyncResourceManager
199
25
 
200
- await self.__resources.remove_ref(self)
26
+ # Explicitly not marked as `ta.TypeAlias` because it confuses pycharm.
27
+ ResourceManaged = _resources.AsyncResourceManaged
201
28
 
202
- def __del__(self) -> None:
203
- if self.__state != 'exited':
204
- log.error(
205
- f'{__package__}.{self.__class__.__name__}.__del__: ' # noqa
206
- f'%r deleted without being entered and exited! '
207
- f'resources: %s',
208
- repr(self),
209
- repr(self.__resources),
210
- )
211
29
 
212
30
  ##
213
31
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ommlds
3
- Version: 0.0.0.dev516
3
+ Version: 0.0.0.dev518
4
4
  Summary: ommlds
5
5
  Author: wrmsr
6
6
  License-Expression: BSD-3-Clause
@@ -14,9 +14,9 @@ Classifier: Programming Language :: Python :: 3.13
14
14
  Requires-Python: >=3.13
15
15
  Description-Content-Type: text/markdown
16
16
  License-File: LICENSE
17
- Requires-Dist: omlish==0.0.0.dev516
17
+ Requires-Dist: omlish==0.0.0.dev518
18
18
  Provides-Extra: all
19
- Requires-Dist: omdev==0.0.0.dev516; extra == "all"
19
+ Requires-Dist: omdev==0.0.0.dev518; extra == "all"
20
20
  Requires-Dist: llama-cpp-python~=0.3; extra == "all"
21
21
  Requires-Dist: mlx~=0.30; sys_platform == "darwin" and extra == "all"
22
22
  Requires-Dist: mlx-lm~=0.30; sys_platform == "darwin" and extra == "all"
@@ -39,7 +39,7 @@ Requires-Dist: mwparserfromhell~=0.7; extra == "all"
39
39
  Requires-Dist: wikitextparser~=0.56; extra == "all"
40
40
  Requires-Dist: lxml>=5.3; python_version < "3.13" and extra == "all"
41
41
  Provides-Extra: omdev
42
- Requires-Dist: omdev==0.0.0.dev516; extra == "omdev"
42
+ Requires-Dist: omdev==0.0.0.dev518; extra == "omdev"
43
43
  Provides-Extra: backends
44
44
  Requires-Dist: llama-cpp-python~=0.3; extra == "backends"
45
45
  Requires-Dist: mlx~=0.30; sys_platform == "darwin" and extra == "backends"
@@ -244,7 +244,7 @@ ommlds/cli/state/storage.py,sha256=Wr8DVuEGUxfFJn0tMWNTVdint6NBDdLKstNWSjx8sKw,3
244
244
  ommlds/datasets/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
245
245
  ommlds/datasets/lib/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
246
246
  ommlds/datasets/lib/movies.py,sha256=LmdfoXsZU9XMM_r-sxCLv_s06BFzwWO4xUj6sc9XVcI,1961
247
- ommlds/minichain/__init__.py,sha256=pmYyU-bDodWx3sKU0NmcLXDY5mp8BxM8S60LMghSxgQ,14048
247
+ ommlds/minichain/__init__.py,sha256=eeJ0JZRJjVwp4hkNtoPpDuPDIAwY__BZ_uD7uzahx44,14048
248
248
  ommlds/minichain/_dataclasses.py,sha256=D3K9j3I5-s5LAN9b5ZmWT80rbYMMVURLDpFXf6MkSQc,956494
249
249
  ommlds/minichain/_marshal.py,sha256=n9PGWrHhvAmGIc7KDOYt3IF9Z6G0ncXskyICTp3Ji6k,1923
250
250
  ommlds/minichain/_typedvalues.py,sha256=0EkpyGo1IVnpcsssz8Xdm_vIoqIbb0dKdhZ5AJzAJCk,2292
@@ -253,7 +253,7 @@ ommlds/minichain/configs.py,sha256=WwrHxfkDAfo_RtuCqUgySthj-2W26lZbpuQoghUyGNw,1
253
253
  ommlds/minichain/envs.py,sha256=vE2CSeT6KYxOpPY72VbFLzGUnBERYdhfiEUlvSRHkXE,225
254
254
  ommlds/minichain/json.py,sha256=0_5rV5Zi2qPOvXi2CLAc5DF7FN3jK3ABbjoKdjtTuVo,360
255
255
  ommlds/minichain/metadata.py,sha256=NRrcfN2l4XArHXje3vA9AmkDceXsN-ymT8fbmbW0Ka8,2228
256
- ommlds/minichain/resources.py,sha256=ERGSVNcfuvjY8lLpcIvoB1X4KVn831F4lmUn7wnYw-M,5880
256
+ ommlds/minichain/resources.py,sha256=4tK9ImmW4j7RWg3Ti2wm7u_wZOlQftzpt4bqFn-3Rh4,1150
257
257
  ommlds/minichain/search.py,sha256=YOMJKkTLLSfGKaxltk9bf5OdPq7MpCtLOC3NZ_4iq-E,1269
258
258
  ommlds/minichain/standard.py,sha256=l7C6EXE4FgfJOphl0vHMwCq4FvMNViloz7pPfMsRnGU,2637
259
259
  ommlds/minichain/types.py,sha256=K6RRjpUi17UEG0cqPrrvbVANU0iRVh3WLiH-y6oEWFI,414
@@ -528,9 +528,9 @@ ommlds/wiki/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU
528
528
  ommlds/wiki/utils/io.py,sha256=UKgDJGtmpnWvIqVd2mJc2QNPOqlToEY1GEveNp6_pMo,7088
529
529
  ommlds/wiki/utils/progress.py,sha256=EhvKcMFYtsarCQhIahlO6f0SboyAKP3UwUyrnVnP-Vk,3222
530
530
  ommlds/wiki/utils/xml.py,sha256=sNJNkZ9rT8B-kJMO6bRz8J1USy4fyPx0m2PwTX7vxYY,3846
531
- ommlds-0.0.0.dev516.dist-info/licenses/LICENSE,sha256=B_hVtavaA8zCYDW99DYdcpDLKz1n3BBRjZrcbv8uG8c,1451
532
- ommlds-0.0.0.dev516.dist-info/METADATA,sha256=cmuDX5xzzGE64NG0octLCakvyjJcJox4PU98KqxnX_I,3598
533
- ommlds-0.0.0.dev516.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
534
- ommlds-0.0.0.dev516.dist-info/entry_points.txt,sha256=Z5YWtX7ClfiCKdW-dd_CSVvM0h4yQpJPi-2G3q6gNFo,35
535
- ommlds-0.0.0.dev516.dist-info/top_level.txt,sha256=Rbnk5d5wi58vnAXx13WFZqdQ4VX8hBCS2hEL3WeXOhY,7
536
- ommlds-0.0.0.dev516.dist-info/RECORD,,
531
+ ommlds-0.0.0.dev518.dist-info/licenses/LICENSE,sha256=B_hVtavaA8zCYDW99DYdcpDLKz1n3BBRjZrcbv8uG8c,1451
532
+ ommlds-0.0.0.dev518.dist-info/METADATA,sha256=dh8M2MdgXfCevQdMxafkcXjCGVkI-9gDVIdqA9B9teA,3598
533
+ ommlds-0.0.0.dev518.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
534
+ ommlds-0.0.0.dev518.dist-info/entry_points.txt,sha256=Z5YWtX7ClfiCKdW-dd_CSVvM0h4yQpJPi-2G3q6gNFo,35
535
+ ommlds-0.0.0.dev518.dist-info/top_level.txt,sha256=Rbnk5d5wi58vnAXx13WFZqdQ4VX8hBCS2hEL3WeXOhY,7
536
+ ommlds-0.0.0.dev518.dist-info/RECORD,,