CMFModelMason.py :  » Database » Modeling-Framework » Modeling-0.9 » Modeling » ModelMasons » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Database » Modeling Framework 
Modeling Framework » Modeling 0.9 » Modeling » ModelMasons » CMFModelMason.py
# -*- 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()) ) )
          
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.