Merge ~kissiel/checkbox-support:audio-settings-snappy into checkbox-support:master

Proposed by Maciej Kisielewski
Status: Rejected
Rejected by: Maciej Kisielewski
Proposed branch: ~kissiel/checkbox-support:audio-settings-snappy
Merge into: checkbox-support:master
Diff against target: 181 lines (+34/-20)
1 file modified
checkbox_support/scripts/audio_settings.py (+34/-20)
Reviewer Review Type Date Requested Status
Maciej Kisielewski Disapprove
Review via email: mp+311695@code.launchpad.net

Description of the change

This MR makes audio_settings script from checkbox_support more snappy-aware.

By calling pulseaudio commands using `pulseaudio.` prefix when $SNAP envvar is found

To post a comment you must log in.
Revision history for this message
Maciej Kisielewski (kissiel) wrote :

Let's use pactl everywhere and drop pacmd.

review: Needs Fixing
Revision history for this message
Maciej Kisielewski (kissiel) wrote :
review: Disapprove

Unmerged commits

b8736df... by Maciej Kisielewski

fix old pep8 issues in audio_settings.py

Signed-off-by: Maciej Kisielewski <email address hidden>

a049e9c... by Maciej Kisielewski

make audio_settings.py snappy-friendly

By replacing direct invocations to pactl and pacmd (PulseAudio control and
command), with pulseaudio.pactl if $SNAP is present in environ.

