omlish 0.0.0.dev148__py3-none-any.whl → 0.0.0.dev150__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
omlish/check.py CHANGED
@@ -1,18 +1,13 @@
1
- """
2
- TODO:
3
- - def maybe(v: lang.Maybe[T])
4
- """
5
- import collections
6
- import threading
7
1
  import typing as ta
8
2
 
3
+ from .lite.check import CheckArgsRenderer as ArgsRenderer # noqa
4
+ from .lite.check import CheckExceptionFactory as ExceptionFactory # noqa
5
+ from .lite.check import CheckLateConfigureFn as LateConfigureFn # noqa
6
+ from .lite.check import CheckMessage as Message # noqa
7
+ from .lite.check import CheckOnRaiseFn as OnRaiseFn # noqa
8
+ from .lite.check import Checks
9
+ from .lite.check import check
9
10
 
10
- T = ta.TypeVar('T')
11
- SizedT = ta.TypeVar('SizedT', bound=ta.Sized)
12
-
13
- Message: ta.TypeAlias = str | ta.Callable[..., str | None] | None
14
-
15
- _NONE_TYPE = type(None)
16
11
 
17
12
  _isinstance = isinstance
18
13
  _issubclass = issubclass
@@ -22,43 +17,21 @@ _callable = callable
22
17
  ##
23
18
 
24
19
 
25
- _CONFIG_LOCK = threading.RLock()
26
-
27
-
28
- OnRaiseFn: ta.TypeAlias = ta.Callable[[Exception], None]
29
- _ON_RAISE: ta.Sequence[OnRaiseFn] = []
30
-
31
-
32
- def register_on_raise(fn: OnRaiseFn) -> None:
33
- global _ON_RAISE
34
- with _CONFIG_LOCK:
35
- _ON_RAISE = [*_ON_RAISE, fn]
36
-
37
-
38
- def unregister_on_raise(fn: OnRaiseFn) -> None:
39
- global _ON_RAISE
40
- with _CONFIG_LOCK:
41
- _ON_RAISE = [e for e in _ON_RAISE if e != fn]
20
+ register_on_raise = check.register_on_raise
21
+ unregister_on_raise = check.unregister_on_raise
42
22
 
43
23
 
44
- #
45
-
46
-
47
- _ARGS_RENDERER: ta.Callable[..., str | None] | None = None
48
-
24
+ ##
49
25
 
50
- def _try_enable_args_rendering() -> bool:
51
- global _ARGS_RENDERER
52
- if _ARGS_RENDERER is not None:
53
- return True
54
26
 
27
+ def _try_get_args_rendering() -> ArgsRenderer | None:
55
28
  try:
56
29
  from .diag.asts import ArgsRenderer
57
30
 
58
31
  ArgsRenderer.smoketest()
59
32
 
60
33
  except Exception: # noqa
61
- return False
34
+ return None
62
35
 
63
36
  def _real_render_args(fmt: str, *args: ta.Any) -> str | None:
64
37
  ra = ArgsRenderer(back=3).render_args(*args)
@@ -67,445 +40,59 @@ def _try_enable_args_rendering() -> bool:
67
40
 
68
41
  return fmt % tuple(str(a) for a in ra)
69
42
 
