-
Notifications
You must be signed in to change notification settings - Fork 1
/
FooThreadDbus.py
551 lines (457 loc) · 18.6 KB
/
FooThreadDbus.py
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# Refactored by Malcolm Jones to work with GTK+3 PyGobject( aka PyGI ).
# Mar 2016.
# ScarlettTasker application showing how once can combine the python
# threading module with GObject signals to make a simple thread
# manager class which can be used to stop horrible blocking GUIs.
#
# (c) 2008, John Stowers <[email protected]>
#
# This program serves as an example, and can be freely used, copied, derived
# and redistributed by anyone. No warranty is implied or given.
import os
import sys
import time
_INSTANCE = None
SCARLETT_DEBUG = True
# Prevents player or command callbacks from running multiple times
player_run = False
command_run = False
if SCARLETT_DEBUG:
# Setting GST_DEBUG_DUMP_DOT_DIR environment variable enables us to have a
# dotfile generated
os.environ[
"GST_DEBUG_DUMP_DOT_DIR"] = "/home/pi/dev/bossjones-github/scarlett-dbus-poc/_debug"
os.putenv('GST_DEBUG_DUMP_DIR_DIR',
'/home/pi/dev/bossjones-github/scarlett-dbus-poc/_debug')
import argparse
import pprint
pp = pprint.PrettyPrinter(indent=4)
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Gst', '1.0')
from gi.repository import GObject
from gi.repository import Gst
from gi.repository import GLib
from gi.repository import Gio
from gi.repository import Gtk
import threading
GObject.threads_init()
Gst.init(None)
import time
import random
import StringIO
import re
import ConfigParser
from signal import signal, SIGWINCH, SIGKILL, SIGTERM
from IPython.core.debugger import Tracer
from IPython.core import ultratb
sys.excepthook = ultratb.FormattedTB(mode='Verbose',
color_scheme='Linux',
call_pdb=True,
ostream=sys.__stdout__)
from colorlog import ColoredFormatter
import logging
from gettext import gettext as _
import traceback
from functools import wraps
import Queue
from random import randint
from pydbus import SessionBus
import test_gdbus_speaker
import test_gdbus_player
###################################################################################################
# Audio Utils Start
###################################################################################################
def calculate_duration(num_samples, sample_rate):
"""Determine duration of samples using GStreamer helper for precise
math."""
return Gst.util_uint64_scale(num_samples, Gst.SECOND, sample_rate)
def create_buffer(data, timestamp=None, duration=None):
"""Create a new GStreamer buffer based on provided data.
Mainly intended to keep gst imports out of non-audio modules.
.. versionchanged:: 2.0
``capabilites`` argument was removed.
"""
if not data:
raise ValueError('Cannot create buffer without data')
buffer_ = Gst.Buffer.new_wrapped(data)
if timestamp is not None:
buffer_.pts = timestamp
if duration is not None:
buffer_.duration = duration
return buffer_
def millisecond_to_clocktime(value):
"""Convert a millisecond time to internal GStreamer time."""
return value * Gst.MSECOND
def clocktime_to_millisecond(value):
"""Convert an internal GStreamer time to millisecond time."""
return value // Gst.MSECOND
###################################################################################################
# Audio Utils End
###################################################################################################
def setup_logger():
"""Return a logger with a default ColoredFormatter."""
formatter = ColoredFormatter(
"(%(threadName)-9s) %(log_color)s%(levelname)-8s%(reset)s %(message_log_color)s%(message)s",
datefmt=None,
reset=True,
log_colors={
'DEBUG': 'cyan',
'INFO': 'green',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red',
},
secondary_log_colors={
'message': {
'ERROR': 'red',
'CRITICAL': 'red',
'DEBUG': 'yellow'
}
},
style='%'
)
logger = logging.getLogger(__name__)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
return logger
def trace(func):
"""Tracing wrapper to log when function enter/exit happens.
:param func: Function to wrap
:type func: callable
"""
@wraps(func)
def wrapper(*args, **kwargs):
logger.debug('Start {!r}'. format(func.__name__))
result = func(*args, **kwargs)
logger.debug('End {!r}'. format(func.__name__))
return result
return wrapper
# source: https://github.com/hpcgam/dicomimport/blob/1f265b1a5c9e631a536333633893ab525da87f16/doc-dcm/SAMPLEZ/nostaples/utils/scanning.py # NOQA
def abort_on_exception(func):
"""
This function decorator wraps the run() method of a thread
so that any exceptions in that thread will be logged and
cause the threads 'abort' signal to be emitted with the exception
as an argument. This way all exception handling can occur
on the main thread.
Note that the entire sys.exc_info() tuple is passed out, this
allows the current traceback to be used in the other thread.
"""
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception, e:
thread_object = args[0]
exc_info = sys.exc_info()
thread_object.log.error('Exception type %s: %s' % (e.__class__.__name__, e.message))
thread_object.emit('aborted', exc_info)
return wrapper
# Create a player
logger = setup_logger()
class _IdleObject(GObject.GObject):
"""
Override GObject.GObject to always emit signals in the main thread
by emmitting on an idle handler
"""
# @trace
def __init__(self):
GObject.GObject.__init__(self)
# @trace
def emit(self, *args):
GObject.idle_add(GObject.GObject.emit, self, *args)
class _FooThread(threading.Thread, _IdleObject):
"""
Cancellable thread which uses gobject signals to return information
to the GUI.
"""
__gsignals__ = {
"completed": (
GObject.SignalFlags.RUN_LAST, None, []),
"progress": (
GObject.SignalFlags.RUN_LAST, None, [
GObject.TYPE_FLOAT]) # percent complete
}
# @trace
def __init__(self, *args):
threading.Thread.__init__(self)
_IdleObject.__init__(self)
self.cancelled = False
self.data = args[0]
self.name = args[1]
# TODO: Add one more option to pass in object ScarlettPlayer or
# ScarlettSpeaker
self.setName(f"{self.name}")
self.event_do_exit = threading.Event()
# test_gdbus_player.ScarlettPlayer('pi-listening')
player = test_gdbus_player
def signal_handler_in_thread():
def function_calling_gtk(event, result):
result.append(player.ScarlettPlayer('pi-listening'))
event.set()
event = threading.Event()
result = []
GLib.idle_add(function_calling_gtk, event, result)
event.wait()
toggle_button_is_active = result[0]
print(toggle_button_is_active)
# toggle_button = Gtk.ToggleButton()
#
# def signal_handler_in_thread():
#
# def function_calling_gtk(event, result):
# result.append(toggle_button.get_active())
# event.set()
#
# event = threading.Event()
# result = []
# GLib.idle_add(function_calling_gtk, event, result)
# event.wait()
# toggle_button_is_active = result[0]
# print(toggle_button_is_active)
# @trace
def cancel(self):
"""
Threads in python are not cancellable, so we implement our own
cancellation logic
"""
self.cancelled = True
# @trace
def run(self):
print "Running %s" % str(self)
for i in range(self.data):
if self.cancelled:
break
time.sleep(0.1)
self.emit("progress", i / float(self.data) * 100)
self.emit("completed")
class FooThreadManager:
"""
Manages many FooThreads. This involves starting and stopping
said threads, and respecting a maximum num of concurrent threads limit
"""
# @trace
def __init__(self, maxConcurrentThreads):
self.maxConcurrentThreads = maxConcurrentThreads
# stores all threads, running or stopped
self.fooThreads = {}
# the pending thread args are used as an index for the stopped threads
self.pendingFooThreadArgs = []
# @trace
def _register_thread_completed(self, thread, *args):
"""
Decrements the count of concurrent threads and starts any
pending threads if there is space
"""
del(self.fooThreads[args])
running = len(self.fooThreads) - len(self.pendingFooThreadArgs)
print "%s completed. %s running, %s pending" % (
thread, running, len(self.pendingFooThreadArgs))
if running < self.maxConcurrentThreads:
try:
args = self.pendingFooThreadArgs.pop()
print "Starting pending %s" % self.fooThreads[args]
self.fooThreads[args].start()
except IndexError:
pass
# @trace
def make_thread(self, completedCb, progressCb, userData, *args):
"""
Makes a thread with args. The thread will be started when there is
a free slot
"""
running = len(self.fooThreads) - len(self.pendingFooThreadArgs)
if args not in self.fooThreads:
thread = _FooThread(*args)
# signals run in the order connected. Connect the user completed
# callback first incase they wish to do something
# before we delete the thread
thread.connect("completed", completedCb, userData)
thread.connect("completed", self._register_thread_completed, *args)
thread.connect("progress", progressCb, userData)
# This is why we use args, not kwargs, because args are hashable
self.fooThreads[args] = thread
if running < self.maxConcurrentThreads:
print "Starting %s" % thread
self.fooThreads[args].start()
else:
print "Queing %s" % thread
self.pendingFooThreadArgs.append(args)
# @trace
def stop_all_threads(self, block=False):
"""
Stops all threads. If block is True then actually wait for the thread
to finish (may block the UI)
"""
for thread in self.fooThreads.values():
thread.cancel()
if block and thread.isAlive():
thread.join()
class ScarlettTasker(_IdleObject):
# @trace
def __init__(self, *args):
_IdleObject.__init__(self)
self.bucket = bucket = Queue.Queue() # NOQA
self.loop = GLib.MainLoop()
self.hello = None
# with SessionBus() as bus:
bus = SessionBus()
ss = bus.get("org.scarlett", object_path='/org/scarlett/Listener') # NOQA
# # SttFailedSignal / player_cb
# ss_failed_signal = bus.con.signal_subscribe(None, # NOQA
# "org.scarlett.Listener",
# "SttFailedSignal",
# '/org/scarlett/Listener',
# None,
# 0,
# player_cb)
ss_failed_signal = ss.SttFailedSignal.connect(player_cb)
# # ListenerReadySignal / player_cb
# ss_rdy_signal = bus.con.signal_subscribe(None, # NOQA
# "org.scarlett.Listener",
# "ListenerReadySignal",
# '/org/scarlett/Listener',
# None,
# 0,
# player_cb)
ss_rdy_signal = ss.ListenerReadySignal.connect(player_cb)
# # KeywordRecognizedSignal / player_cb
# ss_kw_rec_signal = bus.con.signal_subscribe(None, # NOQA
# "org.scarlett.Listener",
# "KeywordRecognizedSignal",
# '/org/scarlett/Listener',
# None,
# 0,
# player_cb)
ss_kw_rec_signal = ss.KeywordRecognizedSignal.connect(player_cb)
# # CommandRecognizedSignal /command_cb
# ss_cmd_rec_signal = bus.con.signal_subscribe(None, # NOQA
# "org.scarlett.Listener",
# "CommandRecognizedSignal",
# '/org/scarlett/Listener',
# None,
# 0,
# command_cb)
ss_cmd_rec_signal = ss.CommandRecognizedSignal.connect(command_cb)
# # ListenerCancelSignal / player_cb
# # signal_subscribe (sender, interface_name, member, object_path, arg0, flags, callback, *user_data)
# ss_cancel_signal = bus.con.signal_subscribe(None, # NOQA
# "org.scarlett.Listener",
# "ListenerCancelSignal",
# '/org/scarlett/Listener',
# None,
# 0,
# player_cb)
ss_cancel_signal = ss.ListenerCancelSignal.connect(player_cb)
ss.emitConnectedToListener('ScarlettTasker')
# THE ACTUAL THREAD BIT
self.manager = FooThreadManager(3)
try:
print "ScarlettTasker Thread Started", self
self.loop.run()
except Exception:
ss_failed_signal.disconnect()
ss_rdy_signal.disconnect()
ss_kw_rec_signal.disconnect()
ss_cmd_rec_signal.disconnect()
ss_cancel_signal.disconnect()
self.bucket.put(sys.exc_info())
raise
# @trace
def quit(self, sender, event):
self.manager.stop_all_threads(block=True)
self.loop.quit()
# @trace
def stop_threads(self, *args):
# THE ACTUAL THREAD BIT
self.manager.stop_all_threads()
# @trace
def add_thread(self, sender):
# make a thread and start it
data = random.randint(20, 60)
name = f"Thread #{random.randint(0, 1000)}"
# rowref = self.pendingModel.insert(0, (name, 0))
rowref = 'rowref userData'
# THE ACTUAL THREAD BIT
# def make_thread(self, completedCb, progressCb, userData, *args):
self.manager.make_thread(
self.thread_finished,
self.thread_progress,
rowref, data, name)
# @trace
def thread_finished(self, thread, rowref):
pass
# log
# logger.info("thread: " + thread)
# logger.info("rowref: " + rowref)
# self.pendingModel.remove(rowref)
# self.completeModel.insert(0, (thread.name,))
# @trace
def thread_progress(self, thread, progress, rowref):
pass
# self.pendingModel.set_value(rowref, 1, int(progress))
def print_keyword_args(**kwargs):
# kwargs is a dict of the keyword args passed to the function
for key, value in kwargs.iteritems():
print "%s = %s" % (key, value)
# NOTE: enumerate req to iterate through tuple and find GVariant
# @trace
def player_cb(*args, **kwargs):
if SCARLETT_DEBUG:
logger.debug("player_cb PrettyPrinter: ")
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(args)
# MAR 13 2016
logger.debug("player_cb kwargs")
print_keyword_args(**kwargs)
for i, v in enumerate(args):
if SCARLETT_DEBUG:
logger.debug(f"Type v: {type(v)}")
logger.debug(f"Type i: {type(i)}")
if type(v) is gi.overrides.GLib.Variant:
if SCARLETT_DEBUG:
logger.debug(f"THIS SHOULD BE A Tuple now: {v}")
msg, scarlett_sound = v
logger.warning(f" msg: {msg}")
logger.warning(f" scarlett_sound: {scarlett_sound}")
if player_run := True:
test_gdbus_player.ScarlettPlayer(scarlett_sound)
player_run = False
# NOTE: Create something like test_gdbus_player.ScarlettPlayer('pi-listening')
# NOTE: test_gdbus_player.ScarlettPlayer
# NOTE: self.bucket.put()
# NOTE: ADD self.queue.put(v)
# NOTE: enumerate req to iterate through tuple and find GVariant
# @trace
def command_cb(*args, **kwargs):
if SCARLETT_DEBUG:
logger.debug("command_cb PrettyPrinter: ")
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(args)
# MAR 13 2016
logger.debug("command_cb kwargs")
print_keyword_args(**kwargs)
for i, v in enumerate(args):
if SCARLETT_DEBUG:
logger.debug(f"Type v: {type(v)}")
logger.debug(f"Type i: {type(i)}")
if type(v) is gi.overrides.GLib.Variant:
if SCARLETT_DEBUG:
logger.debug(f"THIS SHOULD BE A Tuple now: {v}")
msg, scarlett_sound, command = v
logger.warning(f" msg: {msg}")
logger.warning(f" scarlett_sound: {scarlett_sound}")
logger.warning(f" command: {command}")
if command_run := True:
test_gdbus_speaker.ScarlettSpeaker('Hello sir. How are you doing this afternoon? I am full lee function nall, andd red ee for your commands') # NOQA
command_run = False
# NOTE: Create something like test_gdbus_player.ScarlettPlayer('pi-listening')
# NOTE: test_gdbus_player.ScarlettPlayer
# NOTE: self.bucket.put()
# NOTE: ADD self.queue.put(v)
if __name__ == "__main__":
_INSTANCE = st = ScarlettTasker()