blob: 41fd8f2efd12088ccc86b298b9389b0cba883ada [file] [log] [blame]
Benjamin Petersonef3e4c22009-04-11 19:48:14 +00001r"""plistlib.py -- a tool to generate and parse MacOSX .plist files.
Christian Heimes7e182542008-01-27 15:20:13 +00002
Ezio Melotti6e9b1df2009-09-16 00:49:03 +00003The property list (.plist) file format is a simple XML pickle supporting
Christian Heimes7e182542008-01-27 15:20:13 +00004basic object types, like dictionaries, lists, numbers and strings.
5Usually the top level object is a dictionary.
6
7To write out a plist file, use the writePlist(rootObject, pathOrFile)
8function. 'rootObject' is the top level object, 'pathOrFile' is a
9filename or a (writable) file object.
10
11To parse a plist from a file, use the readPlist(pathOrFile) function,
12with a file name or a (readable) file object as the only argument. It
13returns the top level object (again, usually a dictionary).
14
15To work with plist data in bytes objects, you can use readPlistFromBytes()
16and writePlistToBytes().
17
18Values can be strings, integers, floats, booleans, tuples, lists,
Ezio Melotti6e9b1df2009-09-16 00:49:03 +000019dictionaries (but only with string keys), Data or datetime.datetime objects.
20String values (including dictionary keys) have to be unicode strings -- they
21will be written out as UTF-8.
Christian Heimes7e182542008-01-27 15:20:13 +000022
23The <data> plist type is supported through the Data class. This is a
Ezio Melotti6e9b1df2009-09-16 00:49:03 +000024thin wrapper around a Python bytes object. Use 'Data' if your strings
25contain control characters.
Christian Heimes7e182542008-01-27 15:20:13 +000026
27Generate Plist example:
28
29 pl = dict(
Ezio Melotti6e9b1df2009-09-16 00:49:03 +000030 aString = "Doodah",
31 aList = ["A", "B", 12, 32.1, [1, 2, 3]],
Christian Heimes7e182542008-01-27 15:20:13 +000032 aFloat = 0.1,
33 anInt = 728,
Ezio Melotti6e9b1df2009-09-16 00:49:03 +000034 aDict = dict(
35 anotherString = "<hello & hi there!>",
36 aUnicodeValue = "M\xe4ssig, Ma\xdf",
37 aTrueValue = True,
38 aFalseValue = False,
Christian Heimes7e182542008-01-27 15:20:13 +000039 ),
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 Heimes7e182542008-01-27 15:20:13 +000044 writePlist(pl, fileName)
45
46Parse Plist example:
47
48 pl = readPlist(pathOrFile)
49 print pl["aKey"]
50"""
51
52
53__all__ = [
54 "readPlist", "writePlist", "readPlistFromBytes", "writePlistToBytes",
Christian Heimes7e182542008-01-27 15:20:13 +000055 "Plist", "Data", "Dict"
56]
57# Note: the Plist and Dict classes have been deprecated.
58
59import binascii
60import datetime
61from io import BytesIO
62import re
63
64
65def 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 Deilyb8e59f72011-05-28 02:19:19 -070071 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 Heimes7e182542008-01-27 15:20:13 +000080 return rootObject
81
82
83def 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 Deilyb8e59f72011-05-28 02:19:19 -070088 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 Heimes7e182542008-01-27 15:20:13 +000099
100
101def readPlistFromBytes(data):
102 """Read a plist data from a bytes object. Return the root object.
103 """
104 return readPlist(BytesIO(data))
105
106
107def 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 Heimes7e182542008-01-27 15:20:13 +0000115class 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 Pitroufd036452008-08-19 17:56:33 +0000153_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 Heimes7e182542008-01-27 15:20:13 +0000154
155def _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
166def _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
178def _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("&", "&amp;") # escape '&'
186 text = text.replace("<", "&lt;") # escape '<'
187 text = text.replace(">", "&gt;") # escape '>'
188 return text
189
190
191PLISTHEADER = b"""\
192<?xml version="1.0" encoding="UTF-8"?>
Ronald Oussoren33798fd2010-04-20 21:00:34 +0000193<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
Christian Heimes7e182542008-01-27 15:20:13 +0000194"""
195
196class 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 Melotti6e9b1df2009-09-16 00:49:03 +0000226 raise TypeError("unsupported type: %s" % type(value))
Christian Heimes7e182542008-01-27 15:20:13 +0000227
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):
Hynek Schlawack52209d32012-05-29 12:04:54 +0200240 if d:
241 self.beginElement("dict")
242 items = sorted(d.items())
243 for key, value in items:
244 if not isinstance(key, str):
245 raise TypeError("keys must be strings")
246 self.simpleElement("key", key)
247 self.writeValue(value)
248 self.endElement("dict")
249 else:
250 self.simpleElement("dict")
Christian Heimes7e182542008-01-27 15:20:13 +0000251
252 def writeArray(self, array):
Hynek Schlawack52209d32012-05-29 12:04:54 +0200253 if array:
254 self.beginElement("array")
255 for value in array:
256 self.writeValue(value)
257 self.endElement("array")
258 else:
259 self.simpleElement("array")
Christian Heimes7e182542008-01-27 15:20:13 +0000260
261
262class _InternalDict(dict):
263
264 # This class is needed while Dict is scheduled for deprecation:
265 # we only need to warn when a *user* instantiates Dict or when
266 # the "attribute notation for dict keys" is used.
267
268 def __getattr__(self, attr):
269 try:
270 value = self[attr]
271 except KeyError:
272 raise AttributeError(attr)
273 from warnings import warn
274 warn("Attribute access from plist dicts is deprecated, use d[key] "
Victor Stinnerb5752892011-07-04 14:28:45 +0200275 "notation instead", DeprecationWarning, 2)
Christian Heimes7e182542008-01-27 15:20:13 +0000276 return value
277
278 def __setattr__(self, attr, value):
279 from warnings import warn
280 warn("Attribute access from plist dicts is deprecated, use d[key] "
Victor Stinnerb5752892011-07-04 14:28:45 +0200281 "notation instead", DeprecationWarning, 2)
Christian Heimes7e182542008-01-27 15:20:13 +0000282 self[attr] = value
283
284 def __delattr__(self, attr):
285 try:
286 del self[attr]
287 except KeyError:
288 raise AttributeError(attr)
289 from warnings import warn
290 warn("Attribute access from plist dicts is deprecated, use d[key] "
Victor Stinnerb5752892011-07-04 14:28:45 +0200291 "notation instead", DeprecationWarning, 2)
Christian Heimes7e182542008-01-27 15:20:13 +0000292
293class Dict(_InternalDict):
294
295 def __init__(self, **kwargs):
296 from warnings import warn
297 warn("The plistlib.Dict class is deprecated, use builtin dict instead",
Victor Stinnerb5752892011-07-04 14:28:45 +0200298 DeprecationWarning, 2)
Christian Heimes7e182542008-01-27 15:20:13 +0000299 super().__init__(**kwargs)
300
301
302class Plist(_InternalDict):
303
304 """This class has been deprecated. Use readPlist() and writePlist()
305 functions instead, together with regular dict objects.
306 """
307
308 def __init__(self, **kwargs):
309 from warnings import warn
310 warn("The Plist class is deprecated, use the readPlist() and "
Victor Stinnerb5752892011-07-04 14:28:45 +0200311 "writePlist() functions instead", DeprecationWarning, 2)
Christian Heimes7e182542008-01-27 15:20:13 +0000312 super().__init__(**kwargs)
313
314 def fromFile(cls, pathOrFile):
315 """Deprecated. Use the readPlist() function instead."""
316 rootObject = readPlist(pathOrFile)
317 plist = cls()
318 plist.update(rootObject)
319 return plist
320 fromFile = classmethod(fromFile)
321
322 def write(self, pathOrFile):
323 """Deprecated. Use the writePlist() function instead."""
324 writePlist(self, pathOrFile)
325
326
327def _encodeBase64(s, maxlinelength=76):
Georg Brandl706824f2009-06-04 09:42:55 +0000328 # copied from base64.encodebytes(), with added maxlinelength argument
Christian Heimes7e182542008-01-27 15:20:13 +0000329 maxbinsize = (maxlinelength//4)*3
330 pieces = []
331 for i in range(0, len(s), maxbinsize):
332 chunk = s[i : i + maxbinsize]
333 pieces.append(binascii.b2a_base64(chunk))
334 return b''.join(pieces)
335
336class Data:
337
338 """Wrapper for binary data."""
339
340 def __init__(self, data):
341 if not isinstance(data, bytes):
342 raise TypeError("data must be as bytes")
343 self.data = data
344
345 @classmethod
346 def fromBase64(cls, data):
Georg Brandl706824f2009-06-04 09:42:55 +0000347 # base64.decodebytes just calls binascii.a2b_base64;
Christian Heimes7e182542008-01-27 15:20:13 +0000348 # it seems overkill to use both base64 and binascii.
349 return cls(binascii.a2b_base64(data))
350
351 def asBase64(self, maxlinelength=76):
352 return _encodeBase64(self.data, maxlinelength)
353
354 def __eq__(self, other):
355 if isinstance(other, self.__class__):
356 return self.data == other.data
357 elif isinstance(other, str):
358 return self.data == other
359 else:
360 return id(self) == id(other)
361
362 def __repr__(self):
363 return "%s(%s)" % (self.__class__.__name__, repr(self.data))
364
Christian Heimes7e182542008-01-27 15:20:13 +0000365class PlistParser:
366
367 def __init__(self):
368 self.stack = []
369 self.currentKey = None
370 self.root = None
371
372 def parse(self, fileobj):
373 from xml.parsers.expat import ParserCreate
Ned Deilyb8e59f72011-05-28 02:19:19 -0700374 self.parser = ParserCreate()
375 self.parser.StartElementHandler = self.handleBeginElement
376 self.parser.EndElementHandler = self.handleEndElement
377 self.parser.CharacterDataHandler = self.handleData
378 self.parser.ParseFile(fileobj)
Christian Heimes7e182542008-01-27 15:20:13 +0000379 return self.root
380
381 def handleBeginElement(self, element, attrs):
382 self.data = []
383 handler = getattr(self, "begin_" + element, None)
384 if handler is not None:
385 handler(attrs)
386
387 def handleEndElement(self, element):
388 handler = getattr(self, "end_" + element, None)
389 if handler is not None:
390 handler()
391
392 def handleData(self, data):
393 self.data.append(data)
394
395 def addObject(self, value):
396 if self.currentKey is not None:
Ned Deilyb8e59f72011-05-28 02:19:19 -0700397 if not isinstance(self.stack[-1], type({})):
398 raise ValueError("unexpected element at line %d" %
399 self.parser.CurrentLineNumber)
Christian Heimes7e182542008-01-27 15:20:13 +0000400 self.stack[-1][self.currentKey] = value
401 self.currentKey = None
402 elif not self.stack:
403 # this is the root object
404 self.root = value
405 else:
Ned Deilyb8e59f72011-05-28 02:19:19 -0700406 if not isinstance(self.stack[-1], type([])):
407 raise ValueError("unexpected element at line %d" %
408 self.parser.CurrentLineNumber)
Christian Heimes7e182542008-01-27 15:20:13 +0000409 self.stack[-1].append(value)
410
411 def getData(self):
412 data = ''.join(self.data)
413 self.data = []
414 return data
415
416 # element handlers
417
418 def begin_dict(self, attrs):
419 d = _InternalDict()
420 self.addObject(d)
421 self.stack.append(d)
422 def end_dict(self):
Ned Deilyb8e59f72011-05-28 02:19:19 -0700423 if self.currentKey:
424 raise ValueError("missing value for key '%s' at line %d" %
425 (self.currentKey,self.parser.CurrentLineNumber))
Christian Heimes7e182542008-01-27 15:20:13 +0000426 self.stack.pop()
427
428 def end_key(self):
Ned Deilyb8e59f72011-05-28 02:19:19 -0700429 if self.currentKey or not isinstance(self.stack[-1], type({})):
430 raise ValueError("unexpected key at line %d" %
431 self.parser.CurrentLineNumber)
Christian Heimes7e182542008-01-27 15:20:13 +0000432 self.currentKey = self.getData()
433
434 def begin_array(self, attrs):
435 a = []
436 self.addObject(a)
437 self.stack.append(a)
438 def end_array(self):
439 self.stack.pop()
440
441 def end_true(self):
442 self.addObject(True)
443 def end_false(self):
444 self.addObject(False)
445 def end_integer(self):
446 self.addObject(int(self.getData()))
447 def end_real(self):
448 self.addObject(float(self.getData()))
449 def end_string(self):
450 self.addObject(self.getData())
451 def end_data(self):
452 self.addObject(Data.fromBase64(self.getData().encode("utf-8")))
453 def end_date(self):
454 self.addObject(_dateFromString(self.getData()))