File: dask_config_sphinx_ext.py

package info (click to toggle)
dask 2021.01.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,172 kB
  • sloc: python: 74,608; javascript: 186; makefile: 150; sh: 94
file content (99 lines) | stat: -rw-r--r-- 2,755 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
import requests
import yaml

from docutils import nodes
from docutils.parsers.rst import Directive, directives


def get_remote_yaml(url):
    if url.startswith('file:///'):
        with open(url[len('file:///'):], 'rt') as instream:
            text = instream.read()
    else:
        r = requests.get(url)
        text = r.text
    return yaml.safe_load(text)


class DaskConfigDirective(Directive):

    option_spec = {
        "location": directives.unchanged,
        "schema": directives.uri,
        "config": directives.uri,
    }

    def run(self):
        location = self.options["location"]
        config = self.options["config"]
        schema = self.options["schema"]

        config = get_remote_yaml(config)
        schema = get_remote_yaml(schema)

        for k in location.split("."):
            # dask config does not have a top level key
            # we need to pass full schema and config
            if k == "dask":
                schema = schema
                config = config
            else:
                config = config[k]
                schema = schema["properties"].get(k, {})
        html = generate_html(config, schema, location)
        return [nodes.raw("", html, format="html")]


def setup(app):
    app.add_directive("dask-config-block", DaskConfigDirective)

    return {
        "version": "0.1",
        "parallel_read_safe": True,
        "parallel_write_safe": True,
    }


def dask_config_to_html(key, value, schema, prefix=""):
    if isinstance(value, dict):
        return sum(
            [
                dask_config_to_html(
                    k,
                    v,
                    schema.get("properties", {}).get(k, {"properties": {}}),
                    prefix=prefix + key + ".",
                )
                for k, v in value.items()
            ],
            [],
        )

    else:

        try:
            description = schema["description"]
            description = description.strip()
        except KeyError:
            description = "No Comment"

        key = prefix + key
        value = str(value)
        node = f"""<dl class="py data">
                     <dt id="{key}">
                       <code class="sig-prename descclassname">{key}</code>
                       <em class="property">&nbsp;&nbsp;{value}</p></em>
                       <a class="headerlink" href="#{key}" title="Permalink to this definition">ΒΆ</a>
                     </dt>
                    <dd><p>{description}</p></dd>
                    </dl>

                """
        return [node]


def generate_html(config, schema, location):
    nested_html = dask_config_to_html(
        key="", value=config, schema=schema, prefix=location
    )
    return "".join(nested_html)