<commit_before>#! /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) }}

author: {{author}}
version: {{version}}
license: {{license}}
DOI: {{doi}}
URL: {{url}}
{% if parameters %}
Parameters
----------
{% for param in parameters -%}
{{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):
    """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

    parameters = parameters or meta['defaults'].values()
    parameters.sort(key=lambda p: p.name)

    env = jinja2.Environment(loader=jinja2.DictLoader({'docstring': _DOCSTRING}))
    return env.get_template('docstring').render(
        desc=meta['info'].summary,
        name=name,
        parameters=parameters,
        author=meta['info'].author,
        version=meta['info'].version,
        license=meta['info'].license,
        doi=meta['info'].doi,
        url=meta['info'].url,
    )<commit_msg>Add sort filter in template for parameters.<commit_after>#! /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) }}

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):
    """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

    parameters = parameters or meta['defaults'].values()

    env = jinja2.Environment(loader=jinja2.DictLoader({'docstring': _DOCSTRING}))
    return env.get_template('docstring').render(
        desc=meta['info'].summary,
        name=name,
        parameters=parameters,
        author=meta['info'].author,
        version=meta['info'].version,
        license=meta['info'].license,
        doi=meta['info'].doi,
        url=meta['info'].url,
    )
{'fraction_non_alphanumeric': 0.15460873233495043, 'fraction_numerical': 0.004218519299725796, 'mean_word_length': 2.896466721446179, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1532849', 'n_tokens_mistral': 1447, 'n_tokens_neox': 1369, 'n_words': 409}