Signed-off-by: Maciej Kisielewski <email address hidden>

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/checkbox_support/scripts/audio_settings.py b/checkbox_support/scripts/audio_settings.py
2index a4000b5..19bcad0 100644
3--- a/checkbox_support/scripts/audio_settings.py
4+++ b/checkbox_support/scripts/audio_settings.py
5@@ -43,6 +43,17 @@ name_regex = re.compile("(?<=Name:).*")
6
7 entry_pattern = "Name: %s.*?(?=Properties)"
8
9+if 'SNAP' in os.environ:
10+ commands = {
11+ 'pactl': 'pulseaudio.pactl',
12+ 'pacmd': 'pulseaudio.pactl',
13+ }
14+else:
15+ commands = {
16+ 'pactl': 'pactl',
17+ 'pacmd': 'pacmd',
18+ }
19+
20
21 def unlocalized_env(reset={"LANG": "POSIX.UTF-8"}):
22 """
23@@ -69,7 +80,7 @@ def _guess_hdmi_profile(pactl_list):
24
25 # First parse all cards for HDMI / DisplayPort ports
26 for record in parse_pactl_output(pactl_list).record_list:
27- if not 'Card' in record.name:
28+ if 'Card' not in record.name:
29 continue
30 card = re.sub('.*#', '', record.name) # save the card id
31 ports = [
32@@ -94,7 +105,7 @@ def _guess_hdmi_profile(pactl_list):
33 def check_available_port():
34 match_found = False
35 for record in parse_pactl_output(pactl_list).record_list:
36- if not port_status_location in record.name:
37+ if port_status_location not in record.name:
38 continue
39 for sink_port in record.attribute_map['Ports'].value:
40 for card, ports in hdmi_ports.items():
41@@ -128,7 +139,8 @@ def set_profile_hdmi():
42 used as a fallback for setting HDMI / DisplayPort as the output device.
43 """
44 pactl_list = check_output(
45- ['pactl', 'list'], universal_newlines=True, env=unlocalized_env())
46+ [commands['pactl'], 'list'], universal_newlines=True,
47+ env=unlocalized_env())
48
49 card, profile = _guess_hdmi_profile(pactl_list)
50 if not profile:
51@@ -137,7 +149,7 @@ def set_profile_hdmi():
52
53 # Try and set device as default audio output
54 try:
55- check_call(["pactl", "set-card-profile", card, profile])
56+ check_call([commands["pactl"], "set-card-profile", card, profile])
57 except CalledProcessError as error:
58 logging.error("Failed setting audio output to:%s: %s" %
59 (profile, error))
60@@ -146,7 +158,8 @@ def set_profile_hdmi():
61 def get_current_profiles_settings(profiles_file):
62 """Captures and Writes current audio profiles settings"""
63 pactl_list = check_output(
64- ['pactl', 'list'], universal_newlines=True, env=unlocalized_env())
65+ [commands['pactl'], 'list'], universal_newlines=True,
66+ env=unlocalized_env())
67
68 config = configparser.ConfigParser()
69
70@@ -176,8 +189,8 @@ def restore_profiles_settings(profiles_file):
71
72 for card in config.sections():
73 try:
74- check_call(["pactl", "set-card-profile", config[card]['name'],
75- config[card]['profile']])
76+ check_call([commands["pactl"], "set-card-profile",
77+ config[card]['name'], config[card]['profile']])
78 except CalledProcessError as error:
79 logging.error("Failed setting card <%s> profile to <%s>: %s" %
80 (config[card]['name'],
81@@ -185,7 +198,7 @@ def restore_profiles_settings(profiles_file):
82
83
84 def move_sinks(name):
85- sink_inputs = check_output(["pacmd", "list-sink-inputs"],
86+ sink_inputs = check_output([commands["pacmd"], "list-sink-inputs"],
87 universal_newlines=True,
88 env=unlocalized_env())
89 input_indexes = index_regex.findall(sink_inputs)
90@@ -193,8 +206,8 @@ def move_sinks(name):
91 for input_index in input_indexes:
92 try:
93 with open(os.devnull, 'wb') as DEVNULL:
94- check_call(["pacmd", "move-sink-input", input_index, name],
95- stdout=DEVNULL)
96+ check_call([commands["pacmd"], "move-sink-input", input_index,
97+ name], stdout=DEVNULL)
98 except CalledProcessError:
99 logging.error("Failed to move input %d to sink %d" %
100 (input_index, name))
101@@ -210,7 +223,7 @@ def store_audio_settings(file):
102 sys.exit(1)
103
104 for type in TYPES:
105- pactl_status = check_output(["pactl", "info"],
106+ pactl_status = check_output([commands["pactl"], "info"],
107 universal_newlines=True,
108 env=unlocalized_env())
109 default_regex = re.compile(default_pattern % type.title())
110@@ -218,7 +231,7 @@ def store_audio_settings(file):
111
112 print("default_%s: %s" % (type, default), file=settings_file)
113
114- pactl_list = check_output(["pactl", "list", type + 's'],
115+ pactl_list = check_output([commands["pactl"], "list", type + 's'],
116 universal_newlines=True,
117 env=unlocalized_env())
118
119@@ -238,7 +251,7 @@ def store_audio_settings(file):
120
121 def set_audio_settings(device, mute, volume):
122 for type in TYPES:
123- pactl_entries = check_output(["pactl", "list", type + 's'],
124+ pactl_entries = check_output([commands["pactl"], "list", type + 's'],
125 universal_newlines=True,
126 env=unlocalized_env())
127
128@@ -252,8 +265,8 @@ def set_audio_settings(device, mute, volume):
129 logging.info("[ Fallback sink ]".center(80, '='))
130 logging.info("Name: {}".format(name))
131 with open(os.devnull, 'wb') as DEVNULL:
132- check_call(["pacmd", "set-default-%s" % type, name],
133- stdout=DEVNULL)
134+ check_call([commands["pacmd"], "set-default-%s" % type,
135+ name], stdout=DEVNULL)
136 except CalledProcessError:
137 logging.error("Failed to set default %s" % type)
138 sys.exit(1)
139@@ -262,14 +275,14 @@ def set_audio_settings(device, mute, volume):
140 move_sinks(name)
141
142 try:
143- check_call(["pactl",
144+ check_call([commands["pactl"],
145 "set-%s-mute" % type, name, str(int(mute))])
146 except:
147 logging.error("Failed to set mute for %s" % name)
148 sys.exit(1)
149
150 try:
151- check_call(["pactl", "set-%s-volume" % type,
152+ check_call([commands["pactl"], "set-%s-volume" % type,
153 name, str(volume) + '%'])
154 except:
155 logging.error("Failed to set volume for %s" % name)
156@@ -303,7 +316,7 @@ def restore_audio_settings(file):
157
158 try:
159 with open(os.devnull, 'wb') as DEVNULL:
160- check_call(["pacmd", "set-default-%s" % type, name],
161+ check_call([commands["pacmd"], "set-default-%s" % type, name],
162 stdout=DEVNULL)
163 except CalledProcessError:
164 logging.error("Failed to restore default %s" % name)
165@@ -313,13 +326,14 @@ def restore_audio_settings(file):
166 move_sinks(name)
167
168 try:
169- check_call(["pactl", "set-%s-mute" % type, name, muted])
170+ check_call([commands["pactl"], "set-%s-mute" % type, name, muted])
171 except:
172 logging.error("Failed to restore mute for %s" % name)
173 return 1
174
175 try:
176- check_call(["pactl", "set-%s-volume" % type, name, volume])
177+ check_call([commands["pactl"], "set-%s-volume" % type, name,
178+ volume])
179 except:
180 logging.error("Failed to restore volume for %s" % name)
181 return 1

Subscribers

People subscribed via source and target branches