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

from .bmi_metadata import load_bmi_metadata


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

{desc}

author: {author}
version: {version}
license: {license}
DOI: {doi}
URL: {url}

Parameters
----------
{parameters}

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()


_PARAM_DECL = "{name} : {type}, optional"
_PARAM_DESC = "{desc} [default={default} {units}]."


def build_parameters_section(parameters):
    """Build the paramters section of the docstring for a BMI model.

    Parameters
    ----------
    parameters : dict
        Parameters names and descriptions.

    Returns
    -------
    str
        The paramters section.
    """
    params = parameters.values()
    params.sort(key=lambda p: p.name)

    docstrings = []
    for param in params:
        decl = _PARAM_DECL.format(name=param.name, type=param.type)
        desc = _PARAM_DESC.format(desc=param.desc, default=param.value,
                                  units=param.units)

        docstrings.extend(
            textwrap.wrap(decl, subsequent_indent=' ' * (decl.find(':') + 3)))
        docstrings.extend(
            textwrap.wrap(desc, initial_indent=' ' * 4,
                          subsequent_indent=' ' * 4))

    return os.linesep.join(docstrings)


def bmi_docstring(name):
    """Build the docstring for a BMI model.

    Parameters
    ----------
    name : str
        Name of a BMI component.

    Returns
    -------
    str
        The docstring.
    """
    meta = load_bmi_metadata(name)
    desc = '\n'.join(textwrap.wrap(meta['info'].summary))
    
    return _DOCSTRING.format(
        desc=desc, name=name,
        parameters=build_parameters_section(meta['defaults']),
        author=meta['info'].author,
        version=meta['info'].version,
        license=meta['info'].license,
        doi=meta['info'].doi,
        url=meta['info'].url,
    )

@@ -43,16 +43,31 @@
 xtwrap%0A%0A
+import jinja2%0A%0A
 from .bm
@@ -133,19 +133,16 @@
 ic Model
-ing
  Interfa
@@ -157,22 +157,24 @@
 name%7D.%0A%0A
+%7B
 %7Bdesc%7D
+%7D
 %0A%0Aauthor
@@ -179,16 +179,17 @@
 or: 
+%7B
 %7Bauthor%7D
 %0Aver
@@ -184,16 +184,17 @@
 %7Bauthor%7D
+%7D
 %0Aversion
@@ -195,16 +195,17 @@
 ersion: 
+%7B
 %7Bversion
@@ -205,16 +205,17 @@
 version%7D
+%7D
 %0Alicense
@@ -216,16 +216,17 @@
 icense: 
+%7B
 %7Blicense
@@ -230,31 +230,55 @@
 nse%7D
+%7D
 %0ADOI: 
+%7B
 %7Bdoi%7D
+%7D
 %0AURL: 
+%7B
 %7Burl%7D
+%7D
 %0A
+%7B%25 if parameters %25%7D
 %0APar
@@ -297,26 +297,216 @@
 ------%0A%7B
-parameters
+%25 for param in parameters -%25%7D%0A%7B%7Bparam.name%7D%7D : %7B%7Bparam.type%7D%7D, optional%0A    %7B%7B %22%25s %5Bdefault=%25s %25s%5D%22%7Cformat(param.desc, param.value, param.units)%7Ctrim%7Cwordwrap(70)%7Cindent(4) %7D%7D%0A%7B%25 endfor %25%7D%0A%7B%25 endif -%25
 %7D%0A%0AExamp
@@ -550,22 +550,24 @@
  import 
+%7B
 %7Bname%7D
+%7D
 %0A%3E%3E%3E mod
@@ -571,22 +571,24 @@
 model = 
+%7B
 %7Bname%7D
+%7D
 ()%0A%3E%3E%3E (
@@ -2032,32 +2032,211 @@
     
-return _DOCSTRING.format
+params = meta%5B'defaults'%5D.values()%0A    params.sort(key=lambda p: p.name)%0A%0A    env = jinja2.Environment(loader=jinja2.DictLoader(%7B'docstring': _DOCSTRING%7D))%0A    return env.get_template('docstring').render
 (%0A  
@@ -2270,16 +2270,45 @@
 %0A       
+ parameters=params,%0A        #
  paramet

Use jinja template for docstring.