- 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:
parent
3932fb56c4
commit
36fdcf6963
6 changed files with 33 additions and 33 deletions
|
@ -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)])
|
|
@ -1,6 +1,8 @@
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from logging import getLogger
|
from logging import getLogger
|
||||||
|
|
||||||
|
from platypush.common import exec_wrapper
|
||||||
|
|
||||||
logger = getLogger(__name__)
|
logger = getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@ -11,17 +13,7 @@ def cron(cron_expression: str):
|
||||||
|
|
||||||
@wraps(f)
|
@wraps(f)
|
||||||
def wrapped(*args, **kwargs):
|
def wrapped(*args, **kwargs):
|
||||||
from platypush import Response
|
return exec_wrapper(f, *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 wrapped
|
return wrapped
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ import logging
|
||||||
import threading
|
import threading
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
|
||||||
|
from platypush.common import exec_wrapper
|
||||||
from platypush.config import Config
|
from platypush.config import Config
|
||||||
from platypush.message.event import Event
|
from platypush.message.event import Event
|
||||||
from platypush.message.request import Request
|
from platypush.message.request import Request
|
||||||
|
@ -172,17 +173,7 @@ def hook(event_type=Event, **condition):
|
||||||
|
|
||||||
@wraps(f)
|
@wraps(f)
|
||||||
def wrapped(*args, **kwargs):
|
def wrapped(*args, **kwargs):
|
||||||
from platypush import Response
|
return exec_wrapper(f, *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 wrapped
|
return wrapped
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
import sys
|
|
||||||
|
|
||||||
class Logger:
|
class Logger:
|
||||||
def __init__(self, level):
|
def __init__(self, level):
|
||||||
self.level = level
|
self.level = level
|
||||||
|
@ -18,5 +16,12 @@ class Logger:
|
||||||
def flush(self):
|
def flush(self):
|
||||||
pass
|
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:
|
# vim:sw=4:ts=4:et:
|
||||||
|
|
|
@ -77,6 +77,7 @@ class Request(Message):
|
||||||
|
|
||||||
proc_config = procedures[proc_name]
|
proc_config = procedures[proc_name]
|
||||||
if is_functional_procedure(proc_config):
|
if is_functional_procedure(proc_config):
|
||||||
|
self._expand_context(self.args, **kwargs)
|
||||||
kwargs = {**self.args, **kwargs}
|
kwargs = {**self.args, **kwargs}
|
||||||
if 'n_tries' in kwargs:
|
if 'n_tries' in kwargs:
|
||||||
del kwargs['n_tries']
|
del kwargs['n_tries']
|
||||||
|
|
|
@ -5,6 +5,8 @@ import re
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
|
||||||
from queue import LifoQueue
|
from queue import LifoQueue
|
||||||
|
|
||||||
|
from ..common import exec_wrapper
|
||||||
from ..config import Config
|
from ..config import Config
|
||||||
from ..message.request import Request
|
from ..message.request import Request
|
||||||
from ..message.response import Response
|
from ..message.response import Response
|
||||||
|
@ -477,15 +479,7 @@ def procedure(f):
|
||||||
|
|
||||||
@wraps(f)
|
@wraps(f)
|
||||||
def _execute_procedure(*args, **kwargs):
|
def _execute_procedure(*args, **kwargs):
|
||||||
try:
|
return exec_wrapper(f, *args, **kwargs)
|
||||||
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 _execute_procedure
|
return _execute_procedure
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue