"""Ascii menu class"""
from __future__ import print_function
from cloudmesh_client.common.Printer import Printer
from builtins import input
[docs]def num_choice(n, tries=1):
while tries > 0:
# display()
result = input("Select between {0} - {1}: ".format(1, n))
if result == "q":
return 'q'
else:
try:
result = int(result)
except:
print("invalid input...")
tries -= 1
continue
if 0 < result <= n:
print("choice {0} selected.".format(result))
return result - 1
else:
print("ERROR: wrong selection")
return 'q'
[docs]def dict_choice(d):
if d is None:
return None
elements = dict(d)
i = 1
for e in d:
elements[e]["id"] = i
i += 1
# pprint(d)
if elements != {}:
# noinspection PyPep8
print(Printer.write(elements,
order=["id",
"name",
"comment",
"uri",
"fingerprint",
"source"],
output="table",
sort_keys=True))
else:
print("ERROR: No keys in the database")
return
n = num_choice(i - 1, tries=10) + 1
element = None
for e in elements:
if str(elements[e]["id"]) is str(n):
element = elements[e]
break
return element