o
    2g;                     @  s   d Z ddlmZ ddlmZmZmZ ddlm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 er>ddlZG dd deZG dd dZG dd dZG dd dZdS )zProvide the Rule class.    )annotations)TYPE_CHECKINGAnyIterator)quote)warn   )API_PATH)ClientException)_deprecate_argscachedproperty   )
RedditBaseNc                      sV   e Zd ZdZdZedddZd fd
dZ			dd fddZ fddZ	  Z
S )RuleaR  An individual :class:`.Rule` object.

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

    ==================== =============================================================
    Attribute            Description
    ==================== =============================================================
    ``created_utc``      Time the rule was created, represented in `Unix Time`_.
    ``description``      The description of the rule, if provided, otherwise a blank
                         string.
    ``kind``             The kind of rule. Can be ``"link"``, ``comment"``, or
                         ``"all"``.
    ``priority``         Represents where the rule is ranked. For example, the first
                         rule is at priority ``0``. Serves as an index number on the
                         list of rules.
    ``short_name``       The name of the rule.
    ``violation_reason`` The reason that is displayed on the report menu for the rule.
    ==================== =============================================================

    .. _unix time: https://en.wikipedia.org/wiki/Unix_time

    
short_namereturn'praw.models.reddit.rules.RuleModerationc                 C     t | S )a  Contain methods used to moderate rules.

        To delete ``"No spam"`` from r/test try:

        .. code-block:: python

            reddit.subreddit("test").rules["No spam"].mod.delete()

        To update ``"No spam"`` from r/test try:

        .. code-block:: python

            reddit.subreddit("test").removal_reasons["No spam"].mod.update(
                description="Don't do this!", violation_reason="Spam post"
            )

        )RuleModerationself r   P/home/garg/my-data/venv/lib/python3.10/site-packages/praw/models/reddit/rules.pymod,   s   zRule.mod	attributestrr   c                   s,   t  |}|dkr|du rd}t||S )zGet the value of an attribute.	subredditNz;The Rule is missing a subreddit. File a bug report at PRAW.)super__getattribute__
ValueError)r   r   valuemsg	__class__r   r   r   A   s
   zRule.__getattribute__Nredditpraw.Redditr   praw.models.Subreddit | None
str | None_datadict[str, str] | Nonec                   sB   ||f ddkrd}t||r|| _|| _t j||d dS )z%Initialize a :class:`.Rule` instance.Nr   z-Either short_name or _data needs to be given.)r(   )countr   r   r   r   __init__)r   r$   r   r   r(   r!   r"   r   r   r+   I   s   zRule.__init__c                   sT   | j jD ]}|j| jkr| j|j t    d S qd| j  d| j }t|)Nz
Subreddit z does not have the rule )r   rulesr   __dict__updater   _fetchr
   )r   ruler!   r"   r   r   r/   \   s   
zRule._fetch)r   r   )r   r   r   r   )NNN)r$   r%   r   r&   r   r'   r(   r)   )__name__
__module____qualname____doc__	STR_FIELDr   r   r   r+   r/   __classcell__r   r   r"   r   r      s    r   c                   @  sF   e Zd ZdZdddZdd Zedd	d
dddddddddZdS )r   a  Contain methods used to moderate rules.

    To delete ``"No spam"`` from r/test try:

    .. code-block:: python

        reddit.subreddit("test").rules["No spam"].mod.delete()

    To update ``"No spam"`` from r/test try:

    .. code-block:: python

        reddit.subreddit("test").removal_reasons["No spam"].mod.update(
            description="Don't do this!", violation_reason="Spam post"
        )

    r0   praw.models.Rulec                 C  
   || _ dS )z/Initialize a :class:`.RuleModeration` instance.N)r0   )r   r0   r   r   r   r+   y      
zRuleModeration.__init__c                 C  s0   t | jj| jjd}| jjjtd |d dS )zDelete a rule from this subreddit.

        To delete ``"No spam"`` from r/test try:

        .. code-block:: python

            reddit.subreddit("test").rules["No spam"].mod.delete()

        )rr   remove_subreddit_ruledataN)r   r0   r   r   _redditpostr	   )r   r=   r   r   r   delete}   s   
zRuleModeration.deletedescriptionkindr   violation_reasonNrA   rB   r   rC   r'   r   c          	      C  sv   t | jj| jjd}||||d D ]\}}|du r"t| j|n|||< q| jjjtd |dd }| jj|_|S )a
  Update the rule from this subreddit.

        .. note::

            Existing values will be used for any unspecified arguments.

        :param description: The new description for the rule. Can be empty.
        :param kind: The kind of item that the rule applies to. One of ``"link"``,
            ``"comment"``, or ``"all"``.
        :param short_name: The name of the rule.
        :param violation_reason: The reason that is shown on the report menu.

        :returns: A Rule object containing the updated values.

        To update ``"No spam"`` from r/test try:

        .. code-block:: python

            reddit.subreddit("test").removal_reasons["No spam"].mod.update(
                description="Don't do this!", violation_reason="Spam post"
            )

        )r:   old_short_namerD   Nupdate_subreddit_ruler<   r   )	r   r0   r   r   itemsgetattrr>   r?   r	   )	r   rA   rB   r   rC   r=   namer    updated_ruler   r   r   r.      s$   
!

zRuleModeration.update)r0   r7   )
rA   r'   rB   r'   r   r'   rC   r'   r   r7   )r1   r2   r3   r4   r+   r@   r   r.   r   r   r   r   r   f   s    
r   c                   @  sT   e Zd ZdZedddZedddZdd
dZdddZdddZ	dddZ
dS )SubredditRulesa	  Provide a set of functions to access a :class:`.Subreddit`'s rules.

    For example, to list all the rules for a subreddit:

    .. code-block:: python

        for rule in reddit.subreddit("test").rules:
            print(rule)

    Moderators can also add rules to the subreddit. For example, to make a rule called
    ``"No spam"`` in r/test:

    .. code-block:: python

        reddit.subreddit("test").rules.mod.add(
            short_name="No spam", kind="all", description="Do not spam. Spam bad"
        )

    r   
list[Rule]c                 C  s0   | j td j| jd}|D ]}| j|_q|S )ziGet a list of :class:`.Rule` objects.

        :returns: A list of instances of :class:`.Rule`.

        r,   r   )r>   getr	   formatr   )r   	rule_listr0   r   r   r   
_rule_list   s   
zSubredditRules._rule_listSubredditRulesModerationc                 C  r   )a  Contain methods to moderate subreddit rules as a whole.

        To add rule ``"No spam"`` to r/test try:

        .. code-block:: python

            reddit.subreddit("test").rules.mod.add(
                short_name="No spam", kind="all", description="Do not spam. Spam bad"
            )

        To move the fourth rule to the first position, and then to move the prior first
        rule to where the third rule originally was in r/test:

        .. code-block:: python

            subreddit = reddit.subreddit("test")
            rules = list(subreddit.rules)
            new_rules = rules[3:4] + rules[1:3] + rules[0:1] + rules[4:]
            # Alternate: [rules[3]] + rules[1:3] + [rules[0]] + rules[4:]
            new_rule_list = subreddit.rules.mod.reorder(new_rules)

        )rR   r   r   r   r   r      s   zSubredditRules.modlist[praw.models.Rule]c                 C  s,   t dtdd | jjdtd j| jddS )a  Return a list of :class:`.Rule`\ s (Deprecated).

        :returns: A list of instances of :class:`.Rule`.

        .. deprecated:: 7.1

            Use the iterator by removing the call to :class:`.SubredditRules`. For
            example, in order to use the iterator:

            .. code-block:: python

                for rule in reddit.subreddit("test").rules:
                    print(rule)

        a=  Calling SubredditRules to get a list of rules is deprecated. Remove the parentheses to use the iterator. View the PRAW documentation on how to change the code in order to use the iterator (https://praw.readthedocs.io/en/latest/code_overview/other/subredditrules.html#praw.models.reddit.rules.SubredditRules.__call__).   )category
stacklevelGETr,   rM   )methodpath)r   DeprecationWarningr>   requestr	   rO   r   r   r   r   r   __call__   s   zSubredditRules.__call__r   str | int | slicer7   c                 C  s&   t |ts
| j| S t| j| j|dS )a  Return the :class:`.Rule` for the subreddit with short_name ``short_name``.

        :param short_name: The short_name of the rule, or the rule number.

        .. note::

            Rules fetched using a specific rule name are lazily loaded, so you might
            have to access an attribute to get all the expected attributes.

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

        .. code-block:: python

            rule_name = "No spam"
            rule = reddit.subreddit("test").rules[rule_name]
            print(rule)

        You can also fetch a numbered rule of a subreddit.

        Rule numbers start at ``0``, so the first rule is at index ``0``, and the second
        rule is at index ``1``, and so on.

        :raises: :py:class:`IndexError` if a rule of a specific number does not exist.

        .. note::

            You can use negative indexes, such as ``-1``, to get the last rule. You can
            also use slices, to get a subset of rules, such as the last three rules with
            ``rules[-3:]``.

        For example, to fetch the second rule of r/test:

        .. code-block:: python

            rule = reddit.subreddit("test").rules[1]

        )r   r   )
