File: common.py

package info (click to toggle)
python-sphinx-code-include 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,312 kB
  • sloc: javascript: 8,765; python: 1,402; makefile: 23
file content (156 lines) | stat: -rw-r--r-- 4,259 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Useful functions that other testing modules use."""

from __future__ import annotations

import typing
import warnings
from unittest import mock

from docutils import statemachine
from sphinx import application
from sphinx.ext import intersphinx

from code_include import extension
from code_include import helper

if typing.TYPE_CHECKING:
    from sphinx.util.typing import Inventory


def make_mock_directive(
    content: list[str],
    options: typing.Optional[dict[str, object]] = None,
) -> extension.Directive:
    """Create the main class which is translated and rendered as text.

    Args:
        content:
            The lines that the user provides in a standard code-include block.
        options:
            Directive modifiers (e.g. ``{"fallback-text": "foo bar"}``).

    Returns:
        The class that is later translated by Sphinx into HTML tags.

    """
    options = options or {}

    name = "code-include"
    arguments: list[str] = []
    line_number = 11
    content_offset = 10
    block_text = ""
    state = mock.MagicMock()
    state_machine = mock.MagicMock()

    return extension.Directive(
        name,
        arguments,
        options,
        statemachine.StringList(content),
        line_number,
        content_offset,
        block_text,
        state,
        state_machine,
    )


@helper.memoize
def load_cache(path: str) -> Inventory:
    """Load some inventory file as raw data.

    Args:
        path:
            The absolute path where an inventory file can be found.

    Returns:
        Each directive target type, its namespace, and it's file-path/URL information.
        e.g. {
            "py:method": {
                "fake_project.basic.MyKlass.get_method": (
                    "fake_project",
                    "",
                    "api/fake_project.html#fake_project.basic.MyKlass.get_method",
                    "-",
                )
            }
        }

    """

    class MockConfiguration(object):  # pylint: disable=too-few-public-methods
        """A fake set of settings for intersphinx to pass-through."""

        intersphinx_timeout = None  # type: int
        tls_verify = False

    class MockApplication(object):  # pylint: disable=too-few-public-methods
        """A fake state machine for intersphinx to consume and pass-through."""

        srcdir = ""
        config = MockConfiguration()

        @staticmethod
        def warn(message: str) -> None:
            """Send a warning if bad-formatted text is encountered."""
            warnings.warn(message)

    return intersphinx.fetch_inventory(
        typing.cast(application.Sphinx, MockApplication()),
        "",
        path,
    )


@helper.memoize
def load_cache_from_url(url: str) -> Inventory:
    """Load some inventory file as raw data.

    Args:
        url:
            The website address that points to a objects.inv file.
            e.g. "https://foo_bar_name.readthedocs.io/en/latest/objects.inv".

    Returns:
        Each directive target type, its namespace, and it's file-path/URL information.
        e.g. {
            "py:method": {
                "fake_project.basic.MyKlass.get_method": (
                    "fake_project",
                    "",
                    "api/fake_project.html#fake_project.basic.MyKlass.get_method",
                    "-",
                )
            }
        }

    """

    class MockConfiguration(object):  # pylint: disable=too-few-public-methods
        """A fake set of settings for intersphinx to pass-through."""

        intersphinx_timeout = None  # type: int
        tls_cacerts = ""  # Needed for Python 3.10+
        tls_verify = False
        user_agent = ""

    class MockApplication(object):  # pylint: disable=too-few-public-methods
        """A fake state machine for intersphinx to consume and pass-through."""

        srcdir = ""
        config = MockConfiguration()

        @staticmethod
        def warn(message: str) -> None:
            """Send a warning if bad-formatted text is encountered."""
            warnings.warn(message)

    return intersphinx.fetch_inventory(
        typing.cast(application.Sphinx, MockApplication()),
        "",
        url,
    )