| 1 | #! /usr/bin/env python
|
|---|
| 2 | import sys,os
|
|---|
| 3 | import pprint
|
|---|
| 4 |
|
|---|
| 5 | ###############
|
|---|
| 6 | # Notes:
|
|---|
| 7 | # -Strings can include whitespace :-/
|
|---|
| 8 | # -Can a string include a # ?
|
|---|
| 9 | ###############
|
|---|
| 10 |
|
|---|
| 11 | listType= type([])
|
|---|
| 12 | dictType= type({})
|
|---|
| 13 |
|
|---|
| 14 | def _smartInt(str):
|
|---|
| 15 | if str[0:2]=="0x": return int(str,16)
|
|---|
| 16 | else: return int(str)
|
|---|
| 17 |
|
|---|
| 18 | def _smartLong(str):
|
|---|
| 19 | if str[0:2]=="0x": return long(str,16)
|
|---|
| 20 | else: return long(str)
|
|---|
| 21 |
|
|---|
| 22 | def _parseLine(line, contextStack):
|
|---|
| 23 | if len(line)==0:
|
|---|
| 24 | return True
|
|---|
| 25 | words= line.split(None,2)
|
|---|
| 26 | if len(words)==0:
|
|---|
| 27 | return True
|
|---|
| 28 | vecMode= False
|
|---|
| 29 | if words[0][0]=="@":
|
|---|
| 30 | vecMode= True
|
|---|
| 31 | words[0]= words[0][1:]
|
|---|
| 32 | currentName,currentContext,currentTypedefs= contextStack[-1]
|
|---|
| 33 | if len(words)>2:
|
|---|
| 34 | keyword= words[1].upper()
|
|---|
| 35 | if keyword=="STR":
|
|---|
| 36 | if vecMode:
|
|---|
| 37 | res= []
|
|---|
| 38 | vwords= words[2].split(',')
|
|---|
| 39 | for v in vwords:
|
|---|
| 40 | res.append(v.strip())
|
|---|
| 41 | resVal= (keyword,res)
|
|---|
| 42 | if currentContext.has_key(words[0]) \
|
|---|
| 43 | and type(currentContext[words[0]])==listType:
|
|---|
| 44 | currentContext[words[0]].append(resVal)
|
|---|
| 45 | else:
|
|---|
| 46 | currentContext[words[0]]= resVal
|
|---|
| 47 | else:
|
|---|
| 48 | strVal= (keyword,words[2].strip())
|
|---|
| 49 | if currentContext.has_key(words[0]) \
|
|---|
| 50 | and type(currentContext[words[0]])==listType:
|
|---|
| 51 | currentContext[words[0]].append(strVal)
|
|---|
| 52 | else:
|
|---|
| 53 | currentContext[words[0]]= strVal
|
|---|
| 54 | return True
|
|---|
| 55 | # elif keyword=="TAI":
|
|---|
| 56 | # if vecMode:
|
|---|
| 57 | # res= []
|
|---|
| 58 | # vwords= words[2].split(',')
|
|---|
| 59 | # for v in vwords:
|
|---|
| 60 | # res.append(v.strip())
|
|---|
| 61 | # resVal= (keyword,res)
|
|---|
| 62 | # if currentContext.has_key(words[0]) \
|
|---|
| 63 | # and type(currentContext[words[0]])==listType:
|
|---|
| 64 | # currentContext[words[0]].append(resVal)
|
|---|
| 65 | # else:
|
|---|
| 66 | # currentContext[words[0]]= resVal
|
|---|
| 67 | # else:
|
|---|
| 68 | # strVal= (keyword,words[2].strip())
|
|---|
| 69 | # if currentContext.has_key(words[0]) \
|
|---|
| 70 | # and type(currentContext[words[0]])==listType:
|
|---|
| 71 | # currentContext[words[0]].append(strVal)
|
|---|
| 72 | # else:
|
|---|
| 73 | # currentContext[words[0]]= strVal
|
|---|
| 74 | # return True
|
|---|
| 75 | elif keyword=="BOOL":
|
|---|
| 76 | if vecMode:
|
|---|
| 77 | res= []
|
|---|
| 78 | vwords= words[2].split()
|
|---|
| 79 | if len(vwords)==1:
|
|---|
| 80 | vwords= words[2].split(",")
|
|---|
| 81 | for v in vwords:
|
|---|
| 82 | res.append(bool(v.strip()))
|
|---|
| 83 | resVal= (keyword,res)
|
|---|
| 84 | if currentContext.has_key(words[0]) \
|
|---|
| 85 | and type(currentContext[words[0]])==listType:
|
|---|
| 86 | currentContext[words[0]].append(resVal)
|
|---|
| 87 | else:
|
|---|
| 88 | currentContext[words[0]]= resVal
|
|---|
| 89 | else:
|
|---|
| 90 | boolVal= (keyword,bool(words[2].strip()))
|
|---|
| 91 | if currentContext.has_key(words[0]) \
|
|---|
| 92 | and type(currentContext[words[0]])==listType:
|
|---|
| 93 | currentContext[words[0]].append(boolVal)
|
|---|
| 94 | else:
|
|---|
| 95 | currentContext[words[0]]= boolVal
|
|---|
| 96 | return True
|
|---|
| 97 | elif keyword=="S16":
|
|---|
| 98 | if vecMode:
|
|---|
| 99 | res= []
|
|---|
| 100 | vwords= words[2].split()
|
|---|
| 101 | if len(vwords)==1:
|
|---|
| 102 | vwords= words[2].split(",")
|
|---|
| 103 | for v in vwords:
|
|---|
| 104 | res.append(_smartInt(v.strip()))
|
|---|
| 105 | resVal= (keyword,res)
|
|---|
| 106 | if currentContext.has_key(words[0]) \
|
|---|
| 107 | and type(currentContext[words[0]])==listType:
|
|---|
| 108 | currentContext[words[0]].append(resVal)
|
|---|
| 109 | else:
|
|---|
| 110 | currentContext[words[0]]= resVal
|
|---|
| 111 | else:
|
|---|
| 112 | intVal= (keyword,_smartInt(words[2].strip()))
|
|---|
| 113 | if currentContext.has_key(words[0]) \
|
|---|
| 114 | and type(currentContext[words[0]])==listType:
|
|---|
| 115 | currentContext[words[0]].append(intVal)
|
|---|
| 116 | else:
|
|---|
| 117 | currentContext[words[0]]= intVal
|
|---|
| 118 | return True
|
|---|
| 119 | elif keyword=="S32":
|
|---|
| 120 | if vecMode:
|
|---|
| 121 | res= []
|
|---|
| 122 | vwords= words[2].split()
|
|---|
| 123 | if len(vwords)==1:
|
|---|
| 124 | vwords= words[2].split(",")
|
|---|
| 125 | for v in vwords:
|
|---|
| 126 | res.append(_smartInt(v.strip()))
|
|---|
| 127 | resVal= (keyword,res)
|
|---|
| 128 | if currentContext.has_key(words[0]) \
|
|---|
| 129 | and type(currentContext[words[0]])==listType:
|
|---|
| 130 | currentContext[words[0]].append(resVal)
|
|---|
| 131 | else:
|
|---|
| 132 | currentContext[words[0]]= resVal
|
|---|
| 133 | else:
|
|---|
| 134 | intVal= (keyword,_smartInt(words[2].strip()))
|
|---|
| 135 | if currentContext.has_key(words[0]) \
|
|---|
| 136 | and type(currentContext[words[0]])==listType:
|
|---|
| 137 | currentContext[words[0]].append(intVal)
|
|---|
| 138 | else:
|
|---|
| 139 | currentContext[words[0]]= intVal
|
|---|
| 140 | return True
|
|---|
| 141 | elif keyword=="S64":
|
|---|
| 142 | if vecMode:
|
|---|
| 143 | res= []
|
|---|
| 144 | vwords= words[2].split()
|
|---|
| 145 | if len(vwords)==1:
|
|---|
| 146 | vwords= words[2].split(",")
|
|---|
| 147 | for v in vwords:
|
|---|
| 148 | res.append(_smartLong(v.strip()))
|
|---|
| 149 | resVal= (keyword,res)
|
|---|
| 150 | if currentContext.has_key(words[0]) \
|
|---|
| 151 | and type(currentContext[words[0]])==listType:
|
|---|
| 152 | currentContext[words[0]].append(resVal)
|
|---|
| 153 | else:
|
|---|
| 154 | currentContext[words[0]]= resVal
|
|---|
| 155 | else:
|
|---|
| 156 | intVal= (keyword,_smartLong(words[2].strip()))
|
|---|
| 157 | if currentContext.has_key(words[0]) \
|
|---|
| 158 | and type(currentContext[words[0]])==listType:
|
|---|
| 159 | currentContext[words[0]].append(intVal)
|
|---|
| 160 | else:
|
|---|
| 161 | currentContext[words[0]]= intVal
|
|---|
| 162 | return True
|
|---|
| 163 | elif keyword=="U16":
|
|---|
| 164 | if vecMode:
|
|---|
| 165 | res= []
|
|---|
| 166 | vwords= words[2].split()
|
|---|
| 167 | if len(vwords)==1:
|
|---|
| 168 | vwords= words[2].split(",")
|
|---|
| 169 | for v in vwords:
|
|---|
| 170 | res.append(_smartInt(v.strip()))
|
|---|
| 171 | resVal= (keyword,res)
|
|---|
| 172 | if currentContext.has_key(words[0]) \
|
|---|
| 173 | and type(currentContext[words[0]])==listType:
|
|---|
| 174 | currentContext[words[0]].append(resVal)
|
|---|
| 175 | else:
|
|---|
| 176 | currentContext[words[0]]= resVal
|
|---|
| 177 | else:
|
|---|
| 178 | intVal= (keyword,_smartInt(words[2].strip()))
|
|---|
| 179 | if currentContext.has_key(words[0]) \
|
|---|
| 180 | and type(currentContext[words[0]])==listType:
|
|---|
| 181 | currentContext[words[0]].append(intVal)
|
|---|
| 182 | else:
|
|---|
| 183 | currentContext[words[0]]= intVal
|
|---|
| 184 | return True
|
|---|
| 185 | elif keyword=="F32":
|
|---|
| 186 | if vecMode:
|
|---|
| 187 | res= []
|
|---|
| 188 | vwords= words[2].split()
|
|---|
| 189 | if len(vwords)==1:
|
|---|
| 190 | vwords= words[2].split(",")
|
|---|
| 191 | for v in vwords:
|
|---|
| 192 | res.append(float(v.strip()))
|
|---|
| 193 | resVal= (keyword,res)
|
|---|
| 194 | if currentContext.has_key(words[0]) \
|
|---|
| 195 | and type(currentContext[words[0]])==listType:
|
|---|
| 196 | currentContext[words[0]].append(resVal)
|
|---|
| 197 | else:
|
|---|
| 198 | currentContext[words[0]]= resVal
|
|---|
| 199 | else:
|
|---|
| 200 | floatVal= (keyword,float(words[2].strip()))
|
|---|
| 201 | if currentContext.has_key(words[0]) \
|
|---|
| 202 | and type(currentContext[words[0]])==listType:
|
|---|
| 203 | currentContext[words[0]].append(floatVal)
|
|---|
| 204 | else:
|
|---|
| 205 | currentContext[words[0]]= floatVal
|
|---|
| 206 | return True
|
|---|
| 207 | elif keyword=="F64":
|
|---|
| 208 | if vecMode:
|
|---|
| 209 | res= []
|
|---|
| 210 | vwords= words[2].split()
|
|---|
| 211 | if len(vwords)==1:
|
|---|
| 212 | vwords= words[2].split(",")
|
|---|
| 213 | for v in vwords:
|
|---|
| 214 | res.append(float(v.strip()))
|
|---|
| 215 | resVal= (keyword,res)
|
|---|
| 216 | if currentContext.has_key(words[0]) \
|
|---|
| 217 | and type(currentContext[words[0]])==listType:
|
|---|
| 218 | currentContext[words[0]].append(resVal)
|
|---|
| 219 | else:
|
|---|
| 220 | currentContext[words[0]]= resVal
|
|---|
| 221 | else:
|
|---|
| 222 | floatVal= (keyword,float(words[2].strip()))
|
|---|
| 223 | if currentContext.has_key(words[0]) \
|
|---|
| 224 | and type(currentContext[words[0]])==listType:
|
|---|
| 225 | currentContext[words[0]].append(floatVal)
|
|---|
| 226 | else:
|
|---|
| 227 | currentContext[words[0]]= floatVal
|
|---|
| 228 | return True
|
|---|
| 229 | elif keyword=="U64":
|
|---|
| 230 | if vecMode:
|
|---|
| 231 | res= []
|
|---|
| 232 | vwords= words[2].split()
|
|---|
| 233 | if len(vwords)==1:
|
|---|
| 234 | vwords= words[2].split(",")
|
|---|
| 235 | for v in vwords:
|
|---|
| 236 | res.append(_smartLong(v.strip()))
|
|---|
| 237 | resVal= (keyword,res)
|
|---|
| 238 | if currentContext.has_key(words[0]) \
|
|---|
| 239 | and type(currentContext[words[0]])==listType:
|
|---|
| 240 | currentContext[words[0]].append(resVal)
|
|---|
| 241 | else:
|
|---|
| 242 | currentContext[words[0]]= resVal
|
|---|
| 243 | else:
|
|---|
| 244 | longVal= (keyword,_smartLong(words[2].strip()))
|
|---|
| 245 | if currentContext.has_key(words[0]) \
|
|---|
| 246 | and type(currentContext[words[0]])==listType:
|
|---|
| 247 | currentContext[words[0]].append(longVal)
|
|---|
| 248 | else:
|
|---|
| 249 | currentContext[words[0]]= longVal
|
|---|
| 250 | return True
|
|---|
| 251 | elif keyword=="U8":
|
|---|
| 252 | if vecMode:
|
|---|
| 253 | res= []
|
|---|
| 254 | vwords= words[2].split()
|
|---|
| 255 | if len(vwords)==1:
|
|---|
| 256 | vwords= words[2].split(",")
|
|---|
| 257 | for v in vwords:
|
|---|
| 258 | res.append(_smartInt(v.strip()))
|
|---|
| 259 | resVal= (keyword,res)
|
|---|
| 260 | if currentContext.has_key(words[0]) \
|
|---|
| 261 | and type(currentContext[words[0]])==listType:
|
|---|
| 262 | currentContext[words[0]].append(resVal)
|
|---|
| 263 | else:
|
|---|
| 264 | currentContext[words[0]]= resVal
|
|---|
| 265 | else:
|
|---|
| 266 | intVal= (keyword,_smartInt(words[2].strip()))
|
|---|
| 267 | if currentContext.has_key(words[0]) \
|
|---|
| 268 | and type(currentContext[words[0]])==listType:
|
|---|
| 269 | currentContext[words[0]].append(intVal)
|
|---|
| 270 | else:
|
|---|
| 271 | currentContext[words[0]]= intVal
|
|---|
| 272 | return True
|
|---|
| 273 | elif words[0]=="TYPE":
|
|---|
| 274 | typeName= keyword
|
|---|
| 275 | typeFieldRec= words[2].split()
|
|---|
| 276 | currentTypedefs[typeName]= typeFieldRec
|
|---|
| 277 | #print "defining type %s"%typeName
|
|---|
| 278 | return True
|
|---|
| 279 | elif currentTypedefs.has_key(words[1]):
|
|---|
| 280 | vwords= words[2].split()
|
|---|
| 281 | if len(vwords)==1:
|
|---|
| 282 | vwords= words[2].split(",")
|
|---|
| 283 | if len(vwords)==len(currentTypedefs[words[1]]):
|
|---|
| 284 | recName= words[0]
|
|---|
| 285 | recType= words[1]
|
|---|
| 286 | recMetadata= {}
|
|---|
| 287 | offset= 0
|
|---|
| 288 | for field in currentTypedefs[recType]:
|
|---|
| 289 | recMetadata[field]= vwords[offset]
|
|---|
| 290 | offset += 1
|
|---|
| 291 | if currentContext.has_key(recName) \
|
|---|
| 292 | and type(currentContext[recName])==listType:
|
|---|
| 293 | currentContext[recName].append(recMetadata)
|
|---|
| 294 | else:
|
|---|
| 295 | currentContext[recName]= recMetadata
|
|---|
| 296 | #print "loaded %s rec for %s"%(recType,recName)
|
|---|
| 297 | return True
|
|---|
| 298 | else:
|
|---|
| 299 | return False
|
|---|
| 300 |
|
|---|
| 301 | if len(words)>1:
|
|---|
| 302 | if words[1].upper()=="METADATA":
|
|---|
| 303 | newContext= {}
|
|---|
| 304 | newTypedefs= {}
|
|---|
| 305 | currentName= words[0]
|
|---|
| 306 | contextStack.append((currentName,newContext,newTypedefs))
|
|---|
| 307 | currentContext[currentName]= newContext
|
|---|
| 308 | currentContext= newContext
|
|---|
| 309 | currentTypedefs= newTypedefs
|
|---|
| 310 | #print "%s%s begins"%(len(contextStack)*" ",words[0])
|
|---|
| 311 | return True
|
|---|
| 312 | elif words[1].upper()=="MULTI":
|
|---|
| 313 | currentContext[words[0]]= []
|
|---|
| 314 | return True
|
|---|
| 315 |
|
|---|
| 316 | if words[0].upper()=="END":
|
|---|
| 317 | exitingName,exitingContext,exitingTypedefs= contextStack[-1]
|
|---|
| 318 | typeCounts= {}
|
|---|
| 319 | for k in exitingContext.keys():
|
|---|
| 320 | if type(exitingContext[k])==dictType:
|
|---|
| 321 | pass
|
|---|
| 322 | elif type(exitingContext[k])==listType:
|
|---|
| 323 | for maybeTuple in exitingContext[k]:
|
|---|
| 324 | if type(maybeTuple)==dictType:
|
|---|
| 325 | pass
|
|---|
| 326 | else:
|
|---|
| 327 | tp,val= maybeTuple
|
|---|
| 328 | if not typeCounts.has_key(tp):
|
|---|
| 329 | typeCounts[tp]= 0
|
|---|
| 330 | typeCounts[tp] += 1
|
|---|
| 331 | else:
|
|---|
| 332 | tp,val= exitingContext[k]
|
|---|
| 333 | if not typeCounts.has_key(tp):
|
|---|
| 334 | typeCounts[tp]= 0
|
|---|
| 335 | typeCounts[tp] += 1
|
|---|
| 336 | #print "%s%s: %d elements (%s) via %d typedefs"%\
|
|---|
| 337 | (len(contextStack)*" ",
|
|---|
| 338 | exitingName,
|
|---|
| 339 | len(exitingContext.keys()),
|
|---|
| 340 | typeCounts,
|
|---|
| 341 | len(exitingTypedefs.keys()))
|
|---|
| 342 | contextStack.pop()
|
|---|
| 343 | currentName,currentContext,currentTypedefs= contextStack[-1]
|
|---|
| 344 | #print currentContext
|
|---|
| 345 | return True
|
|---|
| 346 |
|
|---|
| 347 | #print "words: %s"%words
|
|---|
| 348 | #print "currentTypedefs: %s"%currentTypedefs
|
|---|
| 349 | return False
|
|---|
| 350 |
|
|---|
| 351 | def parse(f, inContext={}):
|
|---|
| 352 | "Parse the file object f as a config; return (topname,configdict)"
|
|---|
| 353 | currentName= ""
|
|---|
| 354 | currentTypedefs= {}
|
|---|
| 355 | currentContext= inContext.copy()
|
|---|
| 356 | contextStack= [(currentName,currentContext,currentTypedefs)]
|
|---|
| 357 | whichLine= 0
|
|---|
| 358 | for l in f: # for each line
|
|---|
| 359 | whichLine += 1
|
|---|
| 360 | newEnd= l.find("#")
|
|---|
| 361 | if newEnd>=0: l= l[:newEnd]
|
|---|
| 362 | try:
|
|---|
| 363 | if not _parseLine(l,contextStack):
|
|---|
| 364 | raise RuntimeError("Line %d unparsable: <%s>"%(whichLine,l))
|
|---|
| 365 | except ValueError:
|
|---|
| 366 | raise RuntimeError("Line %d unparsable (bad value field): <%s>"%\
|
|---|
| 367 | (whichLine,l))
|
|---|
| 368 | currentName,currentContext,currentTypedefs= contextStack[0]
|
|---|
| 369 | return currentContext
|
|---|
| 370 |
|
|---|
| 371 | def main():
|
|---|
| 372 | pp= pprint.PrettyPrinter(indent=4)
|
|---|
| 373 | for fname in sys.argv[1:]:
|
|---|
| 374 | print "########### %s"%fname
|
|---|
| 375 | f= open(fname,"r")
|
|---|
| 376 | config= parse(f)
|
|---|
| 377 | f.close()
|
|---|
| 378 | pp.pprint(config)
|
|---|
| 379 |
|
|---|
| 380 | ############
|
|---|
| 381 | # Main hook
|
|---|
| 382 | ############
|
|---|
| 383 |
|
|---|
| 384 | if __name__=="__main__":
|
|---|
| 385 | main()
|
|---|