Skip to content

Commit

Permalink
Fix bugs in parser for cards
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidRisch committed Jan 17, 2021
1 parent b5ab021 commit 6f42c6a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion scripts/audio_switcher.py
Expand Up @@ -224,7 +224,7 @@ def get_port(self):
for card in cards:
debug_output += card.name + '\n'
for port in card.ports:
debug_output += ' {}\n'.format(port.product_name)
debug_output += ' {}\n'.format(port.product_name if port.product_name is not None else '-')
log.w('Failed to find any port on any card matching "{}". Name of the product at every port:\n{}'.format(
card_port_product_name_regex, debug_output
))
Expand Down
24 changes: 22 additions & 2 deletions scripts/pactl_interface/card.py
Expand Up @@ -25,7 +25,10 @@ def __init__(self, in_dict, card):
self.profiles = []
self.card = card

key, contents = list(in_dict.items())[0]
if isinstance(in_dict, str):
key, contents = in_dict, []
else:
key, contents = list(in_dict.items())[0]

match = re.match('^(.*): ', key)
self.name = match.group(1)
Expand All @@ -35,7 +38,7 @@ def __init__(self, in_dict, card):
for property_item in content['Properties:']:
if isinstance(property_item, dict):
property_item, _ = list(property_item.items())[0]
match = re.match('^device.product.name = \"(.*)$', property_item)
match = re.match('^device.product.name = \"(.*)\"$', property_item)
if match is not None:
self.product_name = match.group(1)
else:
Expand Down Expand Up @@ -115,13 +118,30 @@ def rescan_all_cards():
if return_code != 0:
log.e('\'{}\' () failed, stderr:\n{}'.format(" ".join(arguments), stderr))

@staticmethod
def cleanup_pactl_output(cards):
# Sometimes 'device.product.name' contains a newline which causes problems.
# This is detected by lines beginning with a space or quote.
cleaned_cards = ''
previous_line = ''
for line in cards.splitlines():
if len(line) > 0:
if line[0] == ' ' or line[0] == '"':
previous_line += line
else:
cleaned_cards += previous_line + '\n'
previous_line = line
return cleaned_cards

@classmethod
def get_all_cards(cls):
arguments = ['pactl', 'list', 'cards']
return_code, stdout, stderr = utlis.run(arguments)

cards = stdout

cards = cls.cleanup_pactl_output(cards)

class Node:
# https://stackoverflow.com/a/53346240/13623303
def __init__(self, indented_line):
Expand Down

0 comments on commit 6f42c6a

Please sign in to comment.