Пример #1
0
    def initialize_templates(self, paths, options) -> None:
        """Initialize and configure template system"""
        # Get the template singleton from the IoC
        templates = uvicore.ioc.make('uvicore.http.templating.jinja.Jinja')

        # Add all packages view paths
        for path in paths:
            templates.include_path(module.location(path))

        # Add all packages deep_merged template options
        if 'context_functions' in options:
            for name, method in options['context_functions'].items():
                templates.include_context_function(name, method)
        if 'context_filters' in options:
            for name, method in options['context_filters'].items():
                templates.include_context_filter(name, method)
        if 'filters' in options:
            for name, method in options['filters'].items():
                templates.include_filter(name, method)
        if 'tests' in options:
            for name, method in options['tests'].items():
                templates.include_test(name, method)

        # Initialize template system
        templates.init()
Пример #2
0
def test_location():
    # Find folder of package
    x = module.location('uvicore.container')
    assert 'uvicore/container' in x

    # Find folder of namespace package
    x = module.location('uvicore.foundation')
    assert 'uvicore/foundation' in x

    # Find folder of module (file)
    x = module.location('uvicore.foundation.config.package')
    assert 'uvicore/foundation/config' in x

    # Find folder of method/class
    x = module.location('uvicore.foundation.config.package.config')
    assert 'uvicore/foundation/config' in x
Пример #3
0
    def configure_webserver(self, web_server) -> None:
        """Configure the webserver with public, asset and view paths and template options"""

        # Ignore if web_server was never fired up
        if not web_server: return

        # Loop each package with an HTTP definition and add to our HTTP server
        public_paths = []
        asset_paths = []
        view_paths = []
        template_options = Dict()
        for package in uvicore.app.packages.values():
            #if not 'http' in package: continue
            if not 'web' in package: continue

            # Append public paths for later
            for path in package.web.public_paths:
                path_location = module.location(path)
                if path_location not in public_paths:
                    public_paths.append(path_location)

            # Append asset paths for later
            for path in package.web.asset_paths:
                path_location = module.location(path)
                if path_location not in asset_paths:
                    asset_paths.append(path_location)

            # Append template paths
            for path in package.web.view_paths or []:
                view_paths.append(path)

            # Deep merge template options
            template_options.merge(package.web.template_options or {})

        # Mount all static paths
        self.mount_static(web_server, public_paths, asset_paths)

        # Initialize new template environment from the options above
        self.initialize_templates(view_paths, template_options)
Пример #4
0
    def _register_providers(self, app_config: Dict) -> None:
        """Register all providers by calling each ServiceProviders register() method"""

        for package_name, service in self.providers.items():
            # Example:
            # package_name = uvicore.configuration
            # service = {'provider': 'uvicore.configuration.services.Configuration'}

            # Start a new package definition
            #x = OrderedDict()
            #x.dotset(package_name, package.Definition({
            self._packages[package_name] = Package({
                'name':
                package_name,
                'short_name':
                package_name.split('.')[-1]
                if '.' in package_name else package_name,
                'vendor':
                package_name.split('.')[0],  # Works fine even if no .
                'main':
                True if package_name == self.main else False,
                'path':
                location(package_name),
            })
            #self._packages.merge(x)
            #self._packages[package_name] = package.Definition({
            #dd(self.packages)

            # Instantiate the provider and call the register() method
            provider = load(service['provider']).object(
                app=self,
                name=package_name,
                package=None,  # Not available in register()
                app_config=app_config,
                package_config=self._get_package_config(package_name, service),
            )
            provider.register()

        # Complete registration
        self._registered = True
        #uvicore.events.dispatch('uvicore.foundation.events.app.Registered')
        #uvicore.events.dispatch(uvicore.foundation.events.app.Registered())
        #uvicore.events.dispatch('uvicore.foundation.events.app.Registered', {'test': 'test1'})
        events.Registered().dispatch()
Пример #5
0
 def _build_package(self, name: str, custom_config: Dict):
     return Package(
         name=name,
         location=location(name),
         main=True if name == self.main else False,
         web_route_prefix=dotget(custom_config, 'route.web_prefix'),
         api_route_prefix=dotget(custom_config, 'route.api_prefix'),
         view_paths=[],
         asset_paths=[],
         template_options={},
         register_web_routes=custom_config.get('register_web_routes')
         or True,
         register_api_routes=custom_config.get('register_api_routes')
         or True,
         register_views=custom_config.get('register_views') or True,
         register_assets=custom_config.get('register_assets') or True,
         register_commands=custom_config.get('register_commands') or True,
         connection_default=dotget(custom_config, 'database.default'),
         connections=self._build_package_connections(custom_config),
         models=[],
         seeders=[],
         custom1='custom1 override here!!!')