diff --git a/tlfi/__main__.py b/tlfi/__main__.py index b7c6346..a704a58 100644 --- a/tlfi/__main__.py +++ b/tlfi/__main__.py @@ -10,6 +10,8 @@ import difflib import gzip import re import unicodedata +from functools import cache +from os import environ from pathlib import Path from bs4 import BeautifulSoup, NavigableString @@ -27,9 +29,11 @@ except ImportError: def main(): ap = argparse.ArgumentParser(description="TLFi CLI") ap.add_argument("query", help="mot(s) à chercher") - ap.add_argument("-f", "--lexical-forms", default="lexical_forms.txt", + ap.add_argument("-f", "--lexical-forms", + default=get_root_path() / "lexical_forms.txt", help="fichier des formes lexicales") - ap.add_argument("-d", "--definitions", default="definitions", + ap.add_argument("-d", "--definitions", + default=get_root_path() / "definitions", help="répertoire des définitions") args = ap.parse_args() @@ -42,6 +46,11 @@ def main(): show_definition(d) +@cache +def get_root_path(): + return Path(environ.get("TLFI_ROOT", ".")) + + def lookup(query, lexical_forms_path): """Return a form for which a definition might exist, else None. @@ -63,7 +72,7 @@ def lookup(query, lexical_forms_path): return None -def get_definition_paths(query, definitions): +def get_definition_paths(query, definitions) -> list: """Return a list of definition file paths for this lexical form.""" nfkd = unicodedata.normalize("NFKD", query[0]) first_char = next((c for c in nfkd if not unicodedata.combining(c)), "") @@ -90,7 +99,7 @@ def show_definition(def_path): TAG_STRIP_RE = re.compile(r"\s+") -def parse_tag(tag): +def parse_tag(tag) -> str: if isinstance(tag, NavigableString): return TAG_STRIP_RE.sub(" ", tag) content = ""