plistlib

plistlib.py – a tool to generate and parse MacOSX .plist files.

The PropertyList (.plist) file format is a simple XML pickle supporting basic object types, like dictionaries, lists, numbers and strings. Usually the top level object is a dictionary.

To write out a plist file, use the writePlist(rootObject, pathOrFile) function. ‘rootObject’ is the top level object, ‘pathOrFile’ is a filename or a (writable) file object.

To parse a plist from a file, use the readPlist(pathOrFile) function, with a file name or a (readable) file object as the only argument. It returns the top level object (again, usually a dictionary).

To work with plist data in strings, you can use readPlistFromString() and writePlistToString().

Values can be strings, integers, floats, booleans, tuples, lists, dictionaries, Data or datetime.datetime objects. String values (including dictionary keys) may be unicode strings – they will be written out as UTF-8.

The <data> plist type is supported through the Data class. This is a thin wrapper around a Python string.

Generate Plist example:

pl = dict(

aString=”Doodah”, aList=[“A”, “B”, 12, 32.1, [1, 2, 3]], aFloat=0.1, anInt=728, aDict=dict(

anotherString=”<hello & hi there!>”, aUnicodeValue=u’Mxe4ssig, Maxdf’, aTrueValue=True, aFalseValue=False,

), someData=Data(“<binary gunk>”), someMoreData=Data(“<lots of binary gunk>” * 10), aDate=datetime.datetime.fromtimestamp(time.mktime(time.gmtime())),

) # unicode keys are possible, but a little awkward to use: pl[u’xc5benraa’] = “That was a unicode key.” writePlist(pl, fileName)

Parse Plist example:

pl = readPlist(pathOrFile) print pl[“aKey”]

Functions

StringIO StringIO([s]) – Return a StringIO-like stream for reading or writing
readPlist(pathOrFile) Read a .plist file.
readPlistFromResource(path[, restype, resid]) Read plst resource from the resource fork of path.
readPlistFromString(data) Read a plist data from a string.
writePlist(rootObject, pathOrFile) Write ‘rootObject’ to a .plist file.
writePlistToResource(rootObject, path[, ...]) Write ‘rootObject’ as a plst resource to the resource fork of path.
writePlistToString(rootObject) Return ‘rootObject’ as a plist-formatted string.

Classes

Data(data) Wrapper for binary data.
Dict(**kwargs)
DumbXMLWriter(file[, indentLevel, indent])
Plist(**kwargs) This class has been deprecated.
PlistParser()
PlistWriter(file[, indentLevel, indent, ...])