70
- _ARGS_RENDERER = _real_render_args
71
- return True
72
-
73
-
74
- _TRIED_ENABLED_ARGS_RENDERING: bool | None = None
75
-
76
-
77
- def try_enable_args_rendering() -> bool:
78
- global _TRIED_ENABLED_ARGS_RENDERING
79
- if _TRIED_ENABLED_ARGS_RENDERING is not None:
80
- return _TRIED_ENABLED_ARGS_RENDERING
81
-
82
- with _CONFIG_LOCK:
83
- if _TRIED_ENABLED_ARGS_RENDERING is None:
84
- _TRIED_ENABLED_ARGS_RENDERING = _try_enable_args_rendering()
85
-
86
- return _TRIED_ENABLED_ARGS_RENDERING
87
-
88
-
89
- ##
90
-
91
-
92
- def _default_exception_factory(exc_cls: type[Exception], *args, **kwargs) -> Exception:
93
- return exc_cls(*args, **kwargs) # noqa
94
-
95
-
96
- _EXCEPTION_FACTORY = _default_exception_factory
97
-
98
-
99
- class _ArgsKwargs:
100
- def __init__(self, *args, **kwargs):
101
- self.args = args
102
- self.kwargs = kwargs
103
-
104
-
105
- def _raise(
106
- exception_type: type[Exception],
107
- default_message: str,
108
- message: Message,
109
- ak: _ArgsKwargs = _ArgsKwargs(),
110
- *,
111
- render_fmt: str | None = None,
112
- ) -> ta.NoReturn:
113
- exc_args = ()
114
- if _callable(message):
115
- message = ta.cast(ta.Callable, message)(*ak.args, **ak.kwargs)
116
- if _isinstance(message, tuple):
117
- message, *exc_args = message # type: ignore
118
-
119
- if message is None:
120
- message = default_message
121
-
122
- try_enable_args_rendering()
123
-
124
- if render_fmt is not None and _ARGS_RENDERER is not None:
125
- rendered_args = _ARGS_RENDERER(render_fmt, *ak.args)
126
- if rendered_args is not None:
127
- message = f'{message} : {rendered_args}'
128
-
129
- exc = _EXCEPTION_FACTORY(
130
- exception_type,
131
- message,
132
- *exc_args,
133
- *ak.args,
134
- **ak.kwargs,
135
- )
136
-
137
- for fn in _ON_RAISE:
138
- fn(exc)
139
-
140
- raise exc
141
-
142
-
143
- ##
144
-
145
-
146
- def _unpack_isinstance_spec(spec: ta.Any) -> tuple:
147
- if _isinstance(spec, type):
148
- return (spec,)
149
- if not _isinstance(spec, tuple):
150
- spec = (spec,)
151
- if None in spec:
152
- spec = tuple(filter(None, spec)) + (_NONE_TYPE,) # noqa
153
- if ta.Any in spec:
154
- spec = (object,)
155
- return spec
156
-
157
-
158
- def isinstance(v: ta.Any, spec: type[T] | tuple, msg: Message = None) -> T: # noqa
159
- if not _isinstance(v, _unpack_isinstance_spec(spec)):
160
- _raise(
161
- TypeError,
162
- 'Must be instance',
163
- msg,
164
- _ArgsKwargs(v, spec),
165
- render_fmt='not isinstance(%s, %s)',
166
- )
167
-
168
- return v
169
-
170
-
171
- def of_isinstance(spec: type[T] | tuple, msg: Message = None) -> ta.Callable[[ta.Any], T]:
172
- def inner(v):
173
- return isinstance(v, _unpack_isinstance_spec(spec), msg)
174
-
175
- return inner
176
-
177
-
178
- def cast(v: ta.Any, cls: type[T], msg: Message = None) -> T: # noqa
179
- if not _isinstance(v, cls):
180
- _raise(
181
- TypeError,
182
- 'Must be instance',
183
- msg,
184
- _ArgsKwargs(v, cls),
185
- )
186
-
187
- return v
188
-
189
-
190
- def of_cast(cls: type[T], msg: Message = None) -> ta.Callable[[T], T]:
191
- def inner(v):
192
- return isinstance(v, cls, msg)
193
-
194
- return inner
195
-
196
-
197
- def not_isinstance(v: T, spec: ta.Any, msg: Message = None) -> T: # noqa
198
- if _isinstance(v, _unpack_isinstance_spec(spec)):
199
- _raise(
200
- TypeError,
201
- 'Must not be instance',
202
- msg,
203
- _ArgsKwargs(v, spec),
204
- render_fmt='isinstance(%s, %s)',
205
- )
206
-
207
- return v
208
-
43
+ return _real_render_args
209
44
 
