File indexing completed on 2024-10-13 13:24:38

0001 #!/usr/bin/env python3
0002 # -*- coding: UTF-8 -*-
0003 
0004 try:
0005     import fallback_import_paths
0006 except:
0007     pass
0008 
0009 import sys
0010 import os
0011 import locale
0012 from tempfile import NamedTemporaryFile
0013 
0014 from pology import version, _, n_
0015 from pology.catalog import Catalog
0016 from pology.message import MessageUnsafe
0017 from pology.lang.sr.wconv import tohi
0018 from pology.colors import ColorOptionParser
0019 from pology.comments import manc_parse_flag_list
0020 from pology.diff import msg_ediff, msg_ediff_to_new
0021 from pology.fsops import str_to_unicode, collect_catalogs
0022 from pology.fsops import collect_paths_cmdline
0023 from pology.msgreport import warning_on_msg, report_msg_content
0024 from pology.report import report, warning, error, format_item_list
0025 from pology.stdcmdopt import add_cmdopt_filesfrom
0026 from pology.vcs import available_vcs, make_vcs
0027 
0028 
0029 def _main ():
0030 
0031     locale.setlocale(locale.LC_ALL, "")
0032 
0033     usage= _("@info command usage",
0034         "%(cmd)s [OPTIONS] VCS [POPATHS...]",
0035         cmd="%prog")
0036     desc = _("@info command description",
0037         "Compose hybridized Ijekavian-Ekavian translation out of "
0038         "translation modified from Ekavian to Ijekavian or vice-versa.")
0039     ver = _("@info command version",
0040         "%(cmd)s (Pology) %(version)s\n"
0041         "Copyright © 2009, 2010 "
0042         "Chusslove Illich (Часлав Илић) <%(email)s>",
0043         cmd="%prog", version=version(), email="caslav.ilic@gmx.net")
0044 
0045     opars = ColorOptionParser(usage=usage, description=desc, version=ver)
0046     opars.add_option(
0047         "-a", "--accept-changes",
0048         action="store_true", dest="accept_changes", default=False,
0049         help=_("@info command line option description",
0050                "Accept messages which have some changes between base "
0051                "and reconstructed base text."))
0052     opars.add_option(
0053         "-r", "--base-revision",
0054         metavar=_("@info command line value placeholder", "REVISION"),
0055         action="store", dest="base_revision", default=None,
0056         help=_("@info command line option description",
0057                "Use the given revision as base for hybridization, "
0058                "instead of local latest revision."))
0059     add_cmdopt_filesfrom(opars)
0060 
0061     (options, free_args) = opars.parse_args(str_to_unicode(sys.argv[1:]))
0062 
0063     try:
0064         import psyco
0065         psyco.full()
0066     except ImportError:
0067         pass
0068 
0069     # Create VCS.
0070     if len(free_args) < 1:
0071         showvcs = list(set(available_vcs()).difference(["none"]))
0072         showvcs.sort()
0073         error(_("@info",
0074                 "Version control system not given "
0075                 "(can be one of: %(vcslist)s).",
0076                 vcslist=format_item_list(showvcs)))
0077     vcskey = free_args.pop(0)
0078     if vcskey not in available_vcs(flat=True):
0079         error(_("@info",
0080                 "Unknown version control system '%(vcs)s'.",
0081                 vcs=vcskey))
0082     vcs = make_vcs(vcskey)
0083 
0084     # Collect PO files in given paths.
0085     popaths = collect_paths_cmdline(rawpaths=free_args,
0086                                     filesfrom=options.files_from,
0087                                     elsecwd=True,
0088                                     respathf=collect_catalogs,
0089                                     abort=True)
0090 
0091     # Catalogs must be under version control.
0092     for path in popaths:
0093         if not vcs.is_versioned(path):
0094             error(_("@info",
0095                     "Catalog '%(file)s' is not under version control.",
0096                     file=path))
0097 
0098     # Go by modified PO file and hybridize it.
0099     for path in popaths:
0100         # Extract local head counterpart.
0101         tmpf = NamedTemporaryFile(prefix="pohybdl-export-", suffix=".po")
0102         if not vcs.export(path, options.base_revision, tmpf.name):
0103             error(_("@info",
0104                     "Version control system cannot export file '%(file)s'.",
0105                     file=path))
0106         # Hybridize by comparing local head and modified file.
0107         hybdl(path, tmpf.name, options.accept_changes)
0108 
0109 
0110 def hybdl (path, path0, accnohyb=False):
0111 
0112     cat = Catalog(path)
0113     cat0 = Catalog(path0, monitored=False)
0114 
0115     nhybridized = 0
0116     nstopped = 0
0117     for msg in cat:
0118 
0119         if "no-hybdl" in manc_parse_flag_list(msg, "|"):
0120             continue
0121 
0122         # Unembed diff if message was diffed for review.
0123         # Replace ediff with manual review flag.
0124         diffed = False
0125         for flag in msg.flag:
0126             if flag.startswith("ediff"):
0127                 msg.flag.remove(flag)
0128                 diffed = True
0129         if diffed:
0130             msg_ediff_to_new(msg, msg)
0131             msg.flag.add("reviewed")
0132 
0133         # Fetch original message.
0134         msg0 = cat0.get(msg)
0135         if msg0 is None:
0136             warning_on_msg(_("@info",
0137                              "Message does not exist in the original catalog."),
0138                            msg, cat)
0139             nstopped += 1
0140             continue
0141         if len(msg.msgstr) != len(msg0.msgstr):
0142             warning_on_msg(_("@info",
0143                              "Number of translations not same as in "
0144                              "the original message."), msg, cat)
0145             nstopped += 1
0146             continue
0147         if msg.msgstr == msg0.msgstr:
0148             # No changes, nothing new to hybridize.
0149             continue
0150 
0151         # Hybridize translation.
0152         textsh = []
0153         textshinv = []
0154         for text0, text in zip(msg0.msgstr, msg.msgstr):
0155             texth = tohi(text0, text, parthyb=True)
0156             textsh.append(texth)
0157             if not accnohyb:
0158                 texthinv = tohi(text, text0, parthyb=True)
0159                 textshinv.append(texthinv)
0160         if accnohyb or textsh == textshinv:
0161             for i, texth in zip(list(range(len(msg.msgstr))), textsh):
0162                 msg.msgstr[i] = texth
0163             nhybridized += 1
0164         else:
0165             nstopped += 1
0166             msgh = MessageUnsafe(msg)
0167             msgh.msgstr = textsh
0168             msghinv = MessageUnsafe(msg)
0169             msghinv.msgstr = textshinv
0170             msg_ediff(msghinv, msgh, emsg=msgh, colorize=True)
0171             report_msg_content(msgh, cat, delim=("-" * 20))
0172 
0173     if nstopped == 0:
0174         if cat.sync():
0175             report("! %s (%d)" % (path, nhybridized))
0176     else:
0177         warning(n_("@info",
0178                    "%(num)d message in '%(file)s' cannot be "
0179                    "cleanly hybridized.",
0180                    "%(num)d messages in '%(file)s' cannot be "
0181                    "cleanly hybridized.",
0182                    num=nstopped, file=path))
0183         nhybridized = 0
0184 
0185     return nhybridized
0186 
0187 
0188 if __name__ == '__main__':
0189     _main()
0190