我们从Python开源项目中,提取了以下17个代码示例,用于说明如何使用plistlib.writePlistToBytes()。
def test_bytes(self): pl = self._create() data = plistlib.writePlistToBytes(pl) pl2 = plistlib.readPlistFromBytes(data) self.assertEqual(dict(pl), dict(pl2)) data2 = plistlib.writePlistToBytes(pl2) self.assertEqual(data, data2)
def test_appleformatting(self): pl = plistlib.readPlistFromBytes(TESTDATA) data = plistlib.writePlistToBytes(pl) self.assertEqual(data, TESTDATA, "generated data was not identical to Apple's output")
def test_controlcharacters(self): for i in range(128): c = chr(i) testString = "string containing %s" % c if i >= 32 or c in "\r\n\t": # \r, \n and \t are the only legal control chars in XML plistlib.writePlistToBytes(testString) else: self.assertRaises(ValueError, plistlib.writePlistToBytes, testString)
def test_nondictroot(self): test1 = "abc" test2 = [1, 2, 3, "abc"] result1 = plistlib.readPlistFromBytes(plistlib.writePlistToBytes(test1)) result2 = plistlib.readPlistFromBytes(plistlib.writePlistToBytes(test2)) self.assertEqual(test1, result1) self.assertEqual(test2, result2)
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 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 test_indentation_array(self): data = [[[[[[[[{'test': plistlib.Data(b'aaaaaa')}]]]]]]]] self.assertEqual(plistlib.readPlistFromBytes(plistlib.writePlistToBytes(data)), data)
def test_indentation_dict(self): data = {'1': {'2': {'3': {'4': {'5': {'6': {'7': {'8': {'9': plistlib.Data(b'aaaaaa')}}}}}}}}} self.assertEqual(plistlib.readPlistFromBytes(plistlib.writePlistToBytes(data)), data)
def test_indentation_dict_mix(self): data = {'1': {'2': [{'3': [[[[[{'test': plistlib.Data(b'aaaaaa')}]]]]]}]}} self.assertEqual(plistlib.readPlistFromBytes(plistlib.writePlistToBytes(data)), data)
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 test_bytes_deprecated(self): pl = { 'key': 42, 'sub': { 'key': 9, 'alt': 'value', 'data': b'buffer', } } with self.assertWarns(DeprecationWarning): data = plistlib.writePlistToBytes(pl) with self.assertWarns(DeprecationWarning): pl2 = plistlib.readPlistFromBytes(data) self.assertIsInstance(pl2, plistlib._InternalDict) self.assertEqual(pl2, plistlib._InternalDict( key=42, sub=plistlib._InternalDict( key=9, alt='value', data=plistlib.Data(b'buffer'), ) )) with self.assertWarns(DeprecationWarning): data2 = plistlib.writePlistToBytes(pl2) self.assertEqual(data, data2)