Example #1
0
    def test_conditional_event(self):
        mgr = EventManager()

        self.handled = False

        def handler(event):
            self.handled = True

        binding = mgr.bind(handler, 'basic', condition=1)
        self.assertEqual(
            binding.__class__, Binding,
            'EventManager().bind() returned something other than an instance of Binding'
        )

        mgr.trigger(Event('basic'))
        self.assertFalse(
            self.handled,
            'Conditional event handler called for the wrong event')

        mgr.trigger(Event('basic', [('condition', 0)]))
        self.assertFalse(
            self.handled,
            'Conditional event handler called for the wrong event')

        mgr.trigger(Event('basic', [('condition', 1)]))
        self.assertTrue(self.handled, 'Conditional event handler not called')
Example #2
0
    def test_basic_event(self):
        # Test a basic event

        # This should not call evt_handler
        self.events.trigger(Event('something'))
        self.assertFalse(self.reactor.handled,
                         'Basic event handler called for the wrong event')

        #This should do the trick
        self.events.trigger(Event('basic'))
        self.assertTrue(self.reactor.handled, 'Basic event handler not called')

        self.reactor.reset()
Example #3
0
    def test_decorated_event(self):
        # Test a decorated event

        # Should be false.
        self.events.trigger(Event('something'))
        self.assertFalse(self.reactor.handled,
                         'Event handler called for the wrong event')

        # Shoud be true
        self.events.trigger(Event('decorated'))
        self.assertTrue(self.reactor.handled,
                        'Decorated event handler not called')

        self.reactor.reset()
Example #4
0
    def test_conditional_event(self):
        # Test a conditional event

        # Should be false.
        self.events.trigger(Event('conditional', [('condition', 0)]))
        self.assertFalse(
            self.reactor.handled,
            'Conditional event handler called for the wrong event')

        # Shoud be true
        self.events.trigger(Event('conditional', [('condition', 1)]))
        self.assertTrue(self.reactor.handled,
                        'Conditional event handler not called')

        self.reactor.reset()
Example #5
0
    def runTest(self):

        sys.stdout.write('\n')

        # Load an event system
        events = EventManager(
        )  #stddebug=lambda n: sys.stdout.write('{0}\n'.format(n)))

        # Load rulesets
        rulesets = RulesetBattery(
        )  #stddebug=lambda n: sys.stdout.write('{0}\n'.format(n)))
        rulesets.load_objects(events, rules)

        self.assertIsNotNone(events.rules.get('example', None),
                             'Failed to load test ruleset')

        # Load plugins!
        plugins = ReactorBattery(
        )  #stddebug=lambda n: sys.stdout.write('{0}\n'.format(n)))
        plugins.load_objects(events, reactors, 'Plugin')

        self.assertIsNotNone(events.map.get('example', None),
                             'Event bindings failed')

        events.rules['example'].reset_called()

        # Fire an event!
        events.trigger(Event('example'))

        # Test Results!
        self.assertTrue(events.rules['example'].called,
                        'Example ruleset not called properly')

        self.assertTrue(plugins.loaded['Example'].full_test,
                        'Example reactor not called properly')
Example #6
0
 def check_command(self, event, dAmn):
     # Check if the message starts with the trigger!
     if event.message[:len(self.btrig)] != self.btrig:
         return
     # Manipulate event data!
     msg = event.message[len(self.btrig):]
     args = msg.split(' ')
     # Create an event object!
     cmd = Event('command', [('cmd', args[0]), ('user', event.user),
                             ('ns', event.ns), ('message', msg),
                             ('args', args), ('raw', event.raw)])
     # Trigger the event!
     self.trigger(cmd, dAmn)
Example #7
0
    def test_basic_decorator_event(self):
        mgr = EventManager()
        handles = mgr.handler

        self.handled = False

        @handles('basic')
        def handler(event):
            self.handled = True

        self.assertTrue(hasattr(handler, 'binding'),
                        'Decorator did not apply a binding to the handler')

        self.assertEqual(
            handler.binding.__class__, Binding,
            'Decorator applied something other than an instance of Binding')

        mgr.trigger(Event('something'))
        self.assertFalse(self.handled,
                         'Decorated event handler called for the wrong event')

        mgr.trigger(Event('basic'))
        self.assertTrue(self.handled, 'Decorated event handler not called')
