Ejemplo n.º 1
0
def render_view(
        registry: ServiceRegistry,
        view: Optional[View] = None,  # Might be in the registry already
        context: Optional[Any] = None,
        resource: Optional[Resource] = None,
        singletons: Singletons = tuple(),
) -> str:
    """ Find/render view with optional context/resource

    Any needed components must be registered before passing in registry.
    """

    if view is not None:
        if context is not None:
            register_view(registry, view, context=context.__class__)
        else:
            register_view(registry, view)
    container = registry.create_container(context=context)

    for service, iface in singletons:
        container.register_singleton(service, iface)

    if resource is not None:
        container.register_singleton(resource, Resource)

    vdom = container.get(View)
    result = render(vdom, container=container)
    return result
Ejemplo n.º 2
0
    def render(
        self,
        container: Optional[ServiceContainer] = None,
        resource: Optional[Union[Resource, Any]] = None,
        view_name: Optional[str] = None,
    ) -> str:
        """ Render a vdom via a view from a container """

        # If a container was passed in, use it as the basis for a render
        # container. Otherwise, use the site container and bind to it.
        if container:
            this_container = container
        else:
            this_container = self.registry.create_container(context=resource)
            # If we were passed a resource, register it as a singleton in
            # the container.
            if resource is not None:
                this_container.register_singleton(resource, Resource)

        # Sometimes we want to use named views
        if view_name:
            this_view = this_container.get(View, name=view_name)
        else:
            this_view = this_container.get(View)

        # Now render a vdom
        this_vdom = this_view()
        return render(this_vdom, container=this_container)
Ejemplo n.º 3
0
def main():
    # The app
    registry = InjectorRegistry()
    registry.scan()

    # Per "request"
    container = registry.create_injectable_container()
    result = render(html('<{Greeting}/>'), container)

    expected = '<h1>Hello viewdom_wired</h1>'
    return expected, result
Ejemplo n.º 4
0
def main():
    # The app
    registry = InjectorRegistry()
    registry.scan()

    # Per "request"
    container = registry.create_injectable_container()
    result = render(html('<{Greeting} first_name="World"/>'), container)

    expected = '<h1>Hello World</h1>'
    return expected, result
Ejemplo n.º 5
0
def main():
    # The app
    registry = InjectorRegistry()
    [registry.scan(plugin) for plugin in plugins]
    registry.scan()

    # Per "request"
    container = registry.create_injectable_container()
    result = render(html('<{Greeting}><span>Children</span><//>'), container)

    expected = '<h1>Hello Site</h1>'
    return expected, result
Ejemplo n.º 6
0
def main():
    # The app
    registry = InjectorRegistry()
    [registry.scan(plugin) for plugin in plugins]
    registry.scan()

    # Per "request"
    customer = Customer(name='Mary')
    container = registry.create_injectable_container(context=customer, )
    result = render(html('<{Greeting}/>'), container)

    expected = '<h1>Hello Mary</h1>'
    return expected, result
Ejemplo n.º 7
0
def main():
    # The app
    registry = InjectorRegistry()
    # Scan the app, then the plugins, then the site
    registry.scan()
    [registry.scan(plugin) for plugin in site.plugins]
    registry.scan(site)

    # Per "request"
    container = registry.create_injectable_container()
    LOGO_SRC = 'logo.png'  # noqa: F841
    result = render(html('<{Navbar} logo_src={LOGO_SRC} />'), container)

    expected = '<nav><img src="logo.png"/></nav>'
    return expected, result
Ejemplo n.º 8
0
def render_vdom(
        registry: ServiceRegistry,
        vdom: VDOM,
        context: Optional[Any] = None,
        resource: Optional[Resource] = None,
        singletons: Singletons = tuple(),
) -> str:
    """ Render a VDOM to string with optional context/resource """

    container = registry.create_container(context=context)
    for service, iface in singletons:
        container.register_singleton(service, iface)

    if resource is not None:
        container.register_singleton(resource, Resource)
    result = render(vdom, container=container)
    return result
Ejemplo n.º 9
0
def render_component(
        registry: ServiceRegistry,
        component: Component,
        context: Optional[Any] = None,
        resource: Optional[Resource] = None,
        singletons: Singletons = tuple(),
) -> str:
    """ Render a component to string with optional context/resource """

    register_component(registry, component)
    container = registry.create_container(context=context)
    for service, iface in singletons:
        container.register_singleton(service, iface)

    if resource is not None:
        container.register_singleton(resource, Resource)
    vdom = html('<{component} />')
    result = render(vdom, container=container)
    return result
Ejemplo n.º 10
0
def render_template(
        registry: ServiceRegistry,
        template: VDOM,
        context: Optional[Any] = None,
        resource: Optional[Resource] = None,
        singletons: Singletons = tuple(),
) -> str:
    """ Find/render template string with optional context/resource

    Any needed components must be registered before passing in registry.
    """

    container = registry.create_container(context=context)

    for service, iface in singletons:
        container.register_singleton(service, iface)

    if resource is not None:
        container.register_singleton(resource, Resource)

    result = render(template, container=container)
    return result
def test_wired_renderer_second(registry: InjectorRegistry):
    container = registry.create_injectable_container(context=SecondContext())
    expected = '<h1>Hello World... Second Context</h1>'
    actual = render(html('''<{Heading} person="World"/>'''), container)
    assert expected == actual