o
    il%                     @   sD  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ed
 ZdZdZG dd	 d	Z		d"dededede	dB def
ddZdedee fddZ	d#dede	dB defddZdedefddZdededefddZ dede!eef dee fd 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                   @   s   e Zd ZdZedfdedededB ddfddZde	fd	d
Z
d&ddZde	fddZd&ddZd&ddZdee fddZdeeef dee fddZdefddZdeddfddZ		d'dedeeef ded e	d!edefd"d#Zdefd$d%ZdS )(r   zRepresents a collection.N
connectioncollection_pathsessionreturnc                 C   s0   || _ || _|| _t|t|| _| jd d S )NLabel)r   r   r   r   COLLECTION_IFACE_collectionget_property)selfr   r   r    r   ^/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/secretstorage/collection.py__init__0   s   zCollection.__init__c                 C   s   t | jdS )zJReturns :const:`True` if item is locked, otherwise
        :const:`False`.Locked)boolr   r   r   r   r   r   	is_locked:   s   zCollection.is_lockedc                 C   s   |   rtddS )zYIf collection is locked, raises
        :exc:`~secretstorage.exceptions.LockedException`.zCollection is locked!N)r!   r   r    r   r   r   ensure_not_locked?   s   zCollection.ensure_not_lockedc                 C   s   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    r   r   r   unlockE   s   
zCollection.unlockc                 C   s$   t tt| j}|dd| jg dS )zLocks the collection.LockaoN)r   r   SERVICE_IFACEr   callr   )r   servicer   r   r   lockQ   s   zCollection.lockc                 C   sD   |    | 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_resultr   r   r   deleteV   s   zCollection.deletec                 c   s*    | j dD ]}t| j|| jV  qdS )z3Returns a generator of all items in the collection.ItemsN)r   r   r
   r   r   )r   	item_pathr   r   r   get_all_items_   s   zCollection.get_all_items
attributesc                 c   s4    | j dd|\}|D ]}t| j|| jV  q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   r5   resultr3   r   r   r   search_itemsd   s
   zCollection.search_itemsc                 C   s   | j d}t|tsJ |S )zReturns the collection label.r   )r   r   
isinstancestrr   labelr   r   r   	get_labelk   s   zCollection.get_labelr=   c                 C   s   |    | jdd| dS )z!Sets collection label to `label`.r   sN)r"   r   set_propertyr<   r   r   r   	set_labelq   s   zCollection.set_labelF
text/plainsecretreplacecontent_typec                 C   s   |    | jst| j| _t| j||}td d|ftd d|fi}| jdd|||\}}	t|dkr<t	| j|| jS t
| j|	\}
}|
rJtd|\}}|d	ksTJ 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.Labelr?   zItem.Attributesr7   
CreateItemza{sv}(oayays)b   r-   o)r"   r   r   r   r   r   r   r'   lenr
   r   r	   )r   r=   r5   rC   rD   rE   _secret
propertiesr3   r.   r/   r8   	signaturer   r   r   create_itemv   s,   	zCollection.create_itemc                 C   s   d|   d| jdS )Nz<Collection z path=>)r>   r   r    r   r   r   __repr__   s   zCollection.__repr__)r   N)FrB   )__name__
__module____qualname____doc__DEFAULT_COLLECTIONr   r;   r   r   r   r!   r"   r#   r)   r1   r   r
   r4   dictr9   r>   rA   bytesrM   rO   r   r   r   r   r   -   s@    




	
!r+   Nr   r=   aliasr   r   c                 C   s   |st | }td d|fi}ttt| }|dd||\}}t|dkr+t| ||dS t| |\}}	|r8t	d|	\}
}|
dksBJ t| ||d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.Labelr?   CreateCollectionza{sv}srG   )r   r-   rH   )
r   r   r   r   r&   r'   rI   r   r   r	   )r   r=   rW   r   rK   r(   r   r.   r/   r8   rL   r   r   r   create_collection   s   rY   c                 c   s.    t tt| }|dD ]}t| |V  qdS )z1Returns a generator of all available collections.CollectionsN)r   r   r&   r   r   )r   r(   r   r   r   r   get_all_collections   s
   r[   c                 C   s,   zt | W S  ty   t| dd| Y S w )zDReturns the default collection. If it doesn't exist,
    creates it.Defaultdefault)r   r   rY   )r   r   r   r   r   get_default_collection   s
   
r^   c                 C   s^   zt | W S  ty   Y nw zt | tW S  ty   Y n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listr[   )r   collectionsr   r   r   get_any_collection   s   
rb   c                 C   s:   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`.	ReadAliasr?   rG   zNo collection with such alias.)r   r   r&   r'   rI   r   r   )r   rW   r(   r   r   r   r   get_collection_by_alias   s
   
rd   r5   c                 c   s>    t tt| }|dd|\}}|| D ]}t| |V  qdS )zsReturns a generator of items in all collections with the given
    attributes. `attributes` should be a dictionary.r6   r7   N)r   r   r&   r'   r
   )r   r5   r(   lockedunlockedr3   r   r   r   r9      s   r9   )r+   N)N)#rS   collections.abcr   jeepney.io.blockingr   secretstorage.definesr   r   secretstorage.dhcryptor   secretstorage.exceptionsr   r   r	   secretstorage.itemr
   secretstorage.utilr   r   r   r   r   r   r&   rT   r_   r   r;   rY   r[   r^   rb   rd   rU   r9   r   r   r   r   <module>   sL   n




