PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Hellanzb/Growl.py

https://github.com/myusuf3/hellanzb
Python | 132 lines | 98 code | 18 blank | 16 comment | 8 complexity | 0aa57298ecbd1d58eea1e237c911fa6e MD5 | raw file
Possible License(s): 0BSD
  1. #!/usr/bin/env python
  2. """Growl 0.6 Network Protocol Client for Python"""
  3. __version__ = "0.6" # will always match Growl version
  4. __author__ = "Rui Carmo (http://the.taoofmac.com)"
  5. __copyright__ = "(C) 2004 Rui Carmo. Code under BSD License."
  6. __contributors__ = "Ingmar J Stein (Growl Team)"
  7. import struct
  8. import md5
  9. from socket import AF_INET, SOCK_DGRAM, socket
  10. GROWL_UDP_PORT=9887
  11. GROWL_PROTOCOL_VERSION=1
  12. GROWL_TYPE_REGISTRATION=0
  13. GROWL_TYPE_NOTIFICATION=1
  14. class GrowlRegistrationPacket:
  15. """Builds a Growl Network Registration packet.
  16. Defaults to emulating the command-line growlnotify utility."""
  17. def __init__(self, application="growlnotify", password = None ):
  18. self.notifications = []
  19. self.defaults = [] # array of indexes into notifications
  20. self.application = application.encode("utf-8")
  21. self.password = password
  22. # end def
  23. def addNotification(self, notification="Command-Line Growl Notification", enabled=True):
  24. """Adds a notification type and sets whether it is enabled on the GUI"""
  25. self.notifications.append(notification)
  26. if enabled:
  27. self.defaults.append(len(self.notifications)-1)
  28. # end def
  29. def payload(self):
  30. """Returns the packet payload."""
  31. self.data = struct.pack( "!BBH",
  32. GROWL_PROTOCOL_VERSION,
  33. GROWL_TYPE_REGISTRATION,
  34. len(self.application) )
  35. self.data += struct.pack( "BB",
  36. len(self.notifications),
  37. len(self.defaults) )
  38. self.data += self.application
  39. for notification in self.notifications:
  40. encoded = notification.encode("utf-8")
  41. self.data += struct.pack("!H", len(encoded))
  42. self.data += encoded
  43. for default in self.defaults:
  44. self.data += struct.pack("B", default)
  45. self.checksum = md5.new()
  46. self.checksum.update(self.data)
  47. if self.password:
  48. self.checksum.update(self.password)
  49. self.data += self.checksum.digest()
  50. return self.data
  51. # end def
  52. # end class
  53. class GrowlNotificationPacket:
  54. """Builds a Growl Network Notification packet.
  55. Defaults to emulating the command-line growlnotify utility."""
  56. def __init__(self, application="growlnotify",
  57. notification="Command-Line Growl Notification", title="Title",
  58. description="Description", priority = 0, sticky = False, password = None ):
  59. self.application = application.encode("utf-8")
  60. self.notification = notification.encode("utf-8")
  61. self.title = title.encode("utf-8")
  62. self.description = description.encode("utf-8")
  63. flags = (priority & 0x07) * 2
  64. if priority < 0:
  65. flags |= 0x08
  66. if sticky:
  67. flags = flags | 0x0001
  68. self.data = struct.pack( "!BBHHHHH",
  69. GROWL_PROTOCOL_VERSION,
  70. GROWL_TYPE_NOTIFICATION,
  71. flags,
  72. len(self.notification),
  73. len(self.title),
  74. len(self.description),
  75. len(self.application) )
  76. self.data += self.notification
  77. self.data += self.title
  78. self.data += self.description
  79. self.data += self.application
  80. self.checksum = md5.new()
  81. self.checksum.update(self.data)
  82. if password:
  83. self.checksum.update(password)
  84. self.data += self.checksum.digest()
  85. # end def
  86. def payload(self):
  87. """Returns the packet payload."""
  88. return self.data
  89. # end def
  90. # end class
  91. if __name__ == '__main__':
  92. print "Starting Unit Test"
  93. print " - please make sure Growl is listening for network notifications"
  94. addr = ("localhost", GROWL_UDP_PORT)
  95. s = socket(AF_INET,SOCK_DGRAM)
  96. print "Assembling registration packet like growlnotify's (no password)"
  97. p = GrowlRegistrationPacket()
  98. p.addNotification()
  99. print "Sending registration packet"
  100. s.sendto(p.payload(), addr)
  101. print "Assembling standard notification packet"
  102. p = GrowlNotificationPacket()
  103. print "Sending standard notification packet"
  104. s.sendto(p.payload(), addr)
  105. print "Assembling priority -2 (Very Low) notification packet"
  106. p = GrowlNotificationPacket(priority=-2)
  107. print "Sending priority -2 notification packet"
  108. s.sendto(p.payload(), addr)
  109. print "Assembling priority 2 (Very High) sticky notification packet"
  110. p = GrowlNotificationPacket(priority=2,sticky=True)
  111. print "Sending priority 2 (Very High) sticky notification packet"
  112. s.sendto(p.payload(), addr)
  113. s.close()
  114. print "Done."