
    gq                      
   d Z ddlZddlZddlZddlZddlmZ ddlZddl	m
Z
 ddlZddgZ ej                  e      Z G d de      Zddd	dd
dddddddefdZedk(  r& ej&                  ej(                          ed       yy)ad  
The gntp.notifier module is provided as a simple way to send notifications
using GNTP

.. note::
	This class is intended to mostly mirror the older Python bindings such
	that you should be able to replace instances of the old bindings with
	this class.
	`Original Python bindings <http://code.google.com/p/growl/source/browse/Bindings/python/Growl.py>`_

    N)__version__miniGrowlNotifierc                   r    e Zd ZdZdZdZdg dddddfdZd	 Zd
 Zdddddi fdZ	d Z
d Zd Zd Zd Zd Zy)r   an  Helper class to simplfy sending Growl messages

	:param string applicationName: Sending application name
	:param list notification: List of valid notifications
	:param list defaultNotifications: List of notifications that should be enabled
		by default
	:param string applicationIcon: Icon URL
	:param string hostname: Remote host
	:param integer port: Remote port
	MD5   zPython GNTPN	localhostZ  c                     || _         t        |      | _        |rt        |      | _        n| j                  | _        || _        || _        || _        t        |      | _        y N)	applicationNamelistnotificationsdefaultNotificationsapplicationIconpasswordhostnameintport)selfr   r   r   r   r   r   r   s           "/opt/Tautulli/lib/gntp/notifier.py__init__zGrowlNotifier.__init__0   sY     )$M*$#$894#114($$-$-$i$)    c                 t    t         j                  d       t        j                  j	                  |      dd dv S )z
		Check the icon to see if it's valid

		If it's a simple URL icon, then we return True. If it's a data icon
		then we return False
		zChecking iconN   )httpfile)loggerinfogntpshimu)r   datas     r   
_checkIconzGrowlNotifier._checkIcon@   s0     	++o	T	2A	"2	22r   c                    t         j                  d| j                  | j                         t        j
                  j                         }|j                  d| j                         | j                  D ]"  }|| j                  v }|j                  ||       $ | j                  re| j                  | j                        r|j                  d| j                         n-|j                  | j                        }|j                  d|       | j                  r&|j!                  | j                  | j"                         | j%                  |       | j'                  |       | j)                  d|      S )zSend GNTP Registration

		.. warning::
			Before sending notifications to Growl, you need to have
			sent a registration message at least once
		zSending registration to %s:%sApplication-NamezApplication-Iconregister)r   r   r   r   r    coreGNTPRegister
add_headerr   r   r   add_notificationr   r$   add_resourcer   set_passwordpasswordHashadd_origin_inforegister_hook_send)r   r'   notificationenabledresources        r   r'   zGrowlNotifier.registerK   s$    	++-t}}diiHYY##%(
($*>*>?(( 4lT6667\734 

ood**+*D,@,@A$$T%9%9:H*H5	]](9(9:x X	J	))r   Fc
                    t         j                  d|| j                  | j                         || j                  v sJ t
        j                  j                         }
|
j                  d| j                         |
j                  d|       |
j                  d|       | j                  r&|
j                  | j                  | j                         |r|
j                  d|       |r|
j                  d|       |rG| j                  |      r|
j                  d|       n#|
j                  |      }|
j                  d|       |r|
j                  d|       |r|
j                  d	|       |r|
j                  d
|       |	D ]  }|
j                  ||	|           | j                  |
       | j!                  |
       | j#                  d|
      S )a  Send a GNTP notifications

		.. warning::
			Must have registered with growl beforehand or messages will be ignored

		:param string noteType: One of the notification names registered earlier
		:param string title: Notification title (usually displayed on the notification)
		:param string description: The main content of the notification
		:param string icon: Icon URL path
		:param boolean sticky: Sticky notification
		:param integer priority: Message priority level from -2 to 2
		:param string callback:  URL callback
		:param dict custom: Custom attributes. Key names should be prefixed with X-
			according to the spec but this is not enforced by this class

		.. warning::
			For now, only URL callbacks are supported. In the future, the
			callback argument will also support a function
		z"Sending notification [%s] to %s:%sr&   zNotification-NamezNotification-TitlezNotification-StickyzNotification-PriorityzNotification-IconzNotification-TextzNotification-Callback-TargetzNotification-Coalescing-IDnotify)r   r   r   r   r   r    r(   
