Beispiel #1
0
class AboutGitHubButton:
    """ The github button block in the About sidebar """

    github_button: Annotated[bool, Get(ThemeConfig, attr='github_button')]
    github_count: Annotated[str, Get(ThemeConfig, attr='github_count')]
    github_repo: Annotated[Optional[str], Get(ThemeConfig, attr='github_repo')]
    github_type: Annotated[str, Get(ThemeConfig, attr='github_type')]
    github_user: Annotated[Optional[str], Get(ThemeConfig, attr='github_user')]
    show_button: bool = field(init=False)

    def __post_init__(self):
        if self.github_button and self.github_repo and self.github_user:
            self.show_button = True
        else:
            self.show_button = False

    def __call__(self) -> Optional[VDOM]:
        if self.show_button:
            return html('''\n
        <p>
            <iframe src="https://ghbtns.com/github-btn.html?user={self.github_user}&repo={self.github_repo}&type={self.github_type}&count={self.github_count}&size=large&v=2"
                    allowtransparency="true" frameborder="0" scrolling="0" width="200px" height="35px"></iframe>
        </p>            
            ''')
        return None
Beispiel #2
0
class Relations:
    master_doc: Annotated[str, Get(SphinxConfig, attr='master_doc')]
    pathto: Annotated[Callable[[str], str], Get(PageContext, attr='pathto')]
    toctree: Annotated[Optional[Callable[[], str]],
                       Get(PageContext, attr='toctree')]
    resolved_pathto: str = field(init=False)
    resolved_toctree: Markup = field(init=False)

    def __post_init__(self):
        self.resolved_pathto = self.pathto(self.master_doc)
        self.resolved_toctree = Markup(self.toctree())

    def __call__(self) -> VDOM:
        # Alabaster has a weird relations.html which isn't really well-formed
        # on looping. This makes it not-well-formed on snippets.
        return html('''\n
<div class="relations">
    <h3>Contents</h3>
    <ul>
        <li><a href={self.resolved_pathto}>Documentation overview</a>
        </li>
    </ul>
    {self.resolved_toctree}
</div>
        ''')
Beispiel #3
0
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>        
            ''')
Beispiel #4
0
class RellinkMarkup:
    """ Markup for rellink bars. """

    previous: Annotated[Link, Get(PageContext, attr='prev'), ]
    next: Annotated[Link, Get(PageContext, attr='next'), ]
    resolved_previous: VDOM = field(init=False)
    resolved_next: VDOM = field(init=False)

    def __post_init__(self):
        self.resolved_previous = html('''\n
<li>
    &larr;
    <a href="{self.previous.link}" title="Previous Document">{self.previous.title}</a>
</li>
''') if self.previous else None
        self.resolved_next = html('''\n
<li>
    <a href="{self.next.link}" title="Next Document">{self.next.title}</a>
    &rarr;
</li>
''') if self.previous else None

    def __call__(self) -> VDOM:
        return html('''\n
<nav id="rellinks">
    <ul>
        {self.resolved_previous}
        {self.resolved_next}
    </ul>
</nav>       
        ''')
Beispiel #5
0
class SearchBox:
    builder: Annotated[str, Get(PageContext, attr='builder')]
    pagename: Annotated[str, Get(PageContext, attr='pagename')]
    pathto: Annotated[Callable[[str], str], Get(PageContext, attr='pathto')]
    resolved_pathto: str = field(init=False)

    def __post_init__(self):
        self.resolved_pathto = self.pathto('search')

    def __call__(self) -> VDOM:
        if self.pagename != 'search' and self.builder != 'singlehtml':
            return html('''\n
<div id="searchbox" style="display: none" role="search" data-testid="searchbox">
    <h3>{{ _('Quick search') }}</h3>
    <div class="searchformwrapper">
        <form class="search" action="{self.resolved_pathto}" method="get">
          <input type="text" name="q" />
          <input type="submit" value="Go" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
            ''')
        return html('')
Beispiel #6
0
class AboutCodeCovButton:
    """ The travis button block in the About sidebar """

    github_repo: Annotated[Optional[str], Get(ThemeConfig, attr='github_repo')]
    github_user: Annotated[Optional[str], Get(ThemeConfig, attr='github_user')]
    codecov_button: Annotated[bool, Get(ThemeConfig, attr='codecov_button')]
    codecov_path: Annotated[str, Get(ThemeConfig, attr='codecov_path')]
    badge_branch: Annotated[str, Get(ThemeConfig, attr='badge_branch')]
    resolved_path: str = field(init=False)

    def __post_init__(self):
        if self.codecov_path:
            self.resolved_path = self.codecov_path
        else:
            self.resolved_path = f'{self.github_user}/{self.github_repo}'

    def __call__(self) -> Optional[VDOM]:
        path = self.resolved_path
        if self.codecov_button:
            return html('''\n
