U
    E,W                     @   s   d Z ddlZddlmZmZ ddlmZmZ ddlm	Z	 ddl
mZmZ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dZdd ZdddZdd Zdd Z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.    N)	SS_PREFIXSS_PATH)LockedExceptionItemNotFoundException)Item)bus_get_objectInterfaceWrapperexec_prompt_glibformat_secretopen_session
to_unicodeunlock_objects
CollectionZServicez(/org/freedesktop/secrets/aliases/defaultz+/org/freedesktop/secrets/collection/sessionc                   @   sr   e Zd ZdZedfddZdd Zdd Zdd	d
Zdd Z	dd Z
dd Zdd Zdd Zdd ZdddZdS )r   zRepresents a collection.Nc                 C   sL   t ||}|| _|| _|| _t|t| _t|tj| _	| j	j
tddd d S )NLabelssZ	signature)r   bussessioncollection_pathr   COLLECTION_IFACEcollection_ifacedbusPROPERTIES_IFACEcollection_props_ifaceGet)selfr   r   r   Zcollection_obj r   :/usr/lib/python3/dist-packages/secretstorage/collection.py__init__!   s    

zCollection.__init__c                 C   s   t | jjtdddS )zDReturns :const:`True` if item is locked, otherwise
		:const:`False`.ZLockedr   r   )boolr   r   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. If `callback` is specified,
		calls it when unlocking is complete (see
		:func:`~secretstorage.util.exec_prompt` description for details).
		Otherwise, uses loop from GLib API and returns a boolean
		representing whether the operation was dismissed.)r   r   r   )r   callbackr   r   r   unlock9   s    zCollection.unlockc                 C   s,   t | jt}t|t}|j| jgdd dS )zLocks the collection.Zaor   N)r   r   r   r   SERVICE_IFACEZLockr   )r   service_objservice_ifacer   r   r   lockA   s    
zCollection.lockc                 C   s   |    | jjdd dS )z/Deletes the collection and all items inside it. r   N)r"   r   ZDeleter    r   r   r   deleteG   s    zCollection.deletec                 c   s.   | j jtdddD ]}t| j|| jV  qdS )z3Returns a generator of all items in the collection.ZItemsr   r   N)r   r   r   r   r   r   )r   	item_pathr   r   r   get_all_itemsL   s      
zCollection.get_all_itemsc                 c   s0   | j 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.a{ss}r   N)r   SearchItemsr   r   r   )r   
attributesresultr+   r   r   r   search_itemsR   s
    zCollection.search_itemsc                 C   s   | j jtddd}t|S )zReturns the collection label.r   r   r   )r   r   r   r   r   labelr   r   r   	get_labelZ   s
     zCollection.get_labelc                 C   s    |    | jjtd|dd dS )z!Sets collection label to `label`.r   Zssvr   N)r"   r   Setr   r2   r   r   r   	set_label`   s
    
 zCollection.set_labelF
text/plainc           	      C   sr   |    | jst| j| _t| j||}tj|dd}td |td |i}| j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.r   r   z
Item.LabelzItem.Attributesza{sv}(oayays)b)r"   r   r   r   r
   r   Z
Dictionaryr   r   Z
CreateItemr   )	r   r3   r/   ZsecretreplaceZcontent_type
propertiesZnew_itempromptr   r   r   create_itemf   s         
zCollection.create_item)N)Fr7   )__name__
__module____qualname____doc__DEFAULT_COLLECTIONr   r!   r"   r$   r(   r*   r,   r1   r4   r6   r;   r   r   r   r   r      s   
  r)   c                 C   s   |st | }td |i}t| t}t|t}|j||dd\}}t|dkr\t	| ||dS t
| |\}	}
|	rvtdt	| |
|dS )a  Creates a new :class:`Collection` with the given `label` and `alias`
	and returns it. This action requires prompting. If prompt is dismissed,
	raises :exc:`~secretstorage.exceptions.ItemNotFoundException`. This is
	synchronous function, uses loop from GLib API.zCollection.Labelza{sv}sr      )r   zPrompt dismissed.)r   r   r   r   r   	Interfacer%   ZCreateCollectionlenr   r	   r   )r   r3   aliasr   r9   r&   r'   r   r:   Z	dismissedunlockedr   r   r   create_collection{   s    
 
rF   c                 c   s>   t | t}t|tj}|jtdddD ]}t| |V  q(dS )z1Returns a generator of all available collections.ZCollectionsr   r   N)r   r   r   rB   r   r   r%   r   )r   r&   Zservice_props_ifacer   r   r   r   get_all_collections   s    
 
rG   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   rF   )r   r   r   r   r   get_default_collection   s    
 rI   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listrG   )r   collectionsr   r   r   get_any_collection   s    
rM   c                 C   sB   t | t}t|t}|j|dd}t|dkr8tdt| |S )zReturns the collection with the given `alias`. If there is no
	such collection, raises
	:exc:`~secretstorage.exceptions.ItemNotFoundException`.sr   rA   zNo collection with such alias.)	r   r   r   rB   r%   Z	ReadAliasrC   r   r   )r   rD   r&   r'   r   r   r   r   get_collection_by_alias   s    
rO   c                 c   sF   t | t}t|t}|j|dd\}}|| D ]}t| |V  q0dS )zpReturns a generator of items in all collections with the given
	attributes. `attributes` should be a dictionary.r-   r   N)r   r   r   rB   r%   r.   r   )r   r/   r&   r'   lockedrE   r+   r   r   r   r1      s    

r1   )r)   N)N)r?   r   Zsecretstorage.definesr   r   Zsecretstorage.exceptionsr   r   Zsecretstorage.itemr   Zsecretstorage.utilr   r   r	   r
   r   r   r   r   r%   r@   rJ   objectr   rF   rG   rI   rM   rO   r1   r   r   r   r   <module>   s    $]
	
	