
    ҙie&                     L   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"y)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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       T/mnt/e/genesis-system/.venv/lib/python3.12/site-packages/secretstorage/collection.py__init__zCollection.__init__0   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_locked?   s     >>!"9:: r    timeoutc                 H    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       doo0D0D/EwWWr    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.lockW   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   r/   r   r   r
   )r   prompt	dismissed_results       r   deletezCollection.delete\   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_itemse   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   r/   r   r   r   )r   r@   resultr=   s       r   search_itemszCollection.search_itemsj   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_labelq   s-      --g6%%%%r    rJ   c                 ^    | j                          | j                  j                  dd|       y)z!Sets collection label to `label`.r   sN)r'   r   set_propertyrI   s     r   	set_labelzCollection.set_labelw   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.LabelrM   zItem.AttributesrC   
CreateItemza{sv}(oayays)b   r6   o)r'   r   r   r   r   r   r   r/   lenr   r   r
   )r   rJ   r@   rP   rQ   rR   _secret
propertiesr=   r7   r8   rD   	signatures                r   create_itemzCollection.create_item|   s    	 ||'8DLflC$sEl))GZ+@

 !,,11
	6 y>ADLLAA'@	6*+>??%	9CDOOY==r    c                 D    d| j                         d| j                  dS )Nz<Collection z path=>)rK   r   r$   s    r   __repr__zCollection.__repr__   s&    dnn.18L8L7OqQQr    )r   NN)Fz
text/plain)__name__
__module____qualname____doc__DEFAULT_COLLECTIONr   rH   r   r   r#   r%   r'   floatr*   r1   r:   r   r   r>   dictrE   rK   rO   bytesr[   r^    r    r   r   r   -   s   " );+//> /"%/!D./48/=4 =
;Xedl Xd X$;
DAx~ A
AtCH~ A(4. A3 ;s ;t ; 49(4> >$sCx. >!>,0>"%>9=>BR# Rr    Nr   rJ   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.LabelrM   CreateCollectionza{sv}srU   )r   r6   rV   )
r   r   r   r   r.   r/   rW   r   r   r
   )r   rJ   ri   r   rY   r0   r   r7   r8   rD   rZ   s              r   create_collectionrl      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   r0   r   s      r   get_all_collectionsro      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   rl   )r   r   s     r   get_default_collectionrs      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listro   )r   collectionss     r   get_any_collectionrx      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`.	ReadAliasrM   rU   zNo collection with such alias.)r   r   r.   r/   rW   r   r   )r   ri   r0   r   s       r   get_collection_by_aliasr{      sM    
 !-DG||Ke<O
?q #$DEEj/22r    r@   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.rB   rC   N)r   r   r.   r/   r   )r   r@   r0   lockedunlockedr=   s         r   rE   rE      sN      !-DG||M7J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   rH   rl   ro   rs   rx   r{   rf   rE   rh   r    r   <module>r      s9   % . 4 * 
 $  |+ I%? B qR qRh LN04D. D DS D&~D9CD06N 6x
7K 6 6:L~ L$+dNL>HL=> =j =.	3 	3#&	3+5	3*^ *!#s(^*08*r    