icalendar.parser module#
This module parses and generates contentlines as defined in RFC 5545 (iCalendar), but will probably work for other MIME types with similar syntax. Eg. RFC 2426 (vCard)
It is stupid in the sense that it treats the content purely as strings. No type conversion is attempted.
- class icalendar.parser.Contentline(value, strict=False, encoding='utf-8')[source]#
Bases:
strA content line is basically a string that can be folded and parsed into parts.
- classmethod from_ical(ical, strict=False)[source]#
Unfold the content lines in an iCalendar into long content lines.
- classmethod from_parts(name, params, values, sorted=True)[source]#
Turn a parts into a content line.
- parts()[source]#
Split the content line into
name,parameters, andvaluesparts.Properly handles escaping with backslashes and double-quote sections to avoid corrupting URL-encoded characters in values.
Example with parameter:
DESCRIPTION;ALTREP="cid:part1.0001@example.org":The Fall'98 Wild
Example without parameters:
DESCRIPTION:The Fall'98 Wild
- Return type:
tuple[str,Parameters,str]
- strict#
- class icalendar.parser.Contentlines(iterable=(), /)[source]#
Bases:
listI assume that iCalendar files generally are a few kilobytes in size. Then this should be efficient. for Huge files, an iterator should probably be used instead.
- class icalendar.parser.HasToIcal(*args, **kwargs)[source]#
Bases:
ProtocolProtocol for objects with a to_ical method.
- __init__(*args, **kwargs)#
- class icalendar.parser.Parameters(*args, **kwargs)[source]#
Bases:
CaselessDictParser and generator of Property parameter strings.
It knows nothing of datatypes. Its main concern is textual structure.
Examples
Modify parameters:
>>> from icalendar import Parameters >>> params = Parameters() >>> params['VALUE'] = 'TEXT' >>> params.value 'TEXT' >>> params Parameters({'VALUE': 'TEXT'})
Create new parameters:
>>> params = Parameters(value="BINARY") >>> params.value 'BINARY'
Set a default:
>>> params = Parameters(value="BINARY", default_value="TEXT") >>> params Parameters({'VALUE': 'BINARY'})
- always_quoted = ('ALTREP', 'DELEGATED-FROM', 'DELEGATED-TO', 'DIR', 'MEMBER', 'SENT-BY', 'X-ADDRESS', 'X-TITLE', 'LINKREL')#
- params()[source]#
In RFC 5545 keys are called parameters, so this is to be consitent with the naming conventions.
- quote_also = {'CN': " '"}#
- update_tzid_from(dt)[source]#
Update the TZID parameter from a datetime object.
This sets the TZID parameter or deletes it according to the datetime.
- Return type:
- property value: VALUE | str | None#
The VALUE parameter from RFC 5545.
- Description:
This parameter specifies the value type and format of the property value. The property values MUST be of a single value type. For example, a "RDATE" property cannot have a combination of DATE-TIME and TIME value types.
If the property's value is the default value type, then this parameter need not be specified. However, if the property's default value type is overridden by some other allowable value type, then this parameter MUST be specified.
Applications MUST preserve the value data for x-name and iana- token values that they don't recognize without attempting to interpret or parse the value data.
For convenience, using this property, the value will be converted to an uppercase string.
>>> from icalendar import Parameters >>> params = Parameters() >>> params.value = "unknown" >>> params Parameters({'VALUE': 'UNKNOWN'})
- icalendar.parser.dquote(val, always_quote=False)[source]#
Enclose parameter values in double quotes when needed.
Parameter values containing special characters
,,;,:, or'must be enclosed in double quotes according to RFC 5545. Double-quote characters in the value are replaced with single quotes since they're forbidden in parameter values.
- icalendar.parser.escape_char(text)[source]#
Format value according to iCalendar TEXT escaping rules.
Escapes special characters in text values according to RFC 5545 Section 3.3.11 rules. The order of replacements matters to avoid double-escaping.
- Parameters:
- Return type:
- Returns:
The escaped text with special characters escaped.
Note
The replacement order is critical:
\N->\n(normalize newlines to lowercase)\->\\(escape backslashes);->\;(escape semicolons),->\,(escape commas)\r\n->\n(normalize line endings)"\n"->r"\n"(transform a newline character to a literal, or raw, newline character)
- icalendar.parser.escape_string(val)[source]#
Escape backslash sequences to URL-encoded hex values.
Converts backslash-escaped characters to their percent-encoded hex equivalents. This is used for parameter parsing to preserve escaped characters during processing.
- Parameters:
val (
str) -- The string with backslash escapes.- Return type:
- Returns:
The string with backslash escapes converted to percent encoding.
Note
Conversions:
\,->%2C\:->%3A\;->%3B\\->%5C
- icalendar.parser.foldline(line, limit=75, fold_sep='\\r\\n ')[source]#
Make a string folded as defined in RFC5545 Lines of text SHOULD NOT be longer than 75 octets, excluding the line break. Long content lines SHOULD be split into a multiple line representations using a line "folding" technique. That is, a long line can be split between any two characters by inserting a CRLF immediately followed by a single linear white-space character (i.e., SPACE or HTAB).
- Return type:
- icalendar.parser.param_value(value, always_quote=False)[source]#
Convert a parameter value to its iCalendar representation.
Applies RFC 6868 escaping and optionally quotes the value according to RFC 5545 parameter value formatting rules.
- Parameters:
- Return type:
- Returns:
The formatted parameter value, escaped and quoted as needed.
- icalendar.parser.q_join(lst, sep=',', always_quote=False)[source]#
Join a list with a separator, quoting items as needed.
Joins list items with the separator, applying
dquote()to each item to add double quotes when they contain special characters.- Parameters:
- Return type:
- Returns:
The joined string with items quoted as needed.
Examples
>>> from icalendar.parser import q_join >>> q_join(['a', 'b', 'c']) 'a,b,c' >>> q_join(['plain', 'has,comma']) 'plain,"has,comma"'
- icalendar.parser.q_split(st, sep=',', maxsplit=-1)[source]#
Split a string on a separator, respecting double quotes.
Splits the string on the separator character, but ignores separators that appear inside double-quoted sections. This is needed for parsing parameter values that may contain quoted strings.
- Parameters:
- Return type:
- Returns:
The split string parts.
Examples
>>> from icalendar.parser import q_split >>> q_split('a,b,c') ['a', 'b', 'c'] >>> q_split('a,"b,c",d') ['a', '"b,c"', 'd'] >>> q_split('a;b;c', sep=';') ['a', 'b', 'c']
- icalendar.parser.rfc_6868_escape(param_value)[source]#
Take care of RFC 6868 escaping.
^ -> ^^
" -> ^'
newline -> ^n
- Return type:
- icalendar.parser.rfc_6868_unescape(param_value)[source]#
Take care of RFC 6868 unescaping.
^^ -> ^
^n -> system specific newline
^' -> "
^ with others stay intact
- Return type:
- icalendar.parser.single_string_parameter(func=None, upper=False)[source]#
Create a parameter getter/setter for a single string parameter.
- icalendar.parser.split_on_unescaped_comma(text)[source]#
Split text on unescaped commas and unescape each part.
Splits only on commas not preceded by backslash. After splitting, unescapes backslash sequences in each part.
- Parameters:
text (
str) -- Text with potential escaped commas (e.g., "foo\, bar,baz")- Return type:
- Returns:
List of unescaped category strings
Examples
>>> from icalendar.parser import split_on_unescaped_comma >>> split_on_unescaped_comma(r"foo\, bar,baz") ['foo, bar', 'baz'] >>> split_on_unescaped_comma("a,b,c") ['a', 'b', 'c'] >>> split_on_unescaped_comma(r"a\,b\,c") ['a,b,c'] >>> split_on_unescaped_comma(r"Work,Personal\,Urgent") ['Work', 'Personal,Urgent']
- icalendar.parser.split_on_unescaped_semicolon(text)[source]#
Split text on unescaped semicolons and unescape each part.
Splits only on semicolons not preceded by a backslash. After splitting, unescapes backslash sequences in each part. Used by vCard structured properties (ADR, N, ORG) per RFC 6350.
- Parameters:
text (
str) -- Text with potential escaped semicolons (e.g., "field1\;with;field2")- Return type:
- Returns:
List of unescaped field strings
Examples
>>> from icalendar.parser import split_on_unescaped_semicolon >>> split_on_unescaped_semicolon(r"field1\;with;field2") ['field1;with', 'field2'] >>> split_on_unescaped_semicolon("a;b;c") ['a', 'b', 'c'] >>> split_on_unescaped_semicolon(r"a\;b\;c") ['a;b;c'] >>> split_on_unescaped_semicolon(r"PO Box 123\;Suite 200;City") ['PO Box 123;Suite 200', 'City']
- icalendar.parser.unescape_backslash(val)[source]#
Unescape backslash sequences in iCalendar text.
Unlike
unescape_string(), this only handles actual backslash escapes per RFC 5545, not URL encoding. This preserves URL-encoded values like%3Ain URLs.Processes backslash escape sequences in a single pass using regex matching.
- icalendar.parser.unescape_char(text)[source]#
Unescape iCalendar TEXT values.
Reverses the escaping applied by
escape_char()according to RFC 5545 Section 3.3.11 TEXT escaping rules.- Parameters:
- Return type:
- Returns:
The unescaped text, or
Noneiftextis neitherstrnorbytes.
Note
The replacement order is critical to avoid double-unescaping:
\N->\n(intermediate step)\r\n->\n(normalize line endings)\n-> newline (unescape newlines)\,->,(unescape commas)\;->;(unescape semicolons)\\->\(unescape backslashes last)
- icalendar.parser.unescape_list_or_string(val)[source]#
Unescape a value that may be a string or list of strings.
Applies
unescape_string()to the value. If the value is a list, unescapes each element.
- icalendar.parser.unescape_string(val)[source]#
Unescape URL-encoded hex values to their original characters.
Reverses
escape_string()by converting percent-encoded hex values back to their original characters. This is used for parameter parsing.- Parameters:
val (
str) -- The string with percent-encoded values.- Return type:
- Returns:
The string with percent encoding converted to characters.
Note
Conversions:
%2C->,%3A->:%3B->;%5C->\
- icalendar.parser.validate_param_value(value, quoted=True)[source]#
Validate a parameter value for unsafe characters.
Checks parameter values for characters that are not allowed according to RFC 5545. Uses different validation rules for quoted and unquoted values.
- Parameters:
- Raises:
ValueError -- If the value contains unsafe characters for its quote state.
- Return type:
- icalendar.parser.validate_token(name)[source]#
Validate that a name is a valid iCalendar token.
Checks if the name matches the RFC 5545 token syntax using the NAME regex pattern (
[\w.-]+).- Parameters:
name (
str) -- The token name to validate.- Raises:
ValueError -- If the name is not a valid token.
- Return type: