mirror of
https://github.com/corpnewt/gibMacOS.git
synced 2024-09-20 09:30:34 +00:00
More plist fixes
This commit is contained in:
parent
7ff7192787
commit
a9087e9a9f
1 changed files with 17 additions and 11 deletions
|
@ -22,6 +22,8 @@ def _check_py3():
|
||||||
return True if sys.version_info >= (3, 0) else False
|
return True if sys.version_info >= (3, 0) else False
|
||||||
|
|
||||||
def _is_binary(fp):
|
def _is_binary(fp):
|
||||||
|
if isinstance(fp, _get_inst()):
|
||||||
|
return fp.startswith(b"bplist00")
|
||||||
header = fp.read(32)
|
header = fp.read(32)
|
||||||
fp.seek(0)
|
fp.seek(0)
|
||||||
return header[:8] == b'bplist00'
|
return header[:8] == b'bplist00'
|
||||||
|
@ -61,16 +63,21 @@ def load(fp, fmt=None, use_builtin_types=True, dict_type=dict):
|
||||||
return readBinaryPlistFile(fp)
|
return readBinaryPlistFile(fp)
|
||||||
|
|
||||||
def loads(value, fmt=None, use_builtin_types=True, dict_type=dict):
|
def loads(value, fmt=None, use_builtin_types=True, dict_type=dict):
|
||||||
if _check_py3() and isinstance(value, _get_inst()):
|
|
||||||
# We were sent a string in py3 - let's encode it to some utf-8 bytes for fun!
|
|
||||||
value = value.encode()
|
|
||||||
fp = BytesIO(value)
|
|
||||||
if _check_py3():
|
if _check_py3():
|
||||||
return plistlib.load(fp, fmt=fmt, use_builtin_types=use_builtin_types, dict_type=dict_type)
|
# Requires fp to be a BytesIO wrapper around a bytes object
|
||||||
elif not _is_binary(fp):
|
if isinstance(value, _get_inst()):
|
||||||
return plistlib.readPlistFromString(value)
|
# If it's a string - encode it
|
||||||
|
value = value.encode()
|
||||||
|
# Load it
|
||||||
|
return plistlib.load(BytesIO(value), fmt=fmt, use_builtin_types=use_builtin_types, dict_type=dict_type)
|
||||||
else:
|
else:
|
||||||
return readBinaryPlistFile(fp)
|
if _is_binary(value):
|
||||||
|
# Has the proper header to be a binary plist
|
||||||
|
return readBinaryPlistFile(BytesIO(value))
|
||||||
|
else:
|
||||||
|
# Is not binary - assume a string - and try to load
|
||||||
|
|
||||||
|
return plistlib.readPlistFromString(value)
|
||||||
|
|
||||||
def dump(value, fp, fmt=FMT_XML, sort_keys=True, skipkeys=False):
|
def dump(value, fp, fmt=FMT_XML, sort_keys=True, skipkeys=False):
|
||||||
if _check_py3():
|
if _check_py3():
|
||||||
|
@ -80,10 +87,9 @@ def dump(value, fp, fmt=FMT_XML, sort_keys=True, skipkeys=False):
|
||||||
|
|
||||||
def dumps(value, fmt=FMT_XML, skipkeys=False):
|
def dumps(value, fmt=FMT_XML, skipkeys=False):
|
||||||
if _check_py3():
|
if _check_py3():
|
||||||
return plistlib.dumps(value, fmt=fmt, skipkeys=skipkeys).encode("utf-8")
|
return plistlib.dumps(value, fmt=fmt, skipkeys=skipkeys).decode("utf-8")
|
||||||
else:
|
else:
|
||||||
return plistlib.writePlistToString(value).encode("utf-8")
|
return plistlib.writePlistToString(value)
|
||||||
|
|
||||||
|
|
||||||
### ###
|
### ###
|
||||||
# Binary Plist Stuff For Py2 #
|
# Binary Plist Stuff For Py2 #
|
||||||
|
|
Loading…
Reference in a new issue