summaryrefslogtreecommitdiffstats
path: root/Lib/plat-mac
diff options
context:
space:
mode:
authorJust van Rossum <just@letterror.com>2004-10-26 06:50:50 (GMT)
committerJust van Rossum <just@letterror.com>2004-10-26 06:50:50 (GMT)
commitc6fdd1b3988778afd4f83d1846f4a3f5c1150cf8 (patch)
treeae8b957ce850b898cf43b2050e77ae5fc91f2916 /Lib/plat-mac
parentdf8a0032a4f2b512509d5b79086c8a6b31d968ef (diff)
downloadcpython-c6fdd1b3988778afd4f83d1846f4a3f5c1150cf8.zip
cpython-c6fdd1b3988778afd4f83d1846f4a3f5c1150cf8.tar.gz
cpython-c6fdd1b3988778afd4f83d1846f4a3f5c1150cf8.tar.bz2
- added two more convenience functions: readPlistFromString() and
writePlistToString() - use these two in the resource functions. - Tweaked module doc string.
Diffstat (limited to 'Lib/plat-mac')
-rw-r--r--Lib/plat-mac/plistlib.py45
1 files changed, 30 insertions, 15 deletions
diff --git a/Lib/plat-mac/plistlib.py b/Lib/plat-mac/plistlib.py
index 23bc33f..d401d33 100644
--- a/Lib/plat-mac/plistlib.py
+++ b/Lib/plat-mac/plistlib.py
@@ -8,9 +8,12 @@ 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 (usually a dictionary).
+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 Date objects. String values (including dictionary
@@ -19,13 +22,13 @@ keys) may be unicode strings -- they will be written out as UTF-8.
This module exports a class named Dict(), which allows you to easily
construct (nested) dicts using keyword arguments as well as accessing
values with attribute notation, where d.foo is equivalent to d["foo"].
-Regular dictionaries work, too.
+Regular dictionaries work, too. Dictionaries are always represented with
+Dict instances when loading plist data.
The <data> plist type is supported through the Data class. This is a
thin wrapper around a Python string.
-The <date> plist data has (limited) support through the Date class.
-(Warning: Dates are only supported if the PyXML package is installed.)
+The <date> plist data has support through the Date class.
Generate Plist example:
@@ -58,13 +61,15 @@ Parse Plist example:
__all__ = [
- "readPlist", "writePlist",
+ "readPlist", "writePlist", "readPlistFromString", "writePlistToString",
"readPlistFromResource", "writePlistToResource",
"Plist", "Data", "Date", "Dict"
]
# Note: the Plist class has been deprecated.
-import base64, datetime
+import base64
+import datetime
+from cStringIO import StringIO
def readPlist(pathOrFile):
@@ -99,19 +104,32 @@ def writePlist(rootObject, pathOrFile):
pathOrFile.close()
+def readPlistFromString(data):
+ """Read a plist data from a string. Return the root object.
+ """
+ return readPlist(StringIO(data))
+
+
+def writePlistToString(rootObject):
+ """Return 'rootObject' as a plist-formatted string.
+ """
+ f = StringIO()
+ writePlist(rootObject, f)
+ return f.getvalue()
+
+
def readPlistFromResource(path, restype='plst', resid=0):
"""Read plst resource from the resource fork of path.
"""
from Carbon.File import FSRef, FSGetResourceForkName
from Carbon.Files import fsRdPerm
from Carbon import Res
- from cStringIO import StringIO
fsRef = FSRef(path)
resNum = Res.FSOpenResourceFile(fsRef, FSGetResourceForkName(), fsRdPerm)
Res.UseResFile(resNum)
- plistData = StringIO(Res.Get1Resource(restype, resid).data)
+ plistData = Res.Get1Resource(restype, resid).data
Res.CloseResFile(resNum)
- return readPlist(plistData)
+ return readPlistFromString(plistData)
def writePlistToResource(rootObject, path, restype='plst', resid=0):
@@ -120,10 +138,7 @@ def writePlistToResource(rootObject, path, restype='plst', resid=0):
from Carbon.File import FSRef, FSGetResourceForkName
from Carbon.Files import fsRdWrPerm
from Carbon import Res
- from cStringIO import StringIO
- plistData = StringIO()
- writePlist(rootObject, plistData)
- plistData = plistData.getvalue()
+ plistData = writePlistToString(rootObject)
fsRef = FSRef(path)
resNum = Res.FSOpenResourceFile(fsRef, FSGetResourceForkName(), fsRdWrPerm)
Res.UseResFile(resNum)