σ
έΉYc           @` sκ   d  Z  d d l m Z m Z m Z m Z y d d l m Z Wn! e k
 r_ d d l	 m Z n Xd d l
 m Z d d l m Z d d l m Z m Z m Z m Z d d d	 g Z d
   Z d e f d     YZ d e f d     YZ d S(   u   
    pyudev.core
    ===========

    Core types and functions of :mod:`pyudev`.

    .. moduleauthor::  Sebastian Wiesner  <lunaryorn@googlemail.com>
i    (   t   print_functiont   divisiont   unicode_literalst   absolute_import(   t   check_output(   t   Device(   t   load_udev_library(   t   ensure_unicode_stringt   ensure_byte_stringt   udev_list_iteratet   property_value_to_bytesu   udev_versionu   Contextu
   Enumeratorc          C` s(   t  t d d g   }  t |  j    S(   ul  
    Get the version of the underlying udev library.

    udev doesn't use a standard major-minor versioning scheme, but instead
    labels releases with a single consecutive number.  Consequently, the
    version number returned by this function is a single integer, and not a
    tuple (like for instance the interpreter version in
    :data:`sys.version_info`).

    As libudev itself does not provide a function to query the version number,
    this function calls the ``udevadm`` utilitiy, so be prepared to catch
    :exc:`~exceptions.EnvironmentError` and
    :exc:`~subprocess.CalledProcessError` if you call this function.

    Return the version number as single integer.  Raise
    :exc:`~exceptions.ValueError`, if the version number retrieved from udev
    could not be converted to an integer.  Raise
    :exc:`~exceptions.EnvironmentError`, if ``udevadm`` was not found, or could
    not be executed.  Raise :exc:`subprocess.CalledProcessError`, if
    ``udevadm`` returned a non-zero exit code.  On Python 2.7 or newer, the
    ``output`` attribute of this exception is correctly set.

    .. versionadded:: 0.8
    u   udevadmu	   --version(   R   R   t   intt   strip(   t   output(    (    s/   /usr/lib/python2.7/site-packages/pyudev/core.pyt   udev_version.   s    t   Contextc           B` sw   e  Z d  Z d   Z d   Z e d    Z e d    Z e d    Z e d    Z	 e	 j
 d    Z	 d   Z RS(	   u  
    The udev context.

    This is *the* central object to access udev.  An instance of this class
    must be created before anything else can be done.  It holds the udev
    configuration and provides the interface to list devices (see
    :meth:`list_devices`).

    Instances of this class can directly be given as ``udev *`` to functions
    wrapped through :mod:`ctypes`.
    c         C` s"   t    |  _ |  j j   |  _ d S(   u'   
        Create a new context.
        N(   R   t   _libudevt   udev_newt   _as_parameter_(   t   self(    (    s/   /usr/lib/python2.7/site-packages/pyudev/core.pyt   __init__X   s    c         C` s   |  j  j |   d  S(   N(   R   t
   udev_unref(   R   (    (    s/   /usr/lib/python2.7/site-packages/pyudev/core.pyt   __del___   s    c         C` s   t  |  j j |    S(   uί   
        The ``sysfs`` mount point defaulting to ``/sys'`` as unicode string.

        The mount point can be overwritten using the environment variable
        :envvar:`SYSFS_PATH`.  Use this for testing purposes.
        (   R   R   t   udev_get_sys_path(   R   (    (    s/   /usr/lib/python2.7/site-packages/pyudev/core.pyt   sys_pathb   s    c         C` s   t  |  j j |    S(   u   
        The device directory path defaulting to ``/dev`` as unicode string.

        This can be overridden in the udev configuration.
        (   R   R   t   udev_get_dev_path(   R   (    (    s/   /usr/lib/python2.7/site-packages/pyudev/core.pyt   device_pathl   s    c         C` s   t  |  j j |    S(   u    
        The run runtime directory path defaulting to ``/run`` as unicode
        string.

        .. udevversion:: 167

        .. versionadded:: 0.10
        (   R   R   t   udev_get_run_path(   R   (    (    s/   /usr/lib/python2.7/site-packages/pyudev/core.pyt   run_pathu   s    
c         C` s   |  j  j |   S(   u  
        The logging priority of the interal logging facitility of udev as
        integer with a standard :mod:`syslog` priority.  Assign to this
        property to change the logging priority.

        UDev uses the standard :mod:`syslog` priorities.  Constants for these
        priorities are defined in the :mod:`syslog` module in the standard
        library:

        >>> import syslog
        >>> context = pyudev.Context()
        >>> context.log_priority = syslog.LOG_DEBUG

        .. versionadded:: 0.9
        (   R   t   udev_get_log_priority(   R   (    (    s/   /usr/lib/python2.7/site-packages/pyudev/core.pyt   log_priority   s    c         C` s   |  j  j |  |  d  S(   N(   R   t   udev_set_log_priority(   R   t   value(    (    s/   /usr/lib/python2.7/site-packages/pyudev/core.pyR      s    c         K` s   t  |   j |   S(   u!  
        List all available devices.

        The arguments of this method are the same as for
        :meth:`Enumerator.match()`.  In fact, the arguments are simply passed
        straight to method :meth:`~Enumerator.match()`.

        This function creates and returns an :class:`Enumerator` object,
        that can be used to filter the list of devices, and eventually
        retrieve :class:`Device` objects representing matching devices.

        .. versionchanged:: 0.8
           Accept keyword arguments now for easy matching
        (   t
   Enumeratort   match(   R   t   kwargs(    (    s/   /usr/lib/python2.7/site-packages/pyudev/core.pyt   list_devices   s    (   t   __name__t
   __module__t   __doc__R   R   t   propertyR   R   R   R   t   setterR$   (    (    (    s/   /usr/lib/python2.7/site-packages/pyudev/core.pyR   K   s   		
	R!   c           B` sw   e  Z d  Z d   Z d   Z d   Z e d  Z d   Z d   Z	 e d  Z
 d   Z d	   Z d
   Z d   Z RS(   uΣ  
    Enumerate all available devices.

    To retrieve devices, simply iterate over an instance of this class.
    This operation yields :class:`Device` objects representing the available
    devices.

    Before iteration the device list can be filtered by subsystem or by
    property values using :meth:`match_subsystem` and
    :meth:`match_property`.  Multiple subsystem (property) filters are
    combined using a logical OR, filters of different types are combined
    using a logical AND.  The following filter for instance::

        devices.match_subsystem('block').match_property(
            'ID_TYPE', 'disk').match_property('DEVTYPE', 'disk')

    means the following::

        subsystem == 'block' and (ID_TYPE == 'disk' or DEVTYPE == 'disk')

    Once added, a filter cannot be removed anymore.  Create a new object
    instead.

    Instances of this class can directly be given as given ``udev_enumerate *``
    to functions wrapped through :mod:`ctypes`.
    c         C` sL   t  | t  s t d   n  | |  _ | j j |  |  _ | j |  _ d S(   uϊ   
        Create a new enumerator with the given ``context`` (a
        :class:`Context` instance).

        While you can create objects of this class directly, this is not
        recommended.  Call :method:`Context.list_devices()` instead.
        u   Invalid context objectN(   t
   isinstanceR   t	   TypeErrort   contextR   t   udev_enumerate_newR   (   R   R,   (    (    s/   /usr/lib/python2.7/site-packages/pyudev/core.pyR   Ζ   s
    	c         C` s   |  j  j |   d  S(   N(   R   t   udev_enumerate_unref(   R   (    (    s/   /usr/lib/python2.7/site-packages/pyudev/core.pyR   Τ   s    c         K` sι   | j  d d  } | d k	 r. |  j |  n  | j  d d  } | d k	 r\ |  j |  n  | j  d d  } | d k	 r |  j |  n  | j  d d  } | d k	 rΈ |  j |  n  x* | j   D] \ } } |  j | |  qΕ W|  S(   u3  
        Include devices according to the rules defined by the keyword
        arguments.  These keyword arguments are interpreted as follows:

        - The value for the keyword argument ``subsystem`` is forwarded to
          :meth:`match_subsystem()`.
        - The value for the keyword argument ``sys_name`` is forwared to
          :meth:`match_sys_name()`.
        - The value for the keyword argument ``tag`` is forwared to
          :meth:`match_tag()`.
        - The value for the keyword argument ``parent`` is forwared to
          :meth:`match_parent()`.
        - All other keyword arguments are forwareded one by one to
          :meth:`match_property()`.  The keyword argument itself is interpreted
          as property name, the value of the keyword argument as the property
          value.

        All keyword arguments are optional, calling this method without no
        arguments at all is simply a noop.

        Return the instance again.

        .. versionadded:: 0.8

        .. versionchanged:: 0.13
           Added ``parent`` keyword
        u	   subsystemu   sys_nameu   tagu   parentN(   t   popt   Nonet   match_subsystemt   match_sys_namet	   match_tagt   match_parentt	   iteritemst   match_property(   R   R#   t	   subsystemt   sys_namet   tagt   parentR(   R    (    (    s/   /usr/lib/python2.7/site-packages/pyudev/core.pyR"   Χ   s    c         C` s5   | s |  j  j n	 |  j  j } | |  t |   |  S(   u  
        Include all devices, which are part of the given ``subsystem``.

        ``subsystem`` is either a unicode string or a byte string, containing
        the name of the subsystem.  If ``nomatch`` is ``True`` (default is
        ``False``), the match is inverted:  A device is only included if it is
        *not* part of the given ``subsystem``.

        Return the instance again.
        (   R   t"   udev_enumerate_add_match_subsystemt$   udev_enumerate_add_nomatch_subsystemR   (   R   R7   t   nomatchR"   (    (    s/   /usr/lib/python2.7/site-packages/pyudev/core.pyR1     s    c         C` s   |  j  j |  t |   |  S(   uΛ   
        Include all devices with the given name.

        ``sys_name`` is a byte or unicode string containing the device name.

        Return the instance again.

        .. versionadded:: 0.8
        (   R   t    udev_enumerate_add_match_sysnameR   (   R   R8   (    (    s/   /usr/lib/python2.7/site-packages/pyudev/core.pyR2     s    
	c         C` s&   |  j  j |  t |  t |   |  S(   uΰ  
        Include all devices, whose ``property`` has the given ``value``.

        ``property`` is either a unicode string or a byte string, containing
        the name of the property to match.  ``value`` is a property value,
        being one of the following types:

        - :func:`int`
        - :func:`bool`
        - A byte string
        - Anything convertable to a unicode string (including a unicode string
          itself)

        Return the instance again.
        (   R   t!   udev_enumerate_add_match_propertyR   R
   (   R   R(   R    (    (    s/   /usr/lib/python2.7/site-packages/pyudev/core.pyR6   "  s    	c         C` s>   | s |  j  j n	 |  j  j } | |  t |  t |   |  S(   uΈ  
        Include all devices, whose ``attribute`` has the given ``value``.

        ``attribute`` is either a unicode string or a byte string, containing
        the name of a sys attribute to match.  ``value`` is an attribute value,
        being one of the following types:

        - :func:`int`,
        - :func:`bool`
        - A byte string
        - Anything convertable to a unicode string (including a unicode string
          itself)

        If ``nomatch`` is ``True`` (default is ``False``), the match is
        inverted:  A device is include if the ``attribute`` does *not* match
        the given ``value``.

        .. note::

           If ``nomatch`` is ``True``, devices which do not have the given
           ``attribute`` at all are also included.  In other words, with
           ``nomatch=True`` the given ``attribute`` is *not* guaranteed to
           exist on all returned devices.

        Return the instance again.
        (   R   t    udev_enumerate_add_match_sysattrt"   udev_enumerate_add_nomatch_sysattrR   R
   (   R   t	   attributeR    R=   R"   (    (    s/   /usr/lib/python2.7/site-packages/pyudev/core.pyt   match_attribute6  s
    c         C` s   |  j  j |  t |   |  S(   uτ   
        Include all devices, which have the given ``tag`` attached.

        ``tag`` is a byte or unicode string containing the tag name.

        Return the instance again.

        .. udevversion:: 154

        .. versionadded:: 0.6
        (   R   t   udev_enumerate_add_match_tagR   (   R   R9   (    (    s/   /usr/lib/python2.7/site-packages/pyudev/core.pyR3   X  s    c         C` s   |  j  j |   |  S(   uΪ  
        Include only devices, which are initialized.

        Initialized devices have properly set device node permissions and
        context, and are (in case of network devices) fully renamed.

        Currently this will not affect devices which do not have device nodes
        and are not network interfaces.

        Return the instance again.

        .. seealso:: :attr:`Device.is_initialized`

        .. udevversion:: 165

        .. versionadded:: 0.8
        (   R   t'   udev_enumerate_add_match_is_initialized(   R   (    (    s/   /usr/lib/python2.7/site-packages/pyudev/core.pyt   match_is_initializedg  s    c         C` s   |  j  j |  |  |  S(   u   
        Include all devices on the subtree of the given ``parent`` device.

        The ``parent`` device itself is also included.

        ``parent`` is a :class:`~pyudev.Device`.

        Return the instance again.

        .. udevversion:: 172

        .. versionadded:: 0.13
        (   R   t   udev_enumerate_add_match_parent(   R   R:   (    (    s/   /usr/lib/python2.7/site-packages/pyudev/core.pyR4   |  s    c         c` s]   |  j  j |   |  j  j |   } x4 t |  j  |  D]  \ } } t j |  j |  Vq5 Wd S(   u\   
        Iterate over all matching devices.

        Yield :class:`Device` objects.
        N(   R   t   udev_enumerate_scan_devicest   udev_enumerate_get_list_entryR	   R   t   from_sys_pathR,   (   R   t   entryt   namet   _(    (    s/   /usr/lib/python2.7/site-packages/pyudev/core.pyt   __iter__  s    (   R%   R&   R'   R   R   R"   t   FalseR1   R2   R6   RC   R3   RF   R4   RN   (    (    (    s/   /usr/lib/python2.7/site-packages/pyudev/core.pyR!   ͺ   s   			,		"			N(   R'   t
   __future__R    R   R   R   t
   subprocessR   t   ImportErrort   pyudev._compatt   pyudev.deviceR   t   pyudev._libudevR   t   pyudev._utilR   R   R	   R
   t   __all__R   t   objectR   R!   (    (    (    s/   /usr/lib/python2.7/site-packages/pyudev/core.pyt   <module>   s   ""	_