notifications.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # vim: set fileencoding=utf-8 :
  2. #
  3. # (C) 2011 Guido Guenther <agx@sigxcpu.org>
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, please see
  16. # <http://www.gnu.org/licenses/>
  17. import warnings
  18. notify_module = None
  19. def enable_notifications():
  20. global notify_module
  21. with warnings.catch_warnings():
  22. # Avoid GTK+ cannot open display warning:
  23. warnings.simplefilter("ignore")
  24. try:
  25. import pynotify
  26. notify_module = pynotify
  27. except (ImportError, RuntimeError):
  28. return False
  29. return notify_module.init("git-buildpackage")
  30. def build_msg(cp, success):
  31. summary = "Gbp %s" % ["failed", "successful"][success]
  32. msg = ("Build of %s %s %s" %
  33. (cp['Source'], cp['Version'], ["failed", "succeeded"][success]))
  34. return summary, msg
  35. def send_notification(summary, msg):
  36. n = notify_module.Notification(summary, msg)
  37. n.set_hint('transient', True)
  38. try:
  39. if not n.show():
  40. return False
  41. except:
  42. return False
  43. return True
  44. def notify(summary, message, notify_opt):
  45. """
  46. Send a notifications
  47. @return: False on error
  48. """
  49. if notify_opt.is_off():
  50. return True
  51. enable = enable_notifications()
  52. if not enable:
  53. return [True, False][notify_opt.is_on()]
  54. return notify_opt.do(send_notification, summary, message)