Last active
August 29, 2015 13:55
-
-
Save hachibeeDI/8704380 to your computer and use it in GitHub Desktop.
require -> `pip install polib`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from __future__ import (print_function, division, absolute_import, unicode_literals, ) | |
import polib | |
from polib import POEntry | |
def __eq__(self, other): | |
return self.msgid == other.msgid and self.msgstr == other.msgstr | |
POEntry.__eq__ = __eq__ | |
try: | |
with open('ignored_id.txt', 'r') as f: | |
IGNORERABLE_PO = set(l.rstrip() for l in f.readlines()) | |
except IOError: | |
IGNORERABLE_PO = [] | |
def compare_po(x, y, strict=False): | |
''' | |
:param str x: filename to compare | |
:param str y: filename to compare | |
:param bool strict: If strict is True, error occurs when msgid is uneven. | |
''' | |
x_po = polib.pofile(x) | |
y_po = list(polib.pofile(y)) # cannot use `set` | |
if strict: | |
x_ids, y_ids = set([_x.msgid for _x in x_po]), set([_y.msgid for _y in y_po]) | |
if not x_ids == y_ids: | |
raise NoMatchedIdError(x_ids ^ y_ids) | |
for x_l in x_po: | |
if x_l in y_po and x_l.msgid not in IGNORERABLE_PO: | |
yield x_l | |
class NoMatchedIdError(Exception): | |
def __init__(self, missing_ids): | |
self.missing_ids = missing_ids | |
super(NoMatchedIdError, self).__init__('msgid is delete or modified!') | |
if __name__ == '__main__': | |
import sys | |
reload(sys) | |
sys.setdefaultencoding('utf-8') | |
desc = """ | |
Usage: (this cmd) org_file_path modified_file_path | |
If you want to assign id is have to ignore, put `ignored_id.txt` and write in msgid. | |
""" | |
# オプション増やす予定もないのでoptparserは使わない | |
if sys.argv[1] in ('-h', '--help'): | |
print(desc) | |
sys.exit(0) | |
x = sys.argv[1] | |
y = sys.argv[2] | |
output_format = 'id: {0}\nmsgstr: {1}\n' | |
try: | |
[print(output_format.format(z.msgid, z.msgstr)) for z in compare_po(x, y, strict=True)] | |
except NoMatchedIdError as e: | |
print(e) | |
print('id are:\n' + '\n'.join(e.missing_ids)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from __future__ import (print_function, division, absolute_import, unicode_literals, ) | |
import sys | |
sys.path.append('../') | |
import find_leftover_po # モンキーパッチを当てているのでこちらを先にimportする | |
import unittest | |
from os import path | |
import polib | |
from nose.tools import ok_, raises | |
suite = unittest.TestSuite() | |
loader = unittest.TestLoader() | |
def test_patch_applied(): | |
entry1 = polib.POEntry(msgid='id1', msgstr='str1') | |
entry1a = polib.POEntry(msgid='id1', msgstr='str1') | |
entry2 = polib.POEntry(msgid='id2', msgstr='str2') | |
ok_(entry1 == entry1a) | |
ok_(entry1 != entry2) | |
def test_find_leftover(use_strict=False): | |
org = path.abspath(path.join(path.dirname(__file__), 'd3web.po')) | |
changed = path.abspath(path.join(path.dirname(__file__), 'tes.po')) | |
results = list(find_leftover_po.compare_po(org, changed, use_strict)) | |
unchanged_ids = [r.msgid for r in results] | |
ok_(' is Failed' not in unchanged_ids) | |
ok_('16bit unsigned integer type' not in unchanged_ids) | |
@raises(find_leftover_po.NoMatchedIdError) | |
def test_id_missing_error(): | |
test_find_leftover(use_strict=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment