blob: 21076db9a5d4e05cf6082a103aabe452821413fa [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):
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
256class _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 Stinnerb5752892011-07-04 14:28:45 +0200269 "notation instead", DeprecationWarning, 2)
Christian Heimes7e182542008-01-27 15:20:13 +0000270 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 Stinnerb5752892011-07-04 14:28:45 +0200275 "notation instead", DeprecationWarning, 2)
Christian Heimes7e182542008-01-27 15:20:13 +0000276 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 Stinnerb5752892011-07-04 14:28:45 +0200285 "notation instead", DeprecationWarning, 2)
Christian Heimes7e182542008-01-27 15:20:13 +0000286
287class 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 Stinnerb5752892011-07-04 14:28:45 +0200292 DeprecationWarning, 2)
Christian Heimes7e182542008-01-27 15:20:13 +0000293 super().__init__(**kwargs)
294
295
296class 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 Stinnerb5752892011-07-04 14:28:45 +0200305 "writePlist() functions instead", DeprecationWarning, 2)
Christian Heimes7e182542008-01-27 15:20:13 +0000306 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
321def _encodeBase64(s, maxlinelength=76):
Georg Brandl706824f2009-06-04 09:42:55 +0000322 # copied from base64.encodebytes(), with added maxlinelength argument
Christian Heimes7e182542008-01-27 15:20:13 +0000323 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
330class 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 Brandl706824f2009-06-04 09:42:55 +0000341 # base64.decodebytes just calls binascii.a2b_base64;
Christian Heimes7e182542008-01-27 15:20:13 +0000342 # 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 Heimes7e182542008-01-27 15:20:13 +0000359class 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 Deilyb8e59f72011-05-28 02:19:19 -0700368 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 Heimes7e182542008-01-27 15:20:13 +0000373 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 Deilyb8e59f72011-05-28 02:19:19 -0700391 if not isinstance(self.stack[-1], type({})):
392 raise ValueError("unexpected element at line %d" %
393 self.parser.CurrentLineNumber)
Christian Heimes7e182542008-01-27 15:20:13 +0000394 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 Deilyb8e59f72011-05-28 02:19:19 -0700400 if not isinstance(self.stack[-1], type([])):
401 raise ValueError("unexpected element at line %d" %
402 self.parser.CurrentLineNumber)
Christian Heimes7e182542008-01-27 15:20:13 +0000403 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 Deilyb8e59f72011-05-28 02:19:19 -0700417 if self.currentKey:
418 raise ValueError("missing value for key '%s' at line %d" %
419 (self.currentKey,self.parser.CurrentLineNumber))
Christian Heimes7e182542008-01-27 15:20:13 +0000420 self.stack.pop()
421
422 def end_key(self):
Ned Deilyb8e59f72011-05-28 02:19:19 -0700423 if self.currentKey or not isinstance(self.stack[-1], type({})):
424 raise ValueError("unexpected key at line %d" %
425 self.parser.CurrentLineNumber)
Christian Heimes7e182542008-01-27 15:20:13 +0000426 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()))