o
    2g_!                     @  s   d Z ddlmZ 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 er4ddlZG dd deZG dd dZdS )zProvide the Emoji class.    )annotations)Path)TYPE_CHECKINGAny   )API_PATH)ClientException)_deprecate_args   )
RedditBaseNc                      sv   e Zd ZdZdZd! fddZd"d
dZ	d#d$ fddZ fddZdd Z	e
dddddddd%dd Z  ZS )&Emojia  An individual :class:`.Emoji` object.

    .. include:: ../../typical_attributes.rst

    ====================== =================================================
    Attribute              Description
    ====================== =================================================
    ``mod_flair_only``     Whether the emoji is restricted for mod use only.
    ``name``               The name of the emoji.
    ``post_flair_allowed`` Whether the emoji may appear in post flair.
    ``url``                The URL of the emoji image.
    ``user_flair_allowed`` Whether the emoji may appear in user flair.
    ====================== =================================================

    nameotherstr | Emojireturnboolc                   sJ   t |tr|t| kS t || jrt| t|ko|j| jkS t |S )z5Return whether the other instance equals the current.)
isinstancestr	__class__	subredditsuper__eq__)selfr   r    P/home/garg/my-data/venv/lib/python3.10/site-packages/praw/models/reddit/emoji.pyr   $   s
   
zEmoji.__eq__intc                 C  s"   t | jjt t| A t | jA S )z(Return the hash of the current instance.)hashr   __name__r   r   r   r   r   r   __hash__,   s   "zEmoji.__hash__Nredditpraw.Redditr   praw.models.Subredditr   _datadict[str, Any] | Nonec                   s    || _ || _t j||d dS )z'Initialize an :class:`.Emoji` instance.r$   N)r   r   r   __init__)r   r!   r   r   r$   r   r   r   r'   0   s   zEmoji.__init__c                   sT   | j jD ]}|j| jkr| j|j t    d S qd| j  d| j }t|)Nzr/z does not have the emoji )r   emojir   __dict__updater   _fetchr   )r   r(   msgr   r   r   r+   <   s   
zEmoji._fetchc                 C  s&   t d j| j| jd}| j| dS )zDelete an emoji from this subreddit by :class:`.Emoji`.

        To delete ``"emoji"`` as an emoji on r/test try:

        .. code-block:: python

            reddit.subreddit("test").emoji["emoji"].delete()

        emoji_delete)
emoji_namer   N)r   formatr   r   _redditdelete)r   urlr   r   r   r1   E   s   
zEmoji.deletemod_flair_onlypost_flair_alloweduser_flair_allowedr3   r4   r5   bool | Nonec          
        s   t    fdddD }tdd | D rd}t|d| ji}| D ]\}}|du r3t| |}|||< q&td	 j| j	d
}	| j
j|	|d | D ]
\}}t| || qMdS )a  Update the permissions of an emoji in this subreddit.

        :param mod_flair_only: Indicate whether the emoji is restricted to mod use only.
            Respects pre-existing settings if not provided.
        :param post_flair_allowed: Indicate whether the emoji may appear in post flair.
            Respects pre-existing settings if not provided.
        :param user_flair_allowed: Indicate whether the emoji may appear in user flair.
            Respects pre-existing settings if not provided.

        .. note::

            In order to retain pre-existing values for those that are not explicitly
            passed, a network request is issued. To avoid that network request,
            explicitly provide all values.

        To restrict the emoji ``"emoji"`` in r/test to mod use only, try:

        .. code-block:: python

            reddit.subreddit("test").emoji["emoji"].update(mod_flair_only=True)

        c                   s   i | ]}| | qS r   r   ).0	attributelocals_referencer   r   
<dictcomp>s   s    z Emoji.update.<locals>.<dictcomp>r6   c                 s  s    | ]}|d u V  qd S Nr   )r8   valuer   r   r   	<genexpr>{   s    zEmoji.update.<locals>.<genexpr>z'At least one attribute must be providedr   Nemoji_updater   data)localsallvalues	TypeErrorr   itemsgetattrr   r/   r   r0   postsetattr)
r   r3   r4   r5   mappingr,   rC   r9   r>   r2   r   r:   r   r*   T   s"   



zEmoji.update)r   r   r   r   )r   r   r=   )r!   r"   r   r#   r   r   r$   r%   )r3   r7   r4   r7   r5   r7   )r   
__module____qualname____doc__	STR_FIELDr   r    r'   r+   r1   r	   r*   __classcell__r   r   r   r   r      s    
		
