Metadata-Version: 2.4
Name: common-fsm
Version: 0.1.3
Summary: A simple and flexible Finite State Machine implementation in Python
Project-URL: Homepage, https://github.com/commonai/python-fsm
Project-URL: Bug Tracker, https://github.com/commonai/python-fsm/issues
Project-URL: Source Code, https://github.com/commonai/python-fsm
Author-email: Helge Modén <helgemod@gmail.com>
License: MIT License
        
        Copyright (c) 2025 Helge Modén
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: async,finite-state-machine,fsm,python,state-machine
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.7
Provides-Extra: dev
Requires-Dist: black; extra == 'dev'
Requires-Dist: flake8; extra == 'dev'
Requires-Dist: mypy; extra == 'dev'
Requires-Dist: pytest>=6.0; extra == 'dev'
Description-Content-Type: text/markdown

# Common-FSM

A simple and flexible Finite State Machine implementation in Python.

## Features

- Simple and intuitive API
- Type-safe with generics support
- Optional state timeouts
- Enter/exit hooks for states
- Verbose mode for debugging
- Fully tested

## Installation

```bash
pip install common-fsm
```

## Quick Start

```python
from enum import Enum
from common_fsm import FSM, State, Event, Transition

# Define your states and events
class States(Enum):
    OFF = "off"
    ON = "on"

class Events(Enum):
    POWER_ON = "power_on"
    POWER_OFF = "power_off"

# Create states with handlers
off_state = State()
off_state.add_handler(
    Events.POWER_ON,
    lambda event: Transition(States.ON)
)

on_state = State()
on_state.add_handler(
    Events.POWER_OFF,
    lambda event: Transition(States.OFF)
)

# Create FSM
fsm = FSM(
    States.OFF,  # Initial state
    {
        States.OFF: off_state,
        States.ON: on_state
    }
)

# Use the FSM
fsm.handle_event(Event(Events.POWER_ON))  # Transitions to ON
fsm.handle_event(Event(Events.POWER_OFF)) # Transitions to OFF
```

## Advanced Features

### State Timeouts

States can have automatic timeouts:

```python
# State with 5 second timeout
on_state = State(timeout=5.0)
on_state.add_handler(
    Events.TIMEOUT,
    lambda event: Transition(States.OFF)
)

fsm = FSM(
    States.OFF,
    {
        States.OFF: off_state,
        States.ON: on_state
    },
    timeout_event=Events.TIMEOUT
)
```

### State Hooks

Add hooks for state transitions:

```python
on_state.add_enter_hook(lambda s: print("Entering ON state"))
on_state.add_exit_hook(lambda s: print("Exiting ON state"))
```

## Examples

See the `examples/` directory for more detailed examples, including:
- Basic state machine
- Speaker with volume control
- Traffic light controller

## Development

Clone the repository:
```bash
git clone https://github.com/commonai/python-fsm.git
cd python-fsm
```

Install development dependencies:
```bash
pip install -e ".[dev]"
```

Run tests:
```bash
pytest tests/
```

## License

This project is licensed under the MIT License - see the LICENSE file for details.
