Add store event callbacks to Python API

This commit is contained in:
alexhudson
2011-01-01 19:09:08 +00:00
parent bc8b623830
commit c4c2dac754
2 changed files with 33 additions and 7 deletions
+21 -5
View File
@@ -92,7 +92,10 @@ class CommandStream:
def __init__(self, socket):
self.stream = socket.makefile()
def setEventCallback(self, callback):
self.eventCallback = callback
def close(self):
self.stream.close()
@@ -113,10 +116,23 @@ class CommandStream:
self.stream.flush()
def GetResponse(self, nolog=False):
s = self.readline()
if not nolog:
self.log.debug("READ: %s", s)
return Response(s[0:4], s[5:])
rCode = 0
s = ''
while rCode == 0:
s = self.readline()
if not nolog:
self.log.debug("READ: %s", s)
rCode = int(s[0:4])
if rCode > 5999:
# this is a store 'event': send the response to the
# callback if we have one; otherwise it essentially
# gets eaten.
rCode = 0
if self.eventCallback:
self.eventCallback(Response(rCode, s[5:])
return Response(rCode, s[5:])
def readline(self):
s = self.stream.readline()
+12 -2
View File
@@ -359,7 +359,7 @@ class CalendarACL :
class StoreClient:
def __init__(self, user, owner, authToken=None, authCookie=None,
authPassword=None, host="localhost", port=689):
authPassword=None, host="localhost", port=689, eventCallback=None):
#from libbongo.libs import msgapi
self.owner = self.user = None
@@ -380,6 +380,8 @@ class StoreClient:
self.connection = StoreConnection(host, port)
self.stream = self.connection.stream
if eventCallback:
self.stream.setEventCallback(callback)
# if we authed with a cookie or password, we don't need to USER
if not authCookie and not authPassword and user is not None:
@@ -665,7 +667,15 @@ class StoreClient:
r = self.stream.GetResponse()
if r.code != 1000:
raise CommandError(r)
def NoOp(self):
command = "NOOP"
self.stream.Write(command)
r = self.stream.GetResponse()
if r.code != 1000:
raise CommandError(r)
# this function is here to coerce strings in unknown encodings
# into something approaching utf-8. This is obviously lossy,
# and we really don't want to have to do this - need to get the