File indexing completed on 2024-05-12 17:18:07

0001 # -*- coding: UTF-8 -*-
0002 
0003 """
0004 Additional header operations for KDE Translation Project.
0005 
0006 @author: Chusslove Illich (Часлав Илић) <caslav.ilic@gmx.net>
0007 @license: GPLv3
0008 """
0009 
0010 import os
0011 import re
0012 
0013 from pology import _, n_
0014 from pology.report import warning
0015 from pology.proj.kde.cattype import get_project_subdir
0016 from pology.proj.kde.cattype import is_txt_cat, is_qt_cat, is_docbook_cat
0017 from pology.proj.kde.cattype import is_html_cat, is_unknown_cat
0018 
0019 
0020 def equip_header (hdr, cat):
0021     """
0022     Add extra information to header [type F4B hook].
0023 
0024     The following header fields are set:
0025       - C{Language}: the language code of translation;
0026             set only if the language can be determined
0027       - C{X-Environment}: linguistic subset of the language of translation
0028             (team choices on terminology, ortography...);
0029             set to C{kde} if not existing, otherwise left untouched.
0030       - C{X-Accelerator-Marker}: accelerator marker character which may
0031             be encountered in text
0032       - C{X-Text-Markup}: text markups (e.g. Qt rich text, Docbook...) which
0033             may be encountered in text, as keywords
0034 
0035     For the hook to function properly, the local checkout of language catalogs
0036     must match the repository structure up to a certain level.
0037     See the documentation on C{check-tp-kde} sieve for details.
0038     TODO: Put that instruction here.
0039     """
0040 
0041     cname = cat.name
0042     csubdir = get_project_subdir(cat.filename)
0043     if not csubdir:
0044         warning(_("@info TP stands for Translation Project",
0045                   "Cannot determine KDE TP subdirectory "
0046                   "of '%(file)s', skipping header updates.",
0047                   file=cat.filename))
0048         return 1
0049 
0050     pathels = os.path.abspath(cat.filename).split(os.path.sep)
0051     lang_rx = re.compile(r"^[a-z]{2}(_[A-Z]{2}|@[a-z]+)?$")
0052     lang = None
0053     if len(pathels) >= 5 and pathels[-4] == "summit":
0054         if lang_rx.search(pathels[-5]):
0055             lang = pathels[-5]
0056     elif len(pathels) >= 4:
0057         if lang_rx.search(pathels[-4]):
0058             lang = pathels[-4]
0059 
0060     if is_txt_cat(cname, csubdir):
0061         accmark = ""
0062         mtypes = [""]
0063     elif is_qt_cat(cname, csubdir):
0064         accmark = "&"
0065         mtypes = ["qtrich"]
0066     elif is_docbook_cat(cname, csubdir):
0067         accmark = ""
0068         mtypes = ["docbook4"]
0069     elif is_html_cat(cname, csubdir):
0070         accmark = ""
0071         mtypes = ["html"]
0072     elif is_unknown_cat(cname, csubdir):
0073         accmark = None
0074         mtypes = None
0075     else: # default to native KDE4 catalog
0076         accmark = "&"
0077         mtypes = ["kde4"]
0078 
0079     fvs = []
0080     fvs.append(("Language", lang, "Language-Team", False))
0081     fvs.append(("X-Environment", "kde", None, True))
0082     if accmark is not None:
0083         fvs.append(("X-Accelerator-Marker", accmark, None, False))
0084     if mtypes is not None:
0085         fvs.append(("X-Text-Markup", ", ".join(mtypes), None, False))
0086     for fnam, fval, fnamaft, fkeep in fvs:
0087         if fval is None:
0088             continue
0089         existing = hdr.select_fields(fnam)
0090         if not (existing and fkeep):
0091             if len(existing) > 1:
0092                 hdr.remove_field(fnam)
0093             hdr.set_field(str(fnam), str(fval), after=fnamaft)
0094 
0095     return 0
0096