paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | """ |
paul@0 | 4 | Parsing of vCard, vCalendar and iCalendar files. |
paul@0 | 5 | |
paul@26 | 6 | Copyright (C) 2005, 2006, 2007, 2008, 2009, 2011, 2013 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 7 | |
paul@0 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@14 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@14 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@14 | 11 | version. |
paul@0 | 12 | |
paul@0 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@0 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@14 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@0 | 16 | details. |
paul@0 | 17 | |
paul@14 | 18 | You should have received a copy of the GNU General Public License along with |
paul@14 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@0 | 20 | |
paul@0 | 21 | -------- |
paul@0 | 22 | |
paul@0 | 23 | References: |
paul@0 | 24 | |
paul@16 | 25 | RFC 5545: Internet Calendaring and Scheduling Core Object Specification |
paul@16 | 26 | (iCalendar) |
paul@18 | 27 | http://tools.ietf.org/html/rfc5545 |
paul@16 | 28 | |
paul@0 | 29 | RFC 2445: Internet Calendaring and Scheduling Core Object Specification |
paul@0 | 30 | (iCalendar) |
paul@18 | 31 | http://tools.ietf.org/html/rfc2445 |
paul@0 | 32 | |
paul@0 | 33 | RFC 2425: A MIME Content-Type for Directory Information |
paul@18 | 34 | http://tools.ietf.org/html/rfc2425 |
paul@0 | 35 | |
paul@0 | 36 | RFC 2426: vCard MIME Directory Profile |
paul@18 | 37 | http://tools.ietf.org/html/rfc2426 |
paul@0 | 38 | """ |
paul@0 | 39 | |
paul@4 | 40 | try: |
paul@4 | 41 | set |
paul@4 | 42 | except NameError: |
paul@4 | 43 | from sets import Set as set |
paul@4 | 44 | |
paul@0 | 45 | # Encoding-related imports. |
paul@0 | 46 | |
paul@0 | 47 | import base64, quopri |
paul@9 | 48 | import codecs |
paul@0 | 49 | |
paul@4 | 50 | # Tokenisation help. |
paul@4 | 51 | |
paul@4 | 52 | import re |
paul@4 | 53 | |
paul@9 | 54 | # Configuration. |
paul@9 | 55 | |
paul@9 | 56 | default_encoding = "utf-8" |
paul@9 | 57 | |
paul@7 | 58 | # Reader and parser classes. |
paul@0 | 59 | |
paul@0 | 60 | class Reader: |
paul@0 | 61 | |
paul@0 | 62 | "A simple class wrapping a file, providing simple pushback capabilities." |
paul@0 | 63 | |
paul@0 | 64 | def __init__(self, f, non_standard_newline=0): |
paul@0 | 65 | |
paul@0 | 66 | """ |
paul@0 | 67 | Initialise the object with the file 'f'. If 'non_standard_newline' is |
paul@0 | 68 | set to a true value (unlike the default), lines ending with CR will be |
paul@0 | 69 | treated as complete lines. |
paul@0 | 70 | """ |
paul@0 | 71 | |
paul@0 | 72 | self.f = f |
paul@0 | 73 | self.non_standard_newline = non_standard_newline |
paul@0 | 74 | self.lines = [] |
paul@8 | 75 | self.line_number = 1 # about to read line 1 |
paul@0 | 76 | |
paul@9 | 77 | def close(self): |
paul@9 | 78 | |
paul@9 | 79 | "Close the reader." |
paul@9 | 80 | |
paul@9 | 81 | self.f.close() |
paul@9 | 82 | |
paul@0 | 83 | def pushback(self, line): |
paul@0 | 84 | |
paul@0 | 85 | """ |
paul@0 | 86 | Push the given 'line' back so that the next line read is actually the |
paul@0 | 87 | given 'line' and not the next line from the underlying file. |
paul@0 | 88 | """ |
paul@0 | 89 | |
paul@0 | 90 | self.lines.append(line) |
paul@0 | 91 | self.line_number -= 1 |
paul@0 | 92 | |
paul@0 | 93 | def readline(self): |
paul@0 | 94 | |
paul@0 | 95 | """ |
paul@0 | 96 | If no pushed-back lines exist, read a line directly from the file. |
paul@0 | 97 | Otherwise, read from the list of pushed-back lines. |
paul@0 | 98 | """ |
paul@0 | 99 | |
paul@0 | 100 | self.line_number += 1 |
paul@0 | 101 | if self.lines: |
paul@0 | 102 | return self.lines.pop() |
paul@0 | 103 | else: |
paul@11 | 104 | # Sanity check for broken lines (\r instead of \r\n or \n). |
paul@0 | 105 | line = self.f.readline() |
paul@0 | 106 | while line.endswith("\r") and not self.non_standard_newline: |
paul@0 | 107 | line += self.f.readline() |
paul@0 | 108 | if line.endswith("\r") and self.non_standard_newline: |
paul@0 | 109 | return line + "\n" |
paul@0 | 110 | else: |
paul@0 | 111 | return line |
paul@0 | 112 | |
paul@8 | 113 | def read_content_line(self): |
paul@0 | 114 | |
paul@0 | 115 | """ |
paul@8 | 116 | Read an entire content line, itself potentially consisting of many |
paul@11 | 117 | physical lines of text, returning a string. |
paul@0 | 118 | """ |
paul@0 | 119 | |
paul@9 | 120 | # Skip blank lines. |
paul@9 | 121 | |
paul@8 | 122 | line = self.readline() |
paul@9 | 123 | while line: |
paul@9 | 124 | line_stripped = line.rstrip("\r\n") |
paul@9 | 125 | if not line_stripped: |
paul@9 | 126 | line = self.readline() |
paul@9 | 127 | else: |
paul@9 | 128 | break |
paul@9 | 129 | else: |
paul@9 | 130 | return "" |
paul@0 | 131 | |
paul@8 | 132 | # Strip all appropriate whitespace from the right end of each line. |
paul@8 | 133 | # For subsequent lines, remove the first whitespace character. |
paul@8 | 134 | # See section 4.1 of the iCalendar specification. |
paul@8 | 135 | |
paul@9 | 136 | lines = [line_stripped] |
paul@0 | 137 | |
paul@0 | 138 | line = self.readline() |
paul@8 | 139 | while line.startswith(" ") or line.startswith("\t"): |
paul@8 | 140 | lines.append(line[1:].rstrip("\r\n")) |
paul@8 | 141 | line = self.readline() |
paul@8 | 142 | |
paul@8 | 143 | # Since one line too many will have been read, push the line back into |
paul@8 | 144 | # the file. |
paul@8 | 145 | |
paul@8 | 146 | if line: |
paul@8 | 147 | self.pushback(line) |
paul@8 | 148 | |
paul@8 | 149 | return "".join(lines) |
paul@8 | 150 | |
paul@8 | 151 | def get_content_line(self): |
paul@8 | 152 | |
paul@8 | 153 | "Return a content line object for the current line." |
paul@8 | 154 | |
paul@8 | 155 | return ContentLine(self.read_content_line()) |
paul@8 | 156 | |
paul@8 | 157 | class ContentLine: |
paul@8 | 158 | |
paul@8 | 159 | "A content line which can be searched." |
paul@8 | 160 | |
paul@8 | 161 | SEPARATORS = re.compile('[;:"]') |
paul@8 | 162 | SEPARATORS_PLUS_EQUALS = re.compile('[=;:"]') |
paul@8 | 163 | |
paul@8 | 164 | def __init__(self, text): |
paul@8 | 165 | self.text = text |
paul@8 | 166 | self.start = 0 |
paul@8 | 167 | |
paul@30 | 168 | def __repr__(self): |
paul@30 | 169 | return "ContentLine(%r)" % self.text |
paul@30 | 170 | |
paul@8 | 171 | def get_remaining(self): |
paul@8 | 172 | |
paul@8 | 173 | "Get the remaining text from the content line." |
paul@8 | 174 | |
paul@8 | 175 | return self.text[self.start:] |
paul@8 | 176 | |
paul@8 | 177 | def search(self, targets): |
paul@8 | 178 | |
paul@8 | 179 | """ |
paul@8 | 180 | Find one of the 'targets' in the text, returning the string from the |
paul@8 | 181 | current position up to the target found, along with the target string, |
paul@8 | 182 | using a tuple of the form (string, target). If no target was found, |
paul@8 | 183 | return the entire string together with a target of None. |
paul@11 | 184 | |
paul@11 | 185 | The 'targets' parameter must be a regular expression object or an object |
paul@11 | 186 | compatible with the API of such objects. |
paul@8 | 187 | """ |
paul@8 | 188 | |
paul@8 | 189 | text = self.text |
paul@8 | 190 | start = pos = self.start |
paul@8 | 191 | length = len(text) |
paul@0 | 192 | |
paul@4 | 193 | # Remember the first target. |
paul@4 | 194 | |
paul@4 | 195 | first = None |
paul@4 | 196 | first_pos = None |
paul@4 | 197 | in_quoted_region = 0 |
paul@0 | 198 | |
paul@8 | 199 | # Process the text, looking for the targets. |
paul@4 | 200 | |
paul@8 | 201 | while pos < length: |
paul@8 | 202 | match = targets.search(text, pos) |
paul@4 | 203 | |
paul@8 | 204 | # Where nothing matches, end the search. |
paul@0 | 205 | |
paul@4 | 206 | if match is None: |
paul@8 | 207 | pos = length |
paul@0 | 208 | |
paul@4 | 209 | # Where a double quote matches, toggle the region state. |
paul@0 | 210 | |
paul@4 | 211 | elif match.group() == '"': |
paul@4 | 212 | in_quoted_region = not in_quoted_region |
paul@8 | 213 | pos = match.end() |
paul@4 | 214 | |
paul@4 | 215 | # Where something else matches outside a region, stop searching. |
paul@0 | 216 | |
paul@4 | 217 | elif not in_quoted_region: |
paul@4 | 218 | first = match.group() |
paul@4 | 219 | first_pos = match.start() |
paul@4 | 220 | break |
paul@0 | 221 | |
paul@4 | 222 | # Otherwise, keep looking for the end of the region. |
paul@4 | 223 | |
paul@4 | 224 | else: |
paul@8 | 225 | pos = match.end() |
paul@4 | 226 | |
paul@4 | 227 | # Where no more input can provide the targets, return a special result. |
paul@0 | 228 | |
paul@4 | 229 | else: |
paul@8 | 230 | self.start = length |
paul@8 | 231 | return text[start:], None |
paul@0 | 232 | |
paul@8 | 233 | self.start = match.end() |
paul@8 | 234 | return text[start:first_pos], first |
paul@0 | 235 | |
paul@0 | 236 | class StreamParser: |
paul@0 | 237 | |
paul@0 | 238 | "A stream parser for content in vCard/vCalendar/iCalendar-like formats." |
paul@0 | 239 | |
paul@0 | 240 | def __init__(self, f): |
paul@0 | 241 | |
paul@0 | 242 | "Initialise the parser for the given file 'f'." |
paul@0 | 243 | |
paul@0 | 244 | self.f = f |
paul@0 | 245 | |
paul@9 | 246 | def close(self): |
paul@9 | 247 | |
paul@9 | 248 | "Close the reader." |
paul@9 | 249 | |
paul@9 | 250 | self.f.close() |
paul@9 | 251 | |
paul@0 | 252 | def __iter__(self): |
paul@0 | 253 | |
paul@0 | 254 | "Return self as the iterator." |
paul@0 | 255 | |
paul@0 | 256 | return self |
paul@0 | 257 | |
paul@0 | 258 | def next(self): |
paul@0 | 259 | |
paul@0 | 260 | """ |
paul@0 | 261 | Return the next content item in the file as a tuple of the form |
paul@0 | 262 | (name, parameters, values). |
paul@0 | 263 | """ |
paul@0 | 264 | |
paul@0 | 265 | return self.parse_content_line() |
paul@0 | 266 | |
paul@7 | 267 | def decode_content(self, value): |
paul@7 | 268 | |
paul@7 | 269 | "Decode the given 'value', replacing quoted characters." |
paul@7 | 270 | |
paul@7 | 271 | return value.replace("\r", "").replace("\\N", "\n").replace("\\n", "\n") |
paul@7 | 272 | |
paul@5 | 273 | # Internal methods. |
paul@5 | 274 | |
paul@0 | 275 | def parse_content_line(self): |
paul@0 | 276 | |
paul@0 | 277 | """ |
paul@7 | 278 | Return the name, parameters and value information for the current |
paul@7 | 279 | content line in the file being parsed. |
paul@0 | 280 | """ |
paul@0 | 281 | |
paul@0 | 282 | f = self.f |
paul@8 | 283 | line_number = f.line_number |
paul@8 | 284 | line = f.get_content_line() |
paul@0 | 285 | |
paul@8 | 286 | # Read the property name. |
paul@0 | 287 | |
paul@8 | 288 | name, sep = line.search(line.SEPARATORS) |
paul@0 | 289 | name = name.strip() |
paul@0 | 290 | |
paul@0 | 291 | if not name and sep is None: |
paul@0 | 292 | raise StopIteration |
paul@0 | 293 | |
paul@8 | 294 | # Read the parameters. |
paul@8 | 295 | |
paul@8 | 296 | parameters = {} |
paul@8 | 297 | |
paul@0 | 298 | while sep == ";": |
paul@0 | 299 | |
paul@0 | 300 | # Find the actual modifier. |
paul@0 | 301 | |
paul@8 | 302 | parameter_name, sep = line.search(line.SEPARATORS_PLUS_EQUALS) |
paul@0 | 303 | parameter_name = parameter_name.strip() |
paul@0 | 304 | |
paul@0 | 305 | if sep == "=": |
paul@8 | 306 | parameter_value, sep = line.search(line.SEPARATORS) |
paul@0 | 307 | parameter_value = parameter_value.strip() |
paul@0 | 308 | else: |
paul@0 | 309 | parameter_value = None |
paul@0 | 310 | |
paul@0 | 311 | # Append a key, value tuple to the parameters list. |
paul@0 | 312 | |
paul@0 | 313 | parameters[parameter_name] = parameter_value |
paul@0 | 314 | |
paul@0 | 315 | # Get the value content. |
paul@0 | 316 | |
paul@0 | 317 | if sep != ":": |
paul@30 | 318 | raise ValueError, (line_number, line) |
paul@0 | 319 | |
paul@8 | 320 | # Obtain and decode the value. |
paul@0 | 321 | |
paul@8 | 322 | value = self.decode(name, parameters, line.get_remaining()) |
paul@0 | 323 | |
paul@0 | 324 | return name, parameters, value |
paul@0 | 325 | |
paul@7 | 326 | def decode(self, name, parameters, value): |
paul@1 | 327 | |
paul@7 | 328 | "Decode using 'name' and 'parameters' the given 'value'." |
paul@0 | 329 | |
paul@1 | 330 | encoding = parameters.get("ENCODING") |
paul@1 | 331 | charset = parameters.get("CHARSET") |
paul@0 | 332 | |
paul@7 | 333 | value = self.decode_content(value) |
paul@0 | 334 | |
paul@0 | 335 | if encoding == "QUOTED-PRINTABLE": |
paul@1 | 336 | return unicode(quopri.decodestring(value), charset or "iso-8859-1") |
paul@0 | 337 | elif encoding == "BASE64": |
paul@0 | 338 | return base64.decodestring(value) |
paul@0 | 339 | else: |
paul@1 | 340 | return value |
paul@0 | 341 | |
paul@2 | 342 | class ParserBase: |
paul@0 | 343 | |
paul@2 | 344 | "An abstract parser for content in vCard/vCalendar/iCalendar-like formats." |
paul@0 | 345 | |
paul@0 | 346 | def __init__(self): |
paul@0 | 347 | |
paul@0 | 348 | "Initialise the parser." |
paul@0 | 349 | |
paul@2 | 350 | self.names = [] |
paul@0 | 351 | |
paul@5 | 352 | def parse(self, f, parser_cls=None): |
paul@0 | 353 | |
paul@0 | 354 | "Parse the contents of the file 'f'." |
paul@0 | 355 | |
paul@5 | 356 | parser = (parser_cls or StreamParser)(f) |
paul@0 | 357 | |
paul@0 | 358 | for name, parameters, value in parser: |
paul@0 | 359 | |
paul@0 | 360 | if name == "BEGIN": |
paul@2 | 361 | self.names.append(value) |
paul@3 | 362 | self.startComponent(value, parameters) |
paul@0 | 363 | |
paul@0 | 364 | elif name == "END": |
paul@2 | 365 | start_name = self.names.pop() |
paul@2 | 366 | if start_name != value: |
paul@0 | 367 | raise ParseError, "Mismatch in BEGIN and END declarations (%r and %r) at line %d." % ( |
paul@2 | 368 | start_name, value, f.line_number) |
paul@2 | 369 | |
paul@3 | 370 | self.endComponent(value) |
paul@0 | 371 | |
paul@0 | 372 | else: |
paul@3 | 373 | self.handleProperty(name, parameters, value) |
paul@2 | 374 | |
paul@2 | 375 | class Parser(ParserBase): |
paul@2 | 376 | |
paul@2 | 377 | "A SAX-like parser for vCard/vCalendar/iCalendar-like formats." |
paul@2 | 378 | |
paul@2 | 379 | def __init__(self): |
paul@2 | 380 | ParserBase.__init__(self) |
paul@3 | 381 | self.components = [] |
paul@2 | 382 | |
paul@3 | 383 | def startComponent(self, name, parameters): |
paul@2 | 384 | |
paul@2 | 385 | """ |
paul@3 | 386 | Add the component with the given 'name' and 'parameters', recording an |
paul@3 | 387 | empty list of children as part of the component's content. |
paul@2 | 388 | """ |
paul@2 | 389 | |
paul@12 | 390 | component = self.handleProperty(name, parameters) |
paul@3 | 391 | self.components.append(component) |
paul@3 | 392 | return component |
paul@2 | 393 | |
paul@3 | 394 | def endComponent(self, name): |
paul@2 | 395 | |
paul@2 | 396 | """ |
paul@3 | 397 | End the component with the given 'name' by removing it from the active |
paul@12 | 398 | component stack. If only one component exists on the stack, retain it |
paul@12 | 399 | for later inspection. |
paul@2 | 400 | """ |
paul@2 | 401 | |
paul@3 | 402 | if len(self.components) > 1: |
paul@3 | 403 | return self.components.pop() |
paul@12 | 404 | |
paul@12 | 405 | # Or return the only element. |
paul@12 | 406 | |
paul@3 | 407 | elif self.components: |
paul@12 | 408 | return self.components[0] |
paul@2 | 409 | |
paul@12 | 410 | def handleProperty(self, name, parameters, value=None): |
paul@0 | 411 | |
paul@2 | 412 | """ |
paul@12 | 413 | Record the property with the given 'name', 'parameters' and optional |
paul@12 | 414 | 'value' as part of the current component's children. |
paul@2 | 415 | """ |
paul@2 | 416 | |
paul@2 | 417 | component = self.makeComponent(name, parameters, value) |
paul@2 | 418 | self.attachComponent(component) |
paul@2 | 419 | return component |
paul@2 | 420 | |
paul@2 | 421 | # Component object construction/manipulation methods. |
paul@2 | 422 | |
paul@2 | 423 | def attachComponent(self, component): |
paul@2 | 424 | |
paul@2 | 425 | "Attach the given 'component' to its parent." |
paul@2 | 426 | |
paul@3 | 427 | if self.components: |
paul@3 | 428 | component_name, component_parameters, component_children = self.components[-1] |
paul@3 | 429 | component_children.append(component) |
paul@2 | 430 | |
paul@12 | 431 | def makeComponent(self, name, parameters, value=None): |
paul@2 | 432 | |
paul@2 | 433 | """ |
paul@12 | 434 | Make a component object from the given 'name', 'parameters' and optional |
paul@12 | 435 | 'value'. |
paul@2 | 436 | """ |
paul@2 | 437 | |
paul@12 | 438 | return (name, parameters, value or []) |
paul@2 | 439 | |
paul@2 | 440 | # Public methods. |
paul@2 | 441 | |
paul@5 | 442 | def parse(self, f, parser_cls=None): |
paul@2 | 443 | |
paul@2 | 444 | "Parse the contents of the file 'f'." |
paul@2 | 445 | |
paul@5 | 446 | ParserBase.parse(self, f, parser_cls) |
paul@3 | 447 | return self.components[0] |
paul@0 | 448 | |
paul@7 | 449 | # Writer classes. |
paul@7 | 450 | |
paul@8 | 451 | class Writer: |
paul@8 | 452 | |
paul@8 | 453 | "A simple class wrapping a file, providing simple output capabilities." |
paul@8 | 454 | |
paul@8 | 455 | default_line_length = 76 |
paul@8 | 456 | |
paul@21 | 457 | def __init__(self, write, line_length=None): |
paul@8 | 458 | |
paul@8 | 459 | """ |
paul@21 | 460 | Initialise the object with the given 'write' operation. If 'line_length' |
paul@21 | 461 | is set, the length of written lines will conform to the specified value |
paul@21 | 462 | instead of the default value. |
paul@8 | 463 | """ |
paul@8 | 464 | |
paul@21 | 465 | self._write = write |
paul@8 | 466 | self.line_length = line_length or self.default_line_length |
paul@8 | 467 | self.char_offset = 0 |
paul@8 | 468 | |
paul@8 | 469 | def write(self, text): |
paul@8 | 470 | |
paul@8 | 471 | "Write the 'text' to the file." |
paul@8 | 472 | |
paul@21 | 473 | write = self._write |
paul@8 | 474 | line_length = self.line_length |
paul@8 | 475 | |
paul@8 | 476 | i = 0 |
paul@8 | 477 | remaining = len(text) |
paul@8 | 478 | |
paul@8 | 479 | while remaining: |
paul@8 | 480 | space = line_length - self.char_offset |
paul@8 | 481 | if remaining > space: |
paul@21 | 482 | write(text[i:i + space]) |
paul@21 | 483 | write("\r\n ") |
paul@8 | 484 | self.char_offset = 1 |
paul@8 | 485 | i += space |
paul@8 | 486 | remaining -= space |
paul@8 | 487 | else: |
paul@21 | 488 | write(text[i:]) |
paul@8 | 489 | self.char_offset += remaining |
paul@8 | 490 | i += remaining |
paul@8 | 491 | remaining = 0 |
paul@8 | 492 | |
paul@8 | 493 | def end_line(self): |
paul@8 | 494 | |
paul@8 | 495 | "End the current content line." |
paul@8 | 496 | |
paul@8 | 497 | if self.char_offset > 0: |
paul@8 | 498 | self.char_offset = 0 |
paul@21 | 499 | self._write("\r\n") |
paul@8 | 500 | |
paul@7 | 501 | class StreamWriter: |
paul@7 | 502 | |
paul@7 | 503 | "A stream writer for content in vCard/vCalendar/iCalendar-like formats." |
paul@7 | 504 | |
paul@8 | 505 | def __init__(self, f): |
paul@7 | 506 | |
paul@21 | 507 | "Initialise the stream writer with the given 'f' stream object." |
paul@7 | 508 | |
paul@7 | 509 | self.f = f |
paul@7 | 510 | |
paul@11 | 511 | def write(self, name, parameters, value): |
paul@7 | 512 | |
paul@7 | 513 | """ |
paul@11 | 514 | Write a content line, serialising the given 'name', 'parameters' and |
paul@11 | 515 | 'value' information. |
paul@11 | 516 | """ |
paul@11 | 517 | |
paul@11 | 518 | self.write_content_line(name, self.encode_parameters(parameters), self.encode_value(name, parameters, value)) |
paul@11 | 519 | |
paul@11 | 520 | # Internal methods. |
paul@11 | 521 | |
paul@11 | 522 | def write_content_line(self, name, encoded_parameters, encoded_value): |
paul@11 | 523 | |
paul@11 | 524 | """ |
paul@11 | 525 | Write a content line for the given 'name', 'encoded_parameters' and |
paul@11 | 526 | 'encoded_value' information. |
paul@7 | 527 | """ |
paul@7 | 528 | |
paul@7 | 529 | f = self.f |
paul@7 | 530 | |
paul@7 | 531 | f.write(name) |
paul@11 | 532 | for param_name, param_value in encoded_parameters.items(): |
paul@8 | 533 | f.write(";") |
paul@11 | 534 | f.write(param_name) |
paul@8 | 535 | f.write("=") |
paul@11 | 536 | f.write(param_value) |
paul@7 | 537 | f.write(":") |
paul@11 | 538 | f.write(encoded_value) |
paul@8 | 539 | f.end_line() |
paul@7 | 540 | |
paul@11 | 541 | def encode_quoted_parameter_value(self, value): |
paul@7 | 542 | |
paul@11 | 543 | "Encode the given 'value'." |
paul@7 | 544 | |
paul@11 | 545 | return '"%s"' % value |
paul@7 | 546 | |
paul@11 | 547 | def encode_value(self, name, parameters, value): |
paul@7 | 548 | |
paul@11 | 549 | """ |
paul@11 | 550 | Encode using 'name' and 'parameters' the given 'value' so that the |
paul@11 | 551 | resulting encoded form employs any specified character encodings. |
paul@11 | 552 | """ |
paul@7 | 553 | |
paul@7 | 554 | encoding = parameters.get("ENCODING") |
paul@7 | 555 | charset = parameters.get("CHARSET") |
paul@7 | 556 | |
paul@7 | 557 | if encoding == "QUOTED-PRINTABLE": |
paul@7 | 558 | value = quopri.encodestring(value.encode(charset or "iso-8859-1")) |
paul@7 | 559 | elif encoding == "BASE64": |
paul@7 | 560 | value = base64.encodestring(value) |
paul@7 | 561 | |
paul@7 | 562 | return self.encode_content(value) |
paul@7 | 563 | |
paul@11 | 564 | # Overrideable methods. |
paul@11 | 565 | |
paul@11 | 566 | def encode_parameters(self, parameters): |
paul@11 | 567 | |
paul@11 | 568 | """ |
paul@11 | 569 | Encode the given 'parameters' according to the vCalendar specification. |
paul@11 | 570 | """ |
paul@11 | 571 | |
paul@11 | 572 | encoded_parameters = {} |
paul@11 | 573 | |
paul@11 | 574 | for param_name, param_value in parameters.items(): |
paul@11 | 575 | |
paul@11 | 576 | # Basic format support merely involves quoting values which seem to |
paul@11 | 577 | # need it. Other more specific formats may define exactly which |
paul@11 | 578 | # parameters should be quoted. |
paul@11 | 579 | |
paul@11 | 580 | if ContentLine.SEPARATORS.search(param_value): |
paul@11 | 581 | param_value = self.encode_quoted_parameter_value(param_value) |
paul@11 | 582 | |
paul@11 | 583 | encoded_parameters[param_name] = param_value |
paul@11 | 584 | |
paul@11 | 585 | return encoded_parameters |
paul@11 | 586 | |
paul@11 | 587 | def encode_content(self, value): |
paul@11 | 588 | |
paul@11 | 589 | "Encode the given 'value', quoting characters." |
paul@11 | 590 | |
paul@11 | 591 | return value.replace("\n", "\\n") |
paul@11 | 592 | |
paul@9 | 593 | # Utility functions. |
paul@9 | 594 | |
paul@9 | 595 | def is_input_stream(stream_or_string): |
paul@9 | 596 | return hasattr(stream_or_string, "read") |
paul@9 | 597 | |
paul@11 | 598 | def get_input_stream(stream_or_string, encoding=None): |
paul@9 | 599 | if is_input_stream(stream_or_string): |
paul@9 | 600 | return stream_or_string |
paul@9 | 601 | else: |
paul@11 | 602 | return codecs.open(stream_or_string, encoding=(encoding or default_encoding)) |
paul@9 | 603 | |
paul@11 | 604 | def get_output_stream(stream_or_string, encoding=None): |
paul@9 | 605 | if hasattr(stream_or_string, "write"): |
paul@9 | 606 | return stream_or_string |
paul@9 | 607 | else: |
paul@11 | 608 | return codecs.open(stream_or_string, "w", encoding=(encoding or default_encoding)) |
paul@9 | 609 | |
paul@0 | 610 | # Public functions. |
paul@0 | 611 | |
paul@11 | 612 | def parse(stream_or_string, encoding=None, non_standard_newline=0, parser_cls=None): |
paul@0 | 613 | |
paul@0 | 614 | """ |
paul@9 | 615 | Parse the resource data found through the use of the 'stream_or_string', |
paul@9 | 616 | which is either a stream providing Unicode data (the codecs module can be |
paul@9 | 617 | used to open files or to wrap streams in order to provide Unicode data) or a |
paul@9 | 618 | filename identifying a file to be parsed. |
paul@0 | 619 | |
paul@11 | 620 | The optional 'encoding' can be used to specify the character encoding used |
paul@11 | 621 | by the file to be parsed. |
paul@11 | 622 | |
paul@0 | 623 | The optional 'non_standard_newline' can be set to a true value (unlike the |
paul@0 | 624 | default) in order to attempt to process files with CR as the end of line |
paul@0 | 625 | character. |
paul@0 | 626 | |
paul@0 | 627 | As a result of parsing the resource, the root node of the imported resource |
paul@0 | 628 | is returned. |
paul@0 | 629 | """ |
paul@0 | 630 | |
paul@11 | 631 | stream = get_input_stream(stream_or_string, encoding) |
paul@9 | 632 | reader = Reader(stream, non_standard_newline) |
paul@9 | 633 | |
paul@9 | 634 | # Parse using the reader. |
paul@0 | 635 | |
paul@9 | 636 | try: |
paul@9 | 637 | parser = (parser_cls or Parser)() |
paul@9 | 638 | return parser.parse(reader) |
paul@9 | 639 | |
paul@9 | 640 | # Close any opened streams. |
paul@9 | 641 | |
paul@9 | 642 | finally: |
paul@9 | 643 | if not is_input_stream(stream_or_string): |
paul@9 | 644 | reader.close() |
paul@9 | 645 | |
paul@11 | 646 | def iterparse(stream_or_string, encoding=None, non_standard_newline=0, parser_cls=None): |
paul@5 | 647 | |
paul@5 | 648 | """ |
paul@9 | 649 | Parse the resource data found through the use of the 'stream_or_string', |
paul@9 | 650 | which is either a stream providing Unicode data (the codecs module can be |
paul@9 | 651 | used to open files or to wrap streams in order to provide Unicode data) or a |
paul@9 | 652 | filename identifying a file to be parsed. |
paul@5 | 653 | |
paul@11 | 654 | The optional 'encoding' can be used to specify the character encoding used |
paul@11 | 655 | by the file to be parsed. |
paul@11 | 656 | |
paul@5 | 657 | The optional 'non_standard_newline' can be set to a true value (unlike the |
paul@5 | 658 | default) in order to attempt to process files with CR as the end of line |
paul@5 | 659 | character. |
paul@5 | 660 | |
paul@5 | 661 | An iterator is returned which provides event tuples describing parsing |
paul@5 | 662 | events of the form (name, parameters, value). |
paul@5 | 663 | """ |
paul@5 | 664 | |
paul@11 | 665 | stream = get_input_stream(stream_or_string, encoding) |
paul@9 | 666 | reader = Reader(stream, non_standard_newline) |
paul@5 | 667 | parser = (parser_cls or StreamParser)(reader) |
paul@9 | 668 | return parser |
paul@5 | 669 | |
paul@21 | 670 | def iterwrite(stream_or_string=None, write=None, encoding=None, line_length=None, writer_cls=None): |
paul@11 | 671 | |
paul@11 | 672 | """ |
paul@21 | 673 | Return a writer which will either send data to the resource found through |
paul@21 | 674 | the use of 'stream_or_string' or using the given 'write' operation. |
paul@21 | 675 | |
paul@21 | 676 | The 'stream_or_string' parameter may be either a stream accepting Unicode |
paul@21 | 677 | data (the codecs module can be used to open files or to wrap streams in |
paul@21 | 678 | order to accept Unicode data) or a filename identifying a file to be |
paul@21 | 679 | written. |
paul@11 | 680 | |
paul@11 | 681 | The optional 'encoding' can be used to specify the character encoding used |
paul@11 | 682 | by the file to be written. |
paul@11 | 683 | |
paul@11 | 684 | The optional 'line_length' can be used to specify how long lines should be |
paul@11 | 685 | in the resulting data. |
paul@11 | 686 | """ |
paul@11 | 687 | |
paul@21 | 688 | if stream_or_string: |
paul@21 | 689 | stream = get_output_stream(stream_or_string, encoding) |
paul@21 | 690 | _writer = Writer(stream.write, line_length) |
paul@21 | 691 | elif write: |
paul@21 | 692 | _writer = Writer(write, line_length) |
paul@21 | 693 | else: |
paul@21 | 694 | raise IOError, "No stream, filename or write operation specified." |
paul@21 | 695 | |
paul@21 | 696 | return (writer_cls or StreamWriter)(_writer) |
paul@8 | 697 | |
paul@0 | 698 | # vim: tabstop=4 expandtab shiftwidth=4 |