diff --git a/.travis.yml b/.travis.yml index 04b6215..4a735b3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 + diff --git a/pusherclient/channel.py b/pusherclient/channel.py index a09fe22..b431afe 100755 --- a/pusherclient/channel.py +++ b/pusherclient/channel.py @@ -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 @@ -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-'' @@ -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"]) diff --git a/pusherclient/connection.py b/pusherclient/connection.py index 75fe53f..ceb5bdb 100755 --- a/pusherclient/connection.py +++ b/pusherclient/connection.py @@ -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 @@ -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: