def pproc_newcontext_po(po, pot_messages, pot_stats):
    print("Adding new contexts to {}...".format(po))
    messages, state, stats = utils.parse_messages(po)
    known_ctxt = stats["contexts"]
    print("Already known (present) context(s): {}".format(str(known_ctxt)))

    new_ctxt = set()
    added = 0
    # Only use valid already translated messages!
    allowed_keys = state["trans_msg"] - state["fuzzy_msg"] - state["comm_msg"]
    for key in pot_messages.keys():
        ctxt, msgid = key
        if ctxt in known_ctxt:
            continue
        new_ctxt.add(ctxt)
        for t_ctxt in known_ctxt:
            # XXX The first match will win, this might not be optimal...
            t_key = (t_ctxt, msgid)
            if t_key in allowed_keys:
                # Wrong comments (sources) will be removed by msgmerge...
                messages[key] = messages[t_key]
                messages[key]["msgctxt_lines"] = [ctxt]
                added += 1

    utils.write_messages(po, messages, state["comm_msg"], state["fuzzy_msg"])
    print("Finished!\n    {} new context(s) was/were added {}, adding {} new "
          "messages.\n".format(len(new_ctxt), str(new_ctxt), added))
    return 0
Example #2
0
def pproc_newcontext_po(po, pot_messages, pot_stats):
    print("Adding new contexts to {}...".format(po))
    messages, state, stats = utils.parse_messages(po)
    known_ctxt = stats["contexts"]
    print("Already known (present) context(s): {}".format(str(known_ctxt)))

    new_ctxt = set()
    added = 0
    # Only use valid already translated messages!
    allowed_keys = state["trans_msg"] - state["fuzzy_msg"] - state["comm_msg"]
    for key in pot_messages.keys():
        ctxt, msgid = key
        if ctxt in known_ctxt:
            continue
        new_ctxt.add(ctxt)
        for t_ctxt in known_ctxt:
            # XXX The first match will win, this might not be optimal...
            t_key = (t_ctxt, msgid)
            if t_key in allowed_keys:
                # Wrong comments (sources) will be removed by msgmerge...
                messages[key] = messages[t_key]
                messages[key]["msgctxt_lines"] = [ctxt]
                added += 1

    utils.write_messages(po, messages, state["comm_msg"], state["fuzzy_msg"])
    print("Finished!\n    {} new context(s) was/were added {}, adding {} new "
          "messages.\n".format(len(new_ctxt), str(new_ctxt), added))
    return 0
def do_clean(po, strict):
    print("Cleaning {}...".format(po))
    messages, states, u1 = utils.parse_messages(po)

    if strict and states["is_broken"]:
        print("ERROR! This .po file is broken!")
        return 1

    for msgkey in states["comm_msg"]:
        del messages[msgkey]
    utils.write_messages(po, messages, states["comm_msg"], states["fuzzy_msg"])
    print("Removed {} commented messages.".format(len(states["comm_msg"])))
    return 0
def do_clean(po, strict):
    print("Cleaning {}...".format(po))
    messages, states, u1 = utils.parse_messages(po)

    if strict and states["is_broken"]:
        print("ERROR! This .po file is broken!")
        return 1

    for msgkey in states["comm_msg"]:
        del messages[msgkey]
    utils.write_messages(po, messages, states["comm_msg"], states["fuzzy_msg"])
    print("Removed {} commented messages.".format(len(states["comm_msg"])))
    return 0
Example #5
0
def process_po(ref_messages, po, glob_stats, do_stats, do_messages):
    print("Checking {}...".format(po))
    ret = 0

    messages, states, stats = utils.parse_messages(po)
    if do_messages:
        t = print_diff(ref_messages, messages, states)
        if t:
            ret = t
    if do_stats:
        print("\tStats:")
        t = utils.print_stats(stats, glob_stats, prefix="        ")
        if t:
            ret = t
    if states["is_broken"]:
        print("\tERROR! This .po is broken!")
        ret = 1
    return ret
def process_po(ref_messages, po, glob_stats, do_stats, do_messages):
    print("Checking {}...".format(po))
    ret = 0

    messages, states, stats = utils.parse_messages(po)
    if do_messages:
        t = print_diff(ref_messages, messages, states)
        if t:
            ret = t
    if do_stats:
        print("\tStats:")
        t = utils.print_stats(stats, glob_stats, prefix="        ")
        if t:
            ret = t
    if states["is_broken"]:
        print("\tERROR! This .po is broken!")
        ret = 1
    return ret
def main():
    import argparse
    parser = argparse.ArgumentParser(description="" \
                    "Preprocesses right-to-left languages.\n" \
                    "You can use it either standalone, or through " \
                    "import_po_from_branches or update_trunk.\n\n" \
                    "Note: This has been tested on Linux, not 100% it will " \
                    "work nicely on Windows or OsX.\n" \
                    "Note: This uses ctypes, as there is no py3 binding for " \
                    "fribidi currently. This implies you only need the " \
                    "compiled C library to run it.\n" \
                    "Note: It handles some formating/escape codes (like " \
                    "\\\", %s, %x12, %.4f, etc.), protecting them from ugly " \
                    "(evil) fribidi, which seems completely unaware of such " \
                    "things (as unicode is...).")
    parser.add_argument('dst', metavar='dst.po',
                        help="The dest po into which write the " \
                             "pre-processed messages.")
    parser.add_argument('src',
                        metavar='src.po',
                        help="The po's to pre-process messages.")
    args = parser.parse_args()

    msgs, state, u1 = utils.parse_messages(args.src)
    if state["is_broken"]:
        print("Source po is BROKEN, aborting.")
        return 1

    keys = []
    trans = []
    for key, val in msgs.items():
        keys.append(key)
        trans.append("".join(val["msgstr_lines"]))
    trans = log2vis(trans)
    for key, trn in zip(keys, trans):
        # Mono-line for now...
        msgs[key]["msgstr_lines"] = [trn]

    utils.write_messages(args.dst, msgs, state["comm_msg"], state["fuzzy_msg"])

    print("RTL pre-process completed.")
    return 0
def main():
    import argparse
    parser = argparse.ArgumentParser(description="" \
                    "Preprocesses right-to-left languages.\n" \
                    "You can use it either standalone, or through " \
                    "import_po_from_branches or update_trunk.\n\n" \
                    "Note: This has been tested on Linux, not 100% it will " \
                    "work nicely on Windows or OsX.\n" \
                    "Note: This uses ctypes, as there is no py3 binding for " \
                    "fribidi currently. This implies you only need the " \
                    "compiled C library to run it.\n" \
                    "Note: It handles some formating/escape codes (like " \
                    "\\\", %s, %x12, %.4f, etc.), protecting them from ugly " \
                    "(evil) fribidi, which seems completely unaware of such " \
                    "things (as unicode is...).")
    parser.add_argument('dst', metavar='dst.po',
                        help="The dest po into which write the " \
                             "pre-processed messages.")
    parser.add_argument('src', metavar='src.po',
                        help="The po's to pre-process messages.")
    args = parser.parse_args()


    msgs, state, u1 = utils.parse_messages(args.src)
    if state["is_broken"]:
        print("Source po is BROKEN, aborting.")
        return 1

    keys = []
    trans = []
    for key, val in msgs.items():
        keys.append(key)
        trans.append("".join(val["msgstr_lines"]))
    trans = log2vis(trans)
    for key, trn in zip(keys, trans):
        # Mono-line for now...
        msgs[key]["msgstr_lines"] = [trn]

    utils.write_messages(args.dst, msgs, state["comm_msg"], state["fuzzy_msg"])

    print("RTL pre-process completed.")
    return 0
Example #9
0
def main():
    import argparse
    parser = argparse.ArgumentParser(description="Import advanced enough po’s " \
                                                 "from branches to trunk.")
    parser.add_argument('-t', '--threshold', type=int,
                        help="Import threshold, as a percentage.")
    parser.add_argument('-s', '--strict', action="store_true",
                        help="Raise an error if a po is broken.")
    parser.add_argument('langs', metavar='ISO_code', nargs='*',
                        help="Restrict processed languages to those.")
    args = parser.parse_args()

    ret = 0

    threshold = float(settings.IMPORT_MIN_LEVEL) / 100.0
    if args.threshold is not None:
        threshold = float(args.threshold) / 100.0

    for lang in os.listdir(BRANCHES_DIR):
        if args.langs and lang not in args.langs:
            continue
        po = os.path.join(BRANCHES_DIR, lang, ".".join((lang, "po")))
        if os.path.exists(po):
            po_is_rtl = os.path.join(BRANCHES_DIR, lang, RTL_PREPROCESS_FILE)
            msgs, state, stats = utils.parse_messages(po)
            tot_msgs = stats["tot_msg"]
            trans_msgs = stats["trans_msg"]
            lvl = 0.0
            if tot_msgs:
                lvl = float(trans_msgs) / float(tot_msgs)
            if lvl > threshold:
                if state["is_broken"] and args.strict:
                    print("{:<10}: {:>6.1%} done, but BROKEN, skipped." \
                          "".format(lang, lvl))
                    ret = 1
                else:
                    if os.path.exists(po_is_rtl):
                        out_po = os.path.join(TRUNK_PO_DIR,
                                              ".".join((lang, "po")))
                        out_raw_po = os.path.join(TRUNK_PO_DIR,
                                                  "_".join((lang, "raw.po")))
                        keys = []
                        trans = []
                        for k, m in msgs.items():
                            keys.append(k)
                            trans.append("".join(m["msgstr_lines"]))
                        trans = rtl_preprocess.log2vis(trans)
                        for k, t in zip(keys, trans):
                            # Mono-line for now...
                            msgs[k]["msgstr_lines"] = [t]
                        utils.write_messages(out_po, msgs, state["comm_msg"],
                                             state["fuzzy_msg"])
                        # Also copies org po!
                        shutil.copy(po, out_raw_po)
                        print("{:<10}: {:>6.1%} done, enough translated " \
                              "messages, processed and copied to trunk." \
                              "".format(lang, lvl))
                    else:
                        shutil.copy(po, TRUNK_PO_DIR)
                        print("{:<10}: {:>6.1%} done, enough translated " \
                              "messages, copied to trunk.".format(lang, lvl))
            else:
                if state["is_broken"] and args.strict:
                    print("{:<10}: {:>6.1%} done, BROKEN and not enough " \
                          "translated messages, skipped".format(lang, lvl))
                    ret = 1
                else:
                    print("{:<10}: {:>6.1%} done, not enough translated " \
                          "messages, skipped.".format(lang, lvl))
    return ret
Example #10
0
def main():
    import argparse
    parser = argparse.ArgumentParser(description="Check po’s in branches " \
                                                 "(or in trunk) for missing" \
                                                 "/unneeded messages.")
    parser.add_argument('-s',
                        '--stats',
                        action="store_true",
                        help="Print po’s stats.")
    parser.add_argument('-m',
                        '--messages',
                        action="store_true",
                        help="Print po’s missing/unneeded/commented messages.")
    parser.add_argument('-t',
                        '--trunk',
                        action="store_true",
                        help="Check po’s in /trunk/po rather than /branches.")
    parser.add_argument('-p',
                        '--pot',
                        help="Specify the .pot file used as reference.")
    parser.add_argument('langs',
                        metavar='ISO_code',
                        nargs='*',
                        help="Restrict processed languages to those.")
    args = parser.parse_args()

    if args.pot:
        global FILE_NAME_POT
        FILE_NAME_POT = args.pot
    glob_stats = {
        "nbr": 0.0,
        "lvl": 0.0,
        "lvl_ttips": 0.0,
        "lvl_trans_ttips": 0.0,
        "lvl_ttips_in_trans": 0.0,
        "lvl_comm": 0.0,
        "nbr_signs": 0,
        "nbr_trans_signs": 0,
        "contexts": set()
    }
    ret = 0

    pot_messages = None
    if args.messages:
        pot_messages, u1, pot_stats = utils.parse_messages(FILE_NAME_POT)
        pot_messages = set(pot_messages.keys())
        glob_stats["nbr_signs"] = pot_stats["nbr_signs"]

    if args.langs:
        for lang in args.langs:
            if args.trunk:
                po = os.path.join(TRUNK_PO_DIR, ".".join((lang, "po")))
            else:
                po = os.path.join(BRANCHES_DIR, lang, ".".join((lang, "po")))
            if os.path.exists(po):
                t = process_po(pot_messages, po, glob_stats, args.stats,
                               args.messages)
                if t:
                    ret = t
    elif args.trunk:
        for po in os.listdir(TRUNK_PO_DIR):
            if po.endswith(".po"):
                po = os.path.join(TRUNK_PO_DIR, po)
                t = process_po(pot_messages, po, glob_stats, args.stats,
                               args.messages)
                if t:
                    ret = t
    else:
        for lang in os.listdir(BRANCHES_DIR):
            for po in os.listdir(os.path.join(BRANCHES_DIR, lang)):
                if po.endswith(".po"):
                    po = os.path.join(BRANCHES_DIR, lang, po)
                    t = process_po(pot_messages, po, glob_stats, args.stats,
                                   args.messages)
                    if t:
                        ret = t

    if args.stats and glob_stats["nbr"] != 0.0:
        nbr_contexts = len(glob_stats["contexts"] - {""})
        if nbr_contexts != 1:
            if nbr_contexts == 0:
                nbr_contexts = "No"
            _ctx_txt = "s are"
        else:
            _ctx_txt = " is"
        print(
            "\nAverage stats for all {:.0f} processed files:\n"
            "    {:>6.1%} done!\n"
            "    {:>6.1%} of messages are tooltips.\n"
            "    {:>6.1%} of tooltips are translated.\n"
            "    {:>6.1%} of translated messages are tooltips.\n"
            "    {:>6.1%} of messages are commented.\n"
            "    The org msgids are currently made of {} signs.\n"
            "    All processed translations are currently made of {} signs.\n"
            "    {} specific context{} present:\n            {}\n"
            "".format(glob_stats["nbr"], glob_stats["lvl"] / glob_stats["nbr"],
                      glob_stats["lvl_ttips"] / glob_stats["nbr"],
                      glob_stats["lvl_trans_ttips"] / glob_stats["nbr"],
                      glob_stats["lvl_ttips_in_trans"] / glob_stats["nbr"],
                      glob_stats["lvl_comm"] / glob_stats["nbr"],
                      glob_stats["nbr_signs"], glob_stats["nbr_trans_signs"],
                      nbr_contexts, _ctx_txt,
                      "\n            ".join(glob_stats["contexts"] - {""})))

    return ret
