From 0121f36ac0c41467ab7dcc77dc6eb5c85db0e75b Mon Sep 17 00:00:00 2001 From: Griatch Date: Tue, 19 Feb 2013 23:30:33 +0100 Subject: [PATCH] Added cmd.funcpart as a way of adding multiple-part commands, each with a possibility to yield with a deferred. This allows for flexible implementation of delayed commands and other asynchronous goodies. --- src/commands/cmdhandler.py | 9 +++++++-- src/utils/utils.py | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/commands/cmdhandler.py b/src/commands/cmdhandler.py index f182f5e5ad..c9dd47aeab 100644 --- a/src/commands/cmdhandler.py +++ b/src/commands/cmdhandler.py @@ -43,7 +43,7 @@ from src.comms.channelhandler import CHANNELHANDLER from src.utils import logger, utils from src.commands.cmdset import CmdSet from src.commands.cmdparser import at_multimatch_cmd -from src.utils.utils import string_suggestions +from src.utils.utils import string_suggestions, make_iter from django.utils.translation import ugettext as _ @@ -254,7 +254,7 @@ def cmdhandler(caller, raw_string, testing=False): cmd.raw_string = unformatted_raw_string if hasattr(cmd, 'obj') and hasattr(cmd.obj, 'scripts'): - # cmd.obj are automatically made available. + # cmd.obj is automatically made available. # we make sure to validate its scripts. yield cmd.obj.scripts.validate() @@ -270,6 +270,11 @@ def cmdhandler(caller, raw_string, testing=False): # (return value is normally None) ret = yield cmd.func() + if hasattr(cmd, "funcparts"): + # yield on command parts (for multi-part delayed commands) + for funcpart in make_iter(cmd.funcparts): + yield funcpart() + # post-command hook yield cmd.at_post_cmd() diff --git a/src/utils/utils.py b/src/utils/utils.py index 0093e819e5..efbcb58e62 100644 --- a/src/utils/utils.py +++ b/src/utils/utils.py @@ -496,7 +496,7 @@ def uses_database(name="sqlite3"): engine = settings.DATABASE_ENGINE return engine == "django.db.backends.%s" % name -def delay(to_return, delay=2, callback=None): +def delay(delay=2, retval=None, callback=None): """ Delay the return of a value. Inputs: @@ -508,7 +508,7 @@ def delay(to_return, delay=2, callback=None): """ d = defer.Deferred() callb = callback or d.callback - reactor.callLater(delay, callb, to_return) + reactor.callLater(delay, callb, retval) return d _FROM_MODEL_MAP = None