summaryrefslogtreecommitdiffstats
path: root/cython/plist.pyx
diff options
context:
space:
mode:
authorGravatar Andrew Udvare2014-04-02 20:37:40 -0700
committerGravatar Nikias Bassen2019-07-11 01:31:00 +0700
commit121c834561b1b3f06259d32681cab0cff1a1a8ae (patch)
tree0d62cf3e48873f3c0faf84f5a8f8759d3bf42451 /cython/plist.pyx
parent7be52ea1769dd6e4f8e747cce8d5000549c6dab3 (diff)
downloadlibplist-121c834561b1b3f06259d32681cab0cff1a1a8ae.tar.gz
libplist-121c834561b1b3f06259d32681cab0cff1a1a8ae.tar.bz2
cython: Implement dump()/dumps() to match up with plistlib (Python 3.4)
Diffstat (limited to 'cython/plist.pyx')
-rw-r--r--cython/plist.pyx35
1 files changed, 35 insertions, 0 deletions
diff --git a/cython/plist.pyx b/cython/plist.pyx
index 573f17f..3c6389c 100644
--- a/cython/plist.pyx
+++ b/cython/plist.pyx
@@ -907,3 +907,38 @@ cpdef object loads(data, fmt=None, use_builtin_types=True, dict_type=dict):
907 raise ValueError('Cannot parse XML property list as binary') 907 raise ValueError('Cannot parse XML property list as binary')
908 908
909 return cb(data) 909 return cb(data)
910
911cpdef object dump(value, fp, fmt=FMT_XML, sort_keys=True, skipkeys=False):
912 fp.write(dumps(value, fmt=fmt))
913
914cpdef object dumps(value, fmt=FMT_XML, sort_keys=True, skipkeys=False):
915 if fmt not in (FMT_XML, FMT_BINARY):
916 raise ValueError('Format must be constant FMT_XML or FMT_BINARY')
917
918 if check_datetime(value):
919 node = Date(value)
920 elif isinstance(value, unicode):
921 node = String(value)
922 elif PY_MAJOR_VERSION >= 3 and isinstance(value, bytes):
923 node = Data(value)
924 elif isinstance(value, str):
925 # See if this is binary
926 try:
927 node = String(value)
928 except ValueError:
929 node = Data(value)
930 elif isinstance(value, bool):
931 node = Bool(value)
932 elif isinstance(value, int):
933 node = Integer(value)
934 elif isinstance(value, float):
935 node = Real(value)
936 elif isinstance(value, dict):
937 node = Dict(value)
938 elif type(value) in (list, set, tuple):
939 node = Array(value)
940
941 if fmt == FMT_XML:
942 return node.to_xml()
943
944 return node.to_bin()