Пример #1
0
    def add_async_utility(self, key, config, loop=None):
        if key in self._async_utilities:
            logger.warn(
                f'Utility already registered {key}')
            return

        interface = import_class(config['provides'])
        factory = import_class(config['factory'])
        try:
            utility_object = lazy_apply(
                factory, config.get('settings', {}),
                loop=loop or self._loop)
        except Exception:
            logger.error('Error initializing utility {}'.format(repr(factory)),
                         exc_info=True)
            raise
        provide_utility(utility_object, interface)
        if hasattr(utility_object, 'initialize'):
            task = asyncio.ensure_future(
                lazy_apply(utility_object.initialize, app=self.app),
                loop=loop or self._loop)
        else:
            task = None
            logger.warn(f'No initialize method found on {utility_object} object')
        self.add_async_task(key, task, config)
Пример #2
0
    def add_async_utility(self, key, config, loop=None):
        if key in self._async_utilities:
            logger.warn(f'Utility already registered {key}')
            return

        interface = import_class(config['provides'])
        factory = import_class(config['factory'])
        try:
            utility_object = lazy_apply(factory,
                                        config.get('settings', {}),
                                        loop=loop or self._loop)
        except Exception:
            logger.error('Error initializing utility {}'.format(repr(factory)),
                         exc_info=True)
            raise
        provide_utility(utility_object, interface)
        if hasattr(utility_object, 'initialize'):
            task = asyncio.ensure_future(lazy_apply(utility_object.initialize,
                                                    app=self.app),
                                         loop=loop or self._loop)
        else:
            task = None
            logger.warn(
                f'No initialize method found on {utility_object} object')
        self.add_async_task(key, task, config)
Пример #3
0
def load_application(module, root, settings):
    # includeme function
    if hasattr(module, 'includeme'):
        lazy_apply(module.includeme, root, settings)
    # app_settings
    if hasattr(module, 'app_settings') and app_settings != module.app_settings:
        update_app_settings(module.app_settings)
    # services
    configure.load_all_configurations(root.config, module.__name__)
Пример #4
0
async def close_utilities(app):
    for utility in getAllUtilitiesRegisteredFor(IAsyncUtility):
        if hasattr(utility, 'finalize'):
            asyncio.ensure_future(lazy_apply(utility.finalize, app=app),
                                  loop=app.loop)
    for db in app.router._root:
        if IDatabase.providedBy(db[1]):
            await db[1]._db.finalize()
Пример #5
0
    def load_application(self, module):
        # includeme function
        if hasattr(module, 'includeme'):
            lazy_apply(module.includeme, self.root, self.settings)
        # app_settings
        if hasattr(module, 'app_settings') and app_settings != module.app_settings:
            update_app_settings(module.app_settings)

        # exclude configuration from sub packages that are registered
        # as applications
        excluded_modules = [
            module_name for module_name in
            set(self.applications) - set([module.__name__])
            if not module.__name__.startswith(module_name)]

        # services
        return configure.load_all_configurations(
            self.root.config, module.__name__, excluded_modules)
Пример #6
0
def test_lazy_apply():
    assert utils.lazy_apply(_test_empty_func, 'blah', foo='bar')
    assert utils.lazy_apply(_test_some_args, 'foo', 'bar') == ('foo', 'bar')
    assert utils.lazy_apply(_test_some_args, 'foo', 'bar', 'ldkfks', 'dsflk') == ('foo', 'bar')
    assert utils.lazy_apply(_test_some_kwargs, 'foo', bar='bar') == ('foo', 'bar')
    assert utils.lazy_apply(_test_some_kwargs, 'foo', bar='bar', rsdfk='ldskf') == ('foo', 'bar')
    assert (utils.lazy_apply(_test_some_stars, 'foo', 'blah', bar='bar', another='another') ==
            ('foo', 'bar', {'another': 'another'}))
Пример #7
0
    def load_application(self, module):
        # includeme function
        if hasattr(module, "includeme"):
            lazy_apply(module.includeme, self.root, self.settings)
        # app_settings
        if hasattr(module,
                   "app_settings") and app_settings != module.app_settings:
            update_app_settings(module.app_settings)

        # exclude configuration from sub packages that are registered
        # as applications
        excluded_modules = [
            module_name
            for module_name in set(self.applications) - set([module.__name__])
            if not module.__name__.startswith(module_name)
        ] + [
            module_name for module_name in self.contrib_apps
            if module_name != module.__name__
        ]
        # services
        return configure.load_all_configurations(self.root.config,
                                                 module.__name__,
                                                 excluded_modules)