210
- def of_not_isinstance(spec: ta.Any, msg: Message = None) -> ta.Callable[[T], T]:
211
- def inner(v):
212
- return not_isinstance(v, _unpack_isinstance_spec(spec), msg)
213
45
 
214
- return inner
46
+ def _try_enable_args_rendering(c: Checks) -> None:
47
+ if (rf := _try_get_args_rendering()) is not None:
48
+ c.set_args_renderer(rf)
215
49
 
216
50
 
217
- ##
218
-
219
-
220
- def issubclass(v: type[T], spec: ta.Any, msg: Message = None) -> type[T]: # noqa
221
- if not _issubclass(v, spec):
222
- _raise(
223
- TypeError,
224
- 'Must be subclass',
225
- msg,
226
- _ArgsKwargs(v, spec),
227
- render_fmt='not issubclass(%s, %s)',
228
- )
229
-
230
- return v
231
-
232
-
233
- def not_issubclass(v: type[T], spec: ta.Any, msg: Message = None) -> type[T]: # noqa
234
- if _issubclass(v, spec):
235
- _raise(
236
- TypeError,
237
- 'Must not be subclass',
238
- msg,
239
- _ArgsKwargs(v, spec),
240
- render_fmt='issubclass(%s, %s)',
241
- )
242
-
243
- return v
244
-
245
-
246
- ##
247
-
248
-
249
- def in_(v: T, c: ta.Container[T], msg: Message = None) -> T:
250
- if v not in c:
251
- _raise(
252
- ValueError,
253
- 'Must be in',
254
- msg,
255
- _ArgsKwargs(v, c),
256
- render_fmt='%s not in %s',
257
- )
258
-
259
- return v
260
-
261
-
262
- def not_in(v: T, c: ta.Container[T], msg: Message = None) -> T:
263
- if v in c:
264
- _raise(
265
- ValueError,
266
- 'Must not be in',
267
- msg,
268
- _ArgsKwargs(v, c),
269
- render_fmt='%s in %s',
270
- )
271
-
272
- return v
273
-
274
-
275
- def empty(v: SizedT, msg: Message = None) -> SizedT:
276
- if len(v) != 0:
277
- _raise(
278
- ValueError,
279
- 'Must be empty',
280
- msg,
281
- _ArgsKwargs(v),
282
- render_fmt='%s',
283
- )
284
-
285
- return v
286
-
287
-
288
- def iterempty(v: ta.Iterable[T], msg: Message = None) -> ta.Iterable[T]:
289
- it = iter(v)
290
- try:
291
- next(it)
292
- except StopIteration:
293
- pass
294
- else:
295
- _raise(
296
- ValueError,
297
- 'Must be empty',
298
- msg,
299
- _ArgsKwargs(v),
300
- render_fmt='%s',
301
- )
302
-
303
- return v
304
-
305
-
306
- def not_empty(v: SizedT, msg: Message = None) -> SizedT:
307
- if len(v) == 0:
308
- _raise(
309
- ValueError,
310
- 'Must not be empty',
311
- msg,
312
- _ArgsKwargs(v),
313
- render_fmt='%s',
314
- )
315
-
316
- return v
317
-
318
-
319
- def unique(it: ta.Iterable[T], msg: Message = None) -> ta.Iterable[T]:
320
- dupes = [e for e, c in collections.Counter(it).items() if c > 1]
321
- if dupes:
322
- _raise(
323
- ValueError,
324
- 'Must be unique',
325
- msg,
326
- _ArgsKwargs(it, dupes),
327
- )
328
-
329
- return it
330
-
331
-
332
- def single(obj: ta.Iterable[T], message: Message = None) -> T:
333
- try:
334
- [value] = obj
335
- except ValueError:
336
- _raise(
337
- ValueError,
338
- 'Must be single',
339
- message,
340
- _ArgsKwargs(obj),
341
- render_fmt='%s',
342
- )
343
-
344
- return value
345
-
346
-
347
- def opt_single(obj: ta.Iterable[T], message: Message = None) -> T | None:
348
- it = iter(obj)
349
- try:
350
- value = next(it)
351
- except StopIteration:
352
- return None
353
-
354
- try:
355
- next(it)
356
- except StopIteration:
357
- return value # noqa
358
-
359
- _raise(
360
- ValueError,
361
- 'Must be empty or single',
362
- message,
363
- _ArgsKwargs(obj),
364
- render_fmt='%s',
365
- )
51
+ check.register_late_configure(_try_enable_args_rendering)
366
52
 
