<s> """<STR_LIT>""" <EOL> __docformat__ = '<STR_LIT>' <EOL> import sys <EOL> import os <EOL> import os . path <EOL> import warnings <EOL> import unicodedata <EOL> from docutils import ApplicationError , DataError <EOL> from docutils import nodes <EOL> class SystemMessage ( ApplicationError ) : <EOL> def __init__ ( self , system_message , level ) : <EOL> Exception . __init__ ( self , system_message . astext ( ) ) <EOL> self . level = level <EOL> class SystemMessagePropagation ( ApplicationError ) : pass <EOL> class Reporter : <EOL> """<STR_LIT>""" <EOL> levels = '<STR_LIT>' . split ( ) <EOL> """<STR_LIT>""" <EOL> ( DEBUG_LEVEL , <EOL> INFO_LEVEL , <EOL> WARNING_LEVEL , <EOL> ERROR_LEVEL , <EOL> SEVERE_LEVEL ) = range ( <NUM_LIT:5> ) <EOL> def __init__ ( self , source , report_level , halt_level , stream = None , <EOL> debug = <NUM_LIT:0> , encoding = '<STR_LIT:ascii>' , error_handler = '<STR_LIT:replace>' ) : <EOL> """<STR_LIT>""" <EOL> self . source = source <EOL> """<STR_LIT>""" <EOL> self . encoding = encoding <EOL> """<STR_LIT>""" <EOL> self . error_handler = error_handler <EOL> """<STR_LIT>""" <EOL> self . debug_flag = debug <EOL> """<STR_LIT>""" <EOL> self . report_level = report_level <EOL> """<STR_LIT>""" <EOL> self . halt_level = halt_level <EOL> """<STR_LIT>""" <EOL> if stream is None : <EOL> stream = sys . stderr <EOL> elif type ( stream ) in ( str , unicode ) : <EOL> if stream != '<STR_LIT>' : <EOL> if type ( stream ) == str : <EOL> stream = open ( stream , '<STR_LIT:w>' ) <EOL> elif type ( stream ) == unicode : <EOL> stream = open ( stream . encode ( ) , '<STR_LIT:w>' ) <EOL> self . stream = stream <EOL> """<STR_LIT>""" <EOL> self . observers = [ ] <EOL> """<STR_LIT>""" <EOL> self . max_level = - <NUM_LIT:1> <EOL> """<STR_LIT>""" <EOL> def set_conditions ( self , category , report_level , halt_level , <EOL> stream = None , debug = <NUM_LIT:0> ) : <EOL> warnings . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> DeprecationWarning , stacklevel = <NUM_LIT:2> ) <EOL> self . report_level = report_level <EOL> self . halt_level = halt_level <EOL> if stream is None : <EOL> stream = sys . stderr <EOL> self . stream = stream <EOL> self . debug_flag = debug <EOL> def attach_observer ( self , observer ) : <EOL> """<STR_LIT>""" <EOL> self . observers . append ( observer ) <EOL> def detach_observer ( self , observer ) : <EOL> self . observers . remove ( observer ) <EOL> def notify_observers ( self , message ) : <EOL> for observer in self . observers : <EOL> observer ( message ) <EOL> def system_message ( self , level , message , * children , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> attributes = kwargs . copy ( ) <EOL> if '<STR_LIT>' in kwargs : <EOL> source , line = get_source_line ( kwargs [ '<STR_LIT>' ] ) <EOL> del attributes [ '<STR_LIT>' ] <EOL> if source is not None : <EOL> attributes . setdefault ( '<STR_LIT:source>' , source ) <EOL> if line is not None : <EOL> attributes . setdefault ( '<STR_LIT>' , line ) <EOL> attributes . setdefault ( '<STR_LIT:source>' , self . source ) <EOL> msg = nodes . system_message ( message , level = level , <EOL> type = self . levels [ level ] , <EOL> * children , ** attributes ) <EOL> if self . stream and ( level >= self . report_level <EOL> or self . debug_flag and level == self . DEBUG_LEVEL ) : <EOL> msgtext = msg . astext ( ) . encode ( self . encoding , self . error_handler ) <EOL> print >> self . stream , msgtext <EOL> if level >= self . halt_level : <EOL> raise SystemMessage ( msg , level ) <EOL> if level > self . DEBUG_LEVEL or self . debug_flag : <EOL> self . notify_observers ( msg ) <EOL> self . max_level = max ( level , self . max_level ) <EOL> return msg <EOL> def debug ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if self . debug_flag : <EOL> return self . system_message ( self . DEBUG_LEVEL , * args , ** kwargs ) <EOL> def info ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . system_message ( self . INFO_LEVEL , * args , ** kwargs ) <EOL> def warning ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . system_message ( self . WARNING_LEVEL , * args , ** kwargs ) <EOL> def error ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . system_message ( self . ERROR_LEVEL , * args , ** kwargs ) <EOL> def severe ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . system_message ( self . SEVERE_LEVEL , * args , ** kwargs ) <EOL> class ExtensionOptionError ( DataError ) : pass <EOL> class BadOptionError ( ExtensionOptionError ) : pass <EOL> class BadOptionDataError ( ExtensionOptionError ) : pass <EOL> class DuplicateOptionError ( ExtensionOptionError ) : pass <EOL> def extract_extension_options ( field_list , options_spec ) : <EOL> """<STR_LIT>""" <EOL> option_list = extract_options ( field_list ) <EOL> option_dict = assemble_option_dict ( option_list , options_spec ) <EOL> return option_dict <EOL> def extract_options ( field_list ) : <EOL> """<STR_LIT>""" <EOL> option_list = [ ] <EOL> for field in field_list : <EOL> if len ( field [ <NUM_LIT:0> ] . astext ( ) . split ( ) ) != <NUM_LIT:1> : <EOL> raise BadOptionError ( <EOL> '<STR_LIT>' ) <EOL> name = str ( field [ <NUM_LIT:0> ] . astext ( ) . lower ( ) ) <EOL> body = field [ <NUM_LIT:1> ] <EOL> if len ( body ) == <NUM_LIT:0> : <EOL> data = None <EOL> elif len ( body ) > <NUM_LIT:1> or not isinstance ( body [ <NUM_LIT:0> ] , nodes . paragraph ) or len ( body [ <NUM_LIT:0> ] ) != <NUM_LIT:1> or not isinstance ( body [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , nodes . Text ) : <EOL> raise BadOptionDataError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % name ) <EOL> else : <EOL> data = body [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . astext ( ) <EOL> option_list . append ( ( name , data ) ) <EOL> return option_list <EOL> def assemble_option_dict ( option_list , options_spec ) : <EOL> """<STR_LIT>""" <EOL> options = { } <EOL> for name , value in option_list : <EOL> convertor = options_spec [ name ] <EOL> if convertor is None : <EOL> raise KeyError ( name ) <EOL> if name in options : <EOL> raise DuplicateOptionError ( '<STR_LIT>' % name ) <EOL> try : <EOL> options [ name ] = convertor ( value ) <EOL> except ( ValueError , TypeError ) , detail : <EOL> raise detail . __class__ ( '<STR_LIT>' <EOL> % ( name , value , '<STR_LIT:U+0020>' . join ( detail . args ) ) ) <EOL> return options <EOL> class NameValueError ( DataError ) : pass <EOL> def extract_name_value ( line ) : <EOL> """<STR_LIT>""" <EOL> attlist = [ ] <EOL> while line : <EOL> equals = line . find ( '<STR_LIT:=>' ) <EOL> if equals == - <NUM_LIT:1> : <EOL> raise NameValueError ( '<STR_LIT>' ) <EOL> attname = line [ : equals ] . strip ( ) <EOL> if equals == <NUM_LIT:0> or not attname : <EOL> raise NameValueError ( <EOL> '<STR_LIT>' ) <EOL> line = line [ equals + <NUM_LIT:1> : ] . lstrip ( ) <EOL> if not line : <EOL> raise NameValueError ( <EOL> '<STR_LIT>' % attname ) <EOL> if line [ <NUM_LIT:0> ] in '<STR_LIT>' : <EOL> endquote = line . find ( line [ <NUM_LIT:0> ] , <NUM_LIT:1> ) <EOL> if endquote == - <NUM_LIT:1> : <EOL> raise NameValueError ( <EOL> '<STR_LIT>' <EOL> % ( attname , line [ <NUM_LIT:0> ] ) ) <EOL> if len ( line ) > endquote + <NUM_LIT:1> and line [ endquote + <NUM_LIT:1> ] . strip ( ) : <EOL> raise NameValueError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( attname , line [ <NUM_LIT:0> ] ) ) <EOL> data = line [ <NUM_LIT:1> : endquote ] <EOL> line = line [ endquote + <NUM_LIT:1> : ] . lstrip ( ) <EOL> else : <EOL> space = line . find ( '<STR_LIT:U+0020>' ) <EOL> if space == - <NUM_LIT:1> : <EOL> data = line <EOL> line = '<STR_LIT>' <EOL> else : <EOL> data = line [ : space ] <EOL> line = line [ space + <NUM_LIT:1> : ] . lstrip ( ) <EOL> attlist . append ( ( attname . lower ( ) , data ) ) <EOL> return attlist <EOL> def new_reporter ( source_path , settings ) : <EOL> """<STR_LIT>""" <EOL> reporter = Reporter ( <EOL> source_path , settings . report_level , settings . halt_level , <EOL> stream = settings . warning_stream , debug = settings . debug , <EOL> encoding = settings . error_encoding , <EOL> error_handler = settings . error_encoding_error_handler ) <EOL> return reporter <EOL> def new_document ( source_path , settings = None ) : <EOL> """<STR_LIT>""" <EOL> from docutils import frontend <EOL> if settings is None : <EOL> settings = frontend . OptionParser ( ) . get_default_values ( ) <EOL> reporter = new_reporter ( source_path , settings ) <EOL> document = nodes . document ( settings , reporter , source = source_path ) <EOL> document . note_source ( source_path , - <NUM_LIT:1> ) <EOL> return document <EOL> def clean_rcs_keywords ( paragraph , keyword_substitutions ) : <EOL> if len ( paragraph ) == <NUM_LIT:1> and isinstance ( paragraph [ <NUM_LIT:0> ] , nodes . Text ) : <EOL> textnode = paragraph [ <NUM_LIT:0> ] <EOL> for pattern , substitution in keyword_substitutions : <EOL> match = pattern . search ( textnode ) <EOL> if match : <EOL> paragraph [ <NUM_LIT:0> ] = nodes . Text ( pattern . sub ( substitution , textnode ) ) <EOL> return <EOL> def relative_path ( source , target ) : <EOL> """<STR_LIT>""" <EOL> source_parts = os . path . abspath ( source or '<STR_LIT>' ) . split ( os . sep ) <EOL> target_parts = os . path . abspath ( target ) . split ( os . sep ) <EOL> if source_parts [ : <NUM_LIT:2> ] != target_parts [ : <NUM_LIT:2> ] : <EOL> return '<STR_LIT:/>' . join ( target_parts ) <EOL> source_parts . reverse ( ) <EOL> target_parts . reverse ( ) <EOL> while ( source_parts and target_parts <EOL> and source_parts [ - <NUM_LIT:1> ] == target_parts [ - <NUM_LIT:1> ] ) : <EOL> source_parts . pop ( ) <EOL> target_parts . pop ( ) <EOL> target_parts . reverse ( ) <EOL> parts = [ '<STR_LIT:..>' ] * ( len ( source_parts ) - <NUM_LIT:1> ) + target_parts <EOL> return '<STR_LIT:/>' . join ( parts ) <EOL> def get_stylesheet_reference ( settings , relative_to = None ) : <EOL> """<STR_LIT>""" <EOL> if settings . stylesheet_path : <EOL> assert not settings . stylesheet , ( <EOL> '<STR_LIT>' ) <EOL> if relative_to == None : <EOL> relative_to = settings . _destination <EOL> return relative_path ( relative_to , settings . stylesheet_path ) <EOL> else : <EOL> return settings . stylesheet <EOL> def get_stylesheet_list ( settings ) : <EOL> """<STR_LIT>""" <EOL> if settings . stylesheet_path : <EOL> assert not settings . stylesheet , ( <EOL> '<STR_LIT>' ) <EOL> return settings . stylesheet_path . split ( "<STR_LIT:U+002C>" ) <EOL> elif settings . stylesheet : <EOL> return settings . stylesheet . split ( "<STR_LIT:U+002C>" ) <EOL> else : <EOL> return [ ] <EOL> def get_trim_footnote_ref_space ( settings ) : <EOL> """<STR_LIT>""" <EOL> if settings . trim_footnote_reference_space is None : <EOL> return hasattr ( settings , '<STR_LIT>' ) and settings . footnote_references == '<STR_LIT>' <EOL> else : <EOL> return settings . trim_footnote_reference_space <EOL> def get_source_line ( node ) : <EOL> """<STR_LIT>""" <EOL> while node : <EOL> if node . source or node . line : <EOL> return node . source , node . line <EOL> node = node . parent <EOL> return None , None <EOL> def escape2null ( text ) : <EOL> """<STR_LIT>""" <EOL> parts = [ ] <EOL> start = <NUM_LIT:0> <EOL> while <NUM_LIT:1> : <EOL> found = text . find ( '<STR_LIT:\\>' , start ) <EOL> if found == - <NUM_LIT:1> : <EOL> parts . append ( text [ start : ] ) <EOL> return '<STR_LIT>' . join ( parts ) <EOL> parts . append ( text [ start : found ] ) <EOL> parts . append ( '<STR_LIT:\x00>' + text [ found + <NUM_LIT:1> : found + <NUM_LIT:2> ] ) <EOL> start = found + <NUM_LIT:2> <EOL> def unescape ( text , restore_backslashes = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if restore_backslashes : <EOL> return text . replace ( '<STR_LIT:\x00>' , '<STR_LIT:\\>' ) <EOL> else : <EOL> for sep in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:\x00>' ] : <EOL> text = '<STR_LIT>' . join ( text . split ( sep ) ) <EOL> return text <EOL> east_asian_widths = { '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT:F>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT:H>' : <NUM_LIT:1> , <EOL> '<STR_LIT:N>' : <NUM_LIT:1> , <EOL> '<STR_LIT:A>' : <NUM_LIT:1> } <EOL> """<STR_LIT>""" <EOL> def east_asian_column_width ( text ) : <EOL> if isinstance ( text , unicode ) : <EOL> total = <NUM_LIT:0> <EOL> for c in text : <EOL> total += east_asian_widths [ unicodedata . east_asian_width ( c ) ] <EOL> return total <EOL> else : <EOL> return len ( text ) <EOL> if hasattr ( unicodedata , '<STR_LIT>' ) : <EOL> column_width = east_asian_column_width <EOL> else : <EOL> column_width = len <EOL> def uniq ( L ) : <EOL> r = [ ] <EOL> for item in L : <EOL> if not item in r : <EOL> r . append ( item ) <EOL> return r <EOL> class DependencyList : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , output_file = None , dependencies = [ ] ) : <EOL> """<STR_LIT>""" <EOL> self . set_output ( output_file ) <EOL> for i in dependencies : <EOL> self . add ( i ) <EOL> def set_output ( self , output_file ) : <EOL> """<STR_LIT>""" <EOL> self . list = [ ] <EOL> if output_file == '<STR_LIT:->' : <EOL> self . file = sys . stdout <EOL> elif output_file : <EOL> self . file = open ( output_file , '<STR_LIT:w>' ) <EOL> else : <EOL> self . file = None <EOL> def add ( self , * filenames ) : <EOL> """<STR_LIT>""" <EOL> for filename in filenames : <EOL> if not filename in self . list : <EOL> self . list . append ( filename ) <EOL> if self . file is not None : <EOL> print >> self . file , filename <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> self . file . close ( ) <EOL> self . file = None <EOL> def __repr__ ( self ) : <EOL> if self . file : <EOL> output_file = self . file . name <EOL> else : <EOL> output_file = None <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , output_file , self . list ) </s>
