| 1 |
# Configuration file for the Sphinx documentation builder. |
| 2 |
# |
| 3 |
# This file only contains a selection of the most common options. For a full |
| 4 |
# list see the documentation: |
| 5 |
# https://www.sphinx-doc.org/en/master/usage/configuration.html |
| 6 |
|
| 7 |
|
| 8 |
# -- Project information ----------------------------------------------------- |
| 9 |
|
| 10 |
project = "Jupyter Cache" |
| 11 |
copyright = "2020, Executable Book Project" |
| 12 |
author = "Executable Book Project" |
| 13 |
|
| 14 |
master_doc = "index" |
| 15 |
|
| 16 |
# -- General configuration --------------------------------------------------- |
| 17 |
|
| 18 |
# Add any Sphinx extension module names here, as strings. They can be |
| 19 |
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom |
| 20 |
# ones. |
| 21 |
extensions = [ |
| 22 |
"myst_nb", |
| 23 |
"sphinx.ext.intersphinx", |
| 24 |
"sphinx_copybutton", |
| 25 |
# "sphinx.ext.autodoc", |
| 26 |
# "sphinx.ext.viewcode", |
| 27 |
] |
| 28 |
myst_enable_extensions = ["colon_fence", "deflist"] |
| 29 |
nb_execution_mode = "off" |
| 30 |
html_theme_options = { |
| 31 |
"repository_url": "https://github.com/executablebooks/jupyter-cache", |
| 32 |
"use_repository_button": True, |
| 33 |
"use_edit_page_button": True, |
| 34 |
"use_issues_button": True, |
| 35 |
"repository_branch": "master", |
| 36 |
"path_to_docs": "docs", |
| 37 |
"home_page_in_toc": True, |
| 38 |
} |
| 39 |
|
| 40 |
# Add any paths that contain templates here, relative to this directory. |
| 41 |
templates_path = ["_templates"] |
| 42 |
|
| 43 |
# List of patterns, relative to source directory, that match files and |
| 44 |
# directories to ignore when looking for source files. |
| 45 |
# This pattern also affects html_static_path and html_extra_path. |
| 46 |
exclude_patterns = [ |
| 47 |
"_build", |
| 48 |
"Thumbs.db", |
| 49 |
".DS_Store", |
| 50 |
"**/example_nbs/*", |
| 51 |
"**/.jupyter_cache/**/*", |
| 52 |
"**/.ipynb_checkpoints/*", |
| 53 |
] |
| 54 |
|
| 55 |
|
| 56 |
# -- Options for HTML output ------------------------------------------------- |
| 57 |
|
| 58 |
# The theme to use for HTML and HTML Help pages. See the documentation for |
| 59 |
# a list of builtin themes. |
| 60 |
# |
| 61 |
html_theme = "sphinx_book_theme" |
| 62 |
html_title = "Jupyter Cache" |
| 63 |
html_logo = "_static/logo_wide.svg" |
| 64 |
html_favicon = "_static/logo_square.svg" |
| 65 |
|
| 66 |
# Add any paths that contain custom static files (such as style sheets) here, |
| 67 |
# relative to this directory. They are copied after the builtin static files, |
| 68 |
# so a file named "default.css" will overwrite the builtin "default.css". |
| 69 |
html_static_path = ["_static"] |
| 70 |
# html_css_files = ["css/custom.css"] |
| 71 |
|
| 72 |
|
| 73 |
intersphinx_mapping = {"python": ("https://docs.python.org/3.8", None)} |
| 74 |
autodoc_member_order = "bysource" |
| 75 |
|
| 76 |
nitpick_ignore = [ |
| 77 |
("py:class", "Any"), |
| 78 |
("py:class", "Tuple"), |
| 79 |
("py:class", "ForwardRef"), |
| 80 |
("py:class", "NoneType"), |
| 81 |
] |
| 82 |
|
| 83 |
|
| 84 |
def setup(app): |
| 85 |
import importlib |
| 86 |
import os |
| 87 |
import shutil |
| 88 |
import traceback |
| 89 |
|
| 90 |
import click |
| 91 |
from click.testing import CliRunner |
| 92 |
from docutils import nodes |
| 93 |
from docutils.parsers.rst import directives |
| 94 |
from sphinx.util.docutils import SphinxDirective |
| 95 |
|
| 96 |
class JcacheClear(SphinxDirective): |
| 97 |
"""A directive to clear the jupyter cache.""" |
| 98 |
|
| 99 |
def run(self): |
| 100 |
path = os.path.join(os.path.dirname(self.env.app.srcdir), ".jupyter_cache") |
| 101 |
if os.path.exists(path): |
| 102 |
shutil.rmtree(path) |
| 103 |
return [] |
| 104 |
|
| 105 |
class JcacheCli(SphinxDirective): |
| 106 |
"""A directive to run a CLI command, |
| 107 |
and output a nicely formatted representation of the input command and its output. |
| 108 |
""" |
| 109 |
|
| 110 |
required_arguments = 1 # command |
| 111 |
final_argument_whitespace = False |
| 112 |
has_content = False |
| 113 |
option_spec = { |
| 114 |
"prog": directives.unchanged_required, |
| 115 |
"command": directives.unchanged_required, |
| 116 |
"args": directives.unchanged_required, |
| 117 |
"input": directives.unchanged_required, |
| 118 |
"allow-exception": directives.flag, |
| 119 |
} |
| 120 |
|
| 121 |
def run(self): |
| 122 |
modpath = self.arguments[0] |
| 123 |
|
| 124 |
try: |
| 125 |
module_name, attr_name = modpath.split(":", 1) |
| 126 |
except ValueError: |
| 127 |
raise self.error(f'"{modpath}" is not of format "module:command"') |
| 128 |
|
| 129 |
try: |
| 130 |
module = importlib.import_module(module_name) |
| 131 |
except Exception: |
| 132 |
raise self.error( |
| 133 |
f"Failed to import '{module_name}': {traceback.format_exc()}" |
| 134 |
) |
| 135 |
|
| 136 |
if not hasattr(module, attr_name): |
| 137 |
raise self.error( |
| 138 |
f'Module "{module_name}" has no attribute "{attr_name}"' |
| 139 |
) |
| 140 |
command = getattr(module, attr_name) |
| 141 |
if not isinstance(command, click.Group): |
| 142 |
raise self.error( |
| 143 |
f'"{modpath}" of type {type(command)}"" is not derived from "click.Group"' |
| 144 |
) |
| 145 |
|
| 146 |
cmd_string = [self.options.get("prog", "jcache")] |
| 147 |
if command.name != cmd_string[0]: |
| 148 |
cmd_string.append(command.name) |
| 149 |
if "command" in self.options: |
| 150 |
cmd_string.append(self.options["command"]) |
| 151 |
command = command.commands[self.options["command"]] |
| 152 |
|
| 153 |
args = self.options.get("args", "") |
| 154 |
|
| 155 |
runner = CliRunner() |
| 156 |
root_path = os.path.dirname(self.env.app.srcdir) |
| 157 |
try: |
| 158 |
old_cwd = os.getcwd() |
| 159 |
os.chdir(root_path) |
| 160 |
result = runner.invoke( |
| 161 |
command, args.split(), input=self.options.get("input", None), env={} |
| 162 |
) |
| 163 |
finally: |
| 164 |
os.chdir(old_cwd) |
| 165 |
|
| 166 |
if result.exception and "allow-exception" not in self.options: |
| 167 |
raise self.error( |
| 168 |
f"CLI raised exception: {result.exception}\n---\n{result.output}\n---\n" |
| 169 |
) |
| 170 |
if result.exit_code != 0 and "allow-exception" not in self.options: |
| 171 |
raise self.error( |
| 172 |
f"CLI non-zero exit code: {result.exit_code}\n---\n{result.output}\n---\n" |
| 173 |
) |
| 174 |
|
| 175 |
text = f"$ {' '.join(cmd_string)} {args}\n{result.output}" |
| 176 |
text = text.replace(root_path + os.sep, "../") |
| 177 |
node = nodes.literal_block(text, text, language="console") |
| 178 |
return [node] |
| 179 |
|
| 180 |
app.add_directive("jcache-clear", JcacheClear) |
| 181 |
app.add_directive("jcache-cli", JcacheCli) |