Exemple #1
0
def equal(element1, element2, ignoreattrs=False):
    """Return True if two elements are equal, false if they are not. 
    
    The ignoreattrs option, when True, causes this function to ignore
    element attributes when checking equality. Added as a requirement for 
    another project (Similar Content), should find a cleaner way of 
    achieving the same result. """
    
    log = logging.getLogger()
    
    #tags
    tag1 = Tag._cleantag(element1.tag)
    tag2 = Tag._cleantag(element2.tag)
    if tag1 != tag2:
        return False
    
    #attrs
    attrib1 = element1.attrib
    Attrib.cleanKeys(attrib1)
    attrib2 = element2.attrib
    Attrib.cleanKeys(attrib2) 
    if (attrib1 != attrib2) and (not ignoreattrs):
        return False
    
    #text
    try:
        text1 = element1.text.rstrip()
    except AttributeError: 
        text1 = ''
    try:
        text2 = element2.text.rstrip()
    except AttributeError:
        text2 = ''
    
    if text1 != text2:
        return False
    
    #tail
    try:
        tail1 = element1.tail.rstrip()
    except AttributeError:
        tail1 = ''
    try:
        tail2 = element2.tail.rstrip()
    except:
        tail2 = ''
    if tail1 != tail2:
        return False
    
    return True
Exemple #2
0
def isUnitEvent(event):
    if event.getEventName() == "StartElementEvent":
        if Tag.isUnitTag(event.tag) and Attrib.isUnitAttrib(event.attr) and Text.isUnitText(event.text):
            return True
        else:
            return False
    
    elif event.getEventName() == "EndElementEvent":
        if Tag.isUnitTag(event.tag) and Text.isUnitText(event.text):
            return True
        else:
            return False
    
    else:
        raise TypeError("Invalid event type: %s" % str(event1))
Exemple #3
0
def add(element1, element2):
    """Add element2 to element1, modify element1 in place and return it. 
    
    This function modifies the node in place to make ensure there are not 
    too many copies created in complicated programs. 
    
    The reason this function also returns the element is to allow this function
    to be used in compound statements, for example: 
    
        add(node1, invert(add(node2, node3)))"""
    
    newtag = Tag.addTags(element1.tag, element2.tag)
    
    #have to wrap the attributes in dict() to avoid a bus error
    newattribs = Attrib.addAttribs(dict(element1.attrib), dict(element2.attrib))
    
    element1.tag = newtag
    element1.text = Text.addText(element1.text, element2.text)
    element1.tail = Text.addText(element1.tail, element2.tail)
    
    for i in element1.attrib:
        del element1.attrib[i]
    for key in newattribs.keys():
        try:
            element1.set(key, newattribs[key])
        except TypeError:
            log = logging.getLogger()
            log.error('TypeError: %s' % str(sys.exc_info()[1]))
            log.error('key = %s\tnewattribs[key] = %s' % (str(key), str(newattribs[key])))
            raise
        
    return element1
Exemple #4
0
def addEvents(event1, event2):
    """Add the two events and return a new event. Raise TypeError if the event
    types do not match."""    
    import TreeGroup.Common.Attrib as Attrib
    import TreeGroup.Common.Tag as Tag
    import TreeGroup.Common.Text as Text
    
    if not type(event1) == type(event2):
        print("%s != %s" % (type(event1), type(event2)))
        raise TypeError
    
    if event1.getEventName() == "StartElementEvent":
        newevent = StartElementEvent(Tag.addTags(event1.tag, event2.tag), Attrib.addAttribs(event1.attr, event2.attr), Text.addText(event1.text, event2.text))
        return newevent
    
    elif event1.getEventName() == "EndElementEvent":
        newevent = EndElementEvent(Tag.addTags(event1.tag, event2.tag), Text.addText(event1.text, event2.text))
        return newevent
    
    else:
        raise TypeError("Invalid event type: %s" % str(event1))
Exemple #5
0
def invert(element1):
    """Invert the element, modify in place and return it.
    
    This function modifies the node in place to make ensure there are not 
    too many copies created in complicated programs. 
    
    The reason this function also returns the element is to allow this function
    to be used in compound statements, for example: 
    
        add(node1, invert(add(node2, node3)))
        
    """
    
#    inverted = lxml.etree.Element(Tag.tagInverse(element1.tag), )
    element1.tag = Tag.tagInverse(element1.tag) 
    element1.text = Text.textInverse(element1.text)
    element1.tail = Text.textInverse(element1.tail)
    newattribs = Attrib.attribInverse(dict(element1.attrib))
    for i in element1.attrib:
        del element1.attrib[i]
    for j in newattribs:
        element1.set(j, newattribs[j])
        
    return element1
Exemple #6
0
 def __eq__(self, o):
     if type(self) == type(o) and Tag.equal(self.tag, o.tag) and Text.equal(self.text, o.text):
         return True
     else:
         return False