Пример #8
0
    def add_async_utility(
        self,
        key: str,
        config: typing.Dict,
        loop: typing.Optional[asyncio.AbstractEventLoop] = None
    ) -> typing.Optional[typing.Tuple[typing.Any,
                                      typing.Optional[asyncio.Future]]]:
        if key in self._async_utilities:
            logger.warn(f'Utility already registered {key}')
            return None

        interface = import_class(config['provides'])
        factory = import_class(config['factory'])
        try:
            utility_object = lazy_apply(factory,
                                        config.get('settings', {}),
                                        loop=loop or self._loop)
        except Exception:
            logger.error('Error initializing utility {}'.format(repr(factory)),
                         exc_info=True)
            raise
        alsoProvides(utility_object, interface)
        kw = {}
        if 'name' in config:
            kw['name'] = config['name']
        provide_utility(utility_object, interface, **kw)
        if hasattr(utility_object, 'initialize'):
            func = lazy_apply(utility_object.initialize, app=self.app)

            task = asyncio.ensure_future(notice_on_error(key, func),
                                         loop=loop or self._loop)
            self.add_async_task(key, task, config)
            return utility_object, task
        else:
            logger.info(
                f'No initialize method found on {utility_object} object')
            return None
Пример #9
0
def test_lazy_apply():
    assert utils.lazy_apply(_test_empty_func, "blah", foo="bar")
    assert utils.lazy_apply(_test_some_args, "foo", "bar") == ("foo", "bar")
    assert utils.lazy_apply(_test_some_args, "foo", "bar", "ldkfks", "dsflk") == ("foo", "bar")
    assert utils.lazy_apply(_test_some_kwargs, "foo", bar="bar") == ("foo", "bar")
    assert utils.lazy_apply(_test_some_kwargs, "foo", bar="bar", rsdfk="ldskf") == ("foo", "bar")
    assert utils.lazy_apply(_test_some_stars, "foo", "blah", bar="bar", another="another") == (
        "foo",
        "bar",
        {"another": "another"},
    )
Пример #10
0
def test_lazy_apply():
    assert utils.lazy_apply(_test_empty_func, 'blah', foo='bar')
    assert utils.lazy_apply(_test_some_args, 'foo', 'bar') == ('foo', 'bar')
    assert utils.lazy_apply(_test_some_args, 'foo', 'bar', 'ldkfks',
                            'dsflk') == ('foo', 'bar')
    assert utils.lazy_apply(
        _test_some_kwargs, 'foo', bar='bar') == ('foo', 'bar')
    assert utils.lazy_apply(
        _test_some_kwargs, 'foo', bar='bar', rsdfk='ldskf') == ('foo', 'bar')
    assert (utils.lazy_apply(
        _test_some_stars, 'foo', 'blah', bar='bar',
        another='another') == ('foo', 'bar', {
            'another': 'another'
        }))
Пример #11
0
 async def _call_after_commit_hooks(self, status=True):
     # Avoid to abort anything at the end if no hooks are registred.
     if not self._after_commit:
         return
     # Call all hooks registered, allowing further registrations
     # during processing.  Note that calls to addAterCommitHook() may
     # add additional hooks while hooks are running, and iterating over a
     # growing list is well-defined in Python.
     for hook, args, kws in self._after_commit:
         # The first argument passed to the hook is a Boolean value,
         # true if the commit succeeded, or false if the commit aborted.
         try:
             result = lazy_apply(hook, status, *args, **kws)
             if asyncio.iscoroutine(result):
                 await result
         except Exception:
             # We need to catch the exceptions if we want all hooks
             # to be called
             logger.error("Error in after commit hook exec in %s ", hook, exc_info=sys.exc_info())
     self._after_commit = []
Пример #12
0
 async def _call_after_commit_hooks(self, status=True):
     # Avoid to abort anything at the end if no hooks are registred.
     if not self._after_commit:
         return
     # Call all hooks registered, allowing further registrations
     # during processing.  Note that calls to addAterCommitHook() may
     # add additional hooks while hooks are running, and iterating over a
     # growing list is well-defined in Python.
     for hook, args, kws in self._after_commit:
         # The first argument passed to the hook is a Boolean value,
         # true if the commit succeeded, or false if the commit aborted.
         try:
             result = lazy_apply(hook, status, *args, **kws)
             if asyncio.iscoroutine(result):
                 await result
         except Exception:
             # We need to catch the exceptions if we want all hooks
             # to be called
             logger.error("Error in after commit hook exec in %s ",
                          hook, exc_info=sys.exc_info())
     self._after_commit = []
Пример #13
0
def get_settings():
    settings = deepcopy(TESTING_SETTINGS)
    for func in _configurators:
        lazy_apply(func, settings, _configurators)
    return settings
