19f22d7c2SAndrew Rist# ************************************************************* 29f22d7c2SAndrew Rist# 39f22d7c2SAndrew Rist# Licensed to the Apache Software Foundation (ASF) under one 49f22d7c2SAndrew Rist# or more contributor license agreements. See the NOTICE file 59f22d7c2SAndrew Rist# distributed with this work for additional information 69f22d7c2SAndrew Rist# regarding copyright ownership. The ASF licenses this file 79f22d7c2SAndrew Rist# to you under the Apache License, Version 2.0 (the 89f22d7c2SAndrew Rist# "License"); you may not use this file except in compliance 99f22d7c2SAndrew Rist# with the License. You may obtain a copy of the License at 109f22d7c2SAndrew Rist# 119f22d7c2SAndrew Rist# http://www.apache.org/licenses/LICENSE-2.0 129f22d7c2SAndrew Rist# 139f22d7c2SAndrew Rist# Unless required by applicable law or agreed to in writing, 149f22d7c2SAndrew Rist# software distributed under the License is distributed on an 159f22d7c2SAndrew Rist# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 169f22d7c2SAndrew Rist# KIND, either express or implied. See the License for the 179f22d7c2SAndrew Rist# specific language governing permissions and limitations 189f22d7c2SAndrew Rist# under the License. 199f22d7c2SAndrew Rist# 209f22d7c2SAndrew Rist# ************************************************************* 219f22d7c2SAndrew Rist 22cdf0e10cSrcweirimport sys 23cdf0e10cSrcweir 24ae54856bSPedro Giffuniclass ParseError (Exception): 25cdf0e10cSrcweir pass 26cdf0e10cSrcweir 27cdf0e10cSrcweirdef error (msg, exit=0): 28cdf0e10cSrcweir sys.stderr.write (msg) 29cdf0e10cSrcweir if exit: 30cdf0e10cSrcweir sys.exit (exit) 31cdf0e10cSrcweir 32cdf0e10cSrcweirdef progress (msg): 33cdf0e10cSrcweir sys.stderr.write(msg) 34cdf0e10cSrcweir 35cdf0e10cSrcweir 36cdf0e10cSrcweirdef removeQuote (text): 37cdf0e10cSrcweir """Remove quotes from a literal. 38cdf0e10cSrcweir""" 39cdf0e10cSrcweir if len(text) >= 2 and text[0] == text[len(text)-1] == '"': 40cdf0e10cSrcweir text = text[1:-1] 41cdf0e10cSrcweir return text 42cdf0e10cSrcweir 43cdf0e10cSrcweir 44cdf0e10cSrcweirclass Macro(object): 45cdf0e10cSrcweir def __init__ (self, name): 46cdf0e10cSrcweir self.name = name 47cdf0e10cSrcweir self.tokens = [] 48cdf0e10cSrcweir self.vars = {} 49cdf0e10cSrcweir 50cdf0e10cSrcweir 51cdf0e10cSrcweirclass Node(object): 52cdf0e10cSrcweir def __init__ (self): 53cdf0e10cSrcweir self.children = [] 54cdf0e10cSrcweir 55cdf0e10cSrcweir def appendChild (self, childnode): 56cdf0e10cSrcweir self.children.append(childnode) 57cdf0e10cSrcweir childnode.parent = self 58cdf0e10cSrcweir 59cdf0e10cSrcweir def getChildren (self): 60cdf0e10cSrcweir return self.children 61cdf0e10cSrcweir 62cdf0e10cSrcweir 63cdf0e10cSrcweirclass RootNode(Node): 64cdf0e10cSrcweir def __init__ (self): 65cdf0e10cSrcweir Node.__init__(self) 66cdf0e10cSrcweir 67cdf0e10cSrcweir def dump (self): 68cdf0e10cSrcweir chars = '' 69cdf0e10cSrcweir for child in self.getChildren(): 70cdf0e10cSrcweir chars += child.dump() 71cdf0e10cSrcweir return chars 72cdf0e10cSrcweir 73cdf0e10cSrcweir 74cdf0e10cSrcweirclass Element(Node): 75cdf0e10cSrcweir 76cdf0e10cSrcweir INDENT = " " 77cdf0e10cSrcweir 78cdf0e10cSrcweir def __init__ (self, name, rid = None): 79cdf0e10cSrcweir Node.__init__(self) 80cdf0e10cSrcweir self.name = name 81cdf0e10cSrcweir self.parent = None 82cdf0e10cSrcweir# print "name: " + self.name - stats ... 83cdf0e10cSrcweir 84cdf0e10cSrcweir # The following attributes are copied when 'clone'ed. 85cdf0e10cSrcweir self.rid = rid 86cdf0e10cSrcweir self.attrs = {} 87cdf0e10cSrcweir 88cdf0e10cSrcweir def dump (self, level = 0): 89cdf0e10cSrcweir chars = '' 90cdf0e10cSrcweir chars += "\n" + Element.INDENT*level 91cdf0e10cSrcweir chars += '<%s'%self.name 92cdf0e10cSrcweir 93cdf0e10cSrcweir if self.rid != None: 94cdf0e10cSrcweir self.setAttr("rid", self.rid) 95cdf0e10cSrcweir 96cdf0e10cSrcweir chars += self.__dumpAttrs() 97cdf0e10cSrcweir 98cdf0e10cSrcweir if len(self.children) == 0: 99cdf0e10cSrcweir chars += '/>' 100cdf0e10cSrcweir else: 101cdf0e10cSrcweir chars += '>' 102cdf0e10cSrcweir for child in self.getChildren(): 103cdf0e10cSrcweir chars += child.dump(level+1) 104ae54856bSPedro Giffuni 105cdf0e10cSrcweir chars += "\n"+Element.INDENT*level 106cdf0e10cSrcweir chars += "</%s>"%self.name 107cdf0e10cSrcweir 108cdf0e10cSrcweir return chars 109cdf0e10cSrcweir 110cdf0e10cSrcweir def hasAttr (self, name): 111*7d9fa7c3SPedro Giffuni return name in self.attrs 112cdf0e10cSrcweir 113cdf0e10cSrcweir def getAttr (self, name): 114cdf0e10cSrcweir return self.attrs[name] 115cdf0e10cSrcweir 116cdf0e10cSrcweir def setAttr (self, name, value): 117cdf0e10cSrcweir if type(value) == type(0): 118cdf0e10cSrcweir value = "%d"%value 119cdf0e10cSrcweir self.attrs[name] = removeQuote(value) 120cdf0e10cSrcweir# print "attr: " + self.name + "." + name - stats ... 121cdf0e10cSrcweir return 122cdf0e10cSrcweir 123cdf0e10cSrcweir def clone (self, elem): 124*7d9fa7c3SPedro Giffuni keys = list(elem.attrs.keys()) 125cdf0e10cSrcweir for key in keys: 126cdf0e10cSrcweir self.attrs[key] = elem.attrs[key] 127cdf0e10cSrcweir self.rid = elem.rid 128cdf0e10cSrcweir 129cdf0e10cSrcweir def __dumpAttrs (self): 130cdf0e10cSrcweir text = '' 13177de67d5SPedro Giffuni keys = sorted(self.attrs.keys()) 132cdf0e10cSrcweir for key in keys: 133cdf0e10cSrcweir value = self.attrs[key] 134cdf0e10cSrcweir text += ' %s="%s"'%(key, value) 135cdf0e10cSrcweir return text 136