def check_available_port(): match_found = False for record in parse_pactl_output(pactl_list).record_list: if not port_status_location in record.name: continue for sink_port in record.attribute_map['Ports'].value: for card, ports in hdmi_ports.items(): for card_port in ports: if sink_port.label == card_port.label: match_found = True if sink_port.availability != 'not available': return {card: card_port} # If the availability cannot be determined then we keep the first # candidate if not match_found and hdmi_ports: card, ports = hdmi_ports.popitem() return {card: ports.pop()}
def get_sound_config(self): text = subprocess.check_output( ["pactl", "list", self.mode], # either 'sources' or 'sinks' env=self.unlocalized_env, universal_newlines=True) doc = parse_pactl_output(text) cfg = set() for record in doc.record_list: active_port = None port_availability = None # We go through the attribute list once to try to find # an active port for attr in record.attribute_list: if attr.name == "Active Port": active_port = attr.value # If there is one, we retrieve its availability flag if active_port: for attr in record.attribute_list: if attr.name == "Ports": for port in attr.value: if port.name == active_port: port_availability = port.availability cfg.add((record.name, active_port, port_availability)) return cfg
def _guess_hdmi_profile(pactl_list): """ Use the pactl parser to get the stereo profile of the available HDMI port :returns: (card, profile) """ hdmi_ports = {} available_port = {} port_status_location = 'Sink' # First parse all cards for HDMI / DisplayPort ports for record in parse_pactl_output(pactl_list).record_list: if not 'Card' in record.name: continue card = re.sub('.*#', '', record.name) # save the card id ports = [ p for p in record.attribute_map['Ports'].value if ('HDMI' in p.label) and (('DisplayPort' in p.label) or ('DP' in p.label))] if not ports: continue if [p for p in ports if p.availability]: port_status_location = 'Card' hdmi_ports[card] = ports if not hdmi_ports: return (None, None) logging.info("[ HDMI / DisplayPort ports ]".center(80, '=')) for card, ports in hdmi_ports.items(): for card_port in ports: logging.info("Card #{} Port: {}".format(card, card_port)) # Check the ports availability in the list of pulseaudio sinks # if the status is not already available in the cards section. def check_available_port(): match_found = False for record in parse_pactl_output(pactl_list).record_list: if not port_status_location in record.name: continue for sink_port in record.attribute_map['Ports'].value: for card, ports in hdmi_ports.items(): for card_port in ports: if sink_port.label == card_port.label: match_found = True if sink_port.availability != 'not available': return {card: card_port} # If the availability cannot be determined then we keep the first # candidate if not match_found and hdmi_ports: card, ports = hdmi_ports.popitem() return {card: ports.pop()} available_port = check_available_port() if available_port: card, port = available_port.popitem() # Keep the shortest string in the profile_list including 'stereo' # it will avoid testing 'surround' profiles profile = min([p for p in port.profile_list if ('stereo' in p) or ('Hdmi' in p)], key=len) logging.info("[ Selected profile ]".center(80, '=')) logging.info("Card #{} Profile: {}".format(card, profile)) return (card, profile) else: return (None, None)
def _guess_hdmi_profile(pactl_list): """ Use the pactl parser to get the stereo profile of the available HDMI port :returns: (card, profile) """ hdmi_ports = {} available_port = {} port_status_location = 'Sink' # First parse all cards for HDMI / DisplayPort ports for record in parse_pactl_output(pactl_list).record_list: if not 'Card' in record.name: continue card = re.sub('.*#', '', record.name) # save the card id ports = [ p for p in record.attribute_map['Ports'].value if 'HDMI / DisplayPort' in p.label] if not ports: continue if [p for p in ports if p.availability]: port_status_location = 'Card' hdmi_ports[card] = ports if not hdmi_ports: return (None, None) logging.info("[ HDMI / DisplayPort ports ]".center(80, '=')) for card, ports in hdmi_ports.items(): for card_port in ports: logging.info("Card #{} Port: {}".format(card, card_port)) # Check the ports availability in the list of pulseaudio sinks # if the status is not already available in the cards section. def check_available_port(): match_found = False for record in parse_pactl_output(pactl_list).record_list: if not port_status_location in record.name: continue for sink_port in record.attribute_map['Ports'].value: for card, ports in hdmi_ports.items(): for card_port in ports: if sink_port.label == card_port.label: match_found = True if sink_port.availability != 'not available': return {card: card_port} # If the availability cannot be determined then we keep the first # candidate if not match_found and hdmi_ports: card, ports = hdmi_ports.popitem() return {card: ports.pop()} available_port = check_available_port() if available_port: card, port = available_port.popitem() # Keep the shortest string in the profile_list including 'stereo' # it will avoid testing 'surround' profiles profile = min([p for p in port.profile_list if 'stereo' in p], key=len) logging.info("[ Selected profile ]".center(80, '=')) logging.info("Card #{} Profile: {}".format(card, profile)) return (card, profile) else: return (None, None)