관리-도구
편집 파일: statemachine.cpython-311.pyc
� P�Dg|� � �v � d Z dZddlZddlZddlmZ ddlmZ G d� d� � Z G d� d � � Z G d � de� � Z G d� d e � � Z G d� d� � Z G d� dee� � Z G d� dee � � Z G d� d� � Z G d� de� � Z G d� de� � Z G d� de� � Z G d� de� � Z G d� de� � Z G d � d!e� � Z G d"� d#e� � Z G d$� d%e� � Z G d&� d'e� � Z G d(� d)e� � Z G d*� d+e� � Zd,d- ej d.� � fd/�Zd0� ZdS )1a� 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':: with open('inputfile', encoding='utf-8') as fp: input_string = fp.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() �restructuredtext� N)�east_asian_width)�utilsc � � e Zd ZdZdd�Zd� Z dd�Zd d�Zd!d �Zd� Z d� Z d � Zd!d�Zd� Z d� Zd� Zd� Zd d�Zd� Zdd�Zd d�Zd� Zd� Zd� Zd� Zd� Zd� Zd� ZdS )"�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. Fc � � d| _ d| _ d| _ d| _ || _ || _ || _ i | _ | � |� � g | _ 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_offset�line�line_offset�debug� initial_state� current_state�states� add_states� observers)�self� state_classesr r s �u/builddir/build/BUILD/imunify360-venv-2.4.0/opt/imunify360/venv/lib/python3.11/site-packages/docutils/statemachine.py�__init__zStateMachine.__init__� s� � � ��� #� ���J��� �!����M��� �$�*���C�*���C����4���� �&�&�&���� *� *� c �t � | j � � � D ]}|� � � �d| _ dS �z9Remove circular references to objects no longer required.N)r �values�unlink�r �states r r zStateMachine.unlink� s9 � ��[�'�'�)�)� � �E��L�L�N�N�N�N�����r r Nc � � | � � � t |t � � r|| _ nt ||�� � | _ || _ d| _ |p| j | _ | j r>t d| j �dd� | j � � ��t j �� � d}g }| � � � } | j rt dt j �� � |� |� � \ }} |� | � � | � � � | j rK| j � | j � � \ } }t d | �d|�d| j � �t j �� � | � |||� � \ }}} |� | � � nj# t( $ r] | j r(t d|j j z t j �� � |� |� � } |� | � � Y �nw xY w d}n�# t0 $ rf} | � � � | j d f}| j r2t d|j j � d|d � d�t j �� � Y d} ~ ��sd} ~ wt6 $ r�} | � � � | j d }t9 | j � � dk rd}n| j d f}| j r(t d|� d|d � d�t j �� � Y d} ~ nd} ~ ww xY w| � |� � }��n!# | j r| � � � � xY wg | _ |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. )�sourcer z, StateMachine.run: input_lines (line_offset=z): | z | ��fileNz! StateMachine.run: bof transitionTz StateMachine.run: line (source=z , offset=z$ StateMachine.run: %s.eof transitionr z2 StateMachine.run: TransitionCorrection to state "z", transition �.� z- StateMachine.run: StateCorrection to state ")�runtime_init� isinstance� StringListr r r r r r �print�join�sys�stderr� get_state�bof�extend� next_line�infor � check_line�EOFError� __class__�__name__�eof�TransitionCorrection� previous_line�args�StateCorrection�len�errorr )r r r �context�input_sourcer �transitions�resultsr �resultr �offset� next_state� exceptions r �runzStateMachine.run� s* � �. �������k�:�.�.� L�*�D���)�+�l�K�K�K�D��(������*�@�d�.@����:� #��E��%�%�%�v�{�{�4�3C�'D�'D�'D�F��z� #� #� #� #� ������� � ��4 ��z� M��:���L�L�L�L�#�i�i��0�0�O�G�V��N�N�6�"�"�"�+ 3�)'�/����(�(�(��:� E�-1�-=�-B�-B� $� 0�.2� .2�N�F�F�!� #3�-3�#3� #3�@F�#3� #3�'+�y�#3� #3�9<��E� E� E� E� 7;�o�o�#�U�K�79� 79�3���V� ���v�.�.�.�.�� $� � � ��:� O�!�"I�$)�O�$<�#=�CF�:�O� O� O� O�!&���7�!3�!3�����v�.�.�.��� ���� /�, #'�K�K��+ ,� � � ��&�&�(�(�(�#,�>�!�#4�"6�K��z� /�� >�(-��(@�>� >�,7��N�>� >� >� $'�:�/� /� /� /� �H�H�H�H�����&� /� /� /��&�&�(�(�(�!*���!2�J��9�>�*�*�a�/�/�&*���'0�~�a�'8�&:���z� /�� N�",�N� N�<G��N�N� N� N�#&�:�/� /� /� /���������� /���� ���z�2�2��W+ 3�$ ��4 ��z� �� � ������������sq �AL/ �BF+ �H �+A#H�H �L/ �H�H �L/ � L�#AJ�>L/ � L�A9L� L/ �L�L/ �/M c � � |rU| j rG|| j k r<t d| j �d|�d| � � � �d�t j �� � || _ | j | j S # t $ r t | j � � �w xY w)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. z. StateMachine.get_state: Changing state from "z" to "z" (input line z).r! ) r r r( �abs_line_numberr* r+ r �KeyError�UnknownStateError)r rB s r r, zStateMachine.get_state s� � � � ,��z� C�j�D�,>�>�>����+�+�+�Z�Z�Z��-�-�/�/�/�/�1� 8;�z�C� C� C� C� ",�D�� 8��;�t�1�2�2��� 8� 8� 8�#�D�$6�7�7�7� 8���s �A+ �+B r$ c � � | xj |z c_ | j | j | _ n# t $ r d| _ t �w xY w| j | � � � S # | � � � w xY w)z9Load `self.line` with the `n`'th next line and return it.N)r r r � IndexErrorr2 �notify_observers�r �ns r r/ zStateMachine.next_line& s� � � $� �� � �A�%� � � �,�T�-=�>�� � ��� � � � �� ��� ���� �9��!�!�#�#�#�#��D�!�!�#�#�#�#���s �'+ �A"