parent
e88592bf4e
commit
ce7a300b25
64
cli.py
64
cli.py
|
@ -17,36 +17,29 @@ from os import system
|
||||||
from db import Db
|
from db import Db
|
||||||
from config import DB_REQUEST, CLI_MSG_DISCLAIMER, CLI_MSG_ASK_IDX, \
|
from config import DB_REQUEST, CLI_MSG_DISCLAIMER, CLI_MSG_ASK_IDX, \
|
||||||
CLI_MSG_ASK_ERR, CLI_MSG_QUIT, CLI_MSG_CHOOSEN_CAT, CLI_MSG_PROD, \
|
CLI_MSG_ASK_ERR, CLI_MSG_QUIT, CLI_MSG_CHOOSEN_CAT, CLI_MSG_PROD, \
|
||||||
CLI_MSG_PROD, CLI_MSG_SUBST, CLI_MSG_NO_SUBST
|
CLI_MSG_SUBST, CLI_MSG_NO_SUBST, CLI_MSG_CAT, CLI_MSG_CHOOSEN_PROD
|
||||||
cli_msg = str()
|
|
||||||
|
|
||||||
|
cli_end_msg = str()
|
||||||
product_asked = {'valid_item': False}
|
product_asked = {'valid_item': False}
|
||||||
|
|
||||||
|
|
||||||
def ask_user(head_msg, foot_msg, item_list, db_obj=None):
|
def ask_user(head_msg, item_list):
|
||||||
"""
|
"""
|
||||||
Ask user to choose an item in the provided list, using numeric index
|
Ask user to choose an item in the provided list, using numeric index
|
||||||
|
|
||||||
:head_msg: Text displayed in header
|
:head_msg: Text displayed in header
|
||||||
:foot_msg: Text displayed in footer
|
|
||||||
:item_list: Dict() containing all data about item (see `get_data_list()`)
|
:item_list: Dict() containing all data about item (see `get_data_list()`)
|
||||||
:db_obj: Database object (optionnal)
|
|
||||||
|
|
||||||
:result:
|
:result:
|
||||||
- item:
|
- item:
|
||||||
- cli_msg:
|
- cli_end_msg:
|
||||||
- valid_item:
|
- valid_item:
|
||||||
"""
|
"""
|
||||||
valid_input = False
|
valid_input = False
|
||||||
|
foot_msg = ""
|
||||||
if db_obj is not None:
|
|
||||||
db_msg = db_obj.message
|
|
||||||
else:
|
|
||||||
db_msg = ""
|
|
||||||
|
|
||||||
while valid_input is False:
|
while valid_input is False:
|
||||||
system('clear')
|
system('clear')
|
||||||
print(db_msg)
|
|
||||||
print(head_msg)
|
print(head_msg)
|
||||||
print(item_list['results_txt'])
|
print(item_list['results_txt'])
|
||||||
print(foot_msg)
|
print(foot_msg)
|
||||||
|
@ -72,14 +65,13 @@ def ask_user(head_msg, foot_msg, item_list, db_obj=None):
|
||||||
valid_input = True
|
valid_input = True
|
||||||
valid_item = True
|
valid_item = True
|
||||||
item = item_list['results_list'][user_input]
|
item = item_list['results_list'][user_input]
|
||||||
foot_msg = CLI_MSG_CHOOSEN_CAT.format(item[1])
|
|
||||||
|
|
||||||
# Response not in range, re-ask
|
# Response not in range, re-ask
|
||||||
else:
|
else:
|
||||||
foot_msg += CLI_MSG_ASK_ERR.format(user_input)
|
foot_msg += CLI_MSG_ASK_ERR.format(user_input)
|
||||||
return {
|
return {
|
||||||
'item': item,
|
'item': item,
|
||||||
'cli_msg': foot_msg,
|
'cli_end_msg': foot_msg,
|
||||||
'valid_item': valid_item
|
'valid_item': valid_item
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -142,12 +134,13 @@ category_list = get_data_list(
|
||||||
DB_REQUEST['list_cat']
|
DB_REQUEST['list_cat']
|
||||||
)
|
)
|
||||||
|
|
||||||
|
head_msg = CLI_MSG_DISCLAIMER
|
||||||
|
head_msg += CLI_MSG_CAT
|
||||||
|
|
||||||
# Asks the user to select a category
|
# Asks the user to select a category
|
||||||
category_asked = ask_user(
|
category_asked = ask_user(
|
||||||
CLI_MSG_DISCLAIMER,
|
head_msg,
|
||||||
"WTF0", # cli_msg
|
category_list
|
||||||
category_list,
|
|
||||||
LOCAL_DB
|
|
||||||
)
|
)
|
||||||
|
|
||||||
##################
|
##################
|
||||||
|
@ -157,13 +150,14 @@ if category_asked['valid_item']:
|
||||||
product_list = get_data_list(
|
product_list = get_data_list(
|
||||||
LOCAL_DB, DB_REQUEST['list_prod'].format(category_asked['item'][1])
|
LOCAL_DB, DB_REQUEST['list_prod'].format(category_asked['item'][1])
|
||||||
)
|
)
|
||||||
CLI_MSG_PROD = CLI_MSG_CHOOSEN_CAT.format(category_asked['item'][1]) \
|
|
||||||
+ CLI_MSG_PROD
|
head_msg = CLI_MSG_DISCLAIMER
|
||||||
|
head_msg += CLI_MSG_CHOOSEN_CAT.format(category_asked['item'][1])
|
||||||
|
head_msg += CLI_MSG_PROD
|
||||||
|
|
||||||
# Asks the user to select a product
|
# Asks the user to select a product
|
||||||
product_asked = ask_user(
|
product_asked = ask_user(
|
||||||
CLI_MSG_PROD,
|
head_msg,
|
||||||
"WTF1", # cli_msg
|
|
||||||
product_list
|
product_list
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -179,9 +173,14 @@ if product_asked['valid_item']:
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
head_msg = CLI_MSG_DISCLAIMER
|
||||||
|
head_msg += CLI_MSG_CHOOSEN_CAT.format(category_asked['item'][1])
|
||||||
|
head_msg += CLI_MSG_CHOOSEN_PROD.format(product_asked['item'][1])
|
||||||
|
head_msg += CLI_MSG_SUBST
|
||||||
|
|
||||||
# No substitute found
|
# No substitute found
|
||||||
if substitute_list['max_id'] == -1:
|
if substitute_list['max_id'] == -1:
|
||||||
cli_msg = CLI_MSG_NO_SUBST.format(
|
cli_end_msg = CLI_MSG_NO_SUBST.format(
|
||||||
product_asked['item'][1],
|
product_asked['item'][1],
|
||||||
product_asked['item'][2]
|
product_asked['item'][2]
|
||||||
)
|
)
|
||||||
|
@ -189,22 +188,23 @@ if product_asked['valid_item']:
|
||||||
# Shows product in the same category with a lowest nutriscore
|
# Shows product in the same category with a lowest nutriscore
|
||||||
elif substitute_list['max_id'] > 0:
|
elif substitute_list['max_id'] > 0:
|
||||||
substitute_asked = ask_user(
|
substitute_asked = ask_user(
|
||||||
CLI_MSG_SUBST,
|
head_msg,
|
||||||
"WTF2",
|
|
||||||
substitute_list
|
substitute_list
|
||||||
)
|
)
|
||||||
|
|
||||||
if substitute_asked['valid_item']:
|
if substitute_asked['valid_item']:
|
||||||
cli_msg = "cli_msg : «{}»\n\n".format(cli_msg)
|
cli_end_msg += "category : «{}»\n".format(category_asked['item'][1])
|
||||||
cli_msg += "category : «{}»\n\n".format(category_asked['item'][1])
|
cli_end_msg += "product : «{}»\n".format(product_asked['item'][1])
|
||||||
cli_msg += "product : «{}»\n\n".format(product_asked['item'][1])
|
cli_end_msg += "substitut : «{}»".format(substitute_asked['item'][1])
|
||||||
cli_msg += "substitut : «{}»".format(substitute_asked['item'][1])
|
|
||||||
|
|
||||||
# Asks the user to select a substitute
|
# Asks the user to select a substitute
|
||||||
|
|
||||||
# Saves if user choose it
|
# Saves if user choose it
|
||||||
|
|
||||||
else:
|
else:
|
||||||
cli_msg = CLI_MSG_QUIT
|
cli_end_msg = CLI_MSG_QUIT
|
||||||
|
|
||||||
print(cli_msg)
|
else:
|
||||||
|
cli_end_msg = CLI_MSG_QUIT
|
||||||
|
|
||||||
|
print(cli_end_msg)
|
||||||
|
|
10
config.py
10
config.py
|
@ -33,21 +33,19 @@ DB_REQUEST = {
|
||||||
'save_substitute': "UPDATE product SET substitute_id={} WHERE id={}",
|
'save_substitute': "UPDATE product SET substitute_id={} WHERE id={}",
|
||||||
}
|
}
|
||||||
|
|
||||||
CLI_MSG_DISCLAIMER = "\n# # # Bienvenu sur le terminal # # #\n"\
|
CLI_MSG_DISCLAIMER = "# # # Bienvenu sur le terminal # # #\n\n"
|
||||||
"Voici la liste des catégories disponibles : \n"
|
CLI_MSG_CAT = "Catégories disponibles :\n"
|
||||||
|
|
||||||
CLI_MSG_PROD = "Produits disponibles :\n"
|
CLI_MSG_PROD = "Produits disponibles :\n"
|
||||||
CLI_MSG_SUBST = "Substituts disponibles :\n"
|
CLI_MSG_SUBST = "Substituts disponibles :\n"
|
||||||
CLI_MSG_ASK_IDX = "Index choisi [0-{}] («Q»uitter) :"
|
CLI_MSG_ASK_IDX = "Index choisi [0-{}] («Q»uitter) :"
|
||||||
|
|
||||||
CLI_MSG_ASK_BAK = "Voulez vous sauvegarder «{}» en substitut du produit «{}»?"\
|
CLI_MSG_ASK_BAK = "Voulez vous sauvegarder «{}» en substitut du produit «{}»?"\
|
||||||
" [O/N]\n(«Q» pour quitter): "
|
" [O/N]\n(«Q» pour quitter): "
|
||||||
|
|
||||||
CLI_MSG_ASK_ERR = "\nSaisie incorrecte : «{}»"
|
CLI_MSG_ASK_ERR = "\nSaisie incorrecte : «{}»"
|
||||||
CLI_MSG_QUIT = "\nAu revoir!"
|
CLI_MSG_QUIT = "\nAu revoir!"
|
||||||
CLI_MSG_CHOOSEN_CAT = "# # Categorie : [ {} ]\n"
|
CLI_MSG_CHOOSEN_CAT = "# # Categorie : [ {} ]\n"
|
||||||
CLI_MSG_CHOOSEN_PROD = "\n# Produits : [ {} ]"
|
CLI_MSG_CHOOSEN_PROD = "# Produits : [ {} ]\n"
|
||||||
CLI_MSG_CHOOSEN_SUBST = "\nSubstitut : [ {} ]"
|
CLI_MSG_CHOOSEN_SUBST = "Substitut : [ {} ]\n"
|
||||||
CLI_MSG_NO_SUBST = "Pas de substitut trouvé pour le produit «{}» (nutriscore : «{}»)"
|
CLI_MSG_NO_SUBST = "Pas de substitut trouvé pour le produit «{}» (nutriscore : «{}»)"
|
||||||
CLI_ITEM_MAX_LEN = 15
|
CLI_ITEM_MAX_LEN = 15
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user