File: utils.py

package info (click to toggle)
python-django-ical 1.8.3-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 248 kB
  • sloc: python: 1,211; makefile: 132; sh: 2
file content (127 lines) | stat: -rw-r--r-- 3,142 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"""Utility functions to build calendar rules."""

from icalendar.prop import vRecur
from recurrence import serialize


def build_rrule(  # noqa
    count=None,
    interval=None,
    bysecond=None,
    byminute=None,
    byhour=None,
    byweekno=None,
    bymonthday=None,
    byyearday=None,
    bymonth=None,
    until=None,
    bysetpos=None,
    wkst=None,
    byday=None,
    freq=None,
):
    """
    Build rrule dictionary for vRecur class.

    :param count: int
    :param interval: int
    :param bysecond: int
    :param byminute: int
    :param byhour: int
    :param byweekno: int
    :param bymonthday: int
    :param byyearday: int
    :param bymonth: int
    :param until: datetime
    :param bysetpos: int
    :param wkst: str, two-letter weekday
    :param byday: weekday
    :param freq: str, frequency name ('WEEK', 'MONTH', etc)
    :return: dict
    """
    result = {}

    if count is not None:
        result["COUNT"] = count

    if interval is not None:
        result["INTERVAL"] = interval

    if bysecond is not None:
        result["BYSECOND"] = bysecond

    if byminute is not None:
        result["BYMINUTE"] = byminute

    if byhour is not None:
        result["BYHOUR"] = byhour

    if byweekno is not None:
        result["BYWEEKNO"] = byweekno

    if bymonthday is not None:
        result["BYMONTHDAY"] = bymonthday

    if byyearday is not None:
        result["BYYEARDAY"] = byyearday

    if bymonth is not None:
        result["BYMONTH"] = bymonth

    if until is not None:
        result["UNTIL"] = until

    if bysetpos is not None:
        result["BYSETPOS"] = bysetpos

    if wkst is not None:
        result["WKST"] = wkst

    if byday is not None:
        result["BYDAY"] = byday

    if freq is not None:
        if freq not in vRecur.frequencies:
            raise ValueError(
                "Frequency value should be one of: {0}".format(vRecur.frequencies)
            )
        result["FREQ"] = freq

    return result


def build_rrule_from_text(rrule_str):
    """Build an rrule from a serialzed RRULE string."""
    recurr = vRecur()
    return recurr.from_ical(rrule_str)


def build_rrule_from_recurrences_rrule(rule):
    """
    Build rrule dictionary for vRecur class from a django_recurrences rrule.

    django_recurrences is a popular implementation for recurrences in django.
    https://pypi.org/project/django-recurrence/
    this is a shortcut to interface between recurrences and icalendar.
    """
    line = serialize(rule)
    if line.startswith("RRULE:"):
        line = line[6:]
    return build_rrule_from_text(line)


def build_rrule_from_dateutil_rrule(rule):
    """
    Build rrule dictionary for vRecur class from a dateutil rrule.

    Dateutils rrule is a popular implementation of rrule in python.
    https://pypi.org/project/python-dateutil/
    this is a shortcut to interface between dateutil and icalendar.
    """
    lines = str(rule).splitlines()
    for line in lines:
        if line.startswith("DTSTART:"):
            continue
        if line.startswith("RRULE:"):
            line = line[6:]
        return build_rrule_from_text(line)