codie-util-twisted 0.1.0__tar.gz
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.
- codie_util_twisted-0.1.0/.gitignore +40 -0
- codie_util_twisted-0.1.0/.gitlab-ci.yml +24 -0
- codie_util_twisted-0.1.0/PKG-INFO +14 -0
- codie_util_twisted-0.1.0/codie_util_twisted/__init__.py +1 -0
- codie_util_twisted-0.1.0/codie_util_twisted/defer_util.py +142 -0
- codie_util_twisted-0.1.0/codie_util_twisted/defer_util_test.py +86 -0
- codie_util_twisted-0.1.0/codie_util_twisted/mypy_twisted_plugin.py +53 -0
- codie_util_twisted-0.1.0/codie_util_twisted/py.typed +1 -0
- codie_util_twisted-0.1.0/codie_util_twisted/typed_reactor.py +62 -0
- codie_util_twisted-0.1.0/publish.sh +73 -0
- codie_util_twisted-0.1.0/pyproject.toml +76 -0
- codie_util_twisted-0.1.0/stubs/twisted/internet/defer.pyi +75 -0
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# OS
|
|
2
|
+
.DS_Store
|
|
3
|
+
Thumbs.db
|
|
4
|
+
|
|
5
|
+
# Python
|
|
6
|
+
.venv/
|
|
7
|
+
__pycache__/
|
|
8
|
+
**/__pycache__/
|
|
9
|
+
*.pyc
|
|
10
|
+
*.pyo
|
|
11
|
+
*.py[cod]
|
|
12
|
+
.env
|
|
13
|
+
.env.*
|
|
14
|
+
dist/
|
|
15
|
+
build/
|
|
16
|
+
*.egg-info/
|
|
17
|
+
.pytest_cache/
|
|
18
|
+
.coverage
|
|
19
|
+
.coverage.*
|
|
20
|
+
htmlcov/
|
|
21
|
+
|
|
22
|
+
# QA tool caches
|
|
23
|
+
.mypy_cache/
|
|
24
|
+
.ruff_cache/
|
|
25
|
+
|
|
26
|
+
# Twisted trial temp dir
|
|
27
|
+
**/_trial_temp/
|
|
28
|
+
|
|
29
|
+
# Node / Angular (codie-shared-ui — workspace_node.md)
|
|
30
|
+
node_modules/
|
|
31
|
+
.angular/
|
|
32
|
+
|
|
33
|
+
# Built UI library pack (scripts/build-pack.sh output)
|
|
34
|
+
**/codie-shared-ui.tgz
|
|
35
|
+
|
|
36
|
+
# IDE
|
|
37
|
+
.vscode/
|
|
38
|
+
|
|
39
|
+
# Local agent test logs
|
|
40
|
+
.phase1-test.log
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Lint and test stages — same checks as local `task qa` and `task test`.
|
|
2
|
+
|
|
3
|
+
stages:
|
|
4
|
+
- lint
|
|
5
|
+
- test
|
|
6
|
+
|
|
7
|
+
lint:python:
|
|
8
|
+
stage: lint
|
|
9
|
+
image: python:3.12
|
|
10
|
+
before_script:
|
|
11
|
+
- pip install -e ".[dev]"
|
|
12
|
+
script:
|
|
13
|
+
- ruff check codie_util_twisted/ --no-fix
|
|
14
|
+
- basedpyright codie_util_twisted/
|
|
15
|
+
- mypy codie_util_twisted/
|
|
16
|
+
- vulture codie_util_twisted/ --min-confidence 80
|
|
17
|
+
|
|
18
|
+
test:python:
|
|
19
|
+
stage: test
|
|
20
|
+
image: python:3.12
|
|
21
|
+
before_script:
|
|
22
|
+
- pip install -e ".[dev]"
|
|
23
|
+
script:
|
|
24
|
+
- pytest codie_util_twisted/
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: codie-util-twisted
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
License-Expression: MIT
|
|
5
|
+
Requires-Python: >=3.12
|
|
6
|
+
Requires-Dist: mypy>=1.0
|
|
7
|
+
Requires-Dist: twisted<26,>=25.5.0
|
|
8
|
+
Provides-Extra: dev
|
|
9
|
+
Requires-Dist: basedpyright>=1.20; extra == 'dev'
|
|
10
|
+
Requires-Dist: pytest-twisted>=1.14; extra == 'dev'
|
|
11
|
+
Requires-Dist: pytest>=8.0; extra == 'dev'
|
|
12
|
+
Requires-Dist: ruff>=0.15; extra == 'dev'
|
|
13
|
+
Requires-Dist: taskipy>=1.13; extra == 'dev'
|
|
14
|
+
Requires-Dist: vulture>=2.14; extra == 'dev'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = '0.1.0'
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import functools
|
|
3
|
+
import inspect
|
|
4
|
+
import logging
|
|
5
|
+
from collections.abc import Callable
|
|
6
|
+
from typing import Any, ParamSpec, TypeVar, cast
|
|
7
|
+
|
|
8
|
+
import twisted.python.threadable
|
|
9
|
+
from twisted.internet.defer import Deferred
|
|
10
|
+
from twisted.internet.threads import deferToThread
|
|
11
|
+
|
|
12
|
+
from codie_util_twisted.typed_reactor import TypedReactor
|
|
13
|
+
|
|
14
|
+
_P = ParamSpec("_P")
|
|
15
|
+
_R = TypeVar("_R")
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def convertAsyncioMethodToDeferred(
|
|
19
|
+
method: Callable[_P, Any],
|
|
20
|
+
) -> Callable[_P, Deferred[Any]]:
|
|
21
|
+
@functools.wraps(method)
|
|
22
|
+
def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> Deferred[Any]:
|
|
23
|
+
return Deferred.fromFuture(asyncio.create_task(method(*args, **kwargs)))
|
|
24
|
+
|
|
25
|
+
return wrapper
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class YieldInDeferToThreadException(Exception):
|
|
29
|
+
pass
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def vortexLogFailure(
|
|
33
|
+
failure: Any,
|
|
34
|
+
loggerArg: logging.Logger,
|
|
35
|
+
consumeError: bool = False,
|
|
36
|
+
successValue: Any = True,
|
|
37
|
+
) -> Any:
|
|
38
|
+
"""Log a Twisted Failure instance using the provided logger."""
|
|
39
|
+
try:
|
|
40
|
+
if not hasattr(failure, "_vortexFailureLogged"):
|
|
41
|
+
exc_info = (
|
|
42
|
+
failure.type,
|
|
43
|
+
failure.value,
|
|
44
|
+
failure.getTracebackObject(),
|
|
45
|
+
)
|
|
46
|
+
loggerArg.exception(str(failure.value), exc_info=exc_info)
|
|
47
|
+
failure._vortexFailureLogged = True
|
|
48
|
+
return successValue if consumeError else failure
|
|
49
|
+
except Exception as exc:
|
|
50
|
+
loggerArg.exception("Error while logging failure: %s", str(exc))
|
|
51
|
+
return failure
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def deferToThreadWrapWithLogger(
|
|
55
|
+
logger: logging.Logger,
|
|
56
|
+
consumeError: bool = False,
|
|
57
|
+
checkMainThread: bool = True,
|
|
58
|
+
) -> Callable[[Callable[_P, _R]], Callable[_P, Deferred[_R]]]:
|
|
59
|
+
"""Decorator to run a blocking function in a thread pool via deferToThread.
|
|
60
|
+
|
|
61
|
+
Usage::
|
|
62
|
+
|
|
63
|
+
import logging
|
|
64
|
+
logger = logging.getLogger(__name__)
|
|
65
|
+
|
|
66
|
+
@deferToThreadWrapWithLogger(logger)
|
|
67
|
+
def myBlockingFunction(arg1, kw=True):
|
|
68
|
+
pass
|
|
69
|
+
|
|
70
|
+
The decorated function returns a Deferred when called. Yield it inside
|
|
71
|
+
an @inlineCallbacks function.
|
|
72
|
+
"""
|
|
73
|
+
assert isinstance(logger, logging.Logger), (
|
|
74
|
+
"deferToThreadWrapWithLogger requires a logging.Logger from "
|
|
75
|
+
"logging.getLogger(__name__)"
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
def wrapper(funcToWrap: Callable[_P, _R]) -> Callable[_P, Deferred[_R]]:
|
|
79
|
+
def checkForInlineCallbackYield(result: _R) -> _R:
|
|
80
|
+
if inspect.isgenerator(result):
|
|
81
|
+
logger.error(
|
|
82
|
+
"Module Name %s, Class Name %s, Function Name %s has a yield in it. IT SHOULD NOT.",
|
|
83
|
+
funcToWrap.__module__,
|
|
84
|
+
funcToWrap.__class__.__name__,
|
|
85
|
+
funcToWrap.__name__,
|
|
86
|
+
)
|
|
87
|
+
raise YieldInDeferToThreadException()
|
|
88
|
+
return result
|
|
89
|
+
|
|
90
|
+
def func(*args: _P.args, **kwargs: _P.kwargs) -> Deferred[_R]:
|
|
91
|
+
if not twisted.python.threadable.isInIOThread() and checkMainThread:
|
|
92
|
+
logger.error(
|
|
93
|
+
"Module Name %s, Class Name %s, Function Name %s is being deferred from a thread. IT SHOULD NOT.",
|
|
94
|
+
funcToWrap.__module__,
|
|
95
|
+
funcToWrap.__class__.__name__,
|
|
96
|
+
funcToWrap.__name__,
|
|
97
|
+
)
|
|
98
|
+
raise Exception(
|
|
99
|
+
"Deferring to a thread can only be done from the main thread"
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
deferred: Deferred[_R] = deferToThread(funcToWrap, *args, **kwargs)
|
|
103
|
+
deferred.addCallback(checkForInlineCallbackYield)
|
|
104
|
+
deferred.addErrback(
|
|
105
|
+
vortexLogFailure, logger, consumeError=consumeError
|
|
106
|
+
)
|
|
107
|
+
return deferred
|
|
108
|
+
|
|
109
|
+
return func
|
|
110
|
+
|
|
111
|
+
return wrapper
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def deferOntoReactor(callback: Callable[_P, _R]) -> Callable[_P, _R]:
|
|
115
|
+
"""Decorator to defer a callback onto the reactor via callLater(0, ...).
|
|
116
|
+
|
|
117
|
+
Usage::
|
|
118
|
+
|
|
119
|
+
@deferOntoReactor
|
|
120
|
+
def _logicOnTupleObservableUpdate(self, tuples: list) -> None:
|
|
121
|
+
...
|
|
122
|
+
|
|
123
|
+
The wrapped callable is scheduled with ``reactor.callLater(0, ...)``
|
|
124
|
+
instead of running inline at notify time, so tuple-observable
|
|
125
|
+
subscribe callbacks never execute re-entrantly inside the
|
|
126
|
+
observable's notification loop
|
|
127
|
+
(``coding_standards/py_tx_observable_calllater``).
|
|
128
|
+
|
|
129
|
+
The wrapper returns immediately with ``None``; the wrapped callable's
|
|
130
|
+
return value is discarded by the scheduled invocation, and the
|
|
131
|
+
``DelayedCall`` from ``callLater`` is not leaked to the caller.
|
|
132
|
+
|
|
133
|
+
:param callback: The callback to schedule onto the reactor.
|
|
134
|
+
:return: A wrapper that schedules ``callback`` via ``callLater(0, ...)``.
|
|
135
|
+
"""
|
|
136
|
+
|
|
137
|
+
@functools.wraps(callback)
|
|
138
|
+
def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> _R:
|
|
139
|
+
TypedReactor.get().callLater(0, callback, *args, **kwargs)
|
|
140
|
+
return cast(_R, None)
|
|
141
|
+
|
|
142
|
+
return wrapper
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"""Tests for the reactor-scheduling helpers in defer_util."""
|
|
2
|
+
|
|
3
|
+
import twisted.internet
|
|
4
|
+
from collections.abc import Callable
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
import pytest
|
|
8
|
+
from twisted.internet import task
|
|
9
|
+
|
|
10
|
+
from codie_util_twisted.defer_util import deferOntoReactor
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@pytest.fixture
|
|
14
|
+
def clock(monkeypatch: pytest.MonkeyPatch) -> task.Clock:
|
|
15
|
+
clock = task.Clock()
|
|
16
|
+
monkeypatch.setattr(twisted.internet, "reactor", clock)
|
|
17
|
+
return clock
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def test_deferOntoReactorDefersCallUntilReactorRuns(
|
|
21
|
+
clock: task.Clock,
|
|
22
|
+
) -> None:
|
|
23
|
+
calls: list[tuple[tuple[Any, ...], dict[str, Any]]] = []
|
|
24
|
+
|
|
25
|
+
@deferOntoReactor
|
|
26
|
+
def callback(*args: Any, **kwargs: Any) -> None:
|
|
27
|
+
calls.append((args, kwargs))
|
|
28
|
+
|
|
29
|
+
callback(1, key="value")
|
|
30
|
+
assert calls == []
|
|
31
|
+
|
|
32
|
+
clock.advance(0)
|
|
33
|
+
assert calls == [((1,), {"key": "value"})]
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def test_deferOntoReactorWrapperReturnsNoneNotDelayedCall(
|
|
37
|
+
clock: task.Clock,
|
|
38
|
+
) -> None:
|
|
39
|
+
wrapped: Callable[[], Any] = deferOntoReactor(lambda: "value")
|
|
40
|
+
|
|
41
|
+
assert wrapped() is None
|
|
42
|
+
|
|
43
|
+
clock.advance(0)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def test_deferOntoReactorRunsAfterPriorScheduledCalls(
|
|
47
|
+
clock: task.Clock,
|
|
48
|
+
) -> None:
|
|
49
|
+
events: list[str] = []
|
|
50
|
+
|
|
51
|
+
@deferOntoReactor
|
|
52
|
+
def callback() -> None:
|
|
53
|
+
events.append("wrapped")
|
|
54
|
+
|
|
55
|
+
clock.callLater(0, events.append, "prior")
|
|
56
|
+
callback()
|
|
57
|
+
assert events == []
|
|
58
|
+
|
|
59
|
+
clock.advance(0)
|
|
60
|
+
assert events == ["prior", "wrapped"]
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def test_deferOntoReactorPreservesInvocationOrder(
|
|
64
|
+
clock: task.Clock,
|
|
65
|
+
) -> None:
|
|
66
|
+
events: list[int] = []
|
|
67
|
+
|
|
68
|
+
@deferOntoReactor
|
|
69
|
+
def callback(value: int) -> None:
|
|
70
|
+
events.append(value)
|
|
71
|
+
|
|
72
|
+
callback(1)
|
|
73
|
+
callback(2)
|
|
74
|
+
assert events == []
|
|
75
|
+
|
|
76
|
+
clock.advance(0)
|
|
77
|
+
assert events == [1, 2]
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def test_deferOntoReactorPreservesFunctionMetadata() -> None:
|
|
81
|
+
@deferOntoReactor
|
|
82
|
+
def myCallback() -> None:
|
|
83
|
+
"""Callback docstring."""
|
|
84
|
+
|
|
85
|
+
assert myCallback.__name__ == "myCallback"
|
|
86
|
+
assert myCallback.__doc__ == "Callback docstring."
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"""Mypy plugin: validate @defer.inlineCallbacks return type annotations.
|
|
2
|
+
|
|
3
|
+
Checks that the inner return type T in Generator[Any, Any, T] is not itself
|
|
4
|
+
a Generator or Deferred — which would indicate a double-wrapping bug.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from typing import Callable
|
|
8
|
+
from mypy.plugin import Plugin, FunctionContext
|
|
9
|
+
from mypy.types import Type, get_proper_type, Instance, AnyType
|
|
10
|
+
|
|
11
|
+
_INLINE_CALLBACKS = {
|
|
12
|
+
"twisted.internet.defer.inlineCallbacks",
|
|
13
|
+
}
|
|
14
|
+
_FORBIDDEN_INNER = {
|
|
15
|
+
"builtins.generator",
|
|
16
|
+
"typing.Generator",
|
|
17
|
+
"twisted.internet.defer.Deferred",
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _check_inner_type(ctx: FunctionContext) -> Type:
|
|
22
|
+
ret = ctx.default_return_type
|
|
23
|
+
proper = get_proper_type(ret)
|
|
24
|
+
# ret is Callable[P, Deferred[T]] after the stub transforms it.
|
|
25
|
+
# We inspect the Deferred's type argument to validate T.
|
|
26
|
+
if not isinstance(proper, Instance):
|
|
27
|
+
return ret
|
|
28
|
+
if not proper.args:
|
|
29
|
+
return ret
|
|
30
|
+
# proper is Deferred[T]; proper.args[0] is T
|
|
31
|
+
inner = get_proper_type(proper.args[0])
|
|
32
|
+
if isinstance(inner, AnyType):
|
|
33
|
+
return ret
|
|
34
|
+
if isinstance(inner, Instance) and inner.type.fullname in _FORBIDDEN_INNER:
|
|
35
|
+
ctx.api.fail(
|
|
36
|
+
f"@inlineCallbacks return type must not be Generator or Deferred "
|
|
37
|
+
f"(got {inner.type.fullname}); use the inner value type directly",
|
|
38
|
+
ctx.context,
|
|
39
|
+
)
|
|
40
|
+
return ret
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class TwistedPlugin(Plugin):
|
|
44
|
+
def get_function_decorator_hook(
|
|
45
|
+
self, fullname: str
|
|
46
|
+
) -> Callable[[FunctionContext], Type] | None:
|
|
47
|
+
if fullname in _INLINE_CALLBACKS:
|
|
48
|
+
return _check_inner_type
|
|
49
|
+
return None
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def plugin(_version: str) -> type[TwistedPlugin]:
|
|
53
|
+
return TwistedPlugin
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
partial
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"""Typed accessor for the Twisted global reactor."""
|
|
2
|
+
|
|
3
|
+
from typing import Any, Protocol, cast
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class _Reactor(Protocol):
|
|
7
|
+
"""Subset of the Twisted reactor interface used across DigOps apps."""
|
|
8
|
+
|
|
9
|
+
def callLater(
|
|
10
|
+
self, _delay: float, _callable: Any, *args: Any, **kwargs: Any
|
|
11
|
+
) -> Any: ...
|
|
12
|
+
|
|
13
|
+
def callWhenRunning(
|
|
14
|
+
self, _callable: Any, *args: Any, **kwargs: Any
|
|
15
|
+
) -> Any: ...
|
|
16
|
+
|
|
17
|
+
def callFromThread(
|
|
18
|
+
self, _callable: Any, *args: Any, **kwargs: Any
|
|
19
|
+
) -> None: ...
|
|
20
|
+
|
|
21
|
+
def addSystemEventTrigger(
|
|
22
|
+
self,
|
|
23
|
+
_phase: str,
|
|
24
|
+
_eventType: str,
|
|
25
|
+
_callable: Any,
|
|
26
|
+
*args: Any,
|
|
27
|
+
**kwargs: Any,
|
|
28
|
+
) -> Any: ...
|
|
29
|
+
|
|
30
|
+
def iterate(self, _delay: float = 0) -> None: ...
|
|
31
|
+
|
|
32
|
+
def listenTCP(
|
|
33
|
+
self, _port: int, _factory: Any, *args: Any, **kwargs: Any
|
|
34
|
+
) -> Any: ...
|
|
35
|
+
|
|
36
|
+
def run(self, *args: Any, **kwargs: Any) -> None: ...
|
|
37
|
+
|
|
38
|
+
def stop(self) -> None: ...
|
|
39
|
+
|
|
40
|
+
def suggestThreadPoolSize(self, _size: int) -> None: ...
|
|
41
|
+
|
|
42
|
+
@property
|
|
43
|
+
def running(self) -> bool: ...
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class TypedReactor:
|
|
47
|
+
"""Class-based accessor for the installed Twisted reactor."""
|
|
48
|
+
|
|
49
|
+
DEFAULT_THREAD_POOL_MAX = 500
|
|
50
|
+
|
|
51
|
+
@classmethod
|
|
52
|
+
def get(cls) -> _Reactor:
|
|
53
|
+
from twisted.internet import reactor
|
|
54
|
+
|
|
55
|
+
return cast(_Reactor, reactor)
|
|
56
|
+
|
|
57
|
+
@classmethod
|
|
58
|
+
def logicSuggestThreadPoolSize(
|
|
59
|
+
cls, maxSize: int = DEFAULT_THREAD_POOL_MAX
|
|
60
|
+
) -> None:
|
|
61
|
+
"""Raise Twisted's deferToThread pool cap (default is 10)."""
|
|
62
|
+
cls.get().suggestThreadPoolSize(maxSize)
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
|
|
3
|
+
set -o nounset
|
|
4
|
+
set -o errexit
|
|
5
|
+
|
|
6
|
+
#------------------------------------------------------------------------------
|
|
7
|
+
# Configure package preferences here
|
|
8
|
+
PY_PACKAGE="codie_util_twisted"
|
|
9
|
+
|
|
10
|
+
# Leave blank not to publish
|
|
11
|
+
# Or select one of the index servers defined in ~/.pypirc
|
|
12
|
+
PYPI_PUBLISH="pypi"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
#------------------------------------------------------------------------------
|
|
16
|
+
PIP_PACKAGE=${PY_PACKAGE//_/-} # Replace _ with -
|
|
17
|
+
HAS_GIT=`ls -d .git 2> /dev/null`
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
if ! [ -f "pyproject.toml" ]; then
|
|
21
|
+
echo "setver.sh must be run in the directory where pyproject.toml is" >&2
|
|
22
|
+
exit 1
|
|
23
|
+
fi
|
|
24
|
+
|
|
25
|
+
VER="${1:?You must pass a version of the format 0.0.0 as the only argument}"
|
|
26
|
+
|
|
27
|
+
if [ $HAS_GIT ]; then
|
|
28
|
+
if [ -z "${CI_PROJECT_DIR:-}" ] && [ -n "$(git status --porcelain)" ]; then
|
|
29
|
+
echo "There are uncomitted changes, please make sure all changes are comitted" >&2
|
|
30
|
+
exit 1
|
|
31
|
+
fi
|
|
32
|
+
|
|
33
|
+
git fetch --all
|
|
34
|
+
if git tag | grep -q "${VER}"; then
|
|
35
|
+
echo "Git tag for version ${VER} already exists." >&2
|
|
36
|
+
exit 1
|
|
37
|
+
fi
|
|
38
|
+
fi
|
|
39
|
+
|
|
40
|
+
#------------------------------------------------------------------------------
|
|
41
|
+
echo "Setting version to $VER"
|
|
42
|
+
|
|
43
|
+
# Update the pyproject.toml
|
|
44
|
+
perl -pi -e "s;^version = \".*\";version = \"${VER}\";" pyproject.toml
|
|
45
|
+
|
|
46
|
+
# Update the package version
|
|
47
|
+
perl -pi -e "s;.*version.*;__version__ = '${VER}';" ${PY_PACKAGE}/__init__.py
|
|
48
|
+
|
|
49
|
+
# Reset the commit, we don't want versions in the commit
|
|
50
|
+
|
|
51
|
+
if [ $HAS_GIT ]; then
|
|
52
|
+
git commit -a -m "Updated to version ${VER}"
|
|
53
|
+
|
|
54
|
+
git tag ${VER}
|
|
55
|
+
git push
|
|
56
|
+
git push --tags
|
|
57
|
+
fi
|
|
58
|
+
|
|
59
|
+
#------------------------------------------------------------------------------
|
|
60
|
+
|
|
61
|
+
echo "Deleting old dist dir"
|
|
62
|
+
[ ! -d dist ] || rm -rf dist
|
|
63
|
+
|
|
64
|
+
echo "Building sdist and wheel"
|
|
65
|
+
python -m build
|
|
66
|
+
|
|
67
|
+
if [ -n "${PYPI_PUBLISH}" ]
|
|
68
|
+
then
|
|
69
|
+
echo "Pushing to pypi index server ${PYPI_PUBLISH}"
|
|
70
|
+
twine upload --repository "${PYPI_PUBLISH}" dist/*
|
|
71
|
+
else
|
|
72
|
+
echo "PYPI_PUBLISH is blank, skipping PyPI publish"
|
|
73
|
+
fi
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "codie-util-twisted"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
license = "MIT"
|
|
9
|
+
requires-python = ">=3.12"
|
|
10
|
+
dependencies = [
|
|
11
|
+
"twisted>=25.5.0,<26",
|
|
12
|
+
"mypy>=1.0",
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
[project.optional-dependencies]
|
|
16
|
+
dev = [
|
|
17
|
+
"pytest>=8.0",
|
|
18
|
+
"pytest-twisted>=1.14",
|
|
19
|
+
"taskipy>=1.13",
|
|
20
|
+
"ruff>=0.15",
|
|
21
|
+
"vulture>=2.14",
|
|
22
|
+
"basedpyright>=1.20",
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
[tool.hatch.build.targets.wheel]
|
|
26
|
+
packages = ["codie_util_twisted"]
|
|
27
|
+
|
|
28
|
+
[tool.pytest.ini_options]
|
|
29
|
+
testpaths = ["codie_util_twisted"]
|
|
30
|
+
python_files = ["*_test.py", "test_*.py"]
|
|
31
|
+
markers = [
|
|
32
|
+
"twisted: twisted/async tests via pytest-twisted",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
[tool.taskipy.tasks]
|
|
36
|
+
qa = "/Users/jchesney/dev-codie/opt/venv/bin/ruff check codie_util_twisted/ --fix && /Users/jchesney/dev-codie/opt/venv/bin/basedpyright codie_util_twisted/ && /Users/jchesney/dev-codie/opt/venv/bin/mypy codie_util_twisted/ && /Users/jchesney/dev-codie/opt/venv/bin/vulture codie_util_twisted/ --min-confidence 80"
|
|
37
|
+
test = "/Users/jchesney/dev-codie/opt/venv/bin/pytest codie_util_twisted/ -xvs"
|
|
38
|
+
|
|
39
|
+
[tool.ruff]
|
|
40
|
+
line-length = 80
|
|
41
|
+
|
|
42
|
+
[tool.ruff.lint]
|
|
43
|
+
select = ["E", "W", "F", "B", "C4", "RET", "SIM102", "TID251"]
|
|
44
|
+
ignore = ["E402", "E501", "SIM105", "C408"]
|
|
45
|
+
|
|
46
|
+
[tool.mypy]
|
|
47
|
+
python_version = "3.12"
|
|
48
|
+
mypy_path = ["stubs"]
|
|
49
|
+
plugins = ["codie_util_twisted.mypy_twisted_plugin"]
|
|
50
|
+
ignore_missing_imports = true
|
|
51
|
+
check_untyped_defs = true
|
|
52
|
+
disallow_untyped_defs = true
|
|
53
|
+
disable_error_code = ["misc", "arg-type"]
|
|
54
|
+
|
|
55
|
+
[tool.basedpyright]
|
|
56
|
+
pythonVersion = "3.12"
|
|
57
|
+
typeCheckingMode = "recommended"
|
|
58
|
+
failOnWarnings = false
|
|
59
|
+
venvPath = "../opt"
|
|
60
|
+
venv = "venv"
|
|
61
|
+
extraPaths = ["stubs"]
|
|
62
|
+
reportMissingTypeArgument = "warning"
|
|
63
|
+
reportReturnType = "warning"
|
|
64
|
+
reportInvalidTypeForm = "warning"
|
|
65
|
+
reportAssignmentType = "warning"
|
|
66
|
+
reportArgumentType = "warning"
|
|
67
|
+
reportImportCycles = "warning"
|
|
68
|
+
reportIncompatibleMethodOverride = "warning"
|
|
69
|
+
reportInvalidCast = "warning"
|
|
70
|
+
allowedUntypedLibraries = [
|
|
71
|
+
"twisted",
|
|
72
|
+
]
|
|
73
|
+
|
|
74
|
+
[tool.vulture]
|
|
75
|
+
paths = ["codie_util_twisted"]
|
|
76
|
+
min_confidence = 80
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
from typing import (
|
|
2
|
+
Any,
|
|
3
|
+
Callable,
|
|
4
|
+
Generator,
|
|
5
|
+
Generic,
|
|
6
|
+
TypeVar,
|
|
7
|
+
ParamSpec,
|
|
8
|
+
)
|
|
9
|
+
from typing import Iterator
|
|
10
|
+
|
|
11
|
+
_T = TypeVar("_T")
|
|
12
|
+
_T2 = TypeVar("_T2")
|
|
13
|
+
_P = ParamSpec("_P")
|
|
14
|
+
|
|
15
|
+
class Deferred(Generic[_T]):
|
|
16
|
+
result: Any
|
|
17
|
+
def addCallback(
|
|
18
|
+
self, callback: Callable[..., Any], *args: Any, **kwargs: Any
|
|
19
|
+
) -> "Deferred[Any]": ...
|
|
20
|
+
def addErrback(
|
|
21
|
+
self, errback: Callable[..., Any], *args: Any, **kwargs: Any
|
|
22
|
+
) -> "Deferred[Any]": ...
|
|
23
|
+
def addBoth(
|
|
24
|
+
self, callback: Callable[..., Any], *args: Any, **kwargs: Any
|
|
25
|
+
) -> "Deferred[Any]": ...
|
|
26
|
+
def addCallbacks(
|
|
27
|
+
self,
|
|
28
|
+
callback: Callable[..., Any],
|
|
29
|
+
errback: Callable[..., Any] | None = ...,
|
|
30
|
+
*args: Any,
|
|
31
|
+
**kwargs: Any,
|
|
32
|
+
) -> "Deferred[Any]": ...
|
|
33
|
+
def callback(self, result: _T) -> None: ...
|
|
34
|
+
def errback(self, fail: Any = ...) -> None: ...
|
|
35
|
+
def cancel(self) -> None: ...
|
|
36
|
+
def chainDeferred(self, d: "Deferred[_T]") -> "Deferred[None]": ...
|
|
37
|
+
def __iter__(self) -> Iterator[Any]: ...
|
|
38
|
+
@staticmethod
|
|
39
|
+
def succeed(result: _T) -> "Deferred[_T]": ...
|
|
40
|
+
@staticmethod
|
|
41
|
+
def fail(result: Any = ...) -> "Deferred[Any]": ...
|
|
42
|
+
@staticmethod
|
|
43
|
+
def fromCoroutine(coro: Any) -> "Deferred[Any]": ...
|
|
44
|
+
|
|
45
|
+
class DeferredList(Deferred[list[tuple[bool, Any]]]):
|
|
46
|
+
def __init__(
|
|
47
|
+
self,
|
|
48
|
+
deferredList: list[Deferred[Any]],
|
|
49
|
+
fireOnOneCallback: bool = ...,
|
|
50
|
+
fireOnOneErrback: bool = ...,
|
|
51
|
+
consumeErrors: bool = ...,
|
|
52
|
+
) -> None: ...
|
|
53
|
+
|
|
54
|
+
class DeferredSemaphore:
|
|
55
|
+
def __init__(self, tokens: int) -> None: ...
|
|
56
|
+
def acquire(self) -> Deferred[Any]: ...
|
|
57
|
+
def release(self) -> None: ...
|
|
58
|
+
def run(
|
|
59
|
+
self, f: Callable[..., Any], *args: Any, **kwargs: Any
|
|
60
|
+
) -> Deferred[Any]: ...
|
|
61
|
+
|
|
62
|
+
class CancelledError(Exception): ...
|
|
63
|
+
class FirstError(Exception): ...
|
|
64
|
+
|
|
65
|
+
def inlineCallbacks(
|
|
66
|
+
f: Callable[_P, Generator[Any, Any, _T]],
|
|
67
|
+
) -> Callable[_P, Deferred[_T]]: ...
|
|
68
|
+
def maybeDeferred(
|
|
69
|
+
f: Callable[..., Any], *args: Any, **kwargs: Any
|
|
70
|
+
) -> Deferred[Any]: ...
|
|
71
|
+
def succeed(result: _T) -> Deferred[_T]: ...
|
|
72
|
+
def fail(result: Any = ...) -> Deferred[Any]: ...
|
|
73
|
+
def gatherResults(
|
|
74
|
+
deferredList: list[Deferred[Any]], consumeErrors: bool = ...
|
|
75
|
+
) -> Deferred[list[Any]]: ...
|