Skip to content

Commit

Permalink
Fixing Sphinx
Browse files Browse the repository at this point in the history
  • Loading branch information
miohtama committed Jan 23, 2024
1 parent 3f9c368 commit 270901f
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 139 deletions.
110 changes: 1 addition & 109 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,112 +120,4 @@
"custom.css",
]

# Monkey-patch autosummary template context
from sphinx.ext.autosummary.generate import AutosummaryRenderer


def smart_fullname(fullname):
parts = fullname.split(".")
return ".".join(parts[1:])


def get_first_line(docstring: str | None) -> str:
if not docstring:
# __doc__ can be None
return ""
lines = docstring.split("\n")
return lines[0]


def extract_module_docstring(mod_name) -> str:
"""See _templates/autosummary/base.rst"""
import sys
mod = sys.modules[mod_name]
return get_first_line(getattr(mod, "__doc__", ""))


def extract_object_docstring(dotted_path: str) -> str:
"""See _templates/autosummary/base.rst"""
from zope.dottedname.resolve import resolve
obj = resolve(dotted_path)
return get_first_line(getattr(obj, "__doc__", ""))



def partial_name(fullname):
parts = fullname.split(".")
return parts[-1]


def obj_path(fullname):
parts = fullname.split(".")
return ".".join(parts[0:-1])

# Patch autosummary internals to allow our tuned templates to access
# necessary Python functions
def fixed_init(self, app, template_dir=None):
AutosummaryRenderer.__old_init__(self, app, template_dir)
self.env.filters["smart_fullname"] = smart_fullname
self.env.filters["extract_module_docstring"] = extract_module_docstring
self.env.filters["extract_object_docstring"] = extract_object_docstring
self.env.filters["partial_name"] = partial_name
self.env.filters["obj_path"] = obj_path



AutosummaryRenderer.__old_init__ = AutosummaryRenderer.__init__
AutosummaryRenderer.__init__ = fixed_init


#
# Monkey patch meta generation.
# See _templates/autosummary.base.rst
#

from sphinx.addnodes import meta
from docutils import nodes
from docutils.io import StringOutput
from sphinx.util.osutil import relative_uri

def write_doc(self, docname: str, doctree: nodes.document) -> None:
destination = StringOutput(encoding='utf-8')
doctree.settings = self.docsettings

self.secnumbers = self.env.toc_secnumbers.get(docname, {})
self.fignumbers = self.env.toc_fignumbers.get(docname, {})
self.imgpath = relative_uri(self.get_target_uri(docname), '_images')
self.dlpath = relative_uri(self.get_target_uri(docname), '_downloads')
self.current_docname = docname
self.docwriter.write(doctree, destination)
self.docwriter.assemble_parts()
body = self.docwriter.parts['fragment']
metatags = self.docwriter.clean_meta

ctx = self.get_doc_context(docname, body, metatags)

# Pass the custom meta attributes in raw objects instead
# of contatenad HTML soup
class ExtractMeta(nodes.GenericNodeVisitor):

def __init__(self, document):
super().__init__(document)
self.metas = {}

def default_visit(self, node):
if isinstance(node, meta):
self.metas[node.attributes.get("name")] = node.rawcontent

def default_departure(self, node):
pass

meta_extractor = ExtractMeta(doctree)
doctree.walkabout(meta_extractor)

ctx["metas"] = meta_extractor.metas

self.handle_page(docname, ctx, event_arg=doctree)

from sphinx.builders.html import StandaloneHTMLBuilder
StandaloneHTMLBuilder.write_doc = write_doc


from eth_defi.docs import monkeypatch
3 changes: 2 additions & 1 deletion docs/source/tutorials/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ Example tutorials

transfer
make-uniswap-swap-in-python
native-token-price
chainlink-price-feed
chainlink-native-token
multithread-reader
verify-node-integrity
live-price
Expand All @@ -53,6 +53,7 @@ Example tutorials
aave-v3-interest-analysis
slippage-and-price-impact
multi-rpc-configuration
enzyme-read-vaults

`For more examples, browse tests folder on Github <https://github.com/tradingstrategy-ai/web3-ethereum-defi/tree/master/tests>`__.
You can also search function names in `the repository <https://github.com/tradingstrategy-ai/web3-ethereum-defi/>`__
Expand Down
1 change: 1 addition & 0 deletions eth_defi/docs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Sphinx monkeypatches to make Sphinx more useful."""
111 changes: 111 additions & 0 deletions eth_defi/docs/monkeypatch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
"""Trying to make Sphinx more useful."""


# Monkey-patch autosummary template context
from sphinx.ext.autosummary.generate import AutosummaryRenderer


def smart_fullname(fullname):
parts = fullname.split(".")
return ".".join(parts[1:])


def get_first_line(docstring: str | None) -> str:
if not docstring:
# __doc__ can be None
return ""
lines = docstring.split("\n")
return lines[0]


def extract_module_docstring(mod_name) -> str:
"""See _templates/autosummary/base.rst"""
import sys
mod = sys.modules[mod_name]
return get_first_line(getattr(mod, "__doc__", ""))


def extract_object_docstring(dotted_path: str) -> str:
"""See _templates/autosummary/base.rst"""
from zope.dottedname.resolve import resolve
obj = resolve(dotted_path)
return get_first_line(getattr(obj, "__doc__", ""))



def partial_name(fullname):
parts = fullname.split(".")
return parts[-1]


def obj_path(fullname):
parts = fullname.split(".")
return ".".join(parts[0:-1])

# Patch autosummary internals to allow our tuned templates to access
# necessary Python functions
def fixed_init(self, app, template_dir=None):
AutosummaryRenderer.__old_init__(self, app, template_dir)
self.env.filters["smart_fullname"] = smart_fullname
self.env.filters["extract_module_docstring"] = extract_module_docstring
self.env.filters["extract_object_docstring"] = extract_object_docstring
self.env.filters["partial_name"] = partial_name
self.env.filters["obj_path"] = obj_path



AutosummaryRenderer.__old_init__ = AutosummaryRenderer.__init__
AutosummaryRenderer.__init__ = fixed_init


#
# Monkey patch meta generation.
# See _templates/autosummary.base.rst
#

from sphinx.addnodes import meta
from docutils import nodes
from docutils.io import StringOutput
from sphinx.util.osutil import relative_uri

def write_doc(self, docname: str, doctree: nodes.document) -> None:
destination = StringOutput(encoding='utf-8')
doctree.settings = self.docsettings

self.secnumbers = self.env.toc_secnumbers.get(docname, {})
self.fignumbers = self.env.toc_fignumbers.get(docname, {})
self.imgpath = relative_uri(self.get_target_uri(docname), '_images')
self.dlpath = relative_uri(self.get_target_uri(docname), '_downloads')
self.current_docname = docname
self.docwriter.write(doctree, destination)
self.docwriter.assemble_parts()
body = self.docwriter.parts['fragment']
metatags = self.docwriter.clean_meta

ctx = self.get_doc_context(docname, body, metatags)

# Pass the custom meta attributes in raw objects instead
# of contatenad HTML soup
class ExtractMeta(nodes.GenericNodeVisitor):

def __init__(self, document):
super().__init__(document)
self.metas = {}

def default_visit(self, node):
if isinstance(node, meta):
self.metas[node.attributes.get("name")] = node.rawcontent

def default_departure(self, node):
pass

meta_extractor = ExtractMeta(doctree)
doctree.walkabout(meta_extractor)

ctx["metas"] = meta_extractor.metas

self.handle_page(docname, ctx, event_arg=doctree)

from sphinx.builders.html import StandaloneHTMLBuilder
StandaloneHTMLBuilder.write_doc = write_doc

2 changes: 1 addition & 1 deletion eth_defi/event_reader/logresult.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class LogResult(TypedDict):
Example data (PancakeSwap swap):
.. code-block:: python
.. code-block:: text
{
'address': '0xc91cd2b9c9aafe494cf3ccc8bee7795deb17231a',
Expand Down
44 changes: 20 additions & 24 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ nbsphinx = {version = "^0.8.9", optional = true}
sphinx-rtd-theme = {version = "^1.0.0", optional = true}
zope-dottedname = {version = "^6.0", optional = true}
furo = {version = "^2022.6.4.1", optional = true}

# Version pindowns
# https://github.com/sphinx-doc/sphinxcontrib-applehelp/blob/master/CHANGES
sphinxcontrib-applehelp = {version = "1.0.4", optional = true}
sphinxcontrib-applehelp = {version = "1.0.4", optional = true} # Version pindowns https://github.com/sphinx-doc/sphinxcontrib-applehelp/blob/master/CHANGES
sphinxcontrib-devhelp = {version = "1.0.2", optional = true} # Version pindowns https://github.com/sphinx-doc/sphinxcontrib-devhelp/blob/master/CHANGES
sphinxcontrib-htmlhelp = {version = "2.0.1", optional = true} # Version pindowns https://github.com/sphinx-doc/sphinxcontrib-htmlhelp/blob/master/CHANGES
sphinxcontrib-serializinghtml = {version = "1.1.5", optional = true} # Version pindowns https://github.com/sphinx-doc/sphinxcontrib-serializinghtml/blob/master/CHANGES
sphinxcontrib-qthelp = {version = "1.0.3", optional = true} # Version pindowns https://github.com/sphinx-doc/sphinxcontrib-qthelp/blob/master/CHANGES

[tool.poetry.dev-dependencies]
pytest = "^7.4.3"
Expand Down

0 comments on commit 270901f

Please sign in to comment.