mercurial-translation-ja / filestat.py

#!/usr/bin/env python

import sys

files = {} # filename => (# of msgid, # of translated msgstr, # of fuzzy)

lineno = 0

infostock = ['', '', False, {}] # msgid, msgstr, fuzzy, filename map

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_value

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

def skip_value(value):
    pass

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[2] = True

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

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

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

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

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

for line in sys.stdin:
    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
        for filename in infostock[3].keys():
            filestat = files.setdefault(filename, [0, 0, 0])
            filestat[0] = filestat[0] + 1
            if 0 != len(infostock[1]): # already translated
                filestat[1] = filestat[1] + 1
            if infostock[2]: # fuzzy
                filestat[2] = filestat[2] + 1

        # clear information
        infostock = ['', '', False, {}]
        state = undefined_state

print ' %-38s msgid done(rest) fuzzy' % ('filename:')
for filename, stat in sorted(files.items()):
    print ('%-40s % 4d % 4d(% 4d) % 4d' %
           (filename, stat[0], stat[1], stat[0] - stat[1], stat[2]))
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.