我们从Python开源项目中,提取了以下17个代码示例,用于说明如何使用plistlib.dump()。
def writePlist(rootObject, pathOrFile, binary=True): if not binary: rootObject = wrapDataObject(rootObject, binary) if hasattr(plistlib, "dump"): if isinstance(pathOrFile, (bytes, unicode)): with open(pathOrFile, 'wb') as f: return plistlib.dump(rootObject, f) else: return plistlib.dump(rootObject, pathOrFile) else: return plistlib.writePlist(rootObject, pathOrFile) else: didOpen = False if isinstance(pathOrFile, (bytes, unicode)): pathOrFile = open(pathOrFile, 'wb') didOpen = True writer = PlistWriter(pathOrFile) result = writer.writeRoot(rootObject) if didOpen: pathOrFile.close() return result
def test_keysort_bytesio(self): pl = collections.OrderedDict() pl['b'] = 1 pl['a'] = 2 pl['c'] = 3 for fmt in ALL_FORMATS: for sort_keys in (False, True): with self.subTest(fmt=fmt, sort_keys=sort_keys): b = BytesIO() plistlib.dump(pl, b, fmt=fmt, sort_keys=sort_keys) pl2 = plistlib.load(BytesIO(b.getvalue()), dict_type=collections.OrderedDict) self.assertEqual(dict(pl), dict(pl2)) if sort_keys: self.assertEqual(list(pl2.keys()), ['a', 'b', 'c']) else: self.assertEqual(list(pl2.keys()), ['b', 'a', 'c'])
def test_skipkeys(self): pl = { 42: 'aNumber', 'snake': 'aWord', } for fmt in ALL_FORMATS: with self.subTest(fmt=fmt): data = plistlib.dumps( pl, fmt=fmt, skipkeys=True, sort_keys=False) pl2 = plistlib.loads(data) self.assertEqual(pl2, {'snake': 'aWord'}) fp = BytesIO() plistlib.dump( pl, fp, fmt=fmt, skipkeys=True, sort_keys=False) data = fp.getvalue() pl2 = plistlib.loads(fp.getvalue()) self.assertEqual(pl2, {'snake': 'aWord'})
def save_plist(data_dict, file_path): """ save a dict as a plist file :param data_dict: dict data :param file_path: plist file path to save :return: """ import plistlib if hasattr(plistlib, "dump"): with open(file_path, 'wb') as fp: plistlib.dump(data_dict, fp) else: plistlib.writePlist(data_dict, file_path)
def set_info(self, k, v): """ Set a value for a given key in an app's base project info.plist """ info = self.get_info() with open(self.info, 'wb') as f: # Note that we have to write the entire contents to the file. # so we load the current data, add whatever we need to it then info[k] = v plistlib.dump(info, f)
def test_io(self): pl = self._create() with open(support.TESTFN, 'wb') as fp: plistlib.dump(pl, fp) with open(support.TESTFN, 'rb') as fp: pl2 = plistlib.load(fp) self.assertEqual(dict(pl), dict(pl2)) self.assertRaises(AttributeError, plistlib.dump, pl, 'filename') self.assertRaises(AttributeError, plistlib.load, 'filename')
def test_bytesio(self): for fmt in ALL_FORMATS: with self.subTest(fmt=fmt): b = BytesIO() pl = self._create(fmt=fmt) plistlib.dump(pl, b, fmt=fmt) pl2 = plistlib.load(BytesIO(b.getvalue()), fmt=fmt) self.assertEqual(dict(pl), dict(pl2)) pl2 = plistlib.load(BytesIO(b.getvalue())) self.assertEqual(dict(pl), dict(pl2))
def test_keys_no_string(self): pl = { 42: 'aNumber' } for fmt in ALL_FORMATS: with self.subTest(fmt=fmt): self.assertRaises(TypeError, plistlib.dumps, pl, fmt=fmt) b = BytesIO() self.assertRaises(TypeError, plistlib.dump, pl, b, fmt=fmt)
def _change_build_version(self): """ set CFBundleVersion and CFBundleShortVersionString. """ build_version_list = self._build_version.split('.') cf_bundle_short_version_string = '.'.join(build_version_list[:3]) with open(self._plist_path, 'rb') as fp: plist_content = plistlib.load(fp) plist_content['CFBundleShortVersionString'] = cf_bundle_short_version_string plist_content['CFBundleVersion'] = self._build_version with open(self._plist_path, 'wb') as fp: plistlib.dump(plist_content, fp)
def vtt_dump(infile, outfile=None, **kwargs): if not os.path.exists(infile): raise VTTLibArgumentError("'%s' not found" % infile) font = TTFont(infile) if font.sfntVersion not in ("\x00\x01\x00\x00", "true"): raise VTTLibArgumentError("Not a TrueType font (bad sfntVersion)") for table_tag in VTT_TABLES: if table_tag not in font: raise VTTLibArgumentError( "Table '%s' not found in input font" % table_tag) if not outfile: ufo = os.path.splitext(infile)[0] + ".ufo" else: ufo = outfile if not os.path.exists(ufo) or not os.path.isdir(ufo): raise VTTLibArgumentError("No such directory: '%s'" % ufo) check_ufo_version(ufo) folder = os.path.join(ufo, "data", TTX_DATA_FOLDER) # create data sub-folder if it doesn't exist already try: os.makedirs(folder) except OSError as e: if e.errno != errno.EEXIST or not os.path.isdir(folder): raise normalize_vtt_programs(font) ufo_contents = read_ufo_contents(ufo) subset_vtt_glyph_programs(font, list(ufo_contents)) for tag in VTT_TABLES: # dump each table individually instead of using 'splitTables' # to avoid creating an extra index file outfile = os.path.join(folder, tagToIdentifier(tag) + '.ttx') # always use Unix LF newlines font.saveXML(outfile, tables=[tag], newlinestr='\n') write_maxp_data(font, ufo)