Skip to content

Commit

Permalink
Fix for FoundationPlist on 10.6.x
Browse files Browse the repository at this point in the history
  • Loading branch information
gregneagle committed Jan 6, 2014
1 parent 1978b49 commit 90b7a21
Showing 1 changed file with 63 additions and 45 deletions.
108 changes: 63 additions & 45 deletions Code/FoundationPlist/FoundationPlist.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/python
# encoding: utf-8
#
# Copyright 2009-2013 Greg Neagle.
# Copyright 2009-2014 Greg Neagle.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -39,77 +39,95 @@
To work with plist data in strings, you can use readPlistFromString()
and writePlistToString().
This version works only with OS X 10.6+., as we are now using the newer
dataWithPropertyList:format:options:error: and
propertyListWithData:options:format:error: NSPropertyListSerialization
class methods.
"""

import os

from Foundation import NSData, \
NSPropertyListSerialization, \
NSPropertyListMutableContainersAndLeaves, \
NSPropertyListXMLFormat_v1_0


class FoundationPlistException(Exception):
'''Base error for this module'''
pass


class NSPropertyListSerializationException(FoundationPlistException):
'''Read error for this module'''
pass


class NSPropertyListWriteException(FoundationPlistException):
'''Write error for this module'''
pass

def readPlist(filepath):
'''Read a .plist file from filepath. Return the unpacked root object
(which is usually a dictionary).'''
plistData = NSData.dataWithContentsOfFile_(filepath)
(dataObject, plistFormat, error) = (
NSPropertyListSerialization.propertyListWithData_options_format_error_(
plistData, NSPropertyListMutableContainersAndLeaves, None, None))

# private functions
def _dataToPlist(data):
'''low-level function that parses a data object into a propertyList object'''
darwin_vers = int(os.uname()[2].split('.')[0])
if darwin_vers > 10:
(plistObject, plistFormat, error) = (
NSPropertyListSerialization.propertyListWithData_options_format_error_(
data, NSPropertyListMutableContainersAndLeaves, None, None))
else:
# 10.5 doesn't support propertyListWithData:options:format:error:
# 10.6's PyObjC wrapper for propertyListWithData:options:format:error:
# is broken
# so use the older NSPropertyListSerialization function
(plistObject, plistFormat, error) = (
NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_(
data, NSPropertyListMutableContainersAndLeaves, None, None))
if error:
errmsg = u"%s in file %s" % (error, filepath)
raise NSPropertyListSerializationException(errmsg)
else:
return dataObject
return plistObject


def readPlistFromString(data):
'''Read a plist data from a string. Return the root object.'''
plistData = buffer(data)
(dataObject, plistFormat, error) = (
NSPropertyListSerialization.propertyListWithData_options_format_error_(
plistData, NSPropertyListMutableContainersAndLeaves, None, None))
def _plistToData(plistObject):
'''low-level function that creates NSData from a plist object'''
darwin_vers = int(os.uname()[2].split('.')[0])
if darwin_vers > 10:
(data, error) = (
NSPropertyListSerialization.dataWithPropertyList_format_options_error_(
plistObject, NSPropertyListXMLFormat_v1_0, 0, None))
else:
# use the older NSPropertyListSerialization function on 10.6 and 10.5
(data, error) = (
NSPropertyListSerialization.dataFromPropertyList_format_errorDescription_(
plistObject, NSPropertyListXMLFormat_v1_0, None))
if error:
raise NSPropertyListSerializationException(error)
else:
return dataObject
return data


def writePlist(dataObject, filepath):
'''Write 'rootObject' as a plist to filepath.'''
(plistData, error) = (
NSPropertyListSerialization.dataWithPropertyList_format_options_error_(
dataObject, NSPropertyListXMLFormat_v1_0, 0, None))
if error:
raise NSPropertyListSerializationException(error)
else:
if plistData.writeToFile_atomically_(filepath, True):
return
else:
raise NSPropertyListWriteException(
u"Failed to write plist data to %s" % filepath)


def writePlistToString(rootObject):
'''Return 'rootObject' as a plist-formatted string.'''
(plistData, error) = (
NSPropertyListSerialization.dataWithPropertyList_format_options_error_(
rootObject, NSPropertyListXMLFormat_v1_0, 0, None))
if error:
raise NSPropertyListSerializationException(error)
# public functions
def readPlist(filepath):
'''Read a .plist file from filepath. Return the unpacked root object
(which is usually a dictionary).'''
data = NSData.dataWithContentsOfFile_(filepath)
return _dataToPlist(data)


def readPlistFromString(aString):
'''Read a plist data from a string. Return the root object.'''
data = buffer(aString)
return _dataToPlist(data)


def writePlist(plistObject, filepath):
'''Write 'plistObject' as a plist to filepath.'''
plistData = _plistToData(plistObject)
if plistData.writeToFile_atomically_(filepath, True):
return
else:
return str(plistData)
raise NSPropertyListWriteException(
u"Failed to write plist data to %s" % filepath)


def writePlistToString(plistObject):
'''Create a plist-formatted string from plistObject.'''
return str(_plistToData(plistObject))

0 comments on commit 90b7a21

Please sign in to comment.