aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorache <ache@ache.one>2020-05-04 05:00:39 +0200
committerache <ache@ache.one>2020-05-04 05:00:39 +0200
commit983a72704a79ad0b482ec59f8dcef17b75fd8119 (patch)
treef6ff4867b71a54cc4978e241968e2be9f7425f85
parentpick a word by HTTP GET (diff)
Arg parsing
-rwxr-xr-xdicofr.py52
1 files 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)