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

0001 # -*- coding: UTF-8 -*-
0002 
0003 """
0004 Helpers for catalog sieves.
0005 
0006 Pology's C{posieve} script processes catalogs with "sieves": objects to which
0007 catalog entries are fed one by one, possibly with finalization phase at the end.
0008 This module contains some common helpers which are used by many sieves.
0009 
0010 @author: Chusslove Illich (Часлав Илић) <caslav.ilic@gmx.net>
0011 @license: GPLv3
0012 """
0013 
0014 import locale
0015 
0016 from pology import PologyError, _, n_
0017 from pology.comments import manc_parse_flag_list
0018 
0019 
0020 class SieveError (PologyError):
0021     """
0022     Base exception class for sieve errors with special meaning.
0023     """
0024 
0025     pass
0026 
0027 
0028 class SieveMessageError (SieveError):
0029     """
0030     Exception for single messages.
0031 
0032     If sieve's C{process} method throws it, client is allowed to send
0033     other messages from the same catalog to the sieve.
0034     """
0035 
0036     pass
0037 
0038 
0039 class SieveCatalogError (SieveError):
0040     """
0041     Exception for single catalogs.
0042 
0043     If sieve's C{process} or C{process_header} method throw it, client is not
0044     allowed to send other messages from the same catalog to the sieve,
0045     but can send messages from other catalogs.
0046     """
0047 
0048     pass
0049 
0050 
0051 def parse_sieve_flags (msg):
0052     """
0053     Extract sieve flags embedded in manual comments.
0054 
0055     Sieve flags are put into manual comments with the following syntax::
0056 
0057         # |, flag1, flag2, ...
0058 
0059     Some sieves will define certain sieve flags by which their behavior
0060     can be altered on a particular message.
0061 
0062     @param msg: message to parse
0063     @type msg: Message
0064 
0065     @returns: parsed flags
0066     @rtype: set of strings
0067     """
0068 
0069     return set(manc_parse_flag_list(msg, "|"))
0070 
0071 
0072 def add_param_lang (p, appx=None):
0073     """
0074     Add C{lang} parameter to sieve parameters.
0075 
0076     @param appx: one or more trailing paragraphs for the parameter description
0077     @type appx: string
0078     """
0079 
0080     desc = _("@info sieve parameter discription",
0081     "The language of translation. "
0082     "If the user configuration or a catalog header specifies the language, "
0083     "this parameter takes precedence."
0084     )
0085     if appx:
0086         desc = "%s\n\n%s" % (desc, appx)
0087     p.add_param("lang", str,
0088                 metavar=_("@info sieve parameter value placeholder", "CODE"),
0089                 desc=desc)
0090 
0091 
0092 def add_param_env (p, appx=None):
0093     """
0094     Add C{env} parameter to sieve parameters.
0095 
0096     @param appx: one or more trailing paragraphs for the parameter description
0097     @type appx: string
0098     """
0099 
0100     desc = _("@info sieve parameter discription",
0101     "The environment (language variation) of translation. "
0102     "If the user configuration or a catalog header specifies the environment, "
0103     "this parameter takes precedence. "
0104     "Several environments can be given as comma-separated list."
0105     )
0106     if appx:
0107         desc = "%s\n\n%s" % (desc, appx)
0108     p.add_param("env", str, seplist=True,
0109                 metavar=_("@info sieve parameter value placeholder", "CODE"),
0110                 desc=desc)
0111 
0112 
0113 def add_param_accel (p, appx=None):
0114     """
0115     Add parameter C{accel} to sieve parameters.
0116 
0117     @param appx: one or more trailing paragraphs for the parameter description
0118     @type appx: string
0119     """
0120 
0121     desc = _("@info sieve parameter discription",
0122     "Character which is used as UI accelerator marker in text fields."
0123     )
0124     if appx:
0125         desc = "%s\n\n%s" % (desc, appx)
0126     p.add_param("accel", str, multival=True,
0127                 metavar=_("@info sieve parameter value placeholder", "CHAR"),
0128                 desc=desc)
0129 
0130 
0131 def add_param_markup (p, appx=None):
0132     """
0133     Add parameter C{markup} to sieve parameters.
0134 
0135     @param appx: one or more trailing paragraphs for the parameter description
0136     @type appx: string
0137     """
0138 
0139     desc = _("@info sieve parameter discription",
0140     "Markup that can be expected in text fields, as special keyword. "
0141     "Several markups can be given as comma-separated list."
0142     )
0143     if appx:
0144         desc = "%s\n\n%s" % (desc, appx)
0145     p.add_param("markup", str, seplist=True,
0146                 metavar=_("@info sieve parameter value placeholder", "KEYWORD"),
0147                 desc=desc)
0148 
0149 
0150 def add_param_filter (p, intro=None):
0151     """
0152     Add C{filter} parameter to sieve parameters.
0153 
0154     @param intro: first paragraph for the parameter description
0155     @type intro: string
0156     """
0157 
0158     desc = _("@info sieve parameter description",
0159     "For a module pology.FOO which defines FOO() function, "
0160     "the hook specification is simply FOO. "
0161     "If the hook function is named BAR() instead of FOO(), then "
0162     "the hook specification is FOO/BAR. "
0163     "Language specific hooks (pology.lang.LANG.FOO) are aditionally "
0164     "preceded by the language code with colon, as LANG:FOO or LANG:FOO/BAR. "
0165     "\n\n"
0166     "If the function is actually a hook factory, the arguments for "
0167     "the factory are passed separated by tilde: LANG:FOO/BAR~ARGS "
0168     "(where LANG: and /BAR may be omitted under previous conditions). "
0169     "The ARGS string is a list of arguments as it would appear "
0170     "in the function call in Python code, omitting parenthesis. "
0171     "\n\n"
0172     "Several hooks can be given by repeating the parameter, "
0173     "when they are applied in the given order."
0174     )
0175     if intro:
0176         desc = "%s\n\n%s" % (intro, desc)
0177 
0178     p.add_param("filter", str, multival=True,
0179                 metavar=_("@info sieve parameter value placeholder",
0180                           "HOOKSPEC"),
0181                 desc=desc)
0182 
0183 
0184 def add_param_poeditors (p):
0185     """
0186     Add parameters for opening messages in editors to sieve parameters.
0187     """
0188 
0189     p.add_param("lokalize", bool, defval=False,
0190                 desc=_("@info sieve parameter discription",
0191     "Open catalogs on reported messages in Lokalize. "
0192     "Lokalize must be already running with the project "
0193     "that contains the sieved catalogs opened."
0194     ))
0195 
0196 
0197 def add_param_entdef (p):
0198     """
0199     Add C{entdef} parameter to sieve parameters.
0200     """
0201 
0202     p.add_param("entdef", str, multival=True,
0203                 metavar="FILE",
0204                 desc=_("@info sieve parameter discription; "
0205                        "in the last line only 'entname' and 'entvalue' "
0206                        "should be translated",
0207     "File defining the entities used in messages "
0208     "(parameter can be repeated to add more files). Entity file "
0209     "defines entities one per line, in the format:"
0210     "\n\n"
0211     "&lt;!ENTITY entname 'entvalue'&gt;"
0212     ))
0213 
0214 
0215 def add_param_spellcheck (p):
0216     """
0217     Add parameters for spell checking to sieve parameters.
0218     """
0219 
0220     add_param_lang(p, appx=_("@info sieve parameter discription",
0221         "The language determines which system dictionary, "
0222         "as well as internal word lists, to use for spell-checking. "
0223         "If the language is left undefined for a given catalog, "
0224         "it will be skipped and a warning may be output."
0225         ))
0226     add_param_env(p, appx=_("@info sieve parameter discription",
0227         "The environment determines which additional "
0228         "internal word lists to use for spell-checking. "
0229         "If the environment is left undefined for a given catalog, "
0230         "only environment-agnostic internal word lists will be used."
0231         ))
0232     add_param_accel(p)
0233     add_param_markup(p)
0234     p.add_param("skip", str,
0235                 metavar=_("@info sieve parameter value placeholder", "REGEX"),
0236                 desc=_("@info sieve parameter discription",
0237     "Regular expression to eliminate from spell-checking words that match it."
0238     ))
0239     p.add_param("case", bool, defval=False,
0240                 desc=_("@info sieve parameter discription",
0241     "Make matching patterns given as parameter values case-sensitive."
0242     ))
0243     add_param_filter(p,
0244         intro=_("@info sieve parameter discription",
0245         "The F1A or F3A/C hook through which to filter the translation "
0246         "before passing it to spell-checking."
0247         ))
0248     p.add_param("suponly", bool, defval=False,
0249                 desc=_("@info sieve parameter discription",
0250     "Use only internal supplement word lists, and not the system dictionary."
0251     ))
0252     p.add_param("list", bool, defval=False,
0253                 desc=_("@info sieve parameter discription",
0254     "Output only a simple sorted list of unknown words."
0255     ))
0256     add_param_poeditors(p)
0257