#!/usr/bin/python
# coding: utf-8
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import os
os.environ["WREPORT_BUILDING_DOCS"] = "true"

import sys
import inspect
import wreport

def print_indented(spaces, *args):
    "Print a string, indented by the given number of spaces"
    for s in args:
        for line in s.split("\n"):
            for i in range(1,spaces):
                sys.stdout.write(" ")
            sys.stdout.write(line)
            sys.stdout.write("\n")

def document_class(cls):
    name = cls.__name__
    print()
    print("wreport.{}".format(name))
    print("-" * (len(name) + 8))
    print()
    print(inspect.getdoc(cls))
    print()
    print("Members")
    print("```````")
    document_members(cls)

def document(obj):
    if obj.__doc__ == None: return

    if inspect.ismodule(obj):
        print_indented(0, inspect.getdoc(obj))
        print
    elif inspect.isroutine(obj) and inspect.isfunction(obj):
        # A Python function
        print_indented(2, '``'+obj.__name__ + inspect.formatargspec(*inspect.getargspec(obj))+"``")
        print_indented(4, inspect.getdoc(obj))
    else:
        print_indented(2, obj.__name__)
        print_indented(4, inspect.getdoc(obj))

def format_argspec(f):
    # TODO: if formatargspec gives nothing (what is nothing?) then look at the
    # first line of the docstring
    if inspect.isfunction(f):
        return inspect.formatargspec(*inspect.getargspec(f))
    else:
        return ""

def document_members(cls):
    # List and classify members
    member_vars = []
    member_funcs = []

    for name, m in inspect.getmembers(cls):
        if name[0] == '_': continue
        fullname = cls.__name__ + "." + name
        if inspect.isroutine(m) and inspect.isfunction(m):
            member_funcs.append((fullname, name, m))
        elif inspect.isclass(m) or inspect.ismodule(m):
            pass
        elif m.__class__.__name__ == "_Feature":
            pass
        else:
            member_vars.append((fullname, name, m))

    member_vars.sort()
    member_funcs.sort()

    # Document vars and get/setters
    for fullname, name, m in member_vars:
        print(".. _{}.{}:".format(cls.__name__, name))
    for fullname, name, m in member_funcs:
        print(".. _{}.{}():".format(cls.__name__, name))
    print()
    for fullname, name, m in member_vars:
        print_indented(2, name)
        print_indented(4, inspect.getdoc(m))

    # Document functions
    for fullname, name, m in member_funcs:
        argspec = format_argspec(m)
        print_indented(2, name + argspec)
        print_indented(4, inspect.getdoc(m))


print("""==================================
README for wreport Python bindings
==================================

wreport provides access to weather data in BUFR and CREX formats.

.. contents::

The wreport API
===============

The 'wreport' module has a few global methods:
""")

document_members(wreport)

print("""
and several classes, documented in their own sections.
""")

document_class(wreport.Var)
document_class(wreport.Varinfo)
document_class(wreport.Vartable)