def main():
    import argparse
    parser = argparse.ArgumentParser(description="Write out messages.txt "
                                                 "from Blender.")
    parser.add_argument('-t', '--trunk', action="store_true",
                        help="Update po’s in /trunk/po rather than /branches.")
    parser.add_argument('-i', '--input', metavar="File",
                        help="Input pot file path.")
    parser.add_argument('--pproc-contexts', action="store_true",
                        help="Pre-process po’s to avoid having plenty of "
                             "fuzzy msgids just because a context was "
                             "added/changed!")
    parser.add_argument('-a', '--add', action="store_true",
                        help="Add missing po’s (useful only when one or "
                             "more languages are given!).")
    parser.add_argument('langs', metavar='ISO_code', nargs='*',
                        help="Restrict processed languages to those.")
    args = parser.parse_args()

    if args.input:
        global FILE_NAME_POT
        FILE_NAME_POT = args.input
    ret = 0

    if args.pproc_contexts:
        _ctxt_proc = pproc_newcontext_po
        pot_messages, _a, pot_stats = utils.parse_messages(FILE_NAME_POT)
    else:
        _ctxt_proc = lambda a, b, c: 0
        pot_messages, pot_stats = None, None

    if args.langs:
        for lang in args.langs:
            if args.trunk:
                dr = TRUNK_PO_DIR
                po = os.path.join(dr, ".".join((lang, "po")))
            else:
                dr = os.path.join(BRANCHES_DIR, lang)
                po = os.path.join(dr, ".".join((lang, "po")))
            if args.add:
                if not os.path.exists(dr):
                    os.makedirs(dr)
                if not os.path.exists(po):
                    shutil.copy(FILE_NAME_POT, po)
            if args.add or os.path.exists(po):
                t = _ctxt_proc(po, pot_messages, pot_stats)
                if t:
                    ret = t
                t = process_po(po, lang)
                if t:
                    ret = t
    elif args.trunk:
        for po in os.listdir(TRUNK_PO_DIR):
            if po.endswith(".po"):
                lang = os.path.basename(po)[:-3]
                po = os.path.join(TRUNK_PO_DIR, po)
                t = _ctxt_proc(po, pot_messages, pot_stats)
                if t:
                    ret = t
                t = process_po(po, lang)
                if t:
                    ret = t
    else:
        for lang in os.listdir(BRANCHES_DIR):
            po = os.path.join(BRANCHES_DIR, lang, ".".join((lang, "po")))
            if os.path.exists(po):
                t = _ctxt_proc(po, pot_messages, pot_stats)
                if t:
                    ret = t
                t = process_po(po, lang)
                if t:
                    ret = t

    return ret
def main():
    import argparse
    parser = argparse.ArgumentParser(
        description=""
        "Update 'languages' text file used by Blender at runtime to build translations menu."
    )
    parser.add_argument('-m',
                        '--min_translation',
                        type=int,
                        default=-100,
                        help="Minimum level of translation, as a percentage "
                        "(translations below this are commented out in menu).")
    parser.add_argument(
        'langs',
        metavar='ISO_code',
        nargs='*',
        help="Unconditionally exclude those languages from the menu.")
    args = parser.parse_args()

    ret = 0
    min_trans = args.min_translation / 100.0
    forbidden = set(args.langs)
    # 'DEFAULT' and en_US are always valid, fully-translated "languages"!
    stats = {"DEFAULT": 1.0, "en_US": 1.0}

    # Get the "done level" of each po in trunk...
    for po in os.listdir(TRUNK_PO_DIR):
        if po.endswith(".po") and not po.endswith("_raw.po"):
            lang = os.path.basename(po)[:-3]
            u1, u2, _stats = utils.parse_messages(
                os.path.join(TRUNK_PO_DIR, po))
            stats[lang] = _stats["trans_msg"] / _stats["tot_msg"]

    # Generate languages file used by Blender's i18n system.
    # First, match all entries in LANGUAGES to a lang in stats, if possible!
    stats = find_matching_po(LANGUAGES, stats, forbidden)
    limits = sorted(LANGUAGES_CATEGORIES, key=lambda it: it[0], reverse=True)
    idx = 0
    stats = sorted(stats, key=lambda it: it[0], reverse=True)
    langs_cats = [[] for i in range(len(limits))]
    highest_uid = 0
    for prop, uid, label, key, flag in stats:
        if prop < limits[idx][0]:
            # Sub-sort languages by iso-codes.
            langs_cats[idx].sort(key=lambda it: it[2])
            idx += 1
        if prop < min_trans and flag == OK:
            flag = TOOLOW
        langs_cats[idx].append((uid, label, key, flag))
        if abs(uid) > highest_uid:
            highest_uid = abs(uid)
    # Sub-sort last group of languages by iso-codes!
    langs_cats[idx].sort(key=lambda it: it[2])
    with open(os.path.join(TRUNK_MO_DIR, LANGUAGES_FILE),
              'w',
              encoding="utf-8") as f:
        f.write(
            "# File used by Blender to know which languages (translations) are available, \n"
        )
        f.write("# and to generate translation menu.\n")
        f.write("#\n")
        f.write("# File format:\n")
        f.write("# ID:MENULABEL:ISOCODE\n")
        f.write(
            "# ID must be unique, except for 0 value (marks categories for menu).\n"
        )
        f.write("# Line starting with a # are comments!\n")
        f.write("#\n")
        f.write(
            "# Automatically generated by bl_i18n_utils/update_languages_menu.py script.\n"
        )
        f.write("# Highest ID currently in use: {}\n".format(highest_uid))
        for cat, langs_cat in zip(limits, langs_cats):
            f.write("#\n")
            # Write "category menu label"...
            if langs_cat:
                f.write("0:{}::\n".format(cat[1]))
            else:
                # Do not write the category if it has no language!
                f.write("# Void category! #0:{}:\n".format(cat[1]))
            # ...and all matching language entries!
            for uid, label, key, flag in langs_cat:
                if flag == OK:
                    f.write("{}:{}:{}\n".format(uid, label, key))
                else:
                    # Non-existing, commented entry!
                    f.write("# {} #{}:{}:{}\n".format(FLAG_MESSAGES[flag], uid,
                                                      label, key))
