Skip to content

Commit 7402d1d

Browse files
committed
Merge #79
2 parents e043305 + 1f71b87 commit 7402d1d

File tree

1 file changed

+108
-12
lines changed

1 file changed

+108
-12
lines changed

src/jack.py

Lines changed: 108 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,60 @@ def _decode(cdata):
8282
del name
8383

8484

85+
class JackError(Exception):
86+
"""Exception for all kinds of JACK-related errors."""
87+
88+
89+
class JackErrorCode(JackError):
90+
91+
def __init__(self, message, code):
92+
"""Exception for JACK errors with an error code.
93+
94+
Subclass of `JackError`.
95+
96+
The following attributes are available:
97+
98+
Attributes
99+
----------
100+
message
101+
Error message.
102+
code
103+
The error code returned by the JACK library function which
104+
resulted in this exception being raised.
105+
106+
"""
107+
self.message = message
108+
self.code = code
109+
110+
def __str__(self):
111+
return '{} ({})'.format(self.message, self.code)
112+
113+
114+
class JackOpenError(JackError):
115+
116+
def __init__(self, name, status):
117+
"""Exception raised for errors while creating a JACK client.
118+
119+
Subclass of `JackError`.
120+
121+
The following attributes are available:
122+
123+
Attributes
124+
----------
125+
name
126+
Requested client name.
127+
status
128+
A :class:`Status` instance representing the status information
129+
received by the ``jack_client_open()`` JACK library call.
130+
131+
"""
132+
self.name = name
133+
self.status = status
134+
135+
def __str__(self):
136+
return 'Error initializing "{}": {}'.format(self.name, self.status)
137+
138+
85139
class Client(object):
86140
"""A client that can connect to the JACK audio server."""
87141

@@ -122,6 +176,11 @@ def __init__(self, name, use_exact_name=False, no_start_server=False,
122176
Pass a SessionID Token. This allows the sessionmanager to
123177
identify the client again.
124178
179+
Raises
180+
------
181+
JackOpenError
182+
If the session with the JACK server could not be opened.
183+
125184
"""
126185
status = _ffi.new('jack_status_t*')
127186
options = _lib.JackNullOption
@@ -140,8 +199,7 @@ def __init__(self, name, use_exact_name=False, no_start_server=False,
140199
*optargs)
141200
self._status = Status(status[0])
142201
if not self._ptr:
143-
raise JackError('Error initializing "{0}": {1}'.format(
144-
name, self.status))
202+
raise JackOpenError(name, self._status)
145203

146204
self._inports = Ports(self, _AUDIO, _lib.JackPortIsInput)
147205
self._outports = Ports(self, _AUDIO, _lib.JackPortIsOutput)
@@ -172,7 +230,14 @@ def name(self):
172230

173231
@property
174232
def uuid(self):
175-
"""The UUID of the JACK client (read-only)."""
233+
"""The UUID of the JACK client (read-only).
234+
235+
Raises
236+
------
237+
JackError
238+
If getting the UUID fails.
239+
240+
"""
176241
uuid = _ffi.gc(_lib.jack_client_get_uuid(self._ptr), _lib.jack_free)
177242
if not uuid:
178243
raise JackError('Unable to get UUID')
@@ -402,6 +467,12 @@ def connect(self, source, destination):
402467
--------
403468
OwnPort.connect, disconnect
404469
470+
Raises
471+
------
472+
JackError
473+
If there is already an existing connection between *source* and
474+
*destination* or the connection can not be established.
475+
405476
"""
406477
if isinstance(source, Port):
407478
source = source.name
@@ -410,8 +481,9 @@ def connect(self, source, destination):
410481
err = _lib.jack_connect(self._ptr, source.encode(),
411482
destination.encode())
412483
if err == _errno.EEXIST:
413-
raise JackError('Connection {0!r} -> {1!r} '
414-
'already exists'.format(source, destination))
484+
raise JackErrorCode('Connection {0!r} -> {1!r} '
485+
'already exists'.format(source, destination),
486+
err)
415487
_check(err,
416488
'Error connecting {0!r} -> {1!r}'.format(source, destination))
417489

@@ -1400,6 +1472,11 @@ def get_uuid_for_client_name(self, name):
14001472
The session manager needs this to reassociate a client name to
14011473
the session ID.
14021474
1475+
Raises
1476+
------
1477+
JackError
1478+
If no client with the given name exists.
1479+
14031480
"""
14041481
uuid = _ffi.gc(_lib.jack_get_uuid_for_client_name(
14051482
self._ptr, name.encode()), _lib.jack_free)
@@ -1413,6 +1490,11 @@ def get_client_name_by_uuid(self, uuid):
14131490
In order to snapshot the graph connections, the session manager
14141491
needs to map session IDs to client names.
14151492
1493+
Raises
1494+
------
1495+
JackError
1496+
If no client with the given UUID exists.
1497+
14161498
"""
14171499
name = _ffi.gc(_lib.jack_get_client_name_by_uuid(
14181500
self._ptr, uuid.encode()), _lib.jack_free)
@@ -1426,6 +1508,11 @@ def get_port_by_name(self, name):
14261508
Given a full port name, this returns a `Port`, `MidiPort`,
14271509
`OwnPort` or `OwnMidiPort` object.
14281510
1511+
Raises
1512+
------
1513+
JackError
1514+
If no port with the given name exists.
1515+
14291516
"""
14301517
port_ptr = _lib.jack_port_by_name(self._ptr, name.encode())
14311518
if not port_ptr:
@@ -1640,7 +1727,15 @@ def callback_decorator(python_callable):
16401727
return callback_decorator
16411728

16421729
def _register_port(self, name, porttype, is_terminal, is_physical, flags):
1643-
"""Create a new port."""
1730+
"""Create a new port.
1731+
1732+
Raises
1733+
------
1734+
JackError
1735+
If the port can not be registered, e.g. because the name is
1736+
non-unique or too long.
1737+
1738+
"""
16441739
if is_terminal:
16451740
flags |= _lib.JackPortIsTerminal
16461741
if is_physical:
@@ -2292,6 +2387,12 @@ def __init__(self, size):
22922387
reserved for internal use. Use `write_space` to
22932388
determine the actual size available for writing.
22942389
2390+
2391+
Raises
2392+
------
2393+
JackError
2394+
If the rightbufefr could not be allocated.
2395+
22952396
"""
22962397
ptr = _lib.jack_ringbuffer_create(size)
22972398
if not ptr:
@@ -2639,11 +2740,6 @@ def __repr__(self):
26392740
}[self._code]
26402741

26412742

2642-
class JackError(Exception):
2643-
"""Exception for all kinds of JACK-related errors."""
2644-
2645-
pass
2646-
26472743

26482744
class CallbackExit(Exception):
26492745
"""To be raised in a callback function to signal failure.
@@ -2915,4 +3011,4 @@ def callback_wrapper(msg):
29153011
def _check(error_code, msg):
29163012
"""Check error code and raise JackError if non-zero."""
29173013
if error_code:
2918-
raise JackError('{0} ({1})'.format(msg, error_code))
3014+
raise JackErrorCode(msg, error_code)

0 commit comments

Comments
 (0)