#! /usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import argparse
from datetime import date, timedelta
from dateutil.relativedelta import relativedelta


def print_echo_date(echo_date, format):
    print(echo_date.strftime(format))


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-s", "--start", help="start month(format: yyyymm)")
    parser.add_argument("-e", "--end", help="end month(format: yyyymm)")
    parser.add_argument("-p",
                        "--periods",
                        help="periods of periods to generate(default: 12)",
                        default='12')
    parser.add_argument("-f",
                        "--format",
                        help="echo month format",
                        default='%Y%m')
    parser.add_argument("-r",
                        "--reverse",
                        help="reverse echo result(Y/N)",
                        default='N')

    args = parser.parse_args()
    parser = argparse.ArgumentParser()
    start_date = getattr(args, 'start')
    end_date = getattr(args, 'end')
    flag_reverse = getattr(args, 'reverse')
    try:
        periods = int(getattr(args, 'periods'))
    except:
        print('Error on periods must be integer')
        exit(1)

    format = getattr(args, 'format')

    try:
        if not start_date:
            start_date = date.today()
            start_date = date(start_date.year, start_date.month, 1)
        else:
            start_date = date(int(start_date[:4]), int(start_date[4:6]), 1)
    except:
        print('Error start_date format')
        exit(1)

    try:
        start_date.strftime(format)
    except:
        print('Error echo format: {format}'.format(format=format))
        exit(1)

    if flag_reverse.upper() not in ('Y', 'N'):
        print('Error reverse parameters must be "Y" or "N"')
        exit(1)

    flag_reverse = flag_reverse.upper()

    if end_date:
        try:
            end_date = date(int(end_date[:4]), int(end_date[4:6]), 1)
        except:
            print('Error end_date format')
            exit(1)
        if start_date.year == end_date.year and start_date.month == end_date.month:
            need_months = 1
        else:
            need_months = int((end_date - start_date).days / 30) + 1
        if flag_reverse == 'Y':
            for x in reversed(range(need_months)):
                echo_date = (start_date + relativedelta(months=x))
                print_echo_date(echo_date, format)
        else:
            for x in range(need_months):
                echo_date = (start_date + relativedelta(months=x))
                print_echo_date(echo_date, format)
        exit()

    if flag_reverse == 'Y':
        for x in reversed(range(periods)):
            echo_date = (start_date + relativedelta(months=x))
            print_echo_date(echo_date, format)
    else:
        for x in range(periods):
            echo_date = (start_date + relativedelta(months=x))
            print_echo_date(echo_date, format)


if __name__ == '__main__':
    main()
