From 983a72704a79ad0b482ec59f8dcef17b75fd8119 Mon Sep 17 00:00:00 2001 From: ache Date: Mon, 4 May 2020 05:00:39 +0200 Subject: Arg parsing --- dicofr.py | 52 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/dicofr.py b/dicofr.py index 96f814c..b41f658 100755 --- a/dicofr.py +++ b/dicofr.py @@ -1,14 +1,22 @@ #!/bin/env python -# To load with python interpreter +import sys +import argparse import msgpack -import ui import sqlite3 -import sys +from os.path import exists + + +DIR_PATH = '/usr/share/dicofr' +sys.path.insert(-1, DIR_PATH) + +import ui + +dico = 'wiktfr.sql' def get_def_sql(word): - with sqlite3.connect("result_all.sql") as con: + with sqlite3.connect(dico) as con: cur = con.cursor() data = (word, ) cur.execute('''SELECT * FROM entry WHERE word = ?''', data) @@ -25,7 +33,7 @@ def get_def_sql(word): def get_def_sql_reg(word): - with sqlite3.connect("result_all.sql") as con: + with sqlite3.connect(dico) as con: cur = con.cursor() data = (word, ) cur.execute('''SELECT * FROM entry WHERE word LIKE ?''', data) @@ -41,12 +49,40 @@ def get_def_sql_reg(word): }, res)) -if __name__ == '__main__': +def matching(word): + """ + Find matching words in the list of words + """ + print("Error: Not implemented yet", file=sys.stderr) + pass + +if __name__ == '__main__': if len(sys.argv) < 2: print("Erreur: Rechercher un mot", file=sys.stderr) exit() - for w in get_def_sql(sys.argv[1]): - ui.show_terminal(w) + if not exists(dico): + if not exists(f'{DIR_PATH}/{dico}'): + print('Error: No sqlite dictionnary', file=sys.stderr) + exit(1) + else: + dico = f'{DIR_PATH}/{dico}' + + parser = argparse.ArgumentParser(description='Get a french word\'s definition.') + parser.add_argument('--sql', dest='action', action='store_const', + const=get_def_sql_reg, default=get_def_sql, + help='search a definition using SQL regex, ' + '_ to match a letter, %% to match a group of letters') + parser.add_argument('--matching', dest='matching', action='store_true', + help='search the french words that match the regex') + parser.add_argument('word', metavar='PATTERN', type=str, + help='the word or the pattern to match') + + arg = parser.parse_args() + if arg.matching: + matching(arg.word) + else: + for w in arg.action(arg.word): + ui.show_terminal(w) -- cgit v1.2.3