L iJ dZddlZddlmZddlmZmZmZmZddl m Z m Z ddl m Z mZmZerddlmZGdd e eeefeeefeeeffZy) z/This module contains the DictPersistence class.N)deepcopy) TYPE_CHECKINGAnyOptionalcast)BasePersistencePersistenceInput)CDCDataConversationDictConversationKey)JSONDictceZdZdZdZ d5deededededed ed effd Z e d ee e e e e fffd Ze d efdZe d ee e e e e fffdZe d efdZe d ee e e ffdZe d efdZe d eefdZe d efdZe d ee eeffdZe d efdZd e e e eefffdZd e e e eefffdZd e eeffdZd eefdZded efdZdede deed dfdZ!d e d!e e e fd dfd"Z"d#e d!e e e fd dfd$Z#d!e e e fd dfd%Z$d!ed dfd&Z%d#e d dfd'Z&d e d dfd(Z'd e d)e e e fd dfd*Z(d#e d+e e e fd dfd,Z)d-e e e fd dfd.Z*d6d/Z+e,d0e eefd efd1Z-e,d2ed e eeffd3Z.e,d!ed e e e eefffd4Z/xZ0S)7DictPersistenceay Using Python's :obj:`dict` and :mod:`json` for making your bot persistent. Attention: The interface provided by this class is intended to be accessed exclusively by :class:`~telegram.ext.Application`. Calling any of the methods below manually might interfere with the integration of persistence into :class:`~telegram.ext.Application`. Note: * Data managed by :class:`DictPersistence` is in-memory only and will be lost when the bot shuts down. This is, because :class:`DictPersistence` is mainly intended as starting point for custom persistence classes that need to JSON-serialize the stored data before writing them to file/database. * This implementation of :class:`BasePersistence` does not handle data that cannot be serialized by :func:`json.dumps`. .. seealso:: :wiki:`Making Your Bot Persistent ` .. versionchanged:: 20.0 The parameters and attributes ``store_*_data`` were replaced by :attr:`store_data`. Args: store_data (:class:`~telegram.ext.PersistenceInput`, optional): Specifies which kinds of data will be saved by this persistence instance. By default, all available kinds of data will be saved. user_data_json (:obj:`str`, optional): JSON string that will be used to reconstruct user_data on creating this persistence. Default is ``""``. chat_data_json (:obj:`str`, optional): JSON string that will be used to reconstruct chat_data on creating this persistence. Default is ``""``. bot_data_json (:obj:`str`, optional): JSON string that will be used to reconstruct bot_data on creating this persistence. Default is ``""``. conversations_json (:obj:`str`, optional): JSON string that will be used to reconstruct conversation on creating this persistence. Default is ``""``. callback_data_json (:obj:`str`, optional): JSON string that will be used to reconstruct callback_data on creating this persistence. Default is ``""``. .. versionadded:: 13.6 update_interval (:obj:`int` | :obj:`float`, optional): The :class:`~telegram.ext.Application` will update the persistence in regular intervals. This parameter specifies the time (in seconds) to wait between two consecutive runs of updating the persistence. Defaults to 60 seconds. .. versionadded:: 20.0 Attributes: store_data (:class:`~telegram.ext.PersistenceInput`): Specifies which kinds of data will be saved by this persistence instance. ) _bot_data_bot_data_json_callback_data_callback_data_json _chat_data_chat_data_json_conversations_conversations_json _user_data_user_data_jsonN store_datauser_data_jsonchat_data_json bot_data_jsonconversations_jsoncallback_data_jsonupdate_intervalc *t |||d|_d|_d|_d|_d|_d|_d|_d|_ d|_ d|_ |r |j||_||_|r |j||_||_|rG t!j"||_||_ t%|jt&s td|r t!j"|} | d|_n;t)d| dD cgc]\} } } | t+| | fc} } } | d f|_||_ |j Gt/d |j dDrt%|j d t&s td |r |j1||_||_ yy#ttf$r}td|d}~wwxYw#ttf$r}td|d}~wwxYw#ttf$r}td|d}~wwxYw#ttf$r}td|d}~wwxYwcc} } } w#tt,f$r}td |d}~wwxYw#ttf$r}td |d}~wwxYw) N)rr z4Unable to deserialize user_data_json. Not valid JSONz4Unable to deserialize chat_data_json. Not valid JSONz3Unable to deserialize bot_data_json. Not valid JSONz%bot_data_json must be serialized dictz8Unable to deserialize callback_data_json. Not valid JSONr rz0callback_data_json is not in the required formatc3lK|],}t|dtxrt|dt.yw)rN) isinstancedictstr).0entrys c/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/telegram/ext/_dictpersistence.py z+DictPersistence.__init__..s6uQx.L:eAh3LL24z8Unable to deserialize conversations_json. Not valid JSON)super__init__rrrrrrrrrr _decode_user_chat_data_from_json ValueErrorAttributeError TypeErrorjsonloadsr%r&rfloat IndexErrorall_decode_conversations_from_json)selfrrrrrrr excdataonetwothree __class__s r*r.zDictPersistence.__init__^s JP"".2.2-126 26  a"&"G"G"W'5$  a"&"G"G"W'5$  `!%M!:&3#dnnd3 GHH  zz"45 ]<*.D'*.!JNq'RRsC3c E2RTXYZT[\+D',>("".!%!4!4Q!7"$"5"5a"8$? RSS  &*&J&JK]&^#+=( [/ a VW]`` a / a VW]`` a / ` UV\__ `/ N S + ] RSY\\ ]/ N sF<<G!H%H%;II-II2<G GGG?. G::G?H" HH"%I4 IIII/ I**I/2J J  Jreturnc|jS)z%:obj:`dict`: The user_data as a dict.)rr9s r* user_datazDictPersistence.user_datacp|jr |jStj|jS)z6:obj:`str`: The user_data serialized as a JSON-string.)rr3dumpsrCrBs r*rzDictPersistence.user_data_json,   '' 'zz$..))rEc|jS)z%:obj:`dict`: The chat_data as a dict.)rrBs r* chat_datazDictPersistence.chat_datarDrEcp|jr |jStj|jS)z6:obj:`str`: The chat_data serialized as a JSON-string.)rr3rGrJrBs r*rzDictPersistence.chat_data_jsonrHrEc|jS)z$:obj:`dict`: The bot_data as a dict.)rrBs r*bot_datazDictPersistence.bot_datas~~rEcp|jr |jStj|jS)z5:obj:`str`: The bot_data serialized as a JSON-string.)rr3rGrMrBs r*rzDictPersistence.bot_data_jsons,   && &zz$--((rEc|jS)ztuple[list[tuple[:obj:`str`, :obj:`float`, dict[:obj:`str`, :class:`object`]]], dict[:obj:`str`, :obj:`str`]]: The metadata on the stored callback data. .. versionadded:: 13.6 )rrBs r* callback_datazDictPersistence.callback_datas"""rEcp|jr |jStj|jS)zo:obj:`str`: The metadata on the stored callback data as a JSON-string. .. versionadded:: 13.6 )rr3rGrPrBs r*rz"DictPersistence.callback_data_jsons.  # #++ +zz$,,--rEc|jS)z):obj:`dict`: The conversations as a dict.)rrBs r* conversationszDictPersistence.conversationss"""rEc|jr |jS|jr|j|jStj|jS)z::obj:`str`: The conversations serialized as a JSON-string.)rrS_encode_conversations_to_jsonr3rGrBs r*rz"DictPersistence.conversations_jsonsN  # #++ +   55d6H6HI Izz$,,--rEcZK|ji|_t|jSw)zReturns the user_data created from the ``user_data_json`` or an empty :obj:`dict`. Returns: :obj:`dict`: The restored user data. )rCrrrBs r* get_user_datazDictPersistence.get_user_data( >> ! DO'')+cZK|ji|_t|jSw)zReturns the chat_data created from the ``chat_data_json`` or an empty :obj:`dict`. Returns: :obj:`dict`: The restored chat data. )rJrrrBs r* get_chat_datazDictPersistence.get_chat_datarXrYcZK|ji|_t|jSw)zReturns the bot_data created from the ``bot_data_json`` or an empty :obj:`dict`. Returns: :obj:`dict`: The restored bot data. )rMrrrBs r* get_bot_datazDictPersistence.get_bot_datas( == DN &&rYc\K|jd|_yt|jSw)adReturns the callback_data created from the ``callback_data_json`` or :obj:`None`. .. versionadded:: 13.6 Returns: tuple[list[tuple[:obj:`str`, :obj:`float`, dict[:obj:`str`, :class:`object`]]], dict[:obj:`str`, :obj:`str`]]: The restored metadata or :obj:`None`, if no data was stored. N)rPrrrBs r*get_callback_dataz!DictPersistence.get_callback_datas0    %"&D **++s*,namecK|ji|_|jj|ijSw)zReturns the conversations created from the ``conversations_json`` or an empty :obj:`dict`. Returns: :obj:`dict`: The restored conversations data. )rSrgetcopy)r9r`s r*get_conversationsz!DictPersistence.get_conversationss>    %"$D !!%%dB/4466s>Akey new_statecK|jsi|_|jj|ij||k(ry||j||<d|_yw)aWill update the conversations for the given handler. Args: name (:obj:`str`): The handler's name. key (:obj:`tuple`): The key the state is changed for. new_state (:obj:`tuple` | :class:`object`): The new state for the given key. N)r setdefaultrbr)r9r`rerfs r*update_conversationz#DictPersistence.update_conversation*s_"""$D     ) )$ 3 7 7 < I )2D!#&#' sAAuser_idr;cK|ji|_|jj||k(ry||j|<d|_yw)zWill update the user_data (if changed). Args: user_id (:obj:`int`): The user the data might have been changed for. data (:obj:`dict`): The :attr:`telegram.ext.Application.user_data` ``[user_id]``. N)rrbr)r9rjr;s r*update_user_dataz DictPersistence.update_user_data;I ?? " DO ??  w '4 / #' #A A chat_idcK|ji|_|jj||k(ry||j|<d|_yw)zWill update the chat_data (if changed). Args: chat_id (:obj:`int`): The chat the data might have been changed for. data (:obj:`dict`): The :attr:`telegram.ext.Application.chat_data` ``[chat_id]``. N)rrbr)r9ror;s r*update_chat_dataz DictPersistence.update_chat_dataIrmrncHK|j|k(ry||_d|_yw)zWill update the bot_data (if changed). Args: data (:obj:`dict`): The :attr:`telegram.ext.Application.bot_data`. N)rrr9r;s r*update_bot_datazDictPersistence.update_bot_dataWs& >>T ! " "cHK|j|k(ry||_d|_yw)aKWill update the callback_data (if changed). .. versionadded:: 13.6 Args: data (tuple[list[tuple[:obj:`str`, :obj:`float`, dict[:obj:`str`, :class:`object`]]], dict[:obj:`str`, :obj:`str`]]): The relevant data to restore :class:`telegram.ext.CallbackDataCache`. N)rrrss r*update_callback_dataz$DictPersistence.update_callback_databs)   $ & "#' ruclK|jy|jj|dd|_yw)zWill delete the specified key from the :attr:`chat_data`. .. versionadded:: 20.0 Args: chat_id (:obj:`int`): The chat id to delete from the persistence. N)rpopr)r9ros r*drop_chat_datazDictPersistence.drop_chat_dataq1 ?? "  GT*#r,clK|jy|jj|dd|_yw)zWill delete the specified key from the :attr:`user_data`. .. versionadded:: 20.0 Args: user_id (:obj:`int`): The user id to delete from the persistence. N)rryr)r9rjs r*drop_user_datazDictPersistence.drop_user_data~r{r,rCc Kyw)zDoes nothing. .. versionadded:: 13.6 .. seealso:: :meth:`telegram.ext.BasePersistence.refresh_user_data` N)r9rjrCs r*refresh_user_dataz!DictPersistence.refresh_user_datarJc Kyw)zDoes nothing. .. versionadded:: 13.6 .. seealso:: :meth:`telegram.ext.BasePersistence.refresh_chat_data` Nr)r9rorJs r*refresh_chat_dataz!DictPersistence.refresh_chat_datarrrMc Kyw)zDoes nothing. .. versionadded:: 13.6 .. seealso:: :meth:`telegram.ext.BasePersistence.refresh_bot_data` Nr)r9rMs r*refresh_bot_dataz DictPersistence.refresh_bot_datarrc Kyw)zvDoes nothing. .. versionadded:: 20.0 .. seealso:: :meth:`telegram.ext.BasePersistence.flush` NrrBs r*flushzDictPersistence.flushrrrSci}|jD]=\}}i||<|jD] \}}|||tj|<"?tj|S)afHelper method to encode a conversations dict (that uses tuples as keys) to a JSON-serializable way. Use :meth:`self._decode_conversations_from_json` to decode. Args: conversations (:obj:`dict`): The conversations dict to transform to JSON. Returns: :obj:`str`: The JSON-serialized conversations dict )itemsr3rG)rStmphandlerstatesrestates r*rUz-DictPersistence._encode_conversations_to_jsonsm$&,224 6OGVCL$lln 6 U05G TZZ_- 6 6zz#rE json_stringc tj|}i}|jD]F\}}i||<|jD])\}}|||ttj|<+H|S)aQHelper method to decode a conversations dict (that uses tuples as keys) from a JSON-string created with :meth:`self._encode_conversations_to_json`. Args: json_string (:obj:`str`): The conversations dict as JSON string. Returns: :obj:`dict`: The conversations dict after decoding )r3r4rtuple)rrrSrrrers r*r8z/DictPersistence._decode_conversations_from_jsons{jj%57 "yy{ GOGV%'M' "$lln G UAF g&uTZZ_'=> G GrEci}tj|}|jD]A\}}t|}i||<|jD]\}} t|}||||<C|S#t$r|}YwxYw)aHelper method to decode chat or user data (that uses ints as keys) from a JSON-string. Args: data (:obj:`str`): The user/chat_data dict as JSON string. Returns: :obj:`dict`: The user/chat_data defaultdict after decoding )r3r4rintr0) r;r decoded_datauserrC int_user_idrevalue_ids r*r/z0DictPersistence._decode_user_chat_data_from_jsons02zz$' +113 .OD)d)K!C 'oo/ . Uc(C).K %  . . "Cs A.. A<;A<)Nrrrr<)r@N)1__name__ __module__ __qualname____doc__ __slots__rr r'r5r.propertyr&rrrCrrJrrMrr rPrr rSrobjectrWr[r]r_rdr rirlrqrtrwrzr}rrrr staticmethodrUr8r/ __classcell__)r?s@r*rr s .` I26  "$"$!#M-.MM M  M  M MM^8Dd38n)<$=>*** 8Dd38n)<$=>*** (4S>2)s)) #x0##.C..#xS2B-B(CD##.C..(T#tFFN/C*C%D((T#tFFN/C*C%D('D$8' ,'): , 7C 74D 7((-(:B6:J( (" $c $c3h $D $ $c $c3h $D $ #$sCx. #T # (w (4 ( $C $D $ $C $D $ s tCH~ RV  s tCH~ RV  tCH~ $  T#?O:O5PUX"ST#GWBW=X$stCffnAUrsX&6 55:PP.Bod38nd38nd3PS8n&TUBrE