o
    j­_Ø   ã                   @   sN  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ed
 ZdZdZG dd	„ d	eƒZ		d"dedededee def
dd„Zdedee fdd„Z 	d#dedee 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 )$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                   @   sî   e Zd ZdZedfdededee 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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   ú:/usr/lib/python3/dist-packages/secretstorage/collection.pyÚ__init__$   s   ÿzCollection.__init__c                 C   s   t | j d¡ƒS )zDReturns :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 )zSIf collection is locked, raises
		:exc:`~secretstorage.exceptions.LockedException`.zCollection is locked!N)r#   r	   r"   r   r   r   Úensure_not_locked3   s   ÿzCollection.ensure_not_lockedc                 C   s   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.

		.. versionchanged:: 3.0
		   No longer accepts the ``callback`` argument.
		)r   r   r   r"   r   r   r   Úunlock9   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   ÚlockE   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   ÚdeleteJ   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_itemsS   s   €ÿzCollection.get_all_itemsÚ
attributesc                 c   s4   � | j  dd|¡\}|D ]}t| j|| jƒV  qdS )z^Returns a generator of items with the given attributes.
		`attributes` should be a dictionary.ÚSearchItemsúa{ss}N)r   r)   r   r   r   )r   r7   Úresultr5   r   r   r   Úsearch_itemsX   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_label_   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_labele   s   zCollection.set_labelFú
text/plainÚsecretÚreplaceÚcontent_typec           
      C   sl   |   ¡  | jst| jƒ| _t| j||ƒ}td d|ftd d|fi}| j dd|||¡\}}	t| j|| jƒS )ac  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.LabelrA   zItem.Attributesr9   Ú
CreateItemza{sv}(oayays)b)	r$   r   r   r   r   r   r   r)   r   )
r   r?   r7   rE   rF   rG   Ú_secretÚ
propertiesÚnew_itemr0   r   r   r   Úcreate_itemj   s   	þ
ÿzCollection.create_item)r   N)FrD   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚDEFAULT_COLLECTIONr   r=   r   r   r   r!   r#   r$   r%   r+   r3   r   r   r6   r   r;   r@   rC   ÚbytesrL   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 )	zâCreates 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.LabelrA   ÚCreateCollectionza{sv}sé   )r   r/   Úo)
r   r   r   r   r(   r)   Úlenr   r   r   )r   r?   rS   r   rJ   r*   r   r0   r1   r:   Ú	signaturer   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 )zAReturns 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ƒ‚)z»Returns 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 )z�Returns the collection with the given `alias`. If there is no
	such collection, raises
	:exc:`~secretstorage.exceptions.ItemNotFoundException`.Ú	ReadAliasrA   rU   zNo collection with such alias.)r   r   r(   r)   rW   r
   r   )r   rS   r*   r   r   r   r   Úget_collection_by_alias¼   s
   
rd   r7   c                 c   s>   � t tt| ƒ}| dd|¡\}}|| D ]}t| |ƒV  qdS )zpReturns a generator of items in all collections with the given
	attributes. `attributes` should be a dictionary.r8   r9   N)r   r   r(   r)   r   )r   r7   r*   ÚlockedÚunlockedr5   r   r   r   r;   Ç   s   €ÿr;   )r-   N)N)%rP   Ú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(   rQ   r_   Úobjectr   r=   rY   r[   r^   rb   rd   r;   r   r   r   r   Ú<module>   sL   ^ÿÿ
ÿÿÿ
ÿ
ÿ
ÿ
ÿÿ