# codegenExample.py - Utilities for Code Generation with the ElementTree # Library # Launch the code with the -v option to see the test output: # python codegenExample.py -v # This code is released under the BSD License below. # # Copyright (c) 2008-2009, Jason Breti (http://breti.org/codegen) # # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # * Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # * The name of the contributor may not be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. #------------------------------------------------------------------------------ import sys import xml.etree.ElementTree as et import codegenUtilitiesWithDictionary as cgud import codegenUtilitiesAutomatic as cgua #------------------------------------------------------------------------------ # # The following set of classes are intended to demonstrate how to create a # code generation system using a dictionary based factory method. The # dictionary is passed into the factory based on the phase of the codegen. # In this example the codegen phase is the SampleCodegenPhaseWithDictionary # class. # #------------------------------------------------------------------------------ class CodegenHandler_Node_Sample(cgud.CodegenHandler): def process(self): print 'Do something interesting with node', self.thisNode.tag #------------------------------------------------------------------------------ class SampleCodegenPhaseWithDictionary(cgud.CodegenPhase): """ Example Usage: >>> root = et.Element("root") >>> names = ['a', 'b', 'c'] >>> for name in names: ... child = et.SubElement(root, name) ... for subname in names: ... childname = name + subname ... ignore = et.SubElement(child, childname) >>> phase = SampleCodegenPhaseWithDictionary(root) >>> phase.process() Do something interesting with node aa Do something interesting with node ab Do something interesting with node ba Do something interesting with node bc Do something interesting with node cb Do something interesting with node cc """ nodeLookup = { 'aa' : CodegenHandler_Node_Sample, 'ab' : CodegenHandler_Node_Sample, 'ba' : CodegenHandler_Node_Sample, 'bc' : CodegenHandler_Node_Sample, 'cb' : CodegenHandler_Node_Sample, 'cc' : CodegenHandler_Node_Sample, } #------------------------------------------------------------------------------ # # The following set of classes are intended to demonstrate how to create a # code generation system using an automatic factory method. The classes # used by the factory method are automatically determined based on the name # of the node. # In this example the codegen phase is the SampleCodegenPhaseAutomatic # class. # #------------------------------------------------------------------------------ class CodegenHandler_Automatic(cgua.CodegenHandler): def process(self): print 'Do something interesting with node', self.thisNode.tag #------------------------------------------------------------------------------ class Sample_a(cgua.CodegenHandler_HandleChildNodes): """ This class does not extend any methods from the base class. """ #------------------------------------------------------------------------------ class Sample_aa(CodegenHandler_Automatic): """ This class does not extend any methods from the base class. """ #------------------------------------------------------------------------------ class Sample_ab(CodegenHandler_Automatic): """ This class does not extend any methods from the base class. """ #------------------------------------------------------------------------------ class Sample_ac(cgua.CodegenHandler_HandleChildNodes): """ This class does not extend any methods from the base class. """ #------------------------------------------------------------------------------ class Sample_b(cgua.CodegenHandler_HandleChildNodes): """ This class does not extend any methods from the base class. """ #------------------------------------------------------------------------------ class Sample_ba(CodegenHandler_Automatic): """ This class does not extend any methods from the base class. """ #------------------------------------------------------------------------------ class Sample_bb(cgua.CodegenHandler_HandleChildNodes): """ This class does not extend any methods from the base class. """ #------------------------------------------------------------------------------ class Sample_bc(CodegenHandler_Automatic): """ This class does not extend any methods from the base class. """ #------------------------------------------------------------------------------ class Sample_c(cgua.CodegenHandler_HandleChildNodes): """ This class does not extend any methods from the base class. """ #------------------------------------------------------------------------------ class Sample_ca(cgua.CodegenHandler_HandleChildNodes): """ This class does not extend any methods from the base class. """ #------------------------------------------------------------------------------ class Sample_cb(CodegenHandler_Automatic): """ This class does not extend any methods from the base class. """ #------------------------------------------------------------------------------ class Sample_cc(CodegenHandler_Automatic): """ This class does not extend any methods from the base class. """ #------------------------------------------------------------------------------ class SampleCodegenPhaseAutomatic(cgua.CodegenPhase): """ Example Usage: >>> root = et.Element("root") >>> names = ['a', 'b', 'c'] >>> for name in names: ... child = et.SubElement(root, name) ... for subname in names: ... childname = name + subname ... ignore = et.SubElement(child, childname) >>> phase = SampleCodegenPhaseAutomatic(root, "Sample_") >>> phase.process() Do something interesting with node aa Do something interesting with node ab Do something interesting with node ba Do something interesting with node bc Do something interesting with node cb Do something interesting with node cc """ #------------------------------------------------------------------------------ def main(args): # You can run this module independently to test out the "Example Usage:" # listed at the top of each class. import doctest doctest.testmod() #------------------------------------------------------------------------------ if __name__ == '__main__': main(sys.argv[1:])