comparison emeraldtree/ElementTree.py @ 15:60f59340b897

Remove argument ordering weirdness of QName. * emeraldtree/ElementTree.py: Redefine interface of QName. * emeraldtree/tests/test_ElementTree.py: Add testcases.
author Bastian Blank <bblank@thinkmo.de>
date Sat, 31 May 2008 17:46:46 +0200
parents 3d7555e0be1b
children 533331338691
comparison
equal deleted inserted replaced
14:3d7555e0be1b 15:60f59340b897
513 513
514 ## 514 ##
515 # QName wrapper. This can be used to wrap a QName attribute value, in 515 # QName wrapper. This can be used to wrap a QName attribute value, in
516 # order to get proper namespace handling on output. 516 # order to get proper namespace handling on output.
517 # 517 #
518 # @param text A string containing the QName value, in the form {uri}local,
519 # or, if the tag argument is given, the URI part of a QName.
520 # @param tag Optional tag. If given, the first argument is interpreted as
521 # an URI, and this argument is interpreted as a local name.
522 # @return An opaque object, representing the QName. 518 # @return An opaque object, representing the QName.
523 519
524 class QName(object): 520 class QName(object):
525 def __init__(self, text_or_uri, tag=None): 521 def __init__(self, text, uri = None):
526 if tag: 522 if text[0] == '{':
527 text_or_uri = "{%s}%s" % (text_or_uri, tag) 523 if uri is not None:
528 self.text = text_or_uri 524 raise ValueError
525 i = text.find('}')
526 if i == -1:
527 raise ValueError
528 uri = text[1:i]
529 text = text[i + 1:]
530 self.name, self.uri = text, uri
531
529 def __str__(self): 532 def __str__(self):
530 return self.text 533 return self.text
534
531 def __hash__(self): 535 def __hash__(self):
532 return hash(self.text) 536 return hash(self.text)
537
533 def __cmp__(self, other): 538 def __cmp__(self, other):
534 if isinstance(other, QName): 539 if isinstance(other, QName):
535 return cmp(self.text, other.text) 540 c = cmp(self.uri, other.uri)
541 if c:
542 return c
543 return cmp(self.name, other.name)
536 return cmp(self.text, other) 544 return cmp(self.text, other)
545
546 @property
547 def text(self):
548 if self.uri is not None:
549 return '{' + self.uri + '}' + self.name
550 return self.name
537 551
538 # -------------------------------------------------------------------- 552 # --------------------------------------------------------------------
539 553
540 ## 554 ##
541 # ElementTree wrapper class. This class represents an entire element 555 # ElementTree wrapper class. This class represents an entire element