ó
½-'Nc           @   s*   d  d l  m Z d e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   difft   UserChangesc           B   sV   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(	   s	  Detect changes made since the last snapshot was taken.

    If no snapshot is available all users and groups are reported.
    When a snapshot is available, only the changes between the current
    state and the snapshotted state are transmitted to the server.
    c         C   s3   t  t |  ƒ j ƒ  | |  _ | |  _ |  j ƒ  d  S(   N(   t   superR   t   __init__t   _persistt	   _providert   _refresh(   t   selft   persistt   provider(    (    s:   /usr/lib/python2.7/dist-packages/landscape/user/changes.pyR      s    		c         C   sp   |  j  j d i  ƒ |  _ |  j  j d i  ƒ |  _ |  j d |  j j ƒ  ƒ |  _ |  j d |  j j ƒ  ƒ |  _	 d S(   s3   Load the previous snapshot and update current data.t   userst   groupst   usernamet   nameN(
   R   t   gett
   _old_userst   _old_groupst   _create_indexR   t	   get_userst
   _new_userst
   get_groupst   _new_groups(   R   (    (    s:   /usr/lib/python2.7/dist-packages/landscape/user/changes.pyR      s    c         C   s0   |  j  j d |  j ƒ |  j  j d |  j ƒ d S(   s;   Save the current state and use it as a comparison snapshot.R
   R   N(   R   t   setR   R   (   R   (    (    s:   /usr/lib/python2.7/dist-packages/landscape/user/changes.pyt   snapshot    s    c         C   s$   |  j  j d ƒ |  j  j d ƒ d S(   s`   
        Reset the snapshot state and forget all knowledge of users and
        groups.
        R
   R   N(   R   t   remove(   R   (    (    s:   /usr/lib/python2.7/dist-packages/landscape/user/changes.pyt   clear%   s    c         C   s)   i  } x | D] } | | | | <q W| S(   ss   
        Given a key and a sequence of dicts, return a dict of the form
        C{{dict[key]: dict, ...}}.
        (    (   R   t   keyt   sequencet   indext   data(    (    s:   /usr/lib/python2.7/dist-packages/landscape/user/changes.pyR   -   s    c         C   s:   |  j  ƒ  i  } | j |  j ƒ  ƒ | j |  j ƒ  ƒ | S(   s¶   Returns the changes since the last snapshot.
        
        See landscape.message_schemas.USERS schema for a description of the
        dictionary returned by this method.
        (   R   t   updatet   _detect_user_changest   _detect_group_changes(   R   t   changes(    (    s:   /usr/lib/python2.7/dist-packages/landscape/user/changes.pyt   create_diff7   s
    
c         C   s…   i  } t  |  j |  j ƒ \ } } } | rC t | j ƒ  ƒ | d <n  | rb t | j ƒ  ƒ | d <n  | r t | j ƒ  ƒ | d <n  | S(   så   
        Compare the current user snapshot to the old one and return a
        C{dict} with C{create-users}, C{update-users} and
        C{delete-users} fields.  Fields without data aren't included
        in the result.
        s   create-userss   update-userss   delete-users(   R    R   R   t   listt
   itervaluest   iterkeys(   R   R!   t   createst   updatest   deletes(    (    s:   /usr/lib/python2.7/dist-packages/landscape/user/changes.pyR   C   s    c         C   s"  i  } t  |  j |  j ƒ \ } } } | r¬ g  } i  } xS | j ƒ  D]E } | j ƒ  } | j d ƒ } | r{ | | | d <n  | j | ƒ qC W| | d <| r¬ | | d <q¬ n  | ri  }	 i  } g  }
 xÃ | j ƒ  D]µ \ } } |  j | } t | d ƒ } t | d ƒ } | | } | r-t	 | ƒ | | <n  | | } | rPt	 | ƒ |	 | <n  | d | d k rÑ |
 j i | d 6| d d 6ƒ qÑ qÑ W| r²| j
 d i  ƒ } | j | ƒ n  |	 rÚ| j
 d i  ƒ } | j |	 ƒ n  |
 r| j
 d g  ƒ } | j |
 ƒ qn  | r| j ƒ  | d <n  | S(	   s  
        Compare the current group snapshot to the old one and create a
        C{dict} with C{create-groups}, C{delete-groups},
        C{create-group-members} and {delete-group-members} fields.
        Fields without data aren't included in the result.
        t   membersR   s   create-groupss   create-group-memberst   gids   delete-group-memberss   update-groupss   delete-groups(   R    R   R   R$   t   copyt   popt   appendt	   iteritemsR   t   sortedt
   setdefaultR   t   extendt   keys(   R   R!   R&   R'   R(   R   t   create_memberst   valueR)   t   remove_memberst   update_groupst	   groupnamet   new_datat   old_datat   old_memberst   new_memberst   createdt   removed(    (    s:   /usr/lib/python2.7/dist-packages/landscape/user/changes.pyR    T   sV    


(   t   __name__t
   __module__t   __doc__R   R   R   R   R   R"   R   R    (    (    (    s:   /usr/lib/python2.7/dist-packages/landscape/user/changes.pyR      s   						
		N(   t   landscape.diffR    t   objectR   (    (    (    s:   /usr/lib/python2.7/dist-packages/landscape/user/changes.pyt   <module>   s   