<s> from __future__ import absolute_import , division , unicode_literals <EOL> try : <EOL> from collections import OrderedDict <EOL> except ImportError : <EOL> try : <EOL> from ordereddict import OrderedDict <EOL> except ImportError : <EOL> OrderedDict = dict <EOL> import gettext <EOL> _ = gettext . gettext <EOL> import re <EOL> from six import text_type <EOL> from . import _base <EOL> from . . utils import moduleFactoryFactory <EOL> tag_regexp = re . compile ( "<STR_LIT>" ) <EOL> def getETreeBuilder ( ElementTreeImplementation ) : <EOL> ElementTree = ElementTreeImplementation <EOL> ElementTreeCommentType = ElementTree . Comment ( "<STR_LIT>" ) . tag <EOL> class TreeWalker ( _base . NonRecursiveTreeWalker ) : <EOL> """<STR_LIT>""" <EOL> def getNodeDetails ( self , node ) : <EOL> if isinstance ( node , tuple ) : <EOL> elt , key , parents , flag = node <EOL> if flag in ( "<STR_LIT:text>" , "<STR_LIT>" ) : <EOL> return _base . TEXT , getattr ( elt , flag ) <EOL> else : <EOL> node = elt <EOL> if not ( hasattr ( node , "<STR_LIT>" ) ) : <EOL> node = node . getroot ( ) <EOL> if node . tag in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> return ( _base . DOCUMENT , ) <EOL> elif node . tag == "<STR_LIT>" : <EOL> return ( _base . DOCTYPE , node . text , <EOL> node . get ( "<STR_LIT>" ) , node . get ( "<STR_LIT>" ) ) <EOL> elif node . tag == ElementTreeCommentType : <EOL> return _base . COMMENT , node . text <EOL> else : <EOL> assert type ( node . tag ) == text_type , type ( node . tag ) <EOL> match = tag_regexp . match ( node . tag ) <EOL> if match : <EOL> namespace , tag = match . groups ( ) <EOL> else : <EOL> namespace = None <EOL> tag = node . tag <EOL> attrs = OrderedDict ( ) <EOL> for name , value in list ( node . attrib . items ( ) ) : <EOL> match = tag_regexp . match ( name ) <EOL> if match : <EOL> attrs [ ( match . group ( <NUM_LIT:1> ) , match . group ( <NUM_LIT:2> ) ) ] = value <EOL> else : <EOL> attrs [ ( None , name ) ] = value <EOL> return ( _base . ELEMENT , namespace , tag , <EOL> attrs , len ( node ) or node . text ) <EOL> def getFirstChild ( self , node ) : <EOL> if isinstance ( node , tuple ) : <EOL> element , key , parents , flag = node <EOL> else : <EOL> element , key , parents , flag = node , None , [ ] , None <EOL> if flag in ( "<STR_LIT:text>" , "<STR_LIT>" ) : <EOL> return None <EOL> else : <EOL> if element . text : <EOL> return element , key , parents , "<STR_LIT:text>" <EOL> elif len ( element ) : <EOL> parents . append ( element ) <EOL> return element [ <NUM_LIT:0> ] , <NUM_LIT:0> , parents , None <EOL> else : <EOL> return None <EOL> def getNextSibling ( self , node ) : <EOL> if isinstance ( node , tuple ) : <EOL> element , key , parents , flag = node <EOL> else : <EOL> return None <EOL> if flag == "<STR_LIT:text>" : <EOL> if len ( element ) : <EOL> parents . append ( element ) <EOL> return element [ <NUM_LIT:0> ] , <NUM_LIT:0> , parents , None <EOL> else : <EOL> return None <EOL> else : <EOL> if element . tail and flag != "<STR_LIT>" : <EOL> return element , key , parents , "<STR_LIT>" <EOL> elif key < len ( parents [ - <NUM_LIT:1> ] ) - <NUM_LIT:1> : <EOL> return parents [ - <NUM_LIT:1> ] [ key + <NUM_LIT:1> ] , key + <NUM_LIT:1> , parents , None <EOL> else : <EOL> return None <EOL> def getParentNode ( self , node ) : <EOL> if isinstance ( node , tuple ) : <EOL> element , key , parents , flag = node <EOL> else : <EOL> return None <EOL> if flag == "<STR_LIT:text>" : <EOL> if not parents : <EOL> return element <EOL> else : <EOL> return element , key , parents , None <EOL> else : <EOL> parent = parents . pop ( ) <EOL> if not parents : <EOL> return parent <EOL> else : <EOL> return parent , list ( parents [ - <NUM_LIT:1> ] ) . index ( parent ) , parents , None <EOL> return locals ( ) <EOL> getETreeModule = moduleFactoryFactory ( getETreeBuilder ) </s>
