summaryrefslogtreecommitdiff
path: root/venv/lib/python3.11/site-packages/faker/sphinx/documentor.py
blob: ad6d7ab4e00768bba2cf150a855d0879e0a053e4 (plain)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# coding=utf-8
import importlib
import inspect
import os

from pathlib import Path

from faker.config import AVAILABLE_LOCALES
from faker.config import PROVIDERS as STANDARD_PROVIDER_NAMES
from faker.providers import BaseProvider

if os.environ.get("READTHEDOCS", False):
    version = os.environ["READTHEDOCS_VERSION"]
    HOME = Path("/home/docs/checkouts/readthedocs.org/user_builds/faker/checkouts") / version
    DOCS_ROOT = HOME / "docs"
else:
    DOCS_ROOT = Path(__file__).resolve().parents[2] / "docs"

SECTION_ADORNMENTS = "#*=-~"

PROVIDER_AUTODOC_TEMPLATE = """
.. autoclass:: {provider_class}
   :members: {provider_methods}
   :undoc-members:
   :show-inheritance:

"""

BASE_PROVIDER_METHOD_NAMES = [
    name for name, method in inspect.getmembers(BaseProvider, inspect.isfunction) if not name.startswith("_")
]


def _get_provider_methods(provider_class):
    try:
        provider_module_name, obj_name = provider_class.rsplit(".", 1)
        provider_module = importlib.import_module(provider_module_name)
        provider = getattr(provider_module, obj_name, None)
    except (ModuleNotFoundError, AttributeError):
        return ""
    else:
        return ", ".join(
            [
                name
                for name, method in inspect.getmembers(provider, inspect.isfunction)
                if not name.startswith("_") and name not in BASE_PROVIDER_METHOD_NAMES
            ]
        )


def _get_localized_provider_info(locale):
    info = []
    for provider_name in STANDARD_PROVIDER_NAMES:
        try:
            locale_module_path = f"{provider_name}.{locale}"
            locale_module = importlib.import_module(locale_module_path)
            provider = getattr(locale_module, "Provider")
        except (ModuleNotFoundError, AttributeError):
            continue
        else:
            provider_class = f"{provider.__module__}.Provider"
            info.append((provider_class, provider_name))
    return info


def _write(fh, s):
    return fh.write(s.encode("utf-8"))


def _hide_edit_on_github(fh):
    _write(fh, ":github_url: hide\n\n")


def _write_title(fh, title, level=1):
    if not isinstance(level, int) or level < 1 or level > 5:
        raise ValueError("`level` must be an integer from 1 to 5")
    if level <= 2:
        _write(fh, SECTION_ADORNMENTS[level - 1] * len(title))
        _write(fh, "\n")
    _write(fh, f"{title}\n")
    _write(fh, SECTION_ADORNMENTS[level - 1] * len(title))
    _write(fh, "\n\n")


def _write_includes(fh):
    _write(fh, ".. include:: ../includes/substitutions.rst")
    _write(fh, "\n\n")


def _write_standard_provider_index():
    with (DOCS_ROOT / "providers.rst").open("wb") as fh:
        _hide_edit_on_github(fh)
        _write_title(fh, "Standard Providers")
        _write(fh, ".. toctree::\n")
        _write(fh, "   :maxdepth: 2\n\n")
        _write(fh, "   providers/baseprovider\n")
        for provider_name in STANDARD_PROVIDER_NAMES:
            _write(fh, f"   providers/{provider_name}\n")


def _write_base_provider_docs():
    (DOCS_ROOT / "providers").mkdir(parents=True, exist_ok=True)
    with (DOCS_ROOT / "providers" / "baseprovider.rst").open("wb") as fh:
        _hide_edit_on_github(fh)
        _write_title(fh, "``faker.providers``")
        _write_includes(fh)
        _write(
            fh,
            PROVIDER_AUTODOC_TEMPLATE.format(
                provider_class="faker.providers.BaseProvider",
                provider_methods=",".join(BASE_PROVIDER_METHOD_NAMES),
            ),
        )


def _write_standard_provider_docs():
    (DOCS_ROOT / "providers").mkdir(parents=True, exist_ok=True)
    for provider_name in STANDARD_PROVIDER_NAMES:
        with (DOCS_ROOT / "providers" / f"{provider_name}.rst").open("wb") as fh:
            provider_class = f"{provider_name}.Provider"
            provider_methods = _get_provider_methods(provider_class)
            _hide_edit_on_github(fh)
            _write_title(fh, f"``{provider_name}``")
            _write_includes(fh)
            _write(
                fh,
                PROVIDER_AUTODOC_TEMPLATE.format(
                    provider_class=provider_class,
                    provider_methods=provider_methods,
                ),
            )


def _write_localized_provider_index():
    with (DOCS_ROOT / "locales.rst").open("wb") as fh:
        _hide_edit_on_github(fh)
        _write_title(fh, "Localized Providers")
        _write(fh, ".. toctree::\n")
        _write(fh, "   :maxdepth: 2\n\n")
        for locale in AVAILABLE_LOCALES:
            _write(fh, f"   locales/{locale}\n")


def _write_localized_provider_docs():
    (DOCS_ROOT / "locales").mkdir(parents=True, exist_ok=True)
    for locale in AVAILABLE_LOCALES:
        info = _get_localized_provider_info(locale)
        with (DOCS_ROOT / "locales" / "{}.rst".format(locale)).open("wb") as fh:
            _hide_edit_on_github(fh)
            _write_title(fh, f"Locale {locale}")
            _write_includes(fh)
            for provider_class, standard_provider_name in info:
                provider_methods = _get_provider_methods(provider_class)
                _write_title(fh, f"``{standard_provider_name}``", level=2)
                _write(
                    fh,
                    PROVIDER_AUTODOC_TEMPLATE.format(
                        provider_class=provider_class,
                        provider_methods=provider_methods,
                    ),
                )


def write_provider_docs():
    DOCS_ROOT.mkdir(parents=True, exist_ok=True)
    _write_standard_provider_index()
    _write_base_provider_docs()
    _write_standard_provider_docs()
    _write_localized_provider_index()
    _write_localized_provider_docs()