def test_args_table(): print('test sending args and kwargs to menus:\n') menu_choices = [ TableItem("Change kwargs to Ron McGee", None, change_kwargs), TableItem("Change kwargs to Len Wanger", None, change_kwargs), TableItem( "Change kwargs to Dick Ellis with lambda", None, lambda tag, ad: ad.update({ 'first': 'Dick', 'last': 'Ellis' })), TableItem("call default action (print args and kwargs)", None, TABLE_ITEM_DEFAULT), TableItem("call action_1 (print args and kwargs)", None, action_1), ] my_profile = {'first': 'Len', 'last': 'Wanger'} print('\nmenu.run - with sub-menu\n') use_style = TableStyle(show_cols=False, show_border=False, hrules=RULE_NONE, vrules=RULE_NONE) menu = Table(menu_choices, add_exit=True, style=use_style, default_action=default_action, action_dict=my_profile) menu.run() print('done')
def test_refresh_table(): print('test refresh option in a menu:\n') my_profile = {'first': 'Len', 'last': 'Wanger'} menu_choices = [ TableItem("Change first name from: {first}", None, change_first_name), TableItem("Change last name from: {last}", None, change_last_name), TableItem( "Change kwargs to Dick Ellis with lambda", None, lambda tag, ad: ad.update({ 'first': 'Dick', 'last': 'Ellis' })), TableItem("call default action (print args and kwargs)", None, TABLE_ITEM_DEFAULT), TableItem("call action_1 (print args and kwargs)", None, action_1), ] print('\nmenu.run - dynamic labels - now w/ refresh\n') use_style = TableStyle(show_cols=False, show_border=False, hrules=RULE_NONE, vrules=RULE_NONE) menu = Table(menu_choices, add_exit=True, style=use_style, default_action=default_action, action_dict=my_profile, refresh=True) menu.run() print('done')
def test_sub_table(): sub_menu_1_items = [ TableItem("sub menu 1: Choice 1", 1, TABLE_ITEM_DEFAULT), TableItem("sub menu 1: Choice 2", 2, TABLE_ITEM_DEFAULT), ] use_style = TableStyle(show_cols=False, show_border=False, hrules=RULE_NONE, vrules=RULE_NONE) sub_menu_1 = Table(sub_menu_1_items, title="Sub-Menu 2", add_exit=TABLE_ADD_RETURN, style=use_style) # call submenus two different ways. First by using it as a callable, which calls run on the sub_menu, and second # with an explicit action handler menu_choices = [ TableItem("Choice 1", None, TABLE_ITEM_DEFAULT), TableItem("sub_menu 1", None, sub_menu_1), TableItem("sub_menu 2", None, sub_menu_action), ] print('\nmenu.run - with sub-menu\n') use_style = TableStyle(show_cols=False, show_border=False, hrules=RULE_NONE, vrules=RULE_NONE) menu = Table(menu_choices, add_exit=True, style=use_style) menu.run() print('done')
def test_dynamic_menu_from_db(filter_items=False): engine = create_engine('sqlite:///:memory:', echo=True) Base.metadata.create_all(engine) session_maker = sessionmaker(bind=engine) session = session_maker() session.add_all([ User(name='ed', fullname='Ed Jones', password='******'), User(name='wendy', fullname='Wendy Williams', password='******'), User(name='mary', fullname='Mary Contrary', password='******'), User(name='fred', fullname='Fred Flinstone', password='******') ]) session.commit() qry = session.query(User.name, User.fullname).order_by(User.fullname) tis = [menu_item_factory(row, item_data={'min_len': 3}) for row in qry] for row in tis: print(row) print('adding foo' ) # show that the stored query will update with data changes session.add(User(name='foo', fullname='Foo Winn', password='******')) session.commit() if filter_items: menu = Table(rows=tis, add_exit=True, item_filter=user_filter) else: menu = Table(rows=tis, add_exit=True) menu()
def test_item_filter(): all_roles = {'roles': {'admin', 'user'}} admin_only = {'roles': {'admin'}} menu_choices = [ TableItem("Change roles from: {roles}", None, change_roles, item_data=all_roles), TableItem("Change roles to: 'admin'", None, lambda tag, ad: ad.update({'roles': {'admin'}}), item_data=all_roles), TableItem("call default action (print args and kwargs) - admin only!", None, lambda tag, ad: print('roles={}'.format(ad['roles'])), item_data=admin_only), TableItem("call show_roles", None, show_roles), ] print('\nmenu.run\n') my_profile = {'first': 'Len', 'last': 'Wanger', 'roles': ['user']} menu = Table(menu_choices, add_exit=True, default_action=default_action, action_dict=my_profile, refresh=True, item_filter=role_item_filter) menu.run() print('done')
def sub_menu_action(row, action_dict): print('sub_menu2: row={}, action_dict={}'.format(row, action_dict)) sub_menu_choices = [ TableItem("sub menu 2: Choice 1", 1, TABLE_ITEM_DEFAULT), TableItem("sub menu 2: Choice 2", 2, TABLE_ITEM_DEFAULT), ] sub_menu = Table(sub_menu_choices, title="Sub-Menu 2", show_cols=False, add_exit=TABLE_ADD_RETURN) sub_menu.run()
def sub_menu_action(row, action_dict): print('sub_menu2: row={}, action_dict={}'.format(row, action_dict)) sub_menu_choices = [ TableItem("sub menu 2: Choice 1", 1, TABLE_ITEM_DEFAULT), TableItem("sub menu 2: Choice 2", 2, TABLE_ITEM_DEFAULT), ] use_style = TableStyle(show_cols=False, show_border=False, hrules=RULE_NONE, vrules=RULE_NONE) sub_menu = Table(sub_menu_choices, title="Sub-Menu 2", add_exit=TABLE_ADD_RETURN, style=use_style) sub_menu.run()
def test_show_table(self): people = [ Person('John', 'Cleese', 78, 14), Person('Terry', 'Gilliam', 77, 10), Person('Eric', 'Idle', 75, 12), ] rows = create_rows(people, ['last', 'first', 'shoe_size']) Table(rows, ['First', 'Shoe Size'], tag_str='Last').show_table() print()
def test_dynamic_menu_from_list(filter_items=False): users = [{ 'name': 'ed', 'fullname': 'Ed Jones', 'password': '******' }, { 'name': 'wendy', 'fullname': 'Wendy Williams', 'password': '******' }, { 'name': 'mary', 'fullname': 'Mary Contrary', 'password': '******' }, { 'name': 'leonard', 'fullname': 'Leonard Nemoy', 'password': '******' }, { 'name': 'fred', 'fullname': 'Fred Flintstone', 'password': '******' }] action_dict = {'min_len': 4} tis = [menu_item_factory2(user, 4) for user in users] tis.append( TableItem('Set minimum length ({min_len})', 'filter', set_filter_len_action, hidden=True, item_data={ 'no_filter': True })) # no_filter tells item_filter not to filter the item header = 'Showing users with user length > {min_len}\n' footer = 'type "filter" to change minimum length' use_style = TableStyle(show_cols=False, show_border=False, hrules=RULE_NONE, vrules=RULE_NONE) menu = Table(rows=tis, add_exit=True, style=use_style, action_dict=action_dict, header=header, footer=footer, item_filter=user_filter2) menu()
return 'Better dead than red!' def return_rgb_action(row, action_dict): return row.values[2] if __name__ == '__main__': table_items = [ TableItem('red'), TableItem('blue'), TableItem('green'), ] # simplest way table = Table(table_items, col_names=['Color']) tag = get_table_input(table) print('\ntag={}\n'.format(tag)) # get value from table with specified default action (get values[0] - color name) table = Table(table_items, col_names=['Color'], default_action=TABLE_RETURN_FIRST_VAL, prompt='Color? (Table prompt) ') color_str = get_table_input(table) print('\ncolor_str={}\n'.format(color_str)) # custom action for default table action and on a table item (red) table_items[0] = TableItem('red', action=red_action) table = Table(table_items, col_names=['Color'],
def test_action_table(): menu_choices = [ TableItem("Choice 1 - no specified tag, no specified action", None, None), TableItem("Choice 2 - default action", 2, TABLE_ITEM_DEFAULT), TableItem( "Choice 3 - text tag, lambda action", 'foo', lambda row, kwargs: print('lambda action: row={}, kwargs={}'.format(row, kwargs))), TableItem("Choice 4 - text tag, action handler function specified", 'bar', action_1), TableItem("STOP the menu!", 'stop', TABLE_ITEM_EXIT), ] print('\nget_table_choice - add_exit=True\n') use_style = TableStyle(show_cols=False, show_border=False, vrules=RULE_NONE) menu = Table(menu_choices[:-1], add_exit=True, tag_str="", style=use_style) choice = menu.get_table_choice() show_choice(menu, choice) print( '\nget_table_choice - add_exit=False (no exit!), case_sensitive=True, with title, no columns\n' ) use_style = TableStyle(show_cols=False, show_border=True, vrules=RULE_NONE) menu = Table(menu_choices[:-1], title='My Menu:', add_exit=False, case_sensitive=True, style=use_style) choice = menu.get_table_choice() show_choice(menu, choice) print( '\nget_table_choice - add_exit=False, w/ prompt, default="stop", hrule=ALL, vrule=NONE\n' ) use_style = TableStyle(show_cols=False, hrules=RULE_ALL, vrules=RULE_NONE) menu = Table(menu_choices, prompt='Choose or die!', default_choice='stop', default_action=default_action, add_exit=False, style=use_style) choice = menu.get_table_choice() show_choice(menu, choice) print( '\nget_table_choice - add_exit=False, w/ prompt, default="stop", no columns, no border\n' ) use_style = TableStyle(show_cols=False, show_border=False, hrules=RULE_NONE, vrules=RULE_NONE) menu = Table(menu_choices, prompt='Choose or die!', default_choice='stop', default_action=default_action, add_exit=False, show_border=False, show_cols=False) choice = menu.get_table_choice() show_choice(menu, choice) print('\nmenu.run - add_exit=True\n') menu.add_exit = True menu.run() print('done')