r   c                   @  sB   e Zd ZdZdddZdd
dZdddZdddddddZdS )SubredditEmojiz?Provides a set of functions to a :class:`.Subreddit` for emoji.r   r   r   r   c                 C  s   t | j| j|S )a;  Lazily return the :class:`.Emoji` for the subreddit named ``name``.

        :param name: The name of the emoji.

        This method is to be used to fetch a specific emoji url, like so:

        .. code-block:: python

            emoji = reddit.subreddit("test").emoji["emoji"]
            print(emoji)

        )r   r0   r   )r   r   r   r   r   __getitem__   s   zSubredditEmoji.__getitem__r   r#   c                 C  s   || _ |j| _dS )z|Initialize a :class:`.SubredditEmoji` instance.

        :param subreddit: The subreddit whose emoji are affected.

        N)r   r0   )r   r   r   r   r   r'      s   zSubredditEmoji.__init__list[Emoji]c                 #  sr     j td j jd} fdd|D }t|dksJ ||d   D ]\}}t j  j||dV  q'dS )	zReturn a list of :class:`.Emoji` for the subreddit.

        This method is to be used to discover all emoji for a subreddit:

        .. code-block:: python

            for emoji in reddit.subreddit("test").emoji:
                print(emoji)

        
emoji_listrA   c                   s$   g | ]}|  jjjd  r|qS rA   )
startswithr0   configkinds)r8   keyr   r   r   
<listcomp>   s    z+SubredditEmoji.__iter__.<locals>.<listcomp>r
   r   r&   N)r0   getr   r/   r   lenrH   r   )r   responsesubreddit_keysr.   
emoji_datar   r   r   __iter__   s   
zSubredditEmoji.__iter__Nr6   
image_pathr3   r7   r4   r5   c                C  s  t |}|jdd}| drd|d< td j| jd}| jj||dd	 }	d
d |	d D }
d|	d  }|	d}| jj
jjj||
d|id}W d   n1 sVw   Y  |  ||||
d |d}td j| jd}| jj||d t| j| j|S )a  Add an emoji to this subreddit.

        :param image_path: A path to a jpeg or png image.
        :param mod_flair_only: When provided, indicate whether the emoji is restricted
            to mod use only (default: ``None``).
        :param name: The name of the emoji.
        :param post_flair_allowed: When provided, indicate whether the emoji may appear
            in post flair (default: ``None``).
        :param user_flair_allowed: When provided, indicate whether the emoji may appear
            in user flair (default: ``None``).

        :returns: The :class:`.Emoji` added.

        To add ``"emoji"`` to r/test try:

        .. code-block:: python

            reddit.subreddit("test").emoji.add(name="emoji", image_path="emoji.png")

        z
image/jpeg)filepathmimetypez.pngz	image/pngrc   emoji_leaserA   rB   s3UploadLeasec                 S  s   i | ]	}|d  |d qS )r   r>   r   )r8   itemr   r   r   r<      s    z&SubredditEmoji.add.<locals>.<dictcomp>fieldszhttps:actionrbfile)rC   filesNrY   )r3   r   r4   s3_keyr5   emoji_upload)r   r   lowerendswithr   r/   r   r0   rJ   open_core
_requestor_httpraise_for_statusr   )r   ra   r3   r   r4   r5   rj   rC   r2   upload_leaseupload_data
upload_urlimager]   r   r   r   add   s2   
zSubredditEmoji.add)r   r   r   r   )r   r#   )r   rT   )ra   r   r3   r7   r   r   r4   r7   r5   r7   r   r   )r   rM   rN   rO   rS   r'   r`   ry   r   r   r   r   rR      s    


	rR   )rO   
__future__r   pathlibr   typingr   r   constr   
exceptionsr   utilr	   baser   prawr   rR   r   r   r   r   <module>   s    y