Source

mercurial-translation-ja / pickup.py

#!/usr/bin/env python

import sys

####################

lineno = 0

# 0      1       2       3              4      5
# msgid, msgstr, lineno, filesname map, fuzzy, wrapped lineno
infostock = ['', '', 0, {}, False, []]

####################

def undefined_state(value):
    raise 'unexpected invocation:%d: %s' % (lineno, value)

def wait_msgid(value):
    if 0 != len(value):
        global state
        infostock[0] = value
        state = skip_msgid

def wait_msgstr(value):
    if 0 != len(value):
        global state
        infostock[1] = value
        state = skip_msgstr

def skip_msgid(value):
    pass

def skip_msgstr(value):
    if not infostock[1].endswith('\\n'):
        infostock[5].append(lineno - 1)
    infostock[1] = value

state = undefined_state

####################

def comment_strategy(line):
    if line.startswith(':'):
        line = line[1:]
        for entry in line.split():
            entry = entry.strip()
            if 0 == len(entry): continue
            filename, lineno = entry.split(':')
            filenamemap = infostock[3]
            filenamemap[filename]  = filename
    else:
        for entry in line.split(','):
            if entry.strip() == 'fuzzy':
                infostock[4] = True

def msgid_strategy(line):
    id = line[1:-1] # eliminate quotations
    if 0 != len(id):
        global state
        infostock[0] = id
        state = skip_msgid
    else:
        state = wait_msgid

def msgstr_strategy(line):
    infostock[2] = lineno
    str = line[1:-1] # eliminate quotations
    if 0 != len(str):
        global state
        infostock[1] = str
        state = skip_msgstr
    else:
        state = wait_msgstr

def value_strategy(line):
    str = line[:-1] # eliminate tail quotation
    state(str)

top_strategies = [
    ('#', comment_strategy),
    ('msgid ', msgid_strategy),
    ('msgstr ', msgstr_strategy),
    ('"', value_strategy),
]

####################

if 2 < len(sys.argv):
    expecteds = sys.argv[2:]

    def checkargs(filenames):
        if not expecteds: return True
        for filename in filenames:
            if filename in expecteds: return True
        return False

    interestedin = checkargs
else:
    interestedin = lambda(x): True

if (1 < len(sys.argv)) and ('-' != sys.argv[1]):
    filename = sys.argv[1]
    infile = open(filename)
else:
    filename = '<stdin>'
    infile = sys.stdin

####################

for line in infile:
    line = line.strip()
    lineno += 1

    for prefix, strategy in top_strategies:
        if line.startswith(prefix):
            line = line[len(prefix):] # trim off
            strategy(line)
            break
    else:
        if 0 != len(line):
            raise 'unexpected line: %s' % line

        if 0 == len(infostock[0]):
            continue

        # empty line flush current status
        
        if interestedin(infostock[3].keys()):
            msgs = []
            if 0 == len(infostock[1]):
                msgs.append('not yet translated')
            if infostock[4]:
                msgs.append('fuzzy msgid')
            if infostock[5]:
                detail = '\n'.join(['%s:%d:wrapped here' % (filename, n)
                                    for n in infostock[5]])
                msgs.append('unexpected wrap in msgstr\n%s' % detail)

            for msg in msgs:
                print '%s:%d:%s' % (filename, infostock[2], msg)

        # clear information
        infostock = ['', '', 0, {}, False, []]
        state = undefined_state
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.