hydrus/include/HydrusPubSub.py

136 lines
4.1 KiB
Python
Raw Normal View History

2013-07-10 20:25:57 +00:00
import HydrusConstants as HC
2013-02-19 00:11:43 +00:00
import Queue
import threading
import traceback
import weakref
import wx
import wx.lib.newevent
( PubSubEvent, EVT_PUBSUB ) = wx.lib.newevent.NewEvent()
2014-07-23 21:21:37 +00:00
class HydrusPubSub( object ):
2013-02-19 00:11:43 +00:00
def __init__( self ):
2013-11-13 21:30:38 +00:00
self._pubsubs = []
self._callables = []
2013-02-19 00:11:43 +00:00
self._lock = threading.Lock()
self._topics_to_objects = {}
self._topics_to_method_names = {}
2013-11-13 21:30:38 +00:00
def _GetCallables( self, topic ):
2013-02-19 00:11:43 +00:00
2013-11-13 21:30:38 +00:00
callables = []
if topic in self._topics_to_objects:
2013-02-19 00:11:43 +00:00
2013-11-13 21:30:38 +00:00
try:
2013-02-19 00:11:43 +00:00
2013-11-13 21:30:38 +00:00
objects = self._topics_to_objects[ topic ]
for object in objects:
2013-02-19 00:11:43 +00:00
2013-11-13 21:30:38 +00:00
method_names = self._topics_to_method_names[ topic ]
2013-02-19 00:11:43 +00:00
2013-11-13 21:30:38 +00:00
for method_name in method_names:
2013-02-19 00:11:43 +00:00
2013-11-13 21:30:38 +00:00
if hasattr( object, method_name ):
2013-02-19 00:11:43 +00:00
2013-11-13 21:30:38 +00:00
try:
callable = getattr( object, method_name )
2013-02-19 00:11:43 +00:00
2013-11-13 21:30:38 +00:00
callables.append( callable )
except wx.PyDeadObjectError: pass
except TypeError as e:
if '_wxPyDeadObject' not in str( e ): raise
2013-02-19 00:11:43 +00:00
2013-11-13 21:30:38 +00:00
except: pass
return callables
2014-06-18 21:53:48 +00:00
def NoJobsQueued( self ):
2013-11-13 21:30:38 +00:00
with self._lock:
return len( self._pubsubs ) == 0
2013-02-19 00:11:43 +00:00
2013-11-13 21:30:38 +00:00
def WXProcessQueueItem( self ):
# we don't want to map a topic to its callables until the previous topic's callables have been fully executed
2014-08-27 22:15:22 +00:00
# e.g. when we start a message with a pubsub, it'll take a while (in independant thread-time) for wx to create
2013-11-13 21:30:38 +00:00
# the dialog and hence map the new callable to the topic. this was leading to messages not being updated
# because the (short) processing thread finished and entirely pubsubbed before wx had a chance to boot the
# message.
do_callable = False
2013-02-19 00:11:43 +00:00
with self._lock:
2013-11-13 21:30:38 +00:00
if len( self._callables ) > 0:
2013-02-19 00:11:43 +00:00
2013-11-13 21:30:38 +00:00
( callable, args, kwargs ) = self._callables.pop( 0 )
do_callable = True
else:
( topic, args, kwargs ) = self._pubsubs.pop( 0 )
callables = self._GetCallables( topic )
self._callables = [ ( callable, args, kwargs ) for callable in callables ]
for i in range( len( self._callables ) ): wx.PostEvent( HC.app, PubSubEvent() )
# do this _outside_ the lock, lol
if do_callable: callable( *args, **kwargs )
def pub( self, topic, *args, **kwargs ):
with self._lock:
2014-08-06 20:29:17 +00:00
self._pubsubs.append( ( topic, args, kwargs ) )
wx.PostEvent( HC.app, PubSubEvent() )
2013-02-19 00:11:43 +00:00
def sub( self, object, method_name, topic ):
with self._lock:
if topic not in self._topics_to_objects: self._topics_to_objects[ topic ] = weakref.WeakSet()
if topic not in self._topics_to_method_names: self._topics_to_method_names[ topic ] = set()
self._topics_to_objects[ topic ].add( object )
self._topics_to_method_names[ topic ].add( method_name )
2013-11-13 21:30:38 +00:00
def WXpubimmediate( self, topic, *args, **kwargs ):
with self._lock:
2014-06-18 21:53:48 +00:00
2013-11-13 21:30:38 +00:00
callables = self._GetCallables( topic )
for callable in callables: callable( *args, **kwargs )
2013-02-19 00:11:43 +00:00