File: conf.py

package info (click to toggle)
python-cyclopts 3.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,288 kB
  • sloc: python: 11,445; makefile: 24
file content (210 lines) | stat: -rw-r--r-- 6,008 bytes parent folder | download | duplicates (2)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Path setup --------------------------------------------------------------
import importlib
import inspect
import sys
from datetime import date
from pathlib import Path

from sphinx.application import Sphinx
from sphinx.ext.autodoc import Options

from cyclopts import __version__

sys.path.insert(0, str(Path("../..").absolute()))

git_commit = "main"

# -- Project information -----------------------------------------------------

project = "cyclopts"
copyright = f"{date.today().year}, Brian Pugh"
author = "Brian Pugh"

# The short X.Y version.
version = __version__
# The full version, including alpha/beta/rc tags
release = __version__


# -- General configuration ---------------------------------------------------

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
    "myst_parser",
    "sphinx_rtd_theme",
    "sphinx.ext.autodoc",
    "sphinx.ext.intersphinx",
    "sphinx.ext.napoleon",
    "sphinx_copybutton",
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ["_templates"]

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = []

smartquotes = False

# user starts in light mode
default_dark_mode = False

# Myst
myst_enable_extensions = [
    "linkify",
]

# Intersphinx
intersphinx_mapping = {
    "pytest": ("/usr/share/doc/python-pytest-doc/html", None),
    "python": ("/usr/share/doc/python3-doc/html", None),
}

# Napoleon settings
napoleon_google_docstring = True
napoleon_numpy_docstring = True
napoleon_include_init_with_doc = False
napoleon_include_private_with_doc = False
napoleon_include_special_with_doc = True
napoleon_use_admonition_for_examples = False
napoleon_use_admonition_for_notes = False
napoleon_use_admonition_for_references = False
napoleon_use_ivar = False
napoleon_use_param = True
napoleon_use_rtype = True
napoleon_preprocess_types = False
napoleon_type_aliases = None
napoleon_attr_annotations = True

# Autodoc
autodoc_default_options = {
    "member-order": "bysource",
    "undoc-members": False,
    "exclude-members": "__weakref__",
}
autoclass_content = "both"

# LinkCode
code_url = f"https://github.com/BrianPugh/cyclopts/blob/{git_commit}"


def linkcode_resolve(domain, info):
    """Link code to github.

    Modified from:
        https://github.com/python-websockets/websockets/blob/778a1ca6936ac67e7a3fe1bbe585db2eafeaa515/docs/conf.py#L100-L134
    """
    # Non-linkable objects from the starter kit in the tutorial.
    if domain == "js":
        return

    if domain != "py":
        raise ValueError("expected only Python objects")

    if not info.get("module"):
        # Documented via py:function::
        return

    mod = importlib.import_module(info["module"])
    if "." in info["fullname"]:
        objname, attrname = info["fullname"].split(".")
        obj = getattr(mod, objname)
        try:
            # object is a method of a class
            obj = getattr(obj, attrname)
        except AttributeError:
            # object is an attribute of a class
            return None
    else:
        obj = getattr(mod, info["fullname"])

    try:
        file = inspect.getsourcefile(obj)
        lines = inspect.getsourcelines(obj)
    except TypeError:
        # e.g. object is a typing.Union
        return None
    if file is None:
        return None
    file = Path(file).resolve().relative_to(git_repo.working_dir)
    if file.parts[0] != "cyclopts":
        # e.g. object is a typing.NewType
        return None
    start, end = lines[1], lines[1] + len(lines[0]) - 1

    return f"{code_url}/{file}#L{start}-L{end}"


# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
#
html_theme = "sphinx_rtd_theme"

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ["_static"]

html_title = project
html_logo = "../../assets/logo_512w.png"
html_favicon = "../../assets/favicon-192.png"

html_theme_options = {
    "logo_only": True,
    "version_selector": True,
    "prev_next_buttons_location": "bottom",
    "style_external_links": False,
    "vcs_pageview_mode": "",
    "style_nav_header_background": "#F7E5B9",
    # Toc options
    "collapse_navigation": True,
    "sticky_navigation": True,
    "navigation_depth": 4,
    "includehidden": True,
    "titles_only": False,
}

html_context = {
    # Github options
    "display_github": True,
    "github_user": "BrianPugh",
    "github_repo": "cyclopts",
    "github_version": "main",
    "conf_py_path": "/docs/source/",
}

html_css_files = [
    "custom.css",
]


# --- Other Custom Stuff


def simplify_exception_signature(
    app: Sphinx, what: str, name: str, obj, options: Options, signature, return_annotation
):
    # Check if the object is an exception and modify the signature
    if what == "exception" and isinstance(obj, type) and issubclass(obj, BaseException):
        return ("", None)  # Return an empty signature and no return annotation


def remove_attrs_methods(app, what, name, obj, options, lines):
    lines[:] = [line for line in lines if not line.startswith("Method generated by attrs for")]


def setup(app: Sphinx):
    app.connect("autodoc-process-signature", simplify_exception_signature)
    app.connect("autodoc-process-docstring", remove_attrs_methods)