Add logging
This commit is contained in:
parent
38e6038b8f
commit
911c04cbf1
3 changed files with 122 additions and 16 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
.. image:: https://travis-ci.org/invisibleroads/socketIO-client.svg?branch=v0.6.1
|
.. image:: https://travis-ci.org/invisibleroads/socketIO-client.svg?branch=0.6.1
|
||||||
:target: https://travis-ci.org/invisibleroads/socketIO-client
|
:target: https://travis-ci.org/invisibleroads/socketIO-client
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import json
|
import json
|
||||||
|
import logging
|
||||||
import requests
|
import requests
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
|
@ -6,6 +7,7 @@ from .exceptions import PacketError
|
||||||
|
|
||||||
|
|
||||||
__version__ = '0.6.1'
|
__version__ = '0.6.1'
|
||||||
|
_log = logging.getLogger(__name__)
|
||||||
TRANSPORTS = []
|
TRANSPORTS = []
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -31,7 +33,6 @@ class EngineIO(object):
|
||||||
print response.url
|
print response.url
|
||||||
|
|
||||||
engineIO_packets = _decode_content(response.content)
|
engineIO_packets = _decode_content(response.content)
|
||||||
print engineIO_packets
|
|
||||||
engineIO_packet_type, engineIO_packet_data = engineIO_packets[0]
|
engineIO_packet_type, engineIO_packet_data = engineIO_packets[0]
|
||||||
assert engineIO_packet_type == 0
|
assert engineIO_packet_type == 0
|
||||||
engineIO_packet_json = json.loads(engineIO_packet_data)
|
engineIO_packet_json = json.loads(engineIO_packet_data)
|
||||||
|
|
@ -41,6 +42,9 @@ class EngineIO(object):
|
||||||
# engineIO_packet_json['upgrades']
|
# engineIO_packet_json['upgrades']
|
||||||
self._session_id = engineIO_packet_json['sid']
|
self._session_id = engineIO_packet_json['sid']
|
||||||
|
|
||||||
|
if Namespace:
|
||||||
|
self.define(Namespace)
|
||||||
|
|
||||||
def wait(self):
|
def wait(self):
|
||||||
while True:
|
while True:
|
||||||
self._process_packets()
|
self._process_packets()
|
||||||
|
|
@ -168,6 +172,7 @@ class SocketIO(EngineIO):
|
||||||
host, port=None, Namespace=None,
|
host, port=None, Namespace=None,
|
||||||
wait_for_connection=True, transports=TRANSPORTS,
|
wait_for_connection=True, transports=TRANSPORTS,
|
||||||
resource='socket.io', **kw):
|
resource='socket.io', **kw):
|
||||||
|
self._namespace_by_path = {}
|
||||||
super(SocketIO, self).__init__(
|
super(SocketIO, self).__init__(
|
||||||
host, port, Namespace,
|
host, port, Namespace,
|
||||||
wait_for_connection, transports,
|
wait_for_connection, transports,
|
||||||
|
|
@ -219,10 +224,10 @@ class SocketIO(EngineIO):
|
||||||
return socketIO_packet_data
|
return socketIO_packet_data
|
||||||
|
|
||||||
def _on_connect(self, data_parsed, find_packet_callback):
|
def _on_connect(self, data_parsed, find_packet_callback):
|
||||||
pass
|
find_packet_callback('connect')()
|
||||||
|
|
||||||
def _on_disconnect(self, data_parsed, find_packet_callback):
|
def _on_disconnect(self, data_parsed, find_packet_callback):
|
||||||
pass
|
find_packet_callback('disconnect')()
|
||||||
|
|
||||||
def _on_event(self, data_parsed, find_packet_callback):
|
def _on_event(self, data_parsed, find_packet_callback):
|
||||||
pass
|
pass
|
||||||
|
|
@ -240,20 +245,121 @@ class SocketIO(EngineIO):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class BaseNamespace(object):
|
class EngineIONamespace(object):
|
||||||
pass
|
'Define engine.io client behavior'
|
||||||
|
|
||||||
|
def __init__(self, io):
|
||||||
|
self._io = io
|
||||||
|
self._callback_by_event = {}
|
||||||
|
self.initialize()
|
||||||
|
|
||||||
|
def initialize(self):
|
||||||
|
'Initialize custom variables here; you can override this method'
|
||||||
|
pass
|
||||||
|
|
||||||
|
def on_event(self, event, *args):
|
||||||
|
"""
|
||||||
|
Called after server sends an event; you can override this method.
|
||||||
|
Called only if a custom event handler does not exist,
|
||||||
|
such as one defined by namespace.on('my_event', my_function).
|
||||||
|
"""
|
||||||
|
callback, args = find_callback(args)
|
||||||
|
if callback:
|
||||||
|
callback(*args)
|
||||||
|
|
||||||
|
def _find_packet_callback(self, event):
|
||||||
|
# Check callbacks defined by on()
|
||||||
|
try:
|
||||||
|
return self._callback_by_event[event]
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
# Check callbacks defined explicitly or use on_event()
|
||||||
|
return getattr(
|
||||||
|
self, 'on_' + event.replace(' ', '_'),
|
||||||
|
lambda *args: self.on_event(event, *args))
|
||||||
|
|
||||||
|
|
||||||
class LoggingNamespace(BaseNamespace):
|
class SocketIONamespace(EngineIONamespace):
|
||||||
pass
|
'Define socket.io client behavior'
|
||||||
|
|
||||||
|
def __init__(self, io, path):
|
||||||
|
self.path = path
|
||||||
|
super(SocketIONamespace, self).__init__(io)
|
||||||
|
|
||||||
|
def on_connect(self):
|
||||||
|
'Called after server connects; you can override this method'
|
||||||
|
|
||||||
|
def on_reconnect(self):
|
||||||
|
'Called after server reconnects; you can override this method'
|
||||||
|
|
||||||
|
def on_disconnect(self):
|
||||||
|
'Called after server disconnects; you can override this method'
|
||||||
|
|
||||||
|
def _find_packet_callback(self, event):
|
||||||
|
# Interpret events
|
||||||
|
if event == 'connect':
|
||||||
|
if not hasattr(self, '_was_connected'):
|
||||||
|
self._was_connected = True
|
||||||
|
else:
|
||||||
|
event = 'reconnect'
|
||||||
|
return super(SocketIONamespace, self)._find_packet_callback(event)
|
||||||
|
|
||||||
|
|
||||||
|
class LoggingMixin(object):
|
||||||
|
|
||||||
|
def _log(self, level, msg, *attrs):
|
||||||
|
_log.log(level, '%s: %s' % (self._io._url, msg), *attrs)
|
||||||
|
|
||||||
|
|
||||||
|
class LoggingEngineIONamespace(EngineIONamespace, LoggingMixin):
|
||||||
|
|
||||||
|
def on_event(self, event, *args):
|
||||||
|
callback, args = find_callback(args)
|
||||||
|
arguments = [repr(_) for _ in args]
|
||||||
|
if callback:
|
||||||
|
arguments.append('callback(*args)')
|
||||||
|
self._log(
|
||||||
|
logging.INFO, '[event] %s(%s)',
|
||||||
|
event, ', '.join(arguments))
|
||||||
|
super(LoggingEngineIONamespace, self).on_event(event, *args)
|
||||||
|
|
||||||
|
|
||||||
|
class LoggingSocketIONamespace(SocketIONamespace, LoggingMixin):
|
||||||
|
|
||||||
|
def on_event(self, event, *args):
|
||||||
|
callback, args = find_callback(args)
|
||||||
|
arguments = [repr(_) for _ in args]
|
||||||
|
if callback:
|
||||||
|
arguments.append('callback(*args)')
|
||||||
|
self._log(
|
||||||
|
logging.INFO, '%s [event] %s(%s)', self.path,
|
||||||
|
event, ', '.join(arguments))
|
||||||
|
super(LoggingSocketIONamespace, self).on_event(event, *args)
|
||||||
|
|
||||||
|
def on_connect(self):
|
||||||
|
self._log(logging.DEBUG, '%s [connect]', self.path)
|
||||||
|
super(LoggingSocketIONamespace, self).on_connect()
|
||||||
|
|
||||||
|
def on_reconnect(self):
|
||||||
|
self._log(logging.DEBUG, '%s [reconnect]', self.path)
|
||||||
|
super(LoggingSocketIONamespace, self).on_reconnect()
|
||||||
|
|
||||||
|
def on_disconnect(self):
|
||||||
|
self._log(logging.DEBUG, '%s [disconnect]', self.path)
|
||||||
|
super(LoggingSocketIONamespace, self).on_disconnect()
|
||||||
|
|
||||||
|
|
||||||
def find_callback(args, kw=None):
|
def find_callback(args, kw=None):
|
||||||
pass
|
'Return callback whether passed as a last argument or as a keyword'
|
||||||
|
if args and callable(args[-1]):
|
||||||
|
return args[-1], args[:-1]
|
||||||
|
try:
|
||||||
|
return kw['callback'], args
|
||||||
|
except (KeyError, TypeError):
|
||||||
|
return None, args
|
||||||
|
|
||||||
|
|
||||||
def _decode_content(content):
|
def _decode_content(content):
|
||||||
print content
|
|
||||||
packets = []
|
packets = []
|
||||||
content_index = 0
|
content_index = 0
|
||||||
content_length = len(content)
|
content_length = len(content)
|
||||||
|
|
@ -306,9 +412,9 @@ def _make_packet_header(packet_string):
|
||||||
return ''.join(chr(x) for x in header_digits)
|
return ''.join(chr(x) for x in header_digits)
|
||||||
|
|
||||||
|
|
||||||
def _parse_engineIO_data():
|
def _parse_engineIO_data(data):
|
||||||
pass
|
return data
|
||||||
|
|
||||||
|
|
||||||
def _parse_socketIO_data():
|
def _parse_socketIO_data(data):
|
||||||
pass
|
return data
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import logging
|
import logging
|
||||||
from unittest import TestCase
|
from unittest import TestCase
|
||||||
|
|
||||||
from .. import SocketIO, LoggingNamespace, find_callback
|
from .. import SocketIO, LoggingSocketIONamespace, find_callback
|
||||||
|
|
||||||
|
|
||||||
HOST = 'localhost'
|
HOST = 'localhost'
|
||||||
|
|
@ -29,7 +29,7 @@ class Test_XHR_PollingTransport(TestCase, BaseMixin):
|
||||||
self.wait_time_in_seconds = 1
|
self.wait_time_in_seconds = 1
|
||||||
|
|
||||||
|
|
||||||
class Namespace(LoggingNamespace):
|
class Namespace(LoggingSocketIONamespace):
|
||||||
|
|
||||||
def initialize(self):
|
def initialize(self):
|
||||||
self.args_by_event = {}
|
self.args_by_event = {}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue