o
    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Ú
CollectionÚServicez(/org/freedesktop/secrets/aliases/defaultz+/org/freedesktop/secrets/collection/sessionc                   @   sv   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©Ú	signature)r   ÚbusÚsessionÚcollection_pathr   ÚCOLLECTION_IFACEÚcollection_ifaceÚdbusÚPROPERTIES_IFACEÚcollection_props_ifaceÚGet)Úselfr   r   r   Úcollection_obj© r   úJ/var/www/html/env/lib/python3.10/site-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`.Ú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.Úaor   N)r   r   r   r   ÚSERVICE_IFACEÚ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   ÚDeleter$   r   r   r    ÚdeleteG   s   zCollection.deletec                 c   s0   � | j jtddd�D ]}t| j|| jƒV  q
dS )z3Returns a generator of all items in the collection.ÚItemsr   r   N)r   r   r   r   r   r   )r   Ú	item_pathr   r   r    Úget_all_itemsL   s   €
ÿþzCollection.get_all_itemsc                 c   s2   � | 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Úresultr3   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   Ússvr   N)r&   r   ÚSetr   r:   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   Ú
Dictionaryr   r   Ú
CreateItemr   )	r   r;   r7   ÚsecretÚreplaceÚcontent_typeÚ
propertiesÚnew_itemÚpromptr   r   r    Úcreate_itemf   s   þ
ÿzCollection.create_item©N)Fr@   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚDEFAULT_COLLECTIONr!   r%   r&   r(   r.   r1   r4   r9   r<   r?   rI   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
| |ƒ\}	}
|	r;t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*   ÚCreateCollectionÚlenr   r	   r   )r   r;   Úaliasr   rF   r,   r-   r   rH   Ú	dismissedÚunlockedr   r   r    Úcreate_collection{   s   

ÿrW   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.ÚCollectionsr   r   N)r   r   r   rQ   r   r   r*   r   )r   r,   Úservice_props_ifacer   r   r   r    Úget_all_collectionsŽ   s   €
ÿ
ÿþrZ   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   rW   )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ÚlistrZ   )r   Úcollectionsr   r   r    Úget_any_collection    s   
ÿÿra   c                 C   sB   t | tƒ}t |t¡}|j|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`.Úsr   rP   zNo collection with such alias.)	r   r   r   rQ   r*   Ú	ReadAliasrS   r   r   )r   rT   r,   r-   r   r   r   r    Úget_collection_by_alias¶   s   

rd   c                 c   sH   � t | tƒ}t |t¡}|j|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   r   N)r   r   r   rQ   r*   r6   r   )r   r7   r,   r-   ÚlockedrV   r3   r   r   r    r9   Á   s   €

ÿÿr9   )r/   NrJ   )rN   r   Úsecretstorage.definesr   r   Úsecretstorage.exceptionsr   r   Úsecretstorage.itemr   Úsecretstorage.utilr   r   r	   r
   r   r   r   r   r*   rO   r^   Úobjectr   rW   rZ   r]   ra   rd   r9   r   r   r   r    Ú<module>   s"   $
]
		