- The context should be properly expanded also when calling a Python procedure

- Refactored the logic for executing a request and wrapping the response common to procedures, crons and event hook decorators into platypush.common.exec_wrapper

- Added mock getvalue() method to Logger to prevent PyCharm failures in the tests when sys.stdout is redirected to the Logger object
This commit is contained in:
Fabio Manganiello 2021-02-27 20:27:36 +01:00
parent 3932fb56c4
commit 36fdcf6963
6 changed files with 33 additions and 33 deletions

View File

@ -0,0 +1,17 @@
import logging
logger = logging.getLogger('platypush')
def exec_wrapper(f, *args, **kwargs):
from platypush import Response
try:
ret = f(*args, **kwargs)
if isinstance(ret, Response):
return ret
return Response(output=ret)
except Exception as e:
logger.exception(e)
return Response(errors=[str(e)])

View File

@ -1,6 +1,8 @@
from functools import wraps
from logging import getLogger
from platypush.common import exec_wrapper
logger = getLogger(__name__)
@ -11,17 +13,7 @@ def cron(cron_expression: str):
@wraps(f)
def wrapped(*args, **kwargs):
from platypush import Response
try:
ret = f(*args, **kwargs)
if isinstance(ret, Response):
return ret
return Response(output=ret)
except Exception as e:
logger.exception(e)
return Response(errors=[str(e)])
return exec_wrapper(f, *args, **kwargs)
return wrapped

View File

@ -4,6 +4,7 @@ import logging
import threading
from functools import wraps
from platypush.common import exec_wrapper
from platypush.config import Config
from platypush.message.event import Event
from platypush.message.request import Request
@ -172,17 +173,7 @@ def hook(event_type=Event, **condition):
@wraps(f)
def wrapped(*args, **kwargs):
from platypush import Response
try:
ret = f(*args, **kwargs)
if isinstance(ret, Response):
return ret
return Response(output=ret)
except Exception as e:
logger.exception(e)
return Response(errors=[str(e)])
return exec_wrapper(f, *args, **kwargs)
return wrapped

View File

@ -1,5 +1,3 @@
import sys
class Logger:
def __init__(self, level):
self.level = level
@ -18,5 +16,12 @@ class Logger:
def flush(self):
pass
def getvalue(self):
"""
This function only serves to prevent PyCharm unit tests from failing when the stdout is redirected to the
Logger.
"""
pass
# vim:sw=4:ts=4:et:

View File

@ -77,6 +77,7 @@ class Request(Message):
proc_config = procedures[proc_name]
if is_functional_procedure(proc_config):
self._expand_context(self.args, **kwargs)
kwargs = {**self.args, **kwargs}
if 'n_tries' in kwargs:
del kwargs['n_tries']

View File

@ -5,6 +5,8 @@ import re
from functools import wraps
from queue import LifoQueue
from ..common import exec_wrapper
from ..config import Config
from ..message.request import Request
from ..message.response import Response
@ -477,15 +479,7 @@ def procedure(f):
@wraps(f)
def _execute_procedure(*args, **kwargs):
try:
ret = f(*args, **kwargs)
if isinstance(ret, Response):
return ret
return Response(output=ret)
except Exception as e:
logger.exception(e)
return Response(errors=[str(e)])
return exec_wrapper(f, *args, **kwargs)
return _execute_procedure