Rename "revert_with" => "revert" and "execute_with" to "execute"

"_with" is kind of obvious, and also less typing for users of this
library

Change-Id: I99f597dae5612cc44cc7f19ae144776f63d32bac
This commit is contained in:
Changbin Liu
2013-08-30 17:12:54 -04:00
parent f822363afa
commit 9443506b93
9 changed files with 21 additions and 21 deletions

View File

@@ -72,13 +72,13 @@ def task(*args, **kwargs):
"""Decorates a given function so that it can be used as a task"""
def decorator(f):
def task_factory(execute_with, **factory_kwargs):
def task_factory(execute, **factory_kwargs):
merged = kwargs.copy()
merged.update(factory_kwargs)
# NOTE(imelnikov): we can't capture f here because for
# bound methods and bound class methods the object it
# is bound to is yet unknown at the moment
return base.FunctorTask(execute_with, **merged)
return base.FunctorTask(execute, **merged)
w_f = _original_function(f)
setattr(w_f, utils.TASK_FACTORY_ATTRIBUTE, task_factory)
return f

View File

@@ -95,7 +95,7 @@ def trash(context, result, cause):
print("Throwing away pieces of car!")
@decorators.task(revert_with=trash)
@decorators.task(revert=trash)
def startup(context, **kwargs):
pass
# TODO(harlowja): try triggering reversion here!

View File

@@ -16,14 +16,14 @@ def undo_call(context, result, cause):
print("Calling %s and apologizing." % result)
@decorators.task(revert_with=undo_call)
@decorators.task(revert=undo_call)
def call_jim(context):
print("Calling jim.")
print("Context = %s" % (context))
return context['jim_number']
@decorators.task(revert_with=undo_call)
@decorators.task(revert=undo_call)
def call_joe(context):
print("Calling joe.")
print("Context = %s" % (context))

View File

@@ -99,42 +99,42 @@ class FunctorTask(BaseTask):
Take any callable and make a task from it.
"""
def __init__(self, execute_with, **kwargs):
def __init__(self, execute, **kwargs):
"""Initialize FunctorTask instance with given callable and kwargs
:param execute_with: the callable
:param execute: the callable
:param kwargs: reserved keywords (all optional) are
name: name of the task, default None (auto generate)
revert_with: the callable to revert, default None
revert: the callable to revert, default None
version: version of the task, default Task's version 1.0
optionals: optionals of the task, default ()
provides: provides of the task, default ()
requires: requires of the task, default ()
auto_extract: auto extract execute_with's args and put it into
auto_extract: auto extract execute's args and put it into
requires, default True
"""
name = kwargs.pop('name', None)
if name is None:
name = utils.get_callable_name(execute_with)
name = utils.get_callable_name(execute)
super(FunctorTask, self).__init__(name)
self._execute_with = execute_with
self._revert_with = kwargs.pop('revert_with', None)
self._execute = execute
self._revert = kwargs.pop('revert', None)
self.version = kwargs.pop('version', self.version)
self.optional.update(kwargs.pop('optional', ()))
self.provides.update(kwargs.pop('provides', ()))
self.requires.update(kwargs.pop('requires', ()))
if kwargs.pop('auto_extract', True):
f_args = utils.get_required_callable_args(execute_with)
f_args = utils.get_required_callable_args(execute)
self.requires.update(a for a in f_args if a != 'context')
if kwargs:
raise TypeError('__init__() got an unexpected keyword argument %r'
% kwargs.keys[0])
def __call__(self, *args, **kwargs):
return self._execute_with(*args, **kwargs)
return self._execute(*args, **kwargs)
def revert(self, *args, **kwargs):
if self._revert_with:
return self._revert_with(*args, **kwargs)
if self._revert:
return self._revert(*args, **kwargs)
else:
return None

View File

@@ -29,7 +29,7 @@ class WrapableObjectsTest(test.TestCase):
def revert_one(*args, **kwargs):
values.append('revert one')
@decorators.task(revert_with=revert_one)
@decorators.task(revert=revert_one)
def run_one(*args, **kwargs):
values.append('one')

View File

@@ -58,7 +58,7 @@ class FunctorTaskTest(test.TestCase):
flow = linear_flow.Flow('test')
flow.add_many((
t(bof.run_one, revert_with=bof.revert_one),
t(bof.run_one, revert=bof.revert_one),
t(bof.run_fail)
))
with self.assertRaisesRegexp(RuntimeError, '^Woot'):

View File

@@ -36,7 +36,7 @@ class GraphFlowTest(test.TestCase):
self.assertEquals(states.REVERTING, cause.flow.state)
self.assertEquals(result, {'a': 1})
@decorators.task(revert_with=run1_revert, provides=['a'])
@decorators.task(revert=run1_revert, provides=['a'])
def run1(context): # pylint: disable=W0613
return {
'a': 1,

View File

@@ -39,7 +39,7 @@ class LinearFlowTest(test.TestCase):
raise Exception("I blew up")
return blow_up
else:
@decorators.task(revert_with=do_revert,
@decorators.task(revert=do_revert,
name='do_apply %s' % token)
def do_apply(context, *args, **kwargs):
context[token] = 'passed'

View File

@@ -318,7 +318,7 @@ class ThreadedFlowTest(test.TestCase):
def reverter(context, result, cause):
context['reverted'] = True
@decorators.task(revert_with=reverter)
@decorators.task(revert=reverter)
def fail_quick(context):
raise IOError("Broken")