Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions firebasin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from firebase import Firebase
from .firebase import Firebase

'''Firebasin Module'''
'''Firebasin Module'''
11 changes: 4 additions & 7 deletions firebasin/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.'''
Expand All @@ -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()
Expand All @@ -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.
Expand Down
21 changes: 13 additions & 8 deletions firebasin/dataref.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.'''
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand Down Expand Up @@ -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'''
Expand Down
2 changes: 1 addition & 1 deletion firebasin/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ def debug(*args):
'''Print out data if we're in verbose mode'''

if VERBOSE:
print args
print(args)
10 changes: 7 additions & 3 deletions firebasin/firebase.py
Original file line number Diff line number Diff line change
@@ -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.'''
Expand All @@ -9,4 +13,4 @@ def Firebase(firebaseUrl):
if url.path == '/' or url.path == '':
return root
else:
return root.child(url.path[1:])
return root.child(url.path[1:])
2 changes: 1 addition & 1 deletion firebasin/structure.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datasnapshot import *
from .datasnapshot import *

class Structure(dict):
'''Hold data related to paths in an organized way.'''
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ws4py
26 changes: 13 additions & 13 deletions tests/test_firebasin.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,47 +25,47 @@


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(), "<specific"
print(snapshot.val(), "<specific")

def onCancel(data):
print 'On was canceled'
print('On was canceled')

def onComplete(data):
print 'Set was completed', data
print('Set was completed', data)


server.child("ask").on('value', on_value_read)
server.child("bid").on('value', on_value_read)

# This is important to interupt the threaded socket when a
# keyboard interupt is received, it allows us to shut down gracefully
server.waitForInterrupt()
server.waitForInterrupt()