367
53
 
368
54
  ##
369
55
 
370
56
 
371
- def none(v: ta.Any, msg: Message = None) -> None:
372
- if v is not None:
373
- _raise(
374
- ValueError,
375
- 'Must be None',
376
- msg,
377
- _ArgsKwargs(v),
378
- render_fmt='%s',
379
- )
380
-
381
-
382
- def not_none(v: T | None, msg: Message = None) -> T:
383
- if v is None:
384
- _raise(
385
- ValueError,
386
- 'Must not be None',
387
- msg,
388
- _ArgsKwargs(v),
389
- render_fmt='%s',
390
- )
391
-
392
- return v
393
-
394
-
395
- ##
396
-
397
-
398
- def equal(v: T, o: ta.Any, msg: Message = None) -> T:
399
- if o != v:
400
- _raise(
401
- ValueError,
402
- 'Must be equal',
403
- msg,
404
- _ArgsKwargs(v, o),
405
- render_fmt='%s != %s',
406
- )
407
-
408
- return v
409
-
410
-
411
- def is_(v: T, o: ta.Any, msg: Message = None) -> T:
412
- if o is not v:
413
- _raise(
414
- ValueError,
415
- 'Must be the same',
416
- msg,
417
- _ArgsKwargs(v, o),
418
- render_fmt='%s is not %s',
419
- )
420
-
421
- return v
422
-
423
-
424
- def is_not(v: T, o: ta.Any, msg: Message = None) -> T:
425
- if o is v:
426
- _raise(
427
- ValueError,
428
- 'Must not be the same',
429
- msg,
430
- _ArgsKwargs(v, o),
431
- render_fmt='%s is %s',
432
- )
433
-
434
- return v
435
-
436
-
437
- def callable(v: T, msg: Message = None) -> T: # noqa
438
- if not _callable(v):
439
- _raise(
440
- TypeError,
441
- 'Must be callable',
442
- msg,
443
- _ArgsKwargs(v),
444
- render_fmt='%s',
445
- )
446
-
447
- return v # type: ignore
448
-
449
-
450
- def non_empty_str(v: str | None, msg: Message = None) -> str:
451
- if not _isinstance(v, str) or not v:
452
- _raise(
453
- ValueError,
454
- 'Must be non-empty str',
455
- msg,
456
- _ArgsKwargs(v),
457
- render_fmt='%s',
458
- )
459
-
460
- return v
57
+ isinstance = check.isinstance # noqa
58
+ of_isinstance = check.of_isinstance
59
+ cast = check.cast
60
+ of_cast = check.of_cast
61
+ not_isinstance = check.not_isinstance
62
+ of_not_isinstance = check.of_not_isinstance
461
63
 
64
+ #
462
65
 
463
- def replacing(expected: ta.Any, old: ta.Any, new: T, msg: Message = None) -> T:
464
- if old != expected:
465
- _raise(
466
- ValueError,
467
- 'Must be replacing',
468
- msg,
469
- _ArgsKwargs(expected, old, new),
470
- render_fmt='%s -> %s -> %s',
471
- )
66
+ issubclass = check.issubclass # noqa
67
+ not_issubclass = check.not_issubclass
472
68
 
473
- return new
69
+ #
474
70
 
71
+ in_ = check.in_
72
+ not_in = check.not_in
73
+ empty = check.empty
74
+ iterempty = check.iterempty
75
+ not_empty = check.not_empty
76
+ unique = check.unique
77
+ single = check.single
78
+ opt_single = check.opt_single
475
79
 
