blob: cf3dd8241ef3283922730751cfb7566df386ea20 [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
14This module provides an interface for reading and writing the "property list"
Georg Brandlc575c902008-09-13 17:46:05 +000015XML files used mainly by Mac OS X.
Georg Brandl86def6c2008-01-21 20:36:10 +000016
17The property list (``.plist``) file format is a simple XML pickle supporting
18basic object types, like dictionaries, lists, numbers and strings. Usually the
19top level object is a dictionary.
20
21Values can be strings, integers, floats, booleans, tuples, lists, dictionaries
22(but only with string keys), :class:`Data` or :class:`datetime.datetime`
23objects. String values (including dictionary keys) may be unicode strings --
24they will be written out as UTF-8.
25
26The ``<data>`` plist type is supported through the :class:`Data` class. This is
27a thin wrapper around a Python string. Use :class:`Data` if your strings
28contain control characters.
29
30.. seealso::
31
32 `PList manual page <http://developer.apple.com/documentation/Darwin/Reference/ManPages/man5/plist.5.html>`
33 Apple's documentation of the file format.
34
35
36This module defines the following functions:
37
38.. function:: readPlist(pathOrFile)
39
40 Read a plist file. *pathOrFile* may either be a file name or a (readable)
41 file object. Return the unpacked root object (which usually is a
42 dictionary).
43
44 The XML data is parsed using the Expat parser from :mod:`xml.parsers.expat`
45 -- see its documentation for possible exceptions on ill-formed XML.
46 Unknown elements will simply be ignored by the plist parser.
47
48
49.. function:: writePlist(rootObject, pathOrFile)
50
51 Write *rootObject* to a plist file. *pathOrFile* may either be a file name
52 or a (writable) file object.
53
54 A :exc:`TypeError` will be raised if the object is of an unsupported type or
55 a container that contains objects of unsupported types.
56
57
58.. function:: readPlistFromString(data)
59
60 Read a plist from a string. Return the root object.
61
62
63.. function:: writePlistToString(rootObject)
64
65 Return *rootObject* as a plist-formatted string.
66
67
Georg Brandl86def6c2008-01-21 20:36:10 +000068The following class is available:
69
70.. class:: Data(data)
71
72 Return a "data" wrapper object around the string *data*. This is used in
73 functions converting from/to plists to represent the ``<data>`` type
74 available in plists.
75
76 It has one attribute, :attr:`data`, that can be used to retrieve the Python
77 string stored in it.
78
79
80Examples
81--------
82
83Generating a plist::
84
85 pl = dict(
86 aString="Doodah",
87 aList=["A", "B", 12, 32.1, [1, 2, 3]],
88 aFloat = 0.1,
89 anInt = 728,
90 aDict=dict(
91 anotherString="<hello & hi there!>",
92 aUnicodeValue=u'M\xe4ssig, Ma\xdf',
93 aTrueValue=True,
94 aFalseValue=False,
95 ),
96 someData = Data("<binary gunk>"),
97 someMoreData = Data("<lots of binary gunk>" * 10),
98 aDate = datetime.datetime.fromtimestamp(time.mktime(time.gmtime())),
99 )
100 # unicode keys are possible, but a little awkward to use:
101 pl[u'\xc5benraa'] = "That was a unicode key."
102 writePlist(pl, fileName)
103
104Parsing a plist::
105
106 pl = readPlist(pathOrFile)
Neal Norwitz752abd02008-05-13 04:55:24 +0000107 print(pl["aKey"])