Back to index

plone3  3.1.7
Public Member Functions | Static Public Attributes
icalendar.parser.Contentline Class Reference

List of all members.

Public Member Functions

def __new__
def from_parts
def parts
def from_string
def __str__

Static Public Attributes

tuple from_parts = staticmethod(from_parts)
tuple from_string = staticmethod(from_string)

Detailed Description

A content line is basically a string that can be folded and parsed into
parts.

>>> c = Contentline('Si meliora dies, ut vina, poemata reddit')
>>> str(c)
'Si meliora dies, ut vina, poemata reddit'

A long line gets folded
>>> c = Contentline(''.join(['123456789 ']*10))
>>> str(c)
'123456789 123456789 123456789 123456789 123456789 123456789 123456789 1234\\r\\n 56789 123456789 123456789 '

A folded line gets unfolded
>>> c = Contentline.from_string(str(c))
>>> c
'123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 '

It can parse itself into parts. Which is a tuple of (name, params, vals)

>>> c = Contentline('dtstart:20050101T120000')
>>> c.parts()
('dtstart', Parameters({}), '20050101T120000')

>>> c = Contentline('dtstart;value=datetime:20050101T120000')
>>> c.parts()
('dtstart', Parameters({'VALUE': 'datetime'}), '20050101T120000')

>>> c = Contentline('ATTENDEE;CN=Max Rasmussen;ROLE=REQ-PARTICIPANT:MAILTO:maxm@example.com')
>>> c.parts()
('ATTENDEE', Parameters({'ROLE': 'REQ-PARTICIPANT', 'CN': 'Max Rasmussen'}), 'MAILTO:maxm@example.com')
>>> str(c)
'ATTENDEE;CN=Max Rasmussen;ROLE=REQ-PARTICIPANT:MAILTO:maxm@example.com'

and back again
>>> parts = ('ATTENDEE', Parameters({'ROLE': 'REQ-PARTICIPANT', 'CN': 'Max Rasmussen'}), 'MAILTO:maxm@example.com')
>>> Contentline.from_parts(parts)
'ATTENDEE;CN=Max Rasmussen;ROLE=REQ-PARTICIPANT:MAILTO:maxm@example.com'

and again
>>> parts = ('ATTENDEE', Parameters(), 'MAILTO:maxm@example.com')
>>> Contentline.from_parts(parts)
'ATTENDEE:MAILTO:maxm@example.com'

A value can also be any of the types defined in PropertyValues
>>> from icalendar.prop import vText
>>> parts = ('ATTENDEE', Parameters(), vText('MAILTO:test@example.com'))
>>> Contentline.from_parts(parts)
'ATTENDEE:MAILTO:test@example.com'

A value can also be unicode
>>> from icalendar.prop import vText
>>> parts = ('SUMMARY', Parameters(), vText(u'INternational char   '))
>>> Contentline.from_parts(parts)
'SUMMARY:INternational char \\xc3\\xa6 \\xc3\\xb8 \\xc3\\xa5'

Traversing could look like this.
>>> name, params, vals = c.parts()
>>> name
'ATTENDEE'
>>> vals
'MAILTO:maxm@example.com'
>>> for key, val in params.items():
...     (key, val)
('ROLE', 'REQ-PARTICIPANT')
('CN', 'Max Rasmussen')

And the traditional failure
>>> c = Contentline('ATTENDEE;maxm@example.com')
>>> c.parts()
Traceback (most recent call last):
    ...
ValueError: Content line could not be parsed into parts

Another failure:
>>> c = Contentline(':maxm@example.com')
>>> c.parts()
Traceback (most recent call last):
    ...
ValueError: Content line could not be parsed into parts

>>> c = Contentline('key;param=:value')
>>> c.parts()
('key', Parameters({'PARAM': ''}), 'value')

>>> c = Contentline('key;param="pvalue":value')
>>> c.parts()
('key', Parameters({'PARAM': 'pvalue'}), 'value')

