ordigi/elodie/plist_parser.py

45 lines
1.0 KiB
Python
Raw Normal View History

"""
Parse OS X plists.
2016-01-08 23:49:06 +01:00
.. moduleauthor:: Jaisen Mathai <jaisen@jmathai.com>
"""
# load modules
from os import path
import plistlib
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.
"""
def __init__(self, source):
if not path.isfile(source):
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.
"""
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
"""
plistlib.writePlist(self.plist, destination)