Skip to content

Add Python API Docs #752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions bdk-python/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ build/

testing-setup-py-simple-example.py
localenvironment/
venv
env

# API docs
api.rst
index.rst
_build/
8 changes: 8 additions & 0 deletions bdk-python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,11 @@ python setup.py --verbose bdist_wheel
```shell
pip install ./dist/bdkpython-<yourversion>.whl
```

## generate the docs

```shell
pip3 install --requirement requirements-dev.txt
python3 scripts/generate_docs.py
sphinx-build -b html -W --keep-going docs/ docs/_build/html
```
31 changes: 31 additions & 0 deletions bdk-python/docs/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import os
import sys
sys.path.insert(0, os.path.abspath('..'))

# Configuration file for the Sphinx documentation builder.
project = 'bdkpython'
copyright = '2025, Bitcoin Dev Kit'
author = 'Bitcoin Dev Kit Developers'

# The full version, including alpha/beta/rc tags
release = '1.2.0'

# Add any Sphinx extension module names here, as strings
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.napoleon',
'sphinx.ext.viewcode',
]

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

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

# The theme to use for HTML and HTML Help pages.
html_theme = 'sphinx_rtd_theme'

# Add any paths that contain custom static files (such as style sheets)
html_static_path = ['_static']
2 changes: 2 additions & 0 deletions bdk-python/requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
pytest==7.1.2
tox==3.25.1
sphinx
sphinx-rtd-theme
45 changes: 45 additions & 0 deletions bdk-python/scripts/generate_docs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import os
import re

# Define the directory where the Python source files are located
src_dir = 'src/bdkpython'
package_root = 'bdkpython'

# Define the output file for the API documentation
output_file = 'docs/api.rst'

# Regex pattern to match class definitions
class_pattern = re.compile(r'^class ([A-Za-z][A-Za-z0-9_]*)')

# Store classes with their full module path
public_classes = {}

for root, _, files in os.walk(src_dir):
for file in files:
if file.endswith('.py'):
module_path = os.path.relpath(os.path.join(root, file), src_dir)
module_path = module_path.replace(os.sep, '.').removesuffix('.py')
full_module = f'{package_root}.{module_path}'
with open(os.path.join(root, file), 'r') as f:
for line in f:
match = class_pattern.match(line)
if match:
class_name = match.group(1)
if not class_name.startswith('_'):
fqcn = f'{full_module}.{class_name}'
public_classes[fqcn] = True

# Generate the RST content
rst_content = "API Reference\n============\n\n"

for fqcn in sorted(public_classes.keys()):
rst_content += f".. autoclass:: {fqcn}\n"
rst_content += " :members:\n"
rst_content += " :undoc-members:\n"
rst_content += " :show-inheritance:\n\n"

# Write the RST content to the output file
with open(output_file, 'w') as f:
f.write(rst_content)

print(f"API documentation has been generated in {output_file}")
Loading