summaryrefslogtreecommitdiffstats
path: root/cython/plist.pyx
diff options
context:
space:
mode:
authorGravatar Andrew Udvare2014-03-30 23:22:09 -0700
committerGravatar Nikias Bassen2019-07-11 01:30:27 +0700
commit7be52ea1769dd6e4f8e747cce8d5000549c6dab3 (patch)
tree0a5eae18ce2e7caf5a4b349882642ae041eb144f /cython/plist.pyx
parent7d6b42c9e738c97f32023d78313ec390806a9731 (diff)
downloadlibplist-7be52ea1769dd6e4f8e747cce8d5000549c6dab3.tar.gz
libplist-7be52ea1769dd6e4f8e747cce8d5000549c6dab3.tar.bz2
cython: Implement load()/loads() to match up with plistlib (Python 3.4)
Diffstat (limited to 'cython/plist.pyx')
-rw-r--r--cython/plist.pyx55
1 files changed, 55 insertions, 0 deletions
diff --git a/cython/plist.pyx b/cython/plist.pyx
index f6696d6..573f17f 100644
--- a/cython/plist.pyx
+++ b/cython/plist.pyx
@@ -852,3 +852,58 @@ cdef object plist_t_to_node(plist_t c_plist, bint managed=True):
return Uid_factory(c_plist, managed)
if t == PLIST_NONE:
return None
+
+# This is to match up with the new plistlib API
+# http://docs.python.org/dev/library/plistlib.html
+# dump() and dumps() are not yet implemented
+FMT_XML = 1
+FMT_BINARY = 2
+
+cpdef object load(fp, fmt=None, use_builtin_types=True, dict_type=dict):
+ is_binary = fp.read(6) == 'bplist'
+ fp.seek(0)
+
+ if not fmt:
+ if is_binary:
+ if 'b' not in fp.mode:
+ raise IOError('File handle must be opened in binary (b) mode to read binary property lists')
+ cb = from_bin
+ else:
+ cb = from_xml
+ else:
+ if fmt not in (FMT_XML, FMT_BINARY):
+ raise ValueError('Format must be constant FMT_XML or FMT_BINARY')
+ if fmt == FMT_BINARY:
+ cb = from_bin
+ elif fmt == FMT_XML:
+ cb = from_xml
+
+ if is_binary and fmt == FMT_XML:
+ raise ValueError('Cannot parse binary property list as XML')
+ elif not is_binary and fmt == FMT_BINARY:
+ raise ValueError('Cannot parse XML property list as binary')
+
+ return cb(fp.read())
+
+cpdef object loads(data, fmt=None, use_builtin_types=True, dict_type=dict):
+ is_binary = data[0:6] == 'bplist'
+
+ if fmt is not None:
+ if fmt not in (FMT_XML, FMT_BINARY):
+ raise ValueError('Format must be constant FMT_XML or FMT_BINARY')
+ if fmt == FMT_BINARY:
+ cb = from_bin
+ else:
+ cb = from_xml
+ else:
+ if is_binary:
+ cb = from_bin
+ else:
+ cb = from_xml
+
+ if is_binary and fmt == FMT_XML:
+ raise ValueError('Cannot parse binary property list as XML')
+ elif not is_binary and fmt == FMT_BINARY:
+ raise ValueError('Cannot parse XML property list as binary')
+
+ return cb(data)