Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 1 | r"""plistlib.py -- a tool to generate and parse MacOSX .plist files. |
Christian Heimes | 7e18254 | 2008-01-27 15:20:13 +0000 | [diff] [blame] | 2 | |
Ezio Melotti | 6e9b1df | 2009-09-16 00:49:03 +0000 | [diff] [blame] | 3 | The property list (.plist) file format is a simple XML pickle supporting |
Christian Heimes | 7e18254 | 2008-01-27 15:20:13 +0000 | [diff] [blame] | 4 | basic object types, like dictionaries, lists, numbers and strings. |
| 5 | Usually the top level object is a dictionary. |
| 6 | |
| 7 | To write out a plist file, use the writePlist(rootObject, pathOrFile) |
| 8 | function. 'rootObject' is the top level object, 'pathOrFile' is a |
| 9 | filename or a (writable) file object. |
| 10 | |
| 11 | To parse a plist from a file, use the readPlist(pathOrFile) function, |
| 12 | with a file name or a (readable) file object as the only argument. It |
| 13 | returns the top level object (again, usually a dictionary). |
| 14 | |
| 15 | To work with plist data in bytes objects, you can use readPlistFromBytes() |
| 16 | and writePlistToBytes(). |
| 17 | |
| 18 | Values can be strings, integers, floats, booleans, tuples, lists, |
Ezio Melotti | 6e9b1df | 2009-09-16 00:49:03 +0000 | [diff] [blame] | 19 | dictionaries (but only with string keys), Data or datetime.datetime objects. |
| 20 | String values (including dictionary keys) have to be unicode strings -- they |
| 21 | will be written out as UTF-8. |
Christian Heimes | 7e18254 | 2008-01-27 15:20:13 +0000 | [diff] [blame] | 22 | |
| 23 | The <data> plist type is supported through the Data class. This is a |
Ezio Melotti | 6e9b1df | 2009-09-16 00:49:03 +0000 | [diff] [blame] | 24 | thin wrapper around a Python bytes object. Use 'Data' if your strings |
| 25 | contain control characters. |
Christian Heimes | 7e18254 | 2008-01-27 15:20:13 +0000 | [diff] [blame] | 26 | |
| 27 | Generate Plist example: |
| 28 | |
| 29 | pl = dict( |
Ezio Melotti | 6e9b1df | 2009-09-16 00:49:03 +0000 | [diff] [blame] | 30 | aString = "Doodah", |
| 31 | aList = ["A", "B", 12, 32.1, [1, 2, 3]], |
Christian Heimes | 7e18254 | 2008-01-27 15:20:13 +0000 | [diff] [blame] | 32 | aFloat = 0.1, |
| 33 | anInt = 728, |
Ezio Melotti | 6e9b1df | 2009-09-16 00:49:03 +0000 | [diff] [blame] | 34 | aDict = dict( |
| 35 | anotherString = "<hello & hi there!>", |
| 36 | aUnicodeValue = "M\xe4ssig, Ma\xdf", |
| 37 | aTrueValue = True, |
| 38 | aFalseValue = False, |
Christian Heimes | 7e18254 | 2008-01-27 15:20:13 +0000 | [diff] [blame] | 39 | ), |
| 40 | someData = Data(b"<binary gunk>"), |
| 41 | someMoreData = Data(b"<lots of binary gunk>" * 10), |
| 42 | aDate = datetime.datetime.fromtimestamp(time.mktime(time.gmtime())), |
| 43 | ) |
Christian Heimes | 7e18254 | 2008-01-27 15:20:13 +0000 | [diff] [blame] | 44 | writePlist(pl, fileName) |
| 45 | |
| 46 | Parse Plist example: |
| 47 | |
| 48 | pl = readPlist(pathOrFile) |
| 49 | print pl["aKey"] |
| 50 | """ |
| 51 | |
| 52 | |
| 53 | __all__ = [ |
| 54 | "readPlist", "writePlist", "readPlistFromBytes", "writePlistToBytes", |
Christian Heimes | 7e18254 | 2008-01-27 15:20:13 +0000 | [diff] [blame] | 55 | "Plist", "Data", "Dict" |
| 56 | ] |
| 57 | # Note: the Plist and Dict classes have been deprecated. |
| 58 | |
| 59 | import binascii |
| 60 | import datetime |
| 61 | from io import BytesIO |
| 62 | import re |
| 63 | |
| 64 | |
| 65 | def readPlist(pathOrFile): |
| 66 | """Read a .plist file. 'pathOrFile' may either be a file name or a |
| 67 | (readable) file object. Return the unpacked root object (which |
| 68 | usually is a dictionary). |
| 69 | """ |
| 70 | didOpen = False |
Ned Deily | b8e59f7 | 2011-05-28 02:19:19 -0700 | [diff] [blame] | 71 | try: |
| 72 | if isinstance(pathOrFile, str): |
| 73 | pathOrFile = open(pathOrFile, 'rb') |
| 74 | didOpen = True |
| 75 | p = PlistParser() |
| 76 | rootObject = p.parse(pathOrFile) |
| 77 | finally: |
| 78 | if didOpen: |
| 79 | pathOrFile.close() |
Christian Heimes | 7e18254 | 2008-01-27 15:20:13 +0000 | [diff] [blame] | 80 | return rootObject |
| 81 | |
| 82 | |
| 83 | def writePlist(rootObject, pathOrFile): |
| 84 | """Write 'rootObject' to a .plist file. 'pathOrFile' may either be a |
| 85 | file name or a (writable) file object. |
| 86 | """ |
| 87 | didOpen = False |
Ned Deily | b8e59f7 | 2011-05-28 02:19:19 -0700 | [diff] [blame] | 88 | try: |
| 89 | if isinstance(pathOrFile, str): |
| 90 | pathOrFile = open(pathOrFile, 'wb') |
| 91 | didOpen = True |
| 92 | writer = PlistWriter(pathOrFile) |
| 93 | writer.writeln("<plist version=\"1.0\">") |
| 94 | writer.writeValue(rootObject) |
| 95 | writer.writeln("</plist>") |
| 96 | finally: |
| 97 | if didOpen: |
| 98 | pathOrFile.close() |
Christian Heimes | 7e18254 | 2008-01-27 15:20:13 +0000 | [diff] [blame] | 99 | |
| 100 | |
| 101 | def readPlistFromBytes(data): |
| 102 | """Read a plist data from a bytes object. Return the root object. |
| 103 | """ |
| 104 | return readPlist(BytesIO(data)) |
| 105 | |
| 106 | |
| 107 | def writePlistToBytes(rootObject): |
| 108 | """Return 'rootObject' as a plist-formatted bytes object. |
| 109 | """ |
| 110 | f = BytesIO() |
| 111 | writePlist(rootObject, f) |
| 112 | return f.getvalue() |
| 113 | |
| 114 | |
Christian Heimes | 7e18254 | 2008-01-27 15:20:13 +0000 | [diff] [blame] | 115 | class DumbXMLWriter: |
| 116 | def __init__(self, file, indentLevel=0, indent="\t"): |
| 117 | self.file = file |
| 118 | self.stack = [] |
| 119 | self.indentLevel = indentLevel |
| 120 | self.indent = indent |
| 121 | |
| 122 | def beginElement(self, element): |
| 123 | self.stack.append(element) |
| 124 | self.writeln("<%s>" % element) |
| 125 | self.indentLevel += 1 |
| 126 | |
| 127 | def endElement(self, element): |
| 128 | assert self.indentLevel > 0 |
| 129 | assert self.stack.pop() == element |
| 130 | self.indentLevel -= 1 |
| 131 | self.writeln("</%s>" % element) |
| 132 | |
| 133 | def simpleElement(self, element, value=None): |
| 134 | if value is not None: |
| 135 | value = _escape(value) |
| 136 | self.writeln("<%s>%s</%s>" % (element, value, element)) |
| 137 | else: |
| 138 | self.writeln("<%s/>" % element) |
| 139 | |
| 140 | def writeln(self, line): |
| 141 | if line: |
| 142 | # plist has fixed encoding of utf-8 |
| 143 | if isinstance(line, str): |
| 144 | line = line.encode('utf-8') |
| 145 | self.file.write(self.indentLevel * self.indent) |
| 146 | self.file.write(line) |
| 147 | self.file.write(b'\n') |
| 148 | |
| 149 | |
| 150 | # Contents should conform to a subset of ISO 8601 |
| 151 | # (in particular, YYYY '-' MM '-' DD 'T' HH ':' MM ':' SS 'Z'. Smaller units may be omitted with |
| 152 | # a loss of precision) |
Antoine Pitrou | fd03645 | 2008-08-19 17:56:33 +0000 | [diff] [blame] | 153 | _dateParser = re.compile(r"(?P<year>\d\d\d\d)(?:-(?P<month>\d\d)(?:-(?P<day>\d\d)(?:T(?P<hour>\d\d)(?::(?P<minute>\d\d)(?::(?P<second>\d\d))?)?)?)?)?Z", re.ASCII) |
Christian Heimes | 7e18254 | 2008-01-27 15:20:13 +0000 | [diff] [blame] | 154 | |
| 155 | def _dateFromString(s): |
| 156 | order = ('year', 'month', 'day', 'hour', 'minute', 'second') |
| 157 | gd = _dateParser.match(s).groupdict() |
| 158 | lst = [] |
| 159 | for key in order: |
| 160 | val = gd[key] |
| 161 | if val is None: |
| 162 | break |
| 163 | lst.append(int(val)) |
| 164 | return datetime.datetime(*lst) |
| 165 | |
| 166 | def _dateToString(d): |
| 167 | return '%04d-%02d-%02dT%02d:%02d:%02dZ' % ( |
| 168 | d.year, d.month, d.day, |
| 169 | d.hour, d.minute, d.second |
| 170 | ) |
| 171 | |
| 172 | |
| 173 | # Regex to find any control chars, except for \t \n and \r |
| 174 | _controlCharPat = re.compile( |
| 175 | r"[\x00\x01\x02\x03\x04\x05\x06\x07\x08\x0b\x0c\x0e\x0f" |
| 176 | r"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f]") |
| 177 | |
| 178 | def _escape(text): |
| 179 | m = _controlCharPat.search(text) |
| 180 | if m is not None: |
| 181 | raise ValueError("strings can't contains control characters; " |
| 182 | "use plistlib.Data instead") |
| 183 | text = text.replace("\r\n", "\n") # convert DOS line endings |
| 184 | text = text.replace("\r", "\n") # convert Mac line endings |
| 185 | text = text.replace("&", "&") # escape '&' |
| 186 | text = text.replace("<", "<") # escape '<' |
| 187 | text = text.replace(">", ">") # escape '>' |
| 188 | return text |
| 189 | |
| 190 | |
| 191 | PLISTHEADER = b"""\ |
| 192 | <?xml version="1.0" encoding="UTF-8"?> |
Ronald Oussoren | 33798fd | 2010-04-20 21:00:34 +0000 | [diff] [blame] | 193 | <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
Christian Heimes | 7e18254 | 2008-01-27 15:20:13 +0000 | [diff] [blame] | 194 | """ |
| 195 | |
| 196 | class PlistWriter(DumbXMLWriter): |
| 197 | |
| 198 | def __init__(self, file, indentLevel=0, indent=b"\t", writeHeader=1): |
| 199 | if writeHeader: |
| 200 | file.write(PLISTHEADER) |
| 201 | DumbXMLWriter.__init__(self, file, indentLevel, indent) |
| 202 | |
| 203 | def writeValue(self, value): |
| 204 | if isinstance(value, str): |
| 205 | self.simpleElement("string", value) |
| 206 | elif isinstance(value, bool): |
| 207 | # must switch for bool before int, as bool is a |
| 208 | # subclass of int... |
| 209 | if value: |
| 210 | self.simpleElement("true") |
| 211 | else: |
| 212 | self.simpleElement("false") |
| 213 | elif isinstance(value, int): |
| 214 | self.simpleElement("integer", "%d" % value) |
| 215 | elif isinstance(value, float): |
| 216 | self.simpleElement("real", repr(value)) |
| 217 | elif isinstance(value, dict): |
| 218 | self.writeDict(value) |
| 219 | elif isinstance(value, Data): |
| 220 | self.writeData(value) |
| 221 | elif isinstance(value, datetime.datetime): |
| 222 | self.simpleElement("date", _dateToString(value)) |
| 223 | elif isinstance(value, (tuple, list)): |
| 224 | self.writeArray(value) |
| 225 | else: |
Ezio Melotti | 6e9b1df | 2009-09-16 00:49:03 +0000 | [diff] [blame] | 226 | raise TypeError("unsupported type: %s" % type(value)) |
Christian Heimes | 7e18254 | 2008-01-27 15:20:13 +0000 | [diff] [blame] | 227 | |
| 228 | def writeData(self, data): |
| 229 | self.beginElement("data") |
| 230 | self.indentLevel -= 1 |
| 231 | maxlinelength = 76 - len(self.indent.replace(b"\t", b" " * 8) * |
| 232 | self.indentLevel) |
| 233 | for line in data.asBase64(maxlinelength).split(b"\n"): |
| 234 | if line: |
| 235 | self.writeln(line) |
| 236 | self.indentLevel += 1 |
| 237 | self.endElement("data") |
| 238 | |
| 239 | def writeDict(self, d): |
| 240 | self.beginElement("dict") |
| 241 | items = sorted(d.items()) |
| 242 | for key, value in items: |
| 243 | if not isinstance(key, str): |
| 244 | raise TypeError("keys must be strings") |
| 245 | self.simpleElement("key", key) |
| 246 | self.writeValue(value) |
| 247 | self.endElement("dict") |
| 248 | |
| 249 | def writeArray(self, array): |
| 250 | self.beginElement("array") |
| 251 | for value in array: |
| 252 | self.writeValue(value) |
| 253 | self.endElement("array") |
| 254 | |
| 255 | |
| 256 | class _InternalDict(dict): |
| 257 | |
| 258 | # This class is needed while Dict is scheduled for deprecation: |
| 259 | # we only need to warn when a *user* instantiates Dict or when |
| 260 | # the "attribute notation for dict keys" is used. |
| 261 | |
| 262 | def __getattr__(self, attr): |
| 263 | try: |
| 264 | value = self[attr] |
| 265 | except KeyError: |
| 266 | raise AttributeError(attr) |
| 267 | from warnings import warn |
| 268 | warn("Attribute access from plist dicts is deprecated, use d[key] " |
Victor Stinner | b575289 | 2011-07-04 14:28:45 +0200 | [diff] [blame] | 269 | "notation instead", DeprecationWarning, 2) |
Christian Heimes | 7e18254 | 2008-01-27 15:20:13 +0000 | [diff] [blame] | 270 | return value |
| 271 | |
| 272 | def __setattr__(self, attr, value): |
| 273 | from warnings import warn |
| 274 | warn("Attribute access from plist dicts is deprecated, use d[key] " |
Victor Stinner | b575289 | 2011-07-04 14:28:45 +0200 | [diff] [blame] | 275 | "notation instead", DeprecationWarning, 2) |
Christian Heimes | 7e18254 | 2008-01-27 15:20:13 +0000 | [diff] [blame] | 276 | self[attr] = value |
| 277 | |
| 278 | def __delattr__(self, attr): |
| 279 | try: |
| 280 | del self[attr] |
| 281 | except KeyError: |
| 282 | raise AttributeError(attr) |
| 283 | from warnings import warn |
| 284 | warn("Attribute access from plist dicts is deprecated, use d[key] " |
Victor Stinner | b575289 | 2011-07-04 14:28:45 +0200 | [diff] [blame] | 285 | "notation instead", DeprecationWarning, 2) |
Christian Heimes | 7e18254 | 2008-01-27 15:20:13 +0000 | [diff] [blame] | 286 | |
| 287 | class Dict(_InternalDict): |
| 288 | |
| 289 | def __init__(self, **kwargs): |
| 290 | from warnings import warn |
| 291 | warn("The plistlib.Dict class is deprecated, use builtin dict instead", |
Victor Stinner | b575289 | 2011-07-04 14:28:45 +0200 | [diff] [blame] | 292 | DeprecationWarning, 2) |
Christian Heimes | 7e18254 | 2008-01-27 15:20:13 +0000 | [diff] [blame] | 293 | super().__init__(**kwargs) |
| 294 | |
| 295 | |
| 296 | class Plist(_InternalDict): |
| 297 | |
| 298 | """This class has been deprecated. Use readPlist() and writePlist() |
| 299 | functions instead, together with regular dict objects. |
| 300 | """ |
| 301 | |
| 302 | def __init__(self, **kwargs): |
| 303 | from warnings import warn |
| 304 | warn("The Plist class is deprecated, use the readPlist() and " |
Victor Stinner | b575289 | 2011-07-04 14:28:45 +0200 | [diff] [blame] | 305 | "writePlist() functions instead", DeprecationWarning, 2) |
Christian Heimes | 7e18254 | 2008-01-27 15:20:13 +0000 | [diff] [blame] | 306 | super().__init__(**kwargs) |
| 307 | |
| 308 | def fromFile(cls, pathOrFile): |
| 309 | """Deprecated. Use the readPlist() function instead.""" |
| 310 | rootObject = readPlist(pathOrFile) |
| 311 | plist = cls() |
| 312 | plist.update(rootObject) |
| 313 | return plist |
| 314 | fromFile = classmethod(fromFile) |
| 315 | |
| 316 | def write(self, pathOrFile): |
| 317 | """Deprecated. Use the writePlist() function instead.""" |
| 318 | writePlist(self, pathOrFile) |
| 319 | |
| 320 | |
| 321 | def _encodeBase64(s, maxlinelength=76): |
Georg Brandl | 706824f | 2009-06-04 09:42:55 +0000 | [diff] [blame] | 322 | # copied from base64.encodebytes(), with added maxlinelength argument |
Christian Heimes | 7e18254 | 2008-01-27 15:20:13 +0000 | [diff] [blame] | 323 | maxbinsize = (maxlinelength//4)*3 |
| 324 | pieces = [] |
| 325 | for i in range(0, len(s), maxbinsize): |
| 326 | chunk = s[i : i + maxbinsize] |
| 327 | pieces.append(binascii.b2a_base64(chunk)) |
| 328 | return b''.join(pieces) |
| 329 | |
| 330 | class Data: |
| 331 | |
| 332 | """Wrapper for binary data.""" |
| 333 | |
| 334 | def __init__(self, data): |
| 335 | if not isinstance(data, bytes): |
| 336 | raise TypeError("data must be as bytes") |
| 337 | self.data = data |
| 338 | |
| 339 | @classmethod |
| 340 | def fromBase64(cls, data): |
Georg Brandl | 706824f | 2009-06-04 09:42:55 +0000 | [diff] [blame] | 341 | # base64.decodebytes just calls binascii.a2b_base64; |
Christian Heimes | 7e18254 | 2008-01-27 15:20:13 +0000 | [diff] [blame] | 342 | # it seems overkill to use both base64 and binascii. |
| 343 | return cls(binascii.a2b_base64(data)) |
| 344 | |
| 345 | def asBase64(self, maxlinelength=76): |
| 346 | return _encodeBase64(self.data, maxlinelength) |
| 347 | |
| 348 | def __eq__(self, other): |
| 349 | if isinstance(other, self.__class__): |
| 350 | return self.data == other.data |
| 351 | elif isinstance(other, str): |
| 352 | return self.data == other |
| 353 | else: |
| 354 | return id(self) == id(other) |
| 355 | |
| 356 | def __repr__(self): |
| 357 | return "%s(%s)" % (self.__class__.__name__, repr(self.data)) |
| 358 | |
Christian Heimes | 7e18254 | 2008-01-27 15:20:13 +0000 | [diff] [blame] | 359 | class PlistParser: |
| 360 | |
| 361 | def __init__(self): |
| 362 | self.stack = [] |
| 363 | self.currentKey = None |
| 364 | self.root = None |
| 365 | |
| 366 | def parse(self, fileobj): |
| 367 | from xml.parsers.expat import ParserCreate |
Ned Deily | b8e59f7 | 2011-05-28 02:19:19 -0700 | [diff] [blame] | 368 | self.parser = ParserCreate() |
| 369 | self.parser.StartElementHandler = self.handleBeginElement |
| 370 | self.parser.EndElementHandler = self.handleEndElement |
| 371 | self.parser.CharacterDataHandler = self.handleData |
| 372 | self.parser.ParseFile(fileobj) |
Christian Heimes | 7e18254 | 2008-01-27 15:20:13 +0000 | [diff] [blame] | 373 | return self.root |
| 374 | |
| 375 | def handleBeginElement(self, element, attrs): |
| 376 | self.data = [] |
| 377 | handler = getattr(self, "begin_" + element, None) |
| 378 | if handler is not None: |
| 379 | handler(attrs) |
| 380 | |
| 381 | def handleEndElement(self, element): |
| 382 | handler = getattr(self, "end_" + element, None) |
| 383 | if handler is not None: |
| 384 | handler() |
| 385 | |
| 386 | def handleData(self, data): |
| 387 | self.data.append(data) |
| 388 | |
| 389 | def addObject(self, value): |
| 390 | if self.currentKey is not None: |
Ned Deily | b8e59f7 | 2011-05-28 02:19:19 -0700 | [diff] [blame] | 391 | if not isinstance(self.stack[-1], type({})): |
| 392 | raise ValueError("unexpected element at line %d" % |
| 393 | self.parser.CurrentLineNumber) |
Christian Heimes | 7e18254 | 2008-01-27 15:20:13 +0000 | [diff] [blame] | 394 | self.stack[-1][self.currentKey] = value |
| 395 | self.currentKey = None |
| 396 | elif not self.stack: |
| 397 | # this is the root object |
| 398 | self.root = value |
| 399 | else: |
Ned Deily | b8e59f7 | 2011-05-28 02:19:19 -0700 | [diff] [blame] | 400 | if not isinstance(self.stack[-1], type([])): |
| 401 | raise ValueError("unexpected element at line %d" % |
| 402 | self.parser.CurrentLineNumber) |
Christian Heimes | 7e18254 | 2008-01-27 15:20:13 +0000 | [diff] [blame] | 403 | self.stack[-1].append(value) |
| 404 | |
| 405 | def getData(self): |
| 406 | data = ''.join(self.data) |
| 407 | self.data = [] |
| 408 | return data |
| 409 | |
| 410 | # element handlers |
| 411 | |
| 412 | def begin_dict(self, attrs): |
| 413 | d = _InternalDict() |
| 414 | self.addObject(d) |
| 415 | self.stack.append(d) |
| 416 | def end_dict(self): |
Ned Deily | b8e59f7 | 2011-05-28 02:19:19 -0700 | [diff] [blame] | 417 | if self.currentKey: |
| 418 | raise ValueError("missing value for key '%s' at line %d" % |
| 419 | (self.currentKey,self.parser.CurrentLineNumber)) |
Christian Heimes | 7e18254 | 2008-01-27 15:20:13 +0000 | [diff] [blame] | 420 | self.stack.pop() |
| 421 | |
| 422 | def end_key(self): |
Ned Deily | b8e59f7 | 2011-05-28 02:19:19 -0700 | [diff] [blame] | 423 | if self.currentKey or not isinstance(self.stack[-1], type({})): |
| 424 | raise ValueError("unexpected key at line %d" % |
| 425 | self.parser.CurrentLineNumber) |
Christian Heimes | 7e18254 | 2008-01-27 15:20:13 +0000 | [diff] [blame] | 426 | self.currentKey = self.getData() |
| 427 | |
| 428 | def begin_array(self, attrs): |
| 429 | a = [] |
| 430 | self.addObject(a) |
| 431 | self.stack.append(a) |
| 432 | def end_array(self): |
| 433 | self.stack.pop() |
| 434 | |
| 435 | def end_true(self): |
| 436 | self.addObject(True) |
| 437 | def end_false(self): |
| 438 | self.addObject(False) |
| 439 | def end_integer(self): |
| 440 | self.addObject(int(self.getData())) |
| 441 | def end_real(self): |
| 442 | self.addObject(float(self.getData())) |
| 443 | def end_string(self): |
| 444 | self.addObject(self.getData()) |
| 445 | def end_data(self): |
| 446 | self.addObject(Data.fromBase64(self.getData().encode("utf-8"))) |
| 447 | def end_date(self): |
| 448 | self.addObject(_dateFromString(self.getData())) |