Validates the user typing and prepares sequel #5

- adds index range
- adds quit option
- makes code compliant with pylint & flake8
This commit is contained in:
Fred Z 2018-08-06 18:12:06 +02:00
parent dadbca63ff
commit 840edb4f40
2 changed files with 71 additions and 24 deletions

83
cli.py
View File

@ -8,46 +8,85 @@ Licence: `GNU GPL v3` GNU GPL v3: http://www.gnu.org/licenses/
Command line interface to interact with local DB
Choose a product in the list of aivaiable categories, the system gives you an
Choose a product in a list of aivaiable CATEGORIES, the system gives you an
alternative with a better 'nutriscore'.
You can save this product to get it later.
"""
from os import system
from db import Db
from config import DB_REQUEST, CLI_MSG_DISCLAIMER, CLI_MSG_ASK_CAT
from config import DB_REQUEST, CLI_MSG_DISCLAIMER, CLI_MSG_ASK_CAT, \
CLI_MSG_ASK_ERR, CLI_MSG_QUIT, CLI_MSG_CHOOSEN_CAT
valid_category = False
cli_msg = str()
# Starts a DB connection
local_db = Db()
# print(local_db.message)
LOCAL_DB = Db()
# print(LOCAL_DB.message)
# Gets category list
local_db.execute(DB_REQUEST['list_cat'])
LOCAL_DB.execute(DB_REQUEST['list_cat'])
IDX_MAX = int(LOCAL_DB.cursor.rowcount - 1)
CATEGORIES = [(idx, val['name'], val['COUNT(*)'])
for idx, val in enumerate(LOCAL_DB.result)]
# Hacky result split for rendering in 2 columns
results_1 = [(idx, val['name'], val['COUNT(*)']) for idx, val in enumerate(local_db.result) if idx%2 != 0]
results_2 = [(idx, val['name'], val['COUNT(*)']) for idx, val in enumerate(local_db.result) if idx%2 == 0]
# Hacky results-split for rendering in 2 columns
RES_EVEN = [(idx, val['name'], val['COUNT(*)'])
for idx, val in enumerate(LOCAL_DB.result) if idx % 2 == 0]
RES_UNEVEN = [(idx, val['name'], val['COUNT(*)'])
for idx, val in enumerate(LOCAL_DB.result) if idx % 2 != 0]
cli_msg = str()
for num, row in enumerate(results_1):
cli_msg += "{} : {} \t\t {} : {}\n".format(
results_2[num][0],
results_2[num][1],
row[0],
row[1]
# category list
CLI_MSG_LIST = ""
for num, row in enumerate(RES_UNEVEN):
CLI_MSG_LIST += "{} : {} \t\t {} : {}\n".format(
RES_EVEN[num][0],
RES_EVEN[num][1],
RES_UNEVEN[num][0],
RES_UNEVEN[num][1]
)
if len(RES_UNEVEN) < len(RES_EVEN):
num += 1
CLI_MSG_LIST += "{} : {}\n".format(
RES_EVEN[num][0],
RES_EVEN[num][1]
)
# Prompts it with an index
system('clear')
print(CLI_MSG_DISCLAIMER)
print(cli_msg)
# Asks the user to enter the index of the selected category
input(CLI_MSG_ASK_CAT)
while valid_category is False:
system('clear')
print(CLI_MSG_DISCLAIMER)
print(CLI_MSG_LIST)
print(cli_msg)
response = input(CLI_MSG_ASK_CAT.format(IDX_MAX))
# If index is not valid, re-ask
if response.lower() == "q":
valid_category = True
cli_msg = CLI_MSG_QUIT
else:
try:
response = int(response)
# Response not int(), re-ask
except ValueError:
cli_msg += CLI_MSG_ASK_ERR.format(response)
# pass
else:
# Response is valid
if response <= IDX_MAX:
valid_category = True
category = CATEGORIES[response]
cli_msg = CLI_MSG_CHOOSEN_CAT.format(category[1])
# Response not in range, re-ask
else:
cli_msg += CLI_MSG_ASK_ERR.format(response)
# Lists all products
@ -58,3 +97,5 @@ input(CLI_MSG_ASK_CAT)
# Shows the 1st product in the same category with the lowest nutriscore
# Saves if user choose it
print(cli_msg)

View File

@ -32,10 +32,16 @@ DB_REQUEST = {
'get_better': "SELECT p.name, p.nutrition_grades FROM product AS p LEFT JOIN category AS c ON p.category_id = c.id WHERE c.name = '{}' AND p.nutrition_grades < '{}'",
'save_substitute': "UPDATE product SET substitute_id={} WHERE id={}",
}
CLI_MSG_DISCLAIMER = "# # # Bienvenu sur le terminal # # #\n"\
"Voici la liste des catégories disponines : \n"
CLI_MSG_ASK_CAT = "Saisissez le nombre de la catégorie choisie : "
CLI_MSG_DISCLAIMER = "# # # Bienvenu sur le terminal # # #\n"\
"Voici la liste des catégories disponibles : \n"
CLI_MSG_ASK_CAT = "Saisissez le nombre de la catégorie choisie [0-{}]"\
"\n(«Q» pour quitter): "
CLI_MSG_ASK_ERR = "\nSaisie incorrecte : «{}»"
CLI_MSG_QUIT = "\nAu revoir!"
CLI_MSG_CHOOSEN_CAT = "\n= = = = =[ {} ]= = = ="
# DATABASE