CMS 3D CMS Logo

Classes | Functions

twikiExport Namespace Reference

Classes

class  Constituent
class  Material

Functions

def getTwiki
def main
def parseComment
def parseInFile
def readConfig
def readMaterialFile
def readSection

Function Documentation

def twikiExport::getTwiki (   materials,
  config 
)

Definition at line 126 of file twikiExport.py.

00127                               :
00128     result = config["twiki"]["pagename"]+"\n"
00129     result += "%TOC%\n\n"
00130     for mat in materials:
00131         result += config["twiki"]["heading"]+mat.theName+"\n"
00132         result += "short Description: *"+mat.theDescription+"* <br>\n"
00133         result += "Mass: %.3fg\n" % mat.getMass()
00134         result += config["twiki"]["tableformat"]+'\n'
00135         result += "| ** | *Volume* | *Density* |  *X<sub>0</sub>* | *&lambda;<sub>0</sub>* |\n"
00136         result += "| ** | *cms<sup>3</sup>* | *g cm<sup>-3</sup>* |  *cm* | *cm* |\n"
00137         result +=  "| Real | %(Volume).3f | %(Density).3f | %(X0).3f | %(L0).3f |\n" % mat.getRealValues()
00138         result +=  "| Simulation | %(Volume).3f | %(Density).3f | %(X0).3f | %(L0).3f |\n" % mat.getSimValues()
00139         result += "\n"+config["twiki"]["subheading"]+"Comments\n"
00140         result += parseComment(mat.theComment)
00141         result += "\n---+++!!Material\n"
00142         result += config["twiki"]["tableformat"]+'\n'
00143         result += "  | *Description* | *Material Name* | *Volume* | *Mass* | *Count* | *Type* | *X<sub>0</sub>* | *&lambda;<sub>0</sub>* |\n"
00144         result += '  | ** | ** | *cm<sup>3</sup>* | *g* | ** | ** | *g cm<sup>-2</sup>* | *g cm<sup>-2</sup>* |\n'
00145         for const in mat.theConstituents:
00146             result += "  | =%s= | =%s= | %.2e | %.2e | %.2f | %s | %.2f | %.2f |\n" % ( const.theDescription , const.theName , const.theVolume , const.theMass , const.theCount , const.theType , const.theX0 , const.theL0 )
00147 #            result += "  | ="+const.theDescription+"= | ="+const.theName+"= | "+str(const.theVolume)+" | "+str(const.theMass)+" | "+str(const.theCount)+" | "+const.theType+" | "+str(const.theX0)+" | "+str(const.theL0)+" |\n"
00148         result += "\n"
00149 
00150     return result
00151 
#reads mixed_materials.input and pure_materials.input
def twikiExport::main ( )

Definition at line 182 of file twikiExport.py.

00183           :
00184     optParser = optparse.OptionParser()
00185     optParser.add_option("-i", "--input", dest="inFile",
00186                   help="the .in material description that is the input", metavar="INPUT")
00187     optParser.add_option("-o", "--output", dest="output",
00188                   help="the file to put the twiki formated output in (default: <.in-Filename>.twiki)", metavar="TWIKIOUT")
00189     optParser.add_option("-c", "--config", dest="config",
00190                   help="configuration to use (default twikiConfig.ini)", metavar="CONFIG")
00191 
00192     (options, args) = optParser.parse_args()
00193 
00194     if options.inFile == None:
00195         raise StandardError, "no .in File given!"
00196     if options.output == None:
00197         options.output = options.inFile.replace(".in",".twiki")
00198     if options.config == None:
00199         options.config = "twikiConfig.ini"
00200     
00201     config = readConfig(options.config)
00202 
00203     predefinedMaterials = {}
00204     predefinedMaterials.update( readMaterialFile("pure_materials.input") )
00205     predefinedMaterials.update( readMaterialFile("mixed_materials.input") )
00206 
00207     inFile = open(options.inFile,"r")
00208     inFileContent = inFile.read()
00209     inFile.close()
00210 
00211     materials = parseInFile(inFileContent, predefinedMaterials, config)
00212     twikiString = getTwiki(materials, config)
00213 
00214     outFile = open(options.output,"w")
00215     outFile.write(twikiString)
00216     outFile.close()
00217 
00218 main()
def twikiExport::parseComment (   comment)

Definition at line 116 of file twikiExport.py.

00117                          :
00118     result = ""
00119     if comment.find("<twiki>") >= 0:
00120         result = comment.split("twiki>")[1][:-2]
00121     else:
00122         result += "<verbatim>\n"
00123         result += comment
00124         result += "</verbatim>\n"        
00125     return result

def twikiExport::parseInFile (   inFile,
  predefinedMaterials,
  config 
)

Definition at line 89 of file twikiExport.py.

00090                                                     :
00091     comment = ""
00092     materials = []
00093     i = 0
00094     for line in inFile.split("\n"):
00095         i=i+1
00096         if i < int(config["parsing"]["intro"]):
00097             continue
00098         if not line == "" and line[0] == "#":
00099             material = Material( line , comment )
00100             if not material.theName == "END":
00101                 materials.append( material )
00102             comment = ""
00103 
00104             #print "Name: "+name+" Description: "+description+" mcVolume: "+str(mcVolume)
00105         elif not line == "" and line[0] == "*":
00106             
00107                 materials[-1].addConstituent(line, predefinedMaterials)
00108         else:
00109             ignore = False
00110             for char in config["parsing"]["ignorelines"]:
00111                 if len(line.strip()) == line.strip().count(char) and not len(line) == 0:
00112                     ignore = True
00113             if not ignore:
00114                 comment += line+"\n"
00115     return materials

def twikiExport::readConfig (   fileName)

Definition at line 170 of file twikiExport.py.

00171                         :
00172     config = ConfigParser.ConfigParser()   
00173     config.read(fileName)
00174     result = {}
00175 #    result["general"] = readSection("general")
00176     result["parsing"] = readSection(config,"parsing")
00177     result["twiki"] = readSection(config,"twiki")
00178 
00179     return result
00180 
00181 
#main
def twikiExport::readMaterialFile (   fileName)

Definition at line 152 of file twikiExport.py.

00153                               :
00154     file = open(fileName,"r")
00155     result = {}
00156     for line in file:
00157         if not line == "\n":
00158             name = line.split('"')[1]
00159             content = line.split('"')[2].split()
00160             result[name] = content
00161     return result
00162 
#reads back one [section] of [config]
def twikiExport::readSection (   config,
  section 
)

Definition at line 163 of file twikiExport.py.

00164                                :
00165     result = {}
00166     for option in config.options(section):
00167         result[option] = config.get(section,option)
00168     return result
00169 
# reads the twikiConfig.ini