def main():
    import argparse
    parser = argparse.ArgumentParser(description=""
                        "Update 'languages' text file used by Blender at runtime to build translations menu.")
    parser.add_argument('-m', '--min_translation', type=int, default=-100,
                        help="Minimum level of translation, as a percentage "
                             "(translations below this are commented out in menu).")
    parser.add_argument('langs', metavar='ISO_code', nargs='*',
                        help="Unconditionally exclude those languages from the menu.")
    args = parser.parse_args()

    ret = 0
    min_trans = args.min_translation / 100.0
    forbidden = set(args.langs)
    # 'DEFAULT' and en_US are always valid, fully-translated "languages"!
    stats = {"DEFAULT": 1.0, "en_US": 1.0}

    # Get the "done level" of each po in trunk...
    for po in os.listdir(TRUNK_PO_DIR):
        if po.endswith(".po") and not po.endswith("_raw.po"):
            lang = os.path.basename(po)[:-3]
            u1, u2, _stats = utils.parse_messages(os.path.join(TRUNK_PO_DIR, po))
            stats[lang] = _stats["trans_msg"] / _stats["tot_msg"]

    # Generate languages file used by Blender's i18n system.
    # First, match all entries in LANGUAGES to a lang in stats, if possible!
    stats = find_matching_po(LANGUAGES, stats, forbidden)
    limits = sorted(LANGUAGES_CATEGORIES, key=lambda it: it[0], reverse=True)
    idx = 0
    stats = sorted(stats, key=lambda it: it[0], reverse=True)
    langs_cats = [[] for i in range(len(limits))]
    highest_uid = 0
    for prop, uid, label, key, flag in stats:
        if prop < limits[idx][0]:
            # Sub-sort languages by iso-codes.
            langs_cats[idx].sort(key=lambda it: it[2])
            idx += 1
        if prop < min_trans and flag == OK:
            flag = TOOLOW
        langs_cats[idx].append((uid, label, key, flag))
        if abs(uid) > highest_uid:
            highest_uid = abs(uid)
    # Sub-sort last group of languages by iso-codes!
    langs_cats[idx].sort(key=lambda it: it[2])
    with open(os.path.join(TRUNK_MO_DIR, LANGUAGES_FILE), 'w', encoding="utf-8") as f:
        f.write("# File used by Blender to know which languages (translations) are available, \n")
        f.write("# and to generate translation menu.\n")
        f.write("#\n")
        f.write("# File format:\n")
        f.write("# ID:MENULABEL:ISOCODE\n")
        f.write("# ID must be unique, except for 0 value (marks categories for menu).\n")
        f.write("# Line starting with a # are comments!\n")
        f.write("#\n")
        f.write("# Automatically generated by bl_i18n_utils/update_languages_menu.py script.\n")
        f.write("# Highest ID currently in use: {}\n".format(highest_uid))
        for cat, langs_cat in zip(limits, langs_cats):
            f.write("#\n")
            # Write "category menu label"...
            if langs_cat:
                f.write("0:{}::\n".format(cat[1]))
            else:
                # Do not write the category if it has no language!
                f.write("# Void category! #0:{}:\n".format(cat[1]))
            # ...and all matching language entries!
            for uid, label, key, flag in langs_cat:
                if flag == OK:
                    f.write("{}:{}:{}\n".format(uid, label, key))
                else:
                    # Non-existing, commented entry!
                    f.write("# {} #{}:{}:{}\n".format(FLAG_MESSAGES[flag], uid, label, key))
