Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import sys
import plistlib
import datetime
class CCBReal:
def __init__(self, value):
self.value_raw = value
self.value = float(value)
def float(self):
return self.value
def raw(self):
return self.value_raw
class CCBParser(plistlib.PlistParser):
def end_real(self):
value = self.getData()
self.addObject(CCBReal(value))
class CCBWriter(plistlib.PlistWriter):
def writeValue(self, value):
if isinstance(value, CCBReal):
self.simpleElement("real", value.raw())
else:
plistlib.PlistWriter.writeValue(self, value)
def writeArray(self, array):
if len(array) <= 0:
self.writeln("<array/>")
else:
plistlib.PlistWriter.writeArray(self, array)
def writeDict(self, d):
if len(d) <= 0:
self.writeln("<dict/>")
else:
plistlib.PlistWriter.writeDict(self, d)
def 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).
"""
didOpen = 0
if isinstance(pathOrFile, (str, unicode)):
pathOrFile = open(pathOrFile)
didOpen = 1
p = CCBParser()
rootObject = p.parse(pathOrFile)
if didOpen:
pathOrFile.close()
return rootObject
def writePlist(rootObject, pathOrFile):
"""Write 'rootObject' to a .plist file. 'pathOrFile' may either be a
file name or a (writable) file object.
"""
didOpen = 0
if isinstance(pathOrFile, (str, unicode)):
pathOrFile = open(pathOrFile, "w")
didOpen = 1
writer = CCBWriter(pathOrFile)
writer.writeln("<plist version=\"1.0\">")
writer.writeValue(rootObject)
writer.writeln("</plist>")
if didOpen:
pathOrFile.close()
if __name__ == "__main__":
ccb = "/path/of/you/file.ccb"
if len(sys.argv) >= 2:
ccb = sys.argv[1]
plist = readPlist(ccb)
print plist.guides[0].position
print plist.guides[0].position.float()
plist.guides[0].position = 5.5
print plist.guides[0].position
writePlist(plist, ccb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment