File indexing completed on 2024-11-03 08:24:27

0001 #!/usr/bin/env python3
0002 # -*- coding: UTF-8 -*-
0003 
0004 """
0005 Rewrap message strings in PO files.
0006 
0007 Documented in C{doc/user/misctools.docbook#sec-mirewrap}.
0008 
0009 @author: Chusslove Illich (Часлав Илић) <caslav.ilic@gmx.net>
0010 @license: GPLv3
0011 """
0012 
0013 import locale
0014 import os
0015 import sys
0016 
0017 try:
0018     import fallback_import_paths
0019 except:
0020     pass
0021 
0022 from pology import version, _, n_
0023 from pology.catalog import Catalog
0024 from pology.colors import ColorOptionParser
0025 import pology.config as pology_config
0026 from pology.fsops import collect_paths_cmdline, collect_catalogs
0027 from pology.fsops import exit_on_exception
0028 from pology.report import report, error
0029 from pology.stdcmdopt import add_cmdopt_filesfrom, add_cmdopt_wrapping
0030 from pology.wrap import select_field_wrapping
0031 
0032 
0033 def main ():
0034 
0035     locale.setlocale(locale.LC_ALL, "")
0036 
0037     # Get defaults for command line options from global config.
0038     cfgsec = pology_config.section("porewrap")
0039 
0040     # Setup options and parse the command line.
0041     usage = _("@info command usage",
0042         "%(cmd)s [options] POFILE...",
0043         cmd="%prog")
0044     desc = _("@info command description",
0045         "Rewrap message strings in PO files.")
0046     ver = _("@info command version",
0047         "%(cmd)s (Pology) %(version)s\n"
0048         "Copyright © 2007, 2008, 2009, 2010 "
0049         "Chusslove Illich (Часлав Илић) &lt;%(email)s&gt;",
0050         cmd="%prog", version=version(), email="caslav.ilic@gmx.net")
0051 
0052     opars = ColorOptionParser(usage=usage, description=desc, version=ver)
0053     opars.add_option(
0054         "-v", "--verbose",
0055         action="store_true", dest="verbose", default=False,
0056         help=_("@info command line option description",
0057                "More detailed progress information."))
0058     add_cmdopt_wrapping(opars)
0059     add_cmdopt_filesfrom(opars)
0060 
0061     (op, fargs) = opars.parse_args()
0062 
0063     if len(fargs) < 1 and not op.files_from:
0064         error(_("@info", "No input files given."))
0065 
0066     # Could use some speedup.
0067     try:
0068         import psyco
0069         psyco.full()
0070     except ImportError:
0071         pass
0072 
0073     # Assemble list of files.
0074     fnames = collect_paths_cmdline(rawpaths=fargs,
0075                                    filesfrom=op.files_from,
0076                                    respathf=collect_catalogs,
0077                                    abort=True)
0078 
0079     # Rewrap all catalogs.
0080     for fname in fnames:
0081         if op.verbose:
0082             report(_("@info:progress", "Rewrapping: %(file)s", file=fname))
0083         cat = Catalog(fname, monitored=False)
0084         wrapping = select_field_wrapping(cfgsec, cat, op)
0085         cat.set_wrapping(wrapping)
0086         cat.sync(force=True)
0087 
0088 
0089 if __name__ == '__main__':
0090     exit_on_exception(main)