File indexing completed on 2024-04-14 15:48:40

0001 # -*- coding: UTF-8 -*-
0002 
0003 """
0004 Initialize and update the PO header with own translation data.
0005 
0006 Documented in C{doc/user/sieving.docbook}.
0007 
0008 @author: Chusslove Illich (Часлав Илић) <caslav.ilic@gmx.net>
0009 @license: GPLv3
0010 """
0011 
0012 import os
0013 import re
0014 import time
0015 
0016 from pology import _, n_
0017 import pology.config as config
0018 from pology.report import warning
0019 from pology.resolve import expand_vars
0020 from pology.sieve import SieveError
0021 
0022 
0023 def setup_sieve (p):
0024 
0025     p.set_desc(_("@info sieve discription",
0026     "Initialize or update the PO header with own translator data."
0027     ))
0028     p.add_param("proj", str, mandatory=True,
0029                 metavar=_("@info sieve parameter value placeholder", "ID"),
0030                 desc=_("@info sieve parameter discription",
0031     "Project ID in Pology configuration file, "
0032     "which contains the necessary project data to update the header."
0033     ))
0034     p.add_param("init", bool, defval=False,
0035                 desc=_("@info sieve parameter discription",
0036     "Consider header as uninitialized, removing any existing information "
0037     "before adding own and project data."
0038     ))
0039     p.add_param("onmod", bool, defval=False,
0040                 desc=_("@info sieve parameter discription",
0041     "Update header only if the catalog was otherwise modified "
0042     "(in sieve chains)."
0043     ))
0044 
0045 
0046 class Sieve (object):
0047 
0048     def __init__ (self, params):
0049 
0050         self.p = params
0051 
0052         # Collect user and project configuration.
0053         prjsect = "project-" + params.proj
0054         if not config.has_section(prjsect):
0055             raise SieveError(
0056                 _("@info",
0057                   "Project '%(id)s' is not defined in user configuration.",
0058                   id=params.proj))
0059         self.prjcfg = config.section(prjsect)
0060         prjcfg = config.section(prjsect)
0061         usrcfg = config.section("user")
0062 
0063         # Collect project data.
0064         self.name = prjcfg.string("name") or usrcfg.string("name")
0065         if not self.name:
0066             warning(_("@info",
0067                       "Field '%(field)s' is not set in "
0068                       "project or user configuration.",
0069                       field="name"))
0070         self.email = prjcfg.string("email") or usrcfg.string("email")
0071         if not self.email:
0072             warning(_("@info",
0073                       "Field '%(field)s' is not set in "
0074                       "project or user configuration.",
0075                       field="email"))
0076         self.langteam = prjcfg.string("language-team")
0077         if not self.langteam:
0078             warning(_("@info",
0079                       "Field '%(field)s' is not set in "
0080                       "project configuration.",
0081                       field="language-team"))
0082         self.teamemail = prjcfg.string("team-email") # ok not to be present
0083         self.langcode = prjcfg.string("language") or usrcfg.string("language")
0084         if not self.langcode:
0085             warning(_("@info",
0086                       "Field '%(field)s' is not set in "
0087                       "project configuration.",
0088                       field="language"))
0089         self.encoding = (   prjcfg.string("encoding")
0090                          or usrcfg.string("encoding")
0091                          or "UTF-8")
0092         self.plforms = (   prjcfg.string("plural-forms")
0093                         or usrcfg.string("plural-forms"))
0094         if not self.plforms:
0095             warning(_("@info",
0096                       "Field '%(field)s' is not set in "
0097                       "project configuration.",
0098                       field="plural-forms"))
0099         self.poeditor = (    prjcfg.string("po-editor")
0100                           or usrcfg.string("po-editor")) # ok not to be present
0101 
0102 
0103     def process_header_last (self, hdr, cat):
0104 
0105         if self.p.onmod and cat.modcount == 0:
0106             return
0107 
0108         if self.p.init:
0109             # Assemble translation title.
0110             if self.langteam:
0111                 title = ("Translation of %(title)s into %(lang)s."
0112                          % dict(title="%poname", lang="%langname"))
0113             else:
0114                 title = ("Translation of %(title)s."
0115                          % dict(title="%poname"))
0116             # Remove some placeholders.
0117             if "YEAR" in hdr.copyright:
0118                 hdr.copyright = None
0119             if "PACKAGE" in hdr.license:
0120                 hdr.license = None
0121 
0122             cat.update_header(project=cat.name, title=title,
0123                               name=self.name, email=self.email,
0124                               teamemail=self.teamemail,
0125                               langname=self.langteam, langcode=self.langcode,
0126                               encoding=self.encoding, ctenc="8bit",
0127                               plforms=self.plforms,
0128                               poeditor=self.poeditor)
0129         else:
0130             cat.update_header(name=self.name, email=self.email,
0131                               poeditor=self.poeditor)
0132