Source code for replay_wizard.models.time_sequence

"""
Time sequence model
"""
import time

from pydantic import Field

from .action import Action
from .keyboard import KeyboardAction
from .mouse import (
    MouseAction,
    ScrollAction,
    ClickAction,
)


[docs] def get_action(subtype: str): """ Get Action class by subtype (classname) """ action_types = [ KeyboardAction, MouseAction, ScrollAction, ClickAction, TimeSequence, ] action_types_dict = { item.__name__: item for item in action_types } return action_types_dict[subtype]
[docs] class TimeSequence(Action): """ Sequence with time """ name: str actions: list = [] timestamp_list: list = [] subtype: str = 'TimeSequence' start_time: float = Field(default_factory=time.time) def __len__(self): return len(self.actions) def __iter__(self): return iter(self.actions) def __contains__(self, item): """ in method """ return item in self.actions def __getitem__(self, item): return self.actions[item]
[docs] @classmethod def combine(cls, name, *sequences): """ Combine many sequences to one """ result = cls(name=name) for sequence in sequences: result.add(sequence) return result
[docs] @staticmethod def get_current_timestamp(): """ Get current timestamp """ return time.time()
[docs] def add(self, new_action): """ Add new action """ self.actions.append(new_action) timestamp = self.get_current_timestamp() self.timestamp_list.append(timestamp)
[docs] def update(self, other): """ Update actions """ self.actions.extend(other.actions) self.timestamp_list.extend(other.timestamp_list)
[docs] @classmethod def model_validate(cls, *args, **kwargs): result = super(TimeSequence, cls).model_validate( *args, **kwargs ) for i, action in enumerate(result): subtype = action['subtype'] action_cls = get_action(subtype) # result.actions[i] = action_cls(**action) result.actions[i] = action_cls.model_validate(action) return result