Should bomb on missing param:
>>> c = Contentline.from_string("k;:no param")
>>> c.parts()
Traceback (most recent call last):
    ...
ValueError: Content line could not be parsed into parts

>>> c = Contentline('key;param=pvalue:value', strict=False)
>>> c.parts()
('key', Parameters({'PARAM': 'pvalue'}), 'value')

If strict is set to True, uppercase param values that are not
double-quoted, this is because the spec says non-quoted params are
case-insensitive.

>>> c = Contentline('key;param=pvalue:value', strict=True)
>>> c.parts()
('key', Parameters({'PARAM': 'PVALUE'}), 'value')

>>> c = Contentline('key;param="pValue":value', strict=True)
>>> c.parts()
('key', Parameters({'PARAM': 'pValue'}), 'value')

Definition at line 245 of file parser.py.


Member Function Documentation

def icalendar.parser.Contentline.__new__ (   cls,
  st,
  strict = False 
)

Definition at line 359 of file parser.py.

00359 
00360     def __new__(cls, st, strict=False):
00361         self = str.__new__(cls, st)
00362         setattr(self, 'strict', strict)
00363         return self

Definition at line 416 of file parser.py.

00416 
00417     def __str__(self):
00418         "Long content lines are folded so they are less than 75 characters wide"
00419         l_line = len(self)
00420         new_lines = []
00421         for i in range(0, l_line, 74):
00422             new_lines.append(self[i:i+74])
00423         return '\r\n '.join(new_lines)
00424 
00425 

Definition at line 364 of file parser.py.

00364 
00365     def from_parts(parts):
00366         "Turns a tuple of parts into a content line"
00367         (name, params, values) = [str(p) for p in parts]
00368         try:
00369             if params:
00370                 return Contentline('%s;%s:%s' % (name, params, values))
00371             return Contentline('%s:%s' %  (name, values))
00372         except:
00373             raise ValueError(
00374                 'Property: %s Wrong values "%s" or "%s"' % (repr(name),
00375                                                             repr(params),
                                                            repr(values)))
def icalendar.parser.Contentline.from_string (   st,
  strict = False 
)

Definition at line 407 of file parser.py.

00407 
00408     def from_string(st, strict=False):
00409         "Unfolds the content lines in an iCalendar into long content lines"
00410         try:
00411             # a fold is carriage return followed by either a space or a tab
00412             return Contentline(FOLD.sub('', st), strict=strict)
00413         except:
            raise ValueError, 'Expected StringType with content line'
Splits the content line up into (name, parameters, values) parts

Definition at line 378 of file parser.py.

00378 
00379     def parts(self):
00380         """ Splits the content line up into (name, parameters, values) parts
00381         """
00382         try:
00383             name_split = None
00384             value_split = None
00385             inquotes = 0
00386             for i in range(len(self)):
00387                 ch = self[i]
00388                 if not inquotes:
00389                     if ch in ':;' and not name_split:
00390                         name_split = i
00391                     if ch == ':' and not value_split:
00392                         value_split = i
00393                 if ch == '"':
00394                     inquotes = not inquotes
00395             name = self[:name_split]
00396             if not name:
00397                 raise ValueError, 'Key name is required'
00398             validate_token(name)
00399             if name_split+1 == value_split:
00400                 raise ValueError, 'Invalid content line'
00401             params = Parameters.from_string(self[name_split+1:value_split],
00402                                             strict=self.strict)
00403             values = self[value_split+1:]
00404             return (name, params, values)
00405         except:
00406             raise ValueError, 'Content line could not be parsed into parts'

Here is the call graph for this function:


Member Data Documentation

tuple icalendar.parser.Contentline.from_parts = staticmethod(from_parts) [static]

Definition at line 376 of file parser.py.

tuple icalendar.parser.Contentline.from_string = staticmethod(from_string) [static]

Definition at line 414 of file parser.py.


The documentation for this class was generated from the following file: