Browse Source

Growl update

pull/84/head
Ruud 13 years ago
parent
commit
e4588a5e7e
  1. 4
      libs/gntp/__init__.py
  2. 21
      libs/gntp/notifier.py

4
libs/gntp/__init__.py

@ -1,8 +1,8 @@
import re
import hashlib import hashlib
import re
import time import time
__version__ = '0.5' __version__ = '0.6'
#GNTP/<version> <messagetype> <encryptionAlgorithmID>[:<ivValue>][ <keyHashAlgorithmID>:<keyHash>.<salt>] #GNTP/<version> <messagetype> <encryptionAlgorithmID>[:<ivValue>][ <keyHashAlgorithmID>:<keyHash>.<salt>]
GNTP_INFO_LINE = re.compile( GNTP_INFO_LINE = re.compile(

21
libs/gntp/notifier.py

@ -24,12 +24,17 @@ logger = logging.getLogger(__name__)
def mini(description, applicationName = 'PythonMini', noteType = "Message", def mini(description, applicationName = 'PythonMini', noteType = "Message",
title = "Mini Message", applicationIcon = None, hostname = 'localhost', title = "Mini Message", applicationIcon = None, hostname = 'localhost',
password = None, port = 23053, sticky = False, priority = None): password = None, port = 23053, sticky = False, priority = None,
callback = None):
"""Single notification function """Single notification function
Simple notification function in one line. Has only one required parameter Simple notification function in one line. Has only one required parameter
and attempts to use reasonable defaults for everything else and attempts to use reasonable defaults for everything else
:param string description: Notification message :param string description: Notification message
.. warning::
For now, only URL callbacks are supported. In the future, the
callback argument will also support a function
""" """
growl = GrowlNotifier( growl = GrowlNotifier(
applicationName = applicationName, applicationName = applicationName,
@ -50,6 +55,7 @@ def mini(description, applicationName = 'PythonMini', noteType = "Message",
icon = applicationIcon, icon = applicationIcon,
sticky = sticky, sticky = sticky,
priority = priority, priority = priority,
callback = callback,
) )
@ -66,6 +72,7 @@ class GrowlNotifier(object):
""" """
passwordHash = 'MD5' passwordHash = 'MD5'
socketTimeout = 3
def __init__(self, applicationName = 'Python GNTP', notifications = [], def __init__(self, applicationName = 'Python GNTP', notifications = [],
defaultNotifications = None, applicationIcon = None, hostname = 'localhost', defaultNotifications = None, applicationIcon = None, hostname = 'localhost',
@ -112,7 +119,8 @@ class GrowlNotifier(object):
self.register_hook(register) self.register_hook(register)
return self._send('register', register) return self._send('register', register)
def notify(self, noteType, title, description, icon = None, sticky = False, priority = None): def notify(self, noteType, title, description, icon = None, sticky = False,
priority = None, callback = None):
"""Send a GNTP notifications """Send a GNTP notifications
.. warning:: .. warning::
@ -124,6 +132,11 @@ class GrowlNotifier(object):
:param string icon: Icon URL path :param string icon: Icon URL path
:param boolean sticky: Sticky notification :param boolean sticky: Sticky notification
:param integer priority: Message priority level from -2 to 2 :param integer priority: Message priority level from -2 to 2
:param string callback: URL callback
.. warning::
For now, only URL callbacks are supported. In the future, the
callback argument will also support a function
""" """
logger.info('Sending notification [%s] to %s:%s', noteType, self.hostname, self.port) logger.info('Sending notification [%s] to %s:%s', noteType, self.hostname, self.port)
assert noteType in self.notifications assert noteType in self.notifications
@ -141,6 +154,8 @@ class GrowlNotifier(object):
notice.add_header('Notification-Icon', self._checkIcon(icon)) notice.add_header('Notification-Icon', self._checkIcon(icon))
if description: if description:
notice.add_header('Notification-Text', description) notice.add_header('Notification-Text', description)
if callback:
notice.add_header('Notification-Callback-Target', callback)
self.add_origin_info(notice) self.add_origin_info(notice)
self.notify_hook(notice) self.notify_hook(notice)
@ -186,7 +201,7 @@ class GrowlNotifier(object):
logger.debug('To : %s:%s <%s>\n%s', self.hostname, self.port, packet.__class__, data) logger.debug('To : %s:%s <%s>\n%s', self.hostname, self.port, packet.__class__, data)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(3) s.settimeout(self.socketTimeout)
s.connect((self.hostname, self.port)) s.connect((self.hostname, self.port))
s.send(data.encode('utf8', 'replace')) s.send(data.encode('utf8', 'replace'))
recv_data = s.recv(1024) recv_data = s.recv(1024)

Loading…
Cancel
Save