diff options
| author | 2014-03-30 23:22:09 -0700 | |
|---|---|---|
| committer | 2019-07-11 01:30:27 +0700 | |
| commit | 7be52ea1769dd6e4f8e747cce8d5000549c6dab3 (patch) | |
| tree | 0a5eae18ce2e7caf5a4b349882642ae041eb144f | |
| parent | 7d6b42c9e738c97f32023d78313ec390806a9731 (diff) | |
| download | libplist-7be52ea1769dd6e4f8e747cce8d5000549c6dab3.tar.gz libplist-7be52ea1769dd6e4f8e747cce8d5000549c6dab3.tar.bz2 | |
cython: Implement load()/loads() to match up with plistlib (Python 3.4)
| -rw-r--r-- | cython/plist.pxd | 3 | ||||
| -rw-r--r-- | cython/plist.pyx | 55 |
2 files changed, 58 insertions, 0 deletions
diff --git a/cython/plist.pxd b/cython/plist.pxd index ee8a65e..99dcca5 100644 --- a/cython/plist.pxd +++ b/cython/plist.pxd | |||
| @@ -69,5 +69,8 @@ cdef class Array(Node): | |||
| 69 | cpdef object from_xml(xml) | 69 | cpdef object from_xml(xml) |
| 70 | cpdef object from_bin(bytes bin) | 70 | cpdef object from_bin(bytes bin) |
| 71 | 71 | ||
| 72 | cpdef object load(fp, fmt=*, use_builtin_types=*, dict_type=*) | ||
| 73 | cpdef object loads(data, fmt=*, use_builtin_types=*, dict_type=*) | ||
| 74 | |||
| 72 | cdef object plist_t_to_node(plist_t c_plist, bint managed=*) | 75 | cdef object plist_t_to_node(plist_t c_plist, bint managed=*) |
| 73 | cdef plist_t native_to_plist_t(object native) | 76 | cdef plist_t native_to_plist_t(object native) |
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): | |||
| 852 | return Uid_factory(c_plist, managed) | 852 | return Uid_factory(c_plist, managed) |
| 853 | if t == PLIST_NONE: | 853 | if t == PLIST_NONE: |
| 854 | return None | 854 | return None |
| 855 | |||
| 856 | # This is to match up with the new plistlib API | ||
| 857 | # http://docs.python.org/dev/library/plistlib.html | ||
| 858 | # dump() and dumps() are not yet implemented | ||
| 859 | FMT_XML = 1 | ||
| 860 | FMT_BINARY = 2 | ||
| 861 | |||
| 862 | cpdef object load(fp, fmt=None, use_builtin_types=True, dict_type=dict): | ||
| 863 | is_binary = fp.read(6) == 'bplist' | ||
| 864 | fp.seek(0) | ||
| 865 | |||
| 866 | if not fmt: | ||
| 867 | if is_binary: | ||
| 868 | if 'b' not in fp.mode: | ||
| 869 | raise IOError('File handle must be opened in binary (b) mode to read binary property lists') | ||
| 870 | cb = from_bin | ||
| 871 | else: | ||
| 872 | cb = from_xml | ||
| 873 | else: | ||
| 874 | if fmt not in (FMT_XML, FMT_BINARY): | ||
| 875 | raise ValueError('Format must be constant FMT_XML or FMT_BINARY') | ||
| 876 | if fmt == FMT_BINARY: | ||
| 877 | cb = from_bin | ||
| 878 | elif fmt == FMT_XML: | ||
| 879 | cb = from_xml | ||
| 880 | |||
| 881 | if is_binary and fmt == FMT_XML: | ||
| 882 | raise ValueError('Cannot parse binary property list as XML') | ||
| 883 | elif not is_binary and fmt == FMT_BINARY: | ||
| 884 | raise ValueError('Cannot parse XML property list as binary') | ||
| 885 | |||
| 886 | return cb(fp.read()) | ||
| 887 | |||
| 888 | cpdef object loads(data, fmt=None, use_builtin_types=True, dict_type=dict): | ||
| 889 | is_binary = data[0:6] == 'bplist' | ||
| 890 | |||
| 891 | if fmt is not None: | ||
| 892 | if fmt not in (FMT_XML, FMT_BINARY): | ||
| 893 | raise ValueError('Format must be constant FMT_XML or FMT_BINARY') | ||
| 894 | if fmt == FMT_BINARY: | ||
| 895 | cb = from_bin | ||
| 896 | else: | ||
| 897 | cb = from_xml | ||
| 898 | else: | ||
| 899 | if is_binary: | ||
| 900 | cb = from_bin | ||
| 901 | else: | ||
| 902 | cb = from_xml | ||
| 903 | |||
| 904 | if is_binary and fmt == FMT_XML: | ||
| 905 | raise ValueError('Cannot parse binary property list as XML') | ||
| 906 | elif not is_binary and fmt == FMT_BINARY: | ||
| 907 | raise ValueError('Cannot parse XML property list as binary') | ||
| 908 | |||
| 909 | return cb(data) | ||
