本文整理汇总了Python中plistlib.dump方法的典型用法代码示例。如果您正苦于以下问题:Python plistlib.dump方法的具体用法?Python plistlib.dump怎么用?Python plistlib.dump使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类plistlib
的用法示例。
在下文中一共展示了plistlib.dump方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: dumps
# 需要导入模块: import plistlib [as 别名]
# 或者: from plistlib import dump [as 别名]
def dumps(value, fmt=FMT_XML, skipkeys=False, sort_keys=True):
if _check_py3():
return plistlib.dumps(value, fmt=fmt, skipkeys=skipkeys, sort_keys=sort_keys).decode("utf-8")
else:
# We avoid using writePlistToString() as that uses
# cStringIO and fails when Unicode strings are detected
f = StringIO()
dump(value, f, fmt=fmt, skipkeys=skipkeys, sort_keys=sort_keys)
return f.getvalue()
### ###
# Binary Plist Stuff For Py2 #
### ###
# From the python 3 plistlib.py source: https://github.com/python/cpython/blob/3.7/Lib/plistlib.py
# Tweaked to function on Python 2
示例2: writePlist
# 需要导入模块: import plistlib [as 别名]
# 或者: from plistlib import dump [as 别名]
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
示例3: test_keysort_bytesio
# 需要导入模块: import plistlib [as 别名]
# 或者: from plistlib import dump [as 别名]
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'])
示例4: test_skipkeys
# 需要导入模块: import plistlib [as 别名]
# 或者: from plistlib import dump [as 别名]
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'})
示例5: create_plist
# 需要导入模块: import plistlib [as 别名]
# 或者: from plistlib import dump [as 别名]
def create_plist(self):
"""
Create the Contents/Info.plist file.
"""
import plistlib
contents = {
'CFBundleName': APPNAME,
'CFBundleGetInfoString': VERSION,
'CFBundleShortVersionString': VERSION,
'CFBundleVersion': VERSION,
'CFBundlePackageType': 'APPL',
'CFBundleIconFile': 'icon.icns',
'CFBundleIdentifier': 'it.uniroma1.eddy',
'CFBundleInfoDictionaryVersion': '6.0',
'CFBundleDevelopmentRegion': 'English',
'CFBundleSpokenName': APPNAME,
'CFBundleExecutable': self.bundle_executable,
'NSPrincipalClass': 'NSApplication',
'NSHighResolutionCapable': 'True',
}
plist = open(os.path.join(self.contentsDir, 'Info.plist'), 'wb')
plistlib.dump(contents, plist)
plist.close()
示例6: write_plist_to_file
# 需要导入模块: import plistlib [as 别名]
# 或者: from plistlib import dump [as 别名]
def write_plist_to_file(deserialised_plist, output_path):
#Using plistLib to write plist
out_file = None
try:
print('Writing out .. ' + output_path)
out_file = open(output_path, 'wb')
try:
plistlib.dump(deserialised_plist, out_file, fmt=plistlib.FMT_BINARY)
out_file.close()
return True
except (TypeError, OverflowError, OSError) as ex:
out_file.close()
print('Had an exception (error)')
traceback.print_exc()
except OSError as ex:
print('Error opening file for writing: Error={} Path={}'.format(output_path, str(ex)))
# Try using biplist
try:
print('Writing out (using biplist) .. ' + output_path)
biplist.writePlist(deserialised_plist, output_path)
return True
except (biplist.InvalidPlistException, biplist.NotBinaryPlistException, OSError) as ex:
print('Had an exception (error)')
traceback.print_exc()
示例7: _create_app
# 需要导入模块: import plistlib [as 别名]
# 或者: from plistlib import dump [as 别名]
def _create_app(app_name, executable, icon, plist):
'''
https://stackoverflow.com/questions/1596945/building-osx-app-bundle
https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFBundles/BundleTypes/BundleTypes.html#//apple_ref/doc/uid/10000123i-CH101-SW16
Only works if app_name finishes in ".app"
'''
import plistlib
# SimNIBS app setup
if os.path.isdir(app_name):
shutil.rmtree(app_name)
os.makedirs(app_name)
contents_dir = os.path.join(app_name, 'Contents')
resouces_dir = os.path.join(contents_dir, 'Resources')
macos_dir = os.path.join(contents_dir, 'MacOS')
if not os.path.isdir(contents_dir):
os.mkdir(contents_dir)
if not os.path.isdir(resouces_dir):
os.mkdir(resouces_dir)
if not os.path.isdir(macos_dir):
os.mkdir(macos_dir)
_copy_and_log(
icon,
os.path.join(resouces_dir))
# Write a simnibs_gui and a gmsh executable
_write_unix_sh(
executable,
os.path.join(macos_dir, 'simnibs_gui'))
with open(os.path.join(contents_dir, 'Info.plist'), 'wb') as fp:
plistlib.dump(plist, fp)
示例8: patch_mac_app
# 需要导入模块: import plistlib [as 别名]
# 或者: from plistlib import dump [as 别名]
def patch_mac_app():
"""Patch .app to use our Info.plist and save some space."""
app_path = os.path.join('dist', 'qutebrowser.app')
# Patch Info.plist - pyinstaller's options are too limiting
plist_path = os.path.join(app_path, 'Contents', 'Info.plist')
with open(plist_path, "rb") as f:
plist_data = plistlib.load(f)
plist_data.update(INFO_PLIST_UPDATES)
with open(plist_path, "wb") as f:
plistlib.dump(plist_data, f)
# Replace some duplicate files by symlinks
framework_path = os.path.join(app_path, 'Contents', 'MacOS', 'PyQt5',
'Qt', 'lib', 'QtWebEngineCore.framework')
core_lib = os.path.join(framework_path, 'Versions', '5', 'QtWebEngineCore')
os.remove(core_lib)
core_target = os.path.join(*[os.pardir] * 7, 'MacOS', 'QtWebEngineCore')
os.symlink(core_target, core_lib)
framework_resource_path = os.path.join(framework_path, 'Resources')
for name in os.listdir(framework_resource_path):
file_path = os.path.join(framework_resource_path, name)
target = os.path.join(*[os.pardir] * 5, name)
if os.path.isdir(file_path):
shutil.rmtree(file_path)
else:
os.remove(file_path)
os.symlink(target, file_path)
示例9: do_plist
# 需要导入模块: import plistlib [as 别名]
# 或者: from plistlib import dump [as 别名]
def do_plist(module, filename, values, backup=False):
working_values = values
changed = False
try:
f = open(filename, 'rb')
plist = plistlib.load(f)
except IOError:
plist = {}
except plistlib.InvalidFileException:
module.fail_json(msg="an invalid plist already exists")
changed = not equal(plist, working_values)
if changed and not module.check_mode:
if backup:
module.backup_local(filename)
try:
update(plist, working_values)
plist_dir = os.path.dirname(filename)
if not os.path.exists(plist_dir):
os.makedirs(plist_dir)
f = open(filename, 'wb')
plistlib.dump(plist, f)
except Exception as e:
module.fail_json(msg="Can't change %s" % filename, error=str(e))
return changed
示例10: writePlist
# 需要导入模块: import plistlib [as 别名]
# 或者: from plistlib import dump [as 别名]
def writePlist(value, pathOrFile):
if not isinstance(pathOrFile, basestring):
return dump(value, pathOrFile, fmt=FMT_XML, sort_keys=True, skipkeys=False)
with open(pathOrFile, "wb") as f:
return dump(value, f, fmt=FMT_XML, sort_keys=True, skipkeys=False)
### ###
# Remapped Functions #
### ###
示例11: dump
# 需要导入模块: import plistlib [as 别名]
# 或者: from plistlib import dump [as 别名]
def dump(value, fp, fmt=FMT_XML, sort_keys=True, skipkeys=False):
if _check_py3():
plistlib.dump(value, fp, fmt=fmt, sort_keys=sort_keys, skipkeys=skipkeys)
else:
if fmt == FMT_XML:
# We need to monkey patch a bunch here too in order to avoid auto-sorting
# of keys
writer = plistlib.PlistWriter(fp)
def writeDict(d):
if d:
writer.beginElement("dict")
items = sorted(d.items()) if sort_keys else d.items()
for key, value in items:
if not isinstance(key, basestring):
if skipkeys:
continue
raise TypeError("keys must be strings")
writer.simpleElement("key", key)
writer.writeValue(value)
writer.endElement("dict")
else:
writer.simpleElement("dict")
writer.writeDict = writeDict
writer.writeln("<plist version=\"1.0\">")
writer.writeValue(value)
writer.writeln("</plist>")
elif fmt == FMT_BINARY:
# Assume binary at this point
writer = _BinaryPlistWriter(fp, sort_keys=sort_keys, skipkeys=skipkeys)
writer.write(value)
else:
# Not a proper format
raise ValueError("Unsupported format: {}".format(fmt))
示例12: writePlist
# 需要导入模块: import plistlib [as 别名]
# 或者: from plistlib import dump [as 别名]
def writePlist(value, pathOrFile):
if not isinstance(pathOrFile, _get_inst()):
return dump(value, pathOrFile, fmt=FMT_XML, sort_keys=True, skipkeys=False)
with open(pathOrFile, "wb") as f:
return dump(value, f, fmt=FMT_XML, sort_keys=True, skipkeys=False)
### ###
# Remapped Functions #
### ###
示例13: dump
# 需要导入模块: import plistlib [as 别名]
# 或者: from plistlib import dump [as 别名]
def dump(value, fp, fmt=FMT_XML, sort_keys=True, skipkeys=False):
if _check_py3():
plistlib.dump(value, fp, fmt=fmt, sort_keys=sort_keys, skipkeys=skipkeys)
else:
if fmt == FMT_XML:
# We need to monkey patch a bunch here too in order to avoid auto-sorting
# of keys
writer = plistlib.PlistWriter(fp)
def writeDict(d):
if d:
writer.beginElement("dict")
items = sorted(d.items()) if sort_keys else d.items()
for key, value in items:
if not isinstance(key, (str,unicode)):
if skipkeys:
continue
raise TypeError("keys must be strings")
writer.simpleElement("key", key)
writer.writeValue(value)
writer.endElement("dict")
else:
writer.simpleElement("dict")
writer.writeDict = writeDict
writer.writeln("<plist version=\"1.0\">")
writer.writeValue(value)
writer.writeln("</plist>")
elif fmt == FMT_BINARY:
# Assume binary at this point
writer = _BinaryPlistWriter(fp, sort_keys=sort_keys, skipkeys=skipkeys)
writer.write(value)
else:
# Not a proper format
raise ValueError("Unsupported format: {}".format(fmt))
示例14: test_io
# 需要导入模块: import plistlib [as 别名]
# 或者: from plistlib import dump [as 别名]
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')