blob: ae5e94d1a909051fd74b210c77c4f3a67a0088f7 [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"
Georg Brandlc575c902008-09-13 17:46:05 +000019XML files used mainly by Mac OS X.
Georg Brandl86def6c2008-01-21 20:36:10 +000020
21The property list (``.plist``) file format is a simple XML pickle supporting
22basic object types, like dictionaries, lists, numbers and strings. Usually the
23top level object is a dictionary.
24
Ezio Melotti6e9b1df2009-09-16 00:49:03 +000025To write out and to parse a plist file, use the :func:`writePlist` and
26:func:`readPlist` functions.
27
28To work with plist data in bytes objects, use :func:`writePlistToBytes`
29and :func:`readPlistFromBytes`.
30
Georg Brandl86def6c2008-01-21 20:36:10 +000031Values can be strings, integers, floats, booleans, tuples, lists, dictionaries
32(but only with string keys), :class:`Data` or :class:`datetime.datetime`
Ezio Melottidc2b8ec2009-09-13 08:01:06 +000033objects. String values (including dictionary keys) have to be unicode strings --
Georg Brandl86def6c2008-01-21 20:36:10 +000034they will be written out as UTF-8.
35
36The ``<data>`` plist type is supported through the :class:`Data` class. This is
Ezio Melotti6e9b1df2009-09-16 00:49:03 +000037a thin wrapper around a Python bytes object. Use :class:`Data` if your strings
Georg Brandl86def6c2008-01-21 20:36:10 +000038contain control characters.
39
40.. seealso::
41
Ezio Melotti6e9b1df2009-09-16 00:49:03 +000042 `PList manual page <http://developer.apple.com/documentation/Darwin/Reference/ManPages/man5/plist.5.html>`_
Georg Brandl86def6c2008-01-21 20:36:10 +000043 Apple's documentation of the file format.
44
45
46This module defines the following functions:
47
48.. function:: readPlist(pathOrFile)
49
50 Read a plist file. *pathOrFile* may either be a file name or a (readable)
51 file object. Return the unpacked root object (which usually is a
52 dictionary).
53
54 The XML data is parsed using the Expat parser from :mod:`xml.parsers.expat`
55 -- see its documentation for possible exceptions on ill-formed XML.
56 Unknown elements will simply be ignored by the plist parser.
57
58
59.. function:: writePlist(rootObject, pathOrFile)
60
61 Write *rootObject* to a plist file. *pathOrFile* may either be a file name
62 or a (writable) file object.
63
64 A :exc:`TypeError` will be raised if the object is of an unsupported type or
65 a container that contains objects of unsupported types.
66
67
Ezio Melotti6e9b1df2009-09-16 00:49:03 +000068.. function:: readPlistFromBytes(data)
Georg Brandl86def6c2008-01-21 20:36:10 +000069
Ezio Melotti6e9b1df2009-09-16 00:49:03 +000070 Read a plist data from a bytes object. Return the root object.
Georg Brandl86def6c2008-01-21 20:36:10 +000071
72
Ezio Melotti6e9b1df2009-09-16 00:49:03 +000073.. function:: writePlistToBytes(rootObject)
Georg Brandl86def6c2008-01-21 20:36:10 +000074
Ezio Melotti6e9b1df2009-09-16 00:49:03 +000075 Return *rootObject* as a plist-formatted bytes object.
Georg Brandl86def6c2008-01-21 20:36:10 +000076
77
Georg Brandl86def6c2008-01-21 20:36:10 +000078The following class is available:
79
80.. class:: Data(data)
81
Ezio Melotti6e9b1df2009-09-16 00:49:03 +000082 Return a "data" wrapper object around the bytes object *data*. This is used
83 in functions converting from/to plists to represent the ``<data>`` type
Georg Brandl86def6c2008-01-21 20:36:10 +000084 available in plists.
85
86 It has one attribute, :attr:`data`, that can be used to retrieve the Python
Ezio Melotti6e9b1df2009-09-16 00:49:03 +000087 bytes object stored in it.
Georg Brandl86def6c2008-01-21 20:36:10 +000088
89
90Examples
91--------
92
93Generating a plist::
94
95 pl = dict(
Ezio Melotti985e24d2009-09-13 07:54:02 +000096 aString = "Doodah",
97 aList = ["A", "B", 12, 32.1, [1, 2, 3]],
Georg Brandl86def6c2008-01-21 20:36:10 +000098 aFloat = 0.1,
99 anInt = 728,
Ezio Melotti985e24d2009-09-13 07:54:02 +0000100 aDict = dict(
101 anotherString = "<hello & hi there!>",
102 aThirdString = "M\xe4ssig, Ma\xdf",
103 aTrueValue = True,
104 aFalseValue = False,
Georg Brandl86def6c2008-01-21 20:36:10 +0000105 ),
Ezio Melotti6e9b1df2009-09-16 00:49:03 +0000106 someData = Data(b"<binary gunk>"),
107 someMoreData = Data(b"<lots of binary gunk>" * 10),
Georg Brandl86def6c2008-01-21 20:36:10 +0000108 aDate = datetime.datetime.fromtimestamp(time.mktime(time.gmtime())),
109 )
Georg Brandl86def6c2008-01-21 20:36:10 +0000110 writePlist(pl, fileName)
111
112Parsing a plist::
113
114 pl = readPlist(pathOrFile)
Neal Norwitz752abd02008-05-13 04:55:24 +0000115 print(pl["aKey"])