<p>
    <a class="badge" href="https://codecov.io/github/{path}">
        <img
                alt="https://codecov.io/github/{path}/coverage.svg?branch={self.badge_branch}"
                src="https://codecov.io/github/{path}/coverage.svg?branch={self.badge_branch}"
        />
    </a>
</p>
            ''')
        return None
Beispiel #7
0
class AboutLogo:
    """ The logo block in the About sidebar """

    logo: Annotated[Optional[str], Get(HTMLConfig, attr='logo')]
    master_doc: Annotated[str, Get(SphinxConfig, attr='master_doc')]
    pathto: Annotated[Callable[[str, Optional[int]], str],
                      Get(PageContext, attr='pathto')]
    resolved_master: str = field(init=False)
    resolved_logo: str = field(init=False)

    def __post_init__(self):
        self.resolved_master = self.pathto(self.master_doc, 0)
        self.resolved_logo = self.pathto(f'_static/{self.logo}',
                                         1) if self.logo else ''

    def __call__(self) -> Optional[VDOM]:
        if self.logo:
            return html('''\n
<p class="logo">
    <a href={self.resolved_master}>
        <img class="logo" src={self.resolved_logo} alt="Logo"/>
    </a>
</p>
                    ''')
        return None
Beispiel #8
0
class Title:
    resource_title: Annotated[str, Get(Resource, attr='title'), ]
    site_title: Annotated[Optional[str], Get(SphinxConfig, attr='project'), ]
    resolved_title: str = field(init=False)

    def __post_init__(self):
        if self.site_title:
            raw_title = f'{self.resource_title} - {self.site_title}'
        else:
            raw_title = f'{self.resource_title}'
        self.resolved_title = Markup(raw_title).striptags()

    def __call__(self) -> VDOM:
        return html('<title>{self.resolved_title}</title>')
Beispiel #9
0
class Relbar2:
    """ Relation bar usually at the bottom. """

    show_relbar_bottom: Annotated[bool,
                                  Get(ThemeConfig, attr='show_relbar_top')]
    show_relbars: Annotated[bool, Get(ThemeConfig, attr='show_relbar_top')]
    show_relbar_top: bool = field(init=False)

    def __post_init__(self):
        self.show_relbar_top = self.show_relbar_bottom or self.show_relbars

    def __call__(self) -> VDOM:
        return html('''\n
<div class="related top"><{RellinkMarkup} /> </div>        
        ''') if self.show_relbar_top else []
Beispiel #10
0
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
Beispiel #11
0
class View:
    settings: Annotated[MySettings, Get(BaseSettings)]

    @property
    def name(self):
        site_name = self.settings.site_name
        return f'View - {site_name}'
 def target(
     french_view: Annotated[
         FrenchView,
         Get(View),
     ]
 ):
     return french_view
Beispiel #13
0
class HelloWorld:
    name: str
    title: Annotated[str, Get(Resource, attr='title')]

    def __call__(self) -> VDOM:
        return html(
            '<h1>Resource: {self.title}</h1><span>Hello {self.name}</span>')
Beispiel #14
0
class Head:
    pathto: Annotated[Callable[[str, int], str],
                      Get(PageContext, attr='pathto'), ]
    extrahead: Optional[Tuple[VDOM, ...]] = None
    charset: str = 'utf-8'
    resolved_custom_css: str = field(init=False)
    resolved_docs_src: str = field(init=False)
    resolved_static_root: str = field(init=False)

    def __post_init__(self):
        self.resolved_custom_css = self.pathto('_static/custom.css', 1)
        self.resolved_docs_src = self.pathto(
            '_static/documentation_options.js', 1)
        self.resolved_static_root = self.pathto('', 1)

    def __call__(self) -> VDOM:
        return html('''\n
<head>
  <meta charset="{self.charset}" />
  <meta name="viewport" content="width=device-width, initial-scale=1"/>
  <{Title} />
  <{CSSFiles} />
  <script id="documentation_options" data-url_root="{self.resolved_static_root}" src="{self.resolved_docs_src}">//</script>
  <{JSFiles} />
  <link rel="stylesheet" href="{self.resolved_custom_css}" type="text/css"/>
  <{CanonicalLink} />
  <{FaviconSet}/>
  <{Linktags} />
  {self.extrahead}
