def uid(self, host_name='example.com', unique=''): """Generates a unique id consisting of: datetime-uniquevalue@host. Like: [email protected] """ host_name = to_unicode(host_name) unique = unique or self.rnd_string() today = to_unicode(vDatetime(datetime.today()).to_ical()) return vText('%s-%s@%s' % (today, unique, host_name))
def __new__(cls, value, encoding=DEFAULT_ENCODING): value = to_unicode(value, encoding=encoding) self = super(vFrequency, cls).__new__(cls, value) if not self in vFrequency.frequencies: raise ValueError('Expected frequency, got: %s' % self) self.params = Parameters() return self
def __new__(cls, value, strict=False, encoding=DEFAULT_ENCODING): value = to_unicode(value, encoding=encoding) assert u'\n' not in value, ('Content line can not contain unescaped ' 'new line characters.') self = super(Contentline, cls).__new__(cls, value) self.strict = strict return self
def test_issue_112(self): """Issue #112 - No timezone info on EXDATE https://github.com/collective/icalendar/issues/112 """ directory = os.path.dirname(__file__) path = os.path.join(directory, 'issue_112_missing_tzinfo_on_exdate.ics') with open(path, 'rb') as ics: cal = icalendar.Calendar.from_ical(ics.read()) event = cal.walk('VEVENT')[0] event_ical = to_unicode(event.to_ical()) # Py3 str type doesn't # support buffer API # General timezone aware dates in ical string self.assertTrue('DTSTART;TZID=America/New_York:20130907T120000' in event_ical) self.assertTrue('DTEND;TZID=America/New_York:20130907T170000' in event_ical) # Specific timezone aware exdates in ical string self.assertTrue('EXDATE;TZID=America/New_York:20131012T120000' in event_ical) self.assertTrue('EXDATE;TZID=America/New_York:20131011T120000' in event_ical) self.assertEqual(event['exdate'][0].dts[0].dt.tzname(), 'EDT')
def test_parser_tools_to_unicode(self): self.assertEqual(to_unicode(b'spam'), 'spam') self.assertEqual(to_unicode('spam'), 'spam') self.assertEqual(to_unicode('spam'.encode('utf-8')), 'spam') self.assertEqual(to_unicode(b'\xc6\xb5'), '\u01b5') self.assertEqual(to_unicode('\xc6\xb5'.encode('iso-8859-1')), '\u01b5') self.assertEqual(to_unicode(b'\xc6\xb5', encoding='ascii'), '\u01b5') self.assertEqual(to_unicode(1), 1) self.assertEqual(to_unicode(None), None)
def __init__(self, *args, **kwargs): """Set keys to upper for initial dict. """ super(CaselessDict, self).__init__(*args, **kwargs) for key, value in self.items(): key_upper = to_unicode(key).upper() if key != key_upper: super(CaselessDict, self).__delitem__(key) self[key_upper] = value
def from_parts(cls, name, params, values, sorted=True): """Turn a parts into a content line. """ assert isinstance(params, Parameters) if hasattr(values, 'to_ical'): values = values.to_ical() else: values = vText(values).to_ical() # elif isinstance(values, basestring): # values = escape_char(values) # TODO: after unicode only, remove this # Convert back to unicode, after to_ical encoded it. name = to_unicode(name) values = to_unicode(values) if params: params = to_unicode(params.to_ical(sorted=sorted)) return cls(u'%s;%s:%s' % (name, params, values)) return cls(u'%s:%s' % (name, values))
def from_ical(cls, st): """Parses a string into content lines. """ st = to_unicode(st) try: # a fold is carriage return followed by either a space or a tab unfolded = uFOLD.sub('', st) lines = cls(Contentline(line) for line in unfolded.splitlines() if line) lines.append('') # '\r\n' at the end of every content line return lines except: raise ValueError('Expected StringType with content lines')
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 pop(self, key, default=None): key = to_unicode(key) return super(CaselessDict, self).pop(key.upper(), default)
def pop(self, key, default=None): key = to_unicode(key) return dict.pop(self, key.upper(), default)
def setdefault(self, key, value=None): key = to_unicode(key) return super(CaselessDict, self).setdefault(key.upper(), value)
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 __getitem__(self, key): key = to_unicode(key) return dict.__getitem__(self, key.upper())
def has_key(self, key): key = to_unicode(key) return super(CaselessDict, self).__contains__(key.upper())
def setdefault(self, key, value=None): key = to_unicode(key) return dict.setdefault(self, key.upper(), value)
def __setitem__(self, key, value): key = to_unicode(key) super(CaselessDict, self).__setitem__(key.upper(), value)
def has_key(self, key): key = to_unicode(key) return dict.__contains__(self, key.upper())
def __delitem__(self, key): key = to_unicode(key) dict.__delitem__(self, key.upper())
def __setitem__(self, key, value): key = to_unicode(key) dict.__setitem__(self, key.upper(), value)
def from_ical(cls, ical, strict=False): """Unfold the content lines in an iCalendar into long content lines. """ ical = to_unicode(ical) # a fold is carriage return followed by either a space or a tab return cls(uFOLD.sub('', ical), strict=strict)
def __getitem__(self, key): key = to_unicode(key) return super(CaselessDict, self).__getitem__(key.upper())
def __delitem__(self, key): key = to_unicode(key) super(CaselessDict, self).__delitem__(key.upper())
def __init__(self, obj): self.obj = to_unicode(obj) self.params = Parameters(encoding='BASE64', value="BINARY")