blob: 6a2d6b4971c088d98eb0b4cfedf9931f11d63a2b [file] [log] [blame]
Georg Brandlc575c902008-09-13 17:46:05 +00001:mod:`plistlib` --- Generate and parse Mac OS X ``.plist`` files
2================================================================
Georg Brandl86def6c2008-01-21 20:36:10 +00003
4.. module:: plistlib
Georg Brandlc575c902008-09-13 17:46:05 +00005 :synopsis: Generate and parse Mac OS X plist files.
Georg Brandl86def6c2008-01-21 20:36:10 +00006.. moduleauthor:: Jack Jansen
7.. sectionauthor:: Georg Brandl <georg@python.org>
8.. (harvested from docstrings in the original file)
9
Georg Brandl86def6c2008-01-21 20:36:10 +000010.. index::
11 pair: plist; file
12 single: property list
13
Raymond Hettinger469271d2011-01-27 20:38:46 +000014**Source code:** :source:`Lib/plistlib.py`
15
16--------------
17
Georg Brandl86def6c2008-01-21 20:36:10 +000018This module provides an interface for reading and writing the "property list"
Ronald Oussorenc5cf7972013-11-21 15:46:49 +010019files used mainly by Mac OS X and supports both binary and XML plist files.
Georg Brandl86def6c2008-01-21 20:36:10 +000020
Ronald Oussorenc5cf7972013-11-21 15:46:49 +010021The property list (``.plist``) file format is a simple serialization supporting
Georg Brandl86def6c2008-01-21 20:36:10 +000022basic object types, like dictionaries, lists, numbers and strings. Usually the
23top level object is a dictionary.
24
Ronald Oussorenc5cf7972013-11-21 15:46:49 +010025To write out and to parse a plist file, use the :func:`dump` and
26:func:`load` functions.
Ezio Melotti6e9b1df2009-09-16 00:49:03 +000027
Ronald Oussorenc5cf7972013-11-21 15:46:49 +010028To work with plist data in bytes objects, use :func:`dumps`
29and :func:`loads`.
Ezio Melotti6e9b1df2009-09-16 00:49:03 +000030
Georg Brandl86def6c2008-01-21 20:36:10 +000031Values can be strings, integers, floats, booleans, tuples, lists, dictionaries
Ronald Oussorenc5cf7972013-11-21 15:46:49 +010032(but only with string keys), :class:`Data`, :class:`bytes`, :class:`bytesarray`
33or :class:`datetime.datetime` objects.
Georg Brandl86def6c2008-01-21 20:36:10 +000034
Larry Hastings3732ed22014-03-15 21:13:56 -070035.. versionchanged:: 3.4
36 New API, old API deprecated. Support for binary format plists added.
37
Georg Brandl86def6c2008-01-21 20:36:10 +000038.. seealso::
39
Ezio Melotti6e9b1df2009-09-16 00:49:03 +000040 `PList manual page <http://developer.apple.com/documentation/Darwin/Reference/ManPages/man5/plist.5.html>`_
Georg Brandl86def6c2008-01-21 20:36:10 +000041 Apple's documentation of the file format.
42
43
44This module defines the following functions:
45
Ronald Oussorenc5cf7972013-11-21 15:46:49 +010046.. function:: load(fp, \*, fmt=None, use_builtin_types=True, dict_type=dict)
Georg Brandl86def6c2008-01-21 20:36:10 +000047
Ronald Oussorenc5cf7972013-11-21 15:46:49 +010048 Read a plist file. *fp* should be a readable and binary file object.
49 Return the unpacked root object (which usually is a
Georg Brandl86def6c2008-01-21 20:36:10 +000050 dictionary).
51
Ronald Oussorenc5cf7972013-11-21 15:46:49 +010052 The *fmt* is the format of the file and the following values are valid:
53
54 * :data:`None`: Autodetect the file format
55
56 * :data:`FMT_XML`: XML file format
57
58 * :data:`FMT_BINARY`: Binary plist format
59
Serhiy Storchaka0e90e992013-11-29 12:19:53 +020060 If *use_builtin_types* is true (the default) binary data will be returned
Ronald Oussorenc5cf7972013-11-21 15:46:49 +010061 as instances of :class:`bytes`, otherwise it is returned as instances of
62 :class:`Data`.
63
64 The *dict_type* is the type used for dictionaries that are read from the
65 plist file. The exact structure of the plist can be recovered by using
66 :class:`collections.OrderedDict` (although the order of keys shouldn't be
67 important in plist files).
68
69 XML data for the :data:`FMT_XML` format is parsed using the Expat parser
70 from :mod:`xml.parsers.expat` -- see its documentation for possible
71 exceptions on ill-formed XML. Unknown elements will simply be ignored
72 by the plist parser.
73
74 The parser for the binary format raises :exc:`InvalidFileException`
75 when the file cannot be parsed.
76
77 .. versionadded:: 3.4
78
79
80.. function:: loads(data, \*, fmt=None, use_builtin_types=True, dict_type=dict)
81
82 Load a plist from a bytes object. See :func:`load` for an explanation of
83 the keyword arguments.
84
Ronald Oussoren6db66532014-01-15 11:32:35 +010085 .. versionadded:: 3.4
86
Ronald Oussorenc5cf7972013-11-21 15:46:49 +010087
88.. function:: dump(value, fp, \*, fmt=FMT_XML, sort_keys=True, skipkeys=False)
89
90 Write *value* to a plist file. *Fp* should be a writable, binary
91 file object.
92
93 The *fmt* argument specifies the format of the plist file and can be
94 one of the following values:
95
96 * :data:`FMT_XML`: XML formatted plist file
97
98 * :data:`FMT_BINARY`: Binary formatted plist file
99
100 When *sort_keys* is true (the default) the keys for dictionaries will be
101 written to the plist in sorted order, otherwise they will be written in
102 the iteration order of the dictionary.
103
104 When *skipkeys* is false (the default) the function raises :exc:`TypeError`
105 when a key of a dictionary is not a string, otherwise such keys are skipped.
106
107 A :exc:`TypeError` will be raised if the object is of an unsupported type or
108 a container that contains objects of unsupported types.
109
Ronald Oussoren6db66532014-01-15 11:32:35 +0100110 An :exc:`OverflowError` will be raised for integer values that cannot
111 be represented in (binary) plist files.
112
Larry Hastings3732ed22014-03-15 21:13:56 -0700113 .. versionadded:: 3.4
Ronald Oussorenc5cf7972013-11-21 15:46:49 +0100114
115
116.. function:: dumps(value, \*, fmt=FMT_XML, sort_keys=True, skipkeys=False)
117
118 Return *value* as a plist-formatted bytes object. See
119 the documentation for :func:`dump` for an explanation of the keyword
120 arguments of this function.
121
Larry Hastings3732ed22014-03-15 21:13:56 -0700122 .. versionadded:: 3.4
Ronald Oussorenc5cf7972013-11-21 15:46:49 +0100123
124The following functions are deprecated:
125
126.. function:: readPlist(pathOrFile)
127
128 Read a plist file. *pathOrFile* may be either a file name or a (readable
129 and binary) file object. Returns the unpacked root object (which usually
130 is a dictionary).
131
132 This function calls :func:`load` to do the actual work, the the documentation
133 of :func:`that function <load>` for an explanation of the keyword arguments.
134
135 .. note::
136
137 Dict values in the result have a ``__getattr__`` method that defers
138 to ``__getitem_``. This means that you can use attribute access to
139 access items of these dictionaries.
140
Larry Hastings3732ed22014-03-15 21:13:56 -0700141 .. deprecated:: 3.4 Use :func:`load` instead.
Georg Brandl86def6c2008-01-21 20:36:10 +0000142
143
144.. function:: writePlist(rootObject, pathOrFile)
145
Ronald Oussorenc5cf7972013-11-21 15:46:49 +0100146 Write *rootObject* to an XML plist file. *pathOrFile* may be either a file name
147 or a (writable and binary) file object
Georg Brandl86def6c2008-01-21 20:36:10 +0000148
Larry Hastings3732ed22014-03-15 21:13:56 -0700149 .. deprecated:: 3.4 Use :func:`dump` instead.
Georg Brandl86def6c2008-01-21 20:36:10 +0000150
151
Ezio Melotti6e9b1df2009-09-16 00:49:03 +0000152.. function:: readPlistFromBytes(data)
Georg Brandl86def6c2008-01-21 20:36:10 +0000153
Ezio Melotti6e9b1df2009-09-16 00:49:03 +0000154 Read a plist data from a bytes object. Return the root object.
Georg Brandl86def6c2008-01-21 20:36:10 +0000155
Ronald Oussorenc5cf7972013-11-21 15:46:49 +0100156 See :func:`load` for a description of the keyword arguments.
157
158 .. note::
159
160 Dict values in the result have a ``__getattr__`` method that defers
161 to ``__getitem_``. This means that you can use attribute access to
162 access items of these dictionaries.
163
164 .. deprecated:: 3.4 Use :func:`loads` instead.
165
Georg Brandl86def6c2008-01-21 20:36:10 +0000166
Ezio Melotti6e9b1df2009-09-16 00:49:03 +0000167.. function:: writePlistToBytes(rootObject)
Georg Brandl86def6c2008-01-21 20:36:10 +0000168
Ronald Oussorenc5cf7972013-11-21 15:46:49 +0100169 Return *rootObject* as an XML plist-formatted bytes object.
170
171 .. deprecated:: 3.4 Use :func:`dumps` instead.
172
Georg Brandl86def6c2008-01-21 20:36:10 +0000173
Ronald Oussorenc5cf7972013-11-21 15:46:49 +0100174The following classes are available:
175
176.. class:: Dict([dict]):
177
178 Return an extended mapping object with the same value as dictionary
179 *dict*.
180
181 This class is a subclass of :class:`dict` where attribute access can
182 be used to access items. That is, ``aDict.key`` is the same as
183 ``aDict['key']`` for getting, setting and deleting items in the mapping.
184
185 .. deprecated:: 3.0
186
Georg Brandl86def6c2008-01-21 20:36:10 +0000187
188.. class:: Data(data)
189
Ezio Melotti6e9b1df2009-09-16 00:49:03 +0000190 Return a "data" wrapper object around the bytes object *data*. This is used
191 in functions converting from/to plists to represent the ``<data>`` type
Georg Brandl86def6c2008-01-21 20:36:10 +0000192 available in plists.
193
194 It has one attribute, :attr:`data`, that can be used to retrieve the Python
Ezio Melotti6e9b1df2009-09-16 00:49:03 +0000195 bytes object stored in it.
Georg Brandl86def6c2008-01-21 20:36:10 +0000196
Ronald Oussorenc5cf7972013-11-21 15:46:49 +0100197 .. deprecated:: 3.4 Use a :class:`bytes` object instead
198
199
Larry Hastings3732ed22014-03-15 21:13:56 -0700200The following constants are available:
Ronald Oussorenc5cf7972013-11-21 15:46:49 +0100201
202.. data:: FMT_XML
203
204 The XML format for plist files.
205
206 .. versionadded:: 3.4
207
208
209.. data:: FMT_BINARY
210
211 The binary format for plist files
212
213 .. versionadded:: 3.4
214
Georg Brandl86def6c2008-01-21 20:36:10 +0000215
216Examples
217--------
218
219Generating a plist::
220
221 pl = dict(
Ezio Melotti985e24d2009-09-13 07:54:02 +0000222 aString = "Doodah",
223 aList = ["A", "B", 12, 32.1, [1, 2, 3]],
Georg Brandl86def6c2008-01-21 20:36:10 +0000224 aFloat = 0.1,
225 anInt = 728,
Ezio Melotti985e24d2009-09-13 07:54:02 +0000226 aDict = dict(
227 anotherString = "<hello & hi there!>",
228 aThirdString = "M\xe4ssig, Ma\xdf",
229 aTrueValue = True,
230 aFalseValue = False,
Georg Brandl86def6c2008-01-21 20:36:10 +0000231 ),
Ronald Oussorenc5cf7972013-11-21 15:46:49 +0100232 someData = b"<binary gunk>",
233 someMoreData = b"<lots of binary gunk>" * 10,
Georg Brandl86def6c2008-01-21 20:36:10 +0000234 aDate = datetime.datetime.fromtimestamp(time.mktime(time.gmtime())),
235 )
Ronald Oussorenc5cf7972013-11-21 15:46:49 +0100236 with open(fileName, 'wb') as fp:
237 dump(pl, fp)
Georg Brandl86def6c2008-01-21 20:36:10 +0000238
239Parsing a plist::
240
Ronald Oussorenc5cf7972013-11-21 15:46:49 +0100241 with open(fileName, 'rb') as fp:
242 pl = load(fp)
Neal Norwitz752abd02008-05-13 04:55:24 +0000243 print(pl["aKey"])