7 def __init__(self, line, predefinedMaterials):
8 if len(line.split(
'"')) < 5
or len(line.split(
'"')[4].
split()) < 3:
9 raise Exception(
"not a well formed Constituent: "+constString)
25 if matString ==
"" or not matString[0] ==
"#":
26 raise Exception(
"not a valid material Definition: "+matString)
29 if len( line.split(
'"') ) < 5
or len( line.split(
'"')[4].
split() ) < 2:
30 raise Exception(
"not a well formed Material Definition: "+matString)
47 pWeight = con.theVolume * con.theDensity / totMass
48 invX0 += pWeight / con.theX0
49 invL0 += pWeight / con.theL0
50 result[
"Volume"] += con.theVolume
51 result[
"Density"] = self.
getMass() / result[
"Volume"]
52 result[
"X0"] = 1 / ( invX0 * result[
"Density"] )
53 result[
"L0"] = 1 / ( invL0 * result[
"Density"] )
62 result[
"X0"] *= fraction
63 result[
"L0"] *= fraction
70 result += con.theMass * con.theCount
74 if constString ==
"" or not constString[0] ==
"*":
75 raise Exception(
"not a valid Constituent: "+constString)
76 line = constString[1:]
77 self.theConstituents.append(
Constituent(line, predefinedMaterials) )
79 number = int( line.split(
'"')[0].
split()[0] )
93 for line
in inFile.split(
"\n"):
95 if i < int(config[
"parsing"][
"intro"]):
97 if not line ==
"" and line[0] ==
"#":
98 material =
Material( line , comment )
99 if not material.theName ==
"END":
100 materials.append( material )
104 elif not line ==
"" and line[0] ==
"*":
106 materials[-1].addConstituent(line, predefinedMaterials)
109 for char
in config[
"parsing"][
"ignorelines"]:
110 if len(line.strip()) == line.strip().
count(char)
and not len(line) == 0:
118 if comment.find(
"<twiki>") >= 0:
119 result = comment.split(
"twiki>")[1][:-2]
121 result +=
"<verbatim>\n"
123 result +=
"</verbatim>\n"
127 result = config[
"twiki"][
"pagename"]+
"\n"
128 result +=
"%TOC%\n\n"
129 for mat
in materials:
130 result += config[
"twiki"][
"heading"]+mat.theName+
"\n"
131 result +=
"short Description: *"+mat.theDescription+
"* <br>\n"
132 result +=
"Mass: %.3fg\n" % mat.getMass()
133 result += config[
"twiki"][
"tableformat"]+
'\n'
134 result +=
"| ** | *Volume* | *Density* | *X<sub>0</sub>* | *λ<sub>0</sub>* |\n"
135 result +=
"| ** | *cms<sup>3</sup>* | *g cm<sup>-3</sup>* | *cm* | *cm* |\n"
136 result +=
"| Real | %(Volume).3f | %(Density).3f | %(X0).3f | %(L0).3f |\n" % mat.getRealValues()
137 result +=
"| Simulation | %(Volume).3f | %(Density).3f | %(X0).3f | %(L0).3f |\n" % mat.getSimValues()
138 result +=
"\n"+config[
"twiki"][
"subheading"]+
"Comments\n"
140 result +=
"\n---+++!!Material\n"
141 result += config[
"twiki"][
"tableformat"]+
'\n'
142 result +=
" | *Description* | *Material Name* | *Volume* | *Mass* | *Count* | *Type* | *X<sub>0</sub>* | *λ<sub>0</sub>* |\n"
143 result +=
' | ** | ** | *cm<sup>3</sup>* | *g* | ** | ** | *g cm<sup>-2</sup>* | *g cm<sup>-2</sup>* |\n'
144 for const
in mat.theConstituents:
145 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 )
153 file = open(fileName,
"r")
157 name = line.split(
'"')[1]
158 content = line.split(
'"')[2].
split()
159 result[name] = content
165 for option
in config.options(section):
166 result[option] = config.get(section,option)
171 config = ConfigParser.ConfigParser()
172 config.read(fileName)
183 optParser = optparse.OptionParser()
184 optParser.add_option(
"-i",
"--input", dest=
"inFile",
185 help=
"the .in material description that is the input", metavar=
"INPUT")
186 optParser.add_option(
"-o",
"--output", dest=
"output",
187 help=
"the file to put the twiki formated output in (default: <.in-Filename>.twiki)", metavar=
"TWIKIOUT")
188 optParser.add_option(
"-c",
"--config", dest=
"config",
189 help=
"configuration to use (default twikiConfig.ini)", metavar=
"CONFIG")
191 (options, args) = optParser.parse_args()
193 if options.inFile ==
None:
195 if options.output ==
None:
196 options.output = options.inFile.replace(
".in",
".twiki")
197 if options.config ==
None:
198 options.config =
"twikiConfig.ini"
202 predefinedMaterials = {}
206 inFile = open(options.inFile,
"r")
207 inFileContent = inFile.read()
210 materials = parseInFile(inFileContent, predefinedMaterials, config)
211 twikiString = getTwiki(materials, config)
213 outFile = open(options.output,"w")
214 outFile.write(twikiString)