2020-01-09 10:28:02 +01:00
|
|
|
import enum
|
2017-12-25 17:23:09 +01:00
|
|
|
import logging
|
2018-01-08 02:41:24 +01:00
|
|
|
import re
|
2020-04-08 23:22:54 +02:00
|
|
|
from functools import wraps
|
2017-12-25 17:23:09 +01:00
|
|
|
|
2018-09-27 01:09:00 +02:00
|
|
|
from queue import LifoQueue
|
2021-02-27 20:27:36 +01:00
|
|
|
|
|
|
|
from ..common import exec_wrapper
|
2017-12-25 17:23:09 +01:00
|
|
|
from ..config import Config
|
|
|
|
from ..message.request import Request
|
2018-01-05 23:20:39 +01:00
|
|
|
from ..message.response import Response
|
2017-12-25 17:23:09 +01:00
|
|
|
|
2020-09-27 01:33:38 +02:00
|
|
|
logger = logging.getLogger('platypush')
|
2018-06-06 20:09:18 +02:00
|
|
|
|
|
|
|
|
2020-01-09 10:28:02 +01:00
|
|
|
class Statement(enum.Enum):
|
|
|
|
BREAK = 'break'
|
|
|
|
CONTINUE = 'continue'
|
|
|
|
RETURN = 'return'
|
|
|
|
|
|
|
|
|
2017-12-25 17:23:09 +01:00
|
|
|
class Procedure(object):
|
|
|
|
""" Procedure class. A procedure is a pre-configured list of requests """
|
|
|
|
|
2018-09-25 19:20:34 +02:00
|
|
|
def __init__(self, name, _async, requests, args=None, backend=None):
|
2017-12-25 17:23:09 +01:00
|
|
|
"""
|
|
|
|
Params:
|
|
|
|
name -- Procedure name
|
2018-08-08 00:00:11 +02:00
|
|
|
_async -- Whether the actions in the procedure are supposed to
|
2018-01-06 00:21:25 +01:00
|
|
|
be executed sequentially or in parallel (True or False)
|
2020-01-09 10:28:02 +01:00
|
|
|
requests -- List of platypush.message.request.Request objects
|
2017-12-25 17:23:09 +01:00
|
|
|
"""
|
|
|
|
|
2019-07-13 15:49:38 +02:00
|
|
|
self.name = name
|
|
|
|
self._async = _async
|
2017-12-25 17:23:09 +01:00
|
|
|
self.requests = requests
|
2019-07-13 15:49:38 +02:00
|
|
|
self.backend = backend
|
|
|
|
self.args = args or {}
|
2020-01-09 10:28:02 +01:00
|
|
|
self._should_return = False
|
2017-12-25 17:23:09 +01:00
|
|
|
|
|
|
|
for req in requests:
|
|
|
|
req.backend = self.backend
|
|
|
|
|
|
|
|
@classmethod
|
2018-09-27 01:09:00 +02:00
|
|
|
def build(cls, name, _async, requests, args=None, backend=None, id=None, procedure_class=None, **kwargs):
|
2017-12-25 17:23:09 +01:00
|
|
|
reqs = []
|
2020-01-09 10:28:02 +01:00
|
|
|
for_count = 0
|
|
|
|
while_count = 0
|
2018-04-29 22:25:31 +02:00
|
|
|
if_count = 0
|
2018-09-27 01:09:00 +02:00
|
|
|
if_config = LifoQueue()
|
|
|
|
procedure_class = procedure_class or cls
|
2019-07-13 14:22:43 +02:00
|
|
|
key = None
|
2018-01-08 02:41:24 +01:00
|
|
|
|
2017-12-25 17:23:09 +01:00
|
|
|
for request_config in requests:
|
2020-01-09 10:28:02 +01:00
|
|
|
# Check if it's a break/continue/return statement
|
|
|
|
if isinstance(request_config, str):
|
|
|
|
reqs.append(Statement(request_config))
|
|
|
|
continue
|
2018-01-08 02:41:24 +01:00
|
|
|
|
2018-04-29 22:25:31 +02:00
|
|
|
# Check if this request is an if-else
|
|
|
|
if len(request_config.keys()) >= 1:
|
|
|
|
key = list(request_config.keys())[0]
|
2021-02-24 01:28:46 +01:00
|
|
|
m = re.match(r'\s*(if)\s+\${(.*)}\s*', key)
|
2018-09-27 01:09:00 +02:00
|
|
|
|
2018-04-29 22:25:31 +02:00
|
|
|
if m:
|
|
|
|
if_count += 1
|
|
|
|
if_name = '{}__if_{}'.format(name, if_count)
|
|
|
|
condition = m.group(2)
|
|
|
|
|
2018-09-27 01:09:00 +02:00
|
|
|
if_config.put({
|
2019-07-13 15:49:38 +02:00
|
|
|
'name': if_name,
|
|
|
|
'_async': False,
|
|
|
|
'requests': request_config[key],
|
|
|
|
'condition': condition,
|
|
|
|
'else_branch': [],
|
|
|
|
'backend': backend,
|
|
|
|
'id': id,
|
2018-09-27 01:09:00 +02:00
|
|
|
})
|
2018-04-29 22:25:31 +02:00
|
|
|
|
|
|
|
continue
|
|
|
|
|
2018-09-27 01:09:00 +02:00
|
|
|
if key == 'else':
|
|
|
|
if if_config.empty():
|
|
|
|
raise RuntimeError('else statement with no ' +
|
|
|
|
'associated if in {}'.format(name))
|
|
|
|
|
|
|
|
conf = if_config.get()
|
|
|
|
conf['else_branch'] = request_config[key]
|
|
|
|
if_config.put(conf)
|
|
|
|
|
2018-09-27 02:20:25 +02:00
|
|
|
if not if_config.empty():
|
|
|
|
reqs.append(IfProcedure.build(**(if_config.get())))
|
|
|
|
if key == 'else':
|
|
|
|
continue
|
2018-09-27 01:09:00 +02:00
|
|
|
|
2020-01-09 10:28:02 +01:00
|
|
|
# Check if this request is a for loop
|
|
|
|
if len(request_config.keys()) == 1:
|
|
|
|
key = list(request_config.keys())[0]
|
2021-02-24 01:28:46 +01:00
|
|
|
m = re.match(r'\s*(fork?)\s+([\w\d_]+)\s+in\s+(.*)\s*', key)
|
2020-01-09 10:28:02 +01:00
|
|
|
|
|
|
|
if m:
|
|
|
|
for_count += 1
|
|
|
|
loop_name = '{}__for_{}'.format(name, for_count)
|
|
|
|
|
|
|
|
# A 'for' loop is synchronous. Declare a 'fork' loop if you
|
|
|
|
# want to process the elements in the iterable in parallel
|
|
|
|
_async = True if m.group(1) == 'fork' else False
|
|
|
|
iterator_name = m.group(2)
|
|
|
|
iterable = m.group(3)
|
|
|
|
|
|
|
|
loop = ForProcedure.build(name=loop_name, _async=_async,
|
|
|
|
requests=request_config[key],
|
|
|
|
backend=backend, id=id,
|
|
|
|
iterator_name=iterator_name,
|
|
|
|
iterable=iterable)
|
|
|
|
|
|
|
|
reqs.append(loop)
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Check if this request is a while loop
|
|
|
|
if len(request_config.keys()) == 1:
|
|
|
|
key = list(request_config.keys())[0]
|
2021-02-24 01:28:46 +01:00
|
|
|
m = re.match(r'\s*while\s+\${(.*)}\s*', key)
|
2020-01-09 10:28:02 +01:00
|
|
|
|
|
|
|
if m:
|
|
|
|
while_count += 1
|
|
|
|
loop_name = '{}__while_{}'.format(name, while_count)
|
|
|
|
condition = m.group(1).strip()
|
|
|
|
|
|
|
|
loop = WhileProcedure.build(name=loop_name, _async=False,
|
|
|
|
requests=request_config[key],
|
|
|
|
condition=condition,
|
|
|
|
backend=backend, id=id)
|
|
|
|
|
|
|
|
reqs.append(loop)
|
|
|
|
continue
|
|
|
|
|
2017-12-25 17:23:09 +01:00
|
|
|
request_config['origin'] = Config.get('device_id')
|
|
|
|
request_config['id'] = id
|
|
|
|
if 'target' not in request_config:
|
|
|
|
request_config['target'] = request_config['origin']
|
|
|
|
|
|
|
|
request = Request.build(request_config)
|
|
|
|
reqs.append(request)
|
|
|
|
|
2019-07-13 22:22:30 +02:00
|
|
|
while not if_config.empty():
|
2019-07-13 15:49:38 +02:00
|
|
|
pending_if = if_config.get()
|
2019-07-13 22:22:30 +02:00
|
|
|
reqs.append(IfProcedure.build(**pending_if))
|
2018-09-27 01:09:00 +02:00
|
|
|
|
2021-02-24 01:28:46 +01:00
|
|
|
# noinspection PyArgumentList
|
2018-09-27 01:09:00 +02:00
|
|
|
return procedure_class(name=name, _async=_async, requests=reqs, args=args, backend=backend, **kwargs)
|
2017-12-25 17:23:09 +01:00
|
|
|
|
2020-01-09 10:28:02 +01:00
|
|
|
@staticmethod
|
|
|
|
def _find_nearest_loop(stack):
|
|
|
|
for proc in stack[::-1]:
|
|
|
|
if isinstance(proc, LoopProcedure):
|
|
|
|
return proc
|
|
|
|
|
|
|
|
raise AssertionError('break/continue statement found outside of a loop')
|
|
|
|
|
|
|
|
def execute(self, n_tries=1, __stack__=None, **context):
|
2017-12-25 17:23:09 +01:00
|
|
|
"""
|
|
|
|
Execute the requests in the procedure
|
|
|
|
Params:
|
|
|
|
n_tries -- Number of tries in case of failure before raising a RuntimeError
|
|
|
|
"""
|
2020-01-09 10:28:02 +01:00
|
|
|
if not __stack__:
|
|
|
|
__stack__ = [self]
|
|
|
|
else:
|
|
|
|
__stack__.append(self)
|
|
|
|
|
2018-09-25 19:20:34 +02:00
|
|
|
if self.args:
|
2019-02-15 11:45:11 +01:00
|
|
|
args = self.args.copy()
|
2019-07-13 15:49:38 +02:00
|
|
|
for k, v in args.items():
|
2018-10-05 09:38:34 +02:00
|
|
|
v = Request.expand_value_from_context(v, **context)
|
2019-02-15 11:45:11 +01:00
|
|
|
args[k] = v
|
2018-09-25 19:20:34 +02:00
|
|
|
context[k] = v
|
2019-02-15 11:45:11 +01:00
|
|
|
logger.info('Executing procedure {} with arguments {}'.format(self.name, args))
|
2018-09-25 19:20:34 +02:00
|
|
|
else:
|
|
|
|
logger.info('Executing procedure {}'.format(self.name))
|
|
|
|
|
2018-01-05 23:20:39 +01:00
|
|
|
response = Response()
|
2018-07-09 16:51:17 +02:00
|
|
|
token = Config.get('token')
|
2018-01-05 23:20:39 +01:00
|
|
|
|
2017-12-25 17:23:09 +01:00
|
|
|
for request in self.requests:
|
2020-04-08 23:22:54 +02:00
|
|
|
if callable(request):
|
|
|
|
response = request(**context)
|
|
|
|
continue
|
|
|
|
|
2020-01-09 10:28:02 +01:00
|
|
|
if isinstance(request, Statement):
|
|
|
|
if request == Statement.RETURN:
|
|
|
|
self._should_return = True
|
|
|
|
for proc in __stack__:
|
|
|
|
proc._should_return = True
|
|
|
|
break
|
|
|
|
|
|
|
|
if request in [Statement.BREAK, Statement.CONTINUE]:
|
|
|
|
loop = self._find_nearest_loop(__stack__)
|
|
|
|
if request == Statement.BREAK:
|
|
|
|
loop._should_break = True
|
|
|
|
else:
|
|
|
|
loop._should_continue = True
|
|
|
|
break
|
|
|
|
|
|
|
|
if isinstance(self, LoopProcedure):
|
|
|
|
if self._should_continue or self._should_break:
|
|
|
|
if self._should_continue:
|
|
|
|
# noinspection PyAttributeOutsideInit
|
|
|
|
self._should_continue = False
|
|
|
|
|
|
|
|
break
|
|
|
|
|
2018-07-09 16:51:17 +02:00
|
|
|
if token:
|
|
|
|
request.token = token
|
|
|
|
|
2019-07-13 15:49:38 +02:00
|
|
|
context['_async'] = self._async
|
|
|
|
context['n_tries'] = n_tries
|
2020-01-09 10:28:02 +01:00
|
|
|
response = request.execute(__stack__=__stack__, **context)
|
2018-01-07 23:31:19 +01:00
|
|
|
|
2019-01-07 00:07:45 +01:00
|
|
|
if not self._async and response:
|
2018-01-07 23:31:19 +01:00
|
|
|
if isinstance(response.output, dict):
|
2019-07-13 15:49:38 +02:00
|
|
|
for k, v in response.output.items():
|
2018-01-07 23:31:19 +01:00
|
|
|
context[k] = v
|
2018-01-06 00:21:25 +01:00
|
|
|
|
|
|
|
context['output'] = response.output
|
|
|
|
context['errors'] = response.errors
|
2018-01-05 23:20:39 +01:00
|
|
|
|
2020-01-09 10:28:02 +01:00
|
|
|
if self._should_return:
|
|
|
|
break
|
|
|
|
|
2019-01-07 00:07:45 +01:00
|
|
|
return response or Response()
|
2017-12-25 17:23:09 +01:00
|
|
|
|
|
|
|
|
2018-01-08 02:41:24 +01:00
|
|
|
class LoopProcedure(Procedure):
|
2020-01-09 10:28:02 +01:00
|
|
|
"""
|
|
|
|
Base class while and for/fork loops.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, name, requests, _async=False, args=None, backend=None):
|
|
|
|
super(). __init__(name=name, _async=_async, requests=requests, args=args, backend=backend)
|
|
|
|
self._should_break = False
|
|
|
|
self._should_continue = False
|
|
|
|
|
|
|
|
|
|
|
|
class ForProcedure(LoopProcedure):
|
2018-01-08 02:41:24 +01:00
|
|
|
"""
|
|
|
|
Models a loop procedure, which expresses a construct similar to a
|
|
|
|
for loop in a programming language. The 'for' keyword implies a synchronous
|
|
|
|
loop, i.e. the nested actions will be executed in sequence. Use 'fork'
|
|
|
|
instead of 'for' if you want to run the actions in parallel.
|
|
|
|
|
2020-01-09 10:28:02 +01:00
|
|
|
Example::
|
2018-01-08 02:41:24 +01:00
|
|
|
|
|
|
|
procedure.sync.process_results:
|
2020-01-09 10:28:02 +01:00
|
|
|
- action: http.get
|
|
|
|
args:
|
|
|
|
url: https://some-service/some/json/endpoint
|
|
|
|
# Example response: { "results": [ {"id":1, "name":"foo"}, {"id":2,"name":"bar"} ]}
|
|
|
|
- for result in ${results}:
|
|
|
|
- action: some.custom.action
|
|
|
|
args:
|
|
|
|
id: ${result['id']}
|
|
|
|
name: ${result['name']}
|
2018-01-08 02:41:24 +01:00
|
|
|
|
2020-01-09 10:28:02 +01:00
|
|
|
"""
|
2018-01-08 02:41:24 +01:00
|
|
|
|
2019-07-13 15:49:38 +02:00
|
|
|
def __init__(self, name, iterator_name, iterable, requests, _async=False, args=None, backend=None):
|
|
|
|
super(). __init__(name=name, _async=_async, requests=requests, args=args, backend=backend)
|
2018-01-08 02:41:24 +01:00
|
|
|
self.iterator_name = iterator_name
|
|
|
|
self.iterable = iterable
|
|
|
|
|
2018-08-08 00:00:11 +02:00
|
|
|
def execute(self, _async=None, **context):
|
2019-08-16 19:04:15 +02:00
|
|
|
# noinspection PyBroadException
|
|
|
|
try:
|
|
|
|
iterable = eval(self.iterable)
|
2020-01-09 10:28:02 +01:00
|
|
|
assert hasattr(iterable, '__iter__'), 'Object of type {} is not iterable: {}'.\
|
|
|
|
format(type(iterable), iterable)
|
2019-08-16 19:04:15 +02:00
|
|
|
except:
|
|
|
|
iterable = Request.expand_value_from_context(self.iterable, **context)
|
|
|
|
|
2018-01-08 02:41:24 +01:00
|
|
|
response = Response()
|
|
|
|
|
2020-01-09 10:28:02 +01:00
|
|
|
# noinspection DuplicatedCode
|
2018-01-08 02:41:24 +01:00
|
|
|
for item in iterable:
|
2020-01-09 10:28:02 +01:00
|
|
|
if self._should_return:
|
|
|
|
logger.info('Returning from {}'.format(self.name))
|
|
|
|
break
|
|
|
|
|
|
|
|
if self._should_continue:
|
|
|
|
self._should_continue = False
|
|
|
|
logger.info('Continuing loop {}'.format(self.name))
|
|
|
|
continue
|
|
|
|
|
|
|
|
if self._should_break:
|
|
|
|
self._should_break = False
|
|
|
|
logger.info('Breaking loop {}'.format(self.name))
|
|
|
|
break
|
|
|
|
|
2018-01-08 02:41:24 +01:00
|
|
|
context[self.iterator_name] = item
|
2018-01-17 03:16:59 +01:00
|
|
|
response = super().execute(**context)
|
2018-01-08 02:41:24 +01:00
|
|
|
|
|
|
|
return response
|
|
|
|
|
2018-04-29 22:25:31 +02:00
|
|
|
|
2020-01-09 10:28:02 +01:00
|
|
|
class WhileProcedure(LoopProcedure):
|
|
|
|
"""
|
|
|
|
Models a while loop procedure.
|
|
|
|
|
|
|
|
Example::
|
|
|
|
|
|
|
|
procedure.process_results:
|
|
|
|
- action: http.get
|
|
|
|
args:
|
|
|
|
url: https://some-service/some/json/endpoint
|
|
|
|
# Example response: {"id":1, "name":"foo"}}
|
|
|
|
|
|
|
|
- while ${output}:
|
|
|
|
- action: some.custom.action
|
|
|
|
args:
|
|
|
|
id: ${id}
|
|
|
|
name: ${name}
|
|
|
|
|
|
|
|
- action: http.get
|
|
|
|
args:
|
|
|
|
url: https://some-service/some/json/endpoint
|
|
|
|
# Example response: {"id":1, "name":"foo"}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, name, condition, requests, _async=False, args=None, backend=None):
|
|
|
|
super(). __init__(name=name, _async=_async, requests=requests, args=args, backend=backend)
|
|
|
|
self.condition = condition
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _get_context(**context):
|
|
|
|
for (k, v) in context.items():
|
|
|
|
# noinspection PyBroadException
|
|
|
|
try:
|
|
|
|
context[k] = eval(v)
|
|
|
|
except:
|
|
|
|
if isinstance(v, str):
|
|
|
|
# noinspection PyBroadException
|
|
|
|
try:
|
2021-02-26 22:33:26 +01:00
|
|
|
context[k] = eval('"{}"'.format(re.sub(r'(^|[^\\])"', '\1\\"', v)))
|
2021-02-26 22:43:54 +01:00
|
|
|
except Exception as e:
|
|
|
|
logger.warning('Could not parse value for context variable {}={}'.format(k, v))
|
2021-03-05 19:00:26 +01:00
|
|
|
logger.warning('Context: {}'.format(context))
|
2021-02-26 22:43:54 +01:00
|
|
|
logger.exception(e)
|
2020-01-09 10:28:02 +01:00
|
|
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
# noinspection DuplicatedCode,PyBroadException
|
|
|
|
def execute(self, _async=None, **context):
|
|
|
|
response = Response()
|
|
|
|
context = self._get_context(**context)
|
|
|
|
for k, v in context.items():
|
|
|
|
try:
|
|
|
|
exec('{}={}'.format(k, v))
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
while True:
|
|
|
|
condition_true = eval(self.condition)
|
|
|
|
if not condition_true:
|
|
|
|
break
|
|
|
|
|
|
|
|
if self._should_return:
|
|
|
|
logger.info('Returning from {}'.format(self.name))
|
|
|
|
break
|
|
|
|
|
|
|
|
if self._should_continue:
|
|
|
|
self._should_continue = False
|
|
|
|
logger.info('Continuing loop {}'.format(self.name))
|
|
|
|
continue
|
|
|
|
|
|
|
|
if self._should_break:
|
|
|
|
self._should_break = False
|
|
|
|
logger.info('Breaking loop {}'.format(self.name))
|
|
|
|
break
|
|
|
|
|
|
|
|
response = super().execute(**context)
|
|
|
|
|
|
|
|
if response.output:
|
|
|
|
if isinstance(response.output, dict):
|
|
|
|
new_context = self._get_context(**response.output)
|
|
|
|
for k, v in new_context.items():
|
|
|
|
try:
|
|
|
|
exec('{}={}'.format(k, v))
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2019-07-13 15:49:38 +02:00
|
|
|
# noinspection PyBroadException
|
2018-04-29 22:25:31 +02:00
|
|
|
class IfProcedure(Procedure):
|
|
|
|
"""
|
|
|
|
Models an if-else construct.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
procedure.sync.process_results:
|
|
|
|
-
|
|
|
|
action: http.get
|
|
|
|
args:
|
|
|
|
url: https://some-service/some/json/endpoint
|
|
|
|
# Example response: { "sensors": [ {"temperature": 18 } ] }
|
|
|
|
-
|
|
|
|
if ${sensors['temperature'] < 20}:
|
|
|
|
-
|
|
|
|
action: shell.exec
|
|
|
|
args:
|
|
|
|
cmd: '/path/turn_on_heating.sh'
|
|
|
|
else:
|
|
|
|
-
|
|
|
|
action: shell.exec
|
|
|
|
args:
|
|
|
|
cmd: '/path/turn_off_heating.sh'
|
|
|
|
"""
|
|
|
|
|
2018-09-27 01:09:00 +02:00
|
|
|
def __init__(self, name, condition, requests, else_branch=None, args=None, backend=None, id=None, **kwargs):
|
2018-08-08 00:00:11 +02:00
|
|
|
kwargs['_async'] = False
|
2018-04-29 22:25:31 +02:00
|
|
|
self.condition = condition
|
2018-09-27 01:09:00 +02:00
|
|
|
self.else_branch = else_branch
|
2018-04-29 22:25:31 +02:00
|
|
|
reqs = []
|
|
|
|
|
|
|
|
for req in requests:
|
2018-09-26 22:31:27 +02:00
|
|
|
if isinstance(req, dict):
|
|
|
|
req['origin'] = Config.get('device_id')
|
|
|
|
req['id'] = id
|
|
|
|
if 'target' not in req:
|
|
|
|
req['target'] = req['origin']
|
|
|
|
|
|
|
|
req = Request.build(req)
|
2018-04-29 22:25:31 +02:00
|
|
|
|
2018-09-26 22:31:27 +02:00
|
|
|
reqs.append(req)
|
2018-04-29 22:25:31 +02:00
|
|
|
|
2018-09-25 19:20:34 +02:00
|
|
|
super(). __init__(name=name, requests=reqs, args=args, backend=backend, **kwargs)
|
2018-04-29 22:25:31 +02:00
|
|
|
|
2018-09-27 01:09:00 +02:00
|
|
|
@classmethod
|
|
|
|
def build(cls, name, condition, requests, else_branch=None, args=None, backend=None, id=None, **kwargs):
|
|
|
|
kwargs['_async'] = False
|
|
|
|
if else_branch:
|
|
|
|
else_branch = super().build(name=name+'__else', requests=else_branch,
|
|
|
|
args=args, backend=backend, id=id,
|
|
|
|
procedure_class=Procedure, **kwargs)
|
|
|
|
|
|
|
|
return super().build(name=name, condition=condition, requests=requests,
|
2019-07-13 15:49:38 +02:00
|
|
|
else_branch=else_branch, args=args, backend=backend, id=id,
|
|
|
|
**kwargs)
|
2018-04-29 22:25:31 +02:00
|
|
|
|
|
|
|
def execute(self, **context):
|
2018-06-06 17:14:44 +02:00
|
|
|
for (k, v) in context.items():
|
2018-06-14 19:02:01 +02:00
|
|
|
try:
|
|
|
|
exec('{}={}'.format(k, v))
|
|
|
|
except:
|
|
|
|
if isinstance(v, str):
|
2018-06-14 19:07:33 +02:00
|
|
|
try:
|
2021-02-26 22:33:26 +01:00
|
|
|
exec('{}="{}"'.format(k, re.sub(r'(^|[^\\])"', '\1\\"', v)))
|
2021-02-26 22:43:54 +01:00
|
|
|
except Exception as e:
|
2021-04-03 21:16:22 +02:00
|
|
|
logger.debug('Could not set context variable {}={}: {}'.format(k, v, str(e)))
|
|
|
|
logger.debug('Context: {}'.format(context))
|
2018-06-06 17:14:44 +02:00
|
|
|
|
2018-04-29 22:25:31 +02:00
|
|
|
condition_true = eval(self.condition)
|
|
|
|
response = Response()
|
|
|
|
|
|
|
|
if condition_true:
|
|
|
|
response = super().execute(**context)
|
|
|
|
elif self.else_branch:
|
2018-09-27 01:09:00 +02:00
|
|
|
response = self.else_branch.execute(**context)
|
2018-04-29 22:25:31 +02:00
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2020-04-08 23:22:54 +02:00
|
|
|
def procedure(f):
|
|
|
|
f.procedure = True
|
|
|
|
|
|
|
|
@wraps(f)
|
|
|
|
def _execute_procedure(*args, **kwargs):
|
2021-02-27 20:27:36 +01:00
|
|
|
return exec_wrapper(f, *args, **kwargs)
|
2020-04-08 23:22:54 +02:00
|
|
|
|
|
|
|
return _execute_procedure
|
|
|
|
|
|
|
|
|
2017-12-25 17:23:09 +01:00
|
|
|
# vim:sw=4:ts=4:et:
|