plistlib.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. r"""plistlib.py -- a tool to generate and parse MacOSX .plist files.
  2. The property list (.plist) file format is a simple XML pickle supporting
  3. basic object types, like dictionaries, lists, numbers and strings.
  4. Usually the top level object is a dictionary.
  5. To write out a plist file, use the dump(value, file)
  6. function. 'value' is the top level object, 'file' is
  7. a (writable) file object.
  8. To parse a plist from a file, use the load(file) function,
  9. with a (readable) file object as the only argument. It
  10. returns the top level object (again, usually a dictionary).
  11. To work with plist data in bytes objects, you can use loads()
  12. and dumps().
  13. Values can be strings, integers, floats, booleans, tuples, lists,
  14. dictionaries (but only with string keys), Data, bytes, bytearray, or
  15. datetime.datetime objects.
  16. Generate Plist example:
  17. import datetime
  18. import plistlib
  19. pl = dict(
  20. aString = "Doodah",
  21. aList = ["A", "B", 12, 32.1, [1, 2, 3]],
  22. aFloat = 0.1,
  23. anInt = 728,
  24. aDict = dict(
  25. anotherString = "<hello & hi there!>",
  26. aThirdString = "M\xe4ssig, Ma\xdf",
  27. aTrueValue = True,
  28. aFalseValue = False,
  29. ),
  30. someData = b"<binary gunk>",
  31. someMoreData = b"<lots of binary gunk>" * 10,
  32. aDate = datetime.datetime.now()
  33. )
  34. print(plistlib.dumps(pl).decode())
  35. Parse Plist example:
  36. import plistlib
  37. plist = b'''<plist version="1.0">
  38. <dict>
  39. <key>foo</key>
  40. <string>bar</string>
  41. </dict>
  42. </plist>'''
  43. pl = plistlib.loads(plist)
  44. print(pl["foo"])
  45. """
  46. __all__ = [
  47. "InvalidFileException", "FMT_XML", "FMT_BINARY", "load", "dump", "loads", "dumps", "UID"
  48. ]
  49. import binascii
  50. import codecs
  51. import datetime
  52. import enum
  53. from io import BytesIO
  54. import itertools
  55. import os
  56. import re
  57. import struct
  58. from xml.parsers.expat import ParserCreate
  59. PlistFormat = enum.Enum('PlistFormat', 'FMT_XML FMT_BINARY', module=__name__)
  60. globals().update(PlistFormat.__members__)
  61. class UID:
  62. def __init__(self, data):
  63. if not isinstance(data, int):
  64. raise TypeError("data must be an int")
  65. if data >= 1 << 64:
  66. raise ValueError("UIDs cannot be >= 2**64")
  67. if data < 0:
  68. raise ValueError("UIDs must be positive")
  69. self.data = data
  70. def __index__(self):
  71. return self.data
  72. def __repr__(self):
  73. return "%s(%s)" % (self.__class__.__name__, repr(self.data))
  74. def __reduce__(self):
  75. return self.__class__, (self.data,)
  76. def __eq__(self, other):
  77. if not isinstance(other, UID):
  78. return NotImplemented
  79. return self.data == other.data
  80. def __hash__(self):
  81. return hash(self.data)
  82. #
  83. # XML support
  84. #
  85. # XML 'header'
  86. PLISTHEADER = b"""\
  87. <?xml version="1.0" encoding="UTF-8"?>
  88. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  89. """
  90. # Regex to find any control chars, except for \t \n and \r
  91. _controlCharPat = re.compile(
  92. r"[\x00\x01\x02\x03\x04\x05\x06\x07\x08\x0b\x0c\x0e\x0f"
  93. r"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f]")
  94. def _encode_base64(s, maxlinelength=76):
  95. # copied from base64.encodebytes(), with added maxlinelength argument
  96. maxbinsize = (maxlinelength//4)*3
  97. pieces = []
  98. for i in range(0, len(s), maxbinsize):
  99. chunk = s[i : i + maxbinsize]
  100. pieces.append(binascii.b2a_base64(chunk))
  101. return b''.join(pieces)
  102. def _decode_base64(s):
  103. if isinstance(s, str):
  104. return binascii.a2b_base64(s.encode("utf-8"))
  105. else:
  106. return binascii.a2b_base64(s)
  107. # Contents should conform to a subset of ISO 8601
  108. # (in particular, YYYY '-' MM '-' DD 'T' HH ':' MM ':' SS 'Z'. Smaller units
  109. # may be omitted with # a loss of precision)
  110. _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", re.ASCII)
  111. def _date_from_string(s):
  112. order = ('year', 'month', 'day', 'hour', 'minute', 'second')
  113. gd = _dateParser.match(s).groupdict()
  114. lst = []
  115. for key in order:
  116. val = gd[key]
  117. if val is None:
  118. break
  119. lst.append(int(val))
  120. return datetime.datetime(*lst)
  121. def _date_to_string(d):
  122. return '%04d-%02d-%02dT%02d:%02d:%02dZ' % (
  123. d.year, d.month, d.day,
  124. d.hour, d.minute, d.second
  125. )
  126. def _escape(text):
  127. m = _controlCharPat.search(text)
  128. if m is not None:
  129. raise ValueError("strings can't contain control characters; "
  130. "use bytes instead")
  131. text = text.replace("\r\n", "\n") # convert DOS line endings
  132. text = text.replace("\r", "\n") # convert Mac line endings
  133. text = text.replace("&", "&amp;") # escape '&'
  134. text = text.replace("<", "&lt;") # escape '<'
  135. text = text.replace(">", "&gt;") # escape '>'
  136. return text
  137. class _PlistParser:
  138. def __init__(self, dict_type):
  139. self.stack = []
  140. self.current_key = None
  141. self.root = None
  142. self._dict_type = dict_type
  143. def parse(self, fileobj):
  144. self.parser = ParserCreate()
  145. self.parser.StartElementHandler = self.handle_begin_element
  146. self.parser.EndElementHandler = self.handle_end_element
  147. self.parser.CharacterDataHandler = self.handle_data
  148. self.parser.EntityDeclHandler = self.handle_entity_decl
  149. self.parser.ParseFile(fileobj)
  150. return self.root
  151. def handle_entity_decl(self, entity_name, is_parameter_entity, value, base, system_id, public_id, notation_name):
  152. # Reject plist files with entity declarations to avoid XML vulnerabilities in expat.
  153. # Regular plist files don't contain those declarations, and Apple's plutil tool does not
  154. # accept them either.
  155. raise InvalidFileException("XML entity declarations are not supported in plist files")
  156. def handle_begin_element(self, element, attrs):
  157. self.data = []
  158. handler = getattr(self, "begin_" + element, None)
  159. if handler is not None:
  160. handler(attrs)
  161. def handle_end_element(self, element):
  162. handler = getattr(self, "end_" + element, None)
  163. if handler is not None:
  164. handler()
  165. def handle_data(self, data):
  166. self.data.append(data)
  167. def add_object(self, value):
  168. if self.current_key is not None:
  169. if not isinstance(self.stack[-1], dict):
  170. raise ValueError("unexpected element at line %d" %
  171. self.parser.CurrentLineNumber)
  172. self.stack[-1][self.current_key] = value
  173. self.current_key = None
  174. elif not self.stack:
  175. # this is the root object
  176. self.root = value
  177. else:
  178. if not isinstance(self.stack[-1], list):
  179. raise ValueError("unexpected element at line %d" %
  180. self.parser.CurrentLineNumber)
  181. self.stack[-1].append(value)
  182. def get_data(self):
  183. data = ''.join(self.data)
  184. self.data = []
  185. return data
  186. # element handlers
  187. def begin_dict(self, attrs):
  188. d = self._dict_type()
  189. self.add_object(d)
  190. self.stack.append(d)
  191. def end_dict(self):
  192. if self.current_key:
  193. raise ValueError("missing value for key '%s' at line %d" %
  194. (self.current_key,self.parser.CurrentLineNumber))
  195. self.stack.pop()
  196. def end_key(self):
  197. if self.current_key or not isinstance(self.stack[-1], dict):
  198. raise ValueError("unexpected key at line %d" %
  199. self.parser.CurrentLineNumber)
  200. self.current_key = self.get_data()
  201. def begin_array(self, attrs):
  202. a = []
  203. self.add_object(a)
  204. self.stack.append(a)
  205. def end_array(self):
  206. self.stack.pop()
  207. def end_true(self):
  208. self.add_object(True)
  209. def end_false(self):
  210. self.add_object(False)
  211. def end_integer(self):
  212. raw = self.get_data()
  213. if raw.startswith('0x') or raw.startswith('0X'):
  214. self.add_object(int(raw, 16))
  215. else:
  216. self.add_object(int(raw))
  217. def end_real(self):
  218. self.add_object(float(self.get_data()))
  219. def end_string(self):
  220. self.add_object(self.get_data())
  221. def end_data(self):
  222. self.add_object(_decode_base64(self.get_data()))
  223. def end_date(self):
  224. self.add_object(_date_from_string(self.get_data()))
  225. class _DumbXMLWriter:
  226. def __init__(self, file, indent_level=0, indent="\t"):
  227. self.file = file
  228. self.stack = []
  229. self._indent_level = indent_level
  230. self.indent = indent
  231. def begin_element(self, element):
  232. self.stack.append(element)
  233. self.writeln("<%s>" % element)
  234. self._indent_level += 1
  235. def end_element(self, element):
  236. assert self._indent_level > 0
  237. assert self.stack.pop() == element
  238. self._indent_level -= 1
  239. self.writeln("</%s>" % element)
  240. def simple_element(self, element, value=None):
  241. if value is not None:
  242. value = _escape(value)
  243. self.writeln("<%s>%s</%s>" % (element, value, element))
  244. else:
  245. self.writeln("<%s/>" % element)
  246. def writeln(self, line):
  247. if line:
  248. # plist has fixed encoding of utf-8
  249. # XXX: is this test needed?
  250. if isinstance(line, str):
  251. line = line.encode('utf-8')
  252. self.file.write(self._indent_level * self.indent)
  253. self.file.write(line)
  254. self.file.write(b'\n')
  255. class _PlistWriter(_DumbXMLWriter):
  256. def __init__(
  257. self, file, indent_level=0, indent=b"\t", writeHeader=1,
  258. sort_keys=True, skipkeys=False):
  259. if writeHeader:
  260. file.write(PLISTHEADER)
  261. _DumbXMLWriter.__init__(self, file, indent_level, indent)
  262. self._sort_keys = sort_keys
  263. self._skipkeys = skipkeys
  264. def write(self, value):
  265. self.writeln("<plist version=\"1.0\">")
  266. self.write_value(value)
  267. self.writeln("</plist>")
  268. def write_value(self, value):
  269. if isinstance(value, str):
  270. self.simple_element("string", value)
  271. elif value is True:
  272. self.simple_element("true")
  273. elif value is False:
  274. self.simple_element("false")
  275. elif isinstance(value, int):
  276. if -1 << 63 <= value < 1 << 64:
  277. self.simple_element("integer", "%d" % value)
  278. else:
  279. raise OverflowError(value)
  280. elif isinstance(value, float):
  281. self.simple_element("real", repr(value))
  282. elif isinstance(value, dict):
  283. self.write_dict(value)
  284. elif isinstance(value, (bytes, bytearray)):
  285. self.write_bytes(value)
  286. elif isinstance(value, datetime.datetime):
  287. self.simple_element("date", _date_to_string(value))
  288. elif isinstance(value, (tuple, list)):
  289. self.write_array(value)
  290. else:
  291. raise TypeError("unsupported type: %s" % type(value))
  292. def write_bytes(self, data):
  293. self.begin_element("data")
  294. self._indent_level -= 1
  295. maxlinelength = max(
  296. 16,
  297. 76 - len(self.indent.replace(b"\t", b" " * 8) * self._indent_level))
  298. for line in _encode_base64(data, maxlinelength).split(b"\n"):
  299. if line:
  300. self.writeln(line)
  301. self._indent_level += 1
  302. self.end_element("data")
  303. def write_dict(self, d):
  304. if d:
  305. self.begin_element("dict")
  306. if self._sort_keys:
  307. items = sorted(d.items())
  308. else:
  309. items = d.items()
  310. for key, value in items:
  311. if not isinstance(key, str):
  312. if self._skipkeys:
  313. continue
  314. raise TypeError("keys must be strings")
  315. self.simple_element("key", key)
  316. self.write_value(value)
  317. self.end_element("dict")
  318. else:
  319. self.simple_element("dict")
  320. def write_array(self, array):
  321. if array:
  322. self.begin_element("array")
  323. for value in array:
  324. self.write_value(value)
  325. self.end_element("array")
  326. else:
  327. self.simple_element("array")
  328. def _is_fmt_xml(header):
  329. prefixes = (b'<?xml', b'<plist')
  330. for pfx in prefixes:
  331. if header.startswith(pfx):
  332. return True
  333. # Also check for alternative XML encodings, this is slightly
  334. # overkill because the Apple tools (and plistlib) will not
  335. # generate files with these encodings.
  336. for bom, encoding in (
  337. (codecs.BOM_UTF8, "utf-8"),
  338. (codecs.BOM_UTF16_BE, "utf-16-be"),
  339. (codecs.BOM_UTF16_LE, "utf-16-le"),
  340. # expat does not support utf-32
  341. #(codecs.BOM_UTF32_BE, "utf-32-be"),
  342. #(codecs.BOM_UTF32_LE, "utf-32-le"),
  343. ):
  344. if not header.startswith(bom):
  345. continue
  346. for start in prefixes:
  347. prefix = bom + start.decode('ascii').encode(encoding)
  348. if header[:len(prefix)] == prefix:
  349. return True
  350. return False
  351. #
  352. # Binary Plist
  353. #
  354. class InvalidFileException (ValueError):
  355. def __init__(self, message="Invalid file"):
  356. ValueError.__init__(self, message)
  357. _BINARY_FORMAT = {1: 'B', 2: 'H', 4: 'L', 8: 'Q'}
  358. _undefined = object()
  359. class _BinaryPlistParser:
  360. """
  361. Read or write a binary plist file, following the description of the binary
  362. format. Raise InvalidFileException in case of error, otherwise return the
  363. root object.
  364. see also: http://opensource.apple.com/source/CF/CF-744.18/CFBinaryPList.c
  365. """
  366. def __init__(self, dict_type):
  367. self._dict_type = dict_type
  368. def parse(self, fp):
  369. try:
  370. # The basic file format:
  371. # HEADER
  372. # object...
  373. # refid->offset...
  374. # TRAILER
  375. self._fp = fp
  376. self._fp.seek(-32, os.SEEK_END)
  377. trailer = self._fp.read(32)
  378. if len(trailer) != 32:
  379. raise InvalidFileException()
  380. (
  381. offset_size, self._ref_size, num_objects, top_object,
  382. offset_table_offset
  383. ) = struct.unpack('>6xBBQQQ', trailer)
  384. self._fp.seek(offset_table_offset)
  385. self._object_offsets = self._read_ints(num_objects, offset_size)
  386. self._objects = [_undefined] * num_objects
  387. return self._read_object(top_object)
  388. except (OSError, IndexError, struct.error, OverflowError,
  389. ValueError):
  390. raise InvalidFileException()
  391. def _get_size(self, tokenL):
  392. """ return the size of the next object."""
  393. if tokenL == 0xF:
  394. m = self._fp.read(1)[0] & 0x3
  395. s = 1 << m
  396. f = '>' + _BINARY_FORMAT[s]
  397. return struct.unpack(f, self._fp.read(s))[0]
  398. return tokenL
  399. def _read_ints(self, n, size):
  400. data = self._fp.read(size * n)
  401. if size in _BINARY_FORMAT:
  402. return struct.unpack(f'>{n}{_BINARY_FORMAT[size]}', data)
  403. else:
  404. if not size or len(data) != size * n:
  405. raise InvalidFileException()
  406. return tuple(int.from_bytes(data[i: i + size], 'big')
  407. for i in range(0, size * n, size))
  408. def _read_refs(self, n):
  409. return self._read_ints(n, self._ref_size)
  410. def _read_object(self, ref):
  411. """
  412. read the object by reference.
  413. May recursively read sub-objects (content of an array/dict/set)
  414. """
  415. result = self._objects[ref]
  416. if result is not _undefined:
  417. return result
  418. offset = self._object_offsets[ref]
  419. self._fp.seek(offset)
  420. token = self._fp.read(1)[0]
  421. tokenH, tokenL = token & 0xF0, token & 0x0F
  422. if token == 0x00:
  423. result = None
  424. elif token == 0x08:
  425. result = False
  426. elif token == 0x09:
  427. result = True
  428. # The referenced source code also mentions URL (0x0c, 0x0d) and
  429. # UUID (0x0e), but neither can be generated using the Cocoa libraries.
  430. elif token == 0x0f:
  431. result = b''
  432. elif tokenH == 0x10: # int
  433. result = int.from_bytes(self._fp.read(1 << tokenL),
  434. 'big', signed=tokenL >= 3)
  435. elif token == 0x22: # real
  436. result = struct.unpack('>f', self._fp.read(4))[0]
  437. elif token == 0x23: # real
  438. result = struct.unpack('>d', self._fp.read(8))[0]
  439. elif token == 0x33: # date
  440. f = struct.unpack('>d', self._fp.read(8))[0]
  441. # timestamp 0 of binary plists corresponds to 1/1/2001
  442. # (year of Mac OS X 10.0), instead of 1/1/1970.
  443. result = (datetime.datetime(2001, 1, 1) +
  444. datetime.timedelta(seconds=f))
  445. elif tokenH == 0x40: # data
  446. s = self._get_size(tokenL)
  447. result = self._fp.read(s)
  448. if len(result) != s:
  449. raise InvalidFileException()
  450. elif tokenH == 0x50: # ascii string
  451. s = self._get_size(tokenL)
  452. data = self._fp.read(s)
  453. if len(data) != s:
  454. raise InvalidFileException()
  455. result = data.decode('ascii')
  456. elif tokenH == 0x60: # unicode string
  457. s = self._get_size(tokenL) * 2
  458. data = self._fp.read(s)
  459. if len(data) != s:
  460. raise InvalidFileException()
  461. result = data.decode('utf-16be')
  462. elif tokenH == 0x80: # UID
  463. # used by Key-Archiver plist files
  464. result = UID(int.from_bytes(self._fp.read(1 + tokenL), 'big'))
  465. elif tokenH == 0xA0: # array
  466. s = self._get_size(tokenL)
  467. obj_refs = self._read_refs(s)
  468. result = []
  469. self._objects[ref] = result
  470. result.extend(self._read_object(x) for x in obj_refs)
  471. # tokenH == 0xB0 is documented as 'ordset', but is not actually
  472. # implemented in the Apple reference code.
  473. # tokenH == 0xC0 is documented as 'set', but sets cannot be used in
  474. # plists.
  475. elif tokenH == 0xD0: # dict
  476. s = self._get_size(tokenL)
  477. key_refs = self._read_refs(s)
  478. obj_refs = self._read_refs(s)
  479. result = self._dict_type()
  480. self._objects[ref] = result
  481. try:
  482. for k, o in zip(key_refs, obj_refs):
  483. result[self._read_object(k)] = self._read_object(o)
  484. except TypeError:
  485. raise InvalidFileException()
  486. else:
  487. raise InvalidFileException()
  488. self._objects[ref] = result
  489. return result
  490. def _count_to_size(count):
  491. if count < 1 << 8:
  492. return 1
  493. elif count < 1 << 16:
  494. return 2
  495. elif count < 1 << 32:
  496. return 4
  497. else:
  498. return 8
  499. _scalars = (str, int, float, datetime.datetime, bytes)
  500. class _BinaryPlistWriter (object):
  501. def __init__(self, fp, sort_keys, skipkeys):
  502. self._fp = fp
  503. self._sort_keys = sort_keys
  504. self._skipkeys = skipkeys
  505. def write(self, value):
  506. # Flattened object list:
  507. self._objlist = []
  508. # Mappings from object->objectid
  509. # First dict has (type(object), object) as the key,
  510. # second dict is used when object is not hashable and
  511. # has id(object) as the key.
  512. self._objtable = {}
  513. self._objidtable = {}
  514. # Create list of all objects in the plist
  515. self._flatten(value)
  516. # Size of object references in serialized containers
  517. # depends on the number of objects in the plist.
  518. num_objects = len(self._objlist)
  519. self._object_offsets = [0]*num_objects
  520. self._ref_size = _count_to_size(num_objects)
  521. self._ref_format = _BINARY_FORMAT[self._ref_size]
  522. # Write file header
  523. self._fp.write(b'bplist00')
  524. # Write object list
  525. for obj in self._objlist:
  526. self._write_object(obj)
  527. # Write refnum->object offset table
  528. top_object = self._getrefnum(value)
  529. offset_table_offset = self._fp.tell()
  530. offset_size = _count_to_size(offset_table_offset)
  531. offset_format = '>' + _BINARY_FORMAT[offset_size] * num_objects
  532. self._fp.write(struct.pack(offset_format, *self._object_offsets))
  533. # Write trailer
  534. sort_version = 0
  535. trailer = (
  536. sort_version, offset_size, self._ref_size, num_objects,
  537. top_object, offset_table_offset
  538. )
  539. self._fp.write(struct.pack('>5xBBBQQQ', *trailer))
  540. def _flatten(self, value):
  541. # First check if the object is in the object table, not used for
  542. # containers to ensure that two subcontainers with the same contents
  543. # will be serialized as distinct values.
  544. if isinstance(value, _scalars):
  545. if (type(value), value) in self._objtable:
  546. return
  547. elif id(value) in self._objidtable:
  548. return
  549. # Add to objectreference map
  550. refnum = len(self._objlist)
  551. self._objlist.append(value)
  552. if isinstance(value, _scalars):
  553. self._objtable[(type(value), value)] = refnum
  554. else:
  555. self._objidtable[id(value)] = refnum
  556. # And finally recurse into containers
  557. if isinstance(value, dict):
  558. keys = []
  559. values = []
  560. items = value.items()
  561. if self._sort_keys:
  562. items = sorted(items)
  563. for k, v in items:
  564. if not isinstance(k, str):
  565. if self._skipkeys:
  566. continue
  567. raise TypeError("keys must be strings")
  568. keys.append(k)
  569. values.append(v)
  570. for o in itertools.chain(keys, values):
  571. self._flatten(o)
  572. elif isinstance(value, (list, tuple)):
  573. for o in value:
  574. self._flatten(o)
  575. def _getrefnum(self, value):
  576. if isinstance(value, _scalars):
  577. return self._objtable[(type(value), value)]
  578. else:
  579. return self._objidtable[id(value)]
  580. def _write_size(self, token, size):
  581. if size < 15:
  582. self._fp.write(struct.pack('>B', token | size))
  583. elif size < 1 << 8:
  584. self._fp.write(struct.pack('>BBB', token | 0xF, 0x10, size))
  585. elif size < 1 << 16:
  586. self._fp.write(struct.pack('>BBH', token | 0xF, 0x11, size))
  587. elif size < 1 << 32:
  588. self._fp.write(struct.pack('>BBL', token | 0xF, 0x12, size))
  589. else:
  590. self._fp.write(struct.pack('>BBQ', token | 0xF, 0x13, size))
  591. def _write_object(self, value):
  592. ref = self._getrefnum(value)
  593. self._object_offsets[ref] = self._fp.tell()
  594. if value is None:
  595. self._fp.write(b'\x00')
  596. elif value is False:
  597. self._fp.write(b'\x08')
  598. elif value is True:
  599. self._fp.write(b'\x09')
  600. elif isinstance(value, int):
  601. if value < 0:
  602. try:
  603. self._fp.write(struct.pack('>Bq', 0x13, value))
  604. except struct.error:
  605. raise OverflowError(value) from None
  606. elif value < 1 << 8:
  607. self._fp.write(struct.pack('>BB', 0x10, value))
  608. elif value < 1 << 16:
  609. self._fp.write(struct.pack('>BH', 0x11, value))
  610. elif value < 1 << 32:
  611. self._fp.write(struct.pack('>BL', 0x12, value))
  612. elif value < 1 << 63:
  613. self._fp.write(struct.pack('>BQ', 0x13, value))
  614. elif value < 1 << 64:
  615. self._fp.write(b'\x14' + value.to_bytes(16, 'big', signed=True))
  616. else:
  617. raise OverflowError(value)
  618. elif isinstance(value, float):
  619. self._fp.write(struct.pack('>Bd', 0x23, value))
  620. elif isinstance(value, datetime.datetime):
  621. f = (value - datetime.datetime(2001, 1, 1)).total_seconds()
  622. self._fp.write(struct.pack('>Bd', 0x33, f))
  623. elif isinstance(value, (bytes, bytearray)):
  624. self._write_size(0x40, len(value))
  625. self._fp.write(value)
  626. elif isinstance(value, str):
  627. try:
  628. t = value.encode('ascii')
  629. self._write_size(0x50, len(value))
  630. except UnicodeEncodeError:
  631. t = value.encode('utf-16be')
  632. self._write_size(0x60, len(t) // 2)
  633. self._fp.write(t)
  634. elif isinstance(value, UID):
  635. if value.data < 0:
  636. raise ValueError("UIDs must be positive")
  637. elif value.data < 1 << 8:
  638. self._fp.write(struct.pack('>BB', 0x80, value))
  639. elif value.data < 1 << 16:
  640. self._fp.write(struct.pack('>BH', 0x81, value))
  641. elif value.data < 1 << 32:
  642. self._fp.write(struct.pack('>BL', 0x83, value))
  643. elif value.data < 1 << 64:
  644. self._fp.write(struct.pack('>BQ', 0x87, value))
  645. else:
  646. raise OverflowError(value)
  647. elif isinstance(value, (list, tuple)):
  648. refs = [self._getrefnum(o) for o in value]
  649. s = len(refs)
  650. self._write_size(0xA0, s)
  651. self._fp.write(struct.pack('>' + self._ref_format * s, *refs))
  652. elif isinstance(value, dict):
  653. keyRefs, valRefs = [], []
  654. if self._sort_keys:
  655. rootItems = sorted(value.items())
  656. else:
  657. rootItems = value.items()
  658. for k, v in rootItems:
  659. if not isinstance(k, str):
  660. if self._skipkeys:
  661. continue
  662. raise TypeError("keys must be strings")
  663. keyRefs.append(self._getrefnum(k))
  664. valRefs.append(self._getrefnum(v))
  665. s = len(keyRefs)
  666. self._write_size(0xD0, s)
  667. self._fp.write(struct.pack('>' + self._ref_format * s, *keyRefs))
  668. self._fp.write(struct.pack('>' + self._ref_format * s, *valRefs))
  669. else:
  670. raise TypeError(value)
  671. def _is_fmt_binary(header):
  672. return header[:8] == b'bplist00'
  673. #
  674. # Generic bits
  675. #
  676. _FORMATS={
  677. FMT_XML: dict(
  678. detect=_is_fmt_xml,
  679. parser=_PlistParser,
  680. writer=_PlistWriter,
  681. ),
  682. FMT_BINARY: dict(
  683. detect=_is_fmt_binary,
  684. parser=_BinaryPlistParser,
  685. writer=_BinaryPlistWriter,
  686. )
  687. }
  688. def load(fp, *, fmt=None, dict_type=dict):
  689. """Read a .plist file. 'fp' should be a readable and binary file object.
  690. Return the unpacked root object (which usually is a dictionary).
  691. """
  692. if fmt is None:
  693. header = fp.read(32)
  694. fp.seek(0)
  695. for info in _FORMATS.values():
  696. if info['detect'](header):
  697. P = info['parser']
  698. break
  699. else:
  700. raise InvalidFileException()
  701. else:
  702. P = _FORMATS[fmt]['parser']
  703. p = P(dict_type=dict_type)
  704. return p.parse(fp)
  705. def loads(value, *, fmt=None, dict_type=dict):
  706. """Read a .plist file from a bytes object.
  707. Return the unpacked root object (which usually is a dictionary).
  708. """
  709. fp = BytesIO(value)
  710. return load(fp, fmt=fmt, dict_type=dict_type)
  711. def dump(value, fp, *, fmt=FMT_XML, sort_keys=True, skipkeys=False):
  712. """Write 'value' to a .plist file. 'fp' should be a writable,
  713. binary file object.
  714. """
  715. if fmt not in _FORMATS:
  716. raise ValueError("Unsupported format: %r"%(fmt,))
  717. writer = _FORMATS[fmt]["writer"](fp, sort_keys=sort_keys, skipkeys=skipkeys)
  718. writer.write(value)
  719. def dumps(value, *, fmt=FMT_XML, skipkeys=False, sort_keys=True):
  720. """Return a bytes object with the contents for a .plist file.
  721. """
  722. fp = BytesIO()
  723. dump(value, fp, fmt=fmt, skipkeys=skipkeys, sort_keys=sort_keys)
  724. return fp.getvalue()