Browse Source

fixed as I didnt knew dateutil isnt built-in lmao

pull/9685/head
Developer Anonymous 9 months ago
parent
commit
96e9be5af2
  1. 132
      discord/scheduled_event.py

132
discord/scheduled_event.py

@ -30,14 +30,13 @@ from typing import (
TYPE_CHECKING, TYPE_CHECKING,
AsyncIterator, AsyncIterator,
Dict, Dict,
NamedTuple,
Optional, Optional,
Union, Union,
overload, overload,
Literal, Literal,
List, List,
NamedTuple,
) )
from dateutil import rrule
from .asset import Asset from .asset import Asset
from .enums import ( from .enums import (
@ -75,8 +74,8 @@ __all__ = (
class _NWeekday(NamedTuple): class _NWeekday(NamedTuple):
week: Literal[1, 2, 3, 4, 5] # "n" for the API week: Literal[1, 2, 3, 4, 5]
day: rrule.weekday day: Literal[0, 1, 2, 3, 4, 5, 6]
class ScheduledEventRecurrenceRule: class ScheduledEventRecurrenceRule:
@ -84,6 +83,8 @@ class ScheduledEventRecurrenceRule:
This follows :class:`dateutil.rrule.rrule` structure. This follows :class:`dateutil.rrule.rrule` structure.
.. versionadded:: 2.5
Parameters Parameters
---------- ----------
start: :class:`datetime.datetime` start: :class:`datetime.datetime`
@ -93,11 +94,19 @@ class ScheduledEventRecurrenceRule:
This can be one of :attr:`dateutil.rrule.YEARLY`, :attr:`dateutil.rrule.MONTHLY`, This can be one of :attr:`dateutil.rrule.YEARLY`, :attr:`dateutil.rrule.MONTHLY`,
:attr:`dateutil.rrule.WEEKLY`, or :attr:`dateutil.rrule.DAILY`. :attr:`dateutil.rrule.WEEKLY`, or :attr:`dateutil.rrule.DAILY`.
interval: :class:`int`
The spacing between the events, defined by ``frequency``.
For example, a ``frequency`` of ``2`` (weekly) and an ``interval`` of ``2`` will result in
a "Every other week" recurrence rule.
weekdays: Optional[List[:class:`datetime.date`]]
The weekdays the event will recur on.
n_weekdays: Optional[List[Tuple[:class:`int`, :class:`int`]]]
A (week, weekday) tuple list of the N weekdays the event will recur on.
month_days: Optional[List[:class:`datetime.date`]]
The months and month days the scheduled event will recur on.
""" """
# As noted in the docs, recurrence rule is implemented by dateutil.rrule so we use
# that to have a better control on this.
def __init__( def __init__(
self, self,
/, /,
@ -105,58 +114,19 @@ class ScheduledEventRecurrenceRule:
frequency: Literal[0, 1, 2, 3,], frequency: Literal[0, 1, 2, 3,],
interval: int, interval: int,
*, *,
weekdays: Optional[List[rrule.weekday]] = MISSING, weekdays: Optional[List[date]] = MISSING,
n_weekdays: Optional[List[_NWeekday]] = MISSING, n_weekdays: Optional[List[_NWeekday]] = MISSING,
month_days: Optional[List[date]] = MISSING, month_days: Optional[List[date]] = MISSING,
) -> None: ) -> None:
self._rule: rrule.rrule = rrule.rrule( self.start: datetime = start
frequency, self.frequency: Literal[0, 1, 2, 3,] = frequency
start, self.interval: int = interval
interval, self._weekdays: Optional[List[date]] = weekdays
) self._n_weekdays: Optional[List[_NWeekday]] = n_weekdays
self._weekdays = weekdays self._month_days: Optional[List[date]] = month_days
self._n_weekdays = n_weekdays
self._month_days = month_days
kwargs = {}
if weekdays not in (MISSING, None):
kwargs.update(byweekday=[wkday.weekday for wkday in weekdays])
if n_weekdays not in (MISSING, None):
n_wkno = []
n_wkdays = []
for n_wkday in n_weekdays:
n_wkno.append(n_wkday[0])
n_wkdays.append(n_wkday[1])
kwargs.update(
byweekno=n_wkno,
byweekday=n_wkdays,
)
del n_wkno, n_wkdays
if month_days not in (MISSING, None):
month_days_months = []
month_days_days = []
for month_day in month_days:
month_days_months.append(month_day.month)
month_days_days.append(month_day.day)
kwargs.update(
bymonth=month_days_months,
bymonthday=month_days_days,
)
del month_days_months, month_days_days
if kwargs:
self._rule = self._rule.replace(**kwargs)
@property @property
def weekdays(self) -> Optional[List[rrule.weekday]]: def weekdays(self) -> Optional[List[date]]:
"""Optional[List[:class:`dateutil.rrule.weekday`]]: Returns a read-only list of the weekdays """Optional[List[:class:`dateutil.rrule.weekday`]]: Returns a read-only list of the weekdays
this event recurs on, or ``None``. this event recurs on, or ``None``.
""" """
@ -165,8 +135,8 @@ class ScheduledEventRecurrenceRule:
return self._weekdays.copy() return self._weekdays.copy()
@weekdays.setter @weekdays.setter
def weekdays(self, new: Optional[List[rrule.weekday]]) -> None: def weekdays(self, new: Optional[List[date]]) -> None:
self.replace(weekdays=new) self._weekdays = new
@property @property
def n_weekdays(self) -> Optional[List[_NWeekday]]: def n_weekdays(self) -> Optional[List[_NWeekday]]:
@ -179,7 +149,7 @@ class ScheduledEventRecurrenceRule:
@n_weekdays.setter @n_weekdays.setter
def n_weekdays(self, new: Optional[List[_NWeekday]]) -> None: def n_weekdays(self, new: Optional[List[_NWeekday]]) -> None:
self.replace(n_weekdays=new) self._n_weekdays = new
@property @property
def month_days(self) -> Optional[List[date]]: def month_days(self) -> Optional[List[date]]:
@ -192,12 +162,12 @@ class ScheduledEventRecurrenceRule:
@month_days.setter @month_days.setter
def month_days(self, new: Optional[List[date]]) -> None: def month_days(self, new: Optional[List[date]]) -> None:
self.replace(month_days=new) self._month_days = new
def replace( def replace(
self, self,
*, *,
weekdays: Optional[List[rrule.weekday]] = MISSING, weekdays: Optional[List[date]] = MISSING,
n_weekdays: Optional[List[_NWeekday]] = MISSING, n_weekdays: Optional[List[_NWeekday]] = MISSING,
month_days: Optional[List[date]] = MISSING, month_days: Optional[List[date]] = MISSING,
) -> Self: ) -> Self:
@ -215,59 +185,21 @@ class ScheduledEventRecurrenceRule:
month_days: Optional[List[:class:`datetime.date`]] month_days: Optional[List[:class:`datetime.date`]]
The new set of month and month days for the event to recur on. The new set of month and month days for the event to recur on.
.. note::
:attr:`datetime.date.year` attribute is ignored when updating the recurrence
rule.
Returns Returns
------- -------
:class:`ScheduledEventRecurrenceRule` :class:`ScheduledEventRecurrenceRule`
The recurrence rule with the replaced values. The recurrence rule with the replaced values.
""" """
kwargs = {}
if weekdays is not MISSING: if weekdays is not MISSING:
if weekdays is None: self._weekdays = weekdays
kwargs.update(byweekday=None)
else:
kwargs.update(byweekday=[wkday.weekday for wkday in weekdays])
if n_weekdays is not MISSING: if n_weekdays is not MISSING:
if n_weekdays is None: self._n_weekdays = n_weekdays
kwargs.update(byweekno=None, byweekday=None)
else:
n_wkno = []
n_wkdays = []
for n_wkday in n_weekdays:
n_wkno.append(n_wkday[0])
n_wkdays.append(n_wkdays[1])
kwargs.update(byweekno=n_wkno, byweekday=n_wkdays)
del n_wkno, n_wkdays
if month_days is not MISSING: if month_days is not MISSING:
if month_days is None: self._month_days = month_days
kwargs.update(bymonth=None, bymonthday=None)
else:
month_days_months = []
month_days_days = []
for month_day in month_days:
month_days_months.append(month_day.month)
month_days_days.append(month_day.day)
kwargs.update(bymonth=month_days_months, bymonthday=month_days_days)
del month_days_months, month_days_days
if not kwargs:
raise ValueError(
'You must provide at least one value to replace on the recurrence rule'
)
self._rule = self._rule.replace(**kwargs)
return self return self

Loading…
Cancel
Save