-
Notifications
You must be signed in to change notification settings - Fork 0
/
nircmd_win_driver.py
59 lines (50 loc) · 2.71 KB
/
nircmd_win_driver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import subprocess
class NircmdError(Exception):
pass
NIRCMD_WIN_MAXVOLUME = 65535
NIRCMD_WIN_MINVOLUME = 0
class NircmdWin(object):
"""
Driver for Nirsoft's Nircmd Windows Utility
http://www.nirsoft.net/utils/nircmd.html
"""
def __init__(self, station_config, operator_interface):
self._operator_interface = operator_interface
self._path_to_nircmd = station_config.NIRCMD_BIN
def _generate_nircmd_call(self, command, *args):
command_string = self._path_to_nircmd + ' ' + command
for arg in args:
command_string = command_string + ' ' + str(arg)
self._operator_interface.print_to_console("NIRCMD command: " + command_string + "\n")
try:
write_process = subprocess.Popen(command_string,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False,
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP)
(nircmd_stdout_data, nircmd_stderr_data) = write_process.communicate()
if write_process.wait() != 0:
self._operator_interface.print_to_console('ERROR: Unable to execute nircmd!\n')
raise NircmdError
else:
if nircmd_stderr_data is not None:
self._operator_interface.print_to_console("NIRCMD STDERR: '" +
str(nircmd_stderr_data) + "'\n")
if nircmd_stdout_data is not None:
self._operator_interface.print_to_console("NIRCMD STDOUT:\n" + nircmd_stdout_data + "\n")
except OSError as the_error:
self._operator_interface.print_to_console("ERROR: Unable to launch nircmd: \n" + str(the_error))
raise NircmdError
def set_output_volume(self, volume):
if volume <= NIRCMD_WIN_MAXVOLUME and volume >= NIRCMD_WIN_MINVOLUME:
self._generate_nircmd_call('setsysvolume', volume, 'speakers')
else:
self._operator_interface.print_to_console("volume must be between {0} and {1}".format(NIRCMD_WIN_MINVOLUME,
NIRCMD_WIN_MAXVOLUME))
raise NircmdError
def set_mic_volume(self, volume):
if volume <= NIRCMD_WIN_MAXVOLUME and volume >= NIRCMD_WIN_MINVOLUME:
self._generate_nircmd_call('setsysvolume', volume, 'microphone')
self._generate_nircmd_call('setsysvolume', volume, 'line-in')
else:
self._operator_interface.print_to_console("volume must be between {0} and {1}".format(NIRCMD_WIN_MINVOLUME,
NIRCMD_WIN_MAXVOLUME))
raise NircmdError