99 lines
3.2 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python3
import inspect
import yaml
from pathlib import Path
from main import IcecastBot
def get_version():
"""Get the current version from VERSION file."""
try:
with open('VERSION') as f:
return f.read().strip()
except FileNotFoundError:
return "Unknown"
def format_docstring(obj):
"""Format a docstring into markdown."""
doc = inspect.getdoc(obj)
if not doc:
return ""
# Split into description and args
parts = doc.split('\n\n')
formatted = [parts[0]] # Description
for part in parts[1:]:
if part.startswith('Args:'):
formatted.append("\n**Arguments:**\n")
# Parse args section
args = part.replace('Args:', '').strip().split('\n')
for arg in args:
if ':' in arg:
name, desc = arg.split(':', 1)
formatted.append(f"- `{name.strip()}`: {desc.strip()}")
elif part.startswith('Returns:'):
formatted.append("\n**Returns:**\n")
formatted.append(part.replace('Returns:', '').strip())
return '\n'.join(formatted)
def generate_command_docs(config_path='config.yaml.example'):
"""Generate command documentation from help templates."""
try:
with open(config_path) as f:
config = yaml.safe_load(f)
except FileNotFoundError:
return "No command documentation available (config file not found)"
help_config = config.get('commands', {}).get('help', {})
if not help_config:
return "No command documentation available (help template not found)"
docs = ["## Commands\n"]
# Regular commands
if 'commands' in help_config.get('sections', {}):
docs.append("### Regular Commands\n")
for cmd, desc in help_config['sections']['commands']['commands'].items():
docs.append(f"- `{cmd}`: {desc}")
docs.append("")
# Admin commands
if 'admin' in help_config.get('sections', {}):
docs.append("### Admin Commands\n")
docs.append("These commands are only available to users listed in the `admin.users` config section.\n")
for cmd, desc in help_config['sections']['admin']['commands'].items():
docs.append(f"- `{cmd}`: {desc}")
docs.append("")
return '\n'.join(docs)
def generate_docs():
"""Generate full documentation in markdown format."""
version = get_version()
docs = [
f"# Icecast IRC Bot Documentation v{version}\n",
"This document is automatically generated from the codebase.\n",
"## Overview\n",
format_docstring(IcecastBot),
"\n## Configuration\n",
"See `config.yaml.example` for a full example configuration file.\n",
generate_command_docs(),
"\n## Methods\n"
]
# Document public methods
for name, method in inspect.getmembers(IcecastBot, predicate=inspect.isfunction):
if not name.startswith('_'): # Only public methods
docs.append(f"### {name}\n")
docs.append(format_docstring(method))
docs.append("\n")
# Write to DOCS.md
with open('DOCS.md', 'w') as f:
f.write('\n'.join(docs))
if __name__ == '__main__':
generate_docs()