Example #1
0
def parse_db_files():

    config = get_config()

    if not 'database_files' in config.sections():
        print()
        default_print_error(
            'Error: There is no [database_files] section in your config file.'
        )
        print()
        write_example_config()

    db_files = []

    for db_file in config['database_files']:
        db_files.append(db_file)
        
    if len(db_files) == 0:
        print()
        default_print_error(
            'Error: No databases are listed in your config file.'
        )
        default_print_instruction(
            'Write a name for a database file into its [database_files] section.'
        )
        default_print_info('This file will be created the next time you run garrick,')
        default_print_info('or it will be used if it already exists.')
        print()
        write_example_config()

    return db_files
Example #2
0
def write_example_config():
    
    garrick_dir, config_file_name = locate_config_file()

    default_print_info(
        'Your config file is {}.'.format(os.path.join(garrick_dir, config_file_name))
    )
    default_print_info(
        'I am writing a file called {}.example into the same directory.'.format(config_file_name)
    )
    default_print_instruction(
        'You can work from this file to restore your garrick.conf file to a valid state.'
    )
    print()

    example_config_file = resource_filename(Requirement.parse('garrick'), 'garrick.conf.example')

    copyfile(example_config_file, os.path.join(garrick_dir, '{}.example'.format(config_file_name)))

    raise Exception('Invalid or incomplete config file.')
Example #3
0
def parse_editor():

    config = get_config()

    if not 'config' in config.sections():
        print()
        default_print_error('Error: There is no [config] section in your config file.')
        print()
        write_example_config()
        
    if not 'editor' in config['config']:
        print()
        default_print_error(
            'Error: There is no "editor" variable in the [config] section of your config file.'
        )
        print()
        write_example_config()

    editor = config['config']['editor']

    if editor == '' or editor == None:

        editor = os.getenv('EDITOR')
            
        if editor == None:
            print()
            default_print_error('Error: No editor is defined in your config file.')
            default_print_instruction(
                'Add the name of your favourite editor at the end of the line "editor = "'
            )
            default_print_instruction('so you can use it to edit your cards.')
            default_print_info(
                "(This is normal if you haven't set the editor variable before.)"
            )
            print()
            write_example_config()
    
    return editor
Example #4
0
def create_config_file(garrick_dir):

    os.mkdir(garrick_dir)
    
    default_editor = os.getenv('EDITOR')

    config_file_full_path = os.path.join(garrick_dir, config_file_name)

    with open(config_file_full_path, 'w') as f:
        f.write('[database_files]\n')
        f.write('cards.db\n\n')
        f.write('[config]\n')
        if default_editor == None:
            f.write('editor =\n')
        else:
            f.write('editor = {}\n'.format(default_editor))
        f.write('\n# COLOURS\n')
        f.write('# Available choices are:\n')
        f.write('# black, red, green, yellow, blue, magenta, cyan, white,\n')
        f.write('# brightblack, brightred, etc.\n')
        f.write('info = brightgreen\n')
        f.write('error = brightred\n')
        f.write('instruction = brightmagenta\n')
        f.write('side_of_card = brightyellow\n')
        f.write('prompt = brightcyan\n')
        f.write('silent_prompt = brightyellow\n')

    print_info('Created directory {}.'.format(garrick_dir))
    if default_editor == None:
        default_print_instruction(
            'You might want to set the "editor" variable in {},'.format(config_file_full_path)
        )
        default_print_instruction('so that you can edit your cards with your favourite editor.')
    else:
        default_print_info('Editor set to {}.'.format(default_editor))
        default_print_instruction('(You can change this in {}.)'.format(config_file_full_path))