<s> """<STR_LIT>""" <EOL> __docformat__ = '<STR_LIT>' <EOL> import sys <EOL> import re <EOL> import types <EOL> import unicodedata <EOL> from docutils . error_reporting import ErrorOutput <EOL> class StateMachine : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , state_classes , initial_state , debug = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> self . input_lines = None <EOL> """<STR_LIT>""" <EOL> self . input_offset = <NUM_LIT:0> <EOL> """<STR_LIT>""" <EOL> self . line = None <EOL> """<STR_LIT>""" <EOL> self . line_offset = - <NUM_LIT:1> <EOL> """<STR_LIT>""" <EOL> self . debug = debug <EOL> """<STR_LIT>""" <EOL> self . initial_state = initial_state <EOL> """<STR_LIT>""" <EOL> self . current_state = initial_state <EOL> """<STR_LIT>""" <EOL> self . states = { } <EOL> """<STR_LIT>""" <EOL> self . add_states ( state_classes ) <EOL> self . observers = [ ] <EOL> """<STR_LIT>""" <EOL> self . _stderr = ErrorOutput ( ) <EOL> """<STR_LIT>""" <EOL> def unlink ( self ) : <EOL> """<STR_LIT>""" <EOL> for state in self . states . values ( ) : <EOL> state . unlink ( ) <EOL> self . states = None <EOL> def run ( self , input_lines , input_offset = <NUM_LIT:0> , context = None , <EOL> input_source = None , initial_state = None ) : <EOL> """<STR_LIT>""" <EOL> self . runtime_init ( ) <EOL> if isinstance ( input_lines , StringList ) : <EOL> self . input_lines = input_lines <EOL> else : <EOL> self . input_lines = StringList ( input_lines , source = input_source ) <EOL> self . input_offset = input_offset <EOL> self . line_offset = - <NUM_LIT:1> <EOL> self . current_state = initial_state or self . initial_state <EOL> if self . debug : <EOL> print >> self . _stderr , ( <EOL> u'<STR_LIT>' <EOL> % ( self . line_offset , u'<STR_LIT>' . join ( self . input_lines ) ) ) <EOL> transitions = None <EOL> results = [ ] <EOL> state = self . get_state ( ) <EOL> try : <EOL> if self . debug : <EOL> print >> self . _stderr , '<STR_LIT>' <EOL> context , result = state . bof ( context ) <EOL> results . extend ( result ) <EOL> while <NUM_LIT:1> : <EOL> try : <EOL> try : <EOL> self . next_line ( ) <EOL> if self . debug : <EOL> source , offset = self . input_lines . info ( <EOL> self . line_offset ) <EOL> print >> self . _stderr , ( <EOL> u'<STR_LIT>' <EOL> u'<STR_LIT>' <EOL> % ( source , offset , self . line ) ) <EOL> context , next_state , result = self . check_line ( <EOL> context , state , transitions ) <EOL> except EOFError : <EOL> if self . debug : <EOL> print >> self . _stderr , ( <EOL> '<STR_LIT>' <EOL> % state . __class__ . __name__ ) <EOL> result = state . eof ( context ) <EOL> results . extend ( result ) <EOL> break <EOL> else : <EOL> results . extend ( result ) <EOL> except TransitionCorrection , exception : <EOL> self . previous_line ( ) <EOL> transitions = ( exception . args [ <NUM_LIT:0> ] , ) <EOL> if self . debug : <EOL> print >> self . _stderr , ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( state . __class__ . __name__ , transitions [ <NUM_LIT:0> ] ) ) <EOL> continue <EOL> except StateCorrection , exception : <EOL> self . previous_line ( ) <EOL> next_state = exception . args [ <NUM_LIT:0> ] <EOL> if len ( exception . args ) == <NUM_LIT:1> : <EOL> transitions = None <EOL> else : <EOL> transitions = ( exception . args [ <NUM_LIT:1> ] , ) <EOL> if self . debug : <EOL> print >> self . _stderr , ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( next_state , transitions [ <NUM_LIT:0> ] ) ) <EOL> else : <EOL> transitions = None <EOL> state = self . get_state ( next_state ) <EOL> except : <EOL> if self . debug : <EOL> self . error ( ) <EOL> raise <EOL> self . observers = [ ] <EOL> return results <EOL> def get_state ( self , next_state = None ) : <EOL> """<STR_LIT>""" <EOL> if next_state : <EOL> if self . debug and next_state != self . current_state : <EOL> print >> self . _stderr , ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( self . current_state , next_state , <EOL> self . abs_line_number ( ) ) ) <EOL> self . current_state = next_state <EOL> try : <EOL> return self . states [ self . current_state ] <EOL> except KeyError : <EOL> raise UnknownStateError ( self . current_state ) <EOL> def next_line ( self , n = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> try : <EOL> self . line_offset += n <EOL> self . line = self . input_lines [ self . line_offset ] <EOL> except IndexError : <EOL> self . line = None <EOL> raise EOFError <EOL> return self . line <EOL> finally : <EOL> self . notify_observers ( ) <EOL> def is_next_line_blank ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return not self . input_lines [ self . line_offset + <NUM_LIT:1> ] . strip ( ) <EOL> except IndexError : <EOL> return <NUM_LIT:1> <EOL> def at_eof ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . line_offset >= len ( self . input_lines ) - <NUM_LIT:1> <EOL> def at_bof ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . line_offset <= <NUM_LIT:0> <EOL> def previous_line ( self , n = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> self . line_offset -= n <EOL> if self . line_offset < <NUM_LIT:0> : <EOL> self . line = None <EOL> else : <EOL> self . line = self . input_lines [ self . line_offset ] <EOL> self . notify_observers ( ) <EOL> return self . line <EOL> def goto_line ( self , line_offset ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> try : <EOL> self . line_offset = line_offset - self . input_offset <EOL> self . line = self . input_lines [ self . line_offset ] <EOL> except IndexError : <EOL> self . line = None <EOL> raise EOFError <EOL> return self . line <EOL> finally : <EOL> self . notify_observers ( ) <EOL> def get_source ( self , line_offset ) : <EOL> """<STR_LIT>""" <EOL> return self . input_lines . source ( line_offset - self . input_offset ) <EOL> def abs_line_offset ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . line_offset + self . input_offset <EOL> def abs_line_number ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . line_offset + self . input_offset + <NUM_LIT:1> <EOL> def get_source_and_line ( self , lineno = None ) : <EOL> """<STR_LIT>""" <EOL> if lineno is None : <EOL> offset = self . line_offset <EOL> else : <EOL> offset = lineno - self . input_offset - <NUM_LIT:1> <EOL> try : <EOL> src , srcoffset = self . input_lines . info ( offset ) <EOL> srcline = srcoffset + <NUM_LIT:1> <EOL> except ( TypeError ) : <EOL> src , srcline = self . get_source_and_line ( offset + self . input_offset ) <EOL> return src , srcline + <NUM_LIT:1> <EOL> except ( IndexError ) : <EOL> src , srcline = None , None <EOL> return ( src , srcline ) <EOL> def insert_input ( self , input_lines , source ) : <EOL> self . input_lines . insert ( self . line_offset + <NUM_LIT:1> , '<STR_LIT>' , <EOL> source = '<STR_LIT>' + source , <EOL> offset = len ( input_lines ) ) <EOL> self . input_lines . insert ( self . line_offset + <NUM_LIT:1> , '<STR_LIT>' , <EOL> source = '<STR_LIT>' + source , <EOL> offset = - <NUM_LIT:1> ) <EOL> self . input_lines . insert ( self . line_offset + <NUM_LIT:2> , <EOL> StringList ( input_lines , source ) ) <EOL> def get_text_block ( self , flush_left = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> block = self . input_lines . get_text_block ( self . line_offset , <EOL> flush_left ) <EOL> self . next_line ( len ( block ) - <NUM_LIT:1> ) <EOL> return block <EOL> except UnexpectedIndentationError , error : <EOL> block , source , lineno = error . args <EOL> self . next_line ( len ( block ) - <NUM_LIT:1> ) <EOL> raise <EOL> def check_line ( self , context , state , transitions = None ) : <EOL> """<STR_LIT>""" <EOL> if transitions is None : <EOL> transitions = state . transition_order <EOL> state_correction = None <EOL> if self . debug : <EOL> print >> self . _stderr , ( <EOL> '<STR_LIT>' <EOL> % ( state . __class__ . __name__ , transitions ) ) <EOL> for name in transitions : <EOL> pattern , method , next_state = state . transitions [ name ] <EOL> match = pattern . match ( self . line ) <EOL> if match : <EOL> if self . debug : <EOL> print >> self . _stderr , ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( name , state . __class__ . __name__ ) ) <EOL> return method ( match , context , next_state ) <EOL> else : <EOL> if self . debug : <EOL> print >> self . _stderr , ( <EOL> '<STR_LIT>' <EOL> % state . __class__ . __name__ ) <EOL> return state . no_match ( context , transitions ) <EOL> def add_state ( self , state_class ) : <EOL> """<STR_LIT>""" <EOL> statename = state_class . __name__ <EOL> if statename in self . states : <EOL> raise DuplicateStateError ( statename ) <EOL> self . states [ statename ] = state_class ( self , self . debug ) <EOL> def add_states ( self , state_classes ) : <EOL> """<STR_LIT>""" <EOL> for state_class in state_classes : <EOL> self . add_state ( state_class ) <EOL> def runtime_init ( self ) : <EOL> """<STR_LIT>""" <EOL> for state in self . states . values ( ) : <EOL> state . runtime_init ( ) <EOL> def error ( self ) : <EOL> """<STR_LIT>""" <EOL> type , value , module , line , function = _exception_data ( ) <EOL> print >> self . _stderr , u'<STR_LIT>' % ( type , value ) <EOL> print >> self . _stderr , '<STR_LIT>' % ( self . abs_line_number ( ) ) <EOL> print >> self . _stderr , ( u'<STR_LIT>' % <EOL> ( module , line , function ) ) <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 ) : <EOL> for observer in self . observers : <EOL> try : <EOL> info = self . input_lines . info ( self . line_offset ) <EOL> except IndexError : <EOL> info = ( None , None ) <EOL> observer ( * info ) <EOL> class State : <EOL> """<STR_LIT>""" <EOL> patterns = None <EOL> """<STR_LIT>""" <EOL> initial_transitions = None <EOL> """<STR_LIT>""" <EOL> nested_sm = None <EOL> """<STR_LIT>""" <EOL> nested_sm_kwargs = None <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , state_machine , debug = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> self . transition_order = [ ] <EOL> """<STR_LIT>""" <EOL> self . transitions = { } <EOL> """<STR_LIT>""" <EOL> self . add_initial_transitions ( ) <EOL> self . state_machine = state_machine <EOL> """<STR_LIT>""" <EOL> self . debug = debug <EOL> """<STR_LIT>""" <EOL> if self . nested_sm is None : <EOL> self . nested_sm = self . state_machine . __class__ <EOL> if self . nested_sm_kwargs is None : <EOL> self . nested_sm_kwargs = { '<STR_LIT>' : [ self . __class__ ] , <EOL> '<STR_LIT>' : self . __class__ . __name__ } <EOL> def runtime_init ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def unlink ( self ) : <EOL> """<STR_LIT>""" <EOL> self . state_machine = None <EOL> def add_initial_transitions ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . initial_transitions : <EOL> names , transitions = self . make_transitions ( <EOL> self . initial_transitions ) <EOL> self . add_transitions ( names , transitions ) <EOL> def add_transitions ( self , names , transitions ) : <EOL> """<STR_LIT>""" <EOL> for name in names : <EOL> if name in self . transitions : <EOL> raise DuplicateTransitionError ( name ) <EOL> if name not in transitions : <EOL> raise UnknownTransitionError ( name ) <EOL> self . transition_order [ : <NUM_LIT:0> ] = names <EOL> self . transitions . update ( transitions ) <EOL> def add_transition ( self , name , transition ) : <EOL> """<STR_LIT>""" <EOL> if name in self . transitions : <EOL> raise DuplicateTransitionError ( name ) <EOL> self . transition_order [ : <NUM_LIT:0> ] = [ name ] <EOL> self . transitions [ name ] = transition <EOL> def remove_transition ( self , name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> del self . transitions [ name ] <EOL> self . transition_order . remove ( name ) <EOL> except : <EOL> raise UnknownTransitionError ( name ) <EOL> def make_transition ( self , name , next_state = None ) : <EOL> """<STR_LIT>""" <EOL> if next_state is None : <EOL> next_state = self . __class__ . __name__ <EOL> try : <EOL> pattern = self . patterns [ name ] <EOL> if not hasattr ( pattern , '<STR_LIT>' ) : <EOL> pattern = re . compile ( pattern ) <EOL> except KeyError : <EOL> raise TransitionPatternNotFound ( <EOL> '<STR_LIT>' % ( self . __class__ . __name__ , name ) ) <EOL> try : <EOL> method = getattr ( self , name ) <EOL> except AttributeError : <EOL> raise TransitionMethodNotFound ( <EOL> '<STR_LIT>' % ( self . __class__ . __name__ , name ) ) <EOL> return ( pattern , method , next_state ) <EOL> def make_transitions ( self , name_list ) : <EOL> """<STR_LIT>""" <EOL> stringtype = type ( '<STR_LIT>' ) <EOL> names = [ ] <EOL> transitions = { } <EOL> for namestate in name_list : <EOL> if type ( namestate ) is stringtype : <EOL> transitions [ namestate ] = self . make_transition ( namestate ) <EOL> names . append ( namestate ) <EOL> else : <EOL> transitions [ namestate [ <NUM_LIT:0> ] ] = self . make_transition ( * namestate ) <EOL> names . append ( namestate [ <NUM_LIT:0> ] ) <EOL> return names , transitions <EOL> def no_match ( self , context , transitions ) : <EOL> """<STR_LIT>""" <EOL> return context , None , [ ] <EOL> def bof ( self , context ) : <EOL> """<STR_LIT>""" <EOL> return context , [ ] <EOL> def eof ( self , context ) : <EOL> """<STR_LIT>""" <EOL> return [ ] <EOL> def nop ( self , match , context , next_state ) : <EOL> """<STR_LIT>""" <EOL> return context , next_state , [ ] <EOL> class StateMachineWS ( StateMachine ) : <EOL> """<STR_LIT>""" <EOL> def get_indented ( self , until_blank = <NUM_LIT:0> , strip_indent = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> offset = self . abs_line_offset ( ) <EOL> indented , indent , blank_finish = self . input_lines . get_indented ( <EOL> self . line_offset , until_blank , strip_indent ) <EOL> if indented : <EOL> self . next_line ( len ( indented ) - <NUM_LIT:1> ) <EOL> while indented and not indented [ <NUM_LIT:0> ] . strip ( ) : <EOL> indented . trim_start ( ) <EOL> offset += <NUM_LIT:1> <EOL> return indented , indent , offset , blank_finish <EOL> def get_known_indented ( self , indent , until_blank = <NUM_LIT:0> , strip_indent = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> offset = self . abs_line_offset ( ) <EOL> indented , indent , blank_finish = self . input_lines . get_indented ( <EOL> self . line_offset , until_blank , strip_indent , <EOL> block_indent = indent ) <EOL> self . next_line ( len ( indented ) - <NUM_LIT:1> ) <EOL> while indented and not indented [ <NUM_LIT:0> ] . strip ( ) : <EOL> indented . trim_start ( ) <EOL> offset += <NUM_LIT:1> <EOL> return indented , offset , blank_finish <EOL> def get_first_known_indented ( self , indent , until_blank = <NUM_LIT:0> , strip_indent = <NUM_LIT:1> , <EOL> strip_top = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> offset = self . abs_line_offset ( ) <EOL> indented , indent , blank_finish = self . input_lines . get_indented ( <EOL> self . line_offset , until_blank , strip_indent , <EOL> first_indent = indent ) <EOL> self . next_line ( len ( indented ) - <NUM_LIT:1> ) <EOL> if strip_top : <EOL> while indented and not indented [ <NUM_LIT:0> ] . strip ( ) : <EOL> indented . trim_start ( ) <EOL> offset += <NUM_LIT:1> <EOL> return indented , indent , offset , blank_finish <EOL> class StateWS ( State ) : <EOL> """<STR_LIT>""" <EOL> indent_sm = None <EOL> """<STR_LIT>""" <EOL> indent_sm_kwargs = None <EOL> """<STR_LIT>""" <EOL> known_indent_sm = None <EOL> """<STR_LIT>""" <EOL> known_indent_sm_kwargs = None <EOL> """<STR_LIT>""" <EOL> ws_patterns = { '<STR_LIT:blank>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> """<STR_LIT>""" <EOL> ws_initial_transitions = ( '<STR_LIT:blank>' , '<STR_LIT>' ) <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , state_machine , debug = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> State . __init__ ( self , state_machine , debug ) <EOL> if self . indent_sm is None : <EOL> self . indent_sm = self . nested_sm <EOL> if self . indent_sm_kwargs is None : <EOL> self . indent_sm_kwargs = self . nested_sm_kwargs <EOL> if self . known_indent_sm is None : <EOL> self . known_indent_sm = self . indent_sm <EOL> if self . known_indent_sm_kwargs is None : <EOL> self . known_indent_sm_kwargs = self . indent_sm_kwargs <EOL> def add_initial_transitions ( self ) : <EOL> """<STR_LIT>""" <EOL> State . add_initial_transitions ( self ) <EOL> if self . patterns is None : <EOL> self . patterns = { } <EOL> self . patterns . update ( self . ws_patterns ) <EOL> names , transitions = self . make_transitions ( <EOL> self . ws_initial_transitions ) <EOL> self . add_transitions ( names , transitions ) <EOL> def blank ( self , match , context , next_state ) : <EOL> """<STR_LIT>""" <EOL> return self . nop ( match , context , next_state ) <EOL> def indent ( self , match , context , next_state ) : <EOL> """<STR_LIT>""" <EOL> indented , indent , line_offset , blank_finish = self . state_machine . get_indented ( ) <EOL> sm = self . indent_sm ( debug = self . debug , ** self . indent_sm_kwargs ) <EOL> results = sm . run ( indented , input_offset = line_offset ) <EOL> return context , next_state , results <EOL> def known_indent ( self , match , context , next_state ) : <EOL> """<STR_LIT>""" <EOL> indented , line_offset , blank_finish = self . state_machine . get_known_indented ( match . end ( ) ) <EOL> sm = self . known_indent_sm ( debug = self . debug , <EOL> ** self . known_indent_sm_kwargs ) <EOL> results = sm . run ( indented , input_offset = line_offset ) <EOL> return context , next_state , results <EOL> def first_known_indent ( self , match , context , next_state ) : <EOL> """<STR_LIT>""" <EOL> indented , line_offset , blank_finish = self . state_machine . get_first_known_indented ( match . end ( ) ) <EOL> sm = self . known_indent_sm ( debug = self . debug , <EOL> ** self . known_indent_sm_kwargs ) <EOL> results = sm . run ( indented , input_offset = line_offset ) <EOL> return context , next_state , results <EOL> class _SearchOverride : <EOL> """<STR_LIT>""" <EOL> def match ( self , pattern ) : <EOL> """<STR_LIT>""" <EOL> return pattern . search ( self . line ) <EOL> class SearchStateMachine ( _SearchOverride , StateMachine ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class SearchStateMachineWS ( _SearchOverride , StateMachineWS ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class ViewList : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , initlist = None , source = None , items = None , <EOL> parent = None , parent_offset = None ) : <EOL> self . data = [ ] <EOL> """<STR_LIT>""" <EOL> self . items = [ ] <EOL> """<STR_LIT>""" <EOL> self . parent = parent <EOL> """<STR_LIT>""" <EOL> self . parent_offset = parent_offset <EOL> """<STR_LIT>""" <EOL> if isinstance ( initlist , ViewList ) : <EOL> self . data = initlist . data [ : ] <EOL> self . items = initlist . items [ : ] <EOL> elif initlist is not None : <EOL> self . data = list ( initlist ) <EOL> if items : <EOL> self . items = items <EOL> else : <EOL> self . items = [ ( source , i ) for i in range ( len ( initlist ) ) ] <EOL> assert len ( self . data ) == len ( self . items ) , '<STR_LIT>' <EOL> def __str__ ( self ) : <EOL> return str ( self . data ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , <EOL> self . data , self . items ) <EOL> def __lt__ ( self , other ) : return self . data < self . __cast ( other ) <EOL> def __le__ ( self , other ) : return self . data <= self . __cast ( other ) <EOL> def __eq__ ( self , other ) : return self . data == self . __cast ( other ) <EOL> def __ne__ ( self , other ) : return self . data != self . __cast ( other ) <EOL> def __gt__ ( self , other ) : return self . data > self . __cast ( other ) <EOL> def __ge__ ( self , other ) : return self . data >= self . __cast ( other ) <EOL> def __cmp__ ( self , other ) : return cmp ( self . data , self . __cast ( other ) ) <EOL> def __cast ( self , other ) : <EOL> if isinstance ( other , ViewList ) : <EOL> return other . data <EOL> else : <EOL> return other <EOL> def __contains__ ( self , item ) : return item in self . data <EOL> def __len__ ( self ) : return len ( self . data ) <EOL> def __getitem__ ( self , i ) : <EOL> if isinstance ( i , types . SliceType ) : <EOL> assert i . step in ( None , <NUM_LIT:1> ) , '<STR_LIT>' <EOL> return self . __class__ ( self . data [ i . start : i . stop ] , <EOL> items = self . items [ i . start : i . stop ] , <EOL> parent = self , parent_offset = i . start or <NUM_LIT:0> ) <EOL> else : <EOL> return self . data [ i ] <EOL> def __setitem__ ( self , i , item ) : <EOL> if isinstance ( i , types . SliceType ) : <EOL> assert i . step in ( None , <NUM_LIT:1> ) , '<STR_LIT>' <EOL> if not isinstance ( item , ViewList ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> self . data [ i . start : i . stop ] = item . data <EOL> self . items [ i . start : i . stop ] = item . items <EOL> assert len ( self . data ) == len ( self . items ) , '<STR_LIT>' <EOL> if self . parent : <EOL> self . parent [ ( i . start or <NUM_LIT:0> ) + self . parent_offset <EOL> : ( i . stop or len ( self ) ) + self . parent_offset ] = item <EOL> else : <EOL> self . data [ i ] = item <EOL> if self . parent : <EOL> self . parent [ i + self . parent_offset ] = item <EOL> def __delitem__ ( self , i ) : <EOL> try : <EOL> del self . data [ i ] <EOL> del self . items [ i ] <EOL> if self . parent : <EOL> del self . parent [ i + self . parent_offset ] <EOL> except TypeError : <EOL> assert i . step is None , '<STR_LIT>' <EOL> del self . data [ i . start : i . stop ] <EOL> del self . items [ i . start : i . stop ] <EOL> if self . parent : <EOL> del self . parent [ ( i . start or <NUM_LIT:0> ) + self . parent_offset <EOL> : ( i . stop or len ( self ) ) + self . parent_offset ] <EOL> def __add__ ( self , other ) : <EOL> if isinstance ( other , ViewList ) : <EOL> return self . __class__ ( self . data + other . data , <EOL> items = ( self . items + other . items ) ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __radd__ ( self , other ) : <EOL> if isinstance ( other , ViewList ) : <EOL> return self . __class__ ( other . data + self . data , <EOL> items = ( other . items + self . items ) ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __iadd__ ( self , other ) : <EOL> if isinstance ( other , ViewList ) : <EOL> self . data += other . data <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> return self <EOL> def __mul__ ( self , n ) : <EOL> return self . __class__ ( self . data * n , items = ( self . items * n ) ) <EOL> __rmul__ = __mul__ <EOL> def __imul__ ( self , n ) : <EOL> self . data *= n <EOL> self . items *= n <EOL> return self <EOL> def extend ( self , other ) : <EOL> if not isinstance ( other , ViewList ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if self . parent : <EOL> self . parent . insert ( len ( self . data ) + self . parent_offset , other ) <EOL> self . data . extend ( other . data ) <EOL> self . items . extend ( other . items ) <EOL> def append ( self , item , source = None , offset = <NUM_LIT:0> ) : <EOL> if source is None : <EOL> self . extend ( item ) <EOL> else : <EOL> if self . parent : <EOL> self . parent . insert ( len ( self . data ) + self . parent_offset , item , <EOL> source , offset ) <EOL> self . data . append ( item ) <EOL> self . items . append ( ( source , offset ) ) <EOL> def insert ( self , i , item , source = None , offset = <NUM_LIT:0> ) : <EOL> if source is None : <EOL> if not isinstance ( item , ViewList ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> self . data [ i : i ] = item . data <EOL> self . items [ i : i ] = item . items <EOL> if self . parent : <EOL> index = ( len ( self . data ) + i ) % len ( self . data ) <EOL> self . parent . insert ( index + self . parent_offset , item ) <EOL> else : <EOL> self . data . insert ( i , item ) <EOL> self . items . insert ( i , ( source , offset ) ) <EOL> if self . parent : <EOL> index = ( len ( self . data ) + i ) % len ( self . data ) <EOL> self . parent . insert ( index + self . parent_offset , item , <EOL> source , offset ) <EOL> def pop ( self , i = - <NUM_LIT:1> ) : <EOL> if self . parent : <EOL> index = ( len ( self . data ) + i ) % len ( self . data ) <EOL> self . parent . pop ( index + self . parent_offset ) <EOL> self . items . pop ( i ) <EOL> return self . data . pop ( i ) <EOL> def trim_start ( self , n = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> if n > len ( self . data ) : <EOL> raise IndexError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( n , len ( self . data ) ) ) <EOL> elif n < <NUM_LIT:0> : <EOL> raise IndexError ( '<STR_LIT>' ) <EOL> del self . data [ : n ] <EOL> del self . items [ : n ] <EOL> if self . parent : <EOL> self . parent_offset += n <EOL> def trim_end ( self , n = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> if n > len ( self . data ) : <EOL> raise IndexError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( n , len ( self . data ) ) ) <EOL> elif n < <NUM_LIT:0> : <EOL> raise IndexError ( '<STR_LIT>' ) <EOL> del self . data [ - n : ] <EOL> del self . items [ - n : ] <EOL> def remove ( self , item ) : <EOL> index = self . index ( item ) <EOL> del self [ index ] <EOL> def count ( self , item ) : return self . data . count ( item ) <EOL> def index ( self , item ) : return self . data . index ( item ) <EOL> def reverse ( self ) : <EOL> self . data . reverse ( ) <EOL> self . items . reverse ( ) <EOL> self . parent = None <EOL> def sort ( self , * args ) : <EOL> tmp = zip ( self . data , self . items ) <EOL> tmp . sort ( * args ) <EOL> self . data = [ entry [ <NUM_LIT:0> ] for entry in tmp ] <EOL> self . items = [ entry [ <NUM_LIT:1> ] for entry in tmp ] <EOL> self . parent = None <EOL> def info ( self , i ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . items [ i ] <EOL> except IndexError : <EOL> if i == len ( self . data ) : <EOL> return self . items [ i - <NUM_LIT:1> ] [ <NUM_LIT:0> ] , None <EOL> else : <EOL> raise <EOL> def source ( self , i ) : <EOL> """<STR_LIT>""" <EOL> return self . info ( i ) [ <NUM_LIT:0> ] <EOL> def offset ( self , i ) : <EOL> """<STR_LIT>""" <EOL> return self . info ( i ) [ <NUM_LIT:1> ] <EOL> def disconnect ( self ) : <EOL> """<STR_LIT>""" <EOL> self . parent = None <EOL> def xitems ( self ) : <EOL> """<STR_LIT>""" <EOL> for ( value , ( source , offset ) ) in zip ( self . data , self . items ) : <EOL> yield ( source , offset , value ) <EOL> def pprint ( self ) : <EOL> """<STR_LIT>""" <EOL> for line in self . xitems ( ) : <EOL> print "<STR_LIT>" % line <EOL> class StringList ( ViewList ) : <EOL> """<STR_LIT>""" <EOL> def trim_left ( self , length , start = <NUM_LIT:0> , end = sys . maxint ) : <EOL> """<STR_LIT>""" <EOL> self . data [ start : end ] = [ line [ length : ] <EOL> for line in self . data [ start : end ] ] <EOL> def get_text_block ( self , start , flush_left = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> end = start <EOL> last = len ( self . data ) <EOL> while end < last : <EOL> line = self . data [ end ] <EOL> if not line . strip ( ) : <EOL> break <EOL> if flush_left and ( line [ <NUM_LIT:0> ] == '<STR_LIT:U+0020>' ) : <EOL> source , offset = self . info ( end ) <EOL> raise UnexpectedIndentationError ( self [ start : end ] , source , <EOL> offset + <NUM_LIT:1> ) <EOL> end += <NUM_LIT:1> <EOL> return self [ start : end ] <EOL> def get_indented ( self , start = <NUM_LIT:0> , until_blank = <NUM_LIT:0> , strip_indent = <NUM_LIT:1> , <EOL> block_indent = None , first_indent = None ) : <EOL> """<STR_LIT>""" <EOL> indent = block_indent <EOL> end = start <EOL> if block_indent is not None and first_indent is None : <EOL> first_indent = block_indent <EOL> if first_indent is not None : <EOL> end += <NUM_LIT:1> <EOL> last = len ( self . data ) <EOL> while end < last : <EOL> line = self . data [ end ] <EOL> if line and ( line [ <NUM_LIT:0> ] != '<STR_LIT:U+0020>' <EOL> or ( block_indent is not None <EOL> and line [ : block_indent ] . strip ( ) ) ) : <EOL> blank_finish = ( ( end > start ) <EOL> and not self . data [ end - <NUM_LIT:1> ] . strip ( ) ) <EOL> break <EOL> stripped = line . lstrip ( ) <EOL> if not stripped : <EOL> if until_blank : <EOL> blank_finish = <NUM_LIT:1> <EOL> break <EOL> elif block_indent is None : <EOL> line_indent = len ( line ) - len ( stripped ) <EOL> if indent is None : <EOL> indent = line_indent <EOL> else : <EOL> indent = min ( indent , line_indent ) <EOL> end += <NUM_LIT:1> <EOL> else : <EOL> blank_finish = <NUM_LIT:1> <EOL> block = self [ start : end ] <EOL> if first_indent is not None and block : <EOL> block . data [ <NUM_LIT:0> ] = block . data [ <NUM_LIT:0> ] [ first_indent : ] <EOL> if indent and strip_indent : <EOL> block . trim_left ( indent , start = ( first_indent is not None ) ) <EOL> return block , indent or <NUM_LIT:0> , blank_finish <EOL> def get_2D_block ( self , top , left , bottom , right , strip_indent = <NUM_LIT:1> ) : <EOL> block = self [ top : bottom ] <EOL> indent = right <EOL> for i in range ( len ( block . data ) ) : <EOL> block . data [ i ] = line = block . data [ i ] [ left : right ] . rstrip ( ) <EOL> if line : <EOL> indent = min ( indent , len ( line ) - len ( line . lstrip ( ) ) ) <EOL> if strip_indent and <NUM_LIT:0> < indent < right : <EOL> block . data = [ line [ indent : ] for line in block . data ] <EOL> return block <EOL> def pad_double_width ( self , pad_char ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( unicodedata , '<STR_LIT>' ) : <EOL> east_asian_width = unicodedata . east_asian_width <EOL> else : <EOL> return <EOL> for i in range ( len ( self . data ) ) : <EOL> line = self . data [ i ] <EOL> if isinstance ( line , unicode ) : <EOL> new = [ ] <EOL> for char in line : <EOL> new . append ( char ) <EOL> if east_asian_width ( char ) in '<STR_LIT>' : <EOL> new . append ( pad_char ) <EOL> self . data [ i ] = '<STR_LIT>' . join ( new ) <EOL> def replace ( self , old , new ) : <EOL> """<STR_LIT>""" <EOL> for i in range ( len ( self . data ) ) : <EOL> self . data [ i ] = self . data [ i ] . replace ( old , new ) <EOL> class StateMachineError ( Exception ) : pass <EOL> class UnknownStateError ( StateMachineError ) : pass <EOL> class DuplicateStateError ( StateMachineError ) : pass <EOL> class UnknownTransitionError ( StateMachineError ) : pass <EOL> class DuplicateTransitionError ( StateMachineError ) : pass <EOL> class TransitionPatternNotFound ( StateMachineError ) : pass <EOL> class TransitionMethodNotFound ( StateMachineError ) : pass <EOL> class UnexpectedIndentationError ( StateMachineError ) : pass <EOL> class TransitionCorrection ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class StateCorrection ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def string2lines ( astring , tab_width = <NUM_LIT:8> , convert_whitespace = <NUM_LIT:0> , <EOL> whitespace = re . compile ( '<STR_LIT>' ) ) : <EOL> """<STR_LIT>""" <EOL> if convert_whitespace : <EOL> astring = whitespace . sub ( '<STR_LIT:U+0020>' , astring ) <EOL> return [ s . expandtabs ( tab_width ) . rstrip ( ) for s in astring . splitlines ( ) ] <EOL> def _exception_data ( ) : <EOL> """<STR_LIT>""" <EOL> type , value , traceback = sys . exc_info ( ) <EOL> while traceback . tb_next : <EOL> traceback = traceback . tb_next <EOL> code = traceback . tb_frame . f_code <EOL> return ( type . __name__ , value , code . co_filename , traceback . tb_lineno , <EOL> code . co_name ) </s>
