From 74a7c581c3c34f840a8a45dd5bbf82cfaf550333 Mon Sep 17 00:00:00 2001 From: Julien Palard Date: Wed, 13 Oct 2021 14:09:33 +0200 Subject: [PATCH] Don't force everyone to use French dictionary. --- padpo/checkers/baseclass.py | 6 ++++++ padpo/checkers/grammalecte.py | 38 ++++++++++++++++++++++------------- padpo/padpo.py | 7 +++++++ 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/padpo/checkers/baseclass.py b/padpo/checkers/baseclass.py index c3d0331..5b1794f 100644 --- a/padpo/checkers/baseclass.py +++ b/padpo/checkers/baseclass.py @@ -26,6 +26,12 @@ class Checker(ABC): """Check an item in a `*.po` file.""" return NotImplementedError + def add_arguments(self, parser): + """Let any checker register argparse arguments.""" + + def configure(self, args): + """Store the result of parse_args, to get back arguments from self.add_arguments.""" + def replace_quotes(match): """Replace match with « xxxxxxx ».""" diff --git a/padpo/checkers/grammalecte.py b/padpo/checkers/grammalecte.py index 897cde4..d7ea189 100644 --- a/padpo/checkers/grammalecte.py +++ b/padpo/checkers/grammalecte.py @@ -1,7 +1,8 @@ """Checker for grammar errors.""" import re -from typing import Set +from pathlib import Path +from typing import Set, Optional import requests import simplelogging @@ -28,7 +29,6 @@ class GrammalecteChecker(Checker): """Initialiser.""" super().__init__() self.personal_dict: Set[str] = set() - self.get_personal_dict() def check_file(self, pofile: PoFile): """Check a `*.po` file.""" @@ -100,18 +100,28 @@ class GrammalecteChecker(Checker): return True return False - def get_personal_dict(self): - """ - Add spelling white list. - - Based on - https://raw.githubusercontent.com/python/python-docs-fr/3.9/dict - """ - download_request = requests.get( - "https://raw.githubusercontent.com/python/python-docs-fr/3.9/dict" - ) - download_request.raise_for_status() - for line in download_request.text.splitlines(): + def _get_personal_dict(self, dict_path: str) -> None: + if "://" in dict_path: + download_request = requests.get(dict_path) + download_request.raise_for_status() + lines = download_request.text + else: + lines = Path(dict_path).read_text(encoding="UTF-8") + for line in lines.splitlines(): word = line.strip() self.personal_dict.add(word) self.personal_dict.add(word.title()) + + def add_arguments(self, parser): + parser.add_argument( + "--dict", + nargs="*", + dest="dicts", + help="Personal dict files or URLs. Should contain onw word per line.", + ) + + def configure(self, args): + """Store the result of parse_args, to get back arguments from self.add_arguments.""" + if args.dicts: + for dict_path in args.dicts: + self._get_personal_dict(dict_path) diff --git a/padpo/padpo.py b/padpo/padpo.py index ec706f9..25cd3bb 100644 --- a/padpo/padpo.py +++ b/padpo/padpo.py @@ -93,6 +93,10 @@ def main(): ) files.add_argument("--version", action="store_true", help="Return version") parser.add_argument("-c", "--color", action="store_true", help="color output") + + for checker in checkers: + checker.add_arguments(parser) + args = parser.parse_args() if args.version: @@ -128,6 +132,9 @@ def main(): path = args.input_path pull_request_info = None + for checker in checkers: + checker.configure(args) + errors, warnings = check_paths(path, pull_request_info=pull_request_info) if errors: sys.exit(1)