Skip to content

Commit

Permalink
cleaned up again
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmancuso committed Sep 29, 2018
1 parent fff391d commit 188edf2
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions events/Event.py
Expand Up @@ -2,52 +2,54 @@

from events.exceptions import InvalidSubscriptionType


class Event:

listeners = {}
_fired_events = {}
_arguments = {}

def __init__(self, container):
"""Event contructor
Arguments:
container {masonite.app.App} -- The Masonite container class
"""

self.container = container

def event(self, event):
"""Starts the event observer
Arguments:
event {string|object}
"""

self.listen(event, [])

def listen(self, event, listeners = []):
def listen(self, event, listeners=[]):
"""Add a listener to an event.
Arguments:
event {string|object} -- [description]
Keyword Arguments:
listeners {list} -- A list of listner classes that should listen to specific events (default: {[]})
Returns:
self
"""

if event in self.listeners:
self.listeners[event] += listeners
return self

self.listeners.update({event: listeners})
return self

def fire(self, events, **keywords):
"""Fire an event which fires all the listeners attached to that event.
Arguments:
events {string|object} -- The event to fire
"""
Expand All @@ -59,13 +61,13 @@ def fire(self, events, **keywords):
for listener in listener_events:
search = events.split('*')
if events.endswith('*') and event_action.startswith(search[0]) \
or events.startswith('*') and event_action.endswith(search[1]) \
or event_action.startswith(search[0]) and event_action.endswith(search[1]):
event = self.container.resolve(listener)
fired_listeners[event_action].append(event)
for key, value in keywords.items():
event._arguments.update({key: value})
self.container.resolve(event.handle)
or events.startswith('*') and event_action.endswith(search[1]) \
or event_action.startswith(search[0]) and event_action.endswith(search[1]):
event = self.container.resolve(listener)
fired_listeners[event_action].append(event)
for key, value in keywords.items():
event._arguments.update({key: value})
self.container.resolve(event.handle)
else:
fired_listeners.update({events: []})
for event in self.listeners[events]:
Expand All @@ -76,14 +78,14 @@ def fire(self, events, **keywords):
self.container.resolve(event.handle)

self._fired_events = self.clear_blank_fired_events(fired_listeners)

def clear_blank_fired_events(self, fired_listeners):
"""Just an internal cleaner helper that cleans any the listeners for any fired events.
This will, in effect, return the fired events
Arguments:
fired_listeners {dict} -- dictionary of event and listeners
Returns:
dict -- returns a dictionary of fired events
"""
Expand All @@ -97,23 +99,24 @@ def clear_blank_fired_events(self, fired_listeners):

def subscribe(self, *listeners):
"""Subscribe a specific listener object to the events system
Raises:
InvalidSubscriptionType -- raises when the subscribe attribute on the listener object is not a class.
"""

for listener in listeners:
if not isinstance(listener.subscribe, list):
raise InvalidSubscriptionType("'subscribe' attribute on {0} class must be a list".format(listener.__name__))
raise InvalidSubscriptionType(
"'subscribe' attribute on {0} class must be a list".format(listener.__name__))
for action in listener.subscribe:
self.listen(action, [listener])

def argument(self, argument):
"""Takes the argument and eventually stores the argument on the event class
Arguments:
argument {string|obj|list|dict} -- Any data type that can be a class attribute
Returns:
dict
"""
Expand Down

0 comments on commit 188edf2

Please sign in to comment.