Skip to content

Commit b397147

Browse files
committed
Add Status class
1 parent e506001 commit b397147

2 files changed

Lines changed: 104 additions & 37 deletions

File tree

‎examples/thru_client.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121

2222
client = jack.Client("Through-Client")
2323

24-
# TODO: check for status: JackServerStarted and JackNameNotUnique
24+
if client.status.server_started:
25+
print("JACK server started")
26+
if client.status.name_not_unique:
27+
print("unique name {0!r} assigned".format(client.name))
2528

2629
event = threading.Event()
2730

‎jack.py‎

Lines changed: 100 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ def __init__(self, name, use_exact_name=False, no_start_server=False,
321321
----------------
322322
use_exact_name : bool
323323
Whether an error should be raised if `name` is not unique.
324+
See :attr:`Status.name_not_unique`.
324325
no_start_server : bool
325326
Do not automatically start the JACK server when it is not
326327
already running. This option is always selected if
@@ -350,10 +351,10 @@ def __init__(self, name, use_exact_name=False, no_start_server=False,
350351
args[1] |= _lib.JackSessionID
351352
args.append(_ffi.new("char[]", session_id.encode()))
352353
self._ptr = _lib.jack_client_open(*args)
354+
self.status = Status(status[0])
355+
"""JACK status. See :class:`Status`."""
353356
if not self._ptr:
354-
status = status[0]
355-
raise JackError(
356-
"{0} ({1})".format(decode_status(status), hex(status)))
357+
raise JackError(str(self.status))
357358

358359
self._inports = self.Ports(self, _lib.JackPortIsInput)
359360
self._outports = self.Ports(self, _lib.JackPortIsOutput)
@@ -674,10 +675,9 @@ def set_shutdown_callback(self, callback, userdata=None):
674675
User-supplied function that is called whenever the JACK
675676
daemon is shutdown. It must have this signature::
676677
677-
callback(status:int, reason:str, userdata) -> None
678+
callback(status:Status, reason:str, userdata) -> None
678679
679-
The argument `status` can be decoded with
680-
:func:`decode_status`.
680+
The argument `status` is of type :class:`jack.Status`.
681681
682682
Note that after server shutdown, `self`
683683
is *not* deallocated by libjack, the application is
@@ -694,7 +694,8 @@ def set_shutdown_callback(self, callback, userdata=None):
694694
"""
695695
@self._callback("JackInfoShutdownCallback")
696696
def callback_wrapper(code, reason, _):
697-
return callback(code, _ffi.string(reason).decode(), userdata)
697+
return callback(Status(code), _ffi.string(reason).decode(),
698+
userdata)
698699

699700
_lib.jack_on_info_shutdown(self._ptr, callback_wrapper, _ffi.NULL)
700701

@@ -1513,35 +1514,98 @@ def get_array(self):
15131514
return np.frombuffer(self.get_buffer(), dtype=np.float32)
15141515

15151516

1516-
def decode_status(status):
1517-
"""Return string corresponding to ``JackStatus`` error code."""
1518-
if status & _lib.JackInvalidOption:
1519-
msg = "The operation contained an invalid or unsupported option"
1520-
elif status & _lib.JackShmFailure:
1521-
msg = "Unable to access shared memory"
1522-
elif status & _lib.JackVersionError:
1523-
msg = "Client's protocol version does not match"
1524-
elif status & _lib.JackClientZombie:
1525-
msg = "Zombie!"
1526-
elif status & _lib.JackNoSuchClient:
1527-
msg = "Requested client does not exist"
1528-
elif status & _lib.JackLoadFailure:
1529-
msg = "Unable to load internal client"
1530-
elif status & _lib.JackInitFailure:
1531-
msg = "Unable to initialize client"
1532-
elif status & _lib.JackBackendError:
1533-
msg = "Backend error"
1534-
elif status & _lib.JackNameNotUnique & _lib.JackFailure:
1535-
msg = "The client name is not unique"
1536-
elif status & _lib.JackServerFailed:
1537-
msg = "Unable to connect to the JACK server"
1538-
elif status & _lib.JackServerError:
1539-
msg = "Communication error with the JACK server"
1540-
elif status & _lib.JackFailure:
1541-
msg = "Overall operation failed"
1542-
else:
1543-
msg = "Unknown error"
1544-
return msg
1517+
class Status(object):
1518+
1519+
"""Representation of the JACK status bits."""
1520+
1521+
def __init__(self, statuscode):
1522+
self._code = statuscode
1523+
1524+
def __repr__(self):
1525+
flags = ", ".join(name for name in dir(self)
1526+
if not name.startswith('_') and getattr(self, name))
1527+
if not flags:
1528+
flags = "no flags set"
1529+
return "<JACK status 0x{0:x}: {1}>".format(self._code, flags)
1530+
1531+
@property
1532+
def failure(self):
1533+
"""Overall operation failed."""
1534+
return bool(self._code & _lib.JackFailure)
1535+
1536+
@property
1537+
def invalid_option(self):
1538+
"""The operation contained an invalid or unsupported option."""
1539+
return bool(self._code & _lib.JackInvalidOption)
1540+
1541+
@property
1542+
def name_not_unique(self):
1543+
"""The desired client name was not unique.
1544+
1545+
With the `use_exact_name` option of :class:`Client`, this
1546+
situation is fatal.
1547+
Otherwise, the name is modified by appending a dash and a
1548+
two-digit number in the range "-01" to "-99".
1549+
:attr:`Client.name` will return the exact string that was used.
1550+
If the specified `name` plus these extra characters would be too
1551+
long, the open fails instead.
1552+
1553+
"""
1554+
return bool(self._code & _lib.JackNameNotUnique)
1555+
1556+
@property
1557+
def server_started(self):
1558+
"""The JACK server was started for this :class:`Client`.
1559+
1560+
Otherwise, it was running already.
1561+
1562+
"""
1563+
return bool(self._code & _lib.JackServerStarted)
1564+
1565+
@property
1566+
def server_failed(self):
1567+
"""Unable to connect to the JACK server."""
1568+
return bool(self._code & _lib.JackServerFailed)
1569+
1570+
@property
1571+
def server_error(self):
1572+
"""Communication error with the JACK server."""
1573+
return bool(self._code & _lib.JackServerError)
1574+
1575+
@property
1576+
def no_such_client(self):
1577+
"""Requested client does not exist."""
1578+
return bool(self._code & _lib.JackNoSuchClient)
1579+
1580+
@property
1581+
def load_failure(self):
1582+
"""Unable to load internal client."""
1583+
return bool(self._code & _lib.JackLoadFailure)
1584+
1585+
@property
1586+
def init_failure(self):
1587+
"""Unable to initialize client."""
1588+
return bool(self._code & _lib.JackInitFailure)
1589+
1590+
@property
1591+
def shm_failure(self):
1592+
"""Unable to access shared memory."""
1593+
return bool(self._code & _lib.JackShmFailure)
1594+
1595+
@property
1596+
def version_error(self):
1597+
"""Client's protocol version does not match."""
1598+
return bool(self._code & _lib.JackVersionError)
1599+
1600+
@property
1601+
def backend_error(self):
1602+
"""Backend error."""
1603+
return bool(self._code & _lib.JackBackendError)
1604+
1605+
@property
1606+
def client_zombie(self):
1607+
"""Client zombified failure."""
1608+
return bool(self._code & _lib.JackClientZombie)
15451609

15461610

15471611
class JackError(Exception):

0 commit comments

Comments
 (0)