From 3942d706e3674b073a50c5591a0f6151b5ba299b Mon Sep 17 00:00:00 2001 From: bitkarrot <73979971+bitkarrot@users.noreply.github.com> Date: Fri, 6 Dec 2024 13:37:19 -0800 Subject: [PATCH] docs: add python api documentation --- bdk-python/.gitignore | 7 +++++ bdk-python/README.md | 8 +++++ bdk-python/docs/conf.py | 31 ++++++++++++++++++++ bdk-python/requirements-dev.txt | 2 ++ bdk-python/scripts/generate_docs.py | 45 +++++++++++++++++++++++++++++ 5 files changed, 93 insertions(+) create mode 100644 bdk-python/docs/conf.py create mode 100644 bdk-python/scripts/generate_docs.py diff --git a/bdk-python/.gitignore b/bdk-python/.gitignore index 260d7d39..d8d7dc4a 100644 --- a/bdk-python/.gitignore +++ b/bdk-python/.gitignore @@ -15,3 +15,10 @@ build/ testing-setup-py-simple-example.py localenvironment/ +venv +env + +# API docs +api.rst +index.rst +_build/ diff --git a/bdk-python/README.md b/bdk-python/README.md index 82110fc9..cd29699a 100644 --- a/bdk-python/README.md +++ b/bdk-python/README.md @@ -40,3 +40,11 @@ python setup.py --verbose bdist_wheel ```shell pip install ./dist/bdkpython-.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 +``` diff --git a/bdk-python/docs/conf.py b/bdk-python/docs/conf.py new file mode 100644 index 00000000..0245efee --- /dev/null +++ b/bdk-python/docs/conf.py @@ -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'] diff --git a/bdk-python/requirements-dev.txt b/bdk-python/requirements-dev.txt index b95a8a8e..22f325b6 100644 --- a/bdk-python/requirements-dev.txt +++ b/bdk-python/requirements-dev.txt @@ -1,2 +1,4 @@ pytest==7.1.2 tox==3.25.1 +sphinx +sphinx-rtd-theme diff --git a/bdk-python/scripts/generate_docs.py b/bdk-python/scripts/generate_docs.py new file mode 100644 index 00000000..9ee1378f --- /dev/null +++ b/bdk-python/scripts/generate_docs.py @@ -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}")