U
    [e                      @   s0  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!eeeee edddZeee dddZ d"eee edddZ!eedddZ"eeedddZ#eeeef ee d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
CollectionZServicez(/org/freedesktop/secrets/aliases/defaultz+/org/freedesktop/secrets/collection/sessionc                   @   s   e Zd ZdZedfeeee ddddZ	e
dddZddd	d
Ze
dddZddddZddddZee dddZeeef ee dddZedddZeddddZd eeeef ee
eed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   Z/var/www/html/services/stratfitenv/lib/python3.8/site-packages/secretstorage/collection.py__init__$   s      zCollection.__init__)r   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.LockZaoN)r   r   SERVICE_IFACEr   callr   )r   servicer   r   r   lockE   s    zCollection.lockc                 C   s@   |    | jdd\}|dkr<t| j|\}}|r<td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.ZItemsN)r   r   r   r   r   )r   	item_pathr   r   r   get_all_itemsS   s    zCollection.get_all_items)
attributesr   c                 c   s2   | 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   r4   resultr2   r   r   r   search_itemsX   s    zCollection.search_itemsc                 C   s   | j d}t|tst|S )zReturns the collection label.r   )r   r   
isinstancestrAssertionErrorr   labelr   r   r   	get_label_   s    zCollection.get_label)r=   r   c                 C   s   |    | jdd| dS )z!Sets collection label to `label`.r   sN)r#   r   Zset_propertyr<   r   r   r   	set_labele   s    zCollection.set_labelF
text/plain)r=   r4   secretreplacecontent_typer   c           
      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.Labelr?   zItem.Attributesr6   Z
CreateItemza{sv}(oayays)b)	r#   r   r   r   r   r   r   r'   r   )
r   r=   r4   rB   rC   rD   Z_secret
propertiesZnew_itemr.   r   r   r   create_itemj   s    	  
  zCollection.create_item)FrA   )__name__
__module____qualname____doc__DEFAULT_COLLECTIONr   r:   r   r   r   r    r"   r#   r$   r)   r1   r   r   r3   r   r8   r>   r@   bytesrF   r   r   r   r   r   !   s2    
	    r+   N)r   r=   aliasr   r   c                 C   s   |st | }td d|fi}ttt| }|dd||\}}t|dkrVt| ||dS t| |\}}	|rpt	d|	\}
}|
dkst
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?   ZCreateCollectionza{sv}s   )r   r-   o)r   r   r   r   r&   r'   lenr   r   r   r;   )r   r=   rM   r   rE   r(   r   r.   r/   r7   	signaturer   r   r   create_collection   s      rR   )r   r   c                 c   s,   t tt| }|dD ]}t| |V  qdS )z1Returns a generator of all available collections.ZCollectionsN)r   r   r&   r   r   )r   r(   r   r   r   r   get_all_collections   s    rS   )r   r   r   c                 C   s2   z
t | W S  tk
r,   t| dd| Y S X dS )zAReturns the default collection. If it doesn't exist,
	creates it.ZDefaultdefaultN)r   r
   rR   )r   r   r   r   r   get_default_collection   s    
 rU   c                 C   sf   z
t | W S  tk
r   Y nX zt | tW S  tk
r@   Y nX tt| }|rZ|d S tddS )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.N)r   r
   SESSION_COLLECTIONlistrS   )r   collectionsr   r   r   get_any_collection   s    
rY   )r   rM   r   c                 C   s:   t tt| }|dd|\}t|dkr0tdt| |S )zReturns the collection with the given `alias`. If there is no
	such collection, raises
	:exc:`~secretstorage.exceptions.ItemNotFoundException`.Z	ReadAliasr?   rN   zNo collection with such alias.)r   r   r&   r'   rP   r
   r   )r   rM   r(   r   r   r   r   get_collection_by_alias   s
    rZ   )r   r4   r   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.r5   r6   N)r   r   r&   r'   r   )r   r4   r(   lockedunlockedr2   r   r   r   r8      s    r8   )r+   N)N)%rJ   typingr   r   r   Zjeepney.io.blockingr   Zsecretstorage.definesr   r   Zsecretstorage.dhcryptor   Zsecretstorage.exceptionsr	   r
   r   Zsecretstorage.itemr   Zsecretstorage.utilr   r   r   r   r   r   r&   rK   rV   objectr   r:   rR   rS   rU   rY   rZ   r8   r   r   r   r   <module>   sB   ^     
 
 