1*cdf0e10cSrcweirimport sys 2*cdf0e10cSrcweir 3*cdf0e10cSrcweirclass ParseError (Exception): 4*cdf0e10cSrcweir pass 5*cdf0e10cSrcweir 6*cdf0e10cSrcweirdef error (msg, exit=0): 7*cdf0e10cSrcweir sys.stderr.write (msg) 8*cdf0e10cSrcweir if exit: 9*cdf0e10cSrcweir sys.exit (exit) 10*cdf0e10cSrcweir 11*cdf0e10cSrcweirdef progress (msg): 12*cdf0e10cSrcweir sys.stderr.write(msg) 13*cdf0e10cSrcweir 14*cdf0e10cSrcweir 15*cdf0e10cSrcweirdef removeQuote (text): 16*cdf0e10cSrcweir """Remove quotes from a literal. 17*cdf0e10cSrcweir""" 18*cdf0e10cSrcweir if len(text) >= 2 and text[0] == text[len(text)-1] == '"': 19*cdf0e10cSrcweir text = text[1:-1] 20*cdf0e10cSrcweir return text 21*cdf0e10cSrcweir 22*cdf0e10cSrcweir 23*cdf0e10cSrcweirclass Macro(object): 24*cdf0e10cSrcweir def __init__ (self, name): 25*cdf0e10cSrcweir self.name = name 26*cdf0e10cSrcweir self.tokens = [] 27*cdf0e10cSrcweir self.vars = {} 28*cdf0e10cSrcweir 29*cdf0e10cSrcweir 30*cdf0e10cSrcweirclass Node(object): 31*cdf0e10cSrcweir def __init__ (self): 32*cdf0e10cSrcweir self.children = [] 33*cdf0e10cSrcweir 34*cdf0e10cSrcweir def appendChild (self, childnode): 35*cdf0e10cSrcweir self.children.append(childnode) 36*cdf0e10cSrcweir childnode.parent = self 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir def getChildren (self): 39*cdf0e10cSrcweir return self.children 40*cdf0e10cSrcweir 41*cdf0e10cSrcweir 42*cdf0e10cSrcweirclass RootNode(Node): 43*cdf0e10cSrcweir def __init__ (self): 44*cdf0e10cSrcweir Node.__init__(self) 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir def dump (self): 47*cdf0e10cSrcweir chars = '' 48*cdf0e10cSrcweir for child in self.getChildren(): 49*cdf0e10cSrcweir chars += child.dump() 50*cdf0e10cSrcweir return chars 51*cdf0e10cSrcweir 52*cdf0e10cSrcweir 53*cdf0e10cSrcweirclass Element(Node): 54*cdf0e10cSrcweir 55*cdf0e10cSrcweir INDENT = " " 56*cdf0e10cSrcweir 57*cdf0e10cSrcweir def __init__ (self, name, rid = None): 58*cdf0e10cSrcweir Node.__init__(self) 59*cdf0e10cSrcweir self.name = name 60*cdf0e10cSrcweir self.parent = None 61*cdf0e10cSrcweir# print "name: " + self.name - stats ... 62*cdf0e10cSrcweir 63*cdf0e10cSrcweir # The following attributes are copied when 'clone'ed. 64*cdf0e10cSrcweir self.rid = rid 65*cdf0e10cSrcweir self.attrs = {} 66*cdf0e10cSrcweir 67*cdf0e10cSrcweir def dump (self, level = 0): 68*cdf0e10cSrcweir chars = '' 69*cdf0e10cSrcweir chars += "\n" + Element.INDENT*level 70*cdf0e10cSrcweir chars += '<%s'%self.name 71*cdf0e10cSrcweir 72*cdf0e10cSrcweir if self.rid != None: 73*cdf0e10cSrcweir self.setAttr("rid", self.rid) 74*cdf0e10cSrcweir 75*cdf0e10cSrcweir chars += self.__dumpAttrs() 76*cdf0e10cSrcweir 77*cdf0e10cSrcweir if len(self.children) == 0: 78*cdf0e10cSrcweir chars += '/>' 79*cdf0e10cSrcweir else: 80*cdf0e10cSrcweir chars += '>' 81*cdf0e10cSrcweir for child in self.getChildren(): 82*cdf0e10cSrcweir chars += child.dump(level+1) 83*cdf0e10cSrcweir 84*cdf0e10cSrcweir chars += "\n"+Element.INDENT*level 85*cdf0e10cSrcweir chars += "</%s>"%self.name 86*cdf0e10cSrcweir 87*cdf0e10cSrcweir return chars 88*cdf0e10cSrcweir 89*cdf0e10cSrcweir def hasAttr (self, name): 90*cdf0e10cSrcweir return self.attrs.has_key(name) 91*cdf0e10cSrcweir 92*cdf0e10cSrcweir def getAttr (self, name): 93*cdf0e10cSrcweir return self.attrs[name] 94*cdf0e10cSrcweir 95*cdf0e10cSrcweir def setAttr (self, name, value): 96*cdf0e10cSrcweir if type(value) == type(0): 97*cdf0e10cSrcweir value = "%d"%value 98*cdf0e10cSrcweir self.attrs[name] = removeQuote(value) 99*cdf0e10cSrcweir# print "attr: " + self.name + "." + name - stats ... 100*cdf0e10cSrcweir return 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir def clone (self, elem): 103*cdf0e10cSrcweir keys = elem.attrs.keys() 104*cdf0e10cSrcweir for key in keys: 105*cdf0e10cSrcweir self.attrs[key] = elem.attrs[key] 106*cdf0e10cSrcweir self.rid = elem.rid 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir def __dumpAttrs (self): 109*cdf0e10cSrcweir text = '' 110*cdf0e10cSrcweir keys = self.attrs.keys() 111*cdf0e10cSrcweir keys.sort() 112*cdf0e10cSrcweir for key in keys: 113*cdf0e10cSrcweir value = self.attrs[key] 114*cdf0e10cSrcweir text += ' %s="%s"'%(key, value) 115*cdf0e10cSrcweir return text 116