def writeContents(self):
"""Write the contents.plist file out to disk. Call this method when
you're done writing glyphs.
"""
from plistlib import writePlistToString
contentsPath = os.path.join(self.dirName, "contents.plist")
# We need to force Unix line endings, even in OS9 MacPython in FL,
# so we do the writing to file ourselves.
plist = writePlistToString(self.contents)
f = open(contentsPath, WRITE_MODE)
f.write(plist)
f.close()
# read caching
python类writePlistToString()的实例源码
def writePlistToString(rootObject, binary=True):
if not binary:
rootObject = wrapDataObject(rootObject, binary)
if hasattr(plistlib, "dumps"):
return plistlib.dumps(rootObject)
elif hasattr(plistlib, "writePlistToBytes"):
return plistlib.writePlistToBytes(rootObject)
else:
return plistlib.writePlistToString(rootObject)
else:
ioObject = io.BytesIO()
writer = PlistWriter(ioObject)
writer.writeRoot(rootObject)
return ioObject.getvalue()
def test_string(self):
pl = self._create()
data = plistlib.writePlistToString(pl)
pl2 = plistlib.readPlistFromString(data)
self.assertEqual(dict(pl), dict(pl2))
data2 = plistlib.writePlistToString(pl2)
self.assertEqual(data, data2)
def test_indentation_array(self):
data = [[[[[[[[{'test': plistlib.Data(b'aaaaaa')}]]]]]]]]
self.assertEqual(plistlib.readPlistFromString(plistlib.writePlistToString(data)), data)
def test_indentation_dict(self):
data = {'1': {'2': {'3': {'4': {'5': {'6': {'7': {'8': {'9': plistlib.Data(b'aaaaaa')}}}}}}}}}
self.assertEqual(plistlib.readPlistFromString(plistlib.writePlistToString(data)), data)
def test_indentation_dict_mix(self):
data = {'1': {'2': [{'3': [[[[[{'test': plistlib.Data(b'aaaaaa')}]]]]]}]}}
self.assertEqual(plistlib.readPlistFromString(plistlib.writePlistToString(data)), data)
def test_appleformatting(self):
pl = plistlib.readPlistFromString(TESTDATA)
data = plistlib.writePlistToString(pl)
self.assertEqual(data, TESTDATA,
"generated data was not identical to Apple's output")
def test_nondictroot(self):
test1 = "abc"
test2 = [1, 2, 3, "abc"]
result1 = plistlib.readPlistFromString(plistlib.writePlistToString(test1))
result2 = plistlib.readPlistFromString(plistlib.writePlistToString(test2))
self.assertEqual(test1, result1)
self.assertEqual(test2, result2)
def test_string(self):
pl = self._create()
data = plistlib.writePlistToString(pl)
pl2 = plistlib.readPlistFromString(data)
self.assertEqual(dict(pl), dict(pl2))
data2 = plistlib.writePlistToString(pl2)
self.assertEqual(data, data2)
def test_indentation_array(self):
data = [[[[[[[[{'test': plistlib.Data(b'aaaaaa')}]]]]]]]]
self.assertEqual(plistlib.readPlistFromString(plistlib.writePlistToString(data)), data)
def test_indentation_dict(self):
data = {'1': {'2': {'3': {'4': {'5': {'6': {'7': {'8': {'9': plistlib.Data(b'aaaaaa')}}}}}}}}}
self.assertEqual(plistlib.readPlistFromString(plistlib.writePlistToString(data)), data)
def test_indentation_dict_mix(self):
data = {'1': {'2': [{'3': [[[[[{'test': plistlib.Data(b'aaaaaa')}]]]]]}]}}
self.assertEqual(plistlib.readPlistFromString(plistlib.writePlistToString(data)), data)
def test_appleformatting(self):
pl = plistlib.readPlistFromString(TESTDATA)
data = plistlib.writePlistToString(pl)
self.assertEqual(data, TESTDATA,
"generated data was not identical to Apple's output")
def test_nondictroot(self):
test1 = "abc"
test2 = [1, 2, 3, "abc"]
result1 = plistlib.readPlistFromString(plistlib.writePlistToString(test1))
result2 = plistlib.readPlistFromString(plistlib.writePlistToString(test2))
self.assertEqual(test1, result1)
self.assertEqual(test2, result2)
def writePlistToString(rootObject, binary=True):
if not binary:
rootObject = wrapDataObject(rootObject, binary)
if six.PY3:
return plistlib.writePlistToBytes(rootObject)
else:
return plistlib.writePlistToString(rootObject)
else:
io = six.BytesIO()
writer = PlistWriter(io)
writer.writeRoot(rootObject)
return io.getvalue()
def writePlist(obj, path):
if not os.path.isabs(path):
path = storage(path)
s = plistlib.writePlistToString(obj)
with codecs.open(path, "w", "utf-8") as f:
f.write(s)
def sendpacket(self, req, tag, payload={}):
payload['ClientVersionString'] = 'usbmux.py by marcan'
if isinstance(req, int):
req = [self.TYPE_CONNECT, self.TYPE_LISTEN][req-2]
payload['MessageType'] = req
payload['ProgName'] = 'tcprelay'
BinaryProtocol.sendpacket(self, self.TYPE_PLIST, tag, plistlib.writePlistToString(payload))
def writePlistToString(rootObject, binary=True):
if not binary:
rootObject = wrapDataObject(rootObject, binary)
if hasattr(plistlib, "dumps"):
return plistlib.dumps(rootObject)
elif hasattr(plistlib, "writePlistToBytes"):
return plistlib.writePlistToBytes(rootObject)
else:
return plistlib.writePlistToString(rootObject)
else:
ioObject = io.BytesIO()
writer = PlistWriter(ioObject)
writer.writeRoot(rootObject)
return ioObject.getvalue()
def gen_launchd(tw, config, writer):
devpibin = py.path.local(sys.argv[0])
plist_content = write_plist_to_bytes(OrderedDict([
("Label", "net.devpi"),
("ProgramArguments", [str(devpibin)] + config.args._raw),
("RunAtLoad", True),
]))
writer("net.devpi.plist", plist_content)
def sendpacket(self, req, tag, payload={}):
payload['ClientVersionString'] = 'usbmux.py by marcan'
if isinstance(req, int):
req = [self.TYPE_CONNECT, self.TYPE_LISTEN][req-2]
payload['MessageType'] = req
payload['ProgName'] = 'tcprelay'
BinaryProtocol.sendpacket(self, self.TYPE_PLIST, tag, plistlib.writePlistToString(payload))
def test_string(self):
pl = self._create()
data = plistlib.writePlistToString(pl)
pl2 = plistlib.readPlistFromString(data)
self.assertEqual(dict(pl), dict(pl2))
data2 = plistlib.writePlistToString(pl2)
self.assertEqual(data, data2)
def test_indentation_array(self):
data = [[[[[[[[{'test': plistlib.Data(b'aaaaaa')}]]]]]]]]
self.assertEqual(plistlib.readPlistFromString(plistlib.writePlistToString(data)), data)
def test_indentation_dict(self):
data = {'1': {'2': {'3': {'4': {'5': {'6': {'7': {'8': {'9': plistlib.Data(b'aaaaaa')}}}}}}}}}
self.assertEqual(plistlib.readPlistFromString(plistlib.writePlistToString(data)), data)
def test_indentation_dict_mix(self):
data = {'1': {'2': [{'3': [[[[[{'test': plistlib.Data(b'aaaaaa')}]]]]]}]}}
self.assertEqual(plistlib.readPlistFromString(plistlib.writePlistToString(data)), data)
def test_appleformatting(self):
pl = plistlib.readPlistFromString(TESTDATA)
data = plistlib.writePlistToString(pl)
self.assertEqual(data, TESTDATA,
"generated data was not identical to Apple's output")
def test_nondictroot(self):
test1 = "abc"
test2 = [1, 2, 3, "abc"]
result1 = plistlib.readPlistFromString(plistlib.writePlistToString(test1))
result2 = plistlib.readPlistFromString(plistlib.writePlistToString(test2))
self.assertEqual(test1, result1)
self.assertEqual(test2, result2)
def test_string(self):
pl = self._create()
data = plistlib.writePlistToString(pl)
pl2 = plistlib.readPlistFromString(data)
self.assertEqual(dict(pl), dict(pl2))
data2 = plistlib.writePlistToString(pl2)
self.assertEqual(data, data2)
def test_indentation_array(self):
data = [[[[[[[[{'test': plistlib.Data(b'aaaaaa')}]]]]]]]]
self.assertEqual(plistlib.readPlistFromString(plistlib.writePlistToString(data)), data)
def test_indentation_dict(self):
data = {'1': {'2': {'3': {'4': {'5': {'6': {'7': {'8': {'9': plistlib.Data(b'aaaaaa')}}}}}}}}}
self.assertEqual(plistlib.readPlistFromString(plistlib.writePlistToString(data)), data)
def test_indentation_dict_mix(self):
data = {'1': {'2': [{'3': [[[[[{'test': plistlib.Data(b'aaaaaa')}]]]]]}]}}
self.assertEqual(plistlib.readPlistFromString(plistlib.writePlistToString(data)), data)