2013-11-18 09:38:53 +00:00
|
|
|
|
#!/usr/bin/env python3
|
2012-07-02 19:51:06 +00:00
|
|
|
|
|
|
|
|
|
# ***** BEGIN GPL LICENSE BLOCK *****
|
|
|
|
|
#
|
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
|
# along with this program; if not, write to the Free Software Foundation,
|
|
|
|
|
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
#
|
|
|
|
|
# ***** END GPL LICENSE BLOCK *****
|
|
|
|
|
|
|
|
|
|
# <pep8 compliant>
|
|
|
|
|
|
|
|
|
|
# Merge one or more .po files into the first dest one.
|
2012-07-29 12:07:06 +00:00
|
|
|
|
# If a msgkey is present in more than one merged po, the one in the first file wins, unless
|
2012-07-02 19:51:06 +00:00
|
|
|
|
# it’s marked as fuzzy and one later is not.
|
|
|
|
|
# The fuzzy flag is removed if necessary.
|
|
|
|
|
# All other comments are never modified.
|
|
|
|
|
# However, commented messages in dst will always remain commented, and commented messages are
|
|
|
|
|
# never merged from sources.
|
|
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
|
2012-07-08 17:10:10 +00:00
|
|
|
|
try:
|
2012-07-18 14:03:10 +00:00
|
|
|
|
import settings
|
2012-07-08 17:10:10 +00:00
|
|
|
|
import utils
|
|
|
|
|
except:
|
2012-07-18 14:03:10 +00:00
|
|
|
|
from . import (settings, utils)
|
2012-07-02 19:51:06 +00:00
|
|
|
|
|
|
|
|
|
|
2013-01-12 16:49:06 +00:00
|
|
|
|
# XXX This is a quick hack to make it work with new I18n... objects! To be reworked!
|
2012-07-02 19:51:06 +00:00
|
|
|
|
def main():
|
|
|
|
|
import argparse
|
2013-01-12 16:49:06 +00:00
|
|
|
|
parser = argparse.ArgumentParser(description=""
|
|
|
|
|
"Merge one or more .po files into the first dest one.\n"
|
|
|
|
|
"If a msgkey (msgctxt, msgid) is present in more than one merged po, the one in the first file "
|
|
|
|
|
"wins, unless it’s marked as fuzzy and one later is not.\n"
|
|
|
|
|
"The fuzzy flag is removed if necessary.\n"
|
|
|
|
|
"All other comments are never modified.\n"
|
|
|
|
|
"Commented messages in dst will always remain commented, and commented messages are never merged "
|
2012-07-02 19:51:06 +00:00
|
|
|
|
"from sources.")
|
2013-01-12 16:49:06 +00:00
|
|
|
|
parser.add_argument('-s', '--stats', action="store_true", help="Show statistics info.")
|
2012-07-02 19:51:06 +00:00
|
|
|
|
parser.add_argument('-r', '--replace', action="store_true",
|
|
|
|
|
help="Replace existing messages of same \"level\" already in dest po.")
|
2013-01-12 16:49:06 +00:00
|
|
|
|
parser.add_argument('dst', metavar='dst.po', help="The dest po into which merge the others.")
|
|
|
|
|
parser.add_argument('src', metavar='src.po', nargs='+', help="The po's to merge into the dst.po one.")
|
2012-07-02 19:51:06 +00:00
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
ret = 0
|
|
|
|
|
done_msgkeys = set()
|
|
|
|
|
done_fuzzy_msgkeys = set()
|
|
|
|
|
nbr_merged = 0
|
|
|
|
|
nbr_replaced = 0
|
|
|
|
|
nbr_added = 0
|
|
|
|
|
nbr_unfuzzied = 0
|
|
|
|
|
|
2013-01-12 16:49:06 +00:00
|
|
|
|
dst_msgs = utils.I18nMessages(kind='PO', src=args.dst)
|
|
|
|
|
if dst_msgs.parsing_errors:
|
2012-07-02 19:51:06 +00:00
|
|
|
|
print("Dest po is BROKEN, aborting.")
|
|
|
|
|
return 1
|
|
|
|
|
if args.stats:
|
|
|
|
|
print("Dest po, before merging:")
|
2013-01-12 16:49:06 +00:00
|
|
|
|
dst_msgs.print_stats(prefix="\t")
|
|
|
|
|
# If we don’t want to replace existing valid translations, pre-populate done_msgkeys and done_fuzzy_msgkeys.
|
2012-07-02 19:51:06 +00:00
|
|
|
|
if not args.replace:
|
2013-01-12 16:49:06 +00:00
|
|
|
|
done_msgkeys = dst_msgs.trans_msgs.copy()
|
|
|
|
|
done_fuzzy_msgkeys = dst_msgs.fuzzy_msgs.copy()
|
2012-07-02 19:51:06 +00:00
|
|
|
|
for po in args.src:
|
2013-01-12 16:49:06 +00:00
|
|
|
|
msgs = utils.I18nMessages(kind='PO', src=po)
|
|
|
|
|
if msgs.parsing_errors:
|
2012-07-02 19:51:06 +00:00
|
|
|
|
print("\tSrc po {} is BROKEN, skipping.".format(po))
|
|
|
|
|
ret = 1
|
|
|
|
|
continue
|
|
|
|
|
print("\tMerging {}...".format(po))
|
|
|
|
|
if args.stats:
|
|
|
|
|
print("\t\tMerged po stats:")
|
2013-01-12 16:49:06 +00:00
|
|
|
|
msgs.print_stats(prefix="\t\t\t")
|
|
|
|
|
for msgkey, msg in msgs.msgs.items():
|
2012-07-02 19:51:06 +00:00
|
|
|
|
msgctxt, msgid = msgkey
|
|
|
|
|
# This msgkey has already been completely merged, or is a commented one,
|
|
|
|
|
# or the new message is commented, skip it.
|
2013-01-12 16:49:06 +00:00
|
|
|
|
if msgkey in (done_msgkeys | dst_msgs.comm_msgs | msgs.comm_msgs):
|
2012-07-02 19:51:06 +00:00
|
|
|
|
continue
|
2013-01-12 16:49:06 +00:00
|
|
|
|
is_ttip = msg.is_tooltip
|
2012-07-02 19:51:06 +00:00
|
|
|
|
# New messages does not yet exists in dest.
|
2013-01-12 16:49:06 +00:00
|
|
|
|
if msgkey not in dst_msgs.msgs:
|
|
|
|
|
dst_msgs[msgkey] = msgs.msgs[msgkey]
|
|
|
|
|
if msgkey in msgs.fuzzy_msgs:
|
2012-07-02 19:51:06 +00:00
|
|
|
|
done_fuzzy_msgkeys.add(msgkey)
|
2013-01-12 16:49:06 +00:00
|
|
|
|
dst_msgs.fuzzy_msgs.add(msgkey)
|
|
|
|
|
elif msgkey in msgs.trans_msgs:
|
2012-07-02 19:51:06 +00:00
|
|
|
|
done_msgkeys.add(msgkey)
|
2013-01-12 16:49:06 +00:00
|
|
|
|
dst_msgs.trans_msgs.add(msgkey)
|
2012-07-02 19:51:06 +00:00
|
|
|
|
nbr_added += 1
|
|
|
|
|
# From now on, the new messages is already in dst.
|
|
|
|
|
# New message is neither translated nor fuzzy, skip it.
|
2013-01-12 16:49:06 +00:00
|
|
|
|
elif msgkey not in (msgs.trans_msgs | msgs.fuzzy_msgs):
|
2012-07-02 19:51:06 +00:00
|
|
|
|
continue
|
|
|
|
|
# From now on, the new message is either translated or fuzzy!
|
|
|
|
|
# The new message is translated.
|
2013-01-12 16:49:06 +00:00
|
|
|
|
elif msgkey in msgs.trans_msgs:
|
|
|
|
|
dst_msgs.msgs[msgkey].msgstr = msg.msgstr
|
2012-07-02 19:51:06 +00:00
|
|
|
|
done_msgkeys.add(msgkey)
|
|
|
|
|
done_fuzzy_msgkeys.discard(msgkey)
|
2013-01-12 16:49:06 +00:00
|
|
|
|
if msgkey in dst_msgs.fuzzy_msgs:
|
|
|
|
|
dst_msgs.fuzzy_msgs.remove(msgkey)
|
2012-07-02 19:51:06 +00:00
|
|
|
|
nbr_unfuzzied += 1
|
2013-01-12 16:49:06 +00:00
|
|
|
|
if msgkey not in dst_msgs.trans_msgs:
|
|
|
|
|
dst_msgs.trans_msgs.add(msgkey)
|
2012-07-02 19:51:06 +00:00
|
|
|
|
else:
|
|
|
|
|
nbr_replaced += 1
|
|
|
|
|
nbr_merged += 1
|
2013-01-12 16:49:06 +00:00
|
|
|
|
# The new message is fuzzy, org one is fuzzy too, and this msgkey has not yet been merged.
|
|
|
|
|
elif msgkey not in (dst_msgs.trans_msgs | done_fuzzy_msgkeys):
|
|
|
|
|
dst_msgs[msgkey].msgstr = msg.msgstr
|
2012-07-02 19:51:06 +00:00
|
|
|
|
done_fuzzy_msgkeys.add(msgkey)
|
2013-01-12 16:49:06 +00:00
|
|
|
|
dst_msgs.fuzzy_msgs.add(msgkey)
|
2012-07-02 19:51:06 +00:00
|
|
|
|
nbr_merged += 1
|
|
|
|
|
nbr_replaced += 1
|
|
|
|
|
|
2013-01-12 16:49:06 +00:00
|
|
|
|
dst_msgs.write(kind='PO', dest=args.dst)
|
2012-07-02 19:51:06 +00:00
|
|
|
|
|
2013-01-12 16:49:06 +00:00
|
|
|
|
print("Merged completed. {} messages were merged (among which {} were replaced), {} were added, "
|
|
|
|
|
"{} were \"un-fuzzied\".".format(nbr_merged, nbr_replaced, nbr_added, nbr_unfuzzied))
|
2012-07-02 19:51:06 +00:00
|
|
|
|
if args.stats:
|
2013-01-12 16:49:06 +00:00
|
|
|
|
dst_msgs.update_info()
|
2012-07-02 19:51:06 +00:00
|
|
|
|
print("Final merged po stats:")
|
2013-01-12 16:49:06 +00:00
|
|
|
|
dst_msgs.print_stats(prefix="\t")
|
2012-07-02 19:51:06 +00:00
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
print("\n\n *** Running {} *** \n".format(__file__))
|
|
|
|
|
sys.exit(main())
|