venv added, updated

This commit is contained in:
Norbert
2024-09-13 09:46:28 +02:00
parent 577596d9f3
commit 82af8c809a
4812 changed files with 640223 additions and 2 deletions

View File

@@ -0,0 +1,13 @@
from .gtkscheduler import GtkScheduler
from .pygamescheduler import PyGameScheduler
from .qtscheduler import QtScheduler
from .tkinterscheduler import TkinterScheduler
from .wxscheduler import WxScheduler
__all__ = [
"GtkScheduler",
"PyGameScheduler",
"QtScheduler",
"TkinterScheduler",
"WxScheduler",
]

View File

@@ -0,0 +1,144 @@
from typing import Any, Optional, TypeVar, cast
from reactivex import abc, typing
from reactivex.disposable import (
CompositeDisposable,
Disposable,
SingleAssignmentDisposable,
)
from ..periodicscheduler import PeriodicScheduler
_TState = TypeVar("_TState")
class GtkScheduler(PeriodicScheduler):
"""A scheduler that schedules work via the GLib main loop
used in GTK+ applications.
See https://wiki.gnome.org/Projects/PyGObject
"""
def __init__(self, glib: Any) -> None:
"""Create a new GtkScheduler.
Args:
glib: The GLib module to use; typically, you would get this by
>>> import gi
>>> gi.require_version('Gtk', '3.0')
>>> from gi.repository import GLib
"""
super().__init__()
self._glib = glib
def _gtk_schedule(
self,
time: typing.AbsoluteOrRelativeTime,
action: typing.ScheduledSingleOrPeriodicAction[_TState],
state: Optional[_TState] = None,
periodic: bool = False,
) -> abc.DisposableBase:
msecs = max(0, int(self.to_seconds(time) * 1000.0))
sad = SingleAssignmentDisposable()
stopped = False
def timer_handler(_: Any) -> bool:
if stopped:
return False
nonlocal state
if periodic:
state = cast(typing.ScheduledPeriodicAction[_TState], action)(state)
else:
sad.disposable = self.invoke_action(
cast(typing.ScheduledAction[_TState], action), state=state
)
return periodic
self._glib.timeout_add(msecs, timer_handler, None)
def dispose() -> None:
nonlocal stopped
stopped = True
return CompositeDisposable(sad, Disposable(dispose))
def schedule(
self, action: typing.ScheduledAction[_TState], state: Optional[_TState] = None
) -> abc.DisposableBase:
"""Schedules an action to be executed.
Args:
action: Action to be executed.
state: [Optional] state to be given to the action function.
Returns:
The disposable object used to cancel the scheduled action
(best effort).
"""
return self._gtk_schedule(0.0, action, state)
def schedule_relative(
self,
duetime: typing.RelativeTime,
action: typing.ScheduledAction[_TState],
state: Optional[_TState] = None,
) -> abc.DisposableBase:
"""Schedules an action to be executed after duetime.
Args:
duetime: Relative time after which to execute the action.
action: Action to be executed.
state: [Optional] state to be given to the action function.
Returns:
The disposable object used to cancel the scheduled action
(best effort).
"""
return self._gtk_schedule(duetime, action, state=state)
def schedule_absolute(
self,
duetime: typing.AbsoluteTime,
action: typing.ScheduledAction[_TState],
state: Optional[_TState] = None,
) -> abc.DisposableBase:
"""Schedules an action to be executed at duetime.
Args:
duetime: Absolute time at which to execute the action.
action: Action to be executed.
state: [Optional] state to be given to the action function.
Returns:
The disposable object used to cancel the scheduled action
(best effort).
"""
duetime = self.to_datetime(duetime)
return self._gtk_schedule(duetime - self.now, action, state=state)
def schedule_periodic(
self,
period: typing.RelativeTime,
action: typing.ScheduledPeriodicAction[_TState],
state: Optional[_TState] = None,
) -> abc.DisposableBase:
"""Schedules a periodic piece of work to be executed in the loop.
Args:
period: Period in seconds for running the work repeatedly.
action: Action to be executed.
state: [Optional] state to be given to the action function.
Returns:
The disposable object used to cancel the scheduled action
(best effort).
"""
return self._gtk_schedule(period, action, state=state, periodic=True)

View File

