| 
									
										
										
										
											2002-11-19 22:01:02 +00:00
										 |  |  | """plistlib.py -- a tool to generate and parse MacOSX .plist files.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-02 08:40:47 +00:00
										 |  |  | The PropertList (.plist) file format is a simple XML pickle supporting | 
					
						
							|  |  |  | basic object types, like dictionaries, lists, numbers and strings. | 
					
						
							|  |  |  | Usually the top level object is a dictionary. | 
					
						
							| 
									
										
										
										
											2002-11-19 22:01:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-02 08:40:47 +00:00
										 |  |  | 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. | 
					
						
							| 
									
										
										
										
											2002-11-19 22:01:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-26 06:50:50 +00:00
										 |  |  | 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(). | 
					
						
							| 
									
										
										
										
											2002-11-19 22:01:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | Values can be strings, integers, floats, booleans, tuples, lists, | 
					
						
							| 
									
										
										
										
											2004-10-26 07:20:26 +00:00
										 |  |  | dictionaries, Data or datetime.datetime objects. String values (including | 
					
						
							|  |  |  | dictionary keys) may be unicode strings -- they will be written out as | 
					
						
							|  |  |  | UTF-8. | 
					
						
							| 
									
										
										
										
											2002-11-19 22:01:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | The <data> plist type is supported through the Data class. This is a | 
					
						
							|  |  |  | thin wrapper around a Python string. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Generate Plist example: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-26 07:38:16 +00:00
										 |  |  |     pl = dict( | 
					
						
							| 
									
										
										
										
											2003-04-09 13:25:43 +00:00
										 |  |  |         aString="Doodah", | 
					
						
							|  |  |  |         aList=["A", "B", 12, 32.1, [1, 2, 3]], | 
					
						
							|  |  |  |         aFloat = 0.1, | 
					
						
							|  |  |  |         anInt = 728, | 
					
						
							| 
									
										
										
										
											2004-10-26 07:38:16 +00:00
										 |  |  |         aDict=dict( | 
					
						
							| 
									
										
										
										
											2003-04-09 13:25:43 +00:00
										 |  |  |             anotherString="<hello & hi there!>", | 
					
						
							|  |  |  |             aUnicodeValue=u'M\xe4ssig, Ma\xdf', | 
					
						
							|  |  |  |             aTrueValue=True, | 
					
						
							|  |  |  |             aFalseValue=False, | 
					
						
							|  |  |  |         ), | 
					
						
							|  |  |  |         someData = Data("<binary gunk>"), | 
					
						
							|  |  |  |         someMoreData = Data("<lots of binary gunk>" * 10), | 
					
						
							| 
									
										
										
										
											2004-10-26 07:20:26 +00:00
										 |  |  |         aDate = datetime.datetime.fromtimestamp(time.mktime(time.gmtime())), | 
					
						
							| 
									
										
										
										
											2003-04-09 13:25:43 +00:00
										 |  |  |     ) | 
					
						
							|  |  |  |     # unicode keys are possible, but a little awkward to use: | 
					
						
							|  |  |  |     pl[u'\xc5benraa'] = "That was a unicode key." | 
					
						
							| 
									
										
										
										
											2004-10-02 08:40:47 +00:00
										 |  |  |     writePlist(pl, fileName) | 
					
						
							| 
									
										
										
										
											2002-11-19 22:01:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | Parse Plist example: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-02 08:40:47 +00:00
										 |  |  |     pl = readPlist(pathOrFile) | 
					
						
							| 
									
										
										
										
											2004-10-26 07:38:16 +00:00
										 |  |  |     print pl["aKey"] | 
					
						
							| 
									
										
										
										
											2002-11-19 22:01:02 +00:00
										 |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-25 15:10:42 +00:00
										 |  |  | __all__ = [ | 
					
						
							| 
									
										
										
										
											2004-10-26 06:50:50 +00:00
										 |  |  |     "readPlist", "writePlist", "readPlistFromString", "writePlistToString", | 
					
						
							| 
									
										
										
										
											2004-10-25 15:10:42 +00:00
										 |  |  |     "readPlistFromResource", "writePlistToResource", | 
					
						
							| 
									
										
										
										
											2004-10-26 07:20:26 +00:00
										 |  |  |     "Plist", "Data", "Dict" | 
					
						
							| 
									
										
										
										
											2004-10-25 15:10:42 +00:00
										 |  |  | ] | 
					
						
							| 
									
										
										
										
											2004-10-26 07:38:16 +00:00
										 |  |  | # Note: the Plist and Dict classes have been deprecated. | 
					
						
							| 
									
										
										
										
											2002-11-19 22:01:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-26 10:30:55 +00:00
										 |  |  | import binascii | 
					
						
							| 
									
										
										
										
											2004-10-26 06:50:50 +00:00
										 |  |  | import datetime | 
					
						
							|  |  |  | from cStringIO import StringIO | 
					
						
							| 
									
										
										
										
											2004-10-25 15:10:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-19 22:01:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-02 08:40:47 +00:00
										 |  |  | 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 = PlistParser() | 
					
						
							|  |  |  |     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 = PlistWriter(pathOrFile) | 
					
						
							|  |  |  |     writer.writeln("<plist version=\"1.0\">") | 
					
						
							|  |  |  |     writer.writeValue(rootObject) | 
					
						
							|  |  |  |     writer.writeln("</plist>") | 
					
						
							|  |  |  |     if didOpen: | 
					
						
							|  |  |  |         pathOrFile.close() | 
					
						
							| 
									
										
										
										
											2002-11-19 22:01:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-26 06:50:50 +00:00
										 |  |  | 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() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-25 15:10:42 +00:00
										 |  |  | 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 | 
					
						
							|  |  |  |     fsRef = FSRef(path) | 
					
						
							|  |  |  |     resNum = Res.FSOpenResourceFile(fsRef, FSGetResourceForkName(), fsRdPerm) | 
					
						
							|  |  |  |     Res.UseResFile(resNum) | 
					
						
							| 
									
										
										
										
											2004-10-26 06:50:50 +00:00
										 |  |  |     plistData = Res.Get1Resource(restype, resid).data | 
					
						
							| 
									
										
										
										
											2004-10-25 15:10:42 +00:00
										 |  |  |     Res.CloseResFile(resNum) | 
					
						
							| 
									
										
										
										
											2004-10-26 06:50:50 +00:00
										 |  |  |     return readPlistFromString(plistData) | 
					
						
							| 
									
										
										
										
											2004-10-25 15:10:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def writePlistToResource(rootObject, path, restype='plst', resid=0): | 
					
						
							|  |  |  |     """Write 'rootObject' as a plst resource to the resource fork of path.
 | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     from Carbon.File import FSRef, FSGetResourceForkName | 
					
						
							|  |  |  |     from Carbon.Files import fsRdWrPerm | 
					
						
							|  |  |  |     from Carbon import Res | 
					
						
							| 
									
										
										
										
											2004-10-26 06:50:50 +00:00
										 |  |  |     plistData = writePlistToString(rootObject) | 
					
						
							| 
									
										
										
										
											2004-10-25 15:10:42 +00:00
										 |  |  |     fsRef = FSRef(path) | 
					
						
							|  |  |  |     resNum = Res.FSOpenResourceFile(fsRef, FSGetResourceForkName(), fsRdWrPerm) | 
					
						
							|  |  |  |     Res.UseResFile(resNum) | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         Res.Get1Resource(restype, resid).RemoveResource() | 
					
						
							|  |  |  |     except Res.Error: | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  |     res = Res.Resource(plistData) | 
					
						
							|  |  |  |     res.AddResource(restype, resid, '') | 
					
						
							|  |  |  |     res.WriteResource() | 
					
						
							|  |  |  |     Res.CloseResFile(resNum) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-19 22:01:02 +00:00
										 |  |  | class DumbXMLWriter: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-02 08:40:47 +00:00
										 |  |  |     def __init__(self, file, indentLevel=0, indent="\t"): | 
					
						
							| 
									
										
										
										
											2003-04-09 13:25:43 +00:00
										 |  |  |         self.file = file | 
					
						
							|  |  |  |         self.stack = [] | 
					
						
							| 
									
										
										
										
											2004-10-02 08:40:47 +00:00
										 |  |  |         self.indentLevel = indentLevel | 
					
						
							|  |  |  |         self.indent = indent | 
					
						
							| 
									
										
										
										
											2002-11-19 22:01:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-09 13:25:43 +00:00
										 |  |  |     def beginElement(self, element): | 
					
						
							|  |  |  |         self.stack.append(element) | 
					
						
							|  |  |  |         self.writeln("<%s>" % element) | 
					
						
							|  |  |  |         self.indentLevel += 1 | 
					
						
							| 
									
										
										
										
											2002-11-19 22:01:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-09 13:25:43 +00:00
										 |  |  |     def endElement(self, element): | 
					
						
							|  |  |  |         assert self.indentLevel > 0 | 
					
						
							|  |  |  |         assert self.stack.pop() == element | 
					
						
							|  |  |  |         self.indentLevel -= 1 | 
					
						
							|  |  |  |         self.writeln("</%s>" % element) | 
					
						
							| 
									
										
										
										
											2002-11-19 22:01:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-09 13:25:43 +00:00
										 |  |  |     def simpleElement(self, element, value=None): | 
					
						
							| 
									
										
										
										
											2003-07-10 14:26:06 +00:00
										 |  |  |         if value is not None: | 
					
						
							| 
									
										
										
										
											2004-10-02 08:40:47 +00:00
										 |  |  |             value = _escapeAndEncode(value) | 
					
						
							| 
									
										
										
										
											2003-04-09 13:25:43 +00:00
										 |  |  |             self.writeln("<%s>%s</%s>" % (element, value, element)) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.writeln("<%s/>" % element) | 
					
						
							| 
									
										
										
										
											2002-11-19 22:01:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-09 13:25:43 +00:00
										 |  |  |     def writeln(self, line): | 
					
						
							|  |  |  |         if line: | 
					
						
							| 
									
										
										
										
											2004-10-02 08:40:47 +00:00
										 |  |  |             self.file.write(self.indentLevel * self.indent + line + "\n") | 
					
						
							| 
									
										
										
										
											2003-04-09 13:25:43 +00:00
										 |  |  |         else: | 
					
						
							|  |  |  |             self.file.write("\n") | 
					
						
							| 
									
										
										
										
											2002-11-19 22:01:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-02 08:40:47 +00:00
										 |  |  | import re | 
					
						
							|  |  |  | # Regex to strip all control chars, but for \t \n \r and \f | 
					
						
							|  |  |  | _controlStripper = re.compile(r"[\x00\x01\x02\x03\x04\x05\x06\x07\x08\x0b\x0e\x0f" | 
					
						
							|  |  |  |     "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f]") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-25 15:10:42 +00:00
										 |  |  | # Contents should conform to a subset of ISO 8601 | 
					
						
							|  |  |  | # (in particular, YYYY '-' MM '-' DD 'T' HH ':' MM ':' SS 'Z'.  Smaller units may be omitted with | 
					
						
							|  |  |  | #  a loss of precision) | 
					
						
							|  |  |  | _dateParser = re.compile(r"(?P<year>\d\d\d\d)(?:-(?P<month>\d\d)(?:-(?P<day>\d\d)(?:T(?P<hour>\d\d)(?::(?P<minute>\d\d)(?::(?P<second>\d\d))?)?)?)?)?Z") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-26 07:20:26 +00:00
										 |  |  | def _dateFromString(s): | 
					
						
							|  |  |  |     order = ('year', 'month', 'day', 'hour', 'minute', 'second') | 
					
						
							|  |  |  |     gd = _dateParser.match(s).groupdict() | 
					
						
							|  |  |  |     lst = [] | 
					
						
							|  |  |  |     for key in order: | 
					
						
							|  |  |  |         val = gd[key] | 
					
						
							|  |  |  |         if val is None: | 
					
						
							|  |  |  |             break | 
					
						
							|  |  |  |         lst.append(int(val)) | 
					
						
							|  |  |  |     return datetime.datetime(*lst) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def _dateToString(d): | 
					
						
							|  |  |  |     return '%04d-%02d-%02dT%02d:%02d:%02dZ' % ( | 
					
						
							|  |  |  |         d.year, d.month, d.day, | 
					
						
							|  |  |  |         d.hour, d.minute, d.second | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-02 08:40:47 +00:00
										 |  |  | def _escapeAndEncode(text): | 
					
						
							|  |  |  |     text = text.replace("\r\n", "\n")       # convert DOS line endings | 
					
						
							|  |  |  |     text = text.replace("\r", "\n")         # convert Mac line endings | 
					
						
							|  |  |  |     text = text.replace("&", "&")       # escape '&' | 
					
						
							|  |  |  |     text = text.replace("<", "<")        # escape '<' | 
					
						
							| 
									
										
										
										
											2004-10-26 10:11:00 +00:00
										 |  |  |     text = text.replace(">", ">")        # escape '>' | 
					
						
							| 
									
										
										
										
											2004-10-02 08:40:47 +00:00
										 |  |  |     text = _controlStripper.sub("?", text)  # replace control chars with '?' | 
					
						
							|  |  |  |     return text.encode("utf-8")             # encode as UTF-8 | 
					
						
							| 
									
										
										
										
											2002-11-19 22:01:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PLISTHEADER = """\
 | 
					
						
							|  |  |  | <?xml version="1.0" encoding="UTF-8"?> | 
					
						
							|  |  |  | <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class PlistWriter(DumbXMLWriter): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-02 08:40:47 +00:00
										 |  |  |     def __init__(self, file, indentLevel=0, indent="\t", writeHeader=1): | 
					
						
							|  |  |  |         if writeHeader: | 
					
						
							|  |  |  |             file.write(PLISTHEADER) | 
					
						
							|  |  |  |         DumbXMLWriter.__init__(self, file, indentLevel, indent) | 
					
						
							| 
									
										
										
										
											2003-04-09 13:25:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def writeValue(self, value): | 
					
						
							|  |  |  |         if isinstance(value, (str, unicode)): | 
					
						
							|  |  |  |             self.simpleElement("string", value) | 
					
						
							|  |  |  |         elif isinstance(value, bool): | 
					
						
							|  |  |  |             # must switch for bool before int, as bool is a | 
					
						
							|  |  |  |             # subclass of int... | 
					
						
							|  |  |  |             if value: | 
					
						
							|  |  |  |                 self.simpleElement("true") | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 self.simpleElement("false") | 
					
						
							|  |  |  |         elif isinstance(value, int): | 
					
						
							|  |  |  |             self.simpleElement("integer", str(value)) | 
					
						
							|  |  |  |         elif isinstance(value, float): | 
					
						
							| 
									
										
										
										
											2004-10-25 15:10:42 +00:00
										 |  |  |             self.simpleElement("real", repr(value)) | 
					
						
							| 
									
										
										
										
											2004-10-02 08:40:47 +00:00
										 |  |  |         elif isinstance(value, dict): | 
					
						
							| 
									
										
										
										
											2003-04-09 13:25:43 +00:00
										 |  |  |             self.writeDict(value) | 
					
						
							|  |  |  |         elif isinstance(value, Data): | 
					
						
							|  |  |  |             self.writeData(value) | 
					
						
							| 
									
										
										
										
											2004-10-26 07:20:26 +00:00
										 |  |  |         elif isinstance(value, datetime.datetime): | 
					
						
							|  |  |  |             self.simpleElement("date", _dateToString(value)) | 
					
						
							| 
									
										
										
										
											2003-04-09 13:25:43 +00:00
										 |  |  |         elif isinstance(value, (tuple, list)): | 
					
						
							|  |  |  |             self.writeArray(value) | 
					
						
							|  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2003-07-01 20:15:38 +00:00
										 |  |  |             raise TypeError("unsuported type: %s" % type(value)) | 
					
						
							| 
									
										
										
										
											2003-04-09 13:25:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def writeData(self, data): | 
					
						
							|  |  |  |         self.beginElement("data") | 
					
						
							| 
									
										
										
										
											2004-10-26 10:30:55 +00:00
										 |  |  |         self.indentLevel -= 1 | 
					
						
							|  |  |  |         maxlinelength = 76 - len(self.indent.replace("\t", " " * 8) * | 
					
						
							|  |  |  |                                  self.indentLevel) | 
					
						
							|  |  |  |         for line in data.asBase64(maxlinelength).split("\n"): | 
					
						
							| 
									
										
										
										
											2003-04-09 13:25:43 +00:00
										 |  |  |             if line: | 
					
						
							|  |  |  |                 self.writeln(line) | 
					
						
							| 
									
										
										
										
											2004-10-26 10:30:55 +00:00
										 |  |  |         self.indentLevel += 1 | 
					
						
							| 
									
										
										
										
											2003-04-09 13:25:43 +00:00
										 |  |  |         self.endElement("data") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def writeDict(self, d): | 
					
						
							|  |  |  |         self.beginElement("dict") | 
					
						
							|  |  |  |         items = d.items() | 
					
						
							|  |  |  |         items.sort() | 
					
						
							|  |  |  |         for key, value in items: | 
					
						
							| 
									
										
										
										
											2003-07-01 20:15:38 +00:00
										 |  |  |             if not isinstance(key, (str, unicode)): | 
					
						
							|  |  |  |                 raise TypeError("keys must be strings") | 
					
						
							| 
									
										
										
										
											2003-04-09 13:25:43 +00:00
										 |  |  |             self.simpleElement("key", key) | 
					
						
							|  |  |  |             self.writeValue(value) | 
					
						
							|  |  |  |         self.endElement("dict") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def writeArray(self, array): | 
					
						
							|  |  |  |         self.beginElement("array") | 
					
						
							|  |  |  |         for value in array: | 
					
						
							|  |  |  |             self.writeValue(value) | 
					
						
							|  |  |  |         self.endElement("array") | 
					
						
							| 
									
										
										
										
											2002-11-19 22:01:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-26 07:38:16 +00:00
										 |  |  | class _InternalDict(dict): | 
					
						
							| 
									
										
										
										
											2002-11-19 22:01:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-26 07:38:16 +00:00
										 |  |  |     # This class is needed while Dict is scheduled for deprecation: | 
					
						
							|  |  |  |     # we only need to warn when a *user* instantiates Dict or when | 
					
						
							|  |  |  |     # the "attribute notation for dict keys" is used. | 
					
						
							| 
									
										
										
										
											2002-11-19 22:01:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-02 08:40:47 +00:00
										 |  |  |     def __getattr__(self, attr): | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             value = self[attr] | 
					
						
							|  |  |  |         except KeyError: | 
					
						
							|  |  |  |             raise AttributeError, attr | 
					
						
							| 
									
										
										
										
											2004-10-26 07:38:16 +00:00
										 |  |  |         from warnings import warn | 
					
						
							|  |  |  |         warn("Attribute access from plist dicts is deprecated, use d[key] " | 
					
						
							|  |  |  |              "notation instead", PendingDeprecationWarning) | 
					
						
							| 
									
										
										
										
											2004-10-02 08:40:47 +00:00
										 |  |  |         return value | 
					
						
							| 
									
										
										
										
											2002-11-19 22:01:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-02 08:40:47 +00:00
										 |  |  |     def __setattr__(self, attr, value): | 
					
						
							| 
									
										
										
										
											2004-10-26 07:38:16 +00:00
										 |  |  |         from warnings import warn | 
					
						
							|  |  |  |         warn("Attribute access from plist dicts is deprecated, use d[key] " | 
					
						
							|  |  |  |              "notation instead", PendingDeprecationWarning) | 
					
						
							| 
									
										
										
										
											2004-10-02 08:40:47 +00:00
										 |  |  |         self[attr] = value | 
					
						
							| 
									
										
										
										
											2002-11-20 20:47:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-02 08:40:47 +00:00
										 |  |  |     def __delattr__(self, attr): | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             del self[attr] | 
					
						
							|  |  |  |         except KeyError: | 
					
						
							|  |  |  |             raise AttributeError, attr | 
					
						
							| 
									
										
										
										
											2004-10-26 07:38:16 +00:00
										 |  |  |         from warnings import warn | 
					
						
							|  |  |  |         warn("Attribute access from plist dicts is deprecated, use d[key] " | 
					
						
							|  |  |  |              "notation instead", PendingDeprecationWarning) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Dict(_InternalDict): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __init__(self, **kwargs): | 
					
						
							|  |  |  |         from warnings import warn | 
					
						
							|  |  |  |         warn("The plistlib.Dict class is deprecated, use builtin dict instead", | 
					
						
							|  |  |  |              PendingDeprecationWarning) | 
					
						
							|  |  |  |         super(Dict, self).__init__(**kwargs) | 
					
						
							| 
									
										
										
										
											2002-11-19 22:01:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-26 07:38:16 +00:00
										 |  |  | class Plist(_InternalDict): | 
					
						
							| 
									
										
										
										
											2002-11-19 22:01:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-26 10:30:55 +00:00
										 |  |  |     """This class has been deprecated. Use readPlist() and writePlist()
 | 
					
						
							| 
									
										
										
										
											2004-10-26 07:38:16 +00:00
										 |  |  |     functions instead, together with regular dict objects. | 
					
						
							| 
									
										
										
										
											2003-04-09 13:25:43 +00:00
										 |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-25 16:09:10 +00:00
										 |  |  |     def __init__(self, **kwargs): | 
					
						
							|  |  |  |         from warnings import warn | 
					
						
							|  |  |  |         warn("The Plist class is deprecated, use the readPlist() and " | 
					
						
							|  |  |  |              "writePlist() functions instead", PendingDeprecationWarning) | 
					
						
							|  |  |  |         super(Plist, self).__init__(**kwargs) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-09 13:25:43 +00:00
										 |  |  |     def fromFile(cls, pathOrFile): | 
					
						
							| 
									
										
										
										
											2004-10-25 16:09:10 +00:00
										 |  |  |         """Deprecated. Use the readPlist() function instead.""" | 
					
						
							| 
									
										
										
										
											2004-10-02 08:40:47 +00:00
										 |  |  |         rootObject = readPlist(pathOrFile) | 
					
						
							|  |  |  |         plist = cls() | 
					
						
							|  |  |  |         plist.update(rootObject) | 
					
						
							| 
									
										
										
										
											2003-04-09 13:25:43 +00:00
										 |  |  |         return plist | 
					
						
							|  |  |  |     fromFile = classmethod(fromFile) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def write(self, pathOrFile): | 
					
						
							| 
									
										
										
										
											2004-10-25 16:09:10 +00:00
										 |  |  |         """Deprecated. Use the writePlist() function instead.""" | 
					
						
							| 
									
										
										
										
											2004-10-02 08:40:47 +00:00
										 |  |  |         writePlist(self, pathOrFile) | 
					
						
							| 
									
										
										
										
											2002-11-19 22:01:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-26 10:30:55 +00:00
										 |  |  | def _encodeBase64(s, maxlinelength=76): | 
					
						
							|  |  |  |     # copied from base64.encodestring(), with added maxlinelength argument | 
					
						
							|  |  |  |     maxbinsize = (maxlinelength//4)*3 | 
					
						
							|  |  |  |     pieces = [] | 
					
						
							|  |  |  |     for i in range(0, len(s), maxbinsize): | 
					
						
							|  |  |  |         chunk = s[i : i + maxbinsize] | 
					
						
							|  |  |  |         pieces.append(binascii.b2a_base64(chunk)) | 
					
						
							|  |  |  |     return "".join(pieces) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-19 22:01:02 +00:00
										 |  |  | class Data: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-09 13:25:43 +00:00
										 |  |  |     """Wrapper for binary data.""" | 
					
						
							| 
									
										
										
										
											2002-11-19 22:01:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-09 13:25:43 +00:00
										 |  |  |     def __init__(self, data): | 
					
						
							|  |  |  |         self.data = data | 
					
						
							| 
									
										
										
										
											2002-11-19 22:01:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-09 13:25:43 +00:00
										 |  |  |     def fromBase64(cls, data): | 
					
						
							| 
									
										
										
										
											2004-10-26 10:30:55 +00:00
										 |  |  |         # base64.decodestring just calls binascii.a2b_base64; | 
					
						
							|  |  |  |         # it seems overkill to use both base64 and binascii. | 
					
						
							|  |  |  |         return cls(binascii.a2b_base64(data)) | 
					
						
							| 
									
										
										
										
											2003-04-09 13:25:43 +00:00
										 |  |  |     fromBase64 = classmethod(fromBase64) | 
					
						
							| 
									
										
										
										
											2002-11-19 22:01:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-26 10:30:55 +00:00
										 |  |  |     def asBase64(self, maxlinelength=76): | 
					
						
							|  |  |  |         return _encodeBase64(self.data, maxlinelength) | 
					
						
							| 
									
										
										
										
											2002-11-19 22:01:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-09 13:25:43 +00:00
										 |  |  |     def __cmp__(self, other): | 
					
						
							|  |  |  |         if isinstance(other, self.__class__): | 
					
						
							|  |  |  |             return cmp(self.data, other.data) | 
					
						
							|  |  |  |         elif isinstance(other, str): | 
					
						
							|  |  |  |             return cmp(self.data, other) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             return cmp(id(self), id(other)) | 
					
						
							| 
									
										
										
										
											2002-11-19 22:01:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-09 13:25:43 +00:00
										 |  |  |     def __repr__(self): | 
					
						
							|  |  |  |         return "%s(%s)" % (self.__class__.__name__, repr(self.data)) | 
					
						
							| 
									
										
										
										
											2002-11-19 22:01:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class PlistParser: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-09 13:25:43 +00:00
										 |  |  |     def __init__(self): | 
					
						
							|  |  |  |         self.stack = [] | 
					
						
							|  |  |  |         self.currentKey = None | 
					
						
							|  |  |  |         self.root = None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-25 15:10:42 +00:00
										 |  |  |     def parse(self, fileobj): | 
					
						
							| 
									
										
										
										
											2003-04-09 13:25:43 +00:00
										 |  |  |         from xml.parsers.expat import ParserCreate | 
					
						
							|  |  |  |         parser = ParserCreate() | 
					
						
							|  |  |  |         parser.StartElementHandler = self.handleBeginElement | 
					
						
							|  |  |  |         parser.EndElementHandler = self.handleEndElement | 
					
						
							|  |  |  |         parser.CharacterDataHandler = self.handleData | 
					
						
							| 
									
										
										
										
											2004-10-25 15:10:42 +00:00
										 |  |  |         parser.ParseFile(fileobj) | 
					
						
							| 
									
										
										
										
											2003-04-09 13:25:43 +00:00
										 |  |  |         return self.root | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def handleBeginElement(self, element, attrs): | 
					
						
							|  |  |  |         self.data = [] | 
					
						
							|  |  |  |         handler = getattr(self, "begin_" + element, None) | 
					
						
							|  |  |  |         if handler is not None: | 
					
						
							|  |  |  |             handler(attrs) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def handleEndElement(self, element): | 
					
						
							|  |  |  |         handler = getattr(self, "end_" + element, None) | 
					
						
							|  |  |  |         if handler is not None: | 
					
						
							|  |  |  |             handler() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def handleData(self, data): | 
					
						
							|  |  |  |         self.data.append(data) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def addObject(self, value): | 
					
						
							|  |  |  |         if self.currentKey is not None: | 
					
						
							|  |  |  |             self.stack[-1][self.currentKey] = value | 
					
						
							|  |  |  |             self.currentKey = None | 
					
						
							|  |  |  |         elif not self.stack: | 
					
						
							|  |  |  |             # this is the root object | 
					
						
							| 
									
										
										
										
											2004-10-02 08:40:47 +00:00
										 |  |  |             self.root = value | 
					
						
							| 
									
										
										
										
											2003-04-09 13:25:43 +00:00
										 |  |  |         else: | 
					
						
							|  |  |  |             self.stack[-1].append(value) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def getData(self): | 
					
						
							|  |  |  |         data = "".join(self.data) | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             data = data.encode("ascii") | 
					
						
							|  |  |  |         except UnicodeError: | 
					
						
							|  |  |  |             pass | 
					
						
							|  |  |  |         self.data = [] | 
					
						
							|  |  |  |         return data | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # element handlers | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def begin_dict(self, attrs): | 
					
						
							| 
									
										
										
										
											2004-10-26 07:38:16 +00:00
										 |  |  |         d = _InternalDict() | 
					
						
							| 
									
										
										
										
											2003-04-09 13:25:43 +00:00
										 |  |  |         self.addObject(d) | 
					
						
							|  |  |  |         self.stack.append(d) | 
					
						
							|  |  |  |     def end_dict(self): | 
					
						
							|  |  |  |         self.stack.pop() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def end_key(self): | 
					
						
							|  |  |  |         self.currentKey = self.getData() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def begin_array(self, attrs): | 
					
						
							|  |  |  |         a = [] | 
					
						
							|  |  |  |         self.addObject(a) | 
					
						
							|  |  |  |         self.stack.append(a) | 
					
						
							|  |  |  |     def end_array(self): | 
					
						
							|  |  |  |         self.stack.pop() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def end_true(self): | 
					
						
							|  |  |  |         self.addObject(True) | 
					
						
							|  |  |  |     def end_false(self): | 
					
						
							|  |  |  |         self.addObject(False) | 
					
						
							|  |  |  |     def end_integer(self): | 
					
						
							|  |  |  |         self.addObject(int(self.getData())) | 
					
						
							|  |  |  |     def end_real(self): | 
					
						
							|  |  |  |         self.addObject(float(self.getData())) | 
					
						
							|  |  |  |     def end_string(self): | 
					
						
							|  |  |  |         self.addObject(self.getData()) | 
					
						
							|  |  |  |     def end_data(self): | 
					
						
							|  |  |  |         self.addObject(Data.fromBase64(self.getData())) | 
					
						
							|  |  |  |     def end_date(self): | 
					
						
							| 
									
										
										
										
											2004-10-26 07:20:26 +00:00
										 |  |  |         self.addObject(_dateFromString(self.getData())) |