Load list of module before loading code

This commit is contained in:
Benjamin Bohard 2020-03-26 12:04:28 +01:00
parent e475dffc95
commit 93b61176d8
4 changed files with 46 additions and 19 deletions

View File

@ -1,13 +1,3 @@
from pkg_resources import iter_entry_points from .http import get_app, services
class Service:
pass
services = Service()
for ep in iter_entry_points(group='risotto_services'):
setattr(services, ep.name, ep.load())
def list_modules():
return services
from .http import get_app
__ALL__ = ('get_app', 'services') __ALL__ = ('get_app', 'services')

View File

@ -2,7 +2,7 @@ from .config import get_config
from .dispatcher import dispatcher from .dispatcher import dispatcher
from .context import Context from .context import Context
from .remote import remote from .remote import remote
from .services import list_modules from . import services
from .utils import _ from .utils import _
@ -11,7 +11,7 @@ class Controller:
""" """
def __init__(self, def __init__(self,
test: bool): test: bool):
self.risotto_modules = list_modules() self.risotto_modules = services.get_list()
async def call(self, async def call(self,
uri: str, uri: str,

View File

@ -11,13 +11,13 @@ from .error import CallError, NotAllowedError, RegistrationError
from .message import get_messages from .message import get_messages
from .logger import log from .logger import log
from .config import get_config from .config import get_config
from .services import list_modules, load_submodules from . import services
extra_routes = {} extra_routes = {}
RISOTTO_MODULES = list_modules() RISOTTO_MODULES = services.get_list()
def create_context(request): def create_context(request):
@ -124,7 +124,7 @@ async def get_app(loop):
""" build all routes """ build all routes
""" """
global extra_routes global extra_routes
load_submodules(dispatcher) services.link_to_dispatcher(dispatcher)
app = Application(loop=loop) app = Application(loop=loop)
routes = [] routes = []
default_storage.engine('dictionary') default_storage.engine('dictionary')

View File

@ -3,15 +3,52 @@ from inspect import signature
from typing import Callable, Optional from typing import Callable, Optional
import asyncpg import asyncpg
from json import dumps, loads from json import dumps, loads
import risotto
from .utils import _ from .utils import _
from .error import RegistrationError from .error import RegistrationError
from .message import get_messages from .message import get_messages
from .context import Context from .context import Context
from .config import get_config from .config import get_config
from .logger import log from .logger import log
from .services import list_modules from pkg_resources import iter_entry_points
class Services():
modules_list = []
modules_loaded = False
def load_modules(self):
for entry_point in iter_entry_points(group='risotto_services'):
setattr(self, entry_point.name, entry_point.load())
self.modules_loaded = True
def list_modules(self):
for entry_point in iter_entry_points(group='risotto_services'):
self.modules_list.append(entry_point.name)
def get_modules(self):
if not self.modules_loaded:
self.load_modules()
return [(m, getattr(self, m)) for m in self.modules_list]
def get_list(self):
return self.modules_list
def link_to_dispatcher(self,
dispatcher,
validate: bool=True,
test: bool=False,
):
for module_name, module in self.get_modules():
dispatcher.set_module(module_name,
module,
test)
if validate:
dispatcher.validate()
services = Services()
services.list_modules()
setattr(risotto, 'services', services)
def register(uris: str, def register(uris: str,
notification: str=None): notification: str=None):
@ -37,7 +74,7 @@ class RegisterDispatcher:
# postgresql pool # postgresql pool
self.pool = None self.pool = None
# load tiramisu objects # load tiramisu objects
self.risotto_modules = list_modules() self.risotto_modules = services.get_list()
messages, self.option = get_messages(self.risotto_modules) messages, self.option = get_messages(self.risotto_modules)
# list of uris with informations: {"v1": {"module_name.xxxxx": yyyyyy}} # list of uris with informations: {"v1": {"module_name.xxxxx": yyyyyy}}
self.messages = {} self.messages = {}