blob: 20c086c8b524a593f493dceb450f9eae7a6342d9 [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.
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04006
Georg Brandl86def6c2008-01-21 20:36:10 +00007.. moduleauthor:: Jack Jansen
8.. sectionauthor:: Georg Brandl <georg@python.org>
9.. (harvested from docstrings in the original file)
10
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040011**Source code:** :source:`Lib/plistlib.py`
12
Georg Brandl86def6c2008-01-21 20:36:10 +000013.. index::
14 pair: plist; file
15 single: property list
16
Raymond Hettinger469271d2011-01-27 20:38:46 +000017--------------
18
Georg Brandl86def6c2008-01-21 20:36:10 +000019This module provides an interface for reading and writing the "property list"
Ronald Oussorenc5cf7972013-11-21 15:46:49 +010020files used mainly by Mac OS X and supports both binary and XML plist files.
Georg Brandl86def6c2008-01-21 20:36:10 +000021
Ronald Oussorenc5cf7972013-11-21 15:46:49 +010022The property list (``.plist``) file format is a simple serialization supporting
Georg Brandl86def6c2008-01-21 20:36:10 +000023basic object types, like dictionaries, lists, numbers and strings. Usually the
24top level object is a dictionary.
25
Ronald Oussorenc5cf7972013-11-21 15:46:49 +010026To write out and to parse a plist file, use the :func:`dump` and
27:func:`load` functions.
Ezio Melotti6e9b1df2009-09-16 00:49:03 +000028
Ronald Oussorenc5cf7972013-11-21 15:46:49 +010029To work with plist data in bytes objects, use :func:`dumps`
30and :func:`loads`.
Ezio Melotti6e9b1df2009-09-16 00:49:03 +000031
Georg Brandl86def6c2008-01-21 20:36:10 +000032Values can be strings, integers, floats, booleans, tuples, lists, dictionaries
Ronald Oussorenc5cf7972013-11-21 15:46:49 +010033(but only with string keys), :class:`Data`, :class:`bytes`, :class:`bytesarray`
34or :class:`datetime.datetime` objects.
Georg Brandl86def6c2008-01-21 20:36:10 +000035
Larry Hastings3732ed22014-03-15 21:13:56 -070036.. versionchanged:: 3.4
37 New API, old API deprecated. Support for binary format plists added.
38
Georg Brandl86def6c2008-01-21 20:36:10 +000039.. seealso::
40
Sanyam Khurana338cd832018-01-20 05:55:37 +053041 `PList manual page <https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/PropertyLists/>`_
Georg Brandl86def6c2008-01-21 20:36:10 +000042 Apple's documentation of the file format.
43
44
45This module defines the following functions:
46
Ronald Oussorenc5cf7972013-11-21 15:46:49 +010047.. function:: load(fp, \*, fmt=None, use_builtin_types=True, dict_type=dict)
Georg Brandl86def6c2008-01-21 20:36:10 +000048
Ronald Oussorenc5cf7972013-11-21 15:46:49 +010049 Read a plist file. *fp* should be a readable and binary file object.
50 Return the unpacked root object (which usually is a
Georg Brandl86def6c2008-01-21 20:36:10 +000051 dictionary).
52
Ronald Oussorenc5cf7972013-11-21 15:46:49 +010053 The *fmt* is the format of the file and the following values are valid:
54
55 * :data:`None`: Autodetect the file format
56
57 * :data:`FMT_XML`: XML file format
58
59 * :data:`FMT_BINARY`: Binary plist format
60
Serhiy Storchaka0e90e992013-11-29 12:19:53 +020061 If *use_builtin_types* is true (the default) binary data will be returned
Ronald Oussorenc5cf7972013-11-21 15:46:49 +010062 as instances of :class:`bytes`, otherwise it is returned as instances of
63 :class:`Data`.
64
65 The *dict_type* is the type used for dictionaries that are read from the
66 plist file. The exact structure of the plist can be recovered by using
67 :class:`collections.OrderedDict` (although the order of keys shouldn't be
68 important in plist files).
69
70 XML data for the :data:`FMT_XML` format is parsed using the Expat parser
71 from :mod:`xml.parsers.expat` -- see its documentation for possible
72 exceptions on ill-formed XML. Unknown elements will simply be ignored
73 by the plist parser.
74
75 The parser for the binary format raises :exc:`InvalidFileException`
76 when the file cannot be parsed.
77
78 .. versionadded:: 3.4
79
80
81.. function:: loads(data, \*, fmt=None, use_builtin_types=True, dict_type=dict)
82
83 Load a plist from a bytes object. See :func:`load` for an explanation of
84 the keyword arguments.
85
Ronald Oussoren6db66532014-01-15 11:32:35 +010086 .. versionadded:: 3.4
87
Ronald Oussorenc5cf7972013-11-21 15:46:49 +010088
89.. function:: dump(value, fp, \*, fmt=FMT_XML, sort_keys=True, skipkeys=False)
90
91 Write *value* to a plist file. *Fp* should be a writable, binary
92 file object.
93
94 The *fmt* argument specifies the format of the plist file and can be
95 one of the following values:
96
97 * :data:`FMT_XML`: XML formatted plist file
98
99 * :data:`FMT_BINARY`: Binary formatted plist file
100
101 When *sort_keys* is true (the default) the keys for dictionaries will be
102 written to the plist in sorted order, otherwise they will be written in
103 the iteration order of the dictionary.
104
105 When *skipkeys* is false (the default) the function raises :exc:`TypeError`
106 when a key of a dictionary is not a string, otherwise such keys are skipped.
107
108 A :exc:`TypeError` will be raised if the object is of an unsupported type or
109 a container that contains objects of unsupported types.
110
Ronald Oussoren6db66532014-01-15 11:32:35 +0100111 An :exc:`OverflowError` will be raised for integer values that cannot
112 be represented in (binary) plist files.
113
Larry Hastings3732ed22014-03-15 21:13:56 -0700114 .. versionadded:: 3.4
Ronald Oussorenc5cf7972013-11-21 15:46:49 +0100115
116
117.. function:: dumps(value, \*, fmt=FMT_XML, sort_keys=True, skipkeys=False)
118
119 Return *value* as a plist-formatted bytes object. See
120 the documentation for :func:`dump` for an explanation of the keyword
121 arguments of this function.
122
Larry Hastings3732ed22014-03-15 21:13:56 -0700123 .. versionadded:: 3.4
Ronald Oussorenc5cf7972013-11-21 15:46:49 +0100124
125The following functions are deprecated:
126
127.. function:: readPlist(pathOrFile)
128
129 Read a plist file. *pathOrFile* may be either a file name or a (readable
130 and binary) file object. Returns the unpacked root object (which usually
131 is a dictionary).
132
Senthil Kumaranb4760ef2015-06-14 17:35:37 -0700133 This function calls :func:`load` to do the actual work, see the documentation
Ronald Oussorenc5cf7972013-11-21 15:46:49 +0100134 of :func:`that function <load>` for an explanation of the keyword arguments.
135
Larry Hastings3732ed22014-03-15 21:13:56 -0700136 .. deprecated:: 3.4 Use :func:`load` instead.
Georg Brandl86def6c2008-01-21 20:36:10 +0000137
Serhiy Storchakaedef3582017-05-15 13:21:31 +0300138 .. versionchanged:: 3.7
139 Dict values in the result are now normal dicts. You no longer can use
140 attribute access to access items of these dictionaries.
141
Georg Brandl86def6c2008-01-21 20:36:10 +0000142
143.. function:: writePlist(rootObject, pathOrFile)
144
Ronald Oussorenc5cf7972013-11-21 15:46:49 +0100145 Write *rootObject* to an XML plist file. *pathOrFile* may be either a file name
146 or a (writable and binary) file object
Georg Brandl86def6c2008-01-21 20:36:10 +0000147
Larry Hastings3732ed22014-03-15 21:13:56 -0700148 .. deprecated:: 3.4 Use :func:`dump` instead.
Georg Brandl86def6c2008-01-21 20:36:10 +0000149
150
Ezio Melotti6e9b1df2009-09-16 00:49:03 +0000151.. function:: readPlistFromBytes(data)
Georg Brandl86def6c2008-01-21 20:36:10 +0000152
Ezio Melotti6e9b1df2009-09-16 00:49:03 +0000153 Read a plist data from a bytes object. Return the root object.
Georg Brandl86def6c2008-01-21 20:36:10 +0000154
Ronald Oussorenc5cf7972013-11-21 15:46:49 +0100155 See :func:`load` for a description of the keyword arguments.
156
Ronald Oussorenc5cf7972013-11-21 15:46:49 +0100157 .. deprecated:: 3.4 Use :func:`loads` instead.
158
Serhiy Storchakaedef3582017-05-15 13:21:31 +0300159 .. versionchanged:: 3.7
160 Dict values in the result are now normal dicts. You no longer can use
161 attribute access to access items of these dictionaries.
162
Georg Brandl86def6c2008-01-21 20:36:10 +0000163
Ezio Melotti6e9b1df2009-09-16 00:49:03 +0000164.. function:: writePlistToBytes(rootObject)
Georg Brandl86def6c2008-01-21 20:36:10 +0000165
Ronald Oussorenc5cf7972013-11-21 15:46:49 +0100166 Return *rootObject* as an XML plist-formatted bytes object.
167
168 .. deprecated:: 3.4 Use :func:`dumps` instead.
169
Georg Brandl86def6c2008-01-21 20:36:10 +0000170
Ronald Oussorenc5cf7972013-11-21 15:46:49 +0100171The following classes are available:
172
Georg Brandl86def6c2008-01-21 20:36:10 +0000173.. class:: Data(data)
174
Ezio Melotti6e9b1df2009-09-16 00:49:03 +0000175 Return a "data" wrapper object around the bytes object *data*. This is used
176 in functions converting from/to plists to represent the ``<data>`` type
Georg Brandl86def6c2008-01-21 20:36:10 +0000177 available in plists.
178
179 It has one attribute, :attr:`data`, that can be used to retrieve the Python
Ezio Melotti6e9b1df2009-09-16 00:49:03 +0000180 bytes object stored in it.
Georg Brandl86def6c2008-01-21 20:36:10 +0000181
Martin Panterd21e0b52015-10-10 10:36:22 +0000182 .. deprecated:: 3.4 Use a :class:`bytes` object instead.
Ronald Oussorenc5cf7972013-11-21 15:46:49 +0100183
184
Larry Hastings3732ed22014-03-15 21:13:56 -0700185The following constants are available:
Ronald Oussorenc5cf7972013-11-21 15:46:49 +0100186
187.. data:: FMT_XML
188
189 The XML format for plist files.
190
191 .. versionadded:: 3.4
192
193
194.. data:: FMT_BINARY
195
196 The binary format for plist files
197
198 .. versionadded:: 3.4
199
Georg Brandl86def6c2008-01-21 20:36:10 +0000200
201Examples
202--------
203
204Generating a plist::
205
206 pl = dict(
Ezio Melotti985e24d2009-09-13 07:54:02 +0000207 aString = "Doodah",
208 aList = ["A", "B", 12, 32.1, [1, 2, 3]],
Georg Brandl86def6c2008-01-21 20:36:10 +0000209 aFloat = 0.1,
210 anInt = 728,
Ezio Melotti985e24d2009-09-13 07:54:02 +0000211 aDict = dict(
212 anotherString = "<hello & hi there!>",
213 aThirdString = "M\xe4ssig, Ma\xdf",
214 aTrueValue = True,
215 aFalseValue = False,
Georg Brandl86def6c2008-01-21 20:36:10 +0000216 ),
Ronald Oussorenc5cf7972013-11-21 15:46:49 +0100217 someData = b"<binary gunk>",
218 someMoreData = b"<lots of binary gunk>" * 10,
Georg Brandl86def6c2008-01-21 20:36:10 +0000219 aDate = datetime.datetime.fromtimestamp(time.mktime(time.gmtime())),
220 )
Ronald Oussorenc5cf7972013-11-21 15:46:49 +0100221 with open(fileName, 'wb') as fp:
222 dump(pl, fp)
Georg Brandl86def6c2008-01-21 20:36:10 +0000223
224Parsing a plist::
225
Ronald Oussorenc5cf7972013-11-21 15:46:49 +0100226 with open(fileName, 'rb') as fp:
227 pl = load(fp)
Neal Norwitz752abd02008-05-13 04:55:24 +0000228 print(pl["aKey"])