From 1c84e378656faac5f3ba138682f6f0b60172636c Mon Sep 17 00:00:00 2001 From: Ab Scond Date: Tue, 8 Apr 2014 09:41:13 -0600 Subject: [PATCH 1/4] Fixes for local imports. Minor syntactical changes for Python 3 compatibility (prints need parenthesis). --- firebasin/__init__.py | 4 ++-- firebasin/connection.py | 2 +- firebasin/dataref.py | 12 ++++++------ firebasin/debug.py | 2 +- firebasin/firebase.py | 10 +++++++--- firebasin/structure.py | 2 +- requirements.txt | 1 + tests/test_firebasin.py | 26 +++++++++++++------------- 8 files changed, 32 insertions(+), 27 deletions(-) create mode 100644 requirements.txt diff --git a/firebasin/__init__.py b/firebasin/__init__.py index 0dcc1fa..edfc17c 100644 --- a/firebasin/__init__.py +++ b/firebasin/__init__.py @@ -1,3 +1,3 @@ -from firebase import Firebase +from .firebase import Firebase -'''Firebasin Module''' \ No newline at end of file +'''Firebasin Module''' diff --git a/firebasin/connection.py b/firebasin/connection.py index a7baccd..f6fc4d5 100644 --- a/firebasin/connection.py +++ b/firebasin/connection.py @@ -3,7 +3,7 @@ import time import json -from debug import debug +from .debug import debug class Connection(threading.Thread): '''Connect to a Firebase websocket.''' diff --git a/firebasin/dataref.py b/firebasin/dataref.py index 860b670..37fd162 100644 --- a/firebasin/dataref.py +++ b/firebasin/dataref.py @@ -5,9 +5,9 @@ import datetime from threading import Timer -from connection import Connection -from structure import Structure -from debug import debug +from .connection import Connection +from .structure import Structure +from .debug import debug class DataRef(object): '''Reference a specific location in a Firebase.''' @@ -277,14 +277,14 @@ def _process(self, message): if error != 'ok': if error == 'permission_denied': path = request['d']['b']['p'] - print 'FIREBASE WARNING: on() or once() for %s failed: %s' % (path, error) + print('FIREBASE WARNING: on() or once() for %s failed: %s' % (path, error)) elif error == 'expired_token' or error == 'invalid_token': - print 'FIREBASE WARNING: auth() failed: %s' % (error) + print('FIREBASE WARNING: auth() failed: %s' % (error)) else: path = request['d']['b']['p'] - print 'FIREBASE WARNING: unknown for %s failed: %s' % (path, error) + print('FIREBASE WARNING: unknown for %s failed: %s' % (path, error)) onCancel = callbacks.get('onCancel', None) if not onCancel is None: diff --git a/firebasin/debug.py b/firebasin/debug.py index f7f82b7..6add161 100644 --- a/firebasin/debug.py +++ b/firebasin/debug.py @@ -3,4 +3,4 @@ def debug(*args): '''Print out data if we're in verbose mode''' if VERBOSE: - print args + print(args) diff --git a/firebasin/firebase.py b/firebasin/firebase.py index 724e41e..c98867c 100644 --- a/firebasin/firebase.py +++ b/firebasin/firebase.py @@ -1,5 +1,9 @@ -from dataref import RootDataRef -import urlparse +from .dataref import RootDataRef +try: + from urllib import parse as urlparse +except ImportError: + import urlparse + def Firebase(firebaseUrl): '''Construct a new Firebase reference from a full Firebase URL.''' @@ -9,4 +13,4 @@ def Firebase(firebaseUrl): if url.path == '/' or url.path == '': return root else: - return root.child(url.path[1:]) \ No newline at end of file + return root.child(url.path[1:]) diff --git a/firebasin/structure.py b/firebasin/structure.py index ff70373..e0d360b 100644 --- a/firebasin/structure.py +++ b/firebasin/structure.py @@ -1,4 +1,4 @@ -from datasnapshot import * +from .datasnapshot import * class Structure(dict): '''Hold data related to paths in an organized way.''' diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e0d32d2 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +ws4py diff --git a/tests/test_firebasin.py b/tests/test_firebasin.py index 0087b10..bfa1d54 100644 --- a/tests/test_firebasin.py +++ b/tests/test_firebasin.py @@ -25,42 +25,42 @@ def on_child_added(snapshot): - print str(snapshot.name()) + ' < child_added to /test_child_added/' + print(str(snapshot.name()) + ' < child_added to /test_child_added/') def on_value_read(snapshot): - print str(snapshot.name()) + str(snapshot.val()) + ' < value of test ' + print(str(snapshot.name()) + str(snapshot.val()) + ' < value of test ') def on_child_added(snapshot): - print str(snapshot.name()) + ' < child_added to /test_child_added/' + print(str(snapshot.name()) + ' < child_added to /test_child_added/') def on_value_read(snapshot): - print str(snapshot.val()) + ' < value of /test_value_read/' + snapshot.name() + print(str(snapshot.val()) + ' < value of /test_value_read/' + snapshot.name()) def on_child_removed (snapshot): - print str(snapshot.val()) + ' < child_removed from /test_child_removed/' + print(str(snapshot.val()) + ' < child_removed from /test_child_removed/') def on_child_added(snapshot): - print str(snapshot.name()) + ' < child_added to /test_child_added/' + print(str(snapshot.name()) + ' < child_added to /test_child_added/') def on_value_read(snapshot): - print str(snapshot.name()) + str(snapshot.val()) + ' < value of test ' + print(str(snapshot.name()) + str(snapshot.val()) + ' < value of test ') def on_child_changed (snapshot): - print str(snapshot.name()) + ' < child_changed in /test_child_changed/' + print(str(snapshot.name()) + ' < child_changed in /test_child_changed/') def on_child_added_once (snapshot): - print str(snapshot.name()) + ' < child_added once!' + print(str(snapshot.name()) + ' < child_added once!') def on_value_read_specific(snapshot): - print snapshot.val(), " Date: Tue, 8 Apr 2014 09:43:59 -0600 Subject: [PATCH 2/4] Move handshake out of connection thread such that creating the connection blocks until the handshake is done. Prior to this change, Firebase refs could be created and sent over the handshake connection instead of the actual connection. This caused several problems, including https://github.com/abeisgreat/python-firebasin/issues/3 . --- firebasin/connection.py | 9 +++------ firebasin/dataref.py | 1 + 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/firebasin/connection.py b/firebasin/connection.py index f6fc4d5..f35f0d7 100644 --- a/firebasin/connection.py +++ b/firebasin/connection.py @@ -20,8 +20,8 @@ def __init__(self, url, root): self.connected = False self.stopped = False - def run(self): - '''Perform a handshake then connect to a Firebase.''' + def handshake(self): + '''Perform a handshake.''' def set_url(d): self.handshake.close() @@ -36,10 +36,7 @@ def set_url(d): while not self.url: time.sleep(0.1) - # Once we have the url connect - self.connect() - - def connect(self): + def run(self): '''Connect to a Firebase.''' # Sometimes self.url is a dictionary with extra data, definitely don't know why that is. diff --git a/firebasin/dataref.py b/firebasin/dataref.py index 37fd162..24b837e 100644 --- a/firebasin/dataref.py +++ b/firebasin/dataref.py @@ -252,6 +252,7 @@ def __init__(self, url): self.subscriptions = {} self.history = [] self.connection.daemon = True + self.connection.do_handshake() self.connection.start() self._keep_alive() atexit.register(self.close) From a7f0c05011116ad1deb7cc21fdaa597a9f574c43 Mon Sep 17 00:00:00 2001 From: Ab Scond Date: Tue, 8 Apr 2014 21:28:19 -0600 Subject: [PATCH 3/4] Oops, name change missed. --- firebasin/connection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firebasin/connection.py b/firebasin/connection.py index f35f0d7..5ab2e62 100644 --- a/firebasin/connection.py +++ b/firebasin/connection.py @@ -20,7 +20,7 @@ def __init__(self, url, root): self.connected = False self.stopped = False - def handshake(self): + def do_handshake(self): '''Perform a handshake.''' def set_url(d): From d94b0423c28280dcce84e28fb1a325fd32889542 Mon Sep 17 00:00:00 2001 From: Ab Scond Date: Wed, 16 Apr 2014 23:07:16 -0600 Subject: [PATCH 4/4] Make the Timer thread a daemon so the program can exit more easily. --- firebasin/dataref.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/firebasin/dataref.py b/firebasin/dataref.py index 24b837e..083cd78 100644 --- a/firebasin/dataref.py +++ b/firebasin/dataref.py @@ -359,9 +359,13 @@ def _keep_alive(self): def send(): self._send({"t":"d", "d":{"r":0}}) - Timer(60.0, send).start() + t = Timer(60.0, send) + t.setDaemon(True) + t.start() - Timer(60.0, send).start() + t = Timer(60.0, send) + t.setDaemon(True) + t.start() def _bind(self, path, event, callback): '''Bind a single callback to an event on a path'''