]>
git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - action.py
ef56b7cb9ebe223c261103382f2b80ea7e98aee2
1 from transitions
.extensions
import HierarchicalMachine
as Machine
2 from .helpers
import debug_print
, error_print
12 'children': ['running']
26 'after': 'poll_loaded'
32 'after': 'poll_loaded'
37 'dest': 'loaded_running',
38 'after': 'finish_action',
39 # if a child has no transitions, then it is bubbled to the parent,
40 # and we don't want that. Not useful in that machine precisely.
41 'conditions': ['is_loaded']
44 'trigger': 'finish_action',
45 'source': 'loaded_running',
50 def __init__(self
, action
, key
, **kwargs
):
51 Machine(model
=self
, states
=self
.STATES
,
52 transitions
=self
.TRANSITIONS
, initial
='initial',
53 ignore_invalid_triggers
=True, queued
=True)
57 self
.mapping
= key
.parent
58 self
.arguments
= kwargs
59 self
.sleep_event
= None
60 self
.waiting_music
= None
62 def is_loaded_or_failed(self
):
63 return self
.is_loaded(allow_substates
=True) or self
.is_failed()
65 def callback_music_loaded(self
, success
):
71 # Machine states / events
72 def on_enter_loading(self
):
73 if hasattr(actions
, self
.action
):
74 if 'music' in self
.arguments
:
75 self
.arguments
['music'].subscribe_loaded(
76 self
.callback_music_loaded
)
80 error_print("Unknown action {}".format(self
.action
))
83 def on_enter_loaded_running(self
, key_start_time
):
84 debug_print(self
.description())
85 if hasattr(actions
, self
.action
):
86 getattr(actions
, self
.action
).run(self
,
87 key_start_time
=key_start_time
, **self
.arguments
)
89 def poll_loaded(self
):
90 self
.key
.callback_action_ready(self
,
91 self
.is_loaded(allow_substates
=True))
93 # This one cannot be in the Machine state since it would be queued to run
94 # *after* the wait is ended...
96 if getattr(actions
, self
.action
, None) and\
97 hasattr(getattr(actions
, self
.action
), 'interrupt'):
98 return getattr(getattr(actions
, self
.action
), 'interrupt')(
99 self
, **self
.arguments
)
102 def music_list(self
, music
):
103 if music
is not None:
106 return self
.mapping
.open_files
.values()
108 def description(self
):
109 if hasattr(actions
, self
.action
):
110 return getattr(actions
, self
.action
)\
111 .description(self
, **self
.arguments
)
113 return "unknown action {}".format(self
.action
)