Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
field_data, fields = dict(), getattr(self, 'c_struct_fields', list())
if is_str_native(fields): fields = self.c_struct_fields = fields.split()
if field_data_list: field_data.update(zip(fields, field_data_list))
if field_data_dict: field_data.update(field_data_dict)
if struct is None: field_data, struct = dict(), field_data
assert not set(field_data.keys()).difference(fields)
if field_data: self._copy_struct_fields(field_data, fields=field_data.keys())
self._copy_struct_fields(struct, fields=set(fields).difference(field_data.keys()))
if struct:
if hasattr(struct, 'proplist'):
self.proplist, state = dict(), c.c_void_p()
while True:
k = c.pa.proplist_iterate(struct.proplist, c.byref(state))
if not k: break
self.proplist[c.force_str(k)] = c.force_str(c.pa.proplist_gets(struct.proplist, k))
if hasattr(struct, 'volume'):
self.volume = self._get_wrapper(PulseVolumeInfo)(struct.volume)
if hasattr(struct, 'n_ports'):
cls_port = self._get_wrapper(PulsePortInfo)
self.port_list = list(
cls_port(struct.ports[n].contents) for n in range(struct.n_ports) )
if hasattr(struct, 'active_port'):
cls_port = self._get_wrapper(PulsePortInfo)
self.port_active = (
None if not struct.active_port else cls_port(struct.active_port.contents) )
if hasattr(struct, 'channel_map'):
self.channel_count, self.channel_list = struct.channel_map.channels, list()
if self.channel_count > 0:
s = c.create_string_buffer(b'\0' * 512)
c.pa.channel_map_snprint(s, len(s), struct.channel_map)
self.channel_list.extend(map(c.force_str, s.value.strip().split(b',')))
def _copy_struct_fields(self, struct, fields=None, str_errors='strict'):
if not fields: fields = self.c_struct_fields
for k in fields:
setattr(self, k, c.force_str( getattr(struct, k)
if not is_dict(struct) else struct[k], str_errors ))
Returned file object has line-buffered output,
so there should be no need to use flush() after every command.
Be sure to read from the socket line-by-line until
"### EOF" or timeout for commands that have output (e.g. "dump\\n").
If default server socket is used (i.e. not specified),
server pid will be signaled to load module-cli between connection attempts.
Completely separate protocol from the regular API, as wrapped by libpulse.
PulseError is raised on any failure.'''
import socket, errno, signal, time
s, n = None, attempts if attempts > 0 else None
try:
pid_path, sock_af, sock_t = None, socket.AF_UNIX, socket.SOCK_STREAM
if not server: server, pid_path = map(c.pa.runtime_path, ['cli', 'pid'])
else:
if not is_list(server):
server = c.force_str(server)
if not server.startswith('/'): server = server, 4712 # default port
if is_list(server):
try:
addrinfo = socket.getaddrinfo(
server[0], server[1], 0, sock_t, socket.IPPROTO_TCP )
if not addrinfo: raise socket.gaierror('No addrinfo for socket: {}'.format(server))
except (socket.gaierror, socket.error) as err:
raise PulseError( 'Failed to resolve socket parameters'
' (address, family) via getaddrinfo: {!r} - {} {}'.format(server, type(err), err) )
sock_af, sock_t, _, _, server = addrinfo[0]
s = socket.socket(sock_af, sock_t)
s.settimeout(socket_timeout)
while True:
ts = c.mono_time()
try: s.connect(server)