| 1 |
#! /usr/bin/python |
|---|
| 2 |
|
|---|
| 3 |
import sys; |
|---|
| 4 |
import xml.sax; |
|---|
| 5 |
import os; |
|---|
| 6 |
import os.path; |
|---|
| 7 |
|
|---|
| 8 |
import logging; |
|---|
| 9 |
|
|---|
| 10 |
from OptionHandler import OptionHandler; |
|---|
| 11 |
from FCDReader import FCDReader; |
|---|
| 12 |
|
|---|
| 13 |
from TemplateReader import TemplateReader; |
|---|
| 14 |
from TemplateWriter import TemplateWriter; |
|---|
| 15 |
from TemplateFiller import TemplateFiller; |
|---|
| 16 |
|
|---|
| 17 |
class FCDProcess: |
|---|
| 18 |
"""Class containing the main functionality of fcd2code. |
|---|
| 19 |
""" |
|---|
| 20 |
|
|---|
| 21 |
def __init__(self): |
|---|
| 22 |
self.m_log = logging.getLogger("FCDProcess"); |
|---|
| 23 |
|
|---|
| 24 |
self.m_decBaseTemplateName = "TemplateFieldContainerBase_h.txt"; |
|---|
| 25 |
self.m_inlBaseTemplateName = "TemplateFieldContainerBase_inl.txt"; |
|---|
| 26 |
self.m_impBaseTemplateName = "TemplateFieldContainerBase_cpp.txt"; |
|---|
| 27 |
|
|---|
| 28 |
self.m_fieldsBaseTemplateName = "TemplateFieldContainerFields_h.txt"; |
|---|
| 29 |
|
|---|
| 30 |
self.m_decTemplateName = "TemplateFieldContainer_h.txt"; |
|---|
| 31 |
self.m_inlTemplateName = "TemplateFieldContainer_inl.txt"; |
|---|
| 32 |
self.m_impTemplateName = "TemplateFieldContainer_cpp.txt"; |
|---|
| 33 |
|
|---|
| 34 |
self.m_filePrefix = "OSG"; |
|---|
| 35 |
self.m_decFileSuffix = ".h"; |
|---|
| 36 |
self.m_inlFileSuffix = ".inl"; |
|---|
| 37 |
self.m_impFileSuffix = ".cpp"; |
|---|
| 38 |
|
|---|
| 39 |
def setupBundleTemplates(self): |
|---|
| 40 |
self.m_log = logging.getLogger("FCDProcess"); |
|---|
| 41 |
|
|---|
| 42 |
self.m_decBaseTemplateName = "TemplateFieldBundleBase_h.txt"; |
|---|
| 43 |
self.m_inlBaseTemplateName = "TemplateFieldBundleBase_inl.txt"; |
|---|
| 44 |
self.m_impBaseTemplateName = "TemplateFieldBundleBase_cpp.txt"; |
|---|
| 45 |
|
|---|
| 46 |
self.m_fieldsBaseTemplateName = "TemplateFieldBundleFields_h.txt"; |
|---|
| 47 |
|
|---|
| 48 |
self.m_decTemplateName = "TemplateFieldBundle_h.txt"; |
|---|
| 49 |
self.m_inlTemplateName = "TemplateFieldBundle_inl.txt"; |
|---|
| 50 |
self.m_impTemplateName = "TemplateFieldBundle_cpp.txt"; |
|---|
| 51 |
|
|---|
| 52 |
self.m_filePrefix = "OSG"; |
|---|
| 53 |
self.m_decFileSuffix = ".h"; |
|---|
| 54 |
self.m_inlFileSuffix = ".inl"; |
|---|
| 55 |
self.m_impFileSuffix = ".cpp"; |
|---|
| 56 |
|
|---|
| 57 |
def run(self): |
|---|
| 58 |
"""Handles the complete program invocation. |
|---|
| 59 |
""" |
|---|
| 60 |
|
|---|
| 61 |
# parse the commandline options |
|---|
| 62 |
args = sys.argv[:]; |
|---|
| 63 |
|
|---|
| 64 |
if len(args) <= 1: |
|---|
| 65 |
args.append("-h"); |
|---|
| 66 |
|
|---|
| 67 |
OptionHandler.setup(); |
|---|
| 68 |
OptionHandler.parse(args); |
|---|
| 69 |
|
|---|
| 70 |
lookupDicts = []; |
|---|
| 71 |
outPath = "."; |
|---|
| 72 |
rootPath = ""; |
|---|
| 73 |
tmplPath = "."; |
|---|
| 74 |
|
|---|
| 75 |
# setup logging - depends on the verbose command line option. |
|---|
| 76 |
if OptionHandler.getOptionActive("verbose"): |
|---|
| 77 |
if sys.hexversion >= 0x020400F0: |
|---|
| 78 |
logging.basicConfig( |
|---|
| 79 |
level = logging.DEBUG, |
|---|
| 80 |
format = "%(levelname)s %(name)s %(message)s"); |
|---|
| 81 |
else: |
|---|
| 82 |
logging.basicConfig(); |
|---|
| 83 |
logging.getLogger().setLevel(logging.DEBUG); |
|---|
| 84 |
self.m_log.info("Enabling verbose output"); |
|---|
| 85 |
else: |
|---|
| 86 |
if sys.hexversion >= 0x020400F0: |
|---|
| 87 |
logging.basicConfig( |
|---|
| 88 |
level = logging.WARNING, |
|---|
| 89 |
format = "%(levelname)s %(name)s %(message)s"); |
|---|
| 90 |
else: |
|---|
| 91 |
logging.basicConfig(); |
|---|
| 92 |
logging.getLogger().setLevel(logging.WARNING); |
|---|
| 93 |
|
|---|
| 94 |
# special case for missing .fcd file - print usage info |
|---|
| 95 |
if not OptionHandler.getOptionActive("fcdFile"): |
|---|
| 96 |
self.m_log.error("No fcd file given."); |
|---|
| 97 |
OptionHandler.parse(["-h"]); |
|---|
| 98 |
return; |
|---|
| 99 |
|
|---|
| 100 |
# setup output path |
|---|
| 101 |
if OptionHandler.getOptionActive("outPath"): |
|---|
| 102 |
outPath = OptionHandler.getOptionArg("outPath"); |
|---|
| 103 |
self.m_log.info("Setting outPath \"%s\"", outPath); |
|---|
| 104 |
|
|---|
| 105 |
# setup root path |
|---|
| 106 |
if OptionHandler.getOptionActive("rootPath"): |
|---|
| 107 |
rootPath = OptionHandler.getOptionArg("rootPath"); |
|---|
| 108 |
self.m_log.info("Setting rootPath \"%s\"", rootPath); |
|---|
| 109 |
|
|---|
| 110 |
if rootPath == "": |
|---|
| 111 |
if os.environ.has_key("OSG_ROOT"): |
|---|
| 112 |
rootPath = os.environ["OSG_ROOT"]; |
|---|
| 113 |
else: |
|---|
| 114 |
rootPath = os.getcwd(); |
|---|
| 115 |
rootPath = rootPath.replace("/Tools/fcd2code", ""); |
|---|
| 116 |
|
|---|
| 117 |
if rootPath != "": |
|---|
| 118 |
tmplPath = os.path.join(rootPath, "Tools/fcd2code"); |
|---|
| 119 |
self.m_log.info("Setting tmplPath \"%s\"", tmplPath); |
|---|
| 120 |
|
|---|
| 121 |
# setup compatibility mode |
|---|
| 122 |
if OptionHandler.getOptionActive("compatMode"): |
|---|
| 123 |
self.m_log.info("Enabling compatMode"); |
|---|
| 124 |
lookupDicts.append(dict([("CompatOneSix", True)])); |
|---|
| 125 |
else: |
|---|
| 126 |
lookupDicts.append(dict([("CompatOneSix", False)])); |
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
if OptionHandler.getOptionActive("bundleMode"): |
|---|
| 130 |
self.setupBundleTemplates(); |
|---|
| 131 |
|
|---|
| 132 |
# read the .fcd file |
|---|
| 133 |
fcdReader = FCDReader(); |
|---|
| 134 |
fcdFile = OptionHandler.getOptionArg("fcdFile"); |
|---|
| 135 |
|
|---|
| 136 |
fcdReader.read(fcdFile); |
|---|
| 137 |
fcdReader.getFieldContainer()._dumpValues(); |
|---|
| 138 |
|
|---|
| 139 |
fieldContainer = fcdReader.getFieldContainer(); |
|---|
| 140 |
lookupDicts.append(fieldContainer); |
|---|
| 141 |
|
|---|
| 142 |
# write out base files |
|---|
| 143 |
if OptionHandler.getOptionActive("writeBase"): |
|---|
| 144 |
nameBaseCPP = os.path.join(tmplPath, self.m_impBaseTemplateName); |
|---|
| 145 |
nameBaseINL = os.path.join(tmplPath, self.m_inlBaseTemplateName); |
|---|
| 146 |
nameBaseH = os.path.join(tmplPath, self.m_decBaseTemplateName); |
|---|
| 147 |
nameFieldsH = os.path.join(tmplPath, self.m_fieldsBaseTemplateName); |
|---|
| 148 |
|
|---|
| 149 |
if self._verifyFile(nameBaseCPP): |
|---|
| 150 |
tmplBaseCPP = TemplateReader(nameBaseCPP).read(); |
|---|
| 151 |
else: |
|---|
| 152 |
return; |
|---|
| 153 |
|
|---|
| 154 |
if self._verifyFile(nameBaseINL): |
|---|
| 155 |
tmplBaseINL = TemplateReader(nameBaseINL).read(); |
|---|
| 156 |
else: |
|---|
| 157 |
return; |
|---|
| 158 |
|
|---|
| 159 |
if self._verifyFile(nameBaseH): |
|---|
| 160 |
tmplBaseH = TemplateReader(nameBaseH).read(); |
|---|
| 161 |
else: |
|---|
| 162 |
return; |
|---|
| 163 |
|
|---|
| 164 |
if self._verifyFile(nameFieldsH): |
|---|
| 165 |
tmplFieldsH = TemplateReader(nameFieldsH).read(); |
|---|
| 166 |
else: |
|---|
| 167 |
return; |
|---|
| 168 |
|
|---|
| 169 |
fieldContainer.setupFieldContainer(); |
|---|
| 170 |
impBaseName = os.path.join(outPath, |
|---|
| 171 |
self.m_filePrefix + fieldContainer["Classname"] + |
|---|
| 172 |
"Base" + self.m_impFileSuffix); |
|---|
| 173 |
inlBaseName = os.path.join(outPath, |
|---|
| 174 |
self.m_filePrefix + fieldContainer["Classname"] + |
|---|
| 175 |
"Base" + self.m_inlFileSuffix); |
|---|
| 176 |
decBaseName = os.path.join(outPath, |
|---|
| 177 |
self.m_filePrefix + fieldContainer["Classname"] + |
|---|
| 178 |
"Base" + self.m_decFileSuffix); |
|---|
| 179 |
fieldsBaseName = os.path.join(outPath, |
|---|
| 180 |
self.m_filePrefix + fieldContainer["Classname"] + |
|---|
| 181 |
"Fields" + self.m_decFileSuffix); |
|---|
| 182 |
|
|---|
| 183 |
self.m_log.info("filling template for file: %s", impBaseName); |
|---|
| 184 |
outLines = TemplateFiller(tmplBaseCPP).fill(lookupDicts); |
|---|
| 185 |
TemplateWriter(impBaseName, outLines).write(); |
|---|
| 186 |
|
|---|
| 187 |
self.m_log.info("filling template for file: %s", inlBaseName); |
|---|
| 188 |
outLines = TemplateFiller(tmplBaseINL).fill(lookupDicts); |
|---|
| 189 |
TemplateWriter(inlBaseName, outLines).write(); |
|---|
| 190 |
|
|---|
| 191 |
self.m_log.info("filling template for file: %s", decBaseName); |
|---|
| 192 |
outLines = TemplateFiller(tmplBaseH).fill(lookupDicts); |
|---|
| 193 |
TemplateWriter(decBaseName, outLines).write(); |
|---|
| 194 |
|
|---|
| 195 |
self.m_log.info("filling template for file: %s", fieldsBaseName); |
|---|
| 196 |
outLines = TemplateFiller(tmplFieldsH).fill(lookupDicts); |
|---|
| 197 |
TemplateWriter(fieldsBaseName, outLines).write(); |
|---|
| 198 |
|
|---|
| 199 |
# for a decoratable container Foo also write FooDecorator classes. |
|---|
| 200 |
if fieldContainer["isDecoratable"] == True: |
|---|
| 201 |
fieldContainer.setupDecorator(); |
|---|
| 202 |
impBaseName = os.path.join(outPath, |
|---|
| 203 |
self.m_filePrefix + fieldContainer["Classname"] + |
|---|
| 204 |
"Base" + self.m_impFileSuffix); |
|---|
| 205 |
inlBaseName = os.path.join(outPath, |
|---|
| 206 |
self.m_filePrefix + fieldContainer["Classname"] + |
|---|
| 207 |
"Base" + self.m_inlFileSuffix); |
|---|
| 208 |
decBaseName = os.path.join(outPath, |
|---|
| 209 |
self.m_filePrefix + fieldContainer["Classname"] + |
|---|
| 210 |
"Base" + self.m_decFileSuffix); |
|---|
| 211 |
fieldsBaseName = os.path.join(outPath, |
|---|
| 212 |
self.m_filePrefix + fieldContainer["Classname"] + |
|---|
| 213 |
"Fields" + self.m_decFileSuffix); |
|---|
| 214 |
|
|---|
| 215 |
self.m_log.info("filling template for file: %s", impBaseName); |
|---|
| 216 |
outLines = TemplateFiller(tmplBaseCPP).fill(lookupDicts); |
|---|
| 217 |
TemplateWriter(impBaseName, outLines).write(); |
|---|
| 218 |
|
|---|
| 219 |
self.m_log.info("filling template for file: %s", inlBaseName); |
|---|
| 220 |
outLines = TemplateFiller(tmplBaseINL).fill(lookupDicts); |
|---|
| 221 |
TemplateWriter(inlBaseName, outLines).write(); |
|---|
| 222 |
|
|---|
| 223 |
self.m_log.info("filling template for file: %s", decBaseName); |
|---|
| 224 |
outLines = TemplateFiller(tmplBaseH).fill(lookupDicts); |
|---|
| 225 |
TemplateWriter(decBaseName, outLines).write(); |
|---|
| 226 |
|
|---|
| 227 |
self.m_log.info("filling template for file: %s", fieldsBaseName); |
|---|
| 228 |
outLines = TemplateFiller(tmplFieldsH).fill(lookupDicts); |
|---|
| 229 |
TemplateWriter(fieldsBaseName, outLines).write(); |
|---|
| 230 |
|
|---|
| 231 |
# write out FC files |
|---|
| 232 |
if OptionHandler.getOptionActive("writeFC"): |
|---|
| 233 |
nameCPP = os.path.join(tmplPath, self.m_impTemplateName); |
|---|
| 234 |
nameINL = os.path.join(tmplPath, self.m_inlTemplateName); |
|---|
| 235 |
nameH = os.path.join(tmplPath, self.m_decTemplateName); |
|---|
| 236 |
|
|---|
| 237 |
if self._verifyFile(nameCPP): |
|---|
| 238 |
tmplCPP = TemplateReader(nameCPP).read(); |
|---|
| 239 |
else: |
|---|
| 240 |
return; |
|---|
| 241 |
|
|---|
| 242 |
if self._verifyFile(nameINL): |
|---|
| 243 |
tmplINL = TemplateReader(nameINL).read(); |
|---|
| 244 |
else: |
|---|
| 245 |
return; |
|---|
| 246 |
|
|---|
| 247 |
if self._verifyFile(nameH): |
|---|
| 248 |
tmplH = TemplateReader(nameH).read(); |
|---|
| 249 |
else: |
|---|
| 250 |
return; |
|---|
| 251 |
|
|---|
| 252 |
fieldContainer.setupFieldContainer(); |
|---|
| 253 |
impName = os.path.join(outPath, |
|---|
| 254 |
self.m_filePrefix + fieldContainer["Classname"] + self.m_impFileSuffix); |
|---|
| 255 |
inlName = os.path.join(outPath, |
|---|
| 256 |
self.m_filePrefix + fieldContainer["Classname"] + self.m_inlFileSuffix); |
|---|
| 257 |
decName = os.path.join(outPath, |
|---|
| 258 |
self.m_filePrefix + fieldContainer["Classname"] + self.m_decFileSuffix); |
|---|
| 259 |
|
|---|
| 260 |
self.m_log.info("filling template for file: %s", impName); |
|---|
| 261 |
outLines = TemplateFiller(tmplCPP).fill(lookupDicts); |
|---|
| 262 |
TemplateWriter(impName, outLines).write(); |
|---|
| 263 |
|
|---|
| 264 |
self.m_log.info("filling template for file: %s", inlName); |
|---|
| 265 |
outLines = TemplateFiller(tmplINL).fill(lookupDicts); |
|---|
| 266 |
TemplateWriter(inlName, outLines).write(); |
|---|
| 267 |
|
|---|
| 268 |
self.m_log.info("filling template for file: %s", decName); |
|---|
| 269 |
outLines = TemplateFiller(tmplH).fill(lookupDicts); |
|---|
| 270 |
TemplateWriter(decName, outLines).write(); |
|---|
| 271 |
|
|---|
| 272 |
# for a decoratable container Foo also write FooDecorator classes. |
|---|
| 273 |
if fieldContainer["isDecoratable"] == True: |
|---|
| 274 |
fieldContainer.setupDecorator(); |
|---|
| 275 |
impName = os.path.join(outPath, |
|---|
| 276 |
self.m_filePrefix + fieldContainer["Classname"] + self.m_impFileSuffix); |
|---|
| 277 |
inlName = os.path.join(outPath, |
|---|
| 278 |
self.m_filePrefix + fieldContainer["Classname"] + self.m_inlFileSuffix); |
|---|
| 279 |
decName = os.path.join(outPath, |
|---|
| 280 |
self.m_filePrefix + fieldContainer["Classname"] + self.m_decFileSuffix); |
|---|
| 281 |
|
|---|
| 282 |
self.m_log.info("filling template for file: %s", impName); |
|---|
| 283 |
outLines = TemplateFiller(tmplCPP).fill(lookupDicts); |
|---|
| 284 |
TemplateWriter(impName, outLines).write(); |
|---|
| 285 |
|
|---|
| 286 |
self.m_log.info("filling template for file: %s", inlName); |
|---|
| 287 |
outLines = TemplateFiller(tmplINL).fill(lookupDicts); |
|---|
| 288 |
TemplateWriter(inlName, outLines).write(); |
|---|
| 289 |
|
|---|
| 290 |
self.m_log.info("filling template for file: %s", decName); |
|---|
| 291 |
outLines = TemplateFiller(tmplH).fill(lookupDicts); |
|---|
| 292 |
TemplateWriter(decName, outLines).write(); |
|---|
| 293 |
return; |
|---|
| 294 |
|
|---|
| 295 |
def _verifyFile(self, name): |
|---|
| 296 |
if not os.path.exists(name) or not os.path.isfile(name): |
|---|
| 297 |
self.m_log.error("Can not find template file: \"%s\"", name); |
|---|
| 298 |
self.m_log.errir("Did you specify a correct root path ?"); |
|---|
| 299 |
return False; |
|---|
| 300 |
else: |
|---|
| 301 |
return True; |
|---|
| 302 |
|
|---|
| 303 |
# |
|---|
| 304 |
# MAIN |
|---|
| 305 |
# |
|---|
| 306 |
|
|---|
| 307 |
fcdProcess = FCDProcess(); |
|---|
| 308 |
fcdProcess.run(); |
|---|