o
    2gB1                     @  s~   d Z ddlmZ ddlmZ ddlmZ ddlmZmZ ddl	m
Z
mZ ddlmZ er6dd	lZdd
lmZ G dd dZd	S )zProvides the Objector class.    )annotations)datetime)loads)TYPE_CHECKINGAny   )ClientExceptionRedditAPIException)snake_case_keysN)
RedditBasec                   @  sL   e Zd ZdZedddZeddd	ZddddZdddZdddZ	d
S )Objectorz1The objector builds :class:`.RedditBase` objects.data%list[Any] | dict[str, dict[str, str]]c                 C  s   |  |}|r	|dS )z;Raise an error if the argument resolves to an error object.N)parse_error)clsr   error r   E/home/garg/my-data/venv/lib/python3.10/site-packages/praw/objector.pycheck_error   s   
zObjector.check_errorreturnRedditAPIException | Nonec                 C  sN   t |trdS |di d}|du rdS t|dk r#d}t||t|S )zConvert JSON response into an error object.

        :param data: The dict to be converted.

        :returns: An instance of :class:`.RedditAPIException`, or ``None`` if ``data``
            doesn't fit this model.

        Njsonerrorsr   zsuccessful error response)
isinstancelistgetlenr   r	   )r   r   r   msgr   r   r   r      s   

zObjector.parse_errorNredditpraw.Redditparsersdict[str, Any] | Nonec                 C  s   |du ri n|| _ || _dS )zmInitialize an :class:`.Objector` instance.

        :param reddit: An instance of :class:`.Reddit`.

        N)r    _reddit)selfr   r    r   r   r   __init__6   s   
zObjector.__init__dict[str, Any]r   c                   s  ddh  r-ddh r- d v r dn d jd }| j nddh  s;ddh  rE d= jd }nh d	  rg } d
 D ]*} d | }|d tdd |d D krw fdd|d D |d< || qR| d< t	  jd }nVh d  rt	  jd }nDddh  rt	  jd }n2h d  rjd }n$ddh  rt	   d=  d d< jjj
jd  }nh d  rt	   d d< jjj
jd  }nh d   rt	  jjj
j d!   }nh d"  s'h d#  r3jjj
jd  }nd$d%h  rSd& v sEd' v rLjd( }njd) }nd*d$h  rbjd+ }nh d,  rqjd- }nqh d.  rj d/ S d0d/h  rj d/ S d1h  rjjj
jd2  }n>d3 v rjd4 }n2h d5  rt	  g } d6 D ]}t	 d7 | }t|d8  |d8< || q| d7< jd9 }nd: v r d: d/< jjj
jd  }nh d;  r d<  d=< jjj
jd>  }nd?d@h  rdjjj
jd>  jdA fdBdC d@D }	 d? D ]}
|
d> rO|	|
d>  |
d>< t|
dD dE  |
dD< q@jdF }n~dGdHh  rj dI  dJ< j d>  d>< j dK  dK<   dG   dG=   dH   dH= jdL }n>d v rt d trdGdHh  d rĈ d   S dK v rjjj
jd  }|d/ dK ij dK<  S | jS )MzCreate :class:`.RedditBase` objects from dicts.

        :param data: The structured data, assumed to be a dict.

        :returns: An instance of :class:`.RedditBase`.

        messages
modActionsconversationsconversationModmailConversationlegacyFirstMessageIdstateobjIds>   r&   r(   conversationIdsr.   numMessagesc                 S  s   g | ]
}|d  dkr|qS )keyr&   r   ).0objr   r   r   
<listcomp>c   s    z,Objector._objectify_dict.<locals>.<listcomp>c                   s"   g | ]}  d  |d  qS )r&   id	objectify)r1   obj_idr   r#   r   r   r3   e   s    zModmailConversations-list>   dateauthoractionTypeIdModmailActionbodyMarkdown
isInternalModmailMessage>   kind
short_nameviolation_reasonruleisAdmin	isDeletedr4   is_modis_subreddit_modredditor>   	banStatus
muteStatusrecentCommentscreatedcreated_string>   r4   typedisplayNamerN   >   r4   r9   name>   r4   rP   permissionstexturlcolorlinkUrlButtonMenuLinkchildrenSubmenu>   rS   widthheightImage>   rP   subscribersisSubscribedrP   authorFlairType	parent_idcommentcollection_id
Collection>   
moderatorssubredditIdmoderatorIdsallUsersLoadedmoderator_idsrd   mod_permissionszmoderator-listusername>   srrP   r]   ri   rk   display_name	subredditdrafts
subredditsUserSubredditc                   s<   i | ]}|d  |d  dr| jn| jqS )rP   display_name_prefixedzu/)
startswithparser"   )r1   rm   )r#   subreddit_parseruser_subreddit_parserr   r   
<dictcomp>   s    z,Objector._objectify_dict.<locals>.<dictcomp>modifiedi  	DraftListmod_action_datauser_note_dataoperator	moderatorusermod_note)issubsetintersectionupdatepopr    _convert_conversation_objectsr"   r   appendr
   configkindsrm   rH   r   keysr   fromtimestamp
astimezoner   dict_objectify_dictrs   )r#   r   parserr(   conversation_idr)   rd   mod_idmodro   draftr   )r   r#   rt   ru   r   r   ?   s   












zObjector._objectify_dict(dict[str, Any] | list[Any] | bool | None5RedditBase | dict[str, Any] | list[Any] | bool | Nonec                   s   du rdS t  trfdd D S t  tr S d v r5d d v r5 d d }t|dkr5t|d v rVd v sC d d	v rVj d jd
 }| jS ddh	 r d jv rj d  } d dkrx| jS | d jS d v r<d d v r<d d d v r S d d d v r
 d d d S d d d v r
t d d d S d d d v rt fdddD r d d d jd  d d jS d d d v r, d d d= jjjjd  } d d d jjjd  dr+ d d d ddd  d d d< njd }| d d jS h d	 rPjd }| jS d v r\
 d S t  trg S  S )zCreate :class:`.RedditBase` objects from data.

        :param data: The structured data.

        :returns: An instance of :class:`.RedditBase`, or ``None`` if given ``data`` is
            ``None``.

        Nc                   s   g | ]}  |qS r   r5   )r1   item)r#   r   r   r3      s    z&Objector.objectify.<locals>.<listcomp>r   r   r   r@   	shortName)menurd   widgetr   ModeratedListwebsocket_urlthingsrulesdrafts_countc                 3  s     | ]}| d  d vV  qdS )r   r   Nr   )r1   r0   )r   r   r   	<genexpr>  s    
z%Objector.objectify.<locals>.<genexpr>)rP   rS   DraftrS   
submissionr4   _r   LiveUpdateEvent>   bodytitleis_public_link)r   r   boolr   r	   r    r   rs   r"   r   r6   r   allr   r   r   rr   splitr   r   )r#   r   r   r   r   r8   r   r6      sj   





zObjector.objectify)r   r   )r   r   r   r   )N)r   r   r    r!   )r   r%   r   r   )r   r   r   r   )
__name__
__module____qualname____doc__classmethodr   r   r$   r   r6   r   r   r   r   r      s    
	 r   )r   
__future__r   r   r   r   typingr   r   
exceptionsr   r	   utilr
   prawmodels.reddit.baser   r   r   r   r   r   <module>   s    