GNTPNoticer*   r   r   r-   r.   r$   r,   r/   notify_hookr1   )r   noteTypetitledescriptioniconstickyprioritycallback
identifiercustomnoticer4   keys                r   r6   zGrowlNotifier.notifyd   s   * 	++2HdmmTYYW	T''	''	'99!&&(<(<='2(%0	]]	t}}d&7&78	*F3	,h7	
ood
)40""4(H
)84	(+6	3X>	1:> 'c	S&+&' v6	Hf	%%r   c                 v   t         j                  j                         }|j                  d|       |j                  d|       |j                  d|       | j                  r&|j                  | j                  | j                         | j                  |       | j                  |       | j                  d|      S )z,Send a Subscribe request to a remote machinezSubscriber-IDzSubscriber-NamezSubscriber-Port	subscribe)
r    r(   GNTPSubscriber*   r   r-   r.   r/   subscribe_hookr1   )r   idnamer   subs        r   rE   zGrowlNotifier.subscribe   s    		!#.."%.."D).."D)	]]DMM4#4#45sc	K	%%r   c                 ,   |j                  dt        j                                |j                  dd       |j                  dt               |j                  dt        j                                |j                  dt        j                                y)z&Add optional Origin headers to messagezOrigin-Machine-NamezOrigin-Software-Namezgntp.pyzOrigin-Software-VersionzOrigin-Platform-NamezOrigin-Platform-VersionN)r*   platformnoder   systemr   packets     r   r/   zGrowlNotifier.add_origin_info   sm    )8==?;*I6-{;*HOO,=>-x/@/@/BCr   c                      y r    rO   s     r   r0   zGrowlNotifier.register_hook       r   c                      y r   rR   rO   s     r   r8   zGrowlNotifier.notify_hook   rS   r   c                      y r   rR   rO   s     r   rG   zGrowlNotifier.subscribe_hook   rS   r   c                    |j                          |j                         }t        j                  d| j                  | j
                  |j                  |       t        j                  t        j                  t        j                        }|j                  | j                         	 |j                  | j                  | j
                  f       |j                  |       |j                  d      }|j                  t         j"                  j%                  d            sC||j                  d      z  }|j                  t         j"                  j%                  d            sCt         j0                  j3                  |      }|j5                          t        j                  d| j                  | j
                  |j                  |       t7        |      t         j0                  j8                  k(  ryt        j'                  d|j'                                |j'                         S # t        j&                  $ r- t)        j*                         d   }t-        j.                  |      w xY w)zSend the GNTP PacketzTo : %s:%s <%s>
%si   z

   zFrom : %s:%s <%s>
%sTzInvalid response: %s)validateencoder   debugr   r   	__class__socketAF_INETSOCK_STREAM
settimeoutsocketTimeoutconnectsendrecvendswithr    r!   berrorsysexc_infoerrorsNetworkErrorr(   
parse_gntpclosetypeGNTPOK)r   messagetyperP   r#   s	recv_dataexcresponses           r   r1   zGrowlNotifier._send   s    	//	$,,$dmmTYY@P@PRVWmmFNNF$6$67!,,t!!"	"99dmmTYY'(66$<vvd|9		J 78I 		J 78 YY!!),(''),,&tyy(BTBTV^_	(^tyy'''
,,%x~~'78		 
 "		3			S	!!"s   &B9H! !A I!)__name__
__module____qualname____doc__r.   r`   r   r$   r'   r6   rE   r/   r0   r8   rG   r1   rR   r   r   r   r   !   sh    	 $1d[u 	3*2 6:%4D5&n&Dr   
PythonMiniMessagezMini Messager	   r
   Fc           
          	  |||g|g||||      }|j                         }|dur|S |j                  ||| |||	|
|      S # t        $ r t        j	                  d       Y yw xY w)aZ  Single notification function

	Simple notification function in one line. Has only one required parameter
	and attempts to use reasonable defaults for everything else
	:param string description: Notification message

	.. warning::
			For now, only URL callbacks are supported. In the future, the
			callback argument will also support a function
	)r   r   r   r   r   r   r   T)r9   r:   r;   r<   r=   r>   r?   r@   zGrowl errorN)r'   r6   	Exceptionr   	exception)r;   r   r9   r:   r   r   r   r   r=   r>   r?   notificationIconr@   notifierFactorygrowlresults                   r   r   r      s    "
"!
"% >>&4
=		
 
 	
 	 	 " 	=!"s   &A A A#"A#__main__)levelzTesting mini notification)rw   loggingrL   r\   rg   gntp.versionr   	gntp.corer    gntp.errorsri   	gntp.shim__all__	getLoggerrt   r   objectr   r   basicConfigINFOrR   r   r   <module>r      s   
    
 $    
 
		8	$vF vr '3Y	uUT4D 	*"X z 7<<(!"	 r   