Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added arguments in the callback with bind #27

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
40 changes: 21 additions & 19 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
language: python
python:
- "2.6"
- "2.7"
# Disable testing 3.2 since autobahn uses unicode literal syntax that wasn't
# re-added into 3.3. See PEP 414
# - "3.2"
- "3.3"
- "3.4"
install:
- pip install .
# Our test server uses autobahn
- pip install autobahn
# For 2.x, our server needs twisted
- pip install twisted
# For 3.x where x < 4, our server needs trollius
- pip install trollius
script: python tests/test_pusherclient.py

language: python
python:
# Disable testing 2.6 since twisted has Python 2.7 or higher as a dependency.
# https://twistedmatrix.com/pipermail/twisted-python/2015-March/029270.html
# - "2.6"
- "2.7"
# Disable testing 3.2 since autobahn uses unicode literal syntax that wasn't
# re-added into 3.3. See PEP 414
# - "3.2"
- "3.3"
- "3.4"
install:
- pip install .
# Our test server uses autobahn
- pip install autobahn
# For 2.x, our server needs twisted
- pip install twisted
# For 3.x where x < 4, our server needs trollius
- pip install trollius
script: python tests/test_pusherclient.py

25 changes: 21 additions & 4 deletions pusherclient/channel.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
try:
import simplejson as json
except ImportError:
import json

class Channel(object):
def __init__(self, channel_name, connection):
self.name = channel_name
Expand All @@ -6,21 +11,30 @@ def __init__(self, channel_name, connection):

self.event_callbacks = {}

def bind(self, event_name, callback):
def bind(self, event_name, callback, kwargs={}, decode_json=False):
"""Bind an event to a callback

:param event_name: The name of the event to bind to.
:type event_name: str

:param callback: The callback to notify of this event.

:param kwargs: The keyword arguments to pass to the callback.
:type kwargs: dict

:param decode_json: Boolean that determines whether json messages will
be sent to the callback in decoded form
:type decode_json: boolean
"""
if event_name not in self.event_callbacks.keys():
self.event_callbacks[event_name] = []

self.event_callbacks[event_name].append(callback)
self.event_callbacks[event_name].append({"func": callback,
"kwargs": kwargs,
"decode_json": decode_json})

def trigger(self, event_name, data):
"""Trigger an event on this channel. Only available for private or
"""Trigger an event on this channel. Only available for private or
presence channels

:param event_name: The name of the event. Must begin with 'client-''
Expand All @@ -36,4 +50,7 @@ def trigger(self, event_name, data):
def _handle_event(self, event_name, data):
if event_name in self.event_callbacks.keys():
for callback in self.event_callbacks[event_name]:
callback(data)
if callback["decode_json"]:
callback["func"](json.loads(data), **callback["kwargs"])
else:
callback["func"](data, **callback["kwargs"])
18 changes: 15 additions & 3 deletions pusherclient/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,26 @@ def __init__(self, event_handler, url, log_level=logging.INFO, daemon=True, reco
Thread.__init__(self)
self.daemon = daemon

def bind(self, event_name, callback):
#TODO add an option to decode json of the message, to make implementations
#more DRY
def bind(self, event_name, callback, kwargs={}, decode_json=False):
"""Bind an event to a callback

:param event_name: The name of the event to bind to.
:type event_name: str

:param callback: The callback to notify of this event.

:param kwargs: The keyword arguments to pass to the callback.
:type kwargs: dict
"""

if event_name not in self.event_callbacks.keys():
self.event_callbacks[event_name] = []

self.event_callbacks[event_name].append(callback)
self.event_callbacks[event_name].append({"func": callback,
"kwargs": kwargs,
"decode_json": decode_json})

def disconnect(self):
self.needs_reconnect = False
Expand Down Expand Up @@ -145,7 +152,12 @@ def _on_message(self, ws, message):
if params['event'] in self.event_callbacks.keys():
for callback in self.event_callbacks[params['event']]:
try:
callback(params['data'])
if callback["decode_json"]:
callback["func"](json.loads(params['data']),
**callback["kwargs"])
else:
callback["func"](params['data'],
**callback["kwargs"])
except Exception:
self.logger.exception("Callback raised unhandled")
else:
Expand Down