forked from platypush/platypush
Support for runtime arguments on procedures
This commit is contained in:
parent
38f61298f8
commit
13c1895295
2 changed files with 16 additions and 9 deletions
|
@ -71,7 +71,7 @@ class Request(Message):
|
||||||
proc_name = self.action.split('.')[-1]
|
proc_name = self.action.split('.')[-1]
|
||||||
proc_config = Config.get_procedures()[proc_name]
|
proc_config = Config.get_procedures()[proc_name]
|
||||||
proc = Procedure.build(name=proc_name, requests=proc_config['actions'],
|
proc = Procedure.build(name=proc_name, requests=proc_config['actions'],
|
||||||
_async=proc_config['_async'],
|
_async=proc_config['_async'], args=self.args,
|
||||||
backend=self.backend, id=self.id)
|
backend=self.backend, id=self.id)
|
||||||
|
|
||||||
return proc.execute(*args, **kwargs)
|
return proc.execute(*args, **kwargs)
|
||||||
|
|
|
@ -11,7 +11,7 @@ logger = logging.getLogger(__name__)
|
||||||
class Procedure(object):
|
class Procedure(object):
|
||||||
""" Procedure class. A procedure is a pre-configured list of requests """
|
""" Procedure class. A procedure is a pre-configured list of requests """
|
||||||
|
|
||||||
def __init__(self, name, _async, requests, backend=None):
|
def __init__(self, name, _async, requests, args=None, backend=None):
|
||||||
"""
|
"""
|
||||||
Params:
|
Params:
|
||||||
name -- Procedure name
|
name -- Procedure name
|
||||||
|
@ -24,13 +24,14 @@ class Procedure(object):
|
||||||
self._async = _async
|
self._async = _async
|
||||||
self.requests = requests
|
self.requests = requests
|
||||||
self.backend = backend
|
self.backend = backend
|
||||||
|
self.args = args or {}
|
||||||
|
|
||||||
for req in requests:
|
for req in requests:
|
||||||
req.backend = self.backend
|
req.backend = self.backend
|
||||||
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def build(cls, name, _async, requests, backend=None, id=None, **kwargs):
|
def build(cls, name, _async, requests, args=None, backend=None, id=None, **kwargs):
|
||||||
reqs = []
|
reqs = []
|
||||||
loop_count = 0
|
loop_count = 0
|
||||||
if_count = 0
|
if_count = 0
|
||||||
|
@ -87,7 +88,7 @@ class Procedure(object):
|
||||||
request = Request.build(request_config)
|
request = Request.build(request_config)
|
||||||
reqs.append(request)
|
reqs.append(request)
|
||||||
|
|
||||||
return cls(name=name, _async=_async, requests=reqs, backend=backend, **kwargs)
|
return cls(name=name, _async=_async, requests=reqs, args=args, backend=backend, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def execute(self, n_tries=1, **context):
|
def execute(self, n_tries=1, **context):
|
||||||
|
@ -97,7 +98,13 @@ class Procedure(object):
|
||||||
n_tries -- Number of tries in case of failure before raising a RuntimeError
|
n_tries -- Number of tries in case of failure before raising a RuntimeError
|
||||||
"""
|
"""
|
||||||
|
|
||||||
logger.info('Executing request {}'.format(self.name))
|
if self.args:
|
||||||
|
logger.info('Executing procedure {} with arguments {}'.format(self.name, self.args))
|
||||||
|
for (k,v) in self.args.items():
|
||||||
|
context[k] = v
|
||||||
|
else:
|
||||||
|
logger.info('Executing procedure {}'.format(self.name))
|
||||||
|
|
||||||
response = Response()
|
response = Response()
|
||||||
token = Config.get('token')
|
token = Config.get('token')
|
||||||
|
|
||||||
|
@ -145,8 +152,8 @@ class LoopProcedure(Procedure):
|
||||||
|
|
||||||
context = {}
|
context = {}
|
||||||
|
|
||||||
def __init__(self, name, iterator_name, iterable, requests, _async=False, backend=None, **kwargs):
|
def __init__(self, name, iterator_name, iterable, requests, _async=False, args=None, backend=None, **kwargs):
|
||||||
super(). __init__(name=name, _async=_async, requests=requests, backend=None, **kwargs)
|
super(). __init__(name=name, _async=_async, requests=requests, args=args, backend=backend, **kwargs)
|
||||||
|
|
||||||
self.iterator_name = iterator_name
|
self.iterator_name = iterator_name
|
||||||
self.iterable = iterable
|
self.iterable = iterable
|
||||||
|
@ -191,7 +198,7 @@ class IfProcedure(Procedure):
|
||||||
|
|
||||||
context = {}
|
context = {}
|
||||||
|
|
||||||
def __init__(self, name, condition, requests, else_branch=[], backend=None, id=None, **kwargs):
|
def __init__(self, name, condition, requests, else_branch=[], args=None, backend=None, id=None, **kwargs):
|
||||||
kwargs['_async'] = False
|
kwargs['_async'] = False
|
||||||
self.condition = condition
|
self.condition = condition
|
||||||
self.else_branch = []
|
self.else_branch = []
|
||||||
|
@ -213,7 +220,7 @@ class IfProcedure(Procedure):
|
||||||
|
|
||||||
self.else_branch.append(Request.build(req))
|
self.else_branch.append(Request.build(req))
|
||||||
|
|
||||||
super(). __init__(name=name, requests=reqs, backend=None, **kwargs)
|
super(). __init__(name=name, requests=reqs, args=args, backend=backend, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def execute(self, **context):
|
def execute(self, **context):
|
||||||
|
|
Loading…
Reference in a new issue