</head>
''')
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>''')
Beispiel #16
0
class DummyComponent2:
    """ Test passing in singletons"""

    singleton_title: Annotated[str, Get(DummySingleton, attr='title')]

    def __call__(self) -> VDOM:
        return html('<div>Hello {self.singleton_title}</div>')
Beispiel #17
0
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}'
Beispiel #18
0
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 []
Beispiel #19
0
def test_pipeline_one(french_container):
    pipeline = (Get(View), )
    result: FrenchView = process_pipeline(
        french_container,
        pipeline,
        start=View,
    )
    assert result.name == 'French View'
Beispiel #20
0
def test_pipeline_two(french_container):
    pipeline = (Get(View), Attr('name'))
    result = process_pipeline(
        french_container,
        pipeline,
        start=View,
    )
    assert result == 'French View'
 def target(
     view_name: Annotated[
         str,
         Get(Foo),
         Attr('name'),
     ] = 'View Name'
 ):
     return view_name
Beispiel #22
0
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 target(
     view_name: Annotated[
         str,
         Get(View),
         Attr('name'),
     ],
 ):
     return view_name
def test_is_pipeline_get(regular_container):
    # There is a pipeline that does one thing
    pipeline = (Get(View), )
    fi = FieldInfo('foo', str, None, True, pipeline)
    field_make_pipeline = FieldMakePipeline(fi, {}, regular_container)
    with pytest.raises(FoundValueField) as exc:
        field_make_pipeline()
    assert exc.value.value.name == 'View'
Beispiel #25
0
class CSSFiles:
    site_files: Annotated[Paths,
                          Get(HTMLConfig, attr='css_files'),
                          StaticPathTo(), ]
    theme_files: Annotated[Paths,
                           Get(ThemeConfig, attr='css_files'),
                           StaticPathTo(), ]
    page_files: Annotated[Paths,
                          Get(PageContext, attr='css_files'),
                          StaticPathTo(), ]
    hrefs: Tuple[str, ...] = field(init=False)

    def __post_init__(self):
        self.hrefs = self.site_files + self.theme_files + self.page_files

    def __call__(self) -> VDOM:
        return html('{[CSSFile(href) for href in self.hrefs]}')
Beispiel #26
0
class JSFiles:
    site_files: Annotated[Paths,
                          Get(HTMLConfig, attr='js_files'),
                          StaticPathTo(), ]
    theme_files: Annotated[Paths,
                           Get(ThemeConfig, 'js_files'),
                           StaticPathTo(), ]
    page_files: Annotated[Paths,
                          Get(PageContext, 'js_files'),
                          StaticPathTo(), ]
    srcs: Tuple[str, ...] = field(init=False)

    def __post_init__(self):
        self.srcs = self.site_files + self.theme_files + self.page_files

    def __call__(self) -> VDOM:
        return html('{[JSFile(src) for src in self.srcs]}')
 def target(
     container: ServiceContainer,
     french_customer: Annotated[
         FrenchView,
         Get(View),
     ],
 ):
     return french_customer
Beispiel #28
0
class LocalToc:
    display_toc: Annotated[bool, Get(PageContext, attr='display_toc')]
    master_doc: Annotated[str, Get(SphinxConfig, attr='master_doc')]
    pathto: Annotated[Callable[[str], str], Get(PageContext, attr='pathto')]
    toc: Annotated[Markup, Get(PageContext, attr='toc')]
    resolved_pathto: str = field(init=False)

    def __post_init__(self):
        self.resolved_pathto = self.pathto(self.master_doc)

    def __call__(self) -> VDOM:
        if self.display_toc:
            return html('''\n
<h3><a href={self.resolved_pathto}>Table of Contents</a></h3>
{self.toc}
            ''')
        return html('')
Beispiel #29
0
class AboutDescription:
    """ The description block in the About sidebar """

    description: Annotated[Optional[str], Get(ThemeConfig, attr='description')]

    def __call__(self) -> Optional[VDOM]:
        if self.description:
            return html('<p class="blurb">{self.description}</p>')
        return None
def test_annotation_optional():
    def target(customer: Optional[Annotated[Customer, Get(FrenchCustomer)]]):
        return 99

    field_infos = _get_field_infos(target)
    assert field_infos[0].field_name == 'customer'
    assert field_infos[0].field_type is Customer
    assert field_infos[0].default_value is None
    assert field_infos[0].pipeline == (Get(FrenchCustomer), )