Skip to content

Commit f9e6926

Browse files
committed
Grammalecte warnings silent examples.
May not be the best way to do it as it does not use the *warning* instance returned by Grammalecte. The filter_out functions may be more appropriate but they do not access the tcomment field. --> would be nice to refactor to get the best of the two approaches.
1 parent 9d9b1fd commit f9e6926

4 files changed

Lines changed: 70 additions & 4 deletions

File tree

padpo/checkers/baseclass.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ def check_item(self, item: PoItem):
2626
"""Check an item in a `*.po` file."""
2727
return NotImplementedError
2828

29+
def match_silent(self, regex, item: PoItem, match: str):
30+
""" Generic function to silent a warning directly raised by the checker in the *match* field """
31+
list_ok = regex.findall(item.entry.tcomment)
32+
return any([match in term for term in list_ok])
33+
34+
2935
def add_warning(self, level: str, item: PoItem, text: str, prefix:str=None, match:str=None, suffix:str=None):
3036
silent_method_names = [self.__getattribute__(name) for name in self.__dir__() if name.startswith('silent_')]
3137
if any([m(item, text, prefix, match, suffix) for m in silent_method_names if callable(m)]):

padpo/checkers/glossary.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ class GlossaryChecker(Checker):
1010
"""Checker for glossary usage."""
1111

1212
name = "Glossary"
13-
silent_re = re.compile('silent-glossary:`([^`]*)`')
13+
re_silent = re.compile('silent-glossary:`([^`]*)`')
1414

1515
def silent_glossary(self, item: PoItem, text: str, prefix, match, suffix):
16-
for term in self.silent_re.findall(item.entry.tcomment):
16+
for term in self.re_silent.findall(item.entry.tcomment):
1717
if term in text[:text.find("that is not translated")]:
1818
return True
1919

padpo/checkers/grammalecte.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ class GrammalecteChecker(Checker):
2323
"""Checker for grammar errors."""
2424

2525
name = "Grammalecte"
26+
capital_silent_re = re.compile('silent-capital:`([^`]*)`')
27+
unknown_word_silent_re = re.compile('silent-unknown:`([^`]*)`')
28+
29+
def silent_capital(self, item: PoItem, text: str, prefix: str, match: str, suffix:str):
30+
return self.match_silent(self.capital_silent_re, item, match)
31+
32+
def silent_unknown_word(self, item: PoItem, text: str, prefix: str, match: str, suffix: str):
33+
return self.match_silent(self.unknown_word_silent_re, item, match)
2634

2735
def __init__(self):
2836
"""Initialiser."""
@@ -54,9 +62,11 @@ def manage_warnings(self, warnings: GrammalecteMessage, pofile: PoFile) -> None:
5462
item = pofile.content[item_index]
5563
start = max(0, warning.start - 40)
5664
end = warning.end + 10
57-
item.add_warning(
58-
self.name,
65+
self.add_warning("warning", item,
5966
f"{warning.message} => " f"###{item.msgstr_rst2txt[start:end]}###",
67+
item.msgstr_rst2txt[start:warning.start],
68+
item.msgstr_rst2txt[warning.start:warning.end],
69+
item.msgstr_rst2txt[warning.end:end]
6070
)
6171

6272
def filter_out_grammar_error(self, warning: GrammalecteMessage) -> bool:
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# example how to silent Grammalecte warnings
2+
#
3+
msgid ""
4+
msgstr ""
5+
"Project-Id-Version: padpo\n"
6+
"Language-Team: FRENCH <traductions@lists.afpy.org>\n"
7+
"Language: fr\n"
8+
"Content-Type: text/plain; charset=UTF-8\n"
9+
"Content-Transfer-Encoding: 8bit\n"
10+
11+
12+
# silent-capital:`alors`
13+
#: tutorial/classes.rst:276
14+
msgid ""
15+
"then ``MyClass.i`` and ``MyClass.f`` are valid attribute references, "
16+
"returning an integer and a function object, respectively. Class attributes "
17+
"can also be assigned to, so you can change the value of ``MyClass.i`` by "
18+
"assignment. :attr:`__doc__` is also a valid attribute, returning the "
19+
"docstring belonging to the class: ``\"A simple example class\"``."
20+
msgstr ""
21+
"alors ``MyClass.i`` et ``MyClass.f`` sont des références valides à des "
22+
"attributs, renvoyant respectivement un entier et un objet fonction. Les "
23+
"attributs de classes peuvent également être affectés, de sorte que vous "
24+
"pouvez modifier la valeur de ``MyClass.i`` par affectation. :attr:`__doc__` "
25+
"est aussi un attribut valide, renvoyant la *docstring* appartenant à la "
26+
"classe : ``\"A simple example class\"``."
27+
28+
# silent-unknown:`SciPy`
29+
#: tutorial/floatingpoint.rst:160
30+
msgid ""
31+
"If you are a heavy user of floating point operations you should take a look "
32+
"at the Numerical Python package and many other packages for mathematical and "
33+
"statistical operations supplied by the SciPy project. See <https://scipy."
34+
"org>."
35+
msgstr ""
36+
"Si vous êtes un utilisateur intensif des opérations sur les nombres à "
37+
"virgule flottante, nous vous conseillons de considérer le paquet *Numerical "
38+
"Python* ainsi que les paquets pour les opérations statistiques et "
39+
"mathématiques fournis par le projet SciPy. Consultez <https://scipy.org>."
40+
41+
# silent-unknown:`The Perils of Floating Point`
42+
#: tutorial/floatingpoint.rst:134
43+
msgid ""
44+
"See `The Perils of Floating Point <http://"
45+
"www.lahey.com/float.htm>`_ for a more complete account of other common "
46+
"surprises."
47+
msgstr ""
48+
"Voir `The Perils of "
49+
"Floating Point <http://www.lahey.com/float.htm>`_ pour une liste plus "
50+
"complète de ce genre de surprises."

0 commit comments

Comments
 (0)