Beispiel #1
0
def main():

    # Initialise colorama
    colorama.init()

    valid_args = ['-n', '-n2', '-s', '-s2', '-e', '-e2', '-b', '-bf', '-bb', '-bs', '-bl', '-br']

    if len(sys.argv) > 1 and sys.argv[1] not in valid_args:
        print_usage_info(sys.argv)        
        if sys.argv[1] not in ['-h', '--help']:
            sys.exit(1)
        sys.exit()

    db_file = pick_db_file()
    conn, cursor = db_connection.connect(db_file)
    card_repository.create_table_if_not_exists(conn, cursor)
    
    if len(sys.argv) == 1:
        table_is_empty = card_repository.check_if_empty(cursor)
        if table_is_empty:
            print_error("You don't have any cards yet.")
            print_instruction(
                'Create some cards by launching garrick with one of the following options first:'
            )
            print_instruction('\t-n\tCreate cards starting in one-way mode.')
            print_instruction('\t-n2\tCreate cards starting in two-way mode.')
            print_instruction('\t-s\tCreate cards starting in single-line and one-way mode.')
            print_instruction('\t-s2\tCreate cards starting in single-line and two-way mode.')
            print_instruction('\t-e\tCreate cards starting in editor mode and in one-way mode.')
            print_instruction('\t-s2\tCreate cards starting in editor mode and in two-way mode.')
        else:
            review.review(conn, cursor)
    elif sys.argv[1] == '-n':
        new_cards(conn, cursor, two_way_card=False, single_line_mode=False, editor_mode=False)
    elif sys.argv[1] == '-n2':
        new_cards(conn, cursor, two_way_card=True, single_line_mode=False, editor_mode=False)
    elif sys.argv[1] == '-s':
        new_cards(conn, cursor, two_way_card=False, single_line_mode=True, editor_mode=False)
    elif sys.argv[1] == '-s2':
        new_cards(conn, cursor, two_way_card=True, single_line_mode=True, editor_mode=False)
    elif sys.argv[1] == '-e':
        new_cards(conn, cursor, two_way_card=False, single_line_mode=False, editor_mode=True)
    elif sys.argv[1] == '-e2':
        new_cards(conn, cursor, two_way_card=True, single_line_mode=False, editor_mode=True)
    elif sys.argv[1] == '-b':
        review.browse_by_regex(conn, cursor) 
    elif sys.argv[1] == '-bf':
        review.browse_by_regex_front(conn, cursor) 
    elif sys.argv[1] == '-bb':
        review.browse_by_regex_back(conn, cursor) 
    elif sys.argv[1] == '-bs':
        review.browse_by_score(conn, cursor)
    elif sys.argv[1] == '-bl':
        review.browse_by_last_viewed(conn, cursor)
    elif sys.argv[1] == '-br':
        review.browse_by_last_viewed_reverse(conn, cursor)
    
    print_info('Kbai')
    
    db_connection.disconnect(conn, cursor)
Beispiel #2
0
def iterate(conn, cursor, iterator, count):

    if count == 0:
        print_error('No results.')
    elif count == 1:
        print_info('[1 CARD.]')
    else:
        print_info('[{} CARDS.]'.format(count))

    try:
        for card in iterator:

            print_instruction('[Ctrl+C to quit.]')

            updated_card, contents_changed = review_card(card)

            is_two_way_card = card_repository.is_two_way_card(cursor, card)

            if is_two_way_card and contents_changed:
                flipped_card_too = include_flipped_card()
            else:
                flipped_card_too = False

            if updated_card == None:
                card_repository.delete(conn, cursor, card)
                if flipped_card_too:
                    card_repository.delete_flipped_card(conn, cursor, card)
                    count -= 1
                # Not an error, but I feel this should be red.
                print_error('DELETED.')
                table_is_empty = card_repository.check_if_empty(cursor)
                if table_is_empty:
                    print_info('You have no more cards!')
                    break
            else:
                if contents_changed:

                    while True:
                        card_repository.insert(conn, cursor, updated_card)
                        if flipped_card_too:
                            card_repository.insert_flipped_card(conn, cursor, updated_card)
                        else:
                            bump_flipped_card_timestamp(conn, cursor, card)
                        print_instruction('Create another version based on this same card?')
                        print_info('Protip: This is how you split one card into several cards.')
                        while True:
                            answer = colored_prompt('[y/n]: ')
                            if answer in ['y', 'n']:
                                break
                        if answer == 'n':
                            break
                        else:
                            updated_card = edit(card, flipped_card_too)

                    card_repository.delete(conn, cursor, card)
                    if flipped_card_too:
                        card_repository.delete_flipped_card(conn, cursor, card)
                else:
                    card_repository.insert(conn, cursor, updated_card)
                    card_repository.delete(conn, cursor, card)
                    bump_flipped_card_timestamp(conn, cursor, card)
            
            count -= 1

            if count > 0:
                print_info('[{} MORE:]'.format(count))

    except KeyboardInterrupt:
        print()