[Modeling-cvs] ProjectModeling/Modeling ChangeLog,1.3,1.4 Model.py,1.17,1.18 ModelSet.py,1.13,1.14
Status: Abandoned
Brought to you by:
sbigaret
From: Sebastien B. <sbi...@us...> - 2004-11-30 17:07:47
|
Update of /cvsroot/modeling/ProjectModeling/Modeling In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv32448/Modeling Modified Files: ChangeLog Model.py ModelSet.py Log Message: * Model.py (_loadModel): Added: now contains the code that was initially in loadModel(), and returns the model AND the detected model_type (xml- or py-model) --this is in particular for future use by mdl_generate_python_code.py * ModelSet.py (ModelSet.addModelFromXML): moved the code which does load the model to Model.loadXMLModel() Index: Model.py =================================================================== RCS file: /cvsroot/modeling/ProjectModeling/Modeling/Model.py,v retrieving revision 1.17 retrieving revision 1.18 diff -C2 -d -r1.17 -r1.18 *** Model.py 20 Jul 2004 06:21:37 -0000 1.17 --- Model.py 30 Nov 2004 17:07:37 -0000 1.18 *************** *** 60,70 **** See also: ModelSet.addModel() ! Parameters: ! ! model -- the model whose conn.dict. should be updated ! ! cfg_path -- the full path to the configuration file, or if omitted or ! None, defaults to the value stored in the env. variable ! MDL_DB_CONNECTIONS_CFG """ --- 60,68 ---- See also: ModelSet.addModel() ! :Parameters: ! - `model`: the model whose conn.dict. should be updated ! - `cfg_path`: the full path to the configuration file, or if omitted or ! ``None``, defaults to the value stored in the env. variable ! ``MDL_DB_CONNECTIONS_CFG`` """ *************** *** 103,136 **** def loadModel(path): """ ! Load a model stored in the file 'path'. The lookup procedure is: ! - if path ends with '.py', we assume it is a python module. This module is ! imported and the following attributes are searched within it, in that order: ! 1. 'model' (either an attribute or a function): if found, we assume ! this is an instance of Modeling.Model.Model and we return the value ! 2. 'pymodel' (id.): if found, we assume this is an instance of ! Modeling.PyModel.Model and we return its 'component' attribute ! If the PyModel has not been built yet, it is build() here. ! 3. 'model_src' (id.): if found, we assume this is a string and return ! the model build from it with ModelSet.addModelFromXML() ! ! - if path ends with '.xml', we assume this is a xml-file and we return the ! model build with ModelSet.addModelFromXML() ! Returns: the loaded Modeling.Model.Model instance ! Raises ValueError if file 'path' cannot be handled, or IOError or ! ImportError if the files does no exists, has errors, etc. ! Parameter: ! path -- the path of the file where the model is stored """ ! if path[-3:]=='.py': import os,imp dir,filename=os.path.dirname(path),os.path.basename(path) --- 101,158 ---- def loadModel(path): """ ! Load a model stored in the file `path`. The lookup procedure is: ! - if `path` ends with ``.py``, we assume it is a python module. This module ! is imported and the following attributes are searched within it, in that order: + + 1. ``model`` (either an attribute or a function): if found, the method + determines whether this is a `Model.Model` or a `PyModel.Model`. + If the PyModel has not been built yet, it is build() here. + + 2. ``pymodel`` (either an attribute or a function): if found, we + assume this is an instance of PyModel.Model and we return + its 'component' attribute. If the PyModel has not been built yet, + it is build() here. + + 3. ``model_src`` (id.): if found, we assume this is a string and the + returned model is built from that xml stream with `loadXMLModel()` + + - if `path` ends with ``.xml``, we assume this is a xml-file and we return + the model build with loadXMLModel() ! Note: this method calls `updateModelWithCFG()` on the model before returning ! it. If you want to have the non-updated model, use `_loadModel` instead. ! :param path: the path of the file where the model is stored ! :return: the loaded Modeling.Model.Model instance ! :raise ValueError: if file 'path' cannot be handled, or IOError or ! ImportError if the files does no exists, has errors, etc. ! """ ! model, model_type = _loadModel(path) ! updateModelWithCFG(model) ! return model ! def _loadModel(path): ! """ ! Note: this method does NOT call updateModelWithCFG() ! ! :return: a tuple ``(model, model_type)``, where ``model`` is the model ! itself, and ``model_type`` a string being either ``"xmlmodel"`` or ! ``"pymodel"`` indicating whether the model was found in a xml stream or in ! a `PyModel`. + """ ! model_type = "xmlmodel" ! ! if path[-4:]=='.xml': ! model=loadXMLModel(open(path, 'rb').read()) ! ! elif path[-3:]=='.py': import os,imp dir,filename=os.path.dirname(path),os.path.basename(path) *************** *** 151,178 **** import PyModel if isinstance(model, PyModel.Model): if not model.is_built: model.build() model=model.component - updateModelWithCFG(model) - return model ! if hasattr(module, 'pymodel'): pymodel=module.pymodel if callable(pymodel): pymodel=pymodel() ! pymodel.build() ! updateModelWithCFG(pymodel.component) ! return pymodel.component ! if hasattr(module, 'model_src'): model_src=module.model_src if callable(model_src): model_src=model_src() from ModelSet import ModelSet ! model=ModelSet().addModelFromXML({'string': model_src}) ! updateModelWithCFG(model) ! return model ! raise ValueError, "Couldn't find any of these attributes in python file '%s': model, pymodel (PyModel) or model_src (xml)"%path except ImportError: --- 173,199 ---- import PyModel if isinstance(model, PyModel.Model): + model_type = "pymodel" if not model.is_built: model.build() model=model.component ! elif hasattr(module, 'pymodel'): ! model_type = "pymodel" pymodel=module.pymodel if callable(pymodel): pymodel=pymodel() ! if not model.is_built: ! pymodel.build() ! model=pymodel.component ! elif hasattr(module, 'model_src'): model_src=module.model_src if callable(model_src): model_src=model_src() from ModelSet import ModelSet ! model=loadXMLModel(model_src) ! else: ! raise ValueError, "Couldn't find any of these attributes in python file '%s': model, pymodel (PyModel) or model_src (xml)"%path except ImportError: *************** *** 186,203 **** finally: if file: file.close() ! elif path[-4:]=='.xml': ! from ModelSet import ModelSet ! return ModelSet().addModelFromXML({'file': path}) ! else: raise ValueError, 'Unable to handle file %s: unrecognized format (filename should end with either with .py or .xml)'%path def searchModel(modelName, path=None, verbose=0): """ ! Searches for the model named 'modelName' by trying loadModel() with the ! following paths: 'pymodel_<modelName>.py', 'model_<modelName>.py' and ! 'model_<modelName>.xml' in the current directory and the MDL/ directory. ! Returns the model, or None if it cannot be found/loaded """ --- 207,250 ---- finally: if file: file.close() ! else: raise ValueError, 'Unable to handle file %s: unrecognized format (filename should end with either with .py or .xml)'%path + return model, model_type + + def loadXMLModel(xml_content): + """ + Loads an xml-model and returns the corresponding Model. + + :param xml_content: a string containing the whole xml stream. If the xml is + stored in a file, it is suggested that the file is opened in binary mode + (for example, ``loadXMLModel(open(file_path, 'rb').read())``) + + + """ + from XMLutils import autoDetectXMLEncoding, XMLImportError, unicodeToStr + from xml.dom.minidom import parseString + + encoding=autoDetectXMLEncoding(xml_content) + xmldoc=parseString(xml_content) + + # Do we have only one model? _TBD: a DTD should be responsible for this! + modelNode=xpath.Evaluate('/model', contextNode=xmldoc) + if len(modelNode)!=1: + raise XMLImportError, 'Only one model in a xml file!' + # Ok, go! + modelNode=modelNode[0] + modelName=modelNode.getAttribute('name') + model=Model(unicodeToStr(modelName, encoding)) + model.initWithXMLDOMNode(modelNode, encoding) + return model + def searchModel(modelName, path=None, verbose=0): """ ! Searches for the model named `modelName` by trying `loadModel()` with the ! following paths: ``pymodel_<modelName>.py``, ``model_<modelName>.py`` and ! ``model_<modelName>.xml`` in the current directory and the MDL/ directory. ! :return: the model, or None if it cannot be found/loaded """ Index: ChangeLog =================================================================== RCS file: /cvsroot/modeling/ProjectModeling/Modeling/ChangeLog,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** ChangeLog 24 Nov 2004 11:26:30 -0000 1.3 --- ChangeLog 30 Nov 2004 17:07:37 -0000 1.4 *************** *** 1,2 **** --- 1,12 ---- + 2004-11-30 Sebastien Bigaret <sbi...@us...> + + * Model.py (_loadModel): Added: now contains the code that was + initially in loadModel(), and returns the model AND the detected + model_type (xml- or py-model) --this is in particular for future + use by mdl_generate_python_code.py + + * ModelSet.py (ModelSet.addModelFromXML): moved the code which + does load the model to Model.loadXMLModel() + 2004-11-24 Sebastien Bigaret <sbi...@us...> Index: ModelSet.py =================================================================== RCS file: /cvsroot/modeling/ProjectModeling/Modeling/ModelSet.py,v retrieving revision 1.13 retrieving revision 1.14 diff -C2 -d -r1.13 -r1.14 *** ModelSet.py 20 Jul 2004 06:21:37 -0000 1.13 --- ModelSet.py 30 Nov 2004 17:07:37 -0000 1.14 *************** *** 16,20 **** The framework'core can only handle one ModelSet at a time: the one returned by ! defaultModelSet(). The module & class main responsabilities are: 1. making sure that all the entities defined in a ModelSet instance have --- 16,20 ---- The framework'core can only handle one ModelSet at a time: the one returned by ! `defaultModelSet()`. The module & class main responsabilities are: 1. making sure that all the entities defined in a ModelSet instance have *************** *** 25,30 **** 2. registering the object returned by defaultModelSet() as the default receiver for the notification ! ClassDescriptionNeededForEntityNameNotification posted by ! classDescriptionForName() in module ClassDescription. --- 25,30 ---- 2. registering the object returned by defaultModelSet() as the default receiver for the notification ! `ClassDescriptionNeededForEntityNameNotification` posted by ! `ClassDescription.classDescriptionForName()`. *************** *** 38,42 **** from Modeling.utils import isaValidName, base_persistent_object - from Modeling.XMLutils import * from Modeling.Model import ModelError, Model from Modeling.ClassDescription import ClassDescriptionNeededForEntityNameNotification --- 38,41 ---- *************** *** 45,49 **** from Modeling import ClassDescription import types - from xml.dom.minidom import parseString from Modeling.logging import error, warn --- 44,47 ---- *************** *** 160,181 **** #import pdb; pdb.set_trace() if xmlSource.has_key('string'): ! encoding=autoDetectXMLEncoding(xmlSource['string']) ! xmldoc=parseString(xmlSource['string']) elif xmlSource.has_key('file'): f=open(xmlSource['file'], 'rb') ! encoding=autoDetectXMLEncoding(f.read()) f.close() - xmldoc=parseString(open(xmlSource['file']).read()) else: raise AttributeError, "xmlSource parameter has no key 'string' or 'file'" ! # Do we have only one model? _TBD: a DTD should be responsible for this! ! modelNode=xpath.Evaluate('/model', contextNode=xmldoc) ! if len(modelNode)!=1: ! raise XMLImportError, 'Only one model in a xml file!' ! # Ok, go! ! modelNode=modelNode[0] ! modelName=modelNode.getAttribute('name') ! model=Model(unicodeToStr(modelName, encoding)) ! model.initWithXMLDOMNode(modelNode, encoding) self.addModel(model) return model --- 158,170 ---- #import pdb; pdb.set_trace() if xmlSource.has_key('string'): ! xml_content=xmlSource['string'] elif xmlSource.has_key('file'): f=open(xmlSource['file'], 'rb') ! xml_content=f.read() f.close() else: raise AttributeError, "xmlSource parameter has no key 'string' or 'file'" ! ! model=Model.loadXMLModel(xml_content) self.addModel(model) return model *************** *** 206,211 **** if not _model: return None - #doc=createDOMDocumentObject() - #root = doc.documentElement return _model.getXMLDOM(doc=None, parentNode=None, encoding=encoding) --- 195,198 ---- |