Пример #14
0
def make_app(config_file=None, settings=None, loop=None, server_app=None):
    app_settings.update(_delayed_default_settings)

    if loop is None:
        loop = asyncio.get_event_loop()

    loop.set_task_factory(aiotask_context.task_factory)

    if config_file is not None:
        with open(config_file, 'r') as config:
            settings = json.load(config)
    elif settings is None:
        raise Exception('Neither configuration or settings')

    # Create root Application
    root = ApplicationRoot(config_file)
    provide_utility(root, IApplication, 'root')

    # Initialize global (threadlocal) ZCA configuration
    config = root.config = ConfigurationMachine()

    import guillotina
    import guillotina.db.factory
    import guillotina.db.writer
    import guillotina.db.db
    configure.scan('guillotina.translation')
    configure.scan('guillotina.renderers')
    configure.scan('guillotina.api')
    configure.scan('guillotina.content')
    configure.scan('guillotina.registry')
    configure.scan('guillotina.auth')
    configure.scan('guillotina.json')
    configure.scan('guillotina.behaviors')
    configure.scan('guillotina.languages')
    configure.scan('guillotina.permissions')
    configure.scan('guillotina.security.security_local')
    configure.scan('guillotina.security.policy')
    configure.scan('guillotina.auth.participation')
    configure.scan('guillotina.catalog.index')
    configure.scan('guillotina.catalog.catalog')
    configure.scan('guillotina.framing')
    configure.scan('guillotina.files')
    configure.scan('guillotina.annotations')
    configure.scan('guillotina.constraintypes')
    configure.scan('guillotina.subscribers')
    configure.scan('guillotina.db.strategies')
    configure.scan('guillotina.db.cache')
    load_application(guillotina, root, settings)
    config.execute_actions()
    config.commit()

    for module_name in settings.get('applications', []):
        config.begin(module_name)
        load_application(resolve_dotted_name(module_name), root, settings)
        config.execute_actions()
        config.commit()

    # XXX we clear now to save some memory
    # it's unclear to me if this is necesary or not but it seems to me that
    # we don't need things registered in both components AND here.
    configure.clear()

    # update *after* plugins loaded
    update_app_settings(settings)

    if 'logging' in app_settings:
        logging.config.dictConfig(app_settings['logging'])

    # Make and initialize aiohttp app
    if server_app is None:
        server_app = make_aiohttp_application()
    root.app = server_app
    server_app.root = root
    server_app.config = config

    content_type = ContentNegotiatorUtility('content_type',
                                            app_settings['renderers'].keys())
    language = ContentNegotiatorUtility('language',
                                        app_settings['languages'].keys())

    provide_utility(content_type, IContentNegotiation, 'content_type')
    provide_utility(language, IContentNegotiation, 'language')

    for database in app_settings['databases']:
        for key, dbconfig in database.items():
            factory = get_utility(IDatabaseConfigurationFactory,
                                  name=dbconfig['storage'])
            if asyncio.iscoroutinefunction(factory):
                future = asyncio.ensure_future(factory(key, dbconfig,
                                                       server_app),
                                               loop=loop)

                loop.run_until_complete(future)
                root[key] = future.result()
            else:
                root[key] = factory(key, dbconfig)

    for key, file_path in list_or_dict_items(app_settings['static']):
        path = resolve_path(file_path).resolve()
        if not path.exists():
            raise Exception('Invalid static directory {}'.format(file_path))
        if path.is_dir():
            root[key] = StaticDirectory(path)
        else:
            root[key] = StaticFile(path)
    for key, file_path in list_or_dict_items(app_settings['jsapps']):
        path = resolve_path(file_path).resolve()
        if not path.exists() or not path.is_dir():
            raise Exception('Invalid jsapps directory {}'.format(file_path))
        root[key] = JavaScriptApplication(path)

    root.set_root_user(app_settings['root_user'])

    if RSA is not None and not app_settings.get('rsa'):
        key = RSA.generate(2048)
        pub_jwk = {'k': key.publickey().exportKey('PEM')}
        priv_jwk = {'k': key.exportKey('PEM')}
        app_settings['rsa'] = {'pub': pub_jwk, 'priv': priv_jwk}

    # Set router root
    server_app.router.set_root(root)

    for utility in get_all_utilities_registered_for(IAsyncUtility):
        # In case there is Utilties that are registered
        if hasattr(utility, 'initialize'):
            task = asyncio.ensure_future(lazy_apply(utility.initialize,
                                                    app=server_app),
                                         loop=loop)
            root.add_async_task(utility, task, {})
        else:
            logger.warn(f'No initialize method found on {utility} object')

    server_app.on_cleanup.append(close_utilities)

    for util in app_settings['utilities']:
        root.add_async_utility(util, loop=loop)

    # Load cached Schemas
    load_cached_schema()

    optimize_settings(app_settings)

    return server_app
Пример #15
0
 async def _call_before_commit_hooks(self):
     for hook, args, kws in self._before_commit:
         result = lazy_apply(hook, *args, **kws)
         if asyncio.iscoroutine(result):
             await result
     self._before_commit = []
