Generate ASP classes

I wrote a simple python script in order to generate some ASP (classic not dot net) classes:
Here it is as it:

# -*- coding: windows-1252 -*-
# @author: Raphaël Beck
# ex: >> python generateAspClass.py -n Country -f code -f name -f id > Country.asp

import os
import string
import re
from collections import defaultdict
from optparse import OptionParser
from datetime import datetime

name = None


baseStruc = """<%%
' -----------------------------------------------------------------------------
' @File   : /%(name)s.asp
' @Desc   : %(name)s
' @Create : %(date)s
' @Author : R.BECK
' -----------------------------------------------------------------------------
class %(name)s

%(fields)s
    
    ' Constructor
    Private Sub Class_Initialize()
%(init_fields)s
    end sub
    
    ' Destructor    
    'Private Sub Class_Terminate()    
    'end sub
    
    'Getters
    %(getters)s
    
    ' Setters
    %(setters)s

end class
%%>
"""

field_format = "\tprivate %(private_field_name)s\n"

getter_format = """
    Public property get %(field_name)s    
        %(field_name)s = %(private_field_name)s
    End Property
"""

setter_format = """
    Public property let %(field_name)s(p_%(field_name)s)  
        %(private_field_name)s = p_%(field_name)s
    End Property
"""

init_format = """\t\t%(private_field_name)s = 0"""

if __name__ == "__main__":
    parser = OptionParser()
    parser.add_option("-f", "--fields",
                      action="append", dest="fields", default=[],
                      help="Class' fields")

    parser.add_option("-n", "--name", dest="name", 
                      help="Class' name")
                      
    (options, args) = parser.parse_args()
    
    if not options.name:
        print "The name of the class is mandatory"
        exit(-1)
    name = options.name
    
    private_fields = []
    getters = []
    setters = []
    init_fields = []

    for field_name in set(options.fields):
        field_dict = {"field_name": field_name, "private_field_name": "m_%s" % field_name}
        private_fields.append(field_format % field_dict)
        getters.append(getter_format % field_dict)
        setters.append(setter_format % field_dict)
        init_fields.append(init_format % field_dict)
    
    date = datetime.now()
    dateStr = "%s/%s/%s" % (date.day, date.month, date.year)
    
    print baseStruc % {
        "name": name,
        "fields": "".join(private_fields),
        "getters": "\n".join(getters),
        "setters": "\n".join(setters),
        "date": dateStr,
        "init_fields": "\n".join(init_fields),
    }
    
    
    
    
    
    

Usage:

python generateAspClass.py -n Country -f code -f name -f id > Country.asp

You can use the code, modify it (I’ll be happy to see your modifications, in case I have forgotten something) and distribute it (it’ll be great if you link to this site :D).

Advertisements
This entry was posted in asp, python and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s