当前位置: 首页>>代码示例>>Python>>正文


Python NSData.dataWithContentsOfMappedFile_方法代码示例

本文整理汇总了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)
开发者ID:svn2github,项目名称:calendarserver-raw,代码行数:72,代码来源:localization.py


注:本文中的Foundation.NSData.dataWithContentsOfMappedFile_方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。