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 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 from_ical(cls, st, multiple=False): """Populates the component recursively from a string. """ stack = [] # a stack of components comps = [] previous_line = None for line in Contentlines.from_ical(st): # raw parsing if not line: continue try: name, params, vals = line.parts() previous_line = line except ValueError as err: # if unable to parse a line within a component # that ignores exceptions, mark the component # as broken and skip the line. otherwise raise. try: if previous_line and name in NEWLINE_FIX_NAMES: try: #Try to generate line from last name key (fix malformed line breaks) new_line = Contentline.from_ical( previous_line + escape_char('\n') + line) name, params, vals = new_line.parts() component = stack[-1] component.pop(name) previous_line = new_line except: raise else: raise except ValueError as err: component = stack[-1] if stack else None if not component or not component.ignore_exceptions: raise component.is_broken = True print "Broken: " + str(component) continue uname = name.upper() # check for start of component if uname == 'BEGIN': # try and create one of the components defined in the spec, # otherwise get a general Components for robustness. c_name = vals.upper() c_class = component_factory.get(c_name, cls) component = c_class() if not getattr(component, 'name', ''): # undefined components component.name = c_name stack.append(component) # check for end of event elif uname == 'END': # we are done adding properties to this component # so pop it from the stack and add it to the new top. component = stack.pop() if not stack: # we are at the end comps.append(component) else: if not component.is_broken: stack[-1].add_component(component) # we are adding properties to the current top of the stack else: factory = types_factory.for_property(name) component = stack[-1] datetime_names = ('DTSTART', 'DTEND', 'RECURRENCE-ID', 'DUE', 'FREEBUSY', 'RDATE', 'EXDATE') try: if name in datetime_names and 'TZID' in params: vals = factory(factory.from_ical(vals, params['TZID'])) else: vals = factory(factory.from_ical(vals)) except ValueError: if not component.ignore_exceptions: raise component.is_broken = True else: vals.params = params component.add(name, vals, encode=0) if multiple: return comps if len(comps) > 1: raise ValueError('Found multiple components where ' 'only one is allowed: {st!r}'.format(**locals())) if len(comps) < 1: #Fix missing END:VCALENDAR flag problem if len(stack)==1 and stack[-1].name == u'VCALENDAR': component = stack.pop() comps.append(component) else: raise ValueError('Found no components where ' 'exactly one is required: ' '{st!r}'.format(**locals())) return comps[0]
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 from_ical(cls, st, multiple=False): """Populates the component recursively from a string. """ stack = [] # a stack of components comps = [] previous_line = None for line in Contentlines.from_ical(st): # raw parsing if not line: continue try: name, params, vals = line.parts() previous_line = line except ValueError as err: # if unable to parse a line within a component # that ignores exceptions, mark the component # as broken and skip the line. otherwise raise. try: if previous_line and name in NEWLINE_FIX_NAMES: try: #Try to generate line from last name key (fix malformed line breaks) new_line = Contentline.from_ical( previous_line + escape_char('\n') + line) name, params, vals = new_line.parts() component = stack[-1] component.pop(name) previous_line = new_line except: raise else: raise except ValueError as err: component = stack[-1] if stack else None if not component or not component.ignore_exceptions: raise component.is_broken = True print "Broken: " + str(component) continue uname = name.upper() # check for start of component if uname == 'BEGIN': # try and create one of the components defined in the spec, # otherwise get a general Components for robustness. c_name = vals.upper() c_class = component_factory.get(c_name, cls) component = c_class() if not getattr(component, 'name', ''): # undefined components component.name = c_name stack.append(component) # check for end of event elif uname == 'END': # we are done adding properties to this component # so pop it from the stack and add it to the new top. component = stack.pop() if not stack: # we are at the end comps.append(component) else: if not component.is_broken: stack[-1].add_component(component) # we are adding properties to the current top of the stack else: factory = types_factory.for_property(name) component = stack[-1] datetime_names = ('DTSTART', 'DTEND', 'RECURRENCE-ID', 'DUE', 'FREEBUSY', 'RDATE', 'EXDATE') try: if name in datetime_names and 'TZID' in params: vals = factory(factory.from_ical(vals, params['TZID'])) else: vals = factory(factory.from_ical(vals)) except ValueError: if not component.ignore_exceptions: raise component.is_broken = True else: vals.params = params component.add(name, vals, encode=0) if multiple: return comps if len(comps) > 1: raise ValueError('Found multiple components where ' 'only one is allowed: {st!r}'.format(**locals())) if len(comps) < 1: #Fix missing END:VCALENDAR flag problem if len(stack) == 1 and stack[-1].name == u'VCALENDAR': component = stack.pop() comps.append(component) else: raise ValueError('Found no components where ' 'exactly one is required: ' '{st!r}'.format(**locals())) return comps[0]