-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerator_utils.py
911 lines (677 loc) · 26.4 KB
/
generator_utils.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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
# -*- coding: utf-8 -*-
# NOTE: THIS IS THE CLASS THAT WILL STORE ALL OF THE EXCEPTIONS ETC
"""Scarlett Generator Object audio utils"""
from __future__ import with_statement
from __future__ import division
import sys
import os
import random # NOQA
import re
import unicodedata
import threading
import subprocess # NOQA
from gettext import gettext as _
from urlparse import urlparse, urlunparse, urlsplit
urlparse, urlunparse, urlsplit
from urllib import pathname2url, url2pathname, quote_plus, unquote_plus
pathname2url, url2pathname, quote_plus, unquote_plus
from urllib2 import urlopen, build_opener
urlopen, build_opener
import errno
from os import environ as environ
import pprint
pp = pprint.PrettyPrinter(indent=4)
from IPython.core.debugger import Tracer # NOQA
from IPython.core import ultratb
sys.excepthook = ultratb.FormattedTB(mode='Verbose',
color_scheme='Linux',
call_pdb=True,
ostream=sys.__stdout__)
import gi
gi.require_version('Gst', '1.0')
from gi.repository import GLib, GObject, Gst # NOQA
# Gst.init(None)
import generator_log # NOQA
import contextlib
import time
import textwrap # NOQA
import logging
from functools import wraps
import traceback
logger = logging.getLogger('scarlettlogger')
_FSCODING = "utf-8"
text_type = unicode
string_types = (str, unicode)
integer_types = (int, long)
number_types = (int, long, float)
PY2 = sys.version_info[0] == 2
########################################################################################################################
# START - SOURCE: https://github.com/quodlibet/quodlibet/blob/master/quodlibet/quodlibet/util/__init__.py
########################################################################################################################
if PY2:
def gdecode(s): # NOQA
"""Returns unicode for the glib text type"""
assert isinstance(s, bytes)
return s.decode("utf-8")
else:
def gdecode(s): # NOQA
"""Returns unicode for the glib text type"""
assert isinstance(s, text_type)
return s
class InstanceTracker(object): # NOQA
"""A mixin for GObjects to return a list of all alive objects
of a given type. Note that it must be used with a GObject or
something with a connect method and destroy signal."""
__kinds = {}
def _register_instance(self, klass=None):
"""Register this object to be returned in the active instance list."""
if klass is None:
klass = type(self)
self.__kinds.setdefault(klass, []).append(self)
self.connect('destroy', self.__kinds[klass].remove)
@classmethod
def instances(cls):
return cls.__kinds.get(cls, [])
def escape(str):
"""Escape a string in a manner suitable for XML/Pango."""
return str.replace("&", "&").replace("<", "<").replace(">", ">")
def unescape(str):
"""Unescape a string in a manner suitable for XML/Pango."""
return str.replace("<", "<").replace(">", ">").replace("&", "&")
def parse_time(timestr, err=(ValueError, re.error)):
"""Parse a time string in hh:mm:ss, mm:ss, or ss format."""
if timestr[:1] == "-":
m = -1
timestr = timestr[1:]
else:
m = 1
try:
return m * reduce(lambda s, a: s * 60 + int(a),
re.split(r":|\.", timestr), 0)
except err:
return 0
def validate_query_date(datestr): # NOQA
"""Validates a user provided date that can be compared using date_key().
Returns True id the date is valid.
"""
parts = datestr.split("-")
if len(parts) > 3:
return False
if len(parts) > 2:
try:
v = int(parts[2])
except ValueError:
return False
else:
if not 1 <= v <= 31:
return False
if len(parts) > 1:
try:
v = int(parts[1])
except ValueError:
return False
else:
if not 1 <= v <= 12:
return False
try:
int(parts[0])
except ValueError:
return False
return True
def date_key(datestr): # NOQA
"""Parse a date format y-m-d and returns an undefined integer that
can only be used to compare dates.
In case the date string is invalid the returned value is undefined.
"""
# this basically does "2001-02-03" -> 20010203
default = [0, 1, 1]
parts = datestr.split("-")
parts += default[len(parts):]
value = 0
for d, p, m in zip(default, parts, (10000, 100, 1)):
try:
value += int(p) * m
except ValueError:
# so that "2003-01-" is equal to "2003-01" ..
value += d * m
return value
def parse_date(datestr): # NOQA
"""Parses yyyy-mm-dd date format and returns unix time.
Raises ValueError in case the input couldn't be parsed.
"""
import time
try:
frmt = ["%Y", "%Y-%m", "%Y-%m-%d"][datestr.count("-")]
except IndexError:
raise ValueError
return time.mktime(time.strptime(datestr, frmt))
def format_rating(value, blank=True):
"""Turn a number into a sequence of rating symbols."""
from quodlibet import config
prefs = config.RATINGS
steps = prefs.number
value = max(min(value, 1.0), 0)
ons = int(round(steps * value))
offs = (steps - ons) if blank else 0
return prefs.full_symbol * ons + prefs.blank_symbol * offs
def format_bitrate(value):
return _("%d kbps") % int(value)
def format_size(size):
"""Turn an integer size value into something human-readable."""
# TODO: Better i18n of this (eg use O/KO/MO/GO in French)
if size >= 1024 ** 3:
return "%.1f GB" % (float(size) / (1024 ** 3))
elif size >= 1024 ** 2 * 100:
return "%.0f MB" % (float(size) / (1024 ** 2))
elif size >= 1024 ** 2 * 10:
return "%.1f MB" % (float(size) / (1024 ** 2))
elif size >= 1024 ** 2:
return "%.2f MB" % (float(size) / (1024 ** 2))
elif size >= 1024 * 10:
return "%d KB" % int(size / 1024)
elif size >= 1024:
return "%.2f KB" % (float(size) / 1024)
else:
return "%d B" % size
def format_time(time):
"""Turn a time value in seconds into hh:mm:ss or mm:ss."""
if time < 0:
time = abs(time)
prefix = "-"
else:
prefix = ""
if time >= 3600: # 1 hour
# time, in hours:minutes:seconds
return "%s%d:%02d:%02d" % (prefix, time // 3600,
(time % 3600) // 60, time % 60)
else:
# time, in minutes:seconds
return "%s%d:%02d" % (prefix, time // 60, time % 60)
def format_time_display(time):
"""Like format_time, but will use RATIO instead of a colon to separate"""
return format_time(time).replace(":", u"\u2236")
def capitalize(str):
"""Capitalize a string, not affecting any character after the first."""
return str[:1].upper() + str[1:]
def _split_numeric_sortkey(s, limit=10,
reg=re.compile(r"[0-9][0-9]*\.?[0-9]*").search,
join=u" ".join):
"""Separate numeric values from the string and convert to float, so
it can be used for human sorting. Also removes all extra whitespace."""
result = reg(s)
if not result or not limit:
text = join(s.split())
return (text,) if text else tuple()
else:
start, end = result.span()
return (
join(s[:start].split()),
float(result.group()),
_split_numeric_sortkey(s[end:], limit - 1))
def human_sort_key(s, normalize=unicodedata.normalize):
if not s:
return ()
if not isinstance(s, text_type):
s = s.decode("utf-8")
s = normalize("NFD", s.lower())
return _split_numeric_sortkey(s)
def spawn(argv, stdout=False):
"""Asynchronously run a program. argv[0] is the executable name, which
must be fully qualified or in the path. If stdout is True, return
a file object corresponding to the child's standard output; otherwise,
return the child's process ID.
argv must be strictly str objects to avoid encoding confusion.
"""
from gi.repository import GLib # NOQA
types = map(type, argv)
if not (min(types) == max(types) == str):
raise TypeError("executables and arguments must be str objects")
logger.debug("Running %r" % " ".join(argv))
args = GLib.spawn_async(argv=argv, flags=GLib.SpawnFlags.SEARCH_PATH,
standard_output=stdout)
return os.fdopen(args[2]) if stdout else args[0]
def fver(tup):
return ".".join(map(str, tup))
def uri_is_valid(uri):
return bool(urlparse(uri)[0])
def make_case_insensitive(filename):
return "".join([f"[{c.lower()}{c.upper()}]" for c in filename])
class DeferredSignal(object):
"""A wrapper for connecting functions to signals.
Some signals may fire hundreds of times, but only require processing
once per group. This class pushes the call to the mainloop at idle
priority and prevents multiple calls from being inserted in the
mainloop at a time, greatly improving responsiveness in some places.
When the target function is finally called, the arguments passed
are the last arguments passed to DeferredSignal.
`priority` defaults to GLib.PRIORITY_DEFAULT
If `owner` is given, it will not call the target after the owner is
destroyed.
Example usage:
def func(widget, user_arg):
pass
widget.connect('signal', DeferredSignal(func, owner=widget), user_arg)
"""
def __init__(self, func, timeout=None, owner=None, priority=None):
"""timeout in milliseconds"""
self.func = func
self.dirty = False
self.args = None
if owner:
def destroy_cb(owner):
self.abort()
owner.connect("destroy", destroy_cb)
from gi.repository import GLib # NOQA
if priority is None:
priority = GLib.PRIORITY_DEFAULT
if timeout is None:
self.do_idle_add = lambda f: GLib.idle_add(f, priority=priority)
else:
self.do_idle_add = lambda f: GLib.timeout_add(
timeout, f, priority=priority)
@property
def __self__(self):
return self.func.__self__
@property
def __code__(self):
return self.func.__code__
@property
def __closure__(self):
return self.func.__closure__
def abort(self):
"""Abort any queued up calls.
Can still be reused afterwards.
"""
if self.dirty:
from gi.repository import GLib # NOQA
GLib.source_remove(self._id)
self.dirty = False
self.args = None
def __call__(self, *args):
self.args = args
if not self.dirty:
self.dirty = True
self._id = self.do_idle_add(self._wrap)
def _wrap(self):
self.func(*self.args)
self.dirty = False
self.args = None
return False
def connect_obj(this, detailed_signal, handler, that, *args, **kwargs):
"""A wrapper for connect() that has the same interface as connect_object().
Used as a temp solution to get rid of connect_object() calls which may
be changed to match the C version more closely in the future.
https://git.gnome.org/browse/pygobject/commit/?id=86fb12b3e9b75
While it's not clear if switching to weak references will break anything,
we mainly used this for adjusting the callback signature. So using
connect() behind the scenes will keep things working as they are now.
"""
def wrap(this, *args):
return handler(that, *args)
return this.connect(detailed_signal, wrap, *args, **kwargs)
def _connect_destroy(sender, func, detailed_signal, handler, *args, **kwargs):
"""Connect a bound method to a foreign object signal and disconnect
if the object the method is bound to emits destroy (Gtk.Widget subclass).
Also works if the handler is a nested function in a method and
references the method's bound object.
This solves the problem that the sender holds a strong reference
to the bound method and the bound to object doesn't get GCed.
"""
if hasattr(handler, "__self__"):
obj = handler.__self__
else:
# XXX: get the "self" var of the enclosing scope.
# Used for nested functions which ref the object but aren't methods.
# In case they don't ref "self" normal connect() should be used anyway.
index = handler.__code__.co_freevars.index("self")
obj = handler.__closure__[index].cell_contents
assert obj is not sender
handler_id = func(detailed_signal, handler, *args, **kwargs)
def disconnect_cb(*args):
sender.disconnect(handler_id)
obj.connect('destroy', disconnect_cb)
return handler_id
def connect_destroy(sender, *args, **kwargs):
return _connect_destroy(sender, sender.connect, *args, **kwargs)
def connect_after_destroy(sender, *args, **kwargs):
return _connect_destroy(sender, sender.connect_after, *args, **kwargs)
def gi_require_versions(name, versions):
"""Like gi.require_version, but will take a list of versions.
Returns the required version or raises ValueError.
"""
assert versions
import gi
error = None
for version in versions:
try:
gi.require_version(name, version)
except ValueError as e:
error = e
else:
return version
raise error
def is_main_thread():
"""If the calling thread is the main one"""
return threading.current_thread().name == "MainThread"
class MainRunnerError(Exception):
pass
class MainRunnerAbortedError(MainRunnerError):
pass
class MainRunnerTimeoutError(MainRunnerError):
pass
class MainRunner(object):
"""Schedule a function call in the main loop from a
worker thread and wait for the result.
Make sure to call abort() before the main loop gets destroyed, otherwise
the worker thread may block forever in call().
"""
def __init__(self):
self._source_id = None
self._call_id = None
self._lock = threading.Lock()
self._cond = threading.Condition(self._lock)
self._return = None
self._error = None
self._aborted = False
def _run(self, func, *args, **kwargs):
try:
self._return = func(*args, **kwargs)
except Exception as e:
self._error = MainRunnerError(e)
def _idle_run(self, call_id, call_event, func, *args, **kwargs):
call_event.set()
with self._lock:
# In case a timeout happened but this got still
# scheduled, this could be called after call() returns;
# Compare to the current call id and do nothing if it isn't ours
if call_id is not self._call_id:
return False
try:
self._run(func, *args, **kwargs)
finally:
self._source_id = None
self._cond.notify()
return False
def abort(self):
"""After this call returns no function will be executed anymore
and a currently blocking call will fail with MainRunnerAbortedError.
Can be called multiple times and can not fail.
call() will always fail after this was called.
"""
from gi.repository import GLib # NOQA
with self._lock:
if self._aborted:
return
if self._source_id is not None:
GLib.source_remove(self._source_id)
self._source_id = None
self._aborted = True
self._call_id = None
self._error = MainRunnerAbortedError("aborted")
self._cond.notify()
def call(self, func, *args, **kwargs):
"""Runs the function in the main loop and blocks until
it is finshed or abort() was called. In case this is called
from the main loop the function gets executed immediately.
The priority kwargs defines the event source priority and will
not be passed to func.
In case a timeout kwarg is given the call will raise
MainRunnerTimeoutError in case the function hasn't been scheduled
(doesn't mean returned) until that time. timeout is a float in seconds.
Can raise MainRunnerError in case the function raises an exception.
Raises MainRunnerAbortedError in case the runner was aborted.
Raises MainRunnerTimeoutError in case the timeout was reached.
"""
from gi.repository import GLib # NOQA
with self._lock:
if self._aborted:
raise self._error
self._error = None
# XXX: ideally this should be GLib.MainContext.default().is_owner()
# but that's not available in older pygobject
if is_main_thread():
kwargs.pop("priority", None)
self._run(func, *args, **kwargs)
else:
assert self._source_id is None
assert self._call_id is None
timeout = kwargs.pop("timeout", None)
call_event = threading.Event()
self._call_id = object()
self._source_id = GLib.idle_add(
self._idle_run, self._call_id, call_event,
func, *args, **kwargs)
# only wait for the result if we are sure it got scheduled
if call_event.wait(timeout):
self._cond.wait()
self._call_id = None
if self._source_id is not None:
GLib.source_remove(self._source_id)
self._source_id = None
raise MainRunnerTimeoutError("timeout: %r" % timeout)
if self._error is not None:
raise self._error
return self._return
def re_escape(string, BAD="/.^$*+-?{,\\[]|()<>#=!:"):
"""A re.escape which also works with unicode"""
needs_escape = lambda c: (c in BAD and "\\" + c) or c # NOQA
return type(string)().join(map(needs_escape, string))
# def reraise(tp, value, tb=None):
# """Reraise an exception with a new exception type and
# the original stack trace
# """
#
# if tb is None:
# tb = sys.exc_info()[2]
# py_reraise(tp, value, tb)
########################################################################################################################
# END - SOURCE: https://github.com/quodlibet/quodlibet/blob/master/quodlibet/quodlibet/util/__init__.py
########################################################################################################################
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)
# source: https://github.com/hpcgam/dicomimport/blob/1f265b1a5c9e631a536333633893ab525da87f16/doc-dcm/SAMPLEZ/nostaples/utils/scanning.py # NOQA
def abort_on_exception(func): # NOQA
"""
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_type, exc_value, exc_tb = exc_info = sys.exc_info()
filename, line_num, func_name, text = traceback.extract_tb(exc_tb)[-1]
logger.error('Exception Thrown from [%s] on line [%s] via function [%s]' % (filename, line_num, func_name))
logger.error('Exception type %s: %s' % (e.__class__.__name__, e.message))
# NOTE: ORIGINAL # thread_object.log.error('Exception type %s: %s' % (e.__class__.__name__, e.message))
thread_object.emit('aborted', exc_info)
return wrapper
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
@contextlib.contextmanager
def time_logger(name, level=logging.DEBUG):
"""Time logger context manager. Shows how long it takes to run a particular method"""
start = time.time()
yield
logger.log(level, '%s took %dms', name, (time.time() - start) * 1000)
def mkdir(dir_, *args): # NOQA
"""Make a directory, including all its parent directories. This does not
raise an exception if the directory already exists (and is a
directory)."""
try:
os.makedirs(dir_, *args)
except OSError as e:
if e.errno != errno.EEXIST or not os.path.isdir(dir_):
raise
def iscommand(s): # NOQA
"""True if an executable file `s` exists in the user's path, or is a
fully qualified and existing executable file."""
if s == "" or os.path.sep in s:
return os.path.isfile(s) and os.access(s, os.X_OK)
s = s.split()[0]
path = environ.get('PATH', '') or os.defpath
for p in path.split(os.path.pathsep):
p2 = os.path.join(p, s)
if os.path.isfile(p2) and os.access(p2, os.X_OK):
return True
return False
def is_fsnative(path):
"""Check if file system native"""
return isinstance(path, bytes)
def fsnative(path=u""):
"""File system native"""
assert isinstance(path, text_type)
return path.encode(_FSCODING, 'replace')
def glib2fsnative(path):
"""Convert glib to native filesystem format"""
assert isinstance(path, bytes)
return path
def fsnative2glib(path):
"""Convert file system to native glib format"""
assert isinstance(path, bytes)
return path
fsnative2bytes = fsnative2glib
bytes2fsnative = glib2fsnative
def listdir(path, hidden=False):
"""List files in a directory, sorted, fully-qualified.
If hidden is false, Unix-style hidden files are not returned.
"""
assert is_fsnative(path)
filt = None if hidden else (lambda base: not base.startswith("."))
join = "".join if path.endswith(os.sep) else os.sep.join
return [join([path, basename])
for basename in sorted(os.listdir(path))
if filt(basename)]
def mtime(filename):
"""Return the mtime of a file, or 0 if an error occurs."""
try:
return os.path.getmtime(filename)
except OSError:
return 0
def filesize(filename):
"""Return the size of a file, or 0 if an error occurs."""
try:
return os.path.getsize(filename)
except OSError:
return 0
def expanduser(filename): # NOQA
"""convience function to have expanduser return wide character paths
"""
return os.path.expanduser(filename)
def unexpand(filename, HOME=expanduser("~")):
"""Replace the user's home directory with ~/, if it appears at the
start of the path name."""
sub = "%USERPROFILE%" if os.name == "nt" else "~"
if filename == HOME:
return sub
elif filename.startswith(HOME + os.path.sep):
filename = filename.replace(HOME, sub, 1)
return filename
def get_home_dir():
"""Returns the root directory of the user, /home/user"""
return expanduser("~")
def calculate_duration(num_samples, sample_rate):
"""Determine duration of samples using GStreamer helper for precise
math."""
if _gst_available():
return Gst.util_uint64_scale(num_samples, Gst.SECOND, sample_rate)
def millisecond_to_clocktime(value):
"""Convert a millisecond time to internal GStreamer time."""
if _gst_available():
return value * Gst.MSECOND
def clocktime_to_millisecond(value):
"""Convert an internal GStreamer time to millisecond time."""
if _gst_available():
return value // Gst.MSECOND
class DecodeError(Exception):
"""The base exception class for all decoding errors raised by this
package.
"""
class NoBackendError(DecodeError):
"""The file could not be decoded by any backend. Either no backends
are available or each available backend failed to decode the file.
"""
class GStreamerError(DecodeError):
"""Something went terribly wrong with Gstreamer"""
pass
class UnknownTypeError(GStreamerError):
"""Raised when Gstreamer can't decode the given file type."""
def __init__(self, streaminfo):
super(UnknownTypeError, self).__init__(f"can't decode stream: {streaminfo}")
self.streaminfo = streaminfo
class FileReadError(GStreamerError):
"""Raised when the file can't be read at all."""
pass
class NoStreamError(GStreamerError):
"""Raised when the file was read successfully but no audio streams
were found.
"""
def __init__(self):
super(NoStreamError, self).__init__('no audio streams found')
class MetadataMissingError(GStreamerError):
"""Raised when GStreamer fails to report stream metadata (duration,
channels, or sample rate).
"""
pass
class IncompleteGStreamerError(GStreamerError):
"""Raised when necessary components of GStreamer (namely, the
principal plugin packages) are missing.
"""
def __init__(self):
super(IncompleteGStreamerError, self).__init__(
'missing GStreamer base plugins'
)
def _gst_available():
"""Determine whether Gstreamer and the Python GObject bindings are
installed.
"""
try:
import gi
except ImportError:
return False
try:
gi.require_version('Gst', '1.0')
except (ValueError, AttributeError):
return False
try:
from gi.repository import Gst # noqa
# from gi.repository import GLib, GObject, Gst # noqa
except ImportError:
return False
return True
def audio_open(path):
"""Open an audio file using a library that is available on this
system.
"""
# GStreamer.
if _gst_available():
from . import generator_player
with contextlib.suppress(DecodeError):
return generator_player.ScarlettPlayer(path)
# All backends failed!
raise NoBackendError()