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


Python NSString.stringWithContentsOfFile_usedEncoding_error_方法代码示例

本文整理汇总了Python中Foundation.NSString.stringWithContentsOfFile_usedEncoding_error_方法的典型用法代码示例。如果您正苦于以下问题:Python NSString.stringWithContentsOfFile_usedEncoding_error_方法的具体用法?Python NSString.stringWithContentsOfFile_usedEncoding_error_怎么用?Python NSString.stringWithContentsOfFile_usedEncoding_error_使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Foundation.NSString的用法示例。


在下文中一共展示了NSString.stringWithContentsOfFile_usedEncoding_error_方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _strings_entries_at_path

# 需要导入模块: from Foundation import NSString [as 别名]
# 或者: from Foundation.NSString import stringWithContentsOfFile_usedEncoding_error_ [as 别名]
def _strings_entries_at_path(path):
    
    content, nsencoding, error = NSString.stringWithContentsOfFile_usedEncoding_error_(path, None, None)
    if nsencoding == 0:
        sys.stderr.write("%s:0:0: error: could not determine file's encoding\n" % (path))
        exit(1)
        
    cfencoding = CFStringConvertNSStringEncodingToEncoding(nsencoding)
    iana_encoding = CFStringConvertEncodingToIANACharSetName(cfencoding)
    
    # Apple docs still say to use UTF-16, and that's what genstrings et al output
    if nsencoding != NSUTF8StringEncoding:
        sys.stderr.write("%s:0:0: warning: file is using %s encoding instead of utf-8\n" % (path, iana_encoding))
    
    entries = {}
    current_entry = None
    state = IN_COMMENT
    
    for line_number, line in enumerate(content.split("\n")):
        line = line.strip("\n")
        
        # make this 1-based everywhere
        line_number = line_number + 1
        
        if line.startswith("/*"):
            state = IN_COMMENT
        elif line.startswith("//"):
            state = SINGLE_LINE_COMMENT

        if state in (IN_COMMENT, SINGLE_LINE_COMMENT):
            
            if current_entry is None:
                current_entry = StringsEntry()
                current_entry.line_number = line_number
                
            current_entry.comment += line + "\n"
            current_entry.order = len(entries)
            
            # reset state for SINGLE_LINE_COMMENT also; next pass through the loop
            # can set it back, in case we have consecutive comments
            if line.endswith("*/") or state == SINGLE_LINE_COMMENT:
                state = IN_VALUE
                continue
            
        if state == IN_VALUE and len(line):
            key, ignored, value = line.partition("\" = \"")
            if key is None or len(key) == 0:
                sys.stderr.write("%s:%d:0: error: missing key in strings file\n" % (path, line_number))
                exit(1)
            if value is None or len(value) == 0:
                sys.stderr.write("%s:%d:0: error: missing value for key in strings file\n" % (path, line_number))
                exit(1)
            if current_entry is None:
                sys.stderr.write("%s:%d:0: error: missing comment in strings file\n" % (path, line_number))
                exit(1)
            assert current_entry.comment, "empty comment found"
            # strip leading quote
            key = _normalize_key(key[1:])
            current_entry.key = key
            # strip trailing semicolon and quote
            current_entry.value = value[:-2]
            entries[key] = current_entry
            current_entry = None            
            
    return iana_encoding, entries
开发者ID:cameozl,项目名称:tlutility,代码行数:67,代码来源:check_localized_strings.py

示例2: _strings_dictionary_at_path

# 需要导入模块: from Foundation import NSString [as 别名]
# 或者: from Foundation.NSString import stringWithContentsOfFile_usedEncoding_error_ [as 别名]
def _strings_dictionary_at_path(path):
    content, encoding, error = NSString.stringWithContentsOfFile_usedEncoding_error_(path, None, None)
    assert encoding == NSUTF8StringEncoding, "%s is not UTF-8 encoded" % (path)
    return content.propertyListFromStringsFileFormat()
开发者ID:cameozl,项目名称:tlutility,代码行数:6,代码来源:check_localized_strings.py


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