o
    LDi                     @   sp   d dl mZ d dlmZmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZmZmZmZmZ G dd deZd	S )
    )Mapping)AnycastAsyncIOMotorClient)DataNotDictLikeError)State)BaseStorageDefaultKeyBuilder
KeyBuilder	StateType
StorageKeyc                   @   s  e Zd ZdZ			d#dededB deded	df
d
dZe	d$dede	ee
f dB de
d	d fddZd%ddZded	edB fddZd$deded	dfddZded	edB fddZdedeee
f d	dfddZded	e	ee
f fdd Zdedeee
f d	e	ee
f fd!d"ZdS )&MongoStoragez

    .. warning::
        DEPRECATED: Use :class:`PyMongoStorage` instead.
        This class will be removed in future versions.


    MongoDB storage required :code:`motor` package installed (:code:`pip install motor`)
    Naiogram_fsmstates_and_dataclientkey_builderdb_namecollection_namereturnc                 C   s6   |du rt  }|| _| j| | _| j| | _|| _dS )a&  
        :param client: Instance of AsyncIOMotorClient
        :param key_builder: builder that helps to convert contextual key to string
        :param db_name: name of the MongoDB database for FSM
        :param collection_name: name of the collection for storing FSM states and data
        N)r
   _client	_database_collection_key_builder)selfr   r   r   r    r   e/var/www/www-root/data/www/77.83.87.30/venv/lib/python3.10/site-packages/aiogram/fsm/storage/mongo.py__init__   s   
zMongoStorage.__init__urlconnection_kwargskwargsc                 K   s.   |du ri }t |fi |}| dd|i|S )ad  
        Create an instance of :class:`MongoStorage` with specifying the connection string

        :param url: for example :code:`mongodb://user:password@host:port`
        :param connection_kwargs: see :code:`motor` docs
        :param kwargs: arguments to be passed to :class:`MongoStorage`
        :return: an instance of :class:`MongoStorage`
        Nr   r   r   )clsr   r   r    r   r   r   r   from_url0   s   zMongoStorage.from_urlc                    s   | j   dS )z5Cleanup client resources and disconnect from MongoDB.N)r   close)r   r   r   r   r#   D   s   zMongoStorage.closevaluec                 C   s$   |d u rd S t |tr|jS t|S N)
isinstancer   statestr)r   r$   r   r   r   resolve_stateH   s
   
zMongoStorage.resolve_statekeyr'   c                    s   | j |}|d u r3| jjd|idddiiddiddI d H }|i kr1| jd|iI d H  d S d S | jjd|idd| |iidd	I d H  d S )
N_id$unsetr'      r   Tfilterupdate
projectionreturn_document$setr/   r0   upsert)r   buildr   find_one_and_update
delete_one
update_oner)   )r   r*   r'   document_idupdatedr   r   r   	set_stateO   s"   
zMongoStorage.set_statec                    s:   | j |}| jd|iI d H }|d u rd S |dS )Nr+   r'   )r   r6   r   find_onegetr   r*   r:   documentr   r   r   	get_statea   s   
zMongoStorage.get_statedatac                    s   t |tsdt|j }t|| j|}|sB| jjd|idddiiddiddI d H }|i kr@| j	d|iI d H  d S d S | jj
d|id	d|iidd
I d H  d S )Nz-Data must be a dict or dict-like object, got r+   r,   rB   r-   r   Tr.   r3   r4   )r&   dicttype__name__r   r   r6   r   r7   r8   r9   )r   r*   rB   msgr:   r;   r   r   r   set_datah   s(   


zMongoStorage.set_datac                    sP   | j |}| jd|iI d H }|d u s|dsi S ttttf |d S )Nr+   rB   )	r   r6   r   r=   r>   r   rC   r(   r   r?   r   r   r   get_data~   s   zMongoStorage.get_datac                    sn   | j |}dd | D }| jjd|id|iddddidI d H }|s1| jd|iI d H  |di S )	Nc                 S   s   i | ]
\}}d | |qS )zdata.r   ).0r*   r$   r   r   r   
<dictcomp>   s    z,MongoStorage.update_data.<locals>.<dictcomp>r+   r3   Tr   )r/   r0   r5   r2   r1   rB   )r   r6   itemsr   r7   r8   r>   )r   r*   rB   r:   update_withupdate_resultr   r   r   update_data   s   zMongoStorage.update_data)Nr   r   r%   )r   N)rE   
__module____qualname____doc__r   r   r(   r   classmethodrC   r   r"   r#   r   r)   r   r<   rA   r   rG   rH   rN   r   r   r   r   r      sD    

*r   N)collections.abcr   typingr   r   motor.motor_asyncior   aiogram.exceptionsr   aiogram.fsm.stater   aiogram.fsm.storage.baser	   r
   r   r   r   r   r   r   r   r   <module>   s    	