summaryrefslogtreecommitdiffstats
path: root/scripts/func-create-module
blob: 4786cb0de34d1a3b53078c1726483819fe6a9430 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/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 ..."