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