Better error handling
This commit is contained in:
parent
1b8a1183c1
commit
e9d8a8768c
1 changed files with 43 additions and 23 deletions
|
@ -19,14 +19,20 @@ Colors:
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
|
|
||||||
|
class NoMicrophone(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class Error(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class Py3status:
|
class Py3status:
|
||||||
format_muted = '\uf131' # FontAwesome
|
format_muted = '\uf131' # FontAwesome
|
||||||
format_unmuted = '\uf130' # FontAwesome
|
format_unmuted = '\uf130' # FontAwesome
|
||||||
|
|
||||||
class Error(Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def microphone(self):
|
def microphone(self):
|
||||||
|
try:
|
||||||
isMute = self.__getMuteStatus__()
|
isMute = self.__getMuteStatus__()
|
||||||
color = self.py3.COLOR_MUTE if isMute else self.py3.COLOR_UNMUTE
|
color = self.py3.COLOR_MUTE if isMute else self.py3.COLOR_UNMUTE
|
||||||
text = self.format_muted if isMute else self.format_unmuted
|
text = self.format_muted if isMute else self.format_unmuted
|
||||||
|
@ -34,30 +40,44 @@ class Py3status:
|
||||||
'full_text': text,
|
'full_text': text,
|
||||||
'color': color,
|
'color': color,
|
||||||
}
|
}
|
||||||
|
except NoMicrophone:
|
||||||
|
return {
|
||||||
|
'full_text': '',
|
||||||
|
}
|
||||||
|
except Error:
|
||||||
|
return {
|
||||||
|
'full_text': '...',
|
||||||
|
}
|
||||||
|
|
||||||
def __getPaList__(self):
|
def __getPaList__(self):
|
||||||
try:
|
try:
|
||||||
paOutput = subprocess.check_output(["pacmd", "list-sources"])
|
paOutput = subprocess.check_output(["pacmd", "list-sources"])
|
||||||
except CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
raise Error()
|
raise Error()
|
||||||
|
|
||||||
return paOutput.decode()
|
return paOutput.decode()
|
||||||
|
|
||||||
def __getMuteStatus__(self):
|
def __getMuteStatus__(self):
|
||||||
|
try:
|
||||||
paList = self.__getPaList__()
|
paList = self.__getPaList__()
|
||||||
fromCurrent = paList[paList.find('* index:'):]
|
fromCurrent = paList[paList.index('* index:'):]
|
||||||
MUTED_STR = 'muted: '
|
MUTED_STR = 'muted: '
|
||||||
fromMutestat = fromCurrent[fromCurrent.find(MUTED_STR)
|
fromMutestat = fromCurrent[fromCurrent.index(MUTED_STR)
|
||||||
+ len(MUTED_STR):]
|
+ len(MUTED_STR):]
|
||||||
return fromMutestat[:3] == 'yes'
|
return fromMutestat[:3] == 'yes'
|
||||||
|
except ValueError:
|
||||||
|
raise NoMicrophone()
|
||||||
|
|
||||||
def __toggleMuteStatus__(self):
|
def __toggleMuteStatus__(self):
|
||||||
|
try:
|
||||||
isMuted = self.__getMuteStatus__()
|
isMuted = self.__getMuteStatus__()
|
||||||
paList = self.__getPaList__()
|
paList = self.__getPaList__()
|
||||||
fromCurrent = paList[paList.find('* index: ')+len('* index: '):]
|
fromCurrent = paList[paList.index('* index: ')+len('* index: '):]
|
||||||
sourceId = int(fromCurrent[:fromCurrent.find('\n')])
|
sourceId = int(fromCurrent[:fromCurrent.index('\n')])
|
||||||
subprocess.call(['pacmd', 'set-source-mute', str(sourceId),
|
subprocess.call(['pacmd', 'set-source-mute', str(sourceId),
|
||||||
str(int(not isMuted))])
|
str(int(not isMuted))])
|
||||||
|
except ValueError:
|
||||||
|
raise NoMicrophone()
|
||||||
|
|
||||||
def on_click(self, event):
|
def on_click(self, event):
|
||||||
self.__toggleMuteStatus__()
|
self.__toggleMuteStatus__()
|
||||||
|
|
Loading…
Reference in a new issue