本文整理汇总了Python中Foundation.NSData.dataWithContentsOfMappedFile_方法的典型用法代码示例。如果您正苦于以下问题:Python NSData.dataWithContentsOfMappedFile_方法的具体用法?Python NSData.dataWithContentsOfMappedFile_怎么用?Python NSData.dataWithContentsOfMappedFile_使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Foundation.NSData
的用法示例。
在下文中一共展示了NSData.dataWithContentsOfMappedFile_方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: convertStringsFile
# 需要导入模块: from Foundation import NSData [as 别名]
# 或者: from Foundation.NSData import dataWithContentsOfMappedFile_ [as 别名]
def convertStringsFile(src, dest):
dir = os.path.dirname(dest)
if not os.path.exists(dir):
try:
os.makedirs(dir)
except OSError:
# can't create directory to hold .po file
return
# Parse the binary plist .strings file:
parser = NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_
data = NSData.dataWithContentsOfMappedFile_(src)
strings, format, error = parser(data, NSPropertyListImmutable, None, None)
if error:
raise ParseError(error)
# The format of GNUtext MO files is described here:
# http://www.gnu.org/software/autoconf/manual/gettext/MO-Files.html
strings = dict(strings)
originals = strings.keys()
originals.sort()
descriptors = []
keys = ''
values = ''
for original in originals:
translation = strings[original]
origStr = original.encode("UTF-8")
transStr = translation.encode("UTF-8")
descriptors.append((len(keys), len(origStr), len(values),
len(transStr)))
keys += origStr + '\0' # <NUL> terminated
values += transStr + '\0'
# The header is 28 bytes, each descriptor is 8 bytes, with two descriptors
# per string (one pointing at original, one pointing at translation)
keysOffset = 28 + len(originals) * 2 * 8
valuesOffset = keysOffset + len(keys)
keyDescriptors = []
valueDescriptors = []
for origOffset, origLen, transOffset, transLen in descriptors:
keyDescriptors.append(origLen)
keyDescriptors.append(keysOffset + origOffset)
valueDescriptors.append(transLen)
valueDescriptors.append(valuesOffset + transOffset)
result = struct.pack(
"Iiiiiii",
0x950412DEL, # magic number
0, # file format revision
len(originals), # number of strings
28, # offset of table with original strings
28 + len(originals) * 8, # offset of table with translation strings
0, # size of hashing table
0 # offset of hashing table
)
result += array.array("i", keyDescriptors).tostring()
result += array.array("i", valueDescriptors).tostring()
result += keys
result += values
with open(dest, "wb") as outFile:
outFile.write(result)