Example #14
0
def main():
    import argparse
    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 " \
                    "from sources.")
    parser.add_argument('-s', '--stats', action="store_true",
                        help="Show statistics info.")
    parser.add_argument('-r', '--replace', action="store_true",
                        help="Replace existing messages of same \"level\" already in dest po.")
    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.")
    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

    dst_messages, dst_states, dst_stats = utils.parse_messages(args.dst)
    if dst_states["is_broken"]:
        print("Dest po is BROKEN, aborting.")
        return 1
    if args.stats:
        print("Dest po, before merging:")
        utils.print_stats(dst_stats, prefix="\t")
    # If we don’t want to replace existing valid translations, pre-populate
    # done_msgkeys and done_fuzzy_msgkeys.
    if not args.replace:
        done_msgkeys =  dst_states["trans_msg"].copy()
        done_fuzzy_msgkeys = dst_states["fuzzy_msg"].copy()
    for po in args.src:
        messages, states, stats = utils.parse_messages(po)
        if states["is_broken"]:
            print("\tSrc po {} is BROKEN, skipping.".format(po))
            ret = 1
            continue
        print("\tMerging {}...".format(po))
        if args.stats:
            print("\t\tMerged po stats:")
            utils.print_stats(stats, prefix="\t\t\t")
        for msgkey, val in messages.items():
            msgctxt, msgid = msgkey
            # This msgkey has already been completely merged, or is a commented one,
            # or the new message is commented, skip it.
            if msgkey in (done_msgkeys | dst_states["comm_msg"] | states["comm_msg"]):
                continue
            is_ttip = utils.is_tooltip(msgid)
            # New messages does not yet exists in dest.
            if msgkey not in dst_messages:
                dst_messages[msgkey] = messages[msgkey]
                if msgkey in states["fuzzy_msg"]:
                    done_fuzzy_msgkeys.add(msgkey)
                    dst_states["fuzzy_msg"].add(msgkey)
                elif msgkey in states["trans_msg"]:
                    done_msgkeys.add(msgkey)
                    dst_states["trans_msg"].add(msgkey)
                    dst_stats["trans_msg"] += 1
                    if is_ttip:
                        dst_stats["trans_ttips"] += 1
                nbr_added += 1
                dst_stats["tot_msg"] += 1
                if is_ttip:
                    dst_stats["tot_ttips"] += 1
            # From now on, the new messages is already in dst.
            # New message is neither translated nor fuzzy, skip it.
            elif msgkey not in (states["trans_msg"] | states["fuzzy_msg"]):
                continue
            # From now on, the new message is either translated or fuzzy!
            # The new message is translated.
            elif msgkey in states["trans_msg"]:
                dst_messages[msgkey]["msgstr_lines"] = messages[msgkey]["msgstr_lines"]
                done_msgkeys.add(msgkey)
                done_fuzzy_msgkeys.discard(msgkey)
                if msgkey in dst_states["fuzzy_msg"]:
                    dst_states["fuzzy_msg"].remove(msgkey)
                    nbr_unfuzzied += 1
                if msgkey not in dst_states["trans_msg"]:
                    dst_states["trans_msg"].add(msgkey)
                    dst_stats["trans_msg"] += 1
                    if is_ttip:
                        dst_stats["trans_ttips"] += 1
                else:
                    nbr_replaced += 1
                nbr_merged += 1
            # The new message is fuzzy, org one is fuzzy too,
            # and this msgkey has not yet been merged.
            elif msgkey not in (dst_states["trans_msg"] | done_fuzzy_msgkeys):
                dst_messages[msgkey]["msgstr_lines"] = messages[msgkey]["msgstr_lines"]
                done_fuzzy_msgkeys.add(msgkey)
                dst_states["fuzzy_msg"].add(msgkey)
                nbr_merged += 1
                nbr_replaced += 1

    utils.write_messages(args.dst, dst_messages, dst_states["comm_msg"], dst_states["fuzzy_msg"])

    print("Merged completed. {} messages were merged (among which {} were replaced), " \
          "{} were added, {} were \"un-fuzzied\"." \
          "".format(nbr_merged, nbr_replaced, nbr_added, nbr_unfuzzied))
    if args.stats:
        print("Final merged po stats:")
        utils.print_stats(dst_stats, prefix="\t")
    return ret