isinstancer   rQ   r   r>   r   )r   r   r   r   r   __getitem__  s   
&
zSubredditRules.__getitem__r   praw.models.Subredditc                 C  s   || _ |j| _dS )z|Initialize a :class:`.SubredditRules` instance.

        :param subreddit: The subreddit whose rules to work with.

        N)r   r>   )r   r   r   r   r   r+   @  s   zSubredditRules.__init__Iterator[praw.models.Rule]c                 C  s
   t | jS )ai  Iterate through the rules of the subreddit.

        :returns: An iterator containing all the rules of a subreddit.

        This method is used to discover all rules for a subreddit.

        For example, to get the rules for r/test:

        .. code-block:: python

            for rule in reddit.subreddit("test").rules:
                print(rule)

        )iterrQ   r   r   r   r   __iter__I  s   
zSubredditRules.__iter__N)r   rL   )r   rR   )r   rS   )r   r]   r   r7   )r   r`   )r   ra   )r1   r2   r3   r4   r   rQ   r   r\   r_   r+   rc   r   r   r   r   rK      s    


*	rK   c                   @  sD   e Zd ZdZdddZedddd	d
dddddZdddZdS )rR   a  Contain methods to moderate subreddit rules as a whole.

    To add rule ``"No spam"`` to r/test try:

    .. code-block:: python

        reddit.subreddit("test").rules.mod.add(
            short_name="No spam", kind="all", description="Do not spam. Spam bad"
        )

    To move the fourth rule to the first position, and then to move the prior first rule
    to where the third rule originally was in r/test:

    .. code-block:: python

        subreddit = reddit.subreddit("test")
        rules = list(subreddit.rules)
        new_rules = rules[3:4] + rules[1:3] + rules[0:1] + rules[4:]
        # Alternate: [rules[3]] + rules[1:3] + [rules[0]] + rules[4:]
        new_rule_list = subreddit.rules.mod.reorder(new_rules)

    subreddit_rulesrK   c                 C  r8   )z9Initialize a :class:`.SubredditRulesModeration` instance.N)rd   )r   rd   r   r   r   r+   s  r9   z!SubredditRulesModeration.__init__r   rB   rA   rC    N)rA   rC   r   r'   r   r7   c                C  sL   t | jj||||du r|n|d}| jjjtd |dd }| jj|_|S )a  Add a removal reason to this subreddit.

        :param description: The description for the rule.
        :param kind: The kind of item that the rule applies to. One of ``"link"``,
            ``"comment"``, or ``"all"``.
        :param short_name: The name of the rule.
        :param violation_reason: The reason that is shown on the report menu. If a
            violation reason is not specified, the short name will be used as the
            violation reason.

        :returns: The added :class:`.Rule`.

        To add rule ``"No spam"`` to r/test try:

        .. code-block:: python

            reddit.subreddit("test").rules.mod.add(
                short_name="No spam", kind="all", description="Do not spam. Spam bad"
            )

        N)r:   rA   rB   r   rC   add_subreddit_ruler<   r   )r   rd   r   r>   r?   r	   )r   rA   rB   r   rC   r=   new_ruler   r   r   addw  s   
	
zSubredditRulesModeration.addrP   rS   c                 C  s\   t ddd |D dd}t| jj|d}| jjjtd |d}|D ]}| jj|_q$|S )a  Reorder the rules of a subreddit.

        :param rule_list: The list of rules, in the wanted order. Each index of the list
            indicates the position of the rule.

        :returns: A list containing the rules in the specified order.

        For example, to move the fourth rule to the first position, and then to move the
        prior first rule to where the third rule originally was in r/test:

        .. code-block:: python

            subreddit = reddit.subreddit("test")
            rules = list(subreddit.rules)
            new_rules = rules[3:4] + rules[1:3] + rules[0:1] + rules[4:]
            # Alternate: [rules[3]] + rules[1:3] + [rules[0]] + rules[4:]
            new_rule_list = subreddit.rules.mod.reorder(new_rules)

        ,c                 S  s   g | ]}|j qS r   )r   ).0r0   r   r   r   
<listcomp>  s    z4SubredditRulesModeration.reorder.<locals>.<listcomp>)safe)r:   new_rule_orderreorder_subreddit_rulesr<   )r   joinr   rd   r   r>   r?   r	   )r   rP   order_stringr=   responser0   r   r   r   reorder  s   
z SubredditRulesModeration.reorder)rd   rK   )
rA   r   rB   r   r   r   rC   r'   r   r7   )rP   rS   r   rS   )r1   r2   r3   r4   r+   r   rh   rr   r   r   r   r   rR   [  s    
,rR   )r4   
__future__r   typingr   r   r   urllib.parser   warningsr   constr	   
exceptionsr
   utilr   r   baser   praw.modelsprawr   r   rK   rR   r   r   r   r   <module>   s     TY 