<s> """<STR_LIT>""" <EOL> __author__ = "<STR_LIT>" <EOL> __version__ = "<STR_LIT>" <EOL> __copyright__ = "<STR_LIT>" <EOL> __license__ = "<STR_LIT>" <EOL> __all__ = [ '<STR_LIT>' ] <EOL> import os <EOL> import re <EOL> import warnings <EOL> from . builder import builder_registry , ParserRejectedMarkup <EOL> from . dammit import UnicodeDammit <EOL> from . element import ( <EOL> CData , <EOL> Comment , <EOL> DEFAULT_OUTPUT_ENCODING , <EOL> Declaration , <EOL> Doctype , <EOL> NavigableString , <EOL> PageElement , <EOL> ProcessingInstruction , <EOL> ResultSet , <EOL> SoupStrainer , <EOL> Tag , <EOL> ) <EOL> syntax_error = u'<STR_LIT>' <EOL> class BeautifulSoup ( Tag ) : <EOL> """<STR_LIT>""" <EOL> ROOT_TAG_NAME = u'<STR_LIT>' <EOL> DEFAULT_BUILDER_FEATURES = [ '<STR_LIT:html>' , '<STR_LIT>' ] <EOL> ASCII_SPACES = '<STR_LIT>' <EOL> def __init__ ( self , markup = "<STR_LIT>" , features = None , builder = None , <EOL> parse_only = None , from_encoding = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in kwargs : <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if '<STR_LIT>' in kwargs : <EOL> del kwargs [ '<STR_LIT>' ] <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if '<STR_LIT>' in kwargs : <EOL> del kwargs [ '<STR_LIT>' ] <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if '<STR_LIT>' in kwargs : <EOL> del kwargs [ '<STR_LIT>' ] <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if '<STR_LIT>' in kwargs : <EOL> del kwargs [ '<STR_LIT>' ] <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def deprecated_argument ( old_name , new_name ) : <EOL> if old_name in kwargs : <EOL> warnings . warn ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( old_name , new_name ) ) <EOL> value = kwargs [ old_name ] <EOL> del kwargs [ old_name ] <EOL> return value <EOL> return None <EOL> parse_only = parse_only or deprecated_argument ( <EOL> "<STR_LIT>" , "<STR_LIT>" ) <EOL> from_encoding = from_encoding or deprecated_argument ( <EOL> "<STR_LIT>" , "<STR_LIT>" ) <EOL> if len ( kwargs ) > <NUM_LIT:0> : <EOL> arg = kwargs . keys ( ) . pop ( ) <EOL> raise TypeError ( <EOL> "<STR_LIT>" % arg ) <EOL> if builder is None : <EOL> if isinstance ( features , basestring ) : <EOL> features = [ features ] <EOL> if features is None or len ( features ) == <NUM_LIT:0> : <EOL> features = self . DEFAULT_BUILDER_FEATURES <EOL> builder_class = builder_registry . lookup ( * features ) <EOL> if builder_class is None : <EOL> raise FeatureNotFound ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % "<STR_LIT:U+002C>" . join ( features ) ) <EOL> builder = builder_class ( ) <EOL> self . builder = builder <EOL> self . is_xml = builder . is_xml <EOL> self . builder . soup = self <EOL> self . parse_only = parse_only <EOL> if hasattr ( markup , '<STR_LIT>' ) : <EOL> markup = markup . read ( ) <EOL> elif len ( markup ) <= <NUM_LIT> : <EOL> if ( isinstance ( markup , unicode ) <EOL> and not os . path . supports_unicode_filenames ) : <EOL> possible_filename = markup . encode ( "<STR_LIT:utf8>" ) <EOL> else : <EOL> possible_filename = markup <EOL> is_file = False <EOL> try : <EOL> is_file = os . path . exists ( possible_filename ) <EOL> except Exception , e : <EOL> pass <EOL> if is_file : <EOL> warnings . warn ( <EOL> '<STR_LIT>' % markup ) <EOL> if markup [ : <NUM_LIT:5> ] == "<STR_LIT>" or markup [ : <NUM_LIT:6> ] == "<STR_LIT>" : <EOL> if ( ( isinstance ( markup , bytes ) and not b'<STR_LIT:U+0020>' in markup ) <EOL> or ( isinstance ( markup , unicode ) and not u'<STR_LIT:U+0020>' in markup ) ) : <EOL> warnings . warn ( <EOL> '<STR_LIT>' % markup ) <EOL> for ( self . markup , self . original_encoding , self . declared_html_encoding , <EOL> self . contains_replacement_characters ) in ( <EOL> self . builder . prepare_markup ( markup , from_encoding ) ) : <EOL> self . reset ( ) <EOL> try : <EOL> self . _feed ( ) <EOL> break <EOL> except ParserRejectedMarkup : <EOL> pass <EOL> self . markup = None <EOL> self . builder . soup = None <EOL> def _feed ( self ) : <EOL> self . builder . reset ( ) <EOL> self . builder . feed ( self . markup ) <EOL> self . endData ( ) <EOL> while self . currentTag . name != self . ROOT_TAG_NAME : <EOL> self . popTag ( ) <EOL> def reset ( self ) : <EOL> Tag . __init__ ( self , self , self . builder , self . ROOT_TAG_NAME ) <EOL> self . hidden = <NUM_LIT:1> <EOL> self . builder . reset ( ) <EOL> self . current_data = [ ] <EOL> self . currentTag = None <EOL> self . tagStack = [ ] <EOL> self . preserve_whitespace_tag_stack = [ ] <EOL> self . pushTag ( self ) <EOL> def new_tag ( self , name , namespace = None , nsprefix = None , ** attrs ) : <EOL> """<STR_LIT>""" <EOL> return Tag ( None , self . builder , name , namespace , nsprefix , attrs ) <EOL> def new_string ( self , s , subclass = NavigableString ) : <EOL> """<STR_LIT>""" <EOL> navigable = subclass ( s ) <EOL> navigable . setup ( ) <EOL> return navigable <EOL> def insert_before ( self , successor ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def insert_after ( self , successor ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def popTag ( self ) : <EOL> tag = self . tagStack . pop ( ) <EOL> if self . preserve_whitespace_tag_stack and tag == self . preserve_whitespace_tag_stack [ - <NUM_LIT:1> ] : <EOL> self . preserve_whitespace_tag_stack . pop ( ) <EOL> if self . tagStack : <EOL> self . currentTag = self . tagStack [ - <NUM_LIT:1> ] <EOL> return self . currentTag <EOL> def pushTag ( self , tag ) : <EOL> if self . currentTag : <EOL> self . currentTag . contents . append ( tag ) <EOL> self . tagStack . append ( tag ) <EOL> self . currentTag = self . tagStack [ - <NUM_LIT:1> ] <EOL> if tag . name in self . builder . preserve_whitespace_tags : <EOL> self . preserve_whitespace_tag_stack . append ( tag ) <EOL> def endData ( self , containerClass = NavigableString ) : <EOL> if self . current_data : <EOL> current_data = u'<STR_LIT>' . join ( self . current_data ) <EOL> if not self . preserve_whitespace_tag_stack : <EOL> strippable = True <EOL> for i in current_data : <EOL> if i not in self . ASCII_SPACES : <EOL> strippable = False <EOL> break <EOL> if strippable : <EOL> if '<STR_LIT:\n>' in current_data : <EOL> current_data = '<STR_LIT:\n>' <EOL> else : <EOL> current_data = '<STR_LIT:U+0020>' <EOL> self . current_data = [ ] <EOL> if self . parse_only and len ( self . tagStack ) <= <NUM_LIT:1> and ( not self . parse_only . text or not self . parse_only . search ( current_data ) ) : <EOL> return <EOL> o = containerClass ( current_data ) <EOL> self . object_was_parsed ( o ) <EOL> def object_was_parsed ( self , o , parent = None , most_recent_element = None ) : <EOL> """<STR_LIT>""" <EOL> parent = parent or self . currentTag <EOL> most_recent_element = most_recent_element or self . _most_recent_element <EOL> o . setup ( parent , most_recent_element ) <EOL> if most_recent_element is not None : <EOL> most_recent_element . next_element = o <EOL> self . _most_recent_element = o <EOL> parent . contents . append ( o ) <EOL> def _popToTag ( self , name , nsprefix = None , inclusivePop = True ) : <EOL> """<STR_LIT>""" <EOL> if name == self . ROOT_TAG_NAME : <EOL> return <EOL> most_recently_popped = None <EOL> stack_size = len ( self . tagStack ) <EOL> for i in range ( stack_size - <NUM_LIT:1> , <NUM_LIT:0> , - <NUM_LIT:1> ) : <EOL> t = self . tagStack [ i ] <EOL> if ( name == t . name and nsprefix == t . prefix ) : <EOL> if inclusivePop : <EOL> most_recently_popped = self . popTag ( ) <EOL> break <EOL> most_recently_popped = self . popTag ( ) <EOL> return most_recently_popped <EOL> def handle_starttag ( self , name , namespace , nsprefix , attrs ) : <EOL> """<STR_LIT>""" <EOL> self . endData ( ) <EOL> if ( self . parse_only and len ( self . tagStack ) <= <NUM_LIT:1> <EOL> and ( self . parse_only . text <EOL> or not self . parse_only . search_tag ( name , attrs ) ) ) : <EOL> return None <EOL> tag = Tag ( self , self . builder , name , namespace , nsprefix , attrs , <EOL> self . currentTag , self . _most_recent_element ) <EOL> if tag is None : <EOL> return tag <EOL> if self . _most_recent_element : <EOL> self . _most_recent_element . next_element = tag <EOL> self . _most_recent_element = tag <EOL> self . pushTag ( tag ) <EOL> return tag <EOL> def handle_endtag ( self , name , nsprefix = None ) : <EOL> self . endData ( ) <EOL> self . _popToTag ( name , nsprefix ) <EOL> def handle_data ( self , data ) : <EOL> self . current_data . append ( data ) <EOL> def decode ( self , pretty_print = False , <EOL> eventual_encoding = DEFAULT_OUTPUT_ENCODING , <EOL> formatter = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> if self . is_xml : <EOL> encoding_part = '<STR_LIT>' <EOL> if eventual_encoding != None : <EOL> encoding_part = '<STR_LIT>' % eventual_encoding <EOL> prefix = u'<STR_LIT>' % encoding_part <EOL> else : <EOL> prefix = u'<STR_LIT>' <EOL> if not pretty_print : <EOL> indent_level = None <EOL> else : <EOL> indent_level = <NUM_LIT:0> <EOL> return prefix + super ( BeautifulSoup , self ) . decode ( <EOL> indent_level , eventual_encoding , formatter ) <EOL> _s = BeautifulSoup <EOL> _soup = BeautifulSoup <EOL> class BeautifulStoneSoup ( BeautifulSoup ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> warnings . warn ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> super ( BeautifulStoneSoup , self ) . __init__ ( * args , ** kwargs ) <EOL> class StopParsing ( Exception ) : <EOL> pass <EOL> class FeatureNotFound ( ValueError ) : <EOL> pass <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import sys <EOL> soup = BeautifulSoup ( sys . stdin ) <EOL> print soup . prettify ( ) </s>
