Esempio n. 1
0
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')
Esempio n. 2
0
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')
Esempio n. 3
0
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')
Esempio n. 4
0
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')
Esempio n. 5
0
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')
    menu = Table(menu_choices[:-1], add_exit=True, tag_str="")
    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'
    )
    menu = Table(menu_choices[:-1],
                 title='My Menu:',
                 add_exit=False,
                 case_sensitive=True,
                 show_cols=False)
    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'
    )
    menu = Table(menu_choices,
                 prompt='Choose or die!',
                 default_choice='stop',
                 default_action=default_action,
                 add_exit=False,
                 hrules=RULE_ALL,
                 vrules=RULE_NONE)
    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'
    )
    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')
Esempio n. 6
0
def menu_item_factory2(row, min_len):
    # test item factory that sets the item to hidden if it's shorter than the minimum length set in the item_data dict
    if len(row['name']) > min_len - 1:
        return TableItem(row['fullname'], None, TABLE_ITEM_DEFAULT)
    else:  # hide short names!
        return TableItem(row['fullname'],
                         None,
                         TABLE_ITEM_DEFAULT,
                         hidden=True,
                         enabled=True)
Esempio n. 7
0
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()
Esempio n. 8
0
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()
Esempio n. 9
0
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()
Esempio n. 10
0

def red_action(row, action_dict):
    if action_dict['live'] is True:
        return 'Live is True!'
    else:
        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)
Esempio n. 11
0
def menu_item_factory(row, item_data):
    return TableItem(row.fullname, None, TABLE_ITEM_DEFAULT, item_data)