Пример #16
0
 async def _call_before_commit_hooks(self):
     for hook, args, kws in self._before_commit:
         result = lazy_apply(hook, *args, **kws)
         if asyncio.iscoroutine(result):
             await result
     self._before_commit = []
Пример #17
0
async def make_app(config_file=None,
                   settings=None,
                   loop=None,
                   server_app=None):

    # reset app_settings
    app_settings.clear()
    app_settings.update(default_settings)

    if loop is None:
        loop = asyncio.get_event_loop()

    # chainmap task factory is actually very important
    # default task factory uses inheritance in a way
    # that bubbles back down. So it's possible for a sub-task
    # to clear out the request of the parent task
    loop.set_task_factory(aiotask_context.chainmap_task_factory)

    if config_file is not None:
        with open(config_file, 'r') as config:
            settings = json.load(config)
    elif settings is None:
        raise Exception('Neither configuration or settings')

    # Create root Application
    root = ApplicationRoot(config_file, loop)
    provide_utility(root, IApplication, 'root')

    # Initialize global (threadlocal) ZCA configuration
    config = root.config = ConfigurationMachine()

    import guillotina
    import guillotina.db.factory
    import guillotina.db.writer
    import guillotina.db.db
    configure.scan('guillotina.renderers')
    configure.scan('guillotina.api')
    configure.scan('guillotina.content')
    configure.scan('guillotina.registry')
    configure.scan('guillotina.auth')
    configure.scan('guillotina.json')
    configure.scan('guillotina.behaviors')
    configure.scan('guillotina.languages')
    configure.scan('guillotina.permissions')
    configure.scan('guillotina.security.security_local')
    configure.scan('guillotina.security.policy')
    configure.scan('guillotina.auth.participation')
    configure.scan('guillotina.catalog.index')
    configure.scan('guillotina.catalog.catalog')
    configure.scan('guillotina.files')
    configure.scan('guillotina.annotations')
    configure.scan('guillotina.constraintypes')
    configure.scan('guillotina.subscribers')
    configure.scan('guillotina.db.strategies')
    configure.scan('guillotina.db.cache')
    configure.scan('guillotina.exc_resp')
    configure.scan('guillotina.fields')
    load_application(guillotina, root, settings)
    config.execute_actions()
    config.commit()

    configured = ['guillotina']
    for module_name in settings.get('applications') or []:
        configure_application(module_name, config, root, settings, configured)

    apply_concrete_behaviors()

    # update *after* plugins loaded
    update_app_settings(settings)

    if 'logging' in app_settings:
        logging.config.dictConfig(app_settings['logging'])

    # Make and initialize aiohttp app
    if server_app is None:
        server_app = make_aiohttp_application()
    root.app = server_app
    server_app.root = root
    server_app.config = config

    optimize_settings(app_settings)

    await notify(ApplicationConfiguredEvent(server_app, loop))

    for key, dbconfig in list_or_dict_items(app_settings['databases']):
        factory = get_utility(IDatabaseConfigurationFactory,
                              name=dbconfig['storage'])
        root[key] = await factory(key, dbconfig, loop)
        await notify(DatabaseInitializedEvent(root[key]))

    for key, file_path in list_or_dict_items(app_settings['static']):
        path = resolve_path(file_path).resolve()
        if not path.exists():
            raise Exception('Invalid static directory {}'.format(file_path))
        if path.is_dir():
            root[key] = StaticDirectory(path)
        else:
            root[key] = StaticFile(path)

    for key, file_path in list_or_dict_items(app_settings['jsapps']):
        path = resolve_path(file_path).resolve()
        if not path.exists() or not path.is_dir():
            raise Exception('Invalid jsapps directory {}'.format(file_path))
        root[key] = JavaScriptApplication(path)

    root.set_root_user(app_settings['root_user'])

    if RSA is not None and not app_settings.get('rsa'):
        key = RSA.generate(2048)
        pub_jwk = {'k': key.publickey().exportKey('PEM')}
        priv_jwk = {'k': key.exportKey('PEM')}
        app_settings['rsa'] = {'pub': pub_jwk, 'priv': priv_jwk}

    # Set router root
    server_app.router.set_root(root)

    for utility in get_all_utilities_registered_for(IAsyncUtility):
        # In case there is Utilties that are registered
        if hasattr(utility, 'initialize'):
            task = asyncio.ensure_future(lazy_apply(utility.initialize,
                                                    app=server_app),
                                         loop=loop)
            root.add_async_task(utility, task, {})
        else:
            logger.warn(f'No initialize method found on {utility} object')

    server_app.on_cleanup.append(cleanup_app)

    for util in app_settings['utilities']:
        root.add_async_utility(util, loop=loop)

    # Load cached Schemas
    load_cached_schema()

    await notify(ApplicationInitializedEvent(server_app, loop))

    return server_app