comparison emeraldtree/ElementTree.py @ 25:47ae505e63ea

Parse tags and attribute names always in QName. * emeraldtree/ElementTree.py - Add QName.copy. - Force usage of QName in XMLParser. - Don't convert all text to ascii. * emeraldtree/tests/test_ElementTree.py: Check for QName.
author Bastian Blank <bblank@thinkmo.de>
date Sun, 08 Jun 2008 12:09:33 +0200
parents 4c1912ff58a3
children eeb0eb69c1da
comparison
equal deleted inserted replaced
24:2015d11467bc 25:47ae505e63ea
497 c = cmp(self.uri, other.uri) 497 c = cmp(self.uri, other.uri)
498 if c: 498 if c:
499 return c 499 return c
500 return cmp(self.name, other.name) 500 return cmp(self.name, other.name)
501 return cmp(self.text, other) 501 return cmp(self.text, other)
502
503 def copy(self):
504 return self.__class__(self.name, self.uri)
502 505
503 @property 506 @property
504 def text(self): 507 def text(self):
505 if self.uri is not None: 508 if self.uri is not None:
506 return '{' + self.uri + '}' + self.name 509 return '{' + self.uri + '}' + self.name
1319 err = ParseError(value) 1322 err = ParseError(value)
1320 err.code = value.code 1323 err.code = value.code
1321 err.position = value.lineno, value.offset 1324 err.position = value.lineno, value.offset
1322 raise err 1325 raise err
1323 1326
1324 def _fixtext(self, text):
1325 # convert text string to ascii, if possible
1326 try:
1327 return text.encode("ascii")
1328 except UnicodeError:
1329 return text
1330
1331 def _fixname(self, key): 1327 def _fixname(self, key):
1332 # expand qname, and convert name string to ascii, if possible 1328 # expand qname, and convert name string to ascii, if possible
1333 try: 1329 if key in self._names:
1334 name = self._names[key] 1330 return self._names[key].copy()
1335 except KeyError: 1331 if '}' in key:
1336 name = key 1332 name = QName(*key.split('}', 1))
1337 if "}" in name: 1333 else:
1338 name = "{" + name 1334 name = QName(key)
1339 self._names[key] = name = self._fixtext(name) 1335 self._names[key] = name
1340 return name 1336 return name
1341 1337
1342 def _start(self, tag, attrib_in): 1338 def _start(self, tag, attrib_in):
1343 fixname = self._fixname 1339 fixname = self._fixname
1344 fixtext = self._fixtext
1345 tag = fixname(tag) 1340 tag = fixname(tag)
1346 attrib = {} 1341 attrib = {}
1347 for key, value in attrib_in.items(): 1342 for key, value in attrib_in.items():
1348 attrib[fixname(key)] = fixtext(value) 1343 attrib[fixname(key)] = value
1349 return self.target.start(tag, attrib) 1344 return self.target.start(tag, attrib)
1350 1345
1351 def _start_list(self, tag, attrib_in): 1346 def _start_list(self, tag, attrib_in):
1352 fixname = self._fixname 1347 fixname = self._fixname
1353 fixtext = self._fixtext
1354 tag = fixname(tag) 1348 tag = fixname(tag)
1355 attrib = {} 1349 attrib = {}
1356 if attrib_in: 1350 if attrib_in:
1357 for i in range(0, len(attrib_in), 2): 1351 for i in range(0, len(attrib_in), 2):
1358 attrib[fixname(attrib_in[i])] = fixtext(attrib_in[i+1]) 1352 attrib[fixname(attrib_in[i])] = attrib_in[i+1]
1359 return self.target.start(tag, attrib) 1353 return self.target.start(tag, attrib)
1360 1354
1361 def _data(self, text): 1355 def _data(self, text):
1362 return self.target.data(self._fixtext(text)) 1356 return self.target.data(text)
1363 1357
1364 def _end(self, tag): 1358 def _end(self, tag):
1365 return self.target.end(self._fixname(tag)) 1359 return self.target.end(self._fixname(tag))
1366 1360
1367 def _default(self, text): 1361 def _default(self, text):