def test_any_text_value_recode(value): esc = TextConv.serialize(value) assert TextConv.parse(esc) == value cl = ContentLine("TEST", value=esc) assert parse_contentline(cl.serialize()) == cl assert list(string_to_containers(cl.serialize())) == [cl] vals = [esc, esc, "test", esc] cl2 = ContentLine("TEST", value=TextConv.join_value_list(vals)) assert list(TextConv.split_value_list(cl2.value)) == vals assert parse_contentline(cl.serialize()) == cl assert list(string_to_containers(cl.serialize())) == [cl]
def parse_multiple(cls, string): """" Parses an input string that may contain mutiple calendars and retruns a list of :class:`ics.event.Calendar` """ containers = string_to_containers(string) return [cls(imports=c) for c in containers]
def test_example_text_recode(inp_esc, out_uesc): par_esc = parse_contentline(inp_esc) par_uesc = attr.evolve(par_esc, value=TextConv.parse(par_esc.value)) out_esc = attr.evolve(out_uesc, value=TextConv.serialize(out_uesc.value)) assert par_uesc == out_uesc ser_esc = out_esc.serialize() assert inp_esc == ser_esc assert list(string_to_containers(inp_esc)) == [par_esc]
def Timezone_from_tzid(tzid: str) -> Timezone: import ics_vtimezones # type: ignore tz_ics = ics_vtimezones.find_vtimezone_ics_file(tzid) if not tz_ics: olson_tzid = ics_vtimezones.windows_to_olson(tzid) if olson_tzid: tz_ics = ics_vtimezones.find_vtimezone_ics_file(olson_tzid) if not tz_ics: raise ValueError("no vTimezone.ics file found for %s" % tzid) ics_cal = one(string_to_containers(tz_ics.read_text())) if not (len(ics_cal) == 3 and ics_cal[2].name == "VTIMEZONE"): raise ValueError("vTimezone.ics file %s has invalid content" % tz_ics) return Timezone.from_container(ics_cal[2])
def __init__(self, imports: Union[str, Container, None] = None, events: Optional[Iterable[Event]] = None, todos: Optional[Iterable[Todo]] = None, creator: str = None, **kwargs): """Initializes a new Calendar. Args: imports (**str**): data to be imported into the Calendar, events (**Iterable[Event]**): `Event`s to be added to the calendar todos (**Iterable[Todo]**): `Todo`s to be added to the calendar creator (**string**): uid of the creator program. """ if events is None: events = tuple() if todos is None: todos = tuple() kwargs.setdefault("version", self.DEFAULT_VERSION) kwargs.setdefault( "prodid", creator if creator is not None else self.DEFAULT_PRODID) super(Calendar, self).__init__(events=events, todos=todos, **kwargs) # type: ignore[arg-type] self.timeline = Timeline(self, None) if imports is not None: if isinstance(imports, Container): self.populate(imports) else: if isinstance(imports, str): containers = iter(string_to_containers(imports)) else: containers = iter(lines_to_containers(imports)) try: container = next(containers) if not isinstance(container, Container): raise ValueError("can't populate from %s" % type(container)) self.populate(container) except StopIteration: raise ValueError("string didn't contain any ics data") try: next(containers) raise ValueError( "Multiple calendars in one file are not supported by this method." "Use ics.Calendar.parse_multiple()") except StopIteration: pass