Add call function to use for deferreds
This commit is contained in:
parent
03e56a4c07
commit
0b4e2f13d4
38
deferred.py
38
deferred.py
|
@ -1,3 +1,5 @@
|
|||
from functools import partial
|
||||
|
||||
class Deferred(object):
|
||||
"""
|
||||
Represents a delayed computation. This is a more elegant way to deal with
|
||||
|
@ -8,7 +10,8 @@ class Deferred(object):
|
|||
value by using the then method. Computations dependent on the Deferred will
|
||||
only proceed when the run method is called.
|
||||
|
||||
Attributes of a Deferred can be accessed directly as methods.
|
||||
Attributes of a Deferred can be accessed directly as methods. The result of
|
||||
calling these functions will be Deferred.
|
||||
|
||||
Example:
|
||||
image = Deferred()
|
||||
|
@ -92,6 +95,39 @@ class Then(object):
|
|||
return self.deferred.then(func)
|
||||
return helper
|
||||
|
||||
def call(func, *args, **kwargs):
|
||||
"""
|
||||
Call a function with deferred arguments
|
||||
|
||||
Example:
|
||||
colors = Deferred()
|
||||
colors.append('blue')
|
||||
colors.run(['red', 'green'])
|
||||
call(print, colors) #=> ['red', 'green', 'blue']
|
||||
call(print, 'hi', colors) #=> hi ['red', 'green', 'blue']
|
||||
"""
|
||||
for i, c in enumerate(args):
|
||||
if isinstance(c, Deferred):
|
||||
# Function without deferred arguments
|
||||
normalfunc = partial(func, *args[:i])
|
||||
# Function with deferred and possibly deferred arguments
|
||||
def restfunc(*arg2, **kwarg2):
|
||||
apply_deferred = partial(normalfunc, *arg2, **kwarg2)
|
||||
return call(apply_deferred, *args[i + 1:], **kwargs)
|
||||
return c.then(restfunc)
|
||||
items = kwargs.items()
|
||||
for i, (k, v) in enumerate(items):
|
||||
if isinstance(v, Deferred):
|
||||
# Function without deferred arguments
|
||||
normalfunc = partial(func, *args, **dict(items[:i]))
|
||||
# Function with deferred and possibly deferred arguments
|
||||
def restfunc2(*arg2, **kwarg2):
|
||||
apply_deferred = partial(normalfunc, *arg2, **kwarg2)
|
||||
return call(apply_deferred, **dict(items[i + 1:]))
|
||||
return c.then(restfunc2)
|
||||
# No items deferred
|
||||
return func(*args, **kwargs)
|
||||
|
||||
class DeferredHasValue(Exception):
|
||||
def __init__(self, string):
|
||||
super(DeferredHasValue, self).__init__(string)
|
||||
|
|
18
session.py
18
session.py
|
@ -40,8 +40,8 @@ from threading import Timer
|
|||
from group import Group
|
||||
from bot import Bot
|
||||
import deferred
|
||||
from deferred import call
|
||||
from yowsupwrapper import YowsupApp
|
||||
from functools import partial
|
||||
|
||||
|
||||
class MsgIDs:
|
||||
|
@ -736,22 +736,18 @@ class Session(YowsupApp):
|
|||
|
||||
# Send VCard
|
||||
if ID != None:
|
||||
response.pictureId().then(partial(
|
||||
self.logger.debug, 'Sending VCard (%s) with image id %s', ID
|
||||
))
|
||||
call(self.logger.debug, 'Sending VCard (%s) with image id %s',
|
||||
ID, response.pictureId())
|
||||
pictureData = response.pictureData()
|
||||
response.pictureData().then(partial(
|
||||
self.backend.handleVCard, self.user, ID, buddy, "", ""
|
||||
))
|
||||
call(self.backend.handleVCard, self.user, ID, buddy, "", "",
|
||||
response.pictureData())
|
||||
|
||||
# Send image hash
|
||||
if not buddy == self.legacyName:
|
||||
obuddy = self.buddies[buddy]
|
||||
image_hash = pictureData.then(utils.sha1hash)
|
||||
image_hash.then(partial(self.logger.debug, 'Image hash is %s'))
|
||||
image_hash.then(partial(
|
||||
self.updateBuddy, buddy, obuddy.nick, obuddy.groups
|
||||
))
|
||||
call(self.logger.debug, 'Image hash is %s', image_hash)
|
||||
call(self.updateBuddy, buddy, obuddy.nick, obuddy.groups, image_hash)
|
||||
|
||||
|
||||
def onDlsuccess(self, path):
|
||||
|
|
Loading…
Reference in a new issue