File: conf.py

package info (click to toggle)
pywayland 0.4.18-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 676 kB
  • sloc: python: 3,625; xml: 189; makefile: 174
file content (220 lines) | stat: -rw-r--r-- 6,479 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
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env python3

import importlib
import os
import shutil
import sys

# -- Mock necessary classes -----------------------------------------------
from unittest.mock import MagicMock

import sphinx_rtd_theme

sys.path.insert(0, os.path.abspath("."))
sys.path.insert(0, os.path.abspath(".."))

MOCK_MODULES = ["pywayland._ffi"]
sys.modules.update((mod_name, MagicMock()) for mod_name in MOCK_MODULES)

# -- Build pywayland.protocol w/docs --------------------------------------
from protocol_build import protocols_build, protocols_version, wayland_version

from pywayland import __version__

protocol_build_dir = "../pywayland/protocol/"
protocol_doc_dir = "module/protocol"

index_header = """\
.. _protocol:

Protocol Modules
================

Wayland protocols built against Wayland {} and Wayland Protocols {}.

.. toctree::
   :maxdepth: 2

""".format(
    wayland_version, protocols_version
)

protocol_header = """\
.. module:: pywayland.protocol.{module}

{module} Module
{empty:=^{len}}======="""

protocol_rst = """\
{protocol}
{empty:-^{len}}

.. wl_protocol:: pywayland.protocol.{module} {protocol}"""


# There is probably a better way to do this in Sphinx, templating or something
# ... but this works
def protocol_doc(input_dir, output_dir):
    modules = os.listdir(input_dir)
    modules = [
        module
        for module in modules
        if os.path.isdir(os.path.join(input_dir, module)) and module != "__pycache__"
    ]

    existing_files = [
        filename for filename in os.listdir(output_dir) if filename != "index.rst"
    ]
    rm_files = [
        filename
        for filename in existing_files
        if os.path.splitext(filename)[0] not in modules
    ]
    for rm_file in rm_files:
        if os.path.isdir(rm_file):
            shutil.rmtree(os.path.join(output_dir, rm_file))
        else:
            os.remove(os.path.join(output_dir, rm_file))

    # Write out the index file
    index_file = os.path.join(output_dir, "index.rst")
    if os.path.exists(index_file):
        with open(index_file) as f:
            existing_index = f.read()
    else:
        existing_index = ""

    generated_index = index_header + "".join(f"   {m}\n" for m in sorted(modules))

    if existing_index != generated_index:
        with open(index_file, "w") as f:
            f.write(generated_index)

    for module in sorted(modules):
        output = [protocol_header.format(module=module, len=len(module), empty="")]

        # get all the python files that we want to document
        doc_files = os.listdir(os.path.join(input_dir, module))
        doc_files = [
            os.path.splitext(doc_file)[0]
            for doc_file in doc_files
            if doc_file != "__init__.py" and os.path.splitext(doc_file)[1] == ".py"
        ]

        # build the rst for each protocol
        for doc_file in sorted(doc_files):
            mod = importlib.import_module(f"pywayland.protocol.{module}.{doc_file}")
            # Get out the name of the class in the module
            class_name = "".join(x.capitalize() for x in doc_file.split("_"))
            for mod_upper in dir(mod):
                if mod_upper == class_name:
                    break
            else:
                raise RuntimeError(f"Unable to find module: {doc_file}, {mod}")

            output.append(
                protocol_rst.format(
                    module=module, protocol=mod_upper, len=len(mod_upper), empty=""
                )
            )

        # build the index.rst for the module
        module_file = os.path.join(output_dir, f"{module}.rst")
        protocol_output = "\n\n".join(output)

        # if file exists and is unchanged, skip
        if os.path.exists(module_file):
            with open(module_file) as f:
                existing_output = f.read()

            if existing_output == protocol_output:
                continue

        with open(module_file, "w") as f:
            f.write("\n\n".join(output))


# Build the protocol directory on RTD
if os.environ.get("READTHEDOCS", None):
    protocols_build(protocol_build_dir)

# Re-build the protocol documentation directory
if not os.path.exists(protocol_doc_dir):
    os.makedirs(protocol_doc_dir)

protocol_doc(protocol_build_dir, protocol_doc_dir)

# -- General configuration ------------------------------------------------
extensions = ["sphinx.ext.autodoc", "sphinx_wl_protocol"]

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

# The suffix of source filenames.
source_suffix = ".rst"

# The master toctree document.
master_doc = "index"

# General information about the project.
project = "pywayland"
copyright = "2016, Sean Vig"

# The short X.Y version.
version = __version__.split("a")[0]
# The full version, including alpha/beta/rc tags.
release = __version__

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
exclude_patterns = ["_build"]

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = "sphinx"


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

# Set the html_theme when building locally
html_theme = "sphinx_rtd_theme"
html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]

# Output file base name for HTML help builder.
htmlhelp_basename = "pywaylanddoc"


# -- Options for LaTeX output ---------------------------------------------

latex_elements = {}

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
#  author, documentclass [howto, manual, or own class]).
latex_documents = [
    ("index", "pywayland.tex", "pywayland Documentation", "Sean Vig", "manual"),
]


# -- Options for manual page output ---------------------------------------

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [("index", "pywayland", "pywayland Documentation", ["Sean Vig"], 1)]


# -- Options for Texinfo output -------------------------------------------

# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
#  dir menu entry, description, category)
texinfo_documents = [
    (
        "index",
        "pywayland",
        "pywayland Documentation",
        "Sean Vig",
        "pywayland",
        "Python bindings for the libwayland library",
        "Miscellaneous",
    ),
]