class vDDDTypes(object): """A combined Datetime, Date or Duration parser/generator. Their format cannot be confused, and often values can be of either types. So this is practical. """ def __init__(self, dt): if not isinstance(dt, (datetime, date, timedelta, time)): raise ValueError('You must use datetime, date, timedelta or time') if isinstance(dt, datetime): self.params = Parameters({'value': 'DATE-TIME'}) elif isinstance(dt, date): self.params = Parameters({'value': 'DATE'}) elif isinstance(dt, time): self.params = Parameters({'value': 'TIME'}) if (isinstance(dt, datetime) or isinstance(dt, time))\ and getattr(dt, 'tzinfo', False): tzinfo = dt.tzinfo if tzinfo is not pytz.utc and\ (tzutc is None or not isinstance(tzinfo, tzutc)): # set the timezone as a parameter to the property tzid = tzid_from_dt(dt) if tzid: self.params.update({'TZID': tzid}) self.dt = dt def to_ical(self): dt = self.dt if isinstance(dt, datetime): return vDatetime(dt).to_ical() elif isinstance(dt, date): return vDate(dt).to_ical() elif isinstance(dt, timedelta): return vDuration(dt).to_ical() elif isinstance(dt, time): return vTime(dt).to_ical() else: raise ValueError('Unknown date type') @classmethod def from_ical(cls, ical, timezone=None): if isinstance(ical, cls): return ical.dt u = ical.upper() if u.startswith(('P', '-P', '+P')): return vDuration.from_ical(ical) if len(ical) in (15, 16): return vDatetime.from_ical(ical, timezone=timezone) elif len(ical) == 8: return vDate.from_ical(ical) elif len(ical) in (6, 7): return vTime.from_ical(ical) else: raise ValueError( "Expected datetime, date, or time, got: '%s'" % ical )
def __init__(self, dt): if not isinstance(dt, (datetime, date, timedelta, time)): raise ValueError('You must use datetime, date, timedelta or time') if isinstance(dt, datetime): self.params = Parameters(dict(value='DATE-TIME')) elif isinstance(dt, date): self.params = Parameters(dict(value='DATE')) elif isinstance(dt, time): self.params = Parameters(dict(value='TIME')) if (isinstance(dt, datetime) or isinstance(dt, time))\ and getattr(dt, 'tzinfo', False): tzinfo = dt.tzinfo if tzinfo is not pytz.utc and not isinstance(tzinfo, tzutc): # set the timezone as a parameter to the property tzid = tzid_from_dt(dt) if tzid: self.params.update({'TZID': tzid}) self.dt = dt
def __init__(self, dt): "Returns vDate from" if type(dt) not in (datetime, date, timedelta, time): raise ValueError("You must use datetime, date, timedelta or time") if isinstance(dt, datetime): self.params = Parameters(dict(value="DATE-TIME")) elif isinstance(dt, date): self.params = Parameters(dict(value="DATE")) elif isinstance(dt, time): self.params = Parameters(dict(value="TIME")) if (isinstance(dt, datetime) or isinstance(dt, time)) and getattr(dt, "tzinfo", False): tzinfo = dt.tzinfo if tzinfo is not pytz.utc and not isinstance(tzinfo, tzutc): # set the timezone as a parameter to the property tzid = tzinfo_from_dt(dt) if tzid: self.params.update({"TZID": tzid}) self.dt = dt
def test_issue_142(self): """Issue #142 - Multivalued parameters This is needed for VCard 3.0. https://github.com/collective/icalendar/pull/142 """ from icalendar.parser import Contentline, Parameters ctl = Contentline.from_ical("TEL;TYPE=HOME,VOICE:000000000") self.assertEqual( ctl.parts(), (u'TEL', Parameters({'TYPE': ['HOME', 'VOICE']}), u'000000000'), )
def __new__(cls, *args, **kwargs): self = super(vWeekday, cls).__new__(cls, *args, **kwargs) 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 not weekday in vWeekday.week_days or sign not in '+-': raise ValueError, 'Expected weekday abbrevation, got: %s' % self self.relative = relative and int(relative) or None self.params = Parameters() return self
def __init__(self, dt): if not isinstance(dt, (datetime, date, timedelta, time, tuple)): raise ValueError('You must use datetime, date, timedelta, ' 'time or tuple (for periods)') if isinstance(dt, datetime): self.params = Parameters({'value': 'DATE-TIME'}) elif isinstance(dt, date): self.params = Parameters({'value': 'DATE'}) elif isinstance(dt, time): self.params = Parameters({'value': 'TIME'}) elif isinstance(dt, tuple): self.params = Parameters({'value': 'PERIOD'}) if (isinstance(dt, datetime) or isinstance(dt, time))\ and getattr(dt, 'tzinfo', False): tzinfo = dt.tzinfo if tzinfo is not pytz.utc and\ (tzutc is None or not isinstance(tzinfo, tzutc)): # set the timezone as a parameter to the property tzid = tzid_from_dt(dt) if tzid: self.params.update({'TZID': tzid}) self.dt = dt
def __init__(self, dt_list): if not hasattr(dt_list, '__iter__'): dt_list = [dt_list] vDDD = [] tzid = None for dt in dt_list: dt = vDDDTypes(dt) vDDD.append(dt) if 'TZID' in dt.params: tzid = dt.params['TZID'] if tzid: # NOTE: no support for multiple timezones here! self.params = Parameters({'TZID': tzid}) self.dts = vDDD
def test_issue_143(self): """Issue #143 - Allow dots in property names. Another vCard related issue. https://github.com/collective/icalendar/pull/143 """ from icalendar.parser import Contentline, Parameters ctl = Contentline.from_ical( "ITEMADRNULLTHISISTHEADRESS08158SOMECITY12345.ADR:;;This is the Adress 08; Some City;;12345;Germany" ) # nopep8 self.assertEqual( ctl.parts(), (u'ITEMADRNULLTHISISTHEADRESS08158SOMECITY12345.ADR', Parameters(), u';;This is the Adress 08; Some City;;12345;Germany'), ) ctl2 = Contentline.from_ical( "ITEMADRNULLTHISISTHEADRESS08158SOMECITY12345.X-ABLABEL:" ) # nopep8 self.assertEqual( ctl2.parts(), (u'ITEMADRNULLTHISISTHEADRESS08158SOMECITY12345.X-ABLABEL', Parameters(), u''), )
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 not weekday in vWeekday.week_days or sign not in '+-': raise ValueError('Expected weekday abbrevation, got: %s' % self) self.relative = relative and int(relative) or None self.params = Parameters() return self
def set_event_info_from_vevent(instance, ev, kw_map, do_action=True): # Make sure a subtransaction happens. We might end up renaming the # event, and we don't really know if it was existing or new. transaction.savepoint(optimistic=True) info = {} for k, v in kw_map.items(): value = ev.get(k, None) if value is None: # Ugh, special case for dtend if k in ('dtend', ): # dtstart and duration *must* exist. dtstart = ev.get('dtstart') params = getattr(dtstart, 'params', {}) duration = ev.get('duration') if duration is None: dtend = dtstart.dt else: dtend = dtstart.dt + duration.dt value = vDDDTypes(dtend) value.params = Parameters(dict(params)) else: continue value = convert(value) info[v] = value instance.update(**info) uid = ev.decoded('uid', None) if uid is None: uid = generateUID(instance) setattr(instance, UID_ATTR, uid) if do_action: # XXX NAAAAASTY Code. We need to create a special # workflow for Event. This assumes the default Plone workflow. wf = getToolByName(instance, 'portal_workflow') state = wf.getInfoFor(instance, 'review_state') status = ev.get('status', 'CONFIRMED') if not re_status_mapping.get(state, 'CONFIRMED') == status: set_event_state_from_status(instance, status, wf) instance.reindexObject()
def __init__(self, per): start, end_or_duration = per if not (isinstance(start, datetime) or isinstance(start, date)): raise ValueError('Start value MUST be a datetime or date instance') if not (isinstance(end_or_duration, datetime) or isinstance(end_or_duration, date) or isinstance(end_or_duration, timedelta)): raise ValueError('end_or_duration MUST be a datetime, date or timedelta instance') self.start = start self.end_or_duration = end_or_duration self.by_duration = 0 if isinstance(end_or_duration, timedelta): self.by_duration = 1 self.duration = end_or_duration self.end = self.start + self.duration else: self.end = end_or_duration self.duration = self.end - self.start if self.start > self.end: raise ValueError("Start time is greater than end time") self.params = Parameters()
def _encode(self, name, value, parameters=None, encode=1): """Encode values to icalendar property values. :param name: Name of the property. :type name: string :param value: Value of the property. Either of a basic Python type of any of the icalendar's own property types. :type value: Python native type or icalendar property type. :param parameters: Property parameter dictionary for the value. Only available, if encode is set to True. :type parameters: Dictionary :param encode: True, if the value should be encoded to one of icalendar's own property types (Fallback is "vText") or False, if not. :type encode: Boolean :returns: icalendar property value """ if not encode: return value if isinstance(value, types_factory.all_types): # Don't encode already encoded values. return value klass = types_factory.for_property(name) obj = klass(value) if parameters: if isinstance(parameters, dict): params = Parameters() for key, item in parameters.items(): params[key] = item parameters = params assert isinstance(parameters, Parameters) obj.params = parameters return obj
class vDatetime: """ Render and generates icalendar datetime format. vDatetime is timezone aware and uses the pytz library, an implementation of the Olson database in Python. When a vDatetime object is created from an ical string, the string must be a valid pytz timezone identifier. When and vDatetime object is created from a python datetime object, it uses the tzinfo component, if present. Otherwise an timezone-naive object is created. Be aware that there are certain limitations with timezone naive DATE-TIME components in the icalendar standard. >>> from icalendar.tools import utctz >>> d = datetime(2001, 1,1, 12, 30, 0) >>> dt = vDatetime(d) >>> dt.to_ical() '20010101T123000' >>> vDatetime.from_ical('20000101T120000') datetime.datetime(2000, 1, 1, 12, 0) >>> dutc = datetime(2001, 1,1, 12, 30, 0, tzinfo=utctz()) >>> vDatetime(dutc).to_ical() '20010101T123000Z' >>> vDatetime.from_ical('20010101T000000') datetime.datetime(2001, 1, 1, 0, 0) >>> vDatetime.from_ical('20010101T000000A') Traceback (most recent call last): ... ValueError: Wrong datetime format: 20010101T000000A >>> utc = vDatetime.from_ical('20010101T000000Z') >>> vDatetime(utc).to_ical() '20010101T000000Z' >>> dat = vDatetime.from_ical('20101010T000000', 'Europe/Vienna') >>> vDatetime(dat).to_ical() '20101010T000000' """ def __init__(self, dt): self.dt = dt self.params = Parameters() def to_ical(self): dt = self.dt tzid = dt.tzinfo and dt.tzinfo.zone or None if tzid == 'UTC': return dt.strftime("%Y%m%dT%H%M%SZ") elif tzid: self.params.update({'TZID': tzid}) # return "TZID=%s;%s" % (timezone, self.dt.strftime("%Y%m%dT%H%M%S")) The timezone should not be printed with the date, but rather in the containing component. return dt.strftime("%Y%m%dT%H%M%S") def from_ical(ical, timezone=None): """ Parses the data format from ical text format. """ # TODO: ical string should better contain also the TZID property. if timezone: timezone = normalized_timezone(timezone) try: timetuple = map(int, (( ical[:4], # year ical[4:6], # month ical[6:8], # day ical[9:11], # hour ical[11:13], # minute ical[13:15], # second ))) if timezone: return datetime(tzinfo=timezone, *timetuple) elif not ical[15:]: return datetime(*timetuple) elif ical[15:16] == 'Z': return datetime(tzinfo=utctz(), *timetuple) else: raise ValueError, ical except: raise ValueError, 'Wrong datetime format: %s' % ical from_ical = staticmethod(from_ical)
class vDatetime: """ Render and generates icalendar datetime format. vDatetime is timezone aware and uses the pytz library, an implementation of the Olson database in Python. When a vDatetime object is created from an ical string, the string must be a valid pytz timezone identifier. When and vDatetime object is created from a python datetime object, it uses the tzinfo component, if present. Otherwise an timezone-naive object is created. Be aware that there are certain limitations with timezone naive DATE-TIME components in the icalendar standard. >>> d = datetime(2001, 1,1, 12, 30, 0) >>> dt = vDatetime(d) >>> dt.to_ical() '20010101T123000' >>> vDatetime.from_ical('20000101T120000') datetime.datetime(2000, 1, 1, 12, 0) >>> dutc = datetime(2001, 1,1, 12, 30, 0, tzinfo=pytz.utc) >>> vDatetime(dutc).to_ical() '20010101T123000Z' >>> vDatetime.from_ical('20010101T000000') datetime.datetime(2001, 1, 1, 0, 0) >>> vDatetime.from_ical('20010101T000000A') Traceback (most recent call last): ... ValueError: Wrong datetime format: 20010101T000000A >>> utc = vDatetime.from_ical('20010101T000000Z') >>> vDatetime(utc).to_ical() '20010101T000000Z' 1 minute before transition to DST >>> dat = vDatetime.from_ical('20120311T015959', 'America/Denver') >>> dat.strftime('%Y%m%d%H%M%S %z') '20120311015959 -0700' After transition to DST >>> dat = vDatetime.from_ical('20120311T030000', 'America/Denver') >>> dat.strftime('%Y%m%d%H%M%S %z') '20120311030000 -0600' >>> dat = vDatetime.from_ical('20101010T000000', 'Europe/Vienna') >>> vDatetime(dat).to_ical() '20101010T000000' """ def __init__(self, dt): self.dt = dt self.params = Parameters() def to_ical(self): dt = self.dt tzid = dt.tzinfo and dt.tzinfo.zone or None if tzid == 'UTC': return dt.strftime("%Y%m%dT%H%M%SZ") elif tzid: self.params.update({'TZID': tzid}) return dt.strftime("%Y%m%dT%H%M%S") def from_ical(ical, timezone=None): """ Parses the data format from ical text format. """ tzinfo = None if timezone: try: tzinfo = pytz.timezone(timezone) except pytz.UnknownTimeZoneError: pass try: timetuple = map(int, (( ical[:4], # year ical[4:6], # month ical[6:8], # day ical[9:11], # hour ical[11:13], # minute ical[13:15], # second ))) if tzinfo: return tzinfo.localize(datetime(*timetuple)) elif not ical[15:]: return datetime(*timetuple) elif ical[15:16] == 'Z': return datetime(tzinfo=pytz.utc, *timetuple) else: raise ValueError, ical except: raise ValueError, 'Wrong datetime format: %s' % ical from_ical = staticmethod(from_ical)
class vDatetime(object): """Render and generates icalendar datetime format. vDatetime is timezone aware and uses the pytz library, an implementation of the Olson database in Python. When a vDatetime object is created from an ical string, you can pass a valid pytz timezone identifier. When a vDatetime object is created from a python datetime object, it uses the tzinfo component, if present. Otherwise an timezone-naive object is created. Be aware that there are certain limitations with timezone naive DATE-TIME components in the icalendar standard. """ def __init__(self, dt): self.dt = dt self.params = Parameters() def to_ical(self): dt = self.dt tzid = tzid_from_dt(dt) s = "%04d%02d%02dT%02d%02d%02d" % ( dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second ) if tzid == 'UTC': s += "Z" elif tzid: self.params.update({'TZID': tzid}) return s.encode('utf-8') @staticmethod def from_ical(ical, timezone=None): tzinfo = None if timezone: try: tzinfo = pytz.timezone(timezone) except pytz.UnknownTimeZoneError: if timezone in WINDOWS_TO_OLSON: tzinfo = pytz.timezone(WINDOWS_TO_OLSON.get(timezone)) else: tzinfo = _timezone_cache.get(timezone, None) try: timetuple = ( int(ical[:4]), # year int(ical[4:6]), # month int(ical[6:8]), # day int(ical[9:11]), # hour int(ical[11:13]), # minute int(ical[13:15]), # second ) if tzinfo: return tzinfo.localize(datetime(*timetuple)) elif not ical[15:]: return datetime(*timetuple) elif ical[15:16] == 'Z': return pytz.utc.localize(datetime(*timetuple)) else: raise ValueError(ical) except: raise ValueError('Wrong datetime format: %s' % ical)
def content_line(self, name, value, sorted=True): """Returns property as content line. """ params = getattr(value, 'params', Parameters()) return Contentline.from_parts(name, params, value, sorted=sorted)
class vDDDTypes: """A combined Datetime, Date or Duration parser/generator. Their format cannot be confused, and often values can be of either types. So this is practical. >>> d = vDDDTypes.from_ical('20010101T123000') >>> type(d) <type 'datetime.datetime'> >>> repr(vDDDTypes.from_ical('20010101T123000Z'))[:65] 'datetime.datetime(2001, 1, 1, 12, 30, tzinfo=<UTC>)' >>> d = vDDDTypes.from_ical('20010101') >>> type(d) <type 'datetime.date'> >>> vDDDTypes.from_ical('P31D') datetime.timedelta(31) >>> vDDDTypes.from_ical('-P31D') datetime.timedelta(-31) Bad input >>> vDDDTypes(42) Traceback (most recent call last): ... ValueError: You must use datetime, date, timedelta or time """ def __init__(self, dt): "Returns vDate from" if type(dt) not in (datetime, date, timedelta, time): raise ValueError ('You must use datetime, date, timedelta or time') if isinstance(dt, datetime): self.params = Parameters(dict(value='DATE-TIME')) elif isinstance(dt, date): self.params = Parameters(dict(value='DATE')) elif isinstance(dt, time): self.params = Parameters(dict(value='TIME')) if (isinstance(dt, datetime) or isinstance(dt, time))\ and getattr(dt, 'tzinfo', False): tzinfo = dt.tzinfo if tzinfo is not pytz.utc and not isinstance(tzinfo, tzutc): # set the timezone as a parameter to the property tzid = tzid_from_dt(dt) if tzid: self.params.update({'TZID': tzid}) self.dt = dt def to_ical(self): dt = self.dt if isinstance(dt, datetime): return vDatetime(dt).to_ical() elif isinstance(dt, date): return vDate(dt).to_ical() elif isinstance(dt, timedelta): return vDuration(dt).to_ical() elif isinstance(dt, time): return vTime(dt).to_ical() else: raise ValueError('Unknown date type') @staticmethod def from_ical(ical, timezone=None): "Parses the data format from ical text format" if isinstance(ical, vDDDTypes): return ical.dt u = ical.upper() if u.startswith('-P') or u.startswith('P'): return vDuration.from_ical(ical) try: return vDatetime.from_ical(ical, timezone=timezone) except ValueError: try: return vDate.from_ical(ical) except ValueError: return vTime.from_ical(ical)
def __init__(self, *args, **kwargs): super(vRecur, self).__init__(*args, **kwargs) self.params = Parameters()
def __init__(self, *args, **kwargs): unicode.__init__(self, *args, **kwargs) self.params = Parameters()
def __init__(self, *args, **kwargs): if not self in vFrequency.frequencies: raise ValueError, 'Expected frequency, got: %s' % self self.params = Parameters()
def __init__(self, dt): if not isinstance(dt, date): raise ValueError('Value MUST be a date instance') self.dt = dt self.params = Parameters(dict(value='DATE'))
class vDDDTypes(object): """A combined Datetime, Date or Duration parser/generator. Their format cannot be confused, and often values can be of either types. So this is practical. """ def __init__(self, dt): if not isinstance(dt, (datetime, date, timedelta, time, tuple)): raise ValueError('You must use datetime, date, timedelta, ' 'time or tuple (for periods)') if isinstance(dt, datetime): self.params = Parameters({'value': 'DATE-TIME'}) elif isinstance(dt, date): self.params = Parameters({'value': 'DATE'}) elif isinstance(dt, time): self.params = Parameters({'value': 'TIME'}) elif isinstance(dt, tuple): self.params = Parameters({'value': 'PERIOD'}) if (isinstance(dt, datetime) or isinstance(dt, time))\ and getattr(dt, 'tzinfo', False): tzinfo = dt.tzinfo if tzinfo is not pytz.utc and\ (tzutc is None or not isinstance(tzinfo, tzutc)): # set the timezone as a parameter to the property tzid = tzid_from_dt(dt) if tzid: self.params.update({'TZID': tzid}) self.dt = dt def to_ical(self): dt = self.dt if isinstance(dt, datetime): return vDatetime(dt).to_ical() elif isinstance(dt, date): return vDate(dt).to_ical() elif isinstance(dt, timedelta): return vDuration(dt).to_ical() elif isinstance(dt, time): return vTime(dt).to_ical() elif isinstance(dt, tuple) and len(dt) == 2: return vPeriod(dt).to_ical() else: raise ValueError('Unknown date type: {}'.format(type(dt))) @classmethod def from_ical(cls, ical, timezone=None): if isinstance(ical, cls): return ical.dt u = ical.upper() if u.startswith(('P', '-P', '+P')): return vDuration.from_ical(ical) if '/' in u: return vPeriod.from_ical(ical) if len(ical) in (15, 16): return vDatetime.from_ical(ical, timezone=timezone) elif len(ical) == 8: return vDate.from_ical(ical) elif len(ical) in (6, 7): return vTime.from_ical(ical) else: raise ValueError("Expected datetime, date, or time, got: '%s'" % ical)
def __init__(self, obj): self.obj = to_unicode(obj) self.params = Parameters(encoding='BASE64', value="BINARY")
class vDatetime: """Render and generates icalendar datetime format. vDatetime is timezone aware and uses the pytz library, an implementation of the Olson database in Python. When a vDatetime object is created from an ical string, the string must be a valid pytz timezone identifier. When and vDatetime object is created from a python datetime object, it uses the tzinfo component, if present. Otherwise an timezone-naive object is created. Be aware that there are certain limitations with timezone naive DATE-TIME components in the icalendar standard. >>> d = datetime(2001, 1,1, 12, 30, 0) >>> dt = vDatetime(d) >>> dt.to_ical() '20010101T123000' >>> vDatetime.from_ical('20000101T120000') datetime.datetime(2000, 1, 1, 12, 0) >>> dutc = datetime(2001, 1,1, 12, 30, 0, tzinfo=pytz.utc) >>> vDatetime(dutc).to_ical() '20010101T123000Z' >>> vDatetime.from_ical('20010101T000000') datetime.datetime(2001, 1, 1, 0, 0) >>> vDatetime.from_ical('20010101T000000A') Traceback (most recent call last): ... ValueError: Wrong datetime format: 20010101T000000A >>> utc = vDatetime.from_ical('20010101T000000Z') >>> vDatetime(utc).to_ical() '20010101T000000Z' 1 minute before transition to DST >>> dat = vDatetime.from_ical('20120311T015959', 'America/Denver') >>> dat.strftime('%Y%m%d%H%M%S %z') '20120311015959 -0700' After transition to DST >>> dat = vDatetime.from_ical('20120311T030000', 'America/Denver') >>> dat.strftime('%Y%m%d%H%M%S %z') '20120311030000 -0600' >>> dat = vDatetime.from_ical('20101010T000000', 'Europe/Vienna') >>> vDatetime(dat).to_ical() '20101010T000000' """ def __init__(self, dt): self.dt = dt self.params = Parameters() def to_ical(self): dt = self.dt tzid = dt.tzinfo and dt.tzinfo.zone or None if tzid == 'UTC': return dt.strftime("%Y%m%dT%H%M%SZ") elif tzid: self.params.update({'TZID': tzid}) return dt.strftime("%Y%m%dT%H%M%S") def from_ical(ical, timezone=None): """ Parses the data format from ical text format. """ tzinfo = None if timezone: try: tzinfo = pytz.timezone(timezone) except pytz.UnknownTimeZoneError: pass try: timetuple = map( int, (( ical[:4], # year ical[4:6], # month ical[6:8], # day ical[9:11], # hour ical[11:13], # minute ical[13:15], # second ))) if tzinfo: return tzinfo.localize(datetime(*timetuple)) elif not ical[15:]: return datetime(*timetuple) elif ical[15:16] == 'Z': return datetime(tzinfo=pytz.utc, *timetuple) else: raise ValueError, ical except: raise ValueError, 'Wrong datetime format: %s' % ical from_ical = staticmethod(from_ical)
def __new__(cls, value): if isinstance(value, unicode): value = value.encode(DEFAULT_ENCODING) self = super(vCalAddress, cls).__new__(cls, value) self.params = Parameters() return self
def __new__(cls, *args, **kwargs): self = super(vInt, cls).__new__(cls, *args, **kwargs) self.params = Parameters() return self
def __init__(self, td): if not isinstance(td, timedelta): raise ValueError('Offset value MUST be a timedelta instance') self.td = td self.params = Parameters()
class vDatetime(object): """Render and generates icalendar datetime format. vDatetime is timezone aware and uses the pytz library, an implementation of the Olson database in Python. When a vDatetime object is created from an ical string, you can pass a valid pytz timezone identifier. When a vDatetime object is created from a python datetime object, it uses the tzinfo component, if present. Otherwise an timezone-naive object is created. Be aware that there are certain limitations with timezone naive DATE-TIME components in the icalendar standard. """ def __init__(self, dt): self.dt = dt self.params = Parameters() def to_ical(self): dt = self.dt tzid = tzid_from_dt(dt) s = "%04d%02d%02dT%02d%02d%02d" % ( dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second ) if tzid == 'UTC': s += "Z" elif tzid: self.params.update({'TZID': tzid}) return s.encode('utf-8') @staticmethod def from_ical(ical, timezone=None): tzinfo = None if timezone: try: tzinfo = pytz.timezone(timezone) except pytz.UnknownTimeZoneError: tzinfo = _timezone_cache.get(timezone, None) try: timetuple = ( int(ical[:4]), # year int(ical[4:6]), # month int(ical[6:8]), # day int(ical[9:11]), # hour int(ical[11:13]), # minute int(ical[13:15]), # second ) if tzinfo: return tzinfo.localize(datetime(*timetuple)) elif not ical[15:]: return datetime(*timetuple) elif ical[15:16] == 'Z': return pytz.utc.localize(datetime(*timetuple)) else: raise ValueError(ical) except: raise ValueError('Wrong datetime format: %s' % ical)
class vDatetime: """ Render and generates iCalendar datetime format. Important: if tzinfo is defined it renders itself as "date with utc time" Meaning that it has a 'Z' appended, and is in absolute time. >>> d = datetime(2001, 1,1, 12, 30, 0) >>> dt = vDatetime(d) >>> dt.to_ical() '20010101T123000' >>> vDatetime.from_ical('20000101T120000') datetime.datetime(2000, 1, 1, 12, 0) >>> dutc = datetime(2001, 1,1, 12, 30, 0, tzinfo=UTC) >>> vDatetime(dutc).to_ical() '20010101T123000Z' >>> vDatetime.from_ical('20010101T000000') datetime.datetime(2001, 1, 1, 0, 0) >>> vDatetime.from_ical('20010101T000000A') Traceback (most recent call last): ... ValueError: Wrong datetime format: 20010101T000000A >>> utc = vDatetime.from_ical('20010101T000000Z') >>> vDatetime(utc).to_ical() '20010101T000000Z' >>> dat = vDatetime.from_ical('20101010T000000', 'Europe/Vienna') >>> vDatetime(dat).to_ical() 'TZID=CET;20101010T000000' """ def __init__(self, dt): self.dt = dt self.params = Parameters() def to_ical(self): timezone = self.dt.tzname() if timezone == 'UTC' or self.dt.tzinfo == UTC: return self.dt.strftime("%Y%m%dT%H%M%SZ") elif timezone: self.params.update({'TZID': timezone}) return "TZID=%s;%s" % (timezone, self.dt.strftime("%Y%m%dT%H%M%S")) return self.dt.strftime("%Y%m%dT%H%M%S") def from_ical(ical, timezone=None): """ Parses the data format from ical text format. """ # TODO: ical string should better contain also the TZID property.deleter( if timezone: timezone = timezone_from_string(timezone) try: timetuple = map(int, (( ical[:4], # year ical[4:6], # month ical[6:8], # day ical[9:11], # hour ical[11:13], # minute ical[13:15], # second ))) if timezone: return datetime(tzinfo=timezone, *timetuple) elif not ical[15:]: return datetime(*timetuple) elif ical[15:16] == 'Z': timetuple += [0, UTC] return datetime(*timetuple) else: raise ValueError, ical except: raise ValueError, 'Wrong datetime format: %s' % ical from_ical = staticmethod(from_ical)
def __init__(self, dt): self.dt = dt self.params = Parameters()
def __init__(self, *args, **kwargs): float.__init__(self, *args, **kwargs) self.params = Parameters()
def __init__(self, *args, **kwargs): CaselessDict.__init__(self, *args, **kwargs) self.params = Parameters()
def __new__(cls, *args, **kwargs): self = super(vFrequency, cls).__new__(cls, *args, **kwargs) if not self in vFrequency.frequencies: raise ValueError, 'Expected frequency, got: %s' % self self.params = Parameters() return self
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 __init__(self, obj): self.obj = obj self.params = Parameters()
def __init__(self, *args, **kwargs): self.params = Parameters()