#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (C) 2015. Tšili Lauri Johannes
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from dateutil.relativedelta import relativedelta


def get_time_range(start_date, end_date, step):
    """Get generator of time values within given times

    Method is used to construct generator of time values within given interval.
    The end values are both inclusive.

    Parameters
    ----------
    start_date, end_date : datetime, date or time
        start_date and end_date are the time values between witch the list is
        constructed.
    step : dict
        Dictionary describing the increment between included time values.
        May hold negative values.

    Yields
    ----------
    datetime, date or time
        The type of the yielded object depends on the input variables.

    See Also
    ----------
    dateutil.relativedelta : Refer here for possible values for step.

    Examples
    ----------
    >>> start_date = datetime.date(2010, 1, 1)
    >>> end_date = datetime.date(2010, 3, 1)
    >>> get_time_range(start_date, end_date, months=1)
    date(2010, 1, 1)
    date(2010, 2, 1)
    date(2010, 3, 1)
    """
    start_date = start_date - relativedelta(**step)
    current_step = step.copy()
    while start_date + relativedelta(**step) < end_date +\
            relativedelta(**current_step):
        yield start_date + relativedelta(**step)
        for key in step.keys():
            step[key] = step[key] + current_step[key]


def format_month(month, out_format="digit"):
    """Format month data to desired format

    Method formats month's  name to zero-padded digit string.

    Parameters
    ----------
    month : str
        The month to be converted.
    out_format : str, optional
        Specifies the format into the month needs to be formated.
        Default value is digit.

    Returns
    ----------
    month : str
        The formated data is returned in a string even when the data represents
        an integer.

    Notes
    ----------
    Currently the only supported formating is from a name into a
    zero-padded string digit.

    The input is not case sensitive.

    Examples
    ----------
    >>> format_month('feb')
    '02'

    >>> format_month('aUG')
    '10'
    """
    if out_format is "digit":
        month = month.upper()[:3]
        if month == 'JAN':
            month = "01"
        elif month == 'FEB':
            month = "02"
        elif month == 'MAR':
            month = "03"
        elif month == 'APR':
            month = "04"
        elif month == 'MAY':
            month = "05"
        elif month == 'JUN':
            month = "06"
        elif month == 'JUL':
            month = "07"
        elif month == 'AUG':
            month = "08"
        elif month == 'SEP':
            month = "09"
        elif month == 'OCT':
            month = "10"
        elif month == 'NOV':
            month = "11"
        elif month == 'DEC':
            month = "12"
        else:
            return None
        return month


def f
season, out_format="digit"):
    """Format season data to desired format

    Method formats seasons's  name to zero-padded digit string.

    Parameters
    ----------
    season : str
        The season to be converted.
    out_format : str, optional
        Specifies the format into the season needs to be formated.
        Default value is digit.

    Returns
    ----------
    season : str
        The formated data is returned in a string even when the data represents
        an integer.

    Notes
    ----------
    Currently the only supported formating is from a name into a
    zero-padded string digit.

    The input is not case sensitive.

    Examples
    ----------
    >>> format_season('spR')
    '03'

    >>> format_season('WintE')
    '12'
    """
    if out_format is "digit":
        season = season.upper()[:3]
        if season == 'SPR':
            season = "03"
        elif season == 'SUM':
            season = "06"
        elif season == 'AUT':
            season = "09"
        elif season == 'WIN':
            season = "12"
        else:
            return None
        return season

ormat_season(