Package docutils :: Module statemachine
[show private | hide private]
[frames | no frames]

Module docutils.statemachine

A finite state machine specialized for regular-expression-based text filters, this module defines the following classes:

Exception classes:

Functions:

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:

    1. Include a pattern for each transition, in State.patterns:

      patterns = {'atransition': r'pattern', ...}
      
    2. Include a list of initial transitions to be set up automatically, in State.initial_transitions:

      initial_transitions = ['atransition', ...]
      
    3. 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.

    4. You may wish to override the State.bof() and/or State.eof() implicit transition methods, which handle the beginning- and end-of-file.

    5. 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:

  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()
    

Classes
SearchStateMachine StateMachine which uses re.search() instead of re.match().
SearchStateMachineWS StateMachineWS which uses re.search() instead of re.match().
State State superclass.
StateMachine A finite state machine for text filters using regular expressions.
StateMachineWS StateMachine subclass specialized for whitespace recognition.
StateWS State superclass specialized for whitespace (blank lines & indents).
StringList A ViewList with string-specific methods.
ViewList List with extended functionality: slices of ViewList objects are child lists, linked to their parents.
_SearchOverride Mix-in class to override StateMachine regular expression behavior.

Exceptions
DuplicateStateError  
DuplicateTransitionError  
StateCorrection Raise from within a transition method to switch to another state.
StateMachineError  
TransitionCorrection Raise from within a transition method to switch to another transition.
TransitionMethodNotFound  
TransitionPatternNotFound  
UnexpectedIndentationError  
UnknownStateError  
UnknownTransitionError  

Function Summary
  string2lines(astring, tab_width, convert_whitespace, whitespace)
Return a list of one-line strings with tabs expanded and no newlines.
  _exception_data()
Return exception information:

Function Details

string2lines(astring, tab_width=8, convert_whitespace=0, whitespace=<_sre.SRE_Pattern object at 0x822b120>)

Return a list of one-line strings with tabs expanded and no newlines.

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:

_exception_data()

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.

Generated by Epydoc 2.0 on Tue Jul 22 05:30:31 2003 http://epydoc.sf.net