#! /usr/bin/env python
import os
import textwrap

import jinja2

from .bmi_metadata import load_bmi_metadata


_DOCSTRING = u"""
Basic Model Interface for {{ name }}.

{{ desc|trim|wordwrap(70) if desc }}

Author: {{ author }}
Version: {{ version }}
License: {{ license }}
DOI: {{ doi }}
URL: {{ url }}
{% if parameters %}
Parameters
----------
{% for param in parameters|sort(attribute='name') -%}
{{param.name}} : {{param.type}}, optional
    {{ "%s [default=%s %s]"|format(param.desc, param.value, param.units)|trim|wordwrap(70)|indent(4) }}
{% endfor %}
{% endif -%}

Examples
--------
>>> from pymt.components import {{name}}
>>> model = {{name}}()
>>> (fname, initdir) = model.setup()
>>> model.initialize(fname, dir=initdir)
>>> for _ in xrange(10):
...     model.update()
>>> model.finalize()
""".strip()


def bmi_docstring(name, author=None, version=None, license=None, doi=None,
                  url=None, parameters=None, summary=None):
    """Build the docstring for a BMI model.

    Parameters
    ----------
    name : str
        Name of a BMI component.
    author : str, optional
        Name of author or authors.
    version : str, optional
        Version string for the component.
    license : str, optional
        Name of the license of the component.
    doi : str, optional
        A DOI for the component.
    url : str, optional
        URL of the component's location on the internet.
    parameters : iterable, optional
        List of input parameters for the component. Each parameter object
        must have attributes for name, type, value, units, and desc.

    Returns
    -------
    str
        The docstring.
    """
    meta = load_bmi_metadata(name)

    author = author or meta['info'].author
    version = version or meta['info'].version
    license = license or meta['info'].license
    doi = doi or meta['info'].doi
    url = url or meta['info'].url
    summary = summary or meta['info'].summary
    parameters = parameters or meta['defaults'].values()

    env = jinja2.Environment(loader=jinja2.DictLoader({'docstring': _DOCSTRING}))
    return env.get_template('docstring').render(
        desc=summary,
        name=name,
        parameters=parameters,
        author=author,
        version=version,
        license=license,
        doi=doi,
        url=url,
    )

@@ -101,16 +101,27 @@
 metadata
+, ModelInfo
 %0A%0A%0A_DOCS
@@ -1660,87 +1660,576 @@
 ng.%0A
-    %22%22%22%0A    meta = load_bmi_metadata(name)%0A%0A   
+%0A    Examples%0A    --------%0A    %3E%3E%3E from pymt.framework.bmi_docstring import bmi_docstring%0A    %3E%3E%3E print bmi_docstring('Model', author='Walt Disney') #doctest: +ELLIPSIS%0A    Basic Model Interface for Model.%0A    ...%0A    %22%22%22%0A    try:%0A        meta = load_bmi_metadata(name)%0A    except IOError:%0A        info = ModelInfo(name=name,
  author
- = 
+=
 author
- or meta%5B'info'%5D
+, version=version,%0A                         license=license, doi=doi, url=url, summary=summary)%0A        defaults = %5B%5D%0A    else:%0A        info = meta%5B'info'%5D%0A        defaults = meta%5B'defaults'%5D.values()%0A%0A    author = author or info
 .aut
@@ -2257,28 +2257,20 @@
 sion or 
-meta%5B'
 info
-'%5D
 .version
@@ -2295,28 +2295,20 @@
 ense or 
-meta%5B'
 info
-'%5D
 .license
@@ -2325,28 +2325,20 @@
  doi or 
-meta%5B'
 info
-'%5D
 .doi%0A   
@@ -2351,28 +2351,20 @@
  url or 
-meta%5B'
 info
-'%5D
 .url%0A   
@@ -2385,28 +2385,20 @@
 mary or 
-meta%5B'
 info
-'%5D
 .summary
@@ -2397,24 +2397,26 @@
 .summary%0A   
+ #
  parameters 
@@ -2452,24 +2452,64 @@
 s'%5D.values()
+%0A    parameters = parameters or defaults
 %0A%0A    env = 

Add simple doctest for bmi_docstring.