1#
2# video_demo.py
3#
4# Simple statemachine demo, based on the state transitions given in videostate.pystate
5#
6
7import statemachine
8import videostate
9
10
11class Video(videostate.VideoStateMixin):
12    def __init__(self, title):
13        self.initialize_state(videostate.Stopped)
14        self.title = title
15
16
17# ==== main loop - a REPL ====
18
19v = Video("Die Hard.mp4")
20
21while True:
22    print(v.state)
23    cmd = input("Command ({})> ".format('/'.join(videostate.VideoState.transition_names))).lower().strip()
24    if not cmd:
25        continue
26
27    if cmd in ('?', 'h', 'help'):
28        print('enter a transition {!r}'.format(videostate.VideoState.transition_names))
29        print(' q - quit')
30        print(' ?, h, help - this message')
31        continue
32
33    # quitting out
34    if cmd.startswith('q'):
35        break
36
37    # get transition function for given command
38    state_transition_fn = getattr(v, cmd, None)
39
40    if state_transition_fn is None:
41        print('???')
42        continue
43
44    # invoke the input transition, handle invalid commands
45    try:
46        state_transition_fn()
47    except videostate.VideoState.InvalidTransitionException as e:
48        print(e)
49