# -*- coding: iso-8859-1 -*-
#-----------------------------------------------------------------------------
# Modeling Framework: an Object-Relational Bridge for python
#
# Copyright (c) 2001-2004 Sbastien Bigaret <sbigaret@users.sourceforge.net>
# All rights reserved.
#
# This file is part of the Modeling Framework.
#
# This code is distributed under a "3-clause BSD"-style license;
# see the LICENSE file for details.
#-----------------------------------------------------------------------------
"""
ModelMason
Documentation forthcoming
CVS information
$Id: CMFModelMason.py 974 2006-02-26 14:06:07Z sbigaret $
"""
__version__='$Revision: 974 $'[11:-2]
import time, os, string, sys
from ModelMason import ModelMason
from Cheetah.Template import Template
class CMFModelMason(ModelMason):
def __init__(self, model, rootPath=None):
"""
Initializes the ModelMason so that the built files are based on the
supplied model. Parameter 'rootPath' specifies where the files will be
placed ; if not supplied, the Mason will default to the Zope's 'Products'
folder.
"""
self._model = model
_path = rootPath
if not _path:
# Defaults to the 'Products' folder
# Tweak this if you have set this to another thing
# First check INSTANCE_HOME, then INST_HOME, then PATH
if os.environ.has_key('INSTANCE_HOME'):
_path=os.path.join(os.environ['INSTANCE_HOME'], 'Products')
elif os.environ.has_key('INST_HOME'):
# seems set for a non_inst_home_install
_path=os.path.join([os.environ['INST_HOME']]+\
string.split('/lib/python/Products', '/'))
elif os.environ.has_key('PWD'):
_path=os.path.join([os.environ['PWD']]+\
string.split('/lib/python/Products', '/'))
else:
raise 'Generation Error', \
'Did not found INSTANCE_HOME, INST_HOME or PWD in os.environ ;'\
" please specify a value for parameter 'rootPath'"
self._productBaseDirectory = os.path.join(_path,"%s/" % model.name())
import Modeling
self._bricksBaseDirectory = os.path.join(
os.path.dirname(Modeling.ModelMasons.CMFModelMason.__file__),
"CMFbricks/")
def templateObjectForFile(self,templateFile):
"""
Overrides ModelMason implementation so that the namespace passed to the
Template object contains: self, an Entity, a Model and a Relationship
"""
templateFile= self.fullPathForBrick(templateFile)
# The following lambda forms are needed since the Cheetah framework
# sometimes really wants callable objects. This obviously solves the pb.
_self = lambda p=self: p
entity = lambda p=self._entity: p
model = lambda p=self._model: p
relation= lambda p=self._relation: p
nameSpace = {'myself': _self,
'entity': entity,
'model' : model,
'rel' : relation
}
templateObj = Template(file=templateFile, searchList=[nameSpace])
return templateObj
def build(self):
"-"
try:
os.mkdir(self.productBaseDirectory())
os.mkdir(self.fullPathForGeneratedFile("help"))
os.mkdir(self.fullPathForGeneratedFile("skins"))
os.mkdir(self.fullPathForGeneratedFile("skins/zpt_model_%s" % self._model.name()))
os.mkdir(self.fullPathForGeneratedFile("Extensions"))
self.createEmptyFile("TODO.txt")
self.createEmptyFile("README.txt")
self.createEmptyFile("VERSION.txt")
self.createEmptyFile("INSTALL.txt")
self.createEmptyFile("DEPENDENCIES.txt")
self.createEmptyFile("refresh.txt")
except OSError:
pass
for entity in self._model.entities():
try:
os.mkdir(self.fullPathForGeneratedFile("skins/zpt_%s" % string.lower(entity.className())))
_filename="skins/zpt_%s/%s_icon.gif"%(string.lower(entity.className()),
string.lower(entity.className()))
self.copyFile('data/default.gif', _filename)
except OSError:
pass
# quick and DIRTY
self._entity=self._model.entities()[0]
self._relation = None
modelFileName='model_'+self._model.name()+'.xml'
self._model.saveModelAsXMLFile(self.fullPathForGeneratedFile(modelFileName))
self.createFileFromTemplate("init.tmpl","__init__.py")
self.createFileFromTemplate("Extensions/init.tmpl","Extensions/__init__.py")
self.createFileFromTemplate("Extensions/InstallOrUpdate.tmpl","Extensions/InstallOrUpdate.py")
self.createFileFromTemplate("displayData_py.tmpl","skins/zpt_model_%s/%s_displayData.py" % (self._model.name(), self._model.name()))
self.createFileFromTemplate("formatErrors.pt.tmpl" ,"skins/zpt_model_%s/%s_formatErrors.pt" % (self._model.name(), self._model.name()))
self.createFileFromTemplate("common.tmpl" ,"%sCommon.py" % self._model.name())
# Attention : self._entity is used by method createFileFromTemplate.
for self._entity in self._model.entities():
l_class_name = string.lower(self._entity.className())
class_name = self._entity.className()
self.createFileFromTemplate("class.tmpl","%s.py" % class_name)
self.createFileFromTemplate("class_permission.tmpl","%sPermissions.py" % class_name)
self.createFileFromTemplate("view_form.tmpl","skins/zpt_%s/%s_view_form.pt" % (l_class_name,l_class_name) )
self.createFileFromTemplate("edit_form.tmpl","skins/zpt_%s/%s_edit_form.pt" % (l_class_name,l_class_name) )
self.createFileFromTemplate("folder.tmpl","skins/zpt_%s/%sFolder.pt" % (l_class_name,l_class_name) )
self.createFileFromTemplate("classfolder.tmpl","%sFolder.py" % (class_name))
for self._relation in self._entity.relationships():
if self._relation.isToMany():
self.createFileFromTemplate("Associations.tmpl","skins/zpt_%s/%s_%s_Associations.py" % (l_class_name,class_name,self._relation.name() ) )
self.createFileFromTemplate("associate.tmpl","skins/zpt_%s/%s_%s_associate.pt" % (l_class_name,class_name,string.lower(self._relation.name()) ) )
else:
self.createFileFromTemplate("SimpleAssociation.tmpl","skins/zpt_%s/%s_%s_Association.py" % (l_class_name,class_name,self._relation.name() ) )
#self.createFileFromTemplate("simple_associate.tmpl","skins/zpt_%s/%s_associate.pt" % (l_class_name,string.lower(self._relation.name()) ) )
|