2015-10-22 03:40:50 +02:00
|
|
|
"""
|
|
|
|
Parse OS X plists.
|
2016-01-08 23:49:06 +01:00
|
|
|
|
|
|
|
.. moduleauthor:: Jaisen Mathai <jaisen@jmathai.com>
|
2015-10-22 03:40:50 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
# load modules
|
|
|
|
from os import path
|
|
|
|
|
|
|
|
import plistlib
|
|
|
|
|
2016-01-02 08:23:06 +01:00
|
|
|
|
2015-10-22 03:40:50 +02:00
|
|
|
class Plist(object):
|
2016-01-08 23:49:06 +01:00
|
|
|
|
|
|
|
"""Parse and interact with a plist file.
|
|
|
|
|
|
|
|
This class wraps the `plistlib module`_ from the standard library.
|
|
|
|
|
|
|
|
.. _plistlib module: https://docs.python.org/3/library/plistlib.html
|
|
|
|
|
|
|
|
:param str source: Source to read the plist from.
|
|
|
|
"""
|
|
|
|
|
2015-10-22 03:40:50 +02:00
|
|
|
def __init__(self, source):
|
2016-01-02 18:34:43 +01:00
|
|
|
if not path.isfile(source):
|
2015-10-22 03:40:50 +02:00
|
|
|
raise IOError('Could not load plist file %s' % source)
|
|
|
|
self.source = source
|
|
|
|
self.plist = plistlib.readPlist(self.source)
|
|
|
|
|
|
|
|
def update_key(self, key, value):
|
2016-01-08 23:49:06 +01:00
|
|
|
"""Update a value in the plist.
|
|
|
|
|
|
|
|
:param str key: Key to modify.
|
|
|
|
:param value: New value.
|
|
|
|
"""
|
2015-10-22 03:40:50 +02:00
|
|
|
self.plist[key] = value
|
|
|
|
|
|
|
|
def write_file(self, destination):
|
2016-01-08 23:49:06 +01:00
|
|
|
"""Save the plist.
|
|
|
|
|
|
|
|
:param destination: Write the plist here.
|
|
|
|
:type destination: str or file object
|
|
|
|
"""
|
2015-10-22 03:40:50 +02:00
|
|
|
plistlib.writePlist(self.plist, destination)
|