mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Prototype of rst_docs_for_dataclass mechanism, refs #1510
This commit is contained in:
parent
007294008d
commit
6822378416
5 changed files with 161 additions and 1 deletions
28
docs/jsoncontext.py
Normal file
28
docs/jsoncontext.py
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
from docutils import nodes
|
||||
from sphinx.util.docutils import SphinxDirective
|
||||
from importlib import import_module
|
||||
import json
|
||||
|
||||
|
||||
class JSONContextDirective(SphinxDirective):
|
||||
required_arguments = 1
|
||||
|
||||
def run(self):
|
||||
module_path, class_name = self.arguments[0].rsplit(".", 1)
|
||||
try:
|
||||
module = import_module(module_path)
|
||||
dataclass = getattr(module, class_name)
|
||||
except ImportError:
|
||||
warning = f"Unable to import {self.arguments[0]}"
|
||||
return [nodes.error(None, nodes.paragraph(text=warning))]
|
||||
|
||||
doc = json.dumps(
|
||||
dataclass.__annotations__, indent=4, sort_keys=True, default=repr
|
||||
)
|
||||
doc_node = nodes.literal_block(text=doc)
|
||||
|
||||
return [doc_node]
|
||||
|
||||
|
||||
def setup(app):
|
||||
app.add_directive("jsoncontext", JSONContextDirective)
|
||||
Loading…
Add table
Add a link
Reference in a new issue