<s> from collections import defaultdict <EOL> import itertools <EOL> import sys <EOL> from . . element import ( <EOL> CharsetMetaAttributeValue , <EOL> ContentMetaAttributeValue , <EOL> whitespace_re <EOL> ) <EOL> __all__ = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> FAST = '<STR_LIT>' <EOL> PERMISSIVE = '<STR_LIT>' <EOL> STRICT = '<STR_LIT:strict>' <EOL> XML = '<STR_LIT>' <EOL> HTML = '<STR_LIT:html>' <EOL> HTML_5 = '<STR_LIT>' <EOL> class TreeBuilderRegistry ( object ) : <EOL> def __init__ ( self ) : <EOL> self . builders_for_feature = defaultdict ( list ) <EOL> self . builders = [ ] <EOL> def register ( self , treebuilder_class ) : <EOL> """<STR_LIT>""" <EOL> for feature in treebuilder_class . features : <EOL> self . builders_for_feature [ feature ] . insert ( <NUM_LIT:0> , treebuilder_class ) <EOL> self . builders . insert ( <NUM_LIT:0> , treebuilder_class ) <EOL> def lookup ( self , * features ) : <EOL> if len ( self . builders ) == <NUM_LIT:0> : <EOL> return None <EOL> if len ( features ) == <NUM_LIT:0> : <EOL> return self . builders [ <NUM_LIT:0> ] <EOL> features = list ( features ) <EOL> features . reverse ( ) <EOL> candidates = None <EOL> candidate_set = None <EOL> while len ( features ) > <NUM_LIT:0> : <EOL> feature = features . pop ( ) <EOL> we_have_the_feature = self . builders_for_feature . get ( feature , [ ] ) <EOL> if len ( we_have_the_feature ) > <NUM_LIT:0> : <EOL> if candidates is None : <EOL> candidates = we_have_the_feature <EOL> candidate_set = set ( candidates ) <EOL> else : <EOL> candidate_set = candidate_set . intersection ( <EOL> set ( we_have_the_feature ) ) <EOL> if candidate_set is None : <EOL> return None <EOL> for candidate in candidates : <EOL> if candidate in candidate_set : <EOL> return candidate <EOL> return None <EOL> builder_registry = TreeBuilderRegistry ( ) <EOL> class TreeBuilder ( object ) : <EOL> """<STR_LIT>""" <EOL> features = [ ] <EOL> is_xml = False <EOL> preserve_whitespace_tags = set ( ) <EOL> empty_element_tags = None <EOL> cdata_list_attributes = { } <EOL> def __init__ ( self ) : <EOL> self . soup = None <EOL> def reset ( self ) : <EOL> pass <EOL> def can_be_empty_element ( self , tag_name ) : <EOL> """<STR_LIT>""" <EOL> if self . empty_element_tags is None : <EOL> return True <EOL> return tag_name in self . empty_element_tags <EOL> def feed ( self , markup ) : <EOL> raise NotImplementedError ( ) <EOL> def prepare_markup ( self , markup , user_specified_encoding = None , <EOL> document_declared_encoding = None ) : <EOL> return markup , None , None , False <EOL> def test_fragment_to_document ( self , fragment ) : <EOL> """<STR_LIT>""" <EOL> return fragment <EOL> def set_up_substitutions ( self , tag ) : <EOL> return False <EOL> def _replace_cdata_list_attribute_values ( self , tag_name , attrs ) : <EOL> """<STR_LIT>""" <EOL> if not attrs : <EOL> return attrs <EOL> if self . cdata_list_attributes : <EOL> universal = self . cdata_list_attributes . get ( '<STR_LIT:*>' , [ ] ) <EOL> tag_specific = self . cdata_list_attributes . get ( <EOL> tag_name . lower ( ) , None ) <EOL> for attr in attrs . keys ( ) : <EOL> if attr in universal or ( tag_specific and attr in tag_specific ) : <EOL> value = attrs [ attr ] <EOL> if isinstance ( value , basestring ) : <EOL> values = whitespace_re . split ( value ) <EOL> else : <EOL> values = value <EOL> attrs [ attr ] = values <EOL> return attrs <EOL> class SAXTreeBuilder ( TreeBuilder ) : <EOL> """<STR_LIT>""" <EOL> def feed ( self , markup ) : <EOL> raise NotImplementedError ( ) <EOL> def close ( self ) : <EOL> pass <EOL> def startElement ( self , name , attrs ) : <EOL> attrs = dict ( ( key [ <NUM_LIT:1> ] , value ) for key , value in list ( attrs . items ( ) ) ) <EOL> self . soup . handle_starttag ( name , attrs ) <EOL> def endElement ( self , name ) : <EOL> self . soup . handle_endtag ( name ) <EOL> def startElementNS ( self , nsTuple , nodeName , attrs ) : <EOL> self . startElement ( nodeName , attrs ) <EOL> def endElementNS ( self , nsTuple , nodeName ) : <EOL> self . endElement ( nodeName ) <EOL> def startPrefixMapping ( self , prefix , nodeValue ) : <EOL> pass <EOL> def endPrefixMapping ( self , prefix ) : <EOL> pass <EOL> def characters ( self , content ) : <EOL> self . soup . handle_data ( content ) <EOL> def startDocument ( self ) : <EOL> pass <EOL> def endDocument ( self ) : <EOL> pass <EOL> class HTMLTreeBuilder ( TreeBuilder ) : <EOL> """<STR_LIT>""" <EOL> preserve_whitespace_tags = set ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> empty_element_tags = set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:input>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> cdata_list_attributes = { <EOL> "<STR_LIT:*>" : [ '<STR_LIT:class>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> "<STR_LIT:a>" : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> "<STR_LIT>" : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> "<STR_LIT>" : [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : [ "<STR_LIT>" ] , <EOL> "<STR_LIT:object>" : [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : [ "<STR_LIT>" ] , <EOL> } <EOL> def set_up_substitutions ( self , tag ) : <EOL> if tag . name != '<STR_LIT>' : <EOL> return False <EOL> http_equiv = tag . get ( '<STR_LIT>' ) <EOL> content = tag . get ( '<STR_LIT:content>' ) <EOL> charset = tag . get ( '<STR_LIT>' ) <EOL> meta_encoding = None <EOL> if charset is not None : <EOL> meta_encoding = charset <EOL> tag [ '<STR_LIT>' ] = CharsetMetaAttributeValue ( charset ) <EOL> elif ( content is not None and http_equiv is not None <EOL> and http_equiv . lower ( ) == '<STR_LIT>' ) : <EOL> tag [ '<STR_LIT:content>' ] = ContentMetaAttributeValue ( content ) <EOL> return ( meta_encoding is not None ) <EOL> def register_treebuilders_from ( module ) : <EOL> """<STR_LIT>""" <EOL> this_module = sys . modules [ '<STR_LIT>' ] <EOL> for name in module . __all__ : <EOL> obj = getattr ( module , name ) <EOL> if issubclass ( obj , TreeBuilder ) : <EOL> setattr ( this_module , name , obj ) <EOL> this_module . __all__ . append ( name ) <EOL> this_module . builder_registry . register ( obj ) <EOL> class ParserRejectedMarkup ( Exception ) : <EOL> pass <EOL> from . import _htmlparser <EOL> register_treebuilders_from ( _htmlparser ) <EOL> try : <EOL> from . import _html5lib <EOL> register_treebuilders_from ( _html5lib ) <EOL> except ImportError : <EOL> pass <EOL> try : <EOL> from . import _lxml <EOL> register_treebuilders_from ( _lxml ) <EOL> except ImportError : <EOL> pass </s>
