
    Dje&                     f   d Z ddl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	dz  def
dZdedee         fdZ	 ddede	dz  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"dS )az  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 blocks until user accepts or declines it).
Creating new items and editing existing ones is possible only in unlocked
collections.
    )Iterator)DBusConnection)SS_PATH	SS_PREFIX)Session)ItemNotFoundExceptionLockedException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dz  ddfdZde	fdZ
dd	Zdd
e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defdZdS )r   zRepresents a collection.N
connectioncollection_pathsessionreturnc                     || _         || _        || _        t          |t          |          | _        | j                            d           d S )NLabel)r   r   r   r   COLLECTION_IFACE_collectionget_property)selfr   r   r   s       S/home/thesage/.hermes/venv/lib/python3.11/site-packages/secretstorage/collection.py__init__zCollection.__init__0   sQ     %.--z; ;%%g.....    c                 P    t          | 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                 L    |                                  rt          d          dS )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_locked?   s/     >> 	;!"9:::	; 	;r    timeoutc                 <    t          | j        | j        g|          S )a  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.

        :raises: ``TimeoutError`` if `timeout` (in seconds) passed
           and the prompt was neither accepted nor dismissed.

        .. versionchanged:: 3.0
           No longer accepts the ``callback`` argument.

        .. versionchanged:: 3.5
           Added ``timeout`` argument.
        )r(   )r   r   r   )r   r(   s     r   unlockzCollection.unlockE   s!      do0D/EwWWWWr    c                     t          t          t          | j                  }|                    dd| j        g           dS )zLocks the collection.LockaoN)r   r   SERVICE_IFACEr   callr   )r   services     r   lockzCollection.lockW   s7    $WmT_MMVTD$8#9:::::r    c                     |                                   | j                            dd          \  }|dk    r)t          | j        |          \  }}|rt          d          dS dS )z/Deletes the collection and all items inside it.Delete /Prompt dismissed.N)r'   r   r/   r   r   r
   )r   prompt	dismissed_results       r   deletezCollection.delete\   sx       "''"55S==!,T_f!E!EIw D./BCCC =D Dr    c              #   ~   K   | j                             d          D ]}t          | j        || j                  V   dS )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_itemse   sS      )66w?? 	A 	AIt	4<@@@@@@	A 	Ar    
attributesc              #      K   | j                             dd|          \  }|D ]}t          | j        || j                  V   dS )zdReturns a generator of items with the given attributes.
        `attributes` should be a dictionary.SearchItemsa{ss}N)r   r/   r   r   r   )r   r?   resultr=   s       r   search_itemszCollection.search_itemsj   s`       "''w
KK 	A 	AIt	4<@@@@@@	A 	Ar    c                 h    | j                             d          }t          |t                    sJ |S )zReturns the collection label.r   )r   r   
isinstancestrr   labels     r   	get_labelzCollection.get_labelq   s3     --g66%%%%%%r    rI   c                 f    |                                   | j                            dd|           dS )z!Sets collection label to `label`.r   sN)r'   r   set_propertyrH   s     r   	set_labelzCollection.set_labelw   s5       %%gsE:::::r    F
text/plainsecretreplacecontent_typec                 
   |                                   | j        st          | j                  | _        t	          | j        ||          }t
          dz   d|ft
          dz   d|fi}| j                            dd|||          \  }}	t          |          dk    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.LabelrL   zItem.AttributesrB   
CreateItemza{sv}(oayays)b   r6   o)r'   r   r   r   r   r   r   r/   lenr   r   r
   )r   rI   r?   rP   rQ   rR   _secret
propertiesr=   r7   r8   rC   	signatures                r   create_itemzCollection.create_item|   s    	   | 	9'88DLflCC$sEl))GZ+@

 !,11
 
	6 y>>ADLAAA'@@	6 	@*+>???%	9CDOY===r    c                 B    d|                                  d| j        dS )Nz<Collection z path=>)rJ   r   r$   s    r   __repr__zCollection.__repr__   s(    Qdnn..QQ8LQQQQr    )r   NN)FrO   )__name__
__module____qualname____doc__DEFAULT_COLLECTIONr   rG   r   r   r#   r%   r'   floatr*   r1   r:   r   r   r>   dictrD   rJ   rN   bytesr[   r^    r    r   r   r   -   s       "" );+// /> /"%/!D./48/ / / /=4 = = = =
; ; ; ;X Xedl Xd X X X X$; ; ; ;
D D D DAx~ A A A A
AtCH~ A(4. A A A A3    ;s ;t ; ; ; ; 49(4> > >$sCx. >!>,0>"%>9=> > > >BR# R R R R R Rr    r4   Nr   rI   aliasr   r   c                 |   |st          |           }t          dz   d|fi}t          t          t          |           }|                    dd||          \  }}t          |          dk    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.LabelrL   CreateCollectionza{sv}srU   )r   r6   rV   )
r   r   r   r   r.   r/   rW   r   r   r
   )r   rI   ri   r   rY   r0   r   r7   r8   rC   rZ   s              r   create_collectionrl      s      +z**003,?J -DDG%ll+=x+5u> >OV
?a*owGGGG#J77Iv <&':;;;!'Ij/7CCCCr    c              #      K   t          t          t          |           }|                    d          D ]}t	          | |          V  dS )z1Returns a generator of all available collections.CollectionsN)r   r   r.   r   r   )r   r0   r   s      r   get_all_collectionsro      sW       -DDG"//>> 6 6_5555556 6r    c                 f    	 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   rl   )r   r   s     r   get_default_collectionrs      sO    L*%%%  L L L Y	7KKKKKLs    00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listro   )r   collectionss     r   get_any_collectionrx      s    *%%%     *&8999    *:6677K =1~#$;<<<s    
7 
AAc                     t          t          t          |           }|                    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`.	ReadAliasrL   rU   zNo collection with such alias.)r   r   r.   r/   rW   r   r   )r   ri   r0   r   s       r   get_collection_by_aliasr{      s_    
 !-DDG||Ke<<O
?q  #$DEEEj/222r    r?   c              #      K   t          t          t          |           }|                    dd|          \  }}||z   D ]}t	          | |          V  dS )zsReturns a generator of items in all collections with the given
    attributes. `attributes` should be a dictionary.rA   rB   N)r   r   r.   r/   r   )r   r?   r0   lockedunlockedr=   s         r   rD   rD      sj       !-DDG||M7JGGFHh& * *	:y))))))* *r    )r4   Nr_   )#rc   collections.abcr   jeepney.io.blockingr   secretstorage.definesr   r   secretstorage.dhcryptor   secretstorage.exceptionsr   r	   r
   secretstorage.itemr   secretstorage.utilr   r   r   r   r   r   r.   rd   ru   r   rG   rl   ro   rs   rx   r{   rf   rD   rh   r    r   <module>r      s    % $ $ $ $ $ . . . . . . 4 4 4 4 4 4 4 4 * * * * * *         
 $ # # # # #              |+ I%? B qR qR qR qR qR qR qR qRh LN04D D. D DS D&~D9CD D D D06N 6x
7K 6 6 6 6 6:L L~ L$+dNL>HL L L L=> =j = = = =.	3 	3#&	3+5	3 	3 	3 	3*^ *!#s(^*08* * * * * *r    