From 36fdcf696355ec5d178239bbdcb2c1bd531c4e29 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sat, 27 Feb 2021 20:27:36 +0100 Subject: [PATCH] - 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 --- platypush/common/__init__.py | 17 +++++++++++++++++ platypush/cron/__init__.py | 14 +++----------- platypush/event/hook.py | 13 ++----------- platypush/logger.py | 9 +++++++-- platypush/message/request/__init__.py | 1 + platypush/procedure/__init__.py | 12 +++--------- 6 files changed, 33 insertions(+), 33 deletions(-) diff --git a/platypush/common/__init__.py b/platypush/common/__init__.py index e69de29bb2..b4ca9f4b9c 100644 --- a/platypush/common/__init__.py +++ b/platypush/common/__init__.py @@ -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)]) diff --git a/platypush/cron/__init__.py b/platypush/cron/__init__.py index 7e92a57b97..39dd621d52 100644 --- a/platypush/cron/__init__.py +++ b/platypush/cron/__init__.py @@ -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 diff --git a/platypush/event/hook.py b/platypush/event/hook.py index f8d2a4c436..fba3f04b01 100644 --- a/platypush/event/hook.py +++ b/platypush/event/hook.py @@ -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 diff --git a/platypush/logger.py b/platypush/logger.py index 926ee83f77..7351278580 100644 --- a/platypush/logger.py +++ b/platypush/logger.py @@ -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: diff --git a/platypush/message/request/__init__.py b/platypush/message/request/__init__.py index 4aa2fb0a61..05162bca53 100644 --- a/platypush/message/request/__init__.py +++ b/platypush/message/request/__init__.py @@ -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'] diff --git a/platypush/procedure/__init__.py b/platypush/procedure/__init__.py index 1bea767d9f..66ca54bbb5 100644 --- a/platypush/procedure/__init__.py +++ b/platypush/procedure/__init__.py @@ -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