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)
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
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
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
class DummyIFWA: target: str = injected(Source, attr='name') # Use this to bundle the monkeypatch @staticmethod def d_get(key, name=''): return Source()
class DummyInjectedSingleton: target: Source = injected(Source) # Use this to bundle the monkeypatch @staticmethod def d_get(key, name=''): return Source()
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()
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}'
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}'
class DocumentView: title: str = injected(Context, attr='title') template: str = 'documentview.jinja2'
class CollectionView: title: str = injected(Context, attr='title') template: str = 'collectionview.jinja2'
class RootView: title: str = injected(Context, attr='title') template: str = 'rootview.jinja2'
class Dummy: context: DummyCustomer = injected(Context)
class Dummy: context: DummyCustomer = injected(ServiceContainer, attr='context')