Skip to content

Commit

Permalink
added qt helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnnsrs committed Mar 9, 2023
1 parent 5754a97 commit d20c2ac
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
75 changes: 73 additions & 2 deletions koil/qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

from qtpy import QtCore, QtWidgets
from typing_extensions import ParamSpec

from functools import partial
from koil.koil import Koil, KoilMixin
from koil.task import KoilFuture, KoilGeneratorRunner, KoilRunner, KoilYieldFuture
from koil.utils import (
iterate_threaded_with_context_and_signals,
run_threaded_with_context_and_signals,
)
from koil.vars import current_loop

import uuid
logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -154,6 +154,77 @@ async def acall(self, *args: P.args, timeout=None, **kwargs: P.kwargs):
raise


class QtListener():

def __init__(self,loop, queue) -> None:
self.queue = queue
self.loop = loop

def __call__(self, *args):
self.loop.call_soon_threadsafe(self.queue.put_nowait, args)


class Iterator():

def __init__(self,queue, timeout=None) -> None:
self.queue = queue
self.timeout = timeout


def __anext__(self):
return self.next()


class QtSignal(QtCore.QObject, Generic[T, P]):

def __init__(
self,

signal: QtCore.Signal,
*args,
use_context=True,
**kwargs,
):
super().__init__(*args, **kwargs)
self.signal = signal
self.signal.connect(self.on_called)
self.listeners = {}
self.use_context = use_context
self._attached = None

def on_called(self, *returns):
for listener in self.listeners.values():
listener(*returns)


async def aiterate(self, timeout=None):
unique_id = uuid.uuid4().hex
loop = asyncio.get_event_loop()
queue = asyncio.Queue()
listener = self.listeners[unique_id] = QtListener(loop, queue)

try:
while True:
z = await asyncio.wait_for(listener.queue.get(), timeout=timeout)
if len(z) == 1:
z = z[0]
yield z

except Exception as e:
del self.listeners[unique_id]
raise e

except asyncio.CancelledError:
del self.listeners[unique_id]
raise

async def aonce(self, timeout=None):
async for i in self.aiterate(timeout=timeout):
return i




class QtRunner(KoilRunner, QtCore.QObject):
started = QtCore.Signal()
errored = QtCore.Signal(Exception)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "koil"
version = "0.2.11"
version = "0.2.12"
readme = "README.md"
description = "Async for a sync world"
authors = ["jhnnsrs <jhnnsrs@gmail.com>"]
Expand Down

0 comments on commit d20c2ac

Please sign in to comment.