
    b$                     T   d Z ddlmZmZmZ ddlmZ ddlmZm	Z	 ddl
mZ ddlmZmZmZ ddlmZ ddlmZmZmZmZmZ ed	z   Zed
z   ZdZdZ G d d	      Z	 	 ddedededee   def
dZdedee   fdZ	 ddedee   defdZ dedefdZ!dededefdZ"dedeeef   dee   fdZ#y)ar  Collection is a place where secret items are stored. Normally, only
the default collection should be used, but this module allows to use any
registered collection. Use :func:`get_default_collection` to get the
default collection (and create it, if necessary).

Collections are usually automatically unlocked when user logs in, but
collections can also be locked and unlocked using
:meth:`Collection.lock` and :meth:`Collection.unlock` methods (unlocking
requires showing the unlocking prompt to user and can be synchronous or
asynchronous). Creating new items and editing existing ones is possible
only in unlocked collection.    )DictIteratorOptional)DBusConnection)	SS_PREFIXSS_PATH)Session)LockedExceptionItemNotFoundExceptionPromptDismissedException)Item)DBusAddressWrapperexec_promptformat_secretopen_sessionunlock_objects
CollectionServicez(/org/freedesktop/secrets/aliases/defaultz+/org/freedesktop/secrets/collection/sessionc                       e Zd ZdZedfdededee   ddfdZ	de
fdZdd	Zde
fd
ZddZddZdee   fdZdeeef   dee   fdZdefdZdeddfdZ	 	 ddedeeef   dede
dedefdZy)r   zRepresents a collection.N
connectioncollection_pathsessionreturnc                     || _         || _        || _        t        |t        |      | _        | j
                  j                  d       y )NLabel)r   r   r   r   COLLECTION_IFACE_collectionget_property)selfr   r   r   s       :/usr/lib/python3/dist-packages/secretstorage/collection.py__init__zCollection.__init__%   sC     %.--z;%%g.    c                 J    t        | j                  j                  d            S )zJReturns :const:`True` if item is locked, otherwise
        :const:`False`.Locked)boolr   r   r   s    r    	is_lockedzCollection.is_locked/   s      D$$11(;<<r"   c                 :    | j                         rt        d      y)zYIf collection is locked, raises
        :exc:`~secretstorage.exceptions.LockedException`.zCollection is locked!N)r'   r
   r&   s    r    ensure_not_lockedzCollection.ensure_not_locked4   s     >>!"9:: r"   c                 D    t        | j                  | j                  g      S )aA  Requests unlocking the collection.

        Returns a boolean representing whether the prompt has been
        dismissed; that means :const:`False` on successful unlocking
        and :const:`True` if it has been dismissed.

        .. versionchanged:: 3.0
           No longer accepts the ``callback`` argument.
        )r   r   r   r&   s    r    unlockzCollection.unlock:   s     doo0D0D/EFFr"   c                 ~    t        t        t        | j                        }|j	                  dd| j
                  g       y)zLocks the collection.LockaoN)r   r   SERVICE_IFACEr   callr   )r   services     r    lockzCollection.lockF   s-    $WmT__MVTD$8$8#9:r"   c                     | j                          | j                  j                  dd      \  }|dk7  r't        | j                  |      \  }}|rt        d      yy)z/Deletes the collection and all items inside it.Delete /Prompt dismissed.N)r)   r   r0   r   r   r   )r   prompt	dismissed_results       r    deletezCollection.deleteK   s]     ""''"5S=!,T__f!EIw./BCC  r"   c              #      K   | j                   j                  d      D ]%  }t        | j                  || j                         ' yw)z3Returns a generator of all items in the collection.ItemsN)r   r   r   r   r   )r   	item_paths     r    get_all_itemszCollection.get_all_itemsT   s?     ))66w? 	AIt	4<<@@	A   AA
attributesc              #      K   | j                   j                  dd|      \  }|D ]%  }t        | j                  || j                         ' yw)zdReturns a generator of items with the given attributes.
        `attributes` should be a dictionary.SearchItemsa{ss}N)r   r0   r   r   r   )r   rA   resultr>   s       r    search_itemszCollection.search_itemsY   sL      ""''w
K 	AIt	4<<@@	As   AAc                 `    | j                   j                  d      }t        |t              sJ |S )zReturns the collection label.r   )r   r   
isinstancestrr   labels     r    	get_labelzCollection.get_label`   s-      --g6%%%%r"   rK   c                 ^    | j                          | j                  j                  dd|       y)z!Sets collection label to `label`.r   sN)r)   r   set_propertyrJ   s     r    	set_labelzCollection.set_labelf   s&     %%gsE:r"   secretreplacecontent_typec                    | j                          | j                  st        | j                        | _        t	        | j                  ||      }t
        dz   d|ft
        dz   d|fi}| j                  j                  dd|||      \  }}	t        |      dkD  r!t        | j                  || j                        S t        | j                  |	      \  }
}|
rt        d      |\  }}|d	k(  sJ t        | j                  || j                        S )
a  Creates a new :class:`~secretstorage.item.Item` with given
        `label` (unicode string), `attributes` (dictionary) and `secret`
        (bytestring). If `replace` is :const:`True`, replaces the existing
        item with the same attributes. If `content_type` is given, also
        sets the content type of the secret (``text/plain`` by default).
        Returns the created item.z
