use TLFI_ROOT env to avoid specifying dirs

This commit is contained in:
dece 2022-01-17 10:57:13 +01:00
parent be17602df5
commit 2bf5568368

View file

@ -10,6 +10,8 @@ import difflib
import gzip import gzip
import re import re
import unicodedata import unicodedata
from functools import cache
from os import environ
from pathlib import Path from pathlib import Path
from bs4 import BeautifulSoup, NavigableString from bs4 import BeautifulSoup, NavigableString
@ -27,9 +29,11 @@ except ImportError:
def main(): def main():
ap = argparse.ArgumentParser(description="TLFi CLI") ap = argparse.ArgumentParser(description="TLFi CLI")
ap.add_argument("query", help="mot(s) à chercher") 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") 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") help="répertoire des définitions")
args = ap.parse_args() args = ap.parse_args()
@ -42,6 +46,11 @@ def main():
show_definition(d) show_definition(d)
@cache
def get_root_path():
return Path(environ.get("TLFI_ROOT", "."))
def lookup(query, lexical_forms_path): def lookup(query, lexical_forms_path):
"""Return a form for which a definition might exist, else None. """Return a form for which a definition might exist, else None.
@ -63,7 +72,7 @@ def lookup(query, lexical_forms_path):
return None 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.""" """Return a list of definition file paths for this lexical form."""
nfkd = unicodedata.normalize("NFKD", query[0]) nfkd = unicodedata.normalize("NFKD", query[0])
first_char = next((c for c in nfkd if not unicodedata.combining(c)), "") 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+") TAG_STRIP_RE = re.compile(r"\s+")
def parse_tag(tag): def parse_tag(tag) -> str:
if isinstance(tag, NavigableString): if isinstance(tag, NavigableString):
return TAG_STRIP_RE.sub(" ", tag) return TAG_STRIP_RE.sub(" ", tag)
content = "" content = ""