|
From: <ki...@us...> - 2003-01-13 23:52:25
|
Update of /cvsroot/pymerase/pymerase/output
In directory sc8-pr-cvs1:/tmp/cvs-serv27913
Added Files:
CreatePyTkDBWidgets.py
Log Message:
Generate DB aware Tkinter Widgets
--- NEW FILE: CreatePyTkDBWidgets.py ---
###########################################################################
# #
# C O P Y R I G H T N O T I C E #
# Copyright (c) 2002 by: #
# * California Institute of Technology #
# #
# All Rights Reserved. #
# #
# Permission is hereby granted, free of charge, to any person #
# obtaining a copy of this software and associated documentation files #
# (the "Software"), to deal in the Software without restriction, #
# including without limitation the rights to use, copy, modify, merge, #
# publish, distribute, sublicense, and/or sell copies of the Software, #
# and to permit persons to whom the Software is furnished to do so, #
# subject to the following conditions: #
# #
# The above copyright notice and this permission notice shall be #
# included in all copies or substantial portions of the Software. #
# #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, #
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF #
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND #
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS #
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN #
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN #
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE #
# SOFTWARE. #
###########################################################################
#
# Authors: Brandon King
# Last Modified: $Date: 2003/01/13 23:52:21 $
#
"""Creates Python TK Widgets of each Class/Table"""
import os
import sys
import re
import glob
import shutil
from output.PyTkWidgets import HelperUtil
from output.PyTkWidgets.Templates import Templates
from util import PymeraseType
from ClassMembers import getAllAttributes
from ClassMembers import getAllAssociations
############################
# Globals
TRANSLATOR_NAME='CreatePyTkWidgets'
DBAPI_TRANSLATOR='CreateDBAPI'
codeTemplates = Templates()
def checkDestination(destination):
"""
Checks to see if the destination path exists, if it doesn't it creates the directory and moves into it.
"""
destination = os.path.abspath(destination)
if os.path.exists(destination) == 0:
os.mkdir(destination)
elif os.path.isdir(destination) == 0:
print "%s exists but is not a directory." % (destination)
sys.exit(2)
###############################################
#CreateTkWidgets write function -- called by pymerase
def write(destination, classList):
"""
Creates PyTk Widgets in destination dirctory.
"""
templateDict = {}
print ""
print "\a"
templateDict['%DBAPI%'] = raw_input("Enter DBAPI Name: ")
print ""
print ""
checkDestination(destination)
util = HelperUtil.HelperUtil()
#Iterate through the tables/classes and process the data
for myClass in classList:
code = codeTemplates.getDbTemplate()
#Replace Template %CLASSNAME% with actual class name
code = re.sub('%CLASSNAME%', myClass.getName(DBAPI_TRANSLATOR), code)
#Reset Grid Layout rowCounter
util.resetRowCounter()
for attrib in getAllAttributes(classList, myClass, DBAPI_TRANSLATOR):
type = attrib.getType().getSQLType()
print "Processing(%s:%s)" % (myClass.getName(TRANSLATOR_NAME), type)
#Process Foriegn keys
if attrib.isPrimaryKey() or type == "serial":
print 'Ignoring Primary Key'
#Process Integers and Doubles
if type == "integer":
code = re.sub('%SAVE_FUNCTION%',
util.makeSaveLabelEntry(attrib.getName(DBAPI_TRANSLATOR)),
code)
code = re.sub('%LOAD_FUNCTION%',
util.makeLoadLabelEntry(attrib.getName(DBAPI_TRANSLATOR)),
code)
elif type == "double precision":
code = re.sub('%SAVE_FUNCTION%',
util.makeSaveLabelEntry(attrib.getName(DBAPI_TRANSLATOR)),
code)
code = re.sub('%LOAD_FUNCTION%',
util.makeLoadLabelEntry(attrib.getName(DBAPI_TRANSLATOR)),
code)
elif type == "name":
print "FIXME: Ignoring name type"
#Process Text
elif type == "text":
code = re.sub('%SAVE_FUNCTION%',
util.makeSaveLabelText(attrib.getName(DBAPI_TRANSLATOR)),
code)
code = re.sub('%LOAD_FUNCTION%',
util.makeLoadLabelText(attrib.getName(DBAPI_TRANSLATOR)),
code)
#Process Variable Characters
elif PymeraseType.isVarchar(type):
code = re.sub('%SAVE_FUNCTION%',
util.makeSaveLabelEntry(attrib.getName(DBAPI_TRANSLATOR)),
code)
code = re.sub('%LOAD_FUNCTION%',
util.makeLoadLabelEntry(attrib.getName(DBAPI_TRANSLATOR)),
code)
#Process Characters
elif PymeraseType.isChar(type):
code = re.sub('%SAVE_FUNCTION%',
util.makeSaveLabelEntry(attrib.getName(DBAPI_TRANSLATOR)),
code)
code = re.sub('%LOAD_FUNCTION%',
util.makeLoadLabelEntry(attrib.getName(DBAPI_TRANSLATOR)),
code)
#Process Boolean
elif type == "boolean":
code = re.sub('%SAVE_FUNCTION%',
util.makeSaveRadioBoolean(attrib.getName(DBAPI_TRANSLATOR)),
code)
code = re.sub('%LOAD_FUNCTION%',
util.makeLoadRadioBoolean(attrib.getName(DBAPI_TRANSLATOR)),
code)
#Process Time Stamps
elif type == "timestamp with time zone":
print "FIXME: Ignoring timestamp"
#Write out what is not being handled.
else:
print ""
print "Table(%s), Type(%s), Attribute(%s) not processed." % \
(myClass.getName(TRANSLATOR_NAME),
type,
attrib.getName(TRANSLATOR_NAME))
print "Please e-mail the above line to pym...@li..."
print ""
#Remove '%*%'
code = re.sub('%SAVE_FUNCTION%', '', code)
code = re.sub('%LOAD_FUNCTION%', '', code)
#Write TkWidget to file
fileName = "%sDbWidget.py" % (myClass.getName(DBAPI_TRANSLATOR))
filePath = os.path.join(os.path.abspath(destination), fileName)
f = open(filePath, 'w')
f.write(code)
f.close()
print os.linesep \
+ "Python Tkinter DB Aware Widget Generation Complete... Good Bye." \
+ os.linesep
|