def main():
    import argparse
    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 " \
                    "from sources.")
    parser.add_argument('-s',
                        '--stats',
                        action="store_true",
                        help="Show statistics info.")
    parser.add_argument(
        '-r',
        '--replace',
        action="store_true",
        help="Replace existing messages of same \"level\" already in dest po.")
    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.")
    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

    dst_messages, dst_states, dst_stats = utils.parse_messages(args.dst)
    if dst_states["is_broken"]:
        print("Dest po is BROKEN, aborting.")
        return 1
    if args.stats:
        print("Dest po, before merging:")
        utils.print_stats(dst_stats, prefix="\t")
    # If we don’t want to replace existing valid translations, pre-populate
    # done_msgkeys and done_fuzzy_msgkeys.
    if not args.replace:
        done_msgkeys = dst_states["trans_msg"].copy()
        done_fuzzy_msgkeys = dst_states["fuzzy_msg"].copy()
    for po in args.src:
        messages, states, stats = utils.parse_messages(po)
        if states["is_broken"]:
            print("\tSrc po {} is BROKEN, skipping.".format(po))
            ret = 1
            continue
        print("\tMerging {}...".format(po))
        if args.stats:
            print("\t\tMerged po stats:")
            utils.print_stats(stats, prefix="\t\t\t")
        for msgkey, val in messages.items():
            msgctxt, msgid = msgkey
            # This msgkey has already been completely merged, or is a commented one,
            # or the new message is commented, skip it.
            if msgkey in (done_msgkeys | dst_states["comm_msg"]
                          | states["comm_msg"]):
                continue
            is_ttip = utils.is_tooltip(msgid)
            # New messages does not yet exists in dest.
            if msgkey not in dst_messages:
                dst_messages[msgkey] = messages[msgkey]
                if msgkey in states["fuzzy_msg"]:
                    done_fuzzy_msgkeys.add(msgkey)
                    dst_states["fuzzy_msg"].add(msgkey)
                elif msgkey in states["trans_msg"]:
                    done_msgkeys.add(msgkey)
                    dst_states["trans_msg"].add(msgkey)
                    dst_stats["trans_msg"] += 1
                    if is_ttip:
                        dst_stats["trans_ttips"] += 1
                nbr_added += 1
                dst_stats["tot_msg"] += 1
                if is_ttip:
                    dst_stats["tot_ttips"] += 1
            # From now on, the new messages is already in dst.
            # New message is neither translated nor fuzzy, skip it.
            elif msgkey not in (states["trans_msg"] | states["fuzzy_msg"]):
                continue
            # From now on, the new message is either translated or fuzzy!
            # The new message is translated.
            elif msgkey in states["trans_msg"]:
                dst_messages[msgkey]["msgstr_lines"] = messages[msgkey][
                    "msgstr_lines"]
                done_msgkeys.add(msgkey)
                done_fuzzy_msgkeys.discard(msgkey)
                if msgkey in dst_states["fuzzy_msg"]:
                    dst_states["fuzzy_msg"].remove(msgkey)
                    nbr_unfuzzied += 1
                if msgkey not in dst_states["trans_msg"]:
                    dst_states["trans_msg"].add(msgkey)
                    dst_stats["trans_msg"] += 1
                    if is_ttip:
                        dst_stats["trans_ttips"] += 1
                else:
                    nbr_replaced += 1
                nbr_merged += 1
            # The new message is fuzzy, org one is fuzzy too,
            # and this msgkey has not yet been merged.
            elif msgkey not in (dst_states["trans_msg"] | done_fuzzy_msgkeys):
                dst_messages[msgkey]["msgstr_lines"] = messages[msgkey][
                    "msgstr_lines"]
                done_fuzzy_msgkeys.add(msgkey)
                dst_states["fuzzy_msg"].add(msgkey)
                nbr_merged += 1
                nbr_replaced += 1

    utils.write_messages(args.dst, dst_messages, dst_states["comm_msg"],
                         dst_states["fuzzy_msg"])

    print("Merged completed. {} messages were merged (among which {} were replaced), " \
          "{} were added, {} were \"un-fuzzied\"." \
          "".format(nbr_merged, nbr_replaced, nbr_added, nbr_unfuzzied))
    if args.stats:
        print("Final merged po stats:")
        utils.print_stats(dst_stats, prefix="\t")
    return ret
Example #16
0
def main():
    import argparse
    parser = argparse.ArgumentParser(description="Write out messages.txt "
                                     "from Blender.")
    parser.add_argument('-t',
                        '--trunk',
                        action="store_true",
                        help="Update po’s in /trunk/po rather than /branches.")
    parser.add_argument('-i',
                        '--input',
                        metavar="File",
                        help="Input pot file path.")
    parser.add_argument('--pproc-contexts',
                        action="store_true",
                        help="Pre-process po’s to avoid having plenty of "
                        "fuzzy msgids just because a context was "
                        "added/changed!")
    parser.add_argument('-a',
                        '--add',
                        action="store_true",
                        help="Add missing po’s (useful only when one or "
                        "more languages are given!).")
    parser.add_argument('langs',
                        metavar='ISO_code',
                        nargs='*',
                        help="Restrict processed languages to those.")
    args = parser.parse_args()

    if args.input:
        global FILE_NAME_POT
        FILE_NAME_POT = args.input
    ret = 0

    if args.pproc_contexts:
        _ctxt_proc = pproc_newcontext_po
        pot_messages, _a, pot_stats = utils.parse_messages(FILE_NAME_POT)
    else:
        _ctxt_proc = lambda a, b, c: 0
        pot_messages, pot_stats = None, None

    if args.langs:
        for lang in args.langs:
            if args.trunk:
                dr = TRUNK_PO_DIR
                po = os.path.join(dr, ".".join((lang, "po")))
            else:
                dr = os.path.join(BRANCHES_DIR, lang)
                po = os.path.join(dr, ".".join((lang, "po")))
            if args.add:
                if not os.path.exists(dr):
                    os.makedirs(dr)
                if not os.path.exists(po):
                    shutil.copy(FILE_NAME_POT, po)
            if args.add or os.path.exists(po):
                t = _ctxt_proc(po, pot_messages, pot_stats)
                if t:
                    ret = t
                t = process_po(po, lang)
                if t:
                    ret = t
    elif args.trunk:
        for po in os.listdir(TRUNK_PO_DIR):
            if po.endswith(".po"):
                lang = os.path.basename(po)[:-3]
                po = os.path.join(TRUNK_PO_DIR, po)
                t = _ctxt_proc(po, pot_messages, pot_stats)
                if t:
                    ret = t
                t = process_po(po, lang)
                if t:
                    ret = t
    else:
        for lang in os.listdir(BRANCHES_DIR):
            po = os.path.join(BRANCHES_DIR, lang, ".".join((lang, "po")))
            if os.path.exists(po):
                t = _ctxt_proc(po, pot_messages, pot_stats)
                if t:
                    ret = t
                t = process_po(po, lang)
                if t:
                    ret = t

    return ret
