|
| 1 | +import shutil |
| 2 | +import subprocess |
| 3 | + |
| 4 | + |
| 5 | +class Notifier(): |
| 6 | + """linuxnotifier: Display Notification in linux""" |
| 7 | + |
| 8 | + def __init__(self, title, descriptions="", timeout=5, urgency="normal", iconpath="", appname=""): |
| 9 | + """ |
| 10 | + Args: |
| 11 | + title ([type]): [description] |
| 12 | + descriptions (str, optional): [description]. Defaults to "". |
| 13 | + timeout (int, optional): [description]. Defaults to 5. |
| 14 | + urgency (str, optional): [description]. Defaults to "normal". |
| 15 | + [low, normal, critical] |
| 16 | + iconpath (str, optional): [description]. Defaults to "". |
| 17 | + appname (str, optional): [description]. Defaults to "". |
| 18 | +
|
| 19 | + """ |
| 20 | + self.__title = title |
| 21 | + self.__descriptions = descriptions |
| 22 | + self.__timeout = timeout |
| 23 | + self.__urgency = urgency |
| 24 | + self.__iconpath = iconpath |
| 25 | + self.__appname = appname |
| 26 | + |
| 27 | + if title is None or title == "": |
| 28 | + raise ValueError("Title can't be empty") |
| 29 | + |
| 30 | + if urgency not in ["normal", "low", "critical", None]: |
| 31 | + raise ValueError("Inappropriate urgency given") |
| 32 | + |
| 33 | + def send_notification(self): |
| 34 | + """Display notification""" |
| 35 | + # raising error if 'notify-send' command can't be found |
| 36 | + if shutil.which("notify-send") is None: |
| 37 | + raise SystemError( |
| 38 | + "Install libnotify-bin\n run 'sudo apt-get install libnotify-bin'") |
| 39 | + |
| 40 | + else: |
| 41 | + # Creating notify-send command into list |
| 42 | + notification = ["notify-send", self.__title, |
| 43 | + self.__descriptions, "-t", f"{self.__timeout * 1000}", ] |
| 44 | + |
| 45 | + # if urgency level is given |
| 46 | + if self.__urgency != "": |
| 47 | + notification += ["-u", self.__urgency] |
| 48 | + |
| 49 | + # if iconpath is given |
| 50 | + if self.__iconpath != "": |
| 51 | + notification += ["-i", self.__iconpath] |
| 52 | + # if appname is given |
| 53 | + if self.__appname != "": |
| 54 | + notification += ["-a", self.__appname] |
| 55 | + |
| 56 | + # Executing Process |
| 57 | + subprocess.Popen(notification, shell=False) |
| 58 | + |
0 commit comments