@@ -0,0 +1,113 @@
import logging
import threading
from typing import Any, Optional, TypeVar
from reactivex import abc, typing
from reactivex.internal import PriorityQueue
from reactivex.internal.constants import DELTA_ZERO
from ..periodicscheduler import PeriodicScheduler
from ..scheduleditem import ScheduledItem
_TState = TypeVar("_TState")
log = logging.getLogger("Rx")
class PyGameScheduler(PeriodicScheduler):
"""A scheduler that schedules works for PyGame.
Note that this class expects the caller to invoke run() repeatedly.
http://www.pygame.org/docs/ref/time.html
http://www.pygame.org/docs/ref/event.html"""
def __init__(self, pygame: Any):
"""Create a new PyGameScheduler.
Args:
pygame: The PyGame module to use; typically, you would get this by
import pygame
"""
super().__init__()
self._pygame = pygame # TODO not used, refactor to actually use pygame?
self._lock = threading.Lock()
self._queue: PriorityQueue[ScheduledItem] = PriorityQueue()
def schedule(
self, action: typing.ScheduledAction[_TState], state: Optional[_TState] = None
) -> abc.DisposableBase:
"""Schedules an action to be executed.
Args:
action: Action to be executed.
state: [Optional] state to be given to the action function.
Returns:
The disposable object used to cancel the scheduled action
(best effort).
"""
log.debug("PyGameScheduler.schedule(state=%s)", state)
return self.schedule_absolute(self.now, action, state=state)
def schedule_relative(
self,
duetime: typing.RelativeTime,
action: typing.ScheduledAction[_TState],
state: Optional[_TState] = None,
) -> abc.DisposableBase:
"""Schedules an action to be executed after duetime.
Args:
duetime: Relative time after which to execute the action.
action: Action to be executed.
state: [Optional] state to be given to the action function.
Returns:
The disposable object used to cancel the scheduled action
(best effort).
"""
duetime = max(DELTA_ZERO, self.to_timedelta(duetime))
return self.schedule_absolute(self.now + duetime, action, state=state)
def schedule_absolute(
self,
duetime: typing.AbsoluteTime,
action: typing.ScheduledAction[_TState],
state: Optional[_TState] = None,
) -> abc.DisposableBase:
"""Schedules an action to be executed at duetime.
Args:
duetime: Absolute time at which to execute the action.
action: Action to be executed.
state: [Optional] state to be given to the action function.
Returns:
The disposable object used to cancel the scheduled action
(best effort).
"""
duetime = self.to_datetime(duetime)
si: ScheduledItem = ScheduledItem(self, state, action, duetime)
with self._lock:
self._queue.enqueue(si)
return si.disposable
def run(self) -> None:
while self._queue:
with self._lock:
item: ScheduledItem = self._queue.peek()
diff = item.duetime - self.now
if diff > DELTA_ZERO:
break
item = self._queue.dequeue()
if not item.is_cancelled():
item.invoke()

View File

