#-----------------------------------------------------------------------------
# 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')
|