コード例 #1
0
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
コード例 #2
0
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
コード例 #3
0
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()
コード例 #4
0
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()