U n^@sdZdZddlZddlZddlZddlZddlmZddlm Z GdddZ Gdd d Z Gd d d e Z Gd d d e Z GdddZGdddee ZGdddee ZGdddZGdddeZGdddeZGdddeZGdddeZGdddeZGd d!d!eZGd"d#d#eZGd$d%d%eZGd&d'd'eZGd(d)d)eZGd*d+d+eZd,d-ed.fd/d0Zd1d2Z dS)3a A finite state machine specialized for regular-expression-based text filters, this module defines the following classes: - `StateMachine`, a state machine - `State`, a state superclass - `StateMachineWS`, a whitespace-sensitive version of `StateMachine` - `StateWS`, a state superclass for use with `StateMachineWS` - `SearchStateMachine`, uses `re.search()` instead of `re.match()` - `SearchStateMachineWS`, uses `re.search()` instead of `re.match()` - `ViewList`, extends standard Python lists. - `StringList`, string-specific ViewList. Exception classes: - `StateMachineError` - `UnknownStateError` - `DuplicateStateError` - `UnknownTransitionError` - `DuplicateTransitionError` - `TransitionPatternNotFound` - `TransitionMethodNotFound` - `UnexpectedIndentationError` - `TransitionCorrection`: Raised to switch to another transition. - `StateCorrection`: Raised to switch to another state & transition. Functions: - `string2lines()`: split a multi-line string into a list of one-line strings How To Use This Module ====================== (See the individual classes, methods, and attributes for details.) 1. Import it: ``import statemachine`` or ``from statemachine import ...``. You will also need to ``import re``. 2. Derive a subclass of `State` (or `StateWS`) for each state in your state machine:: class MyState(statemachine.State): Within the state's class definition: a) Include a pattern for each transition, in `State.patterns`:: patterns = {'atransition': r'pattern', ...} b) Include a list of initial transitions to be set up automatically, in `State.initial_transitions`:: initial_transitions = ['atransition', ...] c) Define a method for each transition, with the same name as the transition pattern:: def atransition(self, match, context, next_state): # do something result = [...] # a list return context, next_state, result # context, next_state may be altered Transition methods may raise an `EOFError` to cut processing short. d) You may wish to override the `State.bof()` and/or `State.eof()` implicit transition methods, which handle the beginning- and end-of-file. e) In order to handle nested processing, you may wish to override the attributes `State.nested_sm` and/or `State.nested_sm_kwargs`. If you are using `StateWS` as a base class, in order to handle nested indented blocks, you may wish to: - override the attributes `StateWS.indent_sm`, `StateWS.indent_sm_kwargs`, `StateWS.known_indent_sm`, and/or `StateWS.known_indent_sm_kwargs`; - override the `StateWS.blank()` method; and/or - override or extend the `StateWS.indent()`, `StateWS.known_indent()`, and/or `StateWS.firstknown_indent()` methods. 3. Create a state machine object:: sm = StateMachine(state_classes=[MyState, ...], initial_state='MyState') 4. Obtain the input text, which needs to be converted into a tab-free list of one-line strings. For example, to read text from a file called 'inputfile':: input_string = open('inputfile').read() input_lines = statemachine.string2lines(input_string) 5. Run the state machine on the input text and collect the results, a list:: results = sm.run(input_lines) 6. Remove any lingering circular references:: sm.unlink() ZrestructuredtextN)utils) ErrorOutputc@seZdZdZd6ddZddZd7d d Zd8d d Zd9ddZddZ ddZ ddZ d:ddZ ddZ ddZddZddZd;d d!Zd"d#Zd StateMachinea A finite state machine for text filters using regular expressions. The input is provided in the form of a list of one-line strings (no newlines). States are subclasses of the `State` class. Transitions consist of regular expression patterns and transition methods, and are defined in each state. The state machine is started with the `run()` method, which returns the results of processing in a list. FcCsLd|_d|_d|_d|_||_||_||_i|_||g|_ t |_ dS)a+ Initialize a `StateMachine` object; add state objects. Parameters: - `state_classes`: a list of `State` (sub)classes. - `initial_state`: a string, the class name of the initial state. - `debug`: a boolean; produce verbose output if true (nonzero). Nr) input_lines input_offsetline line_offsetdebug initial_state current_statestates add_states observersr_stderr)self state_classesr r ro/private/var/folders/sd/whlwsn6x1_qgglc0mjv25_695qk2gl/T/pip-install-4zq3fp6i/docutils/docutils/statemachine.py__init__s  zStateMachine.__init__cCs&t|jD] }|qd|_dSz9Remove circular references to objects no longer required.N)listr valuesunlinkrstaterrrrs zStateMachine.unlinkrNc Cst|t|tr||_nt||d|_||_d|_|p<|j|_|jrft d|jd |jf|j dd}g}| }z|jrt d|j d| |\}} || zzR||jr|j|j\} } t d| | |jf|j d||||\}} } WnPtk rJ|jr*t d |jj|j d||} || YWqHYn X|| Wntk r} zF|| jd f}|jrt d |jj|d f|j dWYqW5d} ~ XYn~tk r6} zZ|| jd } t| jd krd}n | jd f}|jr&t d | |d f|j dW5d} ~ XYnXd}| | }qWn|jrb|YnXg|_|S)a Run the state machine on `input_lines`. Return results (a list). Reset `self.line_offset` and `self.current_state`. Run the beginning-of-file transition. Input one line at a time and check for a matching transition. If a match is found, call the transition method and possibly change the state. Store the context returned by the transition method to be passed on to the next transition matched. Accumulate the results returned by the transition methods in a list. Run the end-of-file transition. Finally, return the accumulated results. Parameters: - `input_lines`: a list of strings without newlines, or `StringList`. - `input_offset`: the line offset of `input_lines` from the beginning of the file. - `context`: application-specific storage. - `input_source`: name or path of source of `input_lines`. - `initial_state`: name of initial state. sourcerz5 StateMachine.run: input_lines (line_offset=%s): | %sz | fileNz! StateMachine.run: bof transitionz4 StateMachine.run: line (source=%r, offset=%r): | %sz$ StateMachine.run: %s.eof transitionrzE StateMachine.run: TransitionCorrection to state "%s", transition %s.z@ StateMachine.run: StateCorrection to state "%s", transition %s.) runtime_init isinstance StringListrrr r r r printjoinr get_statebofextend next_lineinfor check_lineEOFError __class____name__eofTransitionCorrection previous_lineargsStateCorrectionlenerrorr)rrrcontextZ input_sourcer transitionsresultsrresultroffset next_state exceptionrrrruns          zStateMachine.runcCsj|r8|jr2||jkr2td|j||f|jd||_z|j|jWStk rdt|jYnXdS)z Return current state object; set it first if `next_state` given. Parameter `next_state`: a string, the name of the next state. Exception: `UnknownStateError` raised if `next_state` unknown. zJ StateMachine.get_state: Changing state from "%s" to "%s" (input line %s).rN)r r r$abs_line_numberrr KeyErrorUnknownStateError)rr;rrrr&szStateMachine.get_stater cCsZzJz |j|7_|j|j|_Wntk r@d|_tYnX|jWS|XdS)z9Load `self.line` with the `n`'th next line and return it.N)notify_observersr rr IndexErrorr,rnrrrr).s  zStateMachine.next_linecCs4z|j|jd WStk r.YdSXdS)z3Return 1 if the next line is blank or non-existant.r N)rr striprBrrrris_next_line_blank;szStateMachine.is_next_line_blankcCs|jt|jdkS)z0Return 1 if the input is at or past end-of-file.r )r r4rrFrrrat_eofBszStateMachine.at_eofcCs |jdkS)z8Return 1 if the input is at or before beginning-of-file.r)r rFrrrat_bofFszStateMachine.at_bofcCs<|j|8_|jdkr d|_n|j|j|_||jS)z=Load `self.line` with the `n`'th previous line and return it.rN)r rrrArCrrrr1Js  zStateMachine.previous_linecCsXzHz||j|_|j|j|_Wntk r>d|_tYnX|jWS|XdS)z?Jump to absolute line offset `line_offset`, load and return it.N)rArr rrrBr,rr rrr goto_lineTs   zStateMachine.goto_linecCs|j||jS)ziszStateMachine.abs_line_numbercCs|dkr|j}n||jd}z|j|\}}|d}WnNtk rn|||j\}}||dfYStk rd\}}YnX||fS)a\Return (source, line) tuple for current or given line number. Looks up the source and line number in the `self.input_lines` StringList instance to count for included source files. If the optional argument `lineno` is given, convert it from an absolute line number to the corresponding (source, line) pair. Nr NN)r rrr* TypeErrorget_source_and_linerB)rlinenor:srcZ srcoffsetZsrclinerrrrQms  z StateMachine.get_source_and_linecCs^|jj|jddd|t|d|jj|jddd|dd|j|jdt||dS)Nr zinternal padding after )rr:zinternal padding before r)rinsertr r4r#)rrrrrr insert_inputszStateMachine.insert_inputc Csnz(|j|j|}|t|d|WStk rh}z"|jd}|t|dW5d}~XYnXdS) Return a contiguous block of text. If `flush_left` is true, raise `UnexpectedIndentationError` if an indented line is encountered before the text block ends (with a blank line). r rN)rget_text_blockr r)r4UnexpectedIndentationErrorr2)r flush_leftblockerrrrrrYs  zStateMachine.get_text_blockc Cs|dkr|j}d}|jr2td|jj|f|jd|D]T}|j|\}}}||j} | r6|jrztd||jjf|jd|| ||Sq6|jrtd|jj|jd| ||S)a Examine one line of input for a transition match & execute its method. Parameters: - `context`: application-dependent storage. - `state`: a `State` object, the current state. - `transitions`: an optional ordered list of transition names to try, instead of ``state.transition_order``. Return the values returned by the transition method: - context: possibly modified from the parameter `context`; - next state name (`State` subclass name); - the result output of the transition, a list. When there is no match, ``state.no_match()`` is called and its return value is returned. Nz5 StateMachine.check_line: state="%s", transitions=%r.rz@ StateMachine.check_line: Matched transition "%s" in state "%s".z1 StateMachine.check_line: No match in state "%s".) transition_orderr r$r-r.rr7matchrno_match) rr6rr7Zstate_correctionnamepatternmethodr;r_rrrr+s<   zStateMachine.check_linecCs.|j}||jkrt||||j|j|<dS)z Initialize & add a `state_class` (`State` subclass) object. Exception: `DuplicateStateError` raised if `state_class` was already added. N)r.r DuplicateStateErrorr )r state_classZ statenamerrr add_states zStateMachine.add_statecCs|D]}||qdS)zE Add `state_classes` (a list of `State` subclasses). N)rf)rrrerrrrszStateMachine.add_statescCs t|jD] }|qdS)z+ Initialize `self.states`. N)rr rr!rrrrr!szStateMachine.runtime_initcCsXt\}}}}}td||f|jdtd||jdtd|||f|jddS)zReport error details.z%s: %srz input line %szmodule %s, line %s, function %sN)_exception_datar$rr>)rtypevaluemodulerfunctionrrrr5szStateMachine.errorcCs|j|dS)z The `observer` parameter is a function or bound method which takes two arguments, the source and offset of the current line. N)rappendrobserverrrrattach_observerszStateMachine.attach_observercCs|j|dSN)rremovermrrrdetach_observerszStateMachine.detach_observerc CsD|jD]8}z|j|j}Wntk r4d}YnX||qdS)NrO)rrr*r rB)rrnr*rrrrAs   zStateMachine.notify_observers)F)rNNN)N)r )r )N)F)N)r. __module__ __qualname____doc__rrr=r&r)rGrHrIr1rKrLrNr>rQrWrYr+rfrr!r5rorrrArrrrrus8 / a       , rc@seZdZdZdZdZdZdZdddZddZ dd Z d d Z d d Z ddZ ddZdddZddZddZddZddZddZdS) Stateav State superclass. Contains a list of transitions, and transition methods. Transition methods all have the same signature. They take 3 parameters: - An `re` match object. ``match.string`` contains the matched input line, ``match.start()`` gives the start index of the match, and ``match.end()`` gives the end index. - A context object, whose meaning is application-defined (initial value ``None``). It can be used to store any information required by the state machine, and the retured context is passed on to the next transition method unchanged. - The name of the next state, a string, taken from the transitions list; normally it is returned unchanged, but it may be altered by the transition method if necessary. Transition methods all return a 3-tuple: - A context object, as (potentially) modified by the transition method. - The next state name (a return value of ``None`` means no state change). - The processing result, a list, which is accumulated by the state machine. Transition methods may raise an `EOFError` to cut processing short. There are two implicit transitions, and corresponding transition methods are defined: `bof()` handles the beginning-of-file, and `eof()` handles the end-of-file. These methods have non-standard signatures and return values. `bof()` returns the initial context and results, and may be used to return a header string, or do any other processing needed. `eof()` should handle any remaining context and wrap things up; it returns the final processing result. Typical applications need only subclass `State` (or a subclass), set the `patterns` and `initial_transitions` class attributes, and provide corresponding transition methods. The default object initialization will take care of constructing the list of transitions. NFcCsVg|_i|_|||_||_|jdkr4|jj|_|jdkrR|jg|jjd|_dS)z Initialize a `State` object; make & add initial transitions. Parameters: - `statemachine`: the controlling `StateMachine` object. - `debug`: a boolean; produce verbose output if true. N)rr ) r^r7add_initial_transitions state_machiner nested_smr-nested_sm_kwargsr.rrxr rrrrVs     zState.__init__cCsdS)z{ Initialize this `State` before running the state machine; called from `self.state_machine.run()`. NrrFrrrr!zszState.runtime_initcCs d|_dSr)rxrFrrrrsz State.unlinkcCs&|jr"||j\}}|||dS)z>Make and add transitions listed in `self.initial_transitions`.N)initial_transitionsmake_transitionsadd_transitionsrnamesr7rrrrws zState.add_initial_transitionscCsJ|D]&}||jkrt|||krt|q||jdd<|j|dS)a" Add a list of transitions to the start of the transition list. Parameters: - `names`: a list of transition names. - `transitions`: a mapping of names to transition tuples. Exceptions: `DuplicateTransitionError`, `UnknownTransitionError`. Nr)r7DuplicateTransitionErrorUnknownTransitionErrorr^update)rrr7rarrrr~s   zState.add_transitionscCs0||jkrt||g|jdd<||j|<dS)z Add a transition to the start of the transition list. Parameter `transition`: a ready-made transition 3-tuple. Exception: `DuplicateTransitionError`. Nr)r7rr^)rraZ transitionrrradd_transitions zState.add_transitioncCs2z|j|=|j|Wnt|YnXdS)z^ Remove a transition by `name`. Exception: `UnknownTransitionError`. N)r7r^rqr)rrarrrremove_transitions zState.remove_transitioncCs|dkr|jj}z"|j|}t|ds0t|}Wn(tk rZtd|jj|fYnXzt||}Wn(t k rt d|jj|fYnX|||fS)a Make & return a transition tuple based on `name`. This is a convenience function to simplify transition creation. Parameters: - `name`: a string, the name of the transition pattern & method. This `State` object must have a method called '`name`', and a dictionary `self.patterns` containing a key '`name`'. - `next_state`: a string, the name of the next `State` object for this transition. A value of ``None`` (or absent) implies no state change (i.e., continue with the same state). Exceptions: `TransitionPatternNotFound`, `TransitionMethodNotFound`. Nr_z%s.patterns[%r]z%s.%s) r-r.patternshasattrrecompiler?TransitionPatternNotFoundgetattrAttributeErrorTransitionMethodNotFound)rrar;rbrcrrrmake_transitions"    zState.make_transitioncCshtd}g}i}|D]J}t||kr>||||<||q|j|||d<||dq||fS)z Return a list of transition names and a transition mapping. Parameter `name_list`: a list, where each entry is either a transition name string, or a 1- or 2-tuple (transition name, optional next state name). rTr)rhrrl)r name_listZ stringtyperr7Z namestaterrrr}s  zState.make_transitionscCs |dgfS)a' Called when there is no match from `StateMachine.check_line()`. Return the same values returned by transition methods: - context: unchanged; - next state name: ``None``; - empty result list. Override in subclasses to catch this event. Nr)rr6r7rrrr`s zState.no_matchcCs|gfS)z Handle beginning-of-file. Return unchanged `context`, empty result. Override in subclasses. Parameter `context`: application-defined storage. rrr6rrrr'sz State.bofcCsgS)z Handle end-of-file. Return empty result. Override in subclasses. Parameter `context`: application-defined storage. rrrrrr/sz State.eofcCs ||gfS)z A "do nothing" transition method. Return unchanged `context` & `next_state`, empty result. Useful for simple state changes (actionless transitions). rrr_r6r;rrrnopsz State.nop)F)N)r.rsrtrurr|ryrzrr!rrwr~rrrr}r`r'r/rrrrrrv s$' $  !  rvc@s.eZdZdZd ddZd ddZd dd Zd S)StateMachineWSaq `StateMachine` subclass specialized for whitespace recognition. There are three methods provided for extracting indented text blocks: - `get_indented()`: use when the indent is unknown. - `get_known_indented()`: use when the indent is known for all lines. - `get_first_known_indented()`: use when only the first line's indent is known. FTcCsd|}|j|j||\}}}|r6|t|d|rX|dsX||d7}q6||||fS)a Return a block of indented lines of text, and info. Extract an indented block where the indent is unknown for all lines. :Parameters: - `until_blank`: Stop collecting at the first blank line if true. - `strip_indent`: Strip common leading indent if true (default). :Return: - the indented block (a list of lines of text), - its indent, - its first line offset from BOF, and - whether or not it finished with a blank line. r rrNr get_indentedr r)r4rE trim_start)r until_blank strip_indentr:indentedindent blank_finishrrrr&s  zStateMachineWS.get_indentedcCsb|}|jj|j|||d\}}}|t|d|rX|dsX||d7}q6|||fS)a Return an indented block and info. Extract an indented block where the indent is known for all lines. Starting with the current line, extract the entire text block with at least `indent` indentation (which must be whitespace, except for the first line). :Parameters: - `indent`: The number of indent columns/characters. - `until_blank`: Stop collecting at the first blank line if true. - `strip_indent`: Strip `indent` characters of indentation if true (default). :Return: - the indented block, - its first line offset from BOF, and - whether or not it finished with a blank line. ) block_indentr rr)rrrrr:rrrrrget_known_indented@s  z!StateMachineWS.get_known_indentedcCsh|}|jj|j|||d\}}}|t|d|r\|r\|ds\||d7}q:||||fS)a Return an indented block and info. Extract an indented block where the indent is known for the first line and unknown for all other lines. :Parameters: - `indent`: The first line's indent (# of columns/characters). - `until_blank`: Stop collecting at the first blank line if true (1). - `strip_indent`: Strip `indent` characters of indentation if true (1, default). - `strip_top`: Strip blank lines from the beginning of the block. :Return: - the indented block, - its indent, - its first line offset from BOF, and - whether or not it finished with a blank line. ) first_indentr rr)rrrrZ strip_topr:rrrrrget_first_known_indented^s  z'StateMachineWS.get_first_known_indentedN)FT)FT)FTT)r.rsrtrurrrrrrrrs  rc@s`eZdZdZdZdZdZdZdddZdZ dddZ d d Z d d Z d dZ ddZddZdS)StateWSa State superclass specialized for whitespace (blank lines & indents). Use this class with `StateMachineWS`. The transitions 'blank' (for blank lines) and 'indent' (for indented text blocks) are added automatically, before any other transitions. The transition method `blank()` handles blank lines and `indent()` handles nested indented blocks. Indented blocks trigger a new state machine to be created by `indent()` and run. The class of the state machine to be created is in `indent_sm`, and the constructor keyword arguments are in the dictionary `indent_sm_kwargs`. The methods `known_indent()` and `firstknown_indent()` are provided for indented blocks where the indent (all lines' and first line's only, respectively) is known to the transition method, along with the attributes `known_indent_sm` and `known_indent_sm_kwargs`. Neither transition method is triggered automatically. Nz *$z +)blankrFcCsZt||||jdkr |j|_|jdkr2|j|_|jdkrD|j|_|jdkrV|j|_dS)z Initialize a `StateSM` object; extends `State.__init__()`. Check for indent state machine attributes, set defaults if not set. N)rvr indent_smryindent_sm_kwargsrzknown_indent_smknown_indent_sm_kwargsr{rrrrs    zStateWS.__init__cCsHt||jdkri|_|j|j||j\}}|||dS)z Add whitespace-specific transitions before those defined in subclass. Extends `State.add_initial_transitions()`. N)rvrwrr ws_patternsr}ws_initial_transitionsr~rrrrrws  zStateWS.add_initial_transitionscCs||||S)z9Handle blank lines. Does nothing. Override in subclasses.)rrrrrrsz StateWS.blankc CsB|j\}}}}|jfd|ji|j}|j||d} ||| fS)z Handle an indented text block. Extend or override in subclasses. Recursively run the registered state machine for indented blocks (`self.indent_sm`). r r)rxrrr rr=) rr_r6r;rrr rsmr8rrrrs  zStateWS.indentc CsF|j|\}}}|jfd|ji|j}|j||d}|||fS)a Handle a known-indent text block. Extend or override in subclasses. Recursively run the registered state machine for known-indent indented blocks (`self.known_indent_sm`). The indent is the length of the match, ``match.end()``. r r)rxrendrr rr= rr_r6r;rr rrr8rrr known_indents zStateWS.known_indentc CsF|j|\}}}|jfd|ji|j}|j||d}|||fS)a0 Handle an indented text block (first line's indent known). Extend or override in subclasses. Recursively run the registered state machine for known-indent indented blocks (`self.known_indent_sm`). The indent is the length of the match, ``match.end()``. r r)rxrrrr rr=rrrrfirst_known_indents zStateWS.first_known_indent)F)r.rsrtrurrrrrrrrwrrrrrrrrrs  rc@seZdZdZddZdS)_SearchOverridea Mix-in class to override `StateMachine` regular expression behavior. Changes regular expression matching, from the default `re.match()` (succeeds only if the pattern matches at the start of `self.line`) to `re.search()` (succeeds if the pattern matches anywhere in `self.line`). When subclassing a `StateMachine`, list this class **first** in the inheritance list of the class definition. cCs ||jS)z Return the result of a regular expression search. Overrides `StateMachine.match()`. Parameter `pattern`: `re` compiled regular expression. )searchr)rrbrrrr_sz_SearchOverride.matchN)r.rsrtrur_rrrrr s rc@seZdZdZdS)SearchStateMachinez@`StateMachine` which uses `re.search()` instead of `re.match()`.Nr.rsrtrurrrrr$src@seZdZdZdS)SearchStateMachineWSzB`StateMachineWS` which uses `re.search()` instead of `re.match()`.Nrrrrrr)src@sPeZdZdZdRddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*ZeZd+d,Zd-d.ZdSd0d1ZdTd2d3ZdUd5d6ZdVd8d9ZdWd:d;Zdd?Z!d@dAZ"dBdCZ#dDdEZ$dFdGZ%dHdIZ&dJdKZ'dLdMZ(dNdOZ)dPdQZ*dS)XViewLista> List with extended functionality: slices of ViewList objects are child lists, linked to their parents. Changes made to a child list also affect the parent list. A child list is effectively a "view" (in the SQL sense) of the parent list. Changes to parent lists, however, do *not* affect active child lists. If a parent list is changed, any active child lists should be recreated. The start and end of the slice can be trimmed using the `trim_start()` and `trim_end()` methods, without affecting the parent list. The link between child and parent lists can be broken by calling `disconnect()` on the child list. Also, ViewList objects keep track of the source & offset of each item. This information is accessible via the `source()`, `offset()`, and `info()` methods. Ncsg|_g|_||_||_t|trD|jdd|_|jdd|_n:|dk r~t||_|rb||_nfddtt|D|_t|jt|jkst ddS)Ncsg|] }|fqSrr).0irrr Zsz%ViewList.__init__.. data mismatch) dataitemsparent parent_offsetr"rrranger4AssertionError)rinitlistrrrrrrrrBs  zViewList.__init__cCs t|jSrp)strrrFrrr__str__]szViewList.__str__cCsd|jj|j|jfS)Nz%s(%s, items=%s))r-r.rrrFrrr__repr__`szViewList.__repr__cCs|j||kSrpr_ViewList__castrotherrrr__lt__dzViewList.__lt__cCs|j||kSrprrrrr__le__erzViewList.__le__cCs|j||kSrprrrrr__eq__frzViewList.__eq__cCs|j||kSrprrrrr__ne__grzViewList.__ne__cCs|j||kSrprrrrr__gt__hrzViewList.__gt__cCs|j||kSrprrrrr__ge__irzViewList.__ge__cCst|j||Srp)cmprrrrrr__cmp__jrzViewList.__cmp__cCst|tr|jS|SdSrp)r"rrrrrr__castls zViewList.__castcCs ||jkSrprritemrrr __contains__rrzViewList.__contains__cCs t|jSrp)r4rrFrrr__len__srzViewList.__len__cCs^t|trP|jdkstd|j|j|j|j|j|j|j||jpJddS|j|SdS)NNr cannot handle slice with strider)rrr) r"slicesteprr-rstartstoprrrrrr __getitem__ys zViewList.__getitem__cCst|tr|jdkstdt|ts.td|j|j|j|j<|j |j |j|j<t |jt |j ksrtd|j r||j |jpd|j |jpt ||j <n ||j|<|j r||j ||j <dS)Nrrz(assigning non-ViewList to ViewList slicerr) r"rrrrrPrrrrr4rr)rrrrrr __setitem__s   zViewList.__setitem__cCsz(|j|=|j|=|jr&|j||j=Wnttk r|jdksJtd|j|j|j=|j|j|j=|jr|j|jp|d|j|jpt ||j=YnXdS)Nrr) rrrrrPrrrrr4rrrr __delitem__szViewList.__delitem__cCs4t|tr(|j|j|j|j|jdStddS)Nrz!adding non-ViewList to a ViewListr"rr-rrrPrrrr__add__s   zViewList.__add__cCs4t|tr(|j|j|j|j|jdStddS)Nrz!adding ViewList to a non-ViewListrrrrr__radd__s   zViewList.__radd__cCs(t|tr|j|j7_ntd|S)Nz!argument to += must be a ViewList)r"rrrPrrrr__iadd__s zViewList.__iadd__cCs|j|j||j|dS)Nr)r-rrrCrrr__mul__szViewList.__mul__cCs |j|9_|j|9_|Srp)rrrCrrr__imul__szViewList.__imul__cCsRt|tstd|jr2|jt|j|j||j|j|j |j dS)Nz(extending a ViewList with a non-ViewList) r"rrPrrVr4rrr(rrrrrr(s  zViewList.extendrcCsX|dkr||n@|jr8|jt|j|j||||j||j||fdSrp)r(rrVr4rrrlr)rrrr:rrrrls  zViewList.appendcCs|dkrnt|tstd|j|j||<|j|j||<|jrt|j|t|j}|j||j|nV|j|||j|||f|jrt|j|t|j}|j||j|||dS)Nz+inserting non-ViewList with no source given) r"rrPrrrr4rVr)rrrrr:indexrrrrVs  zViewList.insertrcCsH|jr0t|j|t|j}|j||j|j||j|Srp)rr4rpoprr)rrrrrrrs  z ViewList.popr cCsf|t|jkr&td|t|jfn|dkr6td|jd|=|jd|=|jrb|j|7_dS)zW Remove items from the start of the list, without touching the parent. CSize of trim too large; can't trim %s items from a list of size %s.rTrim size must be >= 0.N)r4rrBrrrrCrrrrs   zViewList.trim_startcCsV|t|jkr&td|t|jfn|dkr6td|j| d=|j| d=dS)zU Remove items from the end of the list, without touching the parent. rrrN)r4rrBrrCrrrtrim_ends zViewList.trim_endcCs||}||=dSrp)r)rrrrrrrqs zViewList.removecCs |j|Srp)rcountrrrrrrzViewList.countcCs |j|Srp)rrrrrrrrzViewList.indexcCs|j|jd|_dSrp)rreverserrrFrrrr s  zViewList.reversecGsFtt|j|j}|j|dd|D|_dd|D|_d|_dS)NcSsg|] }|dqS)rrrentryrrrrsz!ViewList.sort..cSsg|] }|dqS)r rrrrrrs)rziprrsortr)rr2tmprrrrs  z ViewList.sortcCsPz |j|WStk rJ|t|jkrD|j|dddfYSYnXdS)z%Return source & offset for index `i`.r rN)rrBr4rrrrrr*s  z ViewList.infocCs||dS)zReturn source for index `i`.rr*rrrrr szViewList.sourcecCs||dS)zReturn offset for index `i`.r rrrrrr:$szViewList.offsetcCs d|_dS)z-Break link between this list and parent list.N)rrFrrr disconnect(szViewList.disconnectccs,t|j|jD]\}\}}|||fVqdS)z8Return iterator yielding (source, offset, value) tuples.N)rrr)rrirr:rrrxitems,szViewList.xitemscCs|D]}td|qdS)z=Print the list in `grep` format (`source:offset:value` lines)z%s:%d:%sN)rr$)rrrrrpprint1s zViewList.pprint)NNNNN)Nr)Nr)r)r )r )+r.rsrtrurrrrrrrrrrrrrrrrrrrr__rmul__rr(rlrVrrrrqrrrrr*rr:rrrrrrrr.sV       rc@sNeZdZdZdejfddZdddZdd d Zdd d Z ddZ ddZ d S)r#z*A `ViewList` with string-specific methods.rcs*fdd|j||D|j||<dS)z Trim `length` characters off the beginning of each item, in-place, from index `start` to `end`. No whitespace-checking is done on the trimmed text. Does not affect slice parent. csg|]}|dqSrprrrlengthrrrAsz(StringList.trim_left..Nr)rrrrrrr trim_left;s  zStringList.trim_leftFcCsv|}t|j}||krj|j|}|s*qj|r`|ddkr`||\}}t|||||d|d7}q|||S)rXr r )r4rrEr*rZ)rrr[rlastrrr:rrrrYDs   zStringList.get_text_blockTNcCsF|}|}|dk r|dkr|}|dk r,|d7}t|j}||kr|j|} | r| ddksp|dk r| d|r||ko|j|d } q| } | s|rd} qn0|dkrt| t| } |dkr| }n t|| }|d7}q6d} |||} |dk r| r| jd|d| jd<|r6|r6| j||dk d| |p@d| fS)a Extract and return a StringList of indented lines of text. Collect all lines with indentation, determine the minimum indentation, remove the minimum indentation from all indented lines (unless `strip_indent` is false), and return them. All lines up to but not including the first unindented line will be returned. :Parameters: - `start`: The index of the first line to examine. - `until_blank`: Stop collecting at the first blank line if true. - `strip_indent`: Strip common leading indent if true (default). - `block_indent`: The indent of the entire block, if known. - `first_indent`: The indent of the first line, if known. :Return: - a StringList of indented lines with mininum indent removed; - the amount of the indent; - a boolean: did the indented block finish with a blank line or EOF? Nr rr)r)r4rrElstripminr)rrrrrrrrrrrstrippedZ line_indentr\rrrrYsH      zStringList.get_indentedc s&|||}|tt|jD]}t|j|}z ||}Wn.tk rl|t|j|t|7}YnXz ||}Wn.tk r|t|j|t|7}YnX|j||||j|<} | rtt| t| q|r"dkr|kr"nnfdd|jD|_|S)Nrcsg|]}|dqSrprrrrrrsz+StringList.get_2D_block..) rr4rrZcolumn_indicesrBrstriprr) rtopleftZbottomrightrr\rcirrrr get_2D_blocks$      "zStringList.get_2D_blockcCsptj}tt|jD]V}|j|}t|trg}|D]$}||||dkr4||q4d||j|<qdS)z Pad all double-width characters in self by appending `pad_char` to each. For East Asian language support. ZWFrTN) unicodedataeast_asian_widthrr4rr"rrlr%)rZpad_charrrrnewcharrrrpad_double_widths     zStringList.pad_double_widthcCs0tt|jD]}|j||||j|<qdS)z6Replace all occurrences of substring `old` with `new`.N)rr4rreplace)roldr rrrrr szStringList.replace)F)rFTNN)T) r.rsrtrusysmaxsizerrYrrr r rrrrr#7s  < r#c@s eZdZdS)StateMachineErrorNr.rsrtrrrrrsrc@s eZdZdS)r@Nrrrrrr@sr@c@s eZdZdS)rdNrrrrrrdsrdc@s eZdZdS)rNrrrrrrsrc@s eZdZdS)rNrrrrrrsrc@s eZdZdS)rNrrrrrrsrc@s eZdZdS)rNrrrrrrsrc@s eZdZdS)rZNrrrrrrZsrZc@seZdZdZdS)r0z Raise from within a transition method to switch to another transition. Raise with one argument, the new transition name. Nrrrrrr0sr0c@seZdZdZdS)r3z Raise from within a transition method to switch to another state. Raise with one or two arguments: new state name, and an optional new transition name. Nrrrrrr3sr3Fz[ ]cs&|r|d|}fdd|DS)a Return a list of one-line strings with tabs expanded, no newlines, and trailing whitespace stripped. Each tab is expanded with between 1 and `tab_width` spaces, so that the next character's index becomes a multiple of `tab_width` (8 by default). Parameters: - `astring`: a multi-line string. - `tab_width`: the number of columns between tab stops. - `convert_whitespace`: convert form feeds and vertical tabs to spaces? rcsg|]}|qSr) expandtabsr)rs tab_widthrrrsz string2lines..)sub splitlines)ZastringrZconvert_whitespace whitespacerrr string2liness rcCs:t\}}}|jr|j}q|jj}|j||j|j|jfS)z Return exception information: - the exception's class name; - the exception object; - the name of the file containing the offending code; - the line number of the offending code; - the function name of the offending code. ) rexc_infotb_nexttb_framef_coder. co_filename tb_linenoco_name)rhri tracebackcoderrrrgs rg)!ruZ __docformat__rrtypesrZdocutilsrZdocutils.utils.error_reportingrrrvrrrrrrr# Exceptionrr@rdrrrrrZr0r3rrrgrrrrsNf  g