plistlib

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

The PropertList (.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’Mässig, Maß’, aTrueValue=True, aFalseValue=False,

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

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

Parse Plist example:

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

Read a .plist file. ‘pathOrFile’ may either be a file name or a (readable) file object. Return the unpacked root object (which usually is a dictionary).

writePlist(rootObject, pathOrFile)

Write ‘rootObject’ to a .plist file. ‘pathOrFile’ may either be a file name or a (writable) file object.

readPlistFromString(data)

Read a plist data from a string. Return the root object.

writePlistToString(rootObject)

Return ‘rootObject’ as a plist-formatted string.

readPlistFromResource(path, restype='plst', resid=0)

Read plst resource from the resource fork of path.

writePlistToResource(rootObject, path, restype='plst', resid=0)

Write ‘rootObject’ as a plst resource to the resource fork of path.

class Plist(**kwargs)

This class has been deprecated. Use readPlist() and writePlist() functions instead, together with regular dict objects.

classmethod fromFile(pathOrFile)

Deprecated. Use the readPlist() function instead.

write(pathOrFile)

Deprecated. Use the writePlist() function instead.

class Data(data)

Wrapper for binary data.

links