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..5ab2e62 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.''' @@ -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 do_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 860b670..083cd78 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.''' @@ -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) @@ -277,14 +278,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: @@ -358,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''' 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(), "