From 8cad12195a95588cd748541df0e9d2d9b66c3b09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20=C5=9Eahin?= Date: Tue, 23 Aug 2016 16:44:02 +0300 Subject: [PATCH] notifier and asking for permission features added to util --- opt/ahenk/base/util/util.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/opt/ahenk/base/util/util.py b/opt/ahenk/base/util/util.py index 9bd4df3..3369dd0 100644 --- a/opt/ahenk/base/util/util.py +++ b/opt/ahenk/base/util/util.py @@ -278,3 +278,37 @@ class Util: command = "sudo apt-get --yes --force-yes purge {0}={1}".format(package_name, package_version) result_code, p_out, p_err = Util.execute(command) return result_code, p_out, p_err + + @staticmethod + def send_notify(title, body, display, user, icon=None, timeout=5000): + + inner_command = 'notify-send "{0}" "{1}" -t {2}'.format(title, body, timeout) + if icon: + inner_command += ' -i {0}'.format(icon) + + if user != 'root': + Util.execute('export DISPLAY={0}; su - {1} -c \'{2}\''.format(display, user,inner_command)) + + @staticmethod + def ask_permission(display, username, message, title): + ask_path = '/opt/ahenk/base/agreement/confirm.py' + try: + + if username is not None: + command = 'export DISPLAY={0};su - {1} -c \'python3 {2} \"{3}\" \"{4}\"\''.format(display, username, + ask_path, + message, + title) + result_code, p_out, p_err = Util.execute(command) + + if p_out.strip() == 'Y': + return True + elif p_out.strip() == 'N': + return False + else: + return None + + else: + return None + except Exception: + return None