File: factory.py

package info (click to toggle)
python-transitions 0.9.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,728 kB
  • sloc: python: 8,765; makefile: 10; sh: 7
file content (123 lines) | stat: -rw-r--r-- 4,563 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
"""
    transitions.extensions.factory
    ------------------------------

    This module contains the definitions of classes which combine the functionality of transitions'
    extension modules. These classes can be accessed by names as well as through a static convenience
    factory object.
"""

from functools import partial
import itertools

from ..core import Machine, Transition

from .nesting import HierarchicalMachine, NestedEvent, NestedTransition
from .locking import LockedMachine
from .diagrams import GraphMachine, NestedGraphTransition, HierarchicalGraphMachine

try:
    from transitions.extensions.asyncio import AsyncMachine, AsyncTransition
    from transitions.extensions.asyncio import HierarchicalAsyncMachine, NestedAsyncTransition
except (ImportError, SyntaxError):  # pragma: no cover
    class AsyncMachine(Machine):  # type: ignore
        """A mock of AsyncMachine for Python 3.6 and earlier."""

    class AsyncTransition(Transition):  # type: ignore
        """A mock of AsyncTransition for Python 3.6 and earlier."""

    class HierarchicalAsyncMachine(HierarchicalMachine):  # type: ignore
        """A mock of HierarchicalAsyncMachine for Python 3.6 and earlier."""

    class NestedAsyncTransition(NestedTransition):  # type: ignore
        """A mock of NestedAsyncTransition for Python 3.6 and earlier."""


class MachineFactory(object):
    """Convenience factory for machine class retrieval."""

    # get one of the predefined classes which fulfill the criteria
    @staticmethod
    def get_predefined(graph=False, nested=False, locked=False, asyncio=False):
        """A function to retrieve machine classes by required functionality.
        Args:
            graph (bool): Whether the returned class should contain graph support.
            nested: Whether the returned machine class should support nested states.
            locked: Whether the returned class should facilitate locks for threadsafety.

        Returns (class): A machine class with the specified features.
        """
        try:
            return _CLASS_MAP[(graph, nested, locked, asyncio)]
        except KeyError:
            raise ValueError("Feature combination not (yet) supported")  # from KeyError


class LockedHierarchicalMachine(LockedMachine, HierarchicalMachine):
    """
        A threadsafe hierarchical machine.
    """

    event_cls = NestedEvent

    def _get_qualified_state_name(self, state):
        return self.get_global_name(state.name)


class LockedGraphMachine(GraphMachine, LockedMachine):
    """
        A threadsafe machine with graph support.
    """

    @staticmethod
    def format_references(func):
        if isinstance(func, partial) and func.func.__name__.startswith('_locked_method'):
            return "%s(%s)" % (
                func.args[0].__name__,
                ", ".join(itertools.chain(
                    (str(_) for _ in func.args[1:]),
                    ("%s=%s" % (key, value)
                     for key, value in (func.keywords if func.keywords else {}).items()))))
        return GraphMachine.format_references(func)


class LockedHierarchicalGraphMachine(GraphMachine, LockedHierarchicalMachine):
    """
        A threadsafe hierarchical machine with graph support.
    """

    transition_cls = NestedGraphTransition
    event_cls = NestedEvent

    @staticmethod
    def format_references(func):
        return LockedGraphMachine.format_references(func)


class AsyncGraphMachine(GraphMachine, AsyncMachine):
    """A machine that supports asynchronous event/callback processing with Graphviz support."""

    transition_cls = AsyncTransition


class HierarchicalAsyncGraphMachine(GraphMachine, HierarchicalAsyncMachine):
    """A hierarchical machine that supports asynchronous event/callback processing with Graphviz support."""

    transition_cls = NestedAsyncTransition


# 4d tuple (graph, nested, locked, async)
_CLASS_MAP = {
    (False, False, False, False): Machine,
    (False, False, True, False): LockedMachine,
    (False, True, False, False): HierarchicalMachine,
    (False, True, True, False): LockedHierarchicalMachine,
    (True, False, False, False): GraphMachine,
    (True, False, True, False): LockedGraphMachine,
    (True, True, False, False): HierarchicalGraphMachine,
    (True, True, True, False): LockedHierarchicalGraphMachine,
    (False, False, False, True): AsyncMachine,
    (True, False, False, True): AsyncGraphMachine,
    (False, True, False, True): HierarchicalAsyncMachine,
    (True, True, False, True): HierarchicalAsyncGraphMachine
}