[Modeling-cvs] ProjectModeling/Modeling/ModelMasons ModelMason.py,1.6,1.6.2.1 PyModelMason.py,1.8.2.
Status: Abandoned
Brought to you by:
sbigaret
From: <sbi...@us...> - 2003-05-18 23:28:29
|
Update of /cvsroot/modeling/ProjectModeling/Modeling/ModelMasons In directory sc8-pr-cvs1:/tmp/cvs-serv17811/ModelMasons Modified Files: Tag: brch-0_9pre6-1-ModelMasons_base_generation_scheme ModelMason.py PyModelMason.py Log Message: * Updated documentation for ModelMason and PyModelMason * Added fake_mode to ModelMason, PyModelMason and option -n/--dry-run in mdl_generate_python_code * scripts/mdl_generate_python_code (option -B), PyModelMason.checkModelIsValid()): the 'base' scheme cannot generate a python-package from a model where a class and at least one of its (direct or indirect) subclasses leave in the same module. This is now checked and correctly reported when the generation cannot be done. Index: ModelMason.py =================================================================== RCS file: /cvsroot/modeling/ProjectModeling/Modeling/ModelMasons/ModelMason.py,v retrieving revision 1.6 retrieving revision 1.6.2.1 diff -C2 -d -r1.6 -r1.6.2.1 *** ModelMason.py 20 Apr 2003 14:16:42 -0000 1.6 --- ModelMason.py 18 May 2003 23:28:24 -0000 1.6.2.1 *************** *** 24,30 **** """ - ModelMason ! Documentation forthcoming. See PyModelMason for an example of use. CVS information --- 24,34 ---- """ ! ModelMason contains the base class for every ''mason'' and is of no interest ! except if you plan to design a new module to generate code/files from a Model. ! ! When this is the case, the class' documentation indicate the rules that should ! be respected to ensure easy integration with the framework's tools such as the ! script mdl_generate_python_code or the ZModeler. CVS information *************** *** 40,46 **** class ModelMason: def __init__(self, model, rootPath, concreteBuilder, bricksDir, ! verbose_mode=0): """ Initializes the ModelMason so that the built files are based on the --- 44,84 ---- class ModelMason: + """ + This class should be the base class for every masons. + + When subclassing this class, you should take care of: + + - call ModelMason.__init__() if it is overridden + + - if the subclass needs to change/create sth. on the filesystem, it *must* + check whether 'fake_mode' is set: if it is set, absolutely no changes + should be made on the disk. + + Methods createEmptyFile(), createFileFromTemplate(), copyFile() and + build_package() can be used without this precaution since they already + check 'self.fake_mode' before making any changes on the disk. + + - call log() to record any action relative to the generation (such as the + creation of a file). Please note that you should be ready to log these + actions even when fake_mode is set. For example, suppose 'file.py' + should be generated but not overwritten; if the file does not exist + you'd log('Creating file file.py'), and if it exists you'd log('File.py + exists, skipping') whether fake_mode is set or not. This makes it + possible for the user to see what would happen whene (re)generating the + code without actually making the changes. + + Following these rules makes it easy to integrate a custom ''mason'' into + the script mdl_generate_python_code and the ZModeler. + + All subclasses need to override build() and put there the logic which + generates the code. You will probably override method tmpl_namespace() as + well (see its documentation for details). + + You can also refer to PyModelMason for an example of use. + + """ def __init__(self, model, rootPath, concreteBuilder, bricksDir, ! verbose_mode=0, fake_mode=0): """ Initializes the ModelMason so that the built files are based on the *************** *** 64,67 **** --- 102,108 ---- generating the files + fake_mode -- if true, do not create or change any file, just report what + would be done + Subclasses may decide to supply a default value for the product's base directory when parameter 'rootPath' is not supplied. *************** *** 75,79 **** bricksDir) self.verbose_mode=verbose_mode ! def fullPathForBrick(self, aBrick): """ --- 116,121 ---- bricksDir) self.verbose_mode=verbose_mode ! self.fake_mode=fake_mode ! def fullPathForBrick(self, aBrick): """ *************** *** 98,103 **** return self.log('Creating empty file %s\n'%filename) ! f = open(filename,"w") ! f.close() def copyFile(self, templateFilename, destinationFilename,overwrite=0): --- 140,146 ---- return self.log('Creating empty file %s\n'%filename) ! if not self.fake_mode: ! f = open(filename,"w") ! f.close() def copyFile(self, templateFilename, destinationFilename,overwrite=0): *************** *** 113,121 **** return self.log('Creating file %s\n'%destinationFilename) ! _f1 = open(self.fullPathForGeneratedFile(destinationFilename),'w') ! _f2 = open(self.fullPathForBrick(templateFilename),'r') ! _f1.write(_f2.read()) ! _f1.close() ! _f2.close() _marker=[] --- 156,165 ---- return self.log('Creating file %s\n'%destinationFilename) ! if not self.fake_mode: ! _f1 = open(self.fullPathForGeneratedFile(destinationFilename),'w') ! _f2 = open(self.fullPathForBrick(templateFilename),'r') ! _f1.write(_f2.read()) ! _f1.close() ! _f2.close() _marker=[] *************** *** 162,169 **** self.log("File %s exists, skipping\n"%destFile) return ! self.log("Generating %s... " % destFile) ! f = open(destFile,'w') ! f.write("%s"%self.templateObjectForTemplate(template,namespace=namespace)) ! self.log("done\n") def build(self): --- 206,219 ---- self.log("File %s exists, skipping\n"%destFile) return ! if not overwrite: ! self.log("Generating %s" % destFile) ! else: ! self.log("Overwriting %s" % destFile) ! ! if not self.fake_mode: ! f = open(destFile,'w') ! f.write("%s"%self.templateObjectForTemplate(template,namespace=namespace)) ! self.log("... done") ! self.log('\n') def build(self): *************** *** 201,205 **** {'model': self.model} ! Subclasses override this method to provide their own namespace """ return {'model': self.model} --- 251,257 ---- {'model': self.model} ! Subclasses override this method to provide their own namespace. This ! namespace is the default one transmitted to the Cheetah template when no ! specific namespace is passed to method createFileFromTemplate(). """ return {'model': self.model} *************** *** 215,224 **** for pack in string.split(self.model.packageName(), '.')[:-1]: currentPath=os.path.join(currentPath, pack) ! self.log('Creating directory %s... '%currentPath) ! try: ! os.mkdir(currentPath) ! except: self.log('no\n') ! else: self.log('ok\n') ! init=os.path.join(currentPath, '__init__.py') if os.path.exists(init): --- 267,278 ---- for pack in string.split(self.model.packageName(), '.')[:-1]: currentPath=os.path.join(currentPath, pack) ! self.log('Creating directory %s'%currentPath) ! if not self.fake_mode: ! try: ! os.mkdir(currentPath) ! except: self.log('... no') ! else: self.log('... ok') ! self.log('\n') ! init=os.path.join(currentPath, '__init__.py') if os.path.exists(init): *************** *** 226,237 **** else: self.log('Creating %s\n'%init) ! f=open(init,'w') ; f.close() # Last, create self.packagePath ! self.log('Creating directory %s... '%self.packagePath) ! try: ! os.mkdir(self.packagePath) ! except: self.log('no\n') ! else: self.log('ok\n') def log(self, msg): --- 280,294 ---- else: self.log('Creating %s\n'%init) ! if not self.fake_mode: ! f=open(init,'w') ; f.close() # Last, create self.packagePath ! self.log('Creating directory %s'%self.packagePath) ! if not self.fake_mode: ! try: ! os.mkdir(self.packagePath) ! except: self.log('... no') ! else: self.log('... ok') ! self.log('\n') def log(self, msg): Index: PyModelMason.py =================================================================== RCS file: /cvsroot/modeling/ProjectModeling/Modeling/ModelMasons/PyModelMason.py,v retrieving revision 1.8.2.2 retrieving revision 1.8.2.3 diff -C2 -d -r1.8.2.2 -r1.8.2.3 *** PyModelMason.py 16 May 2003 11:04:40 -0000 1.8.2.2 --- PyModelMason.py 18 May 2003 23:28:24 -0000 1.8.2.3 *************** *** 24,31 **** """ ! PyModelMason ! Documentation forthcoming CVS information --- 24,45 ---- """ ! PyModelMason generates the python package, modules and classes described ! in a model, ready to be used w/ the modeling framework. ! Its templates are located in sub-package 'Python_bricks'. The generated code ! is compatible with python v2.1 and v2.2. + Given a model, it can generate the appropriate python code in two different + ways: the flat, or 'compact' scheme, and the 'base' scheme. + + The former one generates all files within a single directory (namely: in + package model.packageName()), none of which (except the models) can be + overwritten when the code is regenerated. + + The so-called 'base' scheme adds a subpackage 'MDL' within the generated + package. All files within MDL/ are ALWAYS overwritten when the python code is + regenerated, while others (in the root package) are never overwritten if they + exist. This is probably the one you want to use if your model changes often. + CVS information *************** *** 98,103 **** class PyModelMason(ModelMason): def __init__(self, model, rootPath=None, verbose_mode=0, ! generation_scheme='compact'): """ Initializes the ModelMason so that the built files are based on the --- 112,118 ---- class PyModelMason(ModelMason): + "See the module's documentation for details" def __init__(self, model, rootPath=None, verbose_mode=0, ! generation_scheme='compact', fake_mode=0): """ Initializes the ModelMason so that the built files are based on the *************** *** 106,117 **** Parameters: ! model -- ! rootPath -- ! verbode_mode -- use_scheme -- 'compact' or 'base' """ import Modeling --- 121,134 ---- Parameters: ! model -- see ModelMason.__init__() ! rootPath -- see ModelMason.__init__() ! verbode_mode -- see ModelMason.__init__() use_scheme -- 'compact' or 'base' + fake_mode -- see ModelMason.__init__() + """ import Modeling *************** *** 121,125 **** ModelMason.__init__(self, model, rootPath, Modeling.ModelMasons.PyModelMason, 'Python_bricks', ! verbose_mode) self._entities=[] # used during build self.generation_scheme=generation_scheme --- 138,142 ---- ModelMason.__init__(self, model, rootPath, Modeling.ModelMasons.PyModelMason, 'Python_bricks', ! verbose_mode, fake_mode) self._entities=[] # used during build self.generation_scheme=generation_scheme *************** *** 145,156 **** def create_model_files(self, xml_path, py_path): # model_<modelName>.xml ! self.log('Generating %s... '%xml_path) ! self.model.saveModelAsXMLFile(xml_path) ! self.log('done\n') ! # model_<modelName>.py ! xml=open(xml_path) ! modelStr=xml.read() ! xml.close() self.createFileFromTemplate(model.model(), py_path, --- 162,177 ---- def create_model_files(self, xml_path, py_path): # model_<modelName>.xml ! self.log('Generating %s'%xml_path) ! if not self.fake_mode: ! self.model.saveModelAsXMLFile(xml_path) ! self.log('... done') ! self.log('\n') # model_<modelName>.py ! if not self.fake_mode: ! xml=open(xml_path) ! modelStr=xml.read() ! xml.close() ! else: ! modelStr='fake' self.createFileFromTemplate(model.model(), py_path, *************** *** 193,201 **** # create 'Base' and model files (.xml/.py) basePath=self.fullPathForGeneratedFile(self.base_dir) ! self.log("Creating directory %s... "%basePath) ! try: ! os.mkdir(basePath) ! except: self.log('no\n') ! else: self.log('ok\n') self.createFileFromTemplate(init_base.init_base(), os.path.join(self.base_dir,"__init__.py"), --- 214,223 ---- # create 'Base' and model files (.xml/.py) basePath=self.fullPathForGeneratedFile(self.base_dir) ! self.log("Creating directory %s"%basePath) ! if not self.fake_mode: ! try: ! os.mkdir(basePath) ! except: self.log('no\n') ! else: self.log('ok\n') self.createFileFromTemplate(init_base.init_base(), os.path.join(self.base_dir,"__init__.py"), |