risotto/src/risotto/controller.py

60 lines
2.1 KiB
Python

from .config import get_config
from .dispatcher import dispatcher
from .context import Context
from .remote import remote
from . import services
from .utils import _
class Controller:
"""Common controller used to add a service in Risotto
"""
def __init__(self,
test: bool):
self.risotto_modules = services.get_services_list()
async def call(self,
uri: str,
risotto_context: Context,
*args,
**kwargs):
""" a wrapper to dispatcher's call"""
version, module, message = uri.split('.', 2)
uri = module + '.' + message
if args:
raise ValueError(_(f'the URI "{uri}" can only be called with keyword arguments'))
if module not in self.risotto_modules:
return await remote.remote_call(module,
version,
message,
kwargs)
return await dispatcher.call(version,
uri,
risotto_context,
**kwargs)
async def publish(self,
uri: str,
risotto_context: Context,
*args,
**kwargs):
""" a wrapper to dispatcher's publish"""
version, module, submessage = uri.split('.', 2)
version, message = uri.split('.', 1)
if args:
raise ValueError(_(f'the URI "{uri}" can only be published with keyword arguments'))
if module not in self.risotto_modules:
await remote.remote_call(module,
version,
submessage,
kwargs)
else:
await dispatcher.publish(version,
message,
risotto_context,
**kwargs)
async def on_join(self,
risotto_context):
pass