class Heading: person: str name: Annotated[str, Context(), Attr('name')] greeting: Annotated[str, Get(Settings), Attr('greeting')] def __call__(self): return html('''<h1>{self.greeting} {self.person}, {self.name}</h1>''')
class Document: """ A block in content, holding most of the info on this resource """ body: Annotated[ Markup, Get(PageContext, attr='body'), ] nosidebar: Annotated[ bool, Get(HTMLConfig), Attr('nosidebar'), ] inner: VDOM = field(init=False) def __post_init__(self): # Alabaster wraps the main content in <div class="bodywrapper"> # if nosidebar is true. Thus, get the main content first, then # insert in the two flavors of response. main_content = html('''\n <{Relbar1}/> <div class="body" role="main"> {self.body} </div> <{Relbar2}/> ''') self.inner = main_content if self.nosidebar else html('<div class="bodywrapper">{main_content}</div>') def __call__(self) -> VDOM: return html('''\n <div class="documentwrapper"> {self.inner} </div> ''')
def test_pipeline_two_attr_attr(french_container): @dataclass class Customer: name: str = 'Some Customer' @dataclass class Config: customer: Customer = Customer() pipeline = (Attr('customer'), Attr('name')) result = process_pipeline( french_container, pipeline, start=Config(), ) assert result == 'Some Customer'
def test_get_then_attr(french_container): get = Get(View) start = View result1 = get(start, french_container) attr = Attr('name') result = attr(result1, french_container) assert 'French View' == result
class Relbar1: """ Relation bar usually at the top. """ show_relbar_top: Annotated[bool, Get(ThemeConfig), Attr('show_relbar_top'), ] show_relbars: Annotated[bool, Get(ThemeConfig), Attr('show_relbar_top'), ] resolved_show_relbars: bool = field(init=False) def __post_init__(self): self.resolved_show_relbars = self.show_relbar_top or self.show_relbars def __call__(self) -> VDOM: return html('''\n <div class="related top"><{RellinkMarkup} /> </div> ''') if self.resolved_show_relbars else []
def target( customer_name: Annotated[ str, Context(), Attr('name'), ], ): return customer_name
def target( view_name: Annotated[ str, Get(Foo), Attr('name'), ] = 'View Name' ): return view_name
def target( view_name: Annotated[ str, Get(View), Attr('name'), ], ): return view_name
class CanonicalLink: baseurl: Annotated[Optional[str], Get(HTMLConfig), Attr('baseurl')] canonical_href: Optional[str] = field(init=False) file_suffix: Annotated[str, Get(HTMLConfig), Attr('file_suffix'), ] pagename: Annotated[str, Get(PageContext, attr='pagename'), ] def __post_init__(self): if self.baseurl: self.canonical_href = join(self.baseurl, self.pagename + self.file_suffix) else: self.canonical_href = None def __call__(self) -> Optional[VDOM]: if self.canonical_href: return html('<link rel="canonical" href={self.canonical_href}/>') return None
def test_pipeline_two(french_container): pipeline = (Get(View), Attr('name')) result = process_pipeline( french_container, pipeline, start=View, ) assert result == 'French View'
class View: site_name: Annotated[ str, Get(BaseSettings), Attr('site_name'), ] @property def name(self): site_name = self.site_name return f'View - {site_name}'
class Footer: """ A block in the body below the content block. """ copyright: Annotated[Optional[str], Get(SphinxConfig), Attr('copyright'), ] has_source: Annotated[bool, Get(HTMLConfig), Attr('has_source'), ] pathto: Annotated[Callable[[str], str], Get(PageContext, attr='pathto'), ] show_powered_by: Annotated[bool, Get(ThemeConfig), Attr('show_powered_by'), ] show_copyright: Annotated[bool, Get(HTMLConfig), Attr('show_copyright'), ] show_sourcelink: Annotated[bool, Get(HTMLConfig), Attr('show_sourcelink'), ] sourcename: Annotated[str, Get(PageContext, attr='sourcename'), ] resolved_copyright: str = field(init=False) resolved_powered_by: VDOM = field(init=False) resolved_page_source: VDOM = field(init=False) def __post_init__(self): self.resolved_copyright = f'© {self.copyright}.' if self.copyright else '' self.resolved_powered_by = html('''\n {'|' if self.copyright else ''} Powered by <a href="http://sphinx-doc.org/">Sphinx</a> ''') if self.show_powered_by else html('') if self.show_sourcelink and self.has_source and self.sourcename: ps = self.pathto(f'_sources/{self.sourcename}') self.resolved_page_source = html('''\n {'|' if self.show_copyright or self.show_powered_by else ''} <a href={ps} rel="nofollow">Page source</a> ''') else: self.resolved_page_source = html('') def __call__(self) -> VDOM: return html('''\n <div class="footer"> {self.resolved_copyright} {self.resolved_powered_by} {self.resolved_page_source} </div> ''')
class FaviconSet: favicons: Annotated[Favicons, Get(ThemeConfig), Attr('favicons')] pathto: Annotated[Callable[[str, int], str], Get(PageContext, attr='pathto'), ] shortcut_href: Optional[str] = field(init=False) png_href: Optional[str] = field(init=False) def __post_init__(self): favicons = self.favicons self.shortcut_href = self.pathto(join('static', favicons.shortcut), 1) if favicons.shortcut else None self.png_href = self.pathto(join('static', favicons.png), 1) if favicons.png else None def __call__(self) -> VDOM: shortcut = html( '<link rel="shortcut icon" type="image/x-icon" href={self.shortcut_href} />' ) if self.shortcut_href else None png = html( '<link rel="shortcut icon" type="image/x-icon" href={self.png_href} />' ) if self.png_href else None precomposed = html( '<link rel="apple-touch-icon-precomposed" type="image/x-icon" href={self.png_href} />' ) if self.png_href else None if self.favicons.sizes: sizes = [] for size in self.favicons.sizes: size_href = self.pathto(join('static', size.filename), 1) size_size = size.size sizes.append( html( '<link rel="apple-touch-icon-precomposed" sizes={size_size} href={size_href} />' )) else: sizes = None return html('''\n {shortcut} {png} {precomposed} {sizes} ''')
class BaseLayout: language: Annotated[ str, Get(SphinxConfig), Attr('language'), ] extrahead: Optional[VDOM] = None doctype: Markup = Markup('<!DOCTYPE html>\n') html_props: Dict[str, str] = field(init=False) def __post_init__(self): self.html_props = dict(lang=self.language) if self.language else dict() def __call__(self) -> VDOM: return html('''\n {self.doctype} <html ...{self.html_props}> <{Head} extrahead={self.extrahead} /> <{Body} /> </html> ''')
class Punctuation: character: Annotated[str, Get(PunctuationCharacter), Attr('symbol'), ] def __call__(self): return html('<span>{self.character}</span>')
class Greeting: name: Annotated[str, Context(), Attr('name')] def __call__(self): return html('<h1>Hello {self.name}</h1>')
class FrenchView: customer_name: Annotated[str, Context(), Attr('name'), ] @property def name(self): return f'Bonjour {self.customer_name}'
class ThirdHeading: """ Double injection but with a plain factory, not injectable """ greeting: Annotated[str, Get(Settings), Attr('greeting')] config: Config # Annotated[Config, Get(Config)]
class Favicon: href: Annotated[str, Get(HTMLConfig), Attr('favicon'), StaticPathTo(), ] def __call__(self) -> VDOM: return html('<link rel="shortcut icon" href={self.href} />')
class Target: customer_name: Annotated[str, Context(), Attr('name'), ] def __call__(self): return self.customer_name
class SecondHeading: person: str name: Annotated[str, Context(), Attr('name')] greeting: Annotated[str, Get(Settings), Attr('greeting')]
class Target: customer_name: Annotated[str, Get(View), Attr('caps_name'), ]
class Target: view_name: Annotated[str, Get(Foo), Attr('name'), ] = 'View Name' def __call__(self): return self.view_name
class Target: customer_name: Annotated[str, Get(View), Attr('name'), ] def __call__(self): return self.customer_name
class FrenchGreeting: name: Annotated[str, Context(), Attr('name')] def __call__(self): return html('<h1>Bonjour {self.name}</h1>')
class DummyView(View): context_title: Annotated[DummyContext, Context(), Attr('title')] resource_title: Annotated[DummyContext, Get(Resource, attr='title')] def __call__(self) -> VDOM: return html('<div>Hello {self.context_title} from {self.resource_title}</div>')
def test_attr(regular_container): attr = Attr('name') previous = FrenchCustomer() result = attr(previous, regular_container) assert 'French Customer' == result
class Heading: title: Annotated[ str, Context(), Attr('name'), ]
class View: customer_name: Annotated[str, Context(), Attr('name'), ] @property def name(self): return f'Hello {self.customer_name}'
class ContextView: name: Annotated[str, Context(), Attr('name')] def __call__(self) -> str: return html('<div>Hello Customer</div>')