• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pytransitions / transitions / 8938991339

03 May 2024 12:30PM UTC coverage: 98.432% (+0.2%) from 98.217%
8938991339

push

github

aleneum
use coverage only for mypy job and update setup.py tags

5149 of 5231 relevant lines covered (98.43%)

0.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.56
/transitions/extensions/factory.py
1
"""
1✔
2
    transitions.extensions.factory
3
    ------------------------------
4

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

10
from functools import partial
1✔
11
import itertools
1✔
12
from six import iteritems
1✔
13

14
from ..core import Machine, Transition
1✔
15

16
from .nesting import HierarchicalMachine, NestedEvent, NestedTransition
1✔
17
from .locking import LockedMachine
1✔
18
from .diagrams import GraphMachine, NestedGraphTransition, HierarchicalGraphMachine
1✔
19

20
try:
1✔
21
    from transitions.extensions.asyncio import AsyncMachine, AsyncTransition
1✔
22
    from transitions.extensions.asyncio import HierarchicalAsyncMachine, NestedAsyncTransition
1✔
23
except (ImportError, SyntaxError):  # pragma: no cover
24
    class AsyncMachine(Machine):  # type: ignore
25
        """A mock of AsyncMachine for Python 3.6 and earlier."""
26

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

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

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

36

37
class MachineFactory(object):
1✔
38
    """Convenience factory for machine class retrieval."""
1✔
39

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

49
        Returns (class): A machine class with the specified features.
50
        """
51
        try:
1✔
52
            return _CLASS_MAP[(graph, nested, locked, asyncio)]
1✔
53
        except KeyError:
×
54
            raise ValueError("Feature combination not (yet) supported")  # from KeyError
×
55

56

57
class LockedHierarchicalMachine(LockedMachine, HierarchicalMachine):
1✔
58
    """
1✔
59
        A threadsafe hierarchical machine.
60
    """
61

62
    event_cls = NestedEvent
1✔
63

64
    def _get_qualified_state_name(self, state):
1✔
65
        return self.get_global_name(state.name)
1✔
66

67

68
class LockedGraphMachine(GraphMachine, LockedMachine):
1✔
69
    """
1✔
70
        A threadsafe machine with graph support.
71
    """
72

73
    @staticmethod
1✔
74
    def format_references(func):
1✔
75
        if isinstance(func, partial) and func.func.__name__.startswith('_locked_method'):
1✔
76
            return "%s(%s)" % (
1✔
77
                func.args[0].__name__,
78
                ", ".join(itertools.chain(
79
                    (str(_) for _ in func.args[1:]),
80
                    ("%s=%s" % (key, value)
81
                     for key, value in iteritems(func.keywords if func.keywords else {})))))
82
        return GraphMachine.format_references(func)
1✔
83

84

85
class LockedHierarchicalGraphMachine(GraphMachine, LockedHierarchicalMachine):
1✔
86
    """
1✔
87
        A threadsafe hierarchical machine with graph support.
88
    """
89

90
    transition_cls = NestedGraphTransition
1✔
91
    event_cls = NestedEvent
1✔
92

93
    @staticmethod
1✔
94
    def format_references(func):
1✔
95
        return LockedGraphMachine.format_references(func)
1✔
96

97

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

101
    transition_cls = AsyncTransition
1✔
102

103

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

107
    transition_cls = NestedAsyncTransition
1✔
108

109

110
# 4d tuple (graph, nested, locked, async)
111
_CLASS_MAP = {
1✔
112
    (False, False, False, False): Machine,
113
    (False, False, True, False): LockedMachine,
114
    (False, True, False, False): HierarchicalMachine,
115
    (False, True, True, False): LockedHierarchicalMachine,
116
    (True, False, False, False): GraphMachine,
117
    (True, False, True, False): LockedGraphMachine,
118
    (True, True, False, False): HierarchicalGraphMachine,
119
    (True, True, True, False): LockedHierarchicalGraphMachine,
120
    (False, False, False, True): AsyncMachine,
121
    (True, False, False, True): AsyncGraphMachine,
122
    (False, True, False, True): HierarchicalAsyncMachine,
123
    (True, True, False, True): HierarchicalAsyncGraphMachine
124
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc