3 class ParseError (Exception):
6 def error (msg
, exit
=0):
15 def removeQuote (text
):
16 """Remove quotes from a literal.
18 if len(text
) >= 2 and text
[0] == text
[len(text
)-1] == '"':
24 def __init__ (self
, name
):
34 def appendChild (self
, childnode
):
35 self
.children
.append(childnode
)
36 childnode
.parent
= self
38 def getChildren (self
):
48 for child
in self
.getChildren():
57 def __init__ (self
, name
, rid
= None):
61 # print "name: " + self.name - stats ...
63 # The following attributes are copied when 'clone'ed.
67 def dump (self
, level
= 0):
69 chars
+= "\n" + Element
.INDENT
*level
70 chars
+= '<%s'%self
.name
73 self
.setAttr("rid", self
.rid
)
75 chars
+= self
.__dumpAttrs
()
77 if len(self
.children
) == 0:
81 for child
in self
.getChildren():
82 chars
+= child
.dump(level
+1)
84 chars
+= "\n"+Element
.INDENT
*level
85 chars
+= "</%s>"%self
.name
89 def hasAttr (self
, name
):
90 return self
.attrs
.has_key(name
)
92 def getAttr (self
, name
):
93 return self
.attrs
[name
]
95 def setAttr (self
, name
, value
):
96 if type(value
) == type(0):
98 self
.attrs
[name
] = removeQuote(value
)
99 # print "attr: " + self.name + "." + name - stats ...
102 def clone (self
, elem
):
103 keys
= elem
.attrs
.keys()
105 self
.attrs
[key
] = elem
.attrs
[key
]
108 def __dumpAttrs (self
):
110 keys
= self
.attrs
.keys()
113 value
= self
.attrs
[key
]
114 text
+= ' %s="%s"'%(key
, value
)