def main():
    import argparse
    parser = argparse.ArgumentParser(description="Check po’s in branches " \
                                                 "(or in trunk) for missing" \
                                                 "/unneeded messages.")
    parser.add_argument('-s', '--stats', action="store_true",
                        help="Print po’s stats.")
    parser.add_argument('-m', '--messages', action="store_true",
                        help="Print po’s missing/unneeded/commented messages.")
    parser.add_argument('-t', '--trunk', action="store_true",
                        help="Check po’s in /trunk/po rather than /branches.")
    parser.add_argument('-p', '--pot',
                        help="Specify the .pot file used as reference.")
    parser.add_argument('langs', metavar='ISO_code', nargs='*',
                        help="Restrict processed languages to those.")
    args = parser.parse_args()

    if args.pot:
        global FILE_NAME_POT
        FILE_NAME_POT = args.pot
    glob_stats = {"nbr"               : 0.0,
                  "lvl"               : 0.0,
                  "lvl_ttips"         : 0.0,
                  "lvl_trans_ttips"   : 0.0,
                  "lvl_ttips_in_trans": 0.0,
                  "lvl_comm"          : 0.0,
                  "nbr_signs"         : 0,
                  "nbr_trans_signs"   : 0,
                  "contexts"          : set()}
    ret = 0

    pot_messages = None
    if args.messages:
        pot_messages, u1, pot_stats = utils.parse_messages(FILE_NAME_POT)
        pot_messages = set(pot_messages.keys())
        glob_stats["nbr_signs"] = pot_stats["nbr_signs"]

    if args.langs:
        for lang in args.langs:
            if args.trunk:
                po = os.path.join(TRUNK_PO_DIR, ".".join((lang, "po")))
            else:
                po = os.path.join(BRANCHES_DIR, lang, ".".join((lang, "po")))
            if os.path.exists(po):
                t = process_po(pot_messages, po, glob_stats,
                               args.stats, args.messages)
                if t:
                    ret = t
    elif args.trunk:
        for po in os.listdir(TRUNK_PO_DIR):
            if po.endswith(".po"):
                po = os.path.join(TRUNK_PO_DIR, po)
                t = process_po(pot_messages, po, glob_stats,
                               args.stats, args.messages)
                if t:
                    ret = t
    else:
        for lang in os.listdir(BRANCHES_DIR):
            for po in os.listdir(os.path.join(BRANCHES_DIR, lang)):
                if po.endswith(".po"):
                    po = os.path.join(BRANCHES_DIR, lang, po)
                    t = process_po(pot_messages, po, glob_stats,
                                   args.stats, args.messages)
                    if t:
                        ret = t

    if args.stats and glob_stats["nbr"] != 0.0:
        nbr_contexts = len(glob_stats["contexts"] - {""})
        if nbr_contexts != 1:
            if nbr_contexts == 0:
                nbr_contexts = "No"
            _ctx_txt = "s are"
        else:
            _ctx_txt = " is"
        print("\nAverage stats for all {:.0f} processed files:\n"
              "    {:>6.1%} done!\n"
              "    {:>6.1%} of messages are tooltips.\n"
              "    {:>6.1%} of tooltips are translated.\n"
              "    {:>6.1%} of translated messages are tooltips.\n"
              "    {:>6.1%} of messages are commented.\n"
              "    The org msgids are currently made of {} signs.\n"
              "    All processed translations are currently made of {} signs.\n"
              "    {} specific context{} present:\n            {}\n"
              "".format(glob_stats["nbr"], glob_stats["lvl"] / glob_stats["nbr"],
                        glob_stats["lvl_ttips"] / glob_stats["nbr"],
                        glob_stats["lvl_trans_ttips"] / glob_stats["nbr"],
                        glob_stats["lvl_ttips_in_trans"] / glob_stats["nbr"],
                        glob_stats["lvl_comm"] / glob_stats["nbr"], glob_stats["nbr_signs"],
                        glob_stats["nbr_trans_signs"], nbr_contexts, _ctx_txt,
                        "\n            ".join(glob_stats["contexts"]-{""})))

    return ret