"""State helpers for companion plugins."""
from __future__ import annotations
[docs]
class CompanionState:
"""Minimal resettable key/value store for companion state.
Subclass this to get typed attributes::
class _MyState(CompanionState):
start_xy: tuple | None = None
end_xy: tuple | None = None
"""
#: Current step index inside a multi-step interaction.
def __init__(self) -> None:
self.step = 0
[docs]
def reset(self) -> None:
"""Reset ``step`` and public mutable attributes to class defaults."""
self.step = 0
for name, default in self.__class__.__dict__.items():
if name.startswith('_'):
continue
if name == 'step':
continue
if callable(default) or isinstance(default, (classmethod, staticmethod, property)):
continue
try:
setattr(self, name, default)
except AttributeError:
pass
[docs]
def advance(self) -> int:
"""Increment *step* and return the new value."""
self.step += 1
return self.step