<s> from __future__ import absolute_import , division , unicode_literals <EOL> from pip . _vendor . six import text_type , string_types <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ] <EOL> from xml . dom import Node <EOL> DOCUMENT = Node . DOCUMENT_NODE <EOL> DOCTYPE = Node . DOCUMENT_TYPE_NODE <EOL> TEXT = Node . TEXT_NODE <EOL> ELEMENT = Node . ELEMENT_NODE <EOL> COMMENT = Node . COMMENT_NODE <EOL> ENTITY = Node . ENTITY_NODE <EOL> UNKNOWN = "<STR_LIT>" <EOL> from . . constants import voidElements , spaceCharacters <EOL> spaceCharacters = "<STR_LIT>" . join ( spaceCharacters ) <EOL> def to_text ( s , blank_if_none = True ) : <EOL> """<STR_LIT>""" <EOL> if s is None : <EOL> if blank_if_none : <EOL> return "<STR_LIT>" <EOL> else : <EOL> return None <EOL> elif isinstance ( s , text_type ) : <EOL> return s <EOL> else : <EOL> return text_type ( s ) <EOL> def is_text_or_none ( string ) : <EOL> """<STR_LIT>""" <EOL> return string is None or isinstance ( string , string_types ) <EOL> class TreeWalker ( object ) : <EOL> def __init__ ( self , tree ) : <EOL> self . tree = tree <EOL> def __iter__ ( self ) : <EOL> raise NotImplementedError <EOL> def error ( self , msg ) : <EOL> return { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT:data>" : msg } <EOL> def emptyTag ( self , namespace , name , attrs , hasChildren = False ) : <EOL> assert namespace is None or isinstance ( namespace , string_types ) , type ( namespace ) <EOL> assert isinstance ( name , string_types ) , type ( name ) <EOL> assert all ( ( namespace is None or isinstance ( namespace , string_types ) ) and <EOL> isinstance ( name , string_types ) and <EOL> isinstance ( value , string_types ) <EOL> for ( namespace , name ) , value in attrs . items ( ) ) <EOL> yield { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT:name>" : to_text ( name , False ) , <EOL> "<STR_LIT>" : to_text ( namespace ) , <EOL> "<STR_LIT:data>" : attrs } <EOL> if hasChildren : <EOL> yield self . error ( "<STR_LIT>" ) <EOL> def startTag ( self , namespace , name , attrs ) : <EOL> assert namespace is None or isinstance ( namespace , string_types ) , type ( namespace ) <EOL> assert isinstance ( name , string_types ) , type ( name ) <EOL> assert all ( ( namespace is None or isinstance ( namespace , string_types ) ) and <EOL> isinstance ( name , string_types ) and <EOL> isinstance ( value , string_types ) <EOL> for ( namespace , name ) , value in attrs . items ( ) ) <EOL> return { "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT:name>" : text_type ( name ) , <EOL> "<STR_LIT>" : to_text ( namespace ) , <EOL> "<STR_LIT:data>" : dict ( ( ( to_text ( namespace , False ) , to_text ( name ) ) , <EOL> to_text ( value , False ) ) <EOL> for ( namespace , name ) , value in attrs . items ( ) ) } <EOL> def endTag ( self , namespace , name ) : <EOL> assert namespace is None or isinstance ( namespace , string_types ) , type ( namespace ) <EOL> assert isinstance ( name , string_types ) , type ( namespace ) <EOL> return { "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT:name>" : to_text ( name , False ) , <EOL> "<STR_LIT>" : to_text ( namespace ) , <EOL> "<STR_LIT:data>" : { } } <EOL> def text ( self , data ) : <EOL> assert isinstance ( data , string_types ) , type ( data ) <EOL> data = to_text ( data ) <EOL> middle = data . lstrip ( spaceCharacters ) <EOL> left = data [ : len ( data ) - len ( middle ) ] <EOL> if left : <EOL> yield { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT:data>" : left } <EOL> data = middle <EOL> middle = data . rstrip ( spaceCharacters ) <EOL> right = data [ len ( middle ) : ] <EOL> if middle : <EOL> yield { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT:data>" : middle } <EOL> if right : <EOL> yield { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT:data>" : right } <EOL> def comment ( self , data ) : <EOL> assert isinstance ( data , string_types ) , type ( data ) <EOL> return { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT:data>" : text_type ( data ) } <EOL> def doctype ( self , name , publicId = None , systemId = None , correct = True ) : <EOL> assert is_text_or_none ( name ) , type ( name ) <EOL> assert is_text_or_none ( publicId ) , type ( publicId ) <EOL> assert is_text_or_none ( systemId ) , type ( systemId ) <EOL> return { "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT:name>" : to_text ( name ) , <EOL> "<STR_LIT>" : to_text ( publicId ) , <EOL> "<STR_LIT>" : to_text ( systemId ) , <EOL> "<STR_LIT>" : to_text ( correct ) } <EOL> def entity ( self , name ) : <EOL> assert isinstance ( name , string_types ) , type ( name ) <EOL> return { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT:name>" : text_type ( name ) } <EOL> def unknown ( self , nodeType ) : <EOL> return self . error ( "<STR_LIT>" + nodeType ) <EOL> class NonRecursiveTreeWalker ( TreeWalker ) : <EOL> def getNodeDetails ( self , node ) : <EOL> raise NotImplementedError <EOL> def getFirstChild ( self , node ) : <EOL> raise NotImplementedError <EOL> def getNextSibling ( self , node ) : <EOL> raise NotImplementedError <EOL> def getParentNode ( self , node ) : <EOL> raise NotImplementedError <EOL> def __iter__ ( self ) : <EOL> currentNode = self . tree <EOL> while currentNode is not None : <EOL> details = self . getNodeDetails ( currentNode ) <EOL> type , details = details [ <NUM_LIT:0> ] , details [ <NUM_LIT:1> : ] <EOL> hasChildren = False <EOL> if type == DOCTYPE : <EOL> yield self . doctype ( * details ) <EOL> elif type == TEXT : <EOL> for token in self . text ( * details ) : <EOL> yield token <EOL> elif type == ELEMENT : <EOL> namespace , name , attributes , hasChildren = details <EOL> if name in voidElements : <EOL> for token in self . emptyTag ( namespace , name , attributes , <EOL> hasChildren ) : <EOL> yield token <EOL> hasChildren = False <EOL> else : <EOL> yield self . startTag ( namespace , name , attributes ) <EOL> elif type == COMMENT : <EOL> yield self . comment ( details [ <NUM_LIT:0> ] ) <EOL> elif type == ENTITY : <EOL> yield self . entity ( details [ <NUM_LIT:0> ] ) <EOL> elif type == DOCUMENT : <EOL> hasChildren = True <EOL> else : <EOL> yield self . unknown ( details [ <NUM_LIT:0> ] ) <EOL> if hasChildren : <EOL> firstChild = self . getFirstChild ( currentNode ) <EOL> else : <EOL> firstChild = None <EOL> if firstChild is not None : <EOL> currentNode = firstChild <EOL> else : <EOL> while currentNode is not None : <EOL> details = self . getNodeDetails ( currentNode ) <EOL> type , details = details [ <NUM_LIT:0> ] , details [ <NUM_LIT:1> : ] <EOL> if type == ELEMENT : <EOL> namespace , name , attributes , hasChildren = details <EOL> if name not in voidElements : <EOL> yield self . endTag ( namespace , name ) <EOL> if self . tree is currentNode : <EOL> currentNode = None <EOL> break <EOL> nextSibling = self . getNextSibling ( currentNode ) <EOL> if nextSibling is not None : <EOL> currentNode = nextSibling <EOL> break <EOL> else : <EOL> currentNode = self . getParentNode ( currentNode ) </s>