@@ -0,0 +1,143 @@
import logging
from datetime import timedelta
from typing import Any, Optional, Set, TypeVar
from reactivex import abc, typing
from reactivex.disposable import (
CompositeDisposable,
Disposable,
SingleAssignmentDisposable,
)
from ..periodicscheduler import PeriodicScheduler
_TState = TypeVar("_TState")
log = logging.getLogger(__name__)
class QtScheduler(PeriodicScheduler):
"""A scheduler for a PyQt5/PySide2 event loop."""
def __init__(self, qtcore: Any):
"""Create a new QtScheduler.
Args:
qtcore: The QtCore instance to use; typically you would get this by
either import PyQt5.QtCore or import PySide2.QtCore
"""
super().__init__()
self._qtcore = qtcore
self._periodic_timers: Set[Any] = set()
def schedule(
self, action: typing.ScheduledAction[_TState], state: Optional[_TState] = None
) -> abc.DisposableBase:
"""Schedules an action to be executed.
Args:
action: Action to be executed.
state: [Optional] state to be given to the action function.
Returns:
The disposable object used to cancel the scheduled action
(best effort).
"""
return self.schedule_relative(0.0, action, state=state)
def schedule_relative(
self,
duetime: typing.RelativeTime,
action: typing.ScheduledAction[_TState],
state: Optional[_TState] = None,
) -> abc.DisposableBase:
"""Schedules an action to be executed after duetime.
Args:
duetime: Relative time after which to execute the action.
action: Action to be executed.
state: [Optional] state to be given to the action function.
Returns:
The disposable object used to cancel the scheduled action
(best effort).
"""
msecs = max(0, int(self.to_seconds(duetime) * 1000.0))
sad = SingleAssignmentDisposable()
is_disposed = False
def invoke_action() -> None:
if not is_disposed:
sad.disposable = action(self, state)
log.debug("relative timeout: %sms", msecs)
# Use static method, let Qt C++ handle QTimer lifetime
self._qtcore.QTimer.singleShot(msecs, invoke_action)
def dispose() -> None:
nonlocal is_disposed
is_disposed = True
return CompositeDisposable(sad, Disposable(dispose))
def schedule_absolute(
self,
duetime: typing.AbsoluteTime,
action: typing.ScheduledAction[_TState],
state: Optional[_TState] = None,
) -> abc.DisposableBase:
"""Schedules an action to be executed at duetime.
Args:
duetime: Absolute time at which to execute the action.
action: Action to be executed.
state: [Optional] state to be given to the action function.
Returns:
The disposable object used to cancel the scheduled action
(best effort).
"""
delta: timedelta = self.to_datetime(duetime) - self.now
return self.schedule_relative(delta, action, state=state)
def schedule_periodic(
self,
period: typing.RelativeTime,
action: typing.ScheduledPeriodicAction[_TState],
state: Optional[_TState] = None,
) -> abc.DisposableBase:
"""Schedules a periodic piece of work to be executed in the loop.
Args:
period: Period in seconds for running the work repeatedly.
action: Action to be executed.
state: [Optional] state to be given to the action function.
Returns:
The disposable object used to cancel the scheduled action
(best effort).
"""
msecs = max(0, int(self.to_seconds(period) * 1000.0))
sad = SingleAssignmentDisposable()
def interval() -> None:
nonlocal state
state = action(state)
log.debug("periodic timeout: %sms", msecs)
timer = self._qtcore.QTimer()
timer.setSingleShot(not period)
timer.timeout.connect(interval)
timer.setInterval(msecs)
self._periodic_timers.add(timer)
timer.start()
def dispose() -> None:
timer.stop()
self._periodic_timers.remove(timer)
timer.deleteLater()
return CompositeDisposable(sad, Disposable(dispose))

View File

@@ -0,0 +1,98 @@
from typing import Any, Optional, TypeVar
from reactivex import abc, typing
from reactivex.disposable import (
CompositeDisposable,
Disposable,
SingleAssignmentDisposable,
)
from ..periodicscheduler import PeriodicScheduler
_TState = TypeVar("_TState")
class TkinterScheduler(PeriodicScheduler):
"""A scheduler that schedules work via the Tkinter main event loop.
http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/universal.html
http://effbot.org/tkinterbook/widget.htm"""
def __init__(self, root: Any) -> None:
"""Create a new TkinterScheduler.
Args:
root: The Tk instance to use; typically, you would get this by
import tkinter; tkinter.Tk()
"""
super().__init__()
self._root = root
def schedule(
self, action: typing.ScheduledAction[_TState], state: Optional[_TState] = None
) -> abc.DisposableBase:
"""Schedules an action to be executed.
Args:
action: Action to be executed.
state: [Optional] state to be given to the action function.
Returns:
The disposable object used to cancel the scheduled action
(best effort).
"""
return self.schedule_relative(0.0, action, state)
def schedule_relative(
self,
duetime: typing.RelativeTime,
action: typing.ScheduledAction[_TState],
state: Optional[_TState] = None,
) -> abc.DisposableBase:
"""Schedules an action to be executed after duetime.
Args:
duetime: Relative time after which to execute the action.
action: Action to be executed.
state: [Optional] state to be given to the action function.
Returns:
The disposable object used to cancel the scheduled action
(best effort).
"""
sad = SingleAssignmentDisposable()
def invoke_action() -> None:
sad.disposable = self.invoke_action(action, state=state)
msecs = max(0, int(self.to_seconds(duetime) * 1000.0))
timer = self._root.after(msecs, invoke_action)
def dispose() -> None:
self._root.after_cancel(timer)
return CompositeDisposable(sad, Disposable(dispose))
def schedule_absolute(
self,
duetime: typing.AbsoluteTime,
action: typing.ScheduledAction[_TState],
state: Optional[_TState] = None,
) -> abc.DisposableBase:
"""Schedules an action to be executed at duetime.
Args:
duetime: Absolute time at which to execute the action.
action: Action to be executed.
state: [Optional] state to be given to the action function.
Returns:
The disposable object used to cancel the scheduled action
(best effort).
"""
duetime = self.to_datetime(duetime)
return self.schedule_relative(duetime - self.now, action, state=state)

View File

