Better error handling

This commit is contained in:
Théophile Bastian 2017-03-06 23:45:54 +01:00
parent 1b8a1183c1
commit e9d8a8768c

View file

@ -19,45 +19,65 @@ 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):
isMute = self.__getMuteStatus__() try:
color = self.py3.COLOR_MUTE if isMute else self.py3.COLOR_UNMUTE isMute = self.__getMuteStatus__()
text = self.format_muted if isMute else self.format_unmuted color = self.py3.COLOR_MUTE if isMute else self.py3.COLOR_UNMUTE
return { text = self.format_muted if isMute else self.format_unmuted
'full_text': text, return {
'color': color, 'full_text': text,
} '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):
paList = self.__getPaList__() try:
fromCurrent = paList[paList.find('* index:'):] paList = self.__getPaList__()
MUTED_STR = 'muted: ' fromCurrent = paList[paList.index('* index:'):]
fromMutestat = fromCurrent[fromCurrent.find(MUTED_STR) MUTED_STR = 'muted: '
+ len(MUTED_STR):] fromMutestat = fromCurrent[fromCurrent.index(MUTED_STR)
return fromMutestat[:3] == 'yes' + len(MUTED_STR):]
return fromMutestat[:3] == 'yes'
except ValueError:
raise NoMicrophone()
def __toggleMuteStatus__(self): def __toggleMuteStatus__(self):
isMuted = self.__getMuteStatus__() try:
paList = self.__getPaList__() isMuted = self.__getMuteStatus__()
fromCurrent = paList[paList.find('* index: ')+len('* index: '):] paList = self.__getPaList__()
sourceId = int(fromCurrent[:fromCurrent.find('\n')]) fromCurrent = paList[paList.index('* index: ')+len('* index: '):]
subprocess.call(['pacmd', 'set-source-mute', str(sourceId), sourceId = int(fromCurrent[:fromCurrent.index('\n')])
str(int(not isMuted))]) subprocess.call(['pacmd', 'set-source-mute', str(sourceId),
str(int(not isMuted))])
except ValueError:
raise NoMicrophone()
def on_click(self, event): def on_click(self, event):
self.__toggleMuteStatus__() self.__toggleMuteStatus__()