Item.LabelrN   zItem.AttributesrD   
CreateItemza{sv}(oayays)b   r7   o)r)   r   r   r   r   r   r   r0   lenr   r   r   )r   rK   rA   rQ   rR   rS   _secret
propertiesr>   r8   r9   rE   	signatures                r    create_itemzCollection.create_itemk   s    	 ||'8DLflC$sEl))GZ+@

 !,,11
	6 y>ADLLAA'@	6*+>??%	9CDOOY==r"   )r   N)Fz
text/plain)__name__
__module____qualname____doc__DEFAULT_COLLECTIONr   rI   r   r	   r!   r%   r'   r)   r+   r2   r;   r   r   r?   r   rF   rL   rP   bytesr\    r"   r    r   r   "   s    " );.2/> /"%/"7+/7;/=4 =
;
G 
G;
DAx~ A
AtCH~ A(4. A3 ;s ;t ; 49(4> >$sCx. >!>,0>"%>9=>r"   Nr   rK   aliasr   r   c                 2   |st        |       }t        dz   d|fi}t        t        t        |       }|j                  dd||      \  }}t        |      dkD  rt        | ||      S t        | |      \  }}	|rt        d      |	\  }
}|
dk(  sJ t        | ||      S )	zCreates a new :class:`Collection` with the given `label` and `alias`
    and returns it. This action requires prompting.

    :raises: :exc:`~secretstorage.exceptions.PromptDismissedException`
             if the prompt is dismissed.
    zCollection.LabelrN   CreateCollectionza{sv}srV   )r   r7   rW   )
r   r   r   r   r/   r0   rX   r   r   r   )r   rK   rd   r   rZ   r1   r   r8   r9   rE   r[   s              r    create_collectionrg      s     z*003,?J -DG%ll+=x+5u>OV
?a*owGG#J7Iv&':;;!'Ij/7CCr"   c              #   ~   K   t        t        t        |       }|j                  d      D ]  }t	        | |        yw)z1Returns a generator of all available collections.CollectionsN)r   r   r/   r   r   )r   r1   r   s      r    get_all_collectionsrj      s;      -DG"//> 6_556s   ;=c                 T    	 t        |       S # t        $ r t        | dd|      cY S w xY w)zDReturns the default collection. If it doesn't exist,
    creates it.Defaultdefault)r   r   rg   )r   r   s     r    get_default_collectionrn      s5    L*%%  L Y	7KKLs   
 ''c                     	 t        |       S # t        $ r Y nw xY w	 t        | t              S # t        $ r Y nw xY wt        t	        |             }|r|d   S t        d      )zReturns any collection, in the following order of preference:

    - The default collection;
    - The "session" collection (usually temporary);
    - The first collection in the collections list.r   zNo collections found.)r   r   SESSION_COLLECTIONlistrj   )r   collectionss     r    get_any_collectionrs      st    *%%   *&899  *:67K1~#$;<<s   
 	- 	99c                     t        t        t        |       }|j                  dd|      \  }t	        |      dk  rt        d      t        | |      S )zReturns the collection with the given `alias`. If there is no
    such collection, raises
    :exc:`~secretstorage.exceptions.ItemNotFoundException`.	ReadAliasrN   rV   zNo collection with such alias.)r   r   r/   r0   rX   r   r   )r   rd   r1   r   s       r    get_collection_by_aliasrv      sM    
 !-DG||Ke<O
?q #$DEEj/22r"   rA   c              #      K   t        t        t        |       }|j                  dd|      \  }}||z   D ]  }t	        | |        yw)zsReturns a generator of items in all collections with the given
    attributes. `attributes` should be a dictionary.rC   rD   N)r   r   r/   r0   r   )r   rA   r1   lockedunlockedr>   s         r    rF   rF      sN      !-DG||M7JGFHh& *	:y))*r@   )r5   N)N)$r`   typingr   r   r   jeepney.io.blockingr   secretstorage.definesr   r   secretstorage.dhcryptor	   secretstorage.exceptionsr
   r   r   secretstorage.itemr   secretstorage.utilr   r   r   r   r   r   r/   ra   rp   r   rI   rg   rj   rn   rs   rv   rF   rc   r"   r    <module>r      s:  
  , + . 4 *  #- - |+ I%? B h> h>V LN37D. D DS D'0D<FD06N 6x
7K 6 9=L~ L$,W$5LAKL=> =j =.	3 	3#&	3+5	3*^ *!#s(^*08*r"   