Georg Brandl | c575c90 | 2008-09-13 17:46:05 +0000 | [diff] [blame] | 1 | :mod:`plistlib` --- Generate and parse Mac OS X ``.plist`` files |
| 2 | ================================================================ |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 3 | |
| 4 | .. module:: plistlib |
Georg Brandl | c575c90 | 2008-09-13 17:46:05 +0000 | [diff] [blame] | 5 | :synopsis: Generate and parse Mac OS X plist files. |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 6 | .. moduleauthor:: Jack Jansen |
| 7 | .. sectionauthor:: Georg Brandl <georg@python.org> |
| 8 | .. (harvested from docstrings in the original file) |
| 9 | |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 10 | .. index:: |
| 11 | pair: plist; file |
| 12 | single: property list |
| 13 | |
Raymond Hettinger | 469271d | 2011-01-27 20:38:46 +0000 | [diff] [blame] | 14 | **Source code:** :source:`Lib/plistlib.py` |
| 15 | |
| 16 | -------------- |
| 17 | |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 18 | This module provides an interface for reading and writing the "property list" |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 19 | files used mainly by Mac OS X and supports both binary and XML plist files. |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 20 | |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 21 | The property list (``.plist``) file format is a simple serialization supporting |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 22 | basic object types, like dictionaries, lists, numbers and strings. Usually the |
| 23 | top level object is a dictionary. |
| 24 | |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 25 | To write out and to parse a plist file, use the :func:`dump` and |
| 26 | :func:`load` functions. |
Ezio Melotti | 6e9b1df | 2009-09-16 00:49:03 +0000 | [diff] [blame] | 27 | |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 28 | To work with plist data in bytes objects, use :func:`dumps` |
| 29 | and :func:`loads`. |
Ezio Melotti | 6e9b1df | 2009-09-16 00:49:03 +0000 | [diff] [blame] | 30 | |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 31 | Values can be strings, integers, floats, booleans, tuples, lists, dictionaries |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 32 | (but only with string keys), :class:`Data`, :class:`bytes`, :class:`bytesarray` |
| 33 | or :class:`datetime.datetime` objects. |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 34 | |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame^] | 35 | .. versionchanged:: 3.4 |
| 36 | New API, old API deprecated. Support for binary format plists added. |
| 37 | |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 38 | .. seealso:: |
| 39 | |
Ezio Melotti | 6e9b1df | 2009-09-16 00:49:03 +0000 | [diff] [blame] | 40 | `PList manual page <http://developer.apple.com/documentation/Darwin/Reference/ManPages/man5/plist.5.html>`_ |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 41 | Apple's documentation of the file format. |
| 42 | |
| 43 | |
| 44 | This module defines the following functions: |
| 45 | |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 46 | .. function:: load(fp, \*, fmt=None, use_builtin_types=True, dict_type=dict) |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 47 | |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 48 | Read a plist file. *fp* should be a readable and binary file object. |
| 49 | Return the unpacked root object (which usually is a |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 50 | dictionary). |
| 51 | |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 52 | 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 Storchaka | 0e90e99 | 2013-11-29 12:19:53 +0200 | [diff] [blame] | 60 | If *use_builtin_types* is true (the default) binary data will be returned |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 61 | 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 Oussoren | 6db6653 | 2014-01-15 11:32:35 +0100 | [diff] [blame] | 85 | .. versionadded:: 3.4 |
| 86 | |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 87 | |
| 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 Oussoren | 6db6653 | 2014-01-15 11:32:35 +0100 | [diff] [blame] | 110 | An :exc:`OverflowError` will be raised for integer values that cannot |
| 111 | be represented in (binary) plist files. |
| 112 | |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame^] | 113 | .. versionadded:: 3.4 |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 114 | |
| 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 Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame^] | 122 | .. versionadded:: 3.4 |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 123 | |
| 124 | The 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 Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame^] | 141 | .. deprecated:: 3.4 Use :func:`load` instead. |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 142 | |
| 143 | |
| 144 | .. function:: writePlist(rootObject, pathOrFile) |
| 145 | |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 146 | Write *rootObject* to an XML plist file. *pathOrFile* may be either a file name |
| 147 | or a (writable and binary) file object |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 148 | |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame^] | 149 | .. deprecated:: 3.4 Use :func:`dump` instead. |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 150 | |
| 151 | |
Ezio Melotti | 6e9b1df | 2009-09-16 00:49:03 +0000 | [diff] [blame] | 152 | .. function:: readPlistFromBytes(data) |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 153 | |
Ezio Melotti | 6e9b1df | 2009-09-16 00:49:03 +0000 | [diff] [blame] | 154 | Read a plist data from a bytes object. Return the root object. |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 155 | |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 156 | 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 Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 166 | |
Ezio Melotti | 6e9b1df | 2009-09-16 00:49:03 +0000 | [diff] [blame] | 167 | .. function:: writePlistToBytes(rootObject) |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 168 | |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 169 | Return *rootObject* as an XML plist-formatted bytes object. |
| 170 | |
| 171 | .. deprecated:: 3.4 Use :func:`dumps` instead. |
| 172 | |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 173 | |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 174 | The 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 Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 187 | |
| 188 | .. class:: Data(data) |
| 189 | |
Ezio Melotti | 6e9b1df | 2009-09-16 00:49:03 +0000 | [diff] [blame] | 190 | 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 Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 192 | available in plists. |
| 193 | |
| 194 | It has one attribute, :attr:`data`, that can be used to retrieve the Python |
Ezio Melotti | 6e9b1df | 2009-09-16 00:49:03 +0000 | [diff] [blame] | 195 | bytes object stored in it. |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 196 | |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 197 | .. deprecated:: 3.4 Use a :class:`bytes` object instead |
| 198 | |
| 199 | |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame^] | 200 | The following constants are available: |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 201 | |
| 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 Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 215 | |
| 216 | Examples |
| 217 | -------- |
| 218 | |
| 219 | Generating a plist:: |
| 220 | |
| 221 | pl = dict( |
Ezio Melotti | 985e24d | 2009-09-13 07:54:02 +0000 | [diff] [blame] | 222 | aString = "Doodah", |
| 223 | aList = ["A", "B", 12, 32.1, [1, 2, 3]], |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 224 | aFloat = 0.1, |
| 225 | anInt = 728, |
Ezio Melotti | 985e24d | 2009-09-13 07:54:02 +0000 | [diff] [blame] | 226 | aDict = dict( |
| 227 | anotherString = "<hello & hi there!>", |
| 228 | aThirdString = "M\xe4ssig, Ma\xdf", |
| 229 | aTrueValue = True, |
| 230 | aFalseValue = False, |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 231 | ), |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 232 | someData = b"<binary gunk>", |
| 233 | someMoreData = b"<lots of binary gunk>" * 10, |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 234 | aDate = datetime.datetime.fromtimestamp(time.mktime(time.gmtime())), |
| 235 | ) |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 236 | with open(fileName, 'wb') as fp: |
| 237 | dump(pl, fp) |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 238 | |
| 239 | Parsing a plist:: |
| 240 | |
Ronald Oussoren | c5cf797 | 2013-11-21 15:46:49 +0100 | [diff] [blame] | 241 | with open(fileName, 'rb') as fp: |
| 242 | pl = load(fp) |
Neal Norwitz | 752abd0 | 2008-05-13 04:55:24 +0000 | [diff] [blame] | 243 | print(pl["aKey"]) |