Example #8
0
 def __inst__(self, un, pw, admin, trigger, autojoin, events):
     # Store everything.
     self.events_ = events
     self.reactors = []
     # Stuff to do with the client
     self.user.username = un
     self.user.password = pw
     self._admin = admin
     self._trigger = trigger
     self.autojoin = autojoin
     # Make sure the parent gets its fair share.
     ReconnectingClient.__inst__(self)
     # Load reactors!
     self.load_reactors()
     # Tell the reactors we are ready.
     self.events_.trigger(Event('ready'), self)
Example #9
0
 def e_ready(self, event):
     sys.stdout.write('<< Reflexing! Provoking other reactors!\n')
     self.trigger(Event('provoke', [('source', self.name)]))
Example #10
0
 def pkt_generic(self, data):
     self.events_.trigger(Event(data['event'], data['rules']), self)
Example #11
0
 def test_run(self):
     self.rules.reset_called()
     self.events.trigger(Event('example'))
     self.assertTrue(self.rules.called,
         'Event runner called on the wrong object')
Example #12
0
 def pkt_generic(self, event):
     self._events.trigger(Event(event.name, event.arguments.items()), self)
Example #13
0
from reflex.data import Event
from reflex.control import EventManager
from reflex.interfaces import Reactor


class example(Reactor):

    name = 'example'

    def init(self):
        self.bind(self.event_handler, 'basic')

    def event_handler(self, event, *args):
        print("Hello, world!")


events = EventManager()
obj = example(events)
events.trigger(Event('basic'))
# Prints "Hello, world!"
Example #14
0
from reflex.data import Event
from reflex.control import EventManager
from reflex.interfaces import Reactor

# Create an event manager
events = EventManager()


# Create an event handler
@events.handler('basic')
def example(event, *args):
    print("Hello, world!")


# Trigger the event.
events.trigger(Event('basic'))


# Odd test! Gets funky beyong this point.
class exampler(Reactor):

    name = 'Funky decorator hack'

    def init(self):
        @self.handler('funky')
        def handler(event, *args):
            print(self.name)


exampler(events)
events.trigger(Event('funky'))
Example #15
0
from reflex.data import Event
from reflex.control import EventManager
from reflex.interfaces import Reactor

class example(Reactor):
    
    name = 'example'
    
    def init(self):
        self.bind(self.handler, 'basic', source='main')
        self.bind(self.use_args, 'args')
    
    def handler(self, event, *args):
        print("Hello, world!")
    
    def use_args(self, event, *args):
        print("Event triggered by {0} since because {1}.".format(event.source, args[0]))
    
events = EventManager()
obj = example(events)

# The following event trigger is not handled by anything.
events.trigger(Event('basic'))
# The next event trigger is handled by the handler method.
events.trigger(Event('basic', [('source', 'main')]))
# This one is yes.
events.trigger(Event('args', [('source', 'main')]), 'pickles')
Example #16
0
 def reload(self, event=None, *args):
     self.events.trigger(Event('reloading'))
     self.events.clear_bindings()
     self.reactors = []
     self.load()
     self.events.trigger(Event('reloaded'))
Example #17
0
 def load(self):
     self.reactors = []
     self.reactors.append(example(self.events))
     self.reactors.append(example2(self.events))
     self.events.bind('main app', self.reload, 'reload')
     self.events.trigger(Event('ready'))
Example #18
0
 def __init__(self):
     self.events = EventManager()
     self.load()
     self.events.trigger(Event('reload'))
Example #19
0
from reflex.data import Event
from reflex.control import EventManager

events = EventManager()


def handler(event, *args):
    print("Hello, world!")


events.bind(handler, 'example')

events.trigger(Event('example'))
# prints "Hello, world!"