
\c           @   sp   d  d l  Z  d  d l Z d  d l m Z d g Z e j j   Z d e f d     YZ	 d e f d     YZ
 d S(   iN(   t   constst	   Inventoryc           B   sq   e  Z d  Z d
 d
 d
 d
 e d  Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z d   Z d
 d	  Z RS(   s   
	Inventory object can handle information about available hardware devices. It also informs the plugins
	about related hardware events.
	c         C   s   | d  k	 r | |  _ n t j   |  _ | d  k r? t j } n  | j |  j  |  _ | d  k rl t j } n  | r |  j j	 |  n  | d  k r t
   } n  | |  _ d  |  _ i  |  _ d  S(   N(   t   Nonet   _udev_contextt   pyudevt   Contextt   Monitort   from_netlinkt   _udev_monitorR    t   CFG_DEF_UDEV_BUFFER_SIZEt   set_receive_buffer_sizet   _MonitorObserverFactoryt   _monitor_observer_factoryt   _monitor_observert   _subscriptions(   t   selft   udev_contextt   udev_monitor_clst   monitor_observer_factoryt   buffer_sizeR
   (    (    s<   /usr/lib/python2.7/site-packages/tuned/hardware/inventory.pyt   __init__   s    		c         C   sK   y t  j j |  j | |  SWn' t k
 rF t  j j |  j | |  SXd S(   s9   Get a pyudev.Device object for the sys_name (e.g. 'sda').N(   R   t   Devicest	   from_nameR   t   AttributeErrort   Device(   R   t	   subsystemt   sys_name(    (    s<   /usr/lib/python2.7/site-packages/tuned/hardware/inventory.pyt
   get_device%   s    c         C   s   |  j  j d |  S(   s)   Get list of devices on a given subsystem.R   (   R   t   list_devices(   R   R   (    (    s<   /usr/lib/python2.7/site-packages/tuned/hardware/inventory.pyt   get_devices-   s    c         C   s   | j  |  j k r d  Sxc |  j | j  D]Q \ } } y | | |  Wq' t k
 rw } t j d |  t j |  q' Xq' Wd  S(   Ns+   Exception occured in event handler of '%s'.(   R   R   t	   Exceptiont   logt   errort	   exception(   R   t   eventt   devicet   plugint   callbackt   e(    (    s<   /usr/lib/python2.7/site-packages/tuned/hardware/inventory.pyt   _handle_udev_event1   s    c         C   sz   t  j d | | f  | | f } | |  j k rI |  j | j |  n- | g |  j | <|  j j |  |  j j   d S(   s7   Register handler of device events on a given subsystem.s   adding handler: %s (%s)N(   R   t   debugR   t   appendR   t	   filter_byt   start(   R   R$   R   R%   t   callback_data(    (    s<   /usr/lib/python2.7/site-packages/tuned/hardware/inventory.pyt	   subscribe<   s    c         C   sN   |  j  d  k rJ t j d  |  j j |  j |  j  |  _  |  j  j   n  d  S(   Ns   starting monitor observer(	   R   R   R   R(   R   t   createR   R'   R+   (   R   (    (    s<   /usr/lib/python2.7/site-packages/tuned/hardware/inventory.pyt   start_processing_eventsH   s    c         C   s9   |  j  d  k	 r5 t j d  |  j  j   d  |  _  n  d  S(   Ns   stopping monitor observer(   R   R   R   R(   t   stop(   R   (    (    s<   /usr/lib/python2.7/site-packages/tuned/hardware/inventory.pyt   stop_processing_eventsN   s    c         C   sb   x[ |  j  | D]L } | \ } } | | k r t j d | | f  |  j  | j |  q q Wd  S(   Ns   removing handler: %s (%s)(   R   R   R(   t   remove(   R   R$   R   R,   t   _pluginR%   (    (    s<   /usr/lib/python2.7/site-packages/tuned/hardware/inventory.pyt   _unsubscribe_subsystemT   s
    c         C   s   g  } xe |  j  D]Z } | d k s. | | k r |  j | |  t |  j  |  d k rj | j |  qj q q Wx | D] } |  j  | =qu Wd S(   s4   Unregister handler registered with subscribe method.i    N(   R   R   R4   t   lenR)   (   R   R$   R   t   empty_subsystemst
   _subsystem(    (    s<   /usr/lib/python2.7/site-packages/tuned/hardware/inventory.pyt   unsubscribe[   s    N(   t   __name__t
   __module__t   __doc__R   t   TrueR   R   R   R'   R-   R/   R1   R4   R8   (    (    (    s<   /usr/lib/python2.7/site-packages/tuned/hardware/inventory.pyR   	   s   							R   c           B   s   e  Z d    Z RS(   c         O   s   t  j | |   S(   N(   R   t   MonitorObserver(   R   t   argst   kwargs(    (    s<   /usr/lib/python2.7/site-packages/tuned/hardware/inventory.pyR.   h   s    (   R9   R:   R.   (    (    (    s<   /usr/lib/python2.7/site-packages/tuned/hardware/inventory.pyR   g   s   (   R   t
   tuned.logst   tunedR    t   __all__t   logst   getR   t   objectR   R   (    (    (    s<   /usr/lib/python2.7/site-packages/tuned/hardware/inventory.pyt   <module>   s   	^