mirror of
https://github.com/corpnewt/gibMacOS.git
synced 2024-09-20 01:30:33 +00:00
Fix py3, remove subclass
This commit is contained in:
parent
3e228593ed
commit
e31b473e1e
1 changed files with 21 additions and 38 deletions
|
@ -4,14 +4,16 @@
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
# Force use of StringIO instead of cStringIO as the latter
|
|
||||||
# has issues with Unicode strings
|
|
||||||
from StringIO import StringIO
|
|
||||||
import os
|
import os
|
||||||
import plistlib
|
import plistlib
|
||||||
import struct
|
import struct
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
if sys.version_info < (3,0):
|
||||||
|
# Force use of StringIO instead of cStringIO as the latter
|
||||||
|
# has issues with Unicode strings
|
||||||
|
from StringIO import StringIO
|
||||||
|
|
||||||
try:
|
try:
|
||||||
FMT_XML = plistlib.FMT_XML
|
FMT_XML = plistlib.FMT_XML
|
||||||
except:
|
except:
|
||||||
|
@ -81,8 +83,22 @@ def loads(value, fmt=None, use_builtin_types=True, dict_type=dict):
|
||||||
# Is not binary - assume a string - and try to load
|
# Is not binary - assume a string - and try to load
|
||||||
# We avoid using readPlistFromString() as that uses
|
# We avoid using readPlistFromString() as that uses
|
||||||
# cStringIO and fails when Unicode strings are detected
|
# cStringIO and fails when Unicode strings are detected
|
||||||
s = StringIO(value)
|
# Don't subclass - keep the parser local
|
||||||
p = UnicodePlistParser()
|
from xml.parsers.expat import ParserCreate
|
||||||
|
# Create a new PlistParser object - then we need to set up
|
||||||
|
# the values and parse.
|
||||||
|
p = plistlib.PlistParser()
|
||||||
|
parser = ParserCreate()
|
||||||
|
parser.StartElementHandler = p.handleBeginElement
|
||||||
|
parser.EndElementHandler = p.handleEndElement
|
||||||
|
parser.CharacterDataHandler = p.handleData
|
||||||
|
if isinstance(value, unicode):
|
||||||
|
# Encode unicode -> string; use utf-8 for safety
|
||||||
|
value = value.encode("utf-8")
|
||||||
|
# Parse the string
|
||||||
|
parser.Parse(value, 1)
|
||||||
|
return p.root
|
||||||
|
|
||||||
rootObject = p.parse(s)
|
rootObject = p.parse(s)
|
||||||
return rootObject
|
return rootObject
|
||||||
|
|
||||||
|
@ -98,43 +114,10 @@ def dumps(value, fmt=FMT_XML, skipkeys=False):
|
||||||
else:
|
else:
|
||||||
# We avoid using writePlistToString() as that uses
|
# We avoid using writePlistToString() as that uses
|
||||||
# cStringIO and fails when Unicode strings are detected
|
# cStringIO and fails when Unicode strings are detected
|
||||||
# return plistlib.writePlistToString(value)
|
|
||||||
f = StringIO()
|
f = StringIO()
|
||||||
plistlib.writePlist(value, f)
|
plistlib.writePlist(value, f)
|
||||||
return f.getvalue()
|
return f.getvalue()
|
||||||
|
|
||||||
### ###
|
|
||||||
# PlistParsing of Unicode in Py2 #
|
|
||||||
### ###
|
|
||||||
|
|
||||||
class UnicodePlistParser (plistlib.PlistParser):
|
|
||||||
def _init_(self):
|
|
||||||
return
|
|
||||||
# Override the parse() method to allow for unicode results
|
|
||||||
# from StringIO.read()
|
|
||||||
def parse(self, fileobj):
|
|
||||||
from xml.parsers.expat import ParserCreate
|
|
||||||
parser = ParserCreate()
|
|
||||||
parser.StartElementHandler = self.handleBeginElement
|
|
||||||
parser.EndElementHandler = self.handleEndElement
|
|
||||||
parser.CharacterDataHandler = self.handleData
|
|
||||||
if isinstance(fileobj, StringIO):
|
|
||||||
# If we have a StringIO object, let's try to
|
|
||||||
# read it - and find out what it returns as
|
|
||||||
# the ParseFile() function can't work with
|
|
||||||
# unicode objects
|
|
||||||
f_read = fileobj.read()
|
|
||||||
if isinstance(f_read, unicode):
|
|
||||||
# Encode unicode -> string; use utf-8 for safety
|
|
||||||
f_read = f_read.encode("utf-8")
|
|
||||||
# Parse the string
|
|
||||||
parser.Parse(f_read, 1)
|
|
||||||
else:
|
|
||||||
# Not a StringIO object - probably a file object?
|
|
||||||
# Parse normally.
|
|
||||||
parser.ParseFile(fileobj)
|
|
||||||
return self.root
|
|
||||||
|
|
||||||
### ###
|
### ###
|
||||||
# Binary Plist Stuff For Py2 #
|
# Binary Plist Stuff For Py2 #
|
||||||
### ###
|
### ###
|
||||||
|
|
Loading…
Reference in a new issue