craftbeerpi4-pione/core/api/decorator.py

96 lines
2 KiB
Python
Raw Normal View History

2018-11-16 20:35:59 +01:00
__all__ = ["request_mapping", "on_startup", "on_event", "on_mqtt_message", "on_websocket_message", "action", "background_task"]
2018-11-01 19:50:04 +01:00
from aiohttp_auth import auth
def composed(*decs):
def deco(f):
for dec in reversed(decs):
f = dec(f)
return f
return deco
def request_mapping(path, name=None, method="GET", auth_required=True):
def on_http_request(path, name=None):
def real_decorator(func):
func.route = True
func.path = path
func.name = name
func.method = method
return func
return real_decorator
if auth_required is True:
return composed(
on_http_request(path, name),
auth.auth_required
)
else:
return composed(
on_http_request(path, name)
)
def on_websocket_message(path, name=None):
def real_decorator(func):
func.ws = True
func.key = path
func.name = name
return func
return real_decorator
def on_event(topic):
def real_decorator(func):
func.eventbus = True
func.topic = topic
2018-11-29 21:59:08 +01:00
func.c = None
2018-11-01 19:50:04 +01:00
return func
return real_decorator
def action(key, parameters):
def real_decorator(func):
func.action = True
2018-11-29 21:59:08 +01:00
2018-11-01 19:50:04 +01:00
func.key = key
func.parameters = parameters
return func
return real_decorator
def on_mqtt_message(topic):
def real_decorator(func):
func.mqtt = True
func.topic = topic
return func
return real_decorator
def background_task(name, interval):
def real_decorator(func):
func.background_task = True
func.name = name
func.interval = interval
return func
return real_decorator
2018-11-04 00:47:26 +01:00
def on_startup(name, order=0):
def real_decorator(func):
func.on_startup = True
func.name = name
func.order = order
return func
return real_decorator
2018-11-01 19:50:04 +01:00
def entry_exit(f):
def new_f():
2018-11-04 00:47:26 +01:00
2018-11-01 19:50:04 +01:00
f()
2018-11-04 00:47:26 +01:00
2018-11-01 19:50:04 +01:00
return new_f