3 E\@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-ejd.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|_|j|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/pf/wv4htv3x0qs2c2mp0dnn0kchsvlck3/T/pip-install-emcbgzcf/docutils/docutils/statemachine.py__init__s  zStateMachine.__init__cCs*xt|jjD] }|jqWd|_dS)z9Remove circular references to objects no longer required.N)listrvaluesunlink)rstaterrrrs zStateMachine.unlinkrNcCsn|jt|tr||_nt||d|_||_d |_|p<|j|_|jrft d|jdj |jf|j dd}g}|j }y|jrt d|j d|j |\}} |j| xyyR|j|jr|jj|j\} } t d| | |jf|j d|j|||\}} } WnJtk rH|jr.t d |jj|j d|j|} |j| PYn X|j| Wntk r} zB|j| jd f}|jrt d |jj|d f|j dwWYdd} ~ Xn~tk r.} z\|j| jd } t| jdkrd}n | jdf}|jrt d | |d f|j dWYdd} ~ XnXd}|j | }qWWn|jr\|jYnXg|_|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.r) 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.runc Csh|r8|jr2||jkr2td|j||jf|jd||_y |j|jStk rbt|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_numberrrKeyErrorUnknownStateError)rr7rrrr"s zStateMachine.get_statercCsVzFy |j|7_|j|j|_Wntk r@d|_tYnX|jS|jXdS)z9Load `self.line` with the `n`'th next line and return it.N)r rr IndexErrorr(notify_observers)rnrrrr%.s zStateMachine.next_linec Cs0y|j|jdj Stk r*dSXdS)z3Return 1 if the next line is blank or non-existant.rN)rr stripr=)rrrris_next_line_blank;szStateMachine.is_next_line_blankcCs|jt|jdkS)z0Return 1 if the input is at or past end-of-file.r)r r0r)rrrrat_eofBszStateMachine.at_eofcCs |jdkS)z8Return 1 if the input is at or before beginning-of-file.r)r )rrrrat_bofFszStateMachine.at_bofcCs<|j|8_|jdkr d|_n|j|j|_|j|jS)z=Load `self.line` with the `n`'th previous line and return it.rN)r r rr>)rr?rrrr-Js  zStateMachine.previous_linecCsTzDy||j|_|j|j|_Wntk r>d|_tYnX|jS|jXdS)z?Jump to absolute line offset `line_offset`, load and return it.N)rr rr r=r(r>)rr rrr goto_lineTs  zStateMachine.goto_linecCs|jj||jS)zs   zStateMachine.notify_observers)F)rNNN)N)r)r)N)F)N)r* __module__ __qualname____doc__rrr9r"r%rArBrCr-rDrErFr:rHrNrOr'r\rrr1rdrfr>rrrrrus4  / `       , 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|_|j||_||_|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 ) rTr3add_initial_transitions state_machiner nested_smr)nested_sm_kwargsr*)rrlr rrrrVs     zState.__init__cCsdS)z{ Initialize this `State` before running the state machine; called from `self.state_machine.run()`. Nr)rrrrrzszState.runtime_initcCs d|_dS)z9Remove circular references to objects no longer required.N)rl)rrrrrsz State.unlinkcCs&|jr"|j|j\}}|j||dS)z>Make and add transitions listed in `self.initial_transitions`.N)initial_transitionsmake_transitionsadd_transitions)rnamesr3rrrrks zState.add_initial_transitionscCsNx.|D]&}||jkrt|||krt|qW||jdd<|jj|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)r3DuplicateTransitionErrorUnknownTransitionErrorrTupdate)rrrr3rWrrrrqs   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)r3rsrT)rrWZ transitionrrradd_transitions zState.add_transitionc Cs2y|j|=|jj|Wnt|YnXdS)z^ Remove a transition by `name`. Exception: `UnknownTransitionError`. N)r3rTrert)rrWrrrremove_transitions zState.remove_transitioncCs|dkr|jj}y"|j|}t|ds0tj|}Wn(tk rZtd|jj|fYnXyt||}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`. NrUz%s.patterns[%r]z%s.%s) r)r*patternshasattrrecompiler;TransitionPatternNotFoundgetattrAttributeErrorTransitionMethodNotFound)rrWr7rXrYrrrmake_transitions  zState.make_transitioncCsltd}g}i}xR|D]J}t||kr@|j|||<|j|q|j|||d<|j|dqW||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). rKr)r^rrb)r name_listZ stringtyperrr3Z namestaterrrrps   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)rr2r3rrrrVs zState.no_matchcCs|gfS)z Handle beginning-of-file. Return unchanged `context`, empty result. Override in subclasses. Parameter `context`: application-defined storage. r)rr2rrrr#sz State.bofcCsgS)z Handle end-of-file. Return empty result. Override in subclasses. Parameter `context`: application-defined storage. r)rr2rrrr+sz State.eofcCs ||gfS)z A "do nothing" transition method. Return unchanged `context` & `next_state`, empty result. Useful for simple state changes (actionless transitions). r)rrUr2r7rrrnopsz State.nop)F)N)r*rgrhrirxrormrnrrrrkrqrvrwrrprVr#r+rrrrrrj s$' $  !  rjc@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. FTcCsj|j}|jj|j||\}}}|r6|jt|dx&|r\|dj r\|j|d7}q8W||||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. rr)rFr get_indentedr r%r0r@ trim_start)r until_blank strip_indentr6indentedindent blank_finishrrrr&s zStateMachineWS.get_indentedcCsh|j}|jj|j|||d\}}}|jt|dx&|r\|dj r\|j|d7}q8W|||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_indentrr)rFrrr r%r0r@r)rrrrr6rrrrrget_known_indented@s z!StateMachineWS.get_known_indentedcCsn|j}|jj|j|||d\}}}|jt|d|rbx&|r`|dj r`|j|d7}qZ!d?d@Z"dAdBZ#dCdDZ$dEdFZ%dGdHZ&dIdJZ'dKdLZ(dMdNZ)dOdPZ*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).0i)rrr Zsz%ViewList.__init__..z data mismatch) dataitemsparent parent_offsetrrrranger0AssertionError)rinitlistrrrrr)rrrBs  zViewList.__init__cCs t|jS)N)strr)rrrr__str__]szViewList.__str__cCsd|jj|j|jfS)Nz%s(%s, items=%s))r)r*rr)rrrr__repr__`szViewList.__repr__cCs|j|j|kS)N)r_ViewList__cast)rotherrrr__lt__dszViewList.__lt__cCs|j|j|kS)N)rr)rrrrr__le__eszViewList.__le__cCs|j|j|kS)N)rr)rrrrr__eq__fszViewList.__eq__cCs|j|j|kS)N)rr)rrrrr__ne__gszViewList.__ne__cCs|j|j|kS)N)rr)rrrrr__gt__hszViewList.__gt__cCs|j|j|kS)N)rr)rrrrr__ge__iszViewList.__ge__cCst|j|j|S)N)cmprr)rrrrr__cmp__jszViewList.__cmp__cCst|tr|jS|SdS)N)rrr)rrrrr__castls zViewList.__castcCs ||jkS)N)r)ritemrrr __contains__rszViewList.__contains__cCs t|jS)N)r0r)rrrr__len__sszViewList.__len__cCs^t|trP|jdkstd|j|j|j|j|j|j|j||jpJddS|j|SdS)Nrzcannot handle slice with strider)rrr)Nr) rslicesteprr)rstartstopr)rrrrr __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)Nrzcannot handle slice with stridez(assigning non-ViewList to ViewList slicez data mismatchr)Nr) rrrrrrGrrrrr0rr)rrrrrr __setitem__s  , zViewList.__setitem__c Csy(|j|=|j|=|jr&|j||j=Wnttk r|jdksJtd|j|j|j=|j|j|j=|jr|j|jp|d|j|jpt ||j=YnXdS)Nzcannot handle slice with strider) rrrrrGrrrrr0)rrrrr __delitem__szViewList.__delitem__cCs4t|tr(|j|j|j|j|jdStddS)N)rz!adding non-ViewList to a ViewList)rrr)rrrG)rrrrr__add__s zViewList.__add__cCs4t|tr(|j|j|j|j|jdStddS)N)rz!adding ViewList to a non-ViewList)rrr)rrrG)rrrrr__radd__s zViewList.__radd__cCs(t|tr|j|j7_ntd|S)Nz!argument to += must be a ViewList)rrrrG)rrrrr__iadd__s zViewList.__iadd__cCs|j|j||j|dS)N)r)r)rr)rr?rrr__mul__szViewList.__mul__cCs |j|9_|j|9_|S)N)rr)rr?rrr__imul__szViewList.__imul__cCsRt|tstd|jr2|jjt|j|j||jj|j|j j|j dS)Nz(extending a ViewList with a non-ViewList) rrrGrrMr0rrr$r)rrrrrr$s  zViewList.extendrcCsX|dkr|j|n@|jr8|jjt|j|j||||jj||jj||fdS)N)r$rrMr0rrrbr)rrrr6rrrrbs  zViewList.appendcCs|dkrnt|tstd|j|j||<|j|j||<|jrt|j|t|j}|jj||j|nV|jj|||jj|||f|jrt|j|t|j}|jj||j|||dS)Nz+inserting non-ViewList with no source given) rrrGrrrr0rMr)rrrrr6indexrrrrMs zViewList.insertrcCsH|jr0t|j|t|j}|jj||j|jj||jj|S)N)rr0rpoprr)rrrrrrrs  z ViewList.popcCsf|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. zCSize of trim too large; can't trim %s items from a list of size %s.rzTrim size must be >= 0.N)r0rr=rrr)rr?rrrrs  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. zCSize of trim too large; can't trim %s items from a list of size %s.rzTrim size must be >= 0.N)r0rr=r)rr?rrrtrim_endszViewList.trim_endcCs|j|}||=dS)N)r)rrrrrrres zViewList.removecCs |jj|S)N)rcount)rrrrrrszViewList.countcCs |jj|S)N)rr)rrrrrrszViewList.indexcCs|jj|jjd|_dS)N)rreverserr)rrrrr s  zViewList.reversecGsFtt|j|j}|j|dd|D|_dd|D|_d|_dS)NcSsg|] }|dqS)rr)rentryrrrrsz!ViewList.sort..cSsg|] }|dqS)rr)rrrrrrs)rziprrsortr)rr.tmprrrrs  z ViewList.sortc CsJy |j|Stk rD|t|jkr>|j|dddfSYnXdS)z%Return source & offset for index `i`.rrN)rr=r0r)rrrrrr&s  z ViewList.infocCs|j|dS)zReturn source for index `i`.r)r&)rrrrrr szViewList.sourcecCs|j|dS)zReturn offset for index `i`.r)r&)rrrrrr6$szViewList.offsetcCs d|_dS)z-Break link between this list and parent list.N)r)rrrr disconnect(szViewList.disconnectccs0x*t|j|jD]\}\}}|||fVqWdS)z8Return iterator yielding (source, offset, value) tuples.N)rrr)rr_rr6rrrxitems,szViewList.xitemscCs"x|jD]}td|q WdS)z=Print the list in `grep` format (`source:offset:value` lines)z%s:%d:%sN)rr )rr rrrpprint1szViewList.pprint)NNNNN)Nr)Nrr)r)r)r)+r*rgrhrirrrrrrrrrrrrrrrrrrrr__rmul__rr$rbrMrrrrerrrrr&rr6rrrrrrrr.sR       rc@sNeZdZdZdejfddZdddZdd d Zdd d Z ddZ ddZ d S)rz*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|]}|dqS)Nr)rr )lengthrrrAsz(StringList.trim_left..N)r)rrrrr)rr trim_left;s zStringList.trim_leftFcCsz|}t|j}x^||krl|j|}|js,P|rb|ddkrb|j|\}}t|||||d|d7}qW|||S)z 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 r)r0rr@r&rP)rrrQrlastr rr6rrrrODs     zStringList.get_text_blockTNcCsJ|}|}|dk r|dkr|}|dk r,|d7}t|j}x||kr|j|} | r| ddksr|dk r| d|jr||ko|j|dj } P| j} | s|rd} Pn0|dkrt| t| } |dkr| }n t|| }|d7}q8Wd} |||} |dk r| r| jd|d| jd<|r:|r:| j||dk d| |pDd| 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? Nrrr)r)r0rr@lstripminr)rrrrrrrrrr rstrippedZ line_indentrRrrrrYsB       zStringList.get_indentedc s*|||}|xtt|jD]}tj|j|}y ||}Wn.tk rn|t|j|t|7}YnXy ||}Wn.tk r|t|j|t|7}YnX|j|||j|j|<} | r tt| t| jq W|r&dko|knr&fdd|jD|_|S)Nrcsg|]}|dqS)Nr)rr )rrrrsz+StringList.get_2D_block..) rr0rrZcolumn_indicesr=rstriprr) rtopleftZbottomrightrrRrcir r)rr get_2D_blocks$      "zStringList.get_2D_blockcCsttdrtj}ndSxltt|jD]Z}|j|}t|tr&g}x,|D]$}|j|||dkrH|j|qHWdj ||j|<q&WdS)z Pad all double-width characters in self by appending `pad_char` to each. For East Asian language support. east_asian_widthNZWFrK) ry unicodedatarrr0rrrrbr!)rZpad_charrrr newcharrrrpad_double_widths      zStringList.pad_double_widthcCs4x.tt|jD]}|j|j|||j|<qWdS)z6Replace all occurrences of substring `old` with `new`.N)rr0rreplace)roldrrrrrrszStringList.replace)F)rFTNN)T) r*rgrhrisysmaxsizerrOrrrrrrrrr7s  ; rc@s eZdZdS)StateMachineErrorN)r*rgrhrrrrrsrc@s eZdZdS)r<N)r*rgrhrrrrr<sr<c@s eZdZdS)rZN)r*rgrhrrrrrZsrZc@s eZdZdS)rtN)r*rgrhrrrrrtsrtc@s eZdZdS)rsN)r*rgrhrrrrrssrsc@s eZdZdS)r|N)r*rgrhrrrrr|sr|c@s eZdZdS)rN)r*rgrhrrrrrsrc@s eZdZdS)rPN)r*rgrhrrrrrPsrPc@seZdZdZdS)r,z Raise from within a transition method to switch to another transition. Raise with one argument, the new transition name. N)r*rgrhrirrrrr,sr,c@seZdZdZdS)r/z 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. N)r*rgrhrirrrrr/sr/Fz[ ]cs&|r|jd|}fdd|jDS)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|]}|jjqSr) expandtabsr)rs) tab_widthrrrsz string2lines..)sub splitlines)ZastringrZconvert_whitespace whitespacer)rr string2liness rcCs>tj\}}}x|jr|j}qW|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)r^r_ tracebackcoderrrr]s  r])!riZ __docformat__rrztypesrZdocutilsrZdocutils.utils.error_reportingrrrjrrrrrrr Exceptionrr<rZrtrsr|rrPr,r/r{rr]rrrrisL  g