@@ -0,0 +1,177 @@
import logging
from typing import Any, Optional, Set, TypeVar, cast
from reactivex import abc, typing
from reactivex.disposable import (
CompositeDisposable,
Disposable,
SingleAssignmentDisposable,
)
from ..periodicscheduler import PeriodicScheduler
_TState = TypeVar("_TState")
log = logging.getLogger("Rx")
class WxScheduler(PeriodicScheduler):
"""A scheduler for a wxPython event loop."""
def __init__(self, wx: Any) -> None:
"""Create a new WxScheduler.
Args:
wx: The wx module to use; typically, you would get this by
import wx
"""
super().__init__()
self._wx = wx
timer_class: Any = self._wx.Timer
class Timer(timer_class):
def __init__(self, callback: typing.Action) -> None:
super().__init__() # type: ignore
self.callback = callback
def Notify(self) -> None:
self.callback()
self._timer_class = Timer
self._timers: Set[Timer] = set()
def cancel_all(self) -> None:
"""Cancel all scheduled actions.
Should be called when destroying wx controls to prevent
accessing dead wx objects in actions that might be pending.
"""
for timer in self._timers:
timer.Stop() # type: ignore
def _wxtimer_schedule(
self,
time: typing.AbsoluteOrRelativeTime,
action: typing.ScheduledSingleOrPeriodicAction[_TState],
state: Optional[_TState] = None,
periodic: bool = False,
) -> abc.DisposableBase:
scheduler = self
sad = SingleAssignmentDisposable()
def interval() -> None:
nonlocal state
if periodic:
state = cast(typing.ScheduledPeriodicAction[_TState], action)(state)
else:
sad.disposable = cast(typing.ScheduledAction[_TState], action)(
scheduler, state
)
msecs = max(1, int(self.to_seconds(time) * 1000.0)) # Must be non-zero
log.debug("timeout wx: %s", msecs)
timer = self._timer_class(interval)
# A timer can only be used from the main thread
if self._wx.IsMainThread():
timer.Start(msecs, oneShot=not periodic) # type: ignore
else:
self._wx.CallAfter(timer.Start, msecs, oneShot=not periodic) # type: ignore
self._timers.add(timer)
def dispose() -> None:
timer.Stop() # type: ignore
self._timers.remove(timer)
return CompositeDisposable(sad, Disposable(dispose))
def schedule(
self, action: typing.ScheduledAction[_TState], state: Optional[_TState] = None
) -> abc.DisposableBase:
"""Schedules an action to be executed.
Args:
action: Action to be executed.
state: [Optional] state to be given to the action function.
Returns:
The disposable object used to cancel the scheduled action
(best effort).
"""
sad = SingleAssignmentDisposable()
is_disposed = False
def invoke_action() -> None:
if not is_disposed:
sad.disposable = action(self, state)
self._wx.CallAfter(invoke_action)
def dispose() -> None:
nonlocal is_disposed
is_disposed = True
return CompositeDisposable(sad, Disposable(dispose))
def schedule_relative(
self,
duetime: typing.RelativeTime,
action: typing.ScheduledAction[_TState],
state: Optional[_TState] = None,
) -> abc.DisposableBase:
"""Schedules an action to be executed after duetime.
Args:
duetime: Relative time after which to execute the action.
action: Action to be executed.
state: [Optional] state to be given to the action function.
Returns:
The disposable object used to cancel the scheduled action
(best effort).
"""
return self._wxtimer_schedule(duetime, action, state=state)
def schedule_absolute(
self,
duetime: typing.AbsoluteTime,
action: typing.ScheduledAction[_TState],
state: Optional[_TState] = None,
) -> abc.DisposableBase:
"""Schedules an action to be executed at duetime.
Args:
duetime: Absolute time at which to execute the action.
action: Action to be executed.
state: [Optional] state to be given to the action function.
Returns:
The disposable object used to cancel the scheduled action
(best effort).
"""
duetime = self.to_datetime(duetime)
return self._wxtimer_schedule(duetime - self.now, action, state=state)
def schedule_periodic(
self,
period: typing.RelativeTime,
action: typing.ScheduledPeriodicAction[_TState],
state: Optional[_TState] = None,
) -> abc.DisposableBase:
"""Schedules a periodic piece of work to be executed in the loop.
Args:
period: Period in seconds for running the work repeatedly.
action: Action to be executed.
state: [Optional] state to be given to the action function.
Returns:
The disposable object used to cancel the scheduled action
(best effort).
"""
return self._wxtimer_schedule(period, action, state=state, periodic=True)