#!/usr/bin/env python

# run this script to create an actions.rst file for input to Sphinx.
#
# requires: python-sphinx
#
# from the command line run `sphinx-build` against the docs directory:
# $ sphinx-build -b html docs sst_docs


import inspect
import os
import sys
import textwrap

try:
    # installed
    from sst import actions
except ImportError:
    # from the development directory
    this_dir = os.path.abspath(os.path.dirname(__file__))
    sys.path.append(os.path.join(this_dir, '../src'))
    from sst import actions



with open(os.path.join(this_dir, 'actions.rst'), 'w') as h:
    def _write(text):
        if text.strip() and text.startswith('\n'):
            text = text[1:]
        text = textwrap.dedent(text or '')
        h.write(text)
        h.write('\n')

    head1 = """
=======================
    SST - API Reference
=======================

"""
    h.write(head1)
    _write(actions.__doc__)
    head2 = """
----------------
    Actions API:
----------------

"""
    h.write(head2)
    _write('\n')

    for entry in sorted(actions.__all__):
        member = getattr(actions, entry)
        doc = getattr(member, '__doc__', '')

        if not doc:
            continue

        _write(entry)
        _write('-' * len(entry))
        h.write('\n')

        try:
            spec = inspect.getargspec(member)
        except TypeError:
            pass
        else:
            _write('::')
            spec_text = inspect.formatargspec(*spec)
            h.write('\n   ' + entry + spec_text + '\n\n')

        _write(doc)
        h.write('\n')
