Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from icalendar.parser_tools import DEFAULT_ENCODING
from icalendar.prop import TypesFactory
from icalendar.prop import vText, vDDDLists
from icalendar.timezone_cache import _timezone_cache
import pytz
import dateutil.rrule
from pytz.tzinfo import DstTzInfo
from icalendar.compat import unicode_type
######################################
# The component factory
class ComponentFactory(CaselessDict):
"""All components defined in rfc 2445 are registered in this factory class.
To get a component you can use it like this.
"""
def __init__(self, *args, **kwargs):
"""Set keys to upper for initial dict.
"""
super(ComponentFactory, self).__init__(*args, **kwargs)
self['VEVENT'] = Event
self['VTODO'] = Todo
self['VJOURNAL'] = Journal
self['VFREEBUSY'] = FreeBusy
self['VTIMEZONE'] = Timezone
self['STANDARD'] = TimezoneStandard
self['DAYLIGHT'] = TimezoneDaylight
self['VALARM'] = Alarm
class vRecur(CaselessDict):
"""Recurrence definition.
"""
frequencies = ["SECONDLY", "MINUTELY", "HOURLY", "DAILY", "WEEKLY",
"MONTHLY", "YEARLY"]
# Mac iCal ignores RRULEs where FREQ is not the first rule part.
# Sorts parts according to the order listed in RFC 5545, section 3.3.10.
canonical_order = ("FREQ", "UNTIL", "COUNT", "INTERVAL",
"BYSECOND", "BYMINUTE", "BYHOUR", "BYDAY",
"BYMONTHDAY", "BYYEARDAY", "BYWEEKNO", "BYMONTH",
"BYSETPOS", "WKST")
types = CaselessDict({
'COUNT': vInt,
'INTERVAL': vInt,
'BYSECOND': vInt,
'BYMINUTE': vInt,
'BYHOUR': vInt,
'BYWEEKNO': vInt,
'BYMONTHDAY': vInt,
'BYYEARDAY': vInt,
'BYMONTH': vInt,
'UNTIL': vDDDTypes,
'BYSETPOS': vInt,
'WKST': vWeekday,
'BYDAY': vWeekday,
'FREQ': vFrequency,
})
return (start, end_or_duration)
except:
raise ValueError('Expected period format, got: %s' % ical)
def __repr__(self):
if self.by_duration:
p = (self.start, self.duration)
else:
p = (self.start, self.end)
return 'vPeriod(%r)' % (p, )
class vWeekday(compat.unicode_type):
"""This returns an unquoted weekday abbrevation.
"""
week_days = CaselessDict({
"SU": 0, "MO": 1, "TU": 2, "WE": 3, "TH": 4, "FR": 5, "SA": 6,
})
def __new__(cls, value, encoding=DEFAULT_ENCODING):
value = to_unicode(value, encoding=encoding)
self = super(vWeekday, cls).__new__(cls, value)
match = WEEKDAY_RULE.match(self)
if match is None:
raise ValueError('Expected weekday abbrevation, got: %s' % self)
match = match.groupdict()
sign = match['signal']
weekday = match['weekday']
relative = match['relative']
if weekday not in vWeekday.week_days or sign not in '+-':
raise ValueError('Expected weekday abbrevation, got: %s' % self)
self.relative = relative and int(relative) or None
"""
super(ComponentFactory, self).__init__(*args, **kwargs)
self['VEVENT'] = Event
self['VTODO'] = Todo
self['VJOURNAL'] = Journal
self['VFREEBUSY'] = FreeBusy
self['VTIMEZONE'] = Timezone
self['STANDARD'] = TimezoneStandard
self['DAYLIGHT'] = TimezoneDaylight
self['VALARM'] = Alarm
self['VCALENDAR'] = Calendar
# These Properties have multiple property values inlined in one propertyline
# seperated by comma. Use CaselessDict as simple caseless set.
INLINE = CaselessDict({
'CATEGORIES': 1,
'RESOURCES': 1,
'FREEBUSY': 1,
})
_marker = []
class Component(CaselessDict):
"""Component is the base object for calendar, Event and the other
components defined in RFC 2445. normally you will not use this class
directy, but rather one of the subclasses.
"""
name = None # should be defined in each component
required = () # These properties are required
def __init__(self, *args, **kwargs):
"Set keys to upper for initial dict"
CaselessDict.__init__(self, *args, **kwargs)
# set parameters here for properties that use non-default values
self.subcomponents = [] # Components can be nested.
self.is_broken = False # True iff we ignored an exception while parsing a property
def __init__(self, *args, **kwargs):
"Set keys to upper for initial dict"
CaselessDict.__init__(self, *args, **kwargs)
self['VEVENT'] = Event
self['VTODO'] = Todo
self['VJOURNAL'] = Journal
self['VFREEBUSY'] = FreeBusy
self['VTIMEZONE'] = Timezone
self['VALARM'] = Alarm
self['VCALENDAR'] = Calendar
# These Properties have multiple property values inlined in one propertyline
# seperated by comma. Use CaselessDict as simple caseless set.
INLINE = CaselessDict(
[(cat, 1) for cat in ('CATEGORIES', 'RESOURCES', 'FREEBUSY')]
)
_marker = []
class Component(CaselessDict):
"""
Component is the base object for calendar, Event and the other components
defined in RFC 2445. normally you will not use this class directy, but
rather one of the subclasses.
A component is like a dictionary with extra methods and attributes.
>>> c = Component()
>>> c.name = 'VCALENDAR'
Every key defines a property. A property can consist of either a single
raise ValueError('Expected frequency, got: %s' % self)
self.params = Parameters()
return self
def to_ical(self):
return self.encode(DEFAULT_ENCODING).upper()
@classmethod
def from_ical(cls, ical):
try:
return cls(ical.upper())
except:
raise ValueError('Expected frequency, got: %s' % ical)
class vRecur(CaselessDict):
"""Recurrence definition.
"""
frequencies = ["SECONDLY", "MINUTELY", "HOURLY", "DAILY", "WEEKLY",
"MONTHLY", "YEARLY"]
# Mac iCal ignores RRULEs where FREQ is not the first rule part.
# Sorts parts according to the order listed in RFC 5545, section 3.3.10.
canonical_order = ("FREQ", "UNTIL", "COUNT", "INTERVAL",
"BYSECOND", "BYMINUTE", "BYHOUR", "BYDAY",
"BYMONTHDAY", "BYYEARDAY", "BYWEEKNO", "BYMONTH",
"BYSETPOS", "WKST")
types = CaselessDict({
'COUNT': vInt,
'INTERVAL': vInt,
"""
def __new__(cls, value, encoding=DEFAULT_ENCODING):
value = to_unicode(value, encoding=encoding)
self = super(vInline, cls).__new__(cls, value)
self.params = Parameters()
return self
def to_ical(self):
return self.encode(DEFAULT_ENCODING)
@classmethod
def from_ical(cls, ical):
return cls(ical)
class TypesFactory(CaselessDict):
"""All Value types defined in rfc 2445 are registered in this factory
class.
The value and parameter names don't overlap. So one factory is enough for
both kinds.
"""
def __init__(self, *args, **kwargs):
"Set keys to upper for initial dict"
super(TypesFactory, self).__init__(*args, **kwargs)
self.all_types = (
vBinary,
vBoolean,
vCalAddress,
vDDDLists,
vDDDTypes,
self['integer'] = vInt
self['period'] = vPeriod
self['recur'] = vRecur
self['text'] = vText
self['time'] = vTime
self['uri'] = vUri
self['utc-offset'] = vUTCOffset
self['geo'] = vGeo
self['inline'] = vInline
self['date-time-list'] = vDDDLists
#################################################
# Property types
# These are the default types
types_map = CaselessDict({
####################################
# Property value types
# Calendar Properties
'calscale': 'text',
'method': 'text',
'prodid': 'text',
'version': 'text',
# Descriptive Component Properties
'attach': 'uri',
'categories': 'text',
'class': 'text',
'comment': 'text',
'description': 'text',
'geo': 'geo',
'location': 'text',
'percent-complete': 'integer',
self['VJOURNAL'] = Journal
self['VFREEBUSY'] = FreeBusy
self['VTIMEZONE'] = Timezone
self['VALARM'] = Alarm
self['VCALENDAR'] = Calendar
# These Properties have multiple property values inlined in one propertyline
# seperated by comma. Use CaselessDict as simple caseless set.
INLINE = CaselessDict(
[(cat, 1) for cat in ('CATEGORIES', 'RESOURCES', 'FREEBUSY')]
)
_marker = []
class Component(CaselessDict):
"""
Component is the base object for calendar, Event and the other components
defined in RFC 2445. normally you will not use this class directy, but
rather one of the subclasses.
A component is like a dictionary with extra methods and attributes.
>>> c = Component()
>>> c.name = 'VCALENDAR'
Every key defines a property. A property can consist of either a single
item. This can be set with a single value
>>> c['prodid'] = '-//max m//icalendar.mxm.dk/'
>>> c
VCALENDAR({'PRODID': '-//max m//icalendar.mxm.dk/'})
or with a list