ApplicationModelSet.py :  » Database » Modeling-Framework » Modeling-0.9 » Modeling » utilities » 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 » utilities » ApplicationModelSet.py
#-----------------------------------------------------------------------------
# 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.
#-----------------------------------------------------------------------------


""" ... describe me please I feel alone...

$Id: ApplicationModelSet.py 932 2004-07-20 06:21:57Z sbigaret $"""

__version__='$Revision: 932 $'[11:-2]

_is_development_mode=0
try:
  from Globals import DevelopmentMode
  if DevelopmentMode: _is_development_mode=1
except: pass
print 'Globals :%i'%_is_development_mode

from NotificationFramework import NotificationCenter
from ModelSet import ModelSet
import ClassDescription

from threading import RLock
applicationModelSet_global_lock=RLock()
lock=applicationModelSet_global_lock.acquire
unlock=applicationModelSet_global_lock.release
__application_modelSet__=ModelSet()
__modelsNamesAndPaths__={}
__modelsNamesAndModifTimes__={}

def addModelWithPath(modelPath):
  "Load the model from the supplied (xml) file and registers it"
  lock()
  try:
    data=_readFile(modelPath)
    newModel=addModelFromXML({'string':data})

    __modelsNamesAndPaths__[newModel.name()]=modelPath
    __modelsNamesAndModifTimes__[newModel.name()]=0
    try:
      fp=modelPath
      __modelsNamesAndModifTimes__[newModel.name()]=stat(fp)[8]
    except:
      pass

  finally:
    unlock()

# Wrapping around object methods
def addModelFromXML(xmlSource):
  "Simply wraps the corresponding modelSet method"
  lock()
  try:
    _updateFromFS()
    return __application_modelSet__.addModelFromXML(xmlSource)
  finally: unlock()
    
def classDescriptionForEntityName(entityName):
  "Simply wraps the corresponding modelSet method"
  lock()
  try:
    _updateFromFS()
    return __application_modelSet__.classDescriptionForEntityName(entityName)
  finally: unlock()
    
def requestForClassDescriptionRegistration(notification):
  "Simply wraps the corresponding modelSet method"
  lock()
  try:
    _updateFromFS()
    __application_modelSet__.requestForClassDescriptionRegistration(notification)
  finally: unlock()
    
def getXMLDOMForModelNamed(aModelName):
  "Simply wraps the corresponding modelSet method"
  lock()
  try:
    _updateFromFS()
    return __application_modelSet__.getXMLDOMForModelNamed(aModelName)
  finally: unlock()
    
def getXMLStreamForModelNamed(aModelName):
  "Simply wraps the corresponding modelSet method"
  lock()
  try:
    _updateFromFS()
    return __application_modelSet__.getXMLStreamForModelNamed(aModelName)
  finally: unlock()
    
def entitiesNames():
  "Simply wraps the corresponding modelSet method"
  lock()
  try:
    _updateFromFS()
    return __application_modelSet__.entitiesNames()
  finally: unlock()
    
def entityNamed(aName):
  "Simply wraps the corresponding modelSet method"
  lock()
  try:
    _updateFromFS()
    return __application_modelSet__.entityNamed(aName)
  finally: unlock()
    
def modelForEntityNamed(aName):
  "Simply wraps the corresponding modelSet method"
  lock()
  try:
    _updateFromFS()
    return __application_modelSet__.modelForEntityNamed(aName)
  finally: unlock()
    
def modelNamed(aName):
  "Simply wraps the corresponding modelSet method"
  lock()
  try:
    _updateFromFS()
    return __application_modelSet__.modelNamed(aName)
  finally: unlock()
    
def modelsNames():
  "Simply wraps the corresponding modelSet method"
  lock()
  try:
    _updateFromFS()
    return __application_modelSet__.modelsNames()
  finally: unlock()
    
def models():
  "Simply wraps the corresponding modelSet method"
  lock()
  try:
    _updateFromFS()
    return __application_modelSet__.models()
  finally: unlock()
    
def name():
  "Simply wraps the corresponding modelSet method"
  lock()
  try:
    _updateFromFS()
    return __application_modelSet__.name()
  finally: unlock()
    
def removeModel(name):
  "Simply wraps the corresponding modelSet method"
  lock()
  try:
    __application_modelSet__.removeModel(name)
    del __modelsNamesAndPaths__[name]
    del __modelsNamesAndModifTimes__[name]
    ClassDescription.invalidateClassDescriptionCache()
  finally: unlock()
    
def setName(aName):
  "Simply wraps the corresponding modelSet method"
  lock()
  try: __application_modelSet__.setName(aName)
  finally: unlock()

##
## PRIVATE API follows
##
def __addModel__(aModel):
  "Simply wraps the corresponding modelSet method"
  lock()
  try: return __application_modelSet__.addModel(aModel)
  finally:
    unlock()
    
def modelSet():
  "Returns the application-wide model set"
  lock()
  try:
    _updateFromFS()
    return __application_modelSet__
  finally:
    unlock

def pathForModelNamed(aName):
  "Returns the path registered for the requested model"
  return __modelsNamesAndPaths__.get(aName)

# The 2 following methods were taken from CMFCore.FSObject and e.g.FSimage
#def _readFiles(modelSet, reparse=1):
#  for filePath in __modelsNamesAndPaths__.values():
#    data=_readFile(filePath)
    
def _readFile(filePath):
  fp = filePath
  file = open(fp, 'rb')
  try: data = file.read()
  finally: file.close()
  return data
  
def _updateFromFS():
  lock()
  try:
    if _is_development_mode:
      import os
      print 'lets search'
      hasChanged=0
      for name in __modelsNamesAndPaths__.keys():
        print name
        path=__modelsNamesAndPaths__[name]
        fp = path
        try:    mtime=os.stat(fp)[8]
        except: mtime=0; import sys;print 'except %s %s'%(sys.exc_info()[0],
                                                          sys.exc_info()[1])
        if mtime != __modelsNamesAndModifTimes__[name]:
          hasChanged=1
          __modelsNamesAndModifTimes__[name] = mtime
          data=_readFile(path)
          try:
            __application_modelSet__.removeModel(name)
          except:
            pass
          addModelFromXML({'string': data})
      # If sth. has changed, simply invalidate the cache, that's better
      if hasChanged:
        ClassDescription.invalidateClassDescriptionCache()
  finally:
    unlock()
    
# Register as an observer for
NotificationCenter.addObserver(None,
                               requestForClassDescriptionRegistration,
                               'ClassDescriptionNeededForEntityName')
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.