Пример #1
0
class Request:
    """ State and methods related to the currently-processed entity """

    context: Resource = injected(Context)
    root: Root = injected(IRoot)

    def static_url(self, asset_path: str) -> str:
        path = relative_static_path(self.context, asset_path)
        return path

    # noinspection PyMethodMayBeStatic
    def resource_path(self, resource: Resource) -> str:
        return resource_path(resource)

    def relative_path(self, target: Resource) -> str:
        return relative_path(self.root, self.context, target)
Пример #2
0
class Greeter:
    customer: Customer = injected(Context)
    name: str = 'Mary'

    def __call__(self):
        msg = f'Hello {self.customer.name} my name is {self.name}'
        return msg
Пример #3
0
class FrenchGreeter:
    customer: FrenchCustomer = injected(Context)
    name: str = 'Henri'

    def __call__(self):
        msg = f'Salut {self.customer.name} je m\'apelle {self.name}'
        return msg
Пример #4
0
class JinjaRenderer:
    template_dirs: TemplateDirs = injected(IConfiguration,
                                           attr='template_dirs')
    environment: Environment = field(init=False)

    def __post_init__(self):
        choice_loader = ChoiceLoader([
            PackageLoader(template_dir[0], template_dir[1])
            for template_dir in self.template_dirs
        ])
        self.environment = Environment(loader=choice_loader)

    def render(self, context: Dict, template_name: str,
               container: ServiceContainer) -> Markup:
        """ Given a dataclass, flatten it and render with jinja2 template """

        # Always put the wrapped components into the template context
        from ..component import IWrapComponents
        wrapped_components: Dict[str, Type] = container.get(IWrapComponents)

        context.update(wrapped_components)

        template: Template = self.environment.get_or_select_template(
            template_name)
        result = template.render(**context)
        m = Markup(result)
        return m
Пример #5
0
class DummyIFWA:
    target: str = injected(Source, attr='name')

    # Use this to bundle the monkeypatch
    @staticmethod
    def d_get(key, name=''):
        return Source()
Пример #6
0
class DummyInjectedSingleton:
    target: Source = injected(Source)

    # Use this to bundle the monkeypatch
    @staticmethod
    def d_get(key, name=''):
        return Source()
Пример #7
0
class DummyIFMA:
    target: str = injected(Source, attr='XXX')

    # Use this to bundle the monkeypatch
    @staticmethod
    def d_get(key, name=''):
        if key is Source:
            return Source()
        raise TypeError()
Пример #8
0
class FrenchGreeter:
    """ A greeter to use when the customer (context) is French """

    settings: Settings
    customer: FrenchCustomer = injected(Context)
    name: str = 'Henri'

    def __call__(self):
        punctuation = self.settings.punctuation
        m = f'je m\'apelle {self.name}{punctuation}'
        return f'Salut {self.customer.name} {m}'
Пример #9
0
class Greeter:
    """ A basic greeter """

    settings: Settings
    customer: Customer = injected(Context)
    name: str = 'Mary'

    def __call__(self):
        punctuation = self.settings.punctuation
        m = f'my name is {self.name}{punctuation}'
        return f'Hello {self.customer.name} {m}'
Пример #10
0
class DocumentView:
    title: str = injected(Context, attr='title')
    template: str = 'documentview.jinja2'
Пример #11
0
class CollectionView:
    title: str = injected(Context, attr='title')
    template: str = 'collectionview.jinja2'
Пример #12
0
class RootView:
    title: str = injected(Context, attr='title')
    template: str = 'rootview.jinja2'
Пример #13
0
 class Dummy:
     context: DummyCustomer = injected(Context)
Пример #14
0
 class Dummy:
     context: DummyCustomer = injected(ServiceContainer, attr='context')