476
- def replacing_none(old: ta.Any, new: T, msg: Message = None) -> T:
477
- if old is not None:
478
- _raise(
479
- ValueError,
480
- 'Must be replacing None',
481
- msg,
482
- _ArgsKwargs(old, new),
483
- render_fmt='%s -> %s',
484
- )
80
+ #
485
81
 
486
- return new
82
+ none = check.none
83
+ not_none = check.not_none
487
84
 
85
+ #
488
86
 
489
- ##
87
+ equal = check.equal
88
+ is_ = check.is_
89
+ is_not = check.is_not
90
+ callable = check.callable # noqa
91
+ non_empty_str = check.non_empty_str
92
+ replacing = check.replacing
93
+ replacing_none = check.replacing_none
490
94
 
95
+ #
491
96
 
492
- def arg(v: bool, msg: Message = None) -> None:
493
- if not v:
494
- _raise(
495
- RuntimeError,
496
- 'Argument condition not met',
497
- msg,
498
- _ArgsKwargs(v),
499
- render_fmt='%s',
500
- )
501
-
502
-
503
- def state(v: bool, msg: Message = None) -> None:
504
- if not v:
505
- _raise(
506
- RuntimeError,
507
- 'State condition not met',
508
- msg,
509
- _ArgsKwargs(v),
510
- render_fmt='%s',
511
- )
97
+ arg = check.arg
98
+ state = check.state
omlish/http/__init__.py CHANGED
@@ -1,48 +0,0 @@
1
- from . import consts # noqa
2
-
3
- from .clients import ( # noqa
4
- HttpClient,
5
- HttpClientError,
6
- HttpRequest,
7
- HttpResponse,
8
- HttpxHttpClient,
9
- UrllibHttpClient,
10
- client,
11
- request,
12
- )
13
-
14
- from .cookies import ( # noqa
15
- CookieTooBigError,
16
- dump_cookie,
17
- parse_cookie,
18
- )
19
-
20
- from .dates import ( # noqa
21
- http_date,
22
- parse_date,
23
- )
24
-
25
- from .encodings import ( # noqa
26
- latin1_decode,
27
- latin1_encode,
28
- )
29
-
30
- from .headers import ( # noqa
31
- CanHttpHeaders,
32
- HttpHeaders,
33
- headers,
34
- )
35
-
36
- from .json import ( # noqa
37
- JSON_TAGGER,
38
- JsonTag,
39
- JsonTagger,
40
- json_dumps,
41
- json_loads,
42
- )
43
-
44
- from .multipart import ( # noqa
45
- MultipartData,
46
- MultipartEncoder,
47
- MultipartField,
48
- )
omlish/http/all.py ADDED
@@ -0,0 +1,48 @@
1
+ from . import consts # noqa
2
+
3
+ from .clients import ( # noqa
4
+ HttpClient,
5
+ HttpClientError,
6
+ HttpRequest,
7
+ HttpResponse,
8
+ HttpxHttpClient,
9
+ UrllibHttpClient,
10
+ client,
11
+ request,
12
+ )
13
+
14
+ from .cookies import ( # noqa
15
+ CookieTooBigError,
16
+ dump_cookie,
17
+ parse_cookie,
18
+ )
19
+
20
+ from .dates import ( # noqa
21
+ http_date,
22
+ parse_date,
23
+ )
24
+
25
+ from .encodings import ( # noqa
26
+ latin1_decode,
27
+ latin1_encode,
28
+ )
29
+
30
+ from .headers import ( # noqa
31
+ CanHttpHeaders,
32
+ HttpHeaders,
33
+ headers,
34
+ )
35
+
36
+ from .json import ( # noqa
37
+ JSON_TAGGER,
38
+ JsonTag,
39
+ JsonTagger,
40
+ json_dumps,
41
+ json_loads,
42
+ )
43
+
44
+ from .multipart import ( # noqa
45
+ MultipartData,
46
+ MultipartEncoder,
47
+ MultipartField,
48
+ )