#!/usr/bin/env python
#
# Copyright 2008, Red Hat, Inc
# Steve 'Ashcrow' Milner <smilner@redhat.com>
# John Eckersberg <jeckersb@redhat.com>
#
# This software may be freely redistributed under the terms of the GNU
# general public license.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
"""
Creates a boilerplate minion module.
"""


TEMPLATE = """\
#
# Copyright %s
# %s <%s>
#
# This software may be freely redistributed under the terms of the GNU
# general public license.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

import func_module


class %s(func_module.FuncModule):

    # Update these if need be.
    version = "0.0.1"
    api_version = "0.0.1"
    description = "%s"

%s
"""


METHOD_TEMPLATE = '''\
    def %s(self):
        """
        TODO: Document me ...
        """
        pass

'''


def populate_template(author_name, author_email, module_name, desc, methods):
    """
    Makes the method strings and populates the template.
    """
    from datetime import datetime
    
    actual_methods = ""
    for method in methods:
        actual_methods += METHOD_TEMPLATE % method
    return TEMPLATE % (datetime.now().strftime("%Y"), author_name, 
                       author_email, module_name, desc, actual_methods[:-2])



def get_email():
    """
    Get and return a valid email address.
    """
    import re

    regx = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$"
    while True:
        author_email = get_input("Email")
        if re.match(regx, author_email) != None:
            break
        print "Please enter a valid email!"
    return author_email


def get_input(prompt):
    """
    Get input and make sure input is given.
    """
    result = raw_input("%s: " % prompt)
    if not result:
        print "Please input the requested information."
        return get_input(prompt)
    return result


if __name__ == '__main__':
    try:
        MODULE_NAME = get_input("Module Name").capitalize()
        DESC = get_input("Description")
        AUTHOR_NAME = get_input("Author")
        AUTHOR_EMAIL = get_email()
        METHODS = []
        print "\nLeave blank to finish."
        while True:
            METHOD = raw_input("Method: ")
            if METHOD == '':
                break
            METHODS.append(METHOD)
        # Write it out to a file
        FILE_NAME = "%s.py" % MODULE_NAME.lower()
        FILE_OBJ = open(FILE_NAME, "w")
        FILE_OBJ.write(populate_template(AUTHOR_NAME, AUTHOR_EMAIL, 
                                      MODULE_NAME, DESC, METHODS))
        FILE_OBJ.close()
        print "Your module is ready to be hacked on. Wrote out to %s." % FILE_NAME
    except KeyboardInterrupt, ex:
        print "\nExiting ..."
