<commit_before>from csv2_common import check_keys, show_table

import json
import os
import shutil
import yaml

def delete(gvar):
    """
    Delete settings.
    """

    mandatory = ['-s']
    required = []
    optional = ['-H', '-h', '-Y']

    if gvar['retrieve_options']:
        return mandatory + required + optional

    # Check for missing arguments or help required.
    check_keys(gvar, mandatory, required, optional, requires_server=False)

    if os.path.isdir('%s/.csv2/%s' % (gvar['home_dir'], gvar['server'])):
        # Confirm settings delete.
        if not gvar['user_settings']['yes']:
            print('Are you sure you want to delete the settings for server "%s"? (yes|..)' % gvar['server'])
            _reply = input()
            if _reply != 'yes':
                print('%s settings delete "%" cancelled.' % gvar['server'] % gvar['command_name'])
                exit(0)

        shutil.rmtree('%s/.csv2/%s' % (gvar['home_dir'], gvar['server']))
    else:
        print('Error: Settings for server "%s" do not exist.' % gvar['server'])
        exit(1)

def list(gvar):
    """
    List settings.
    """

    mandatory = []
    required = []
    optional = ['-H', '-h', '-NV', '-ok', '-r', '-s', '-V', '-VC']

    if gvar['retrieve_options']:
        return mandatory + required + optional

    # Check for missing arguments or help required.
    check_keys(gvar, mandatory, required, optional, requires_server=False)

    # Retrive all possible option names ordered by 'server' and then alphabetically.
    _keys = ['server,k']
    for _ix in range(len(gvar['command_keys'])):
        key = gvar['command_keys'][_ix][1][2:]
        if key not in _keys:
            _keys.append(key)
    _keys = [_keys[0]] + sorted(_keys[1:])

    # Build a queryset of the settings.
    _queryset = []
    for server in os.listdir('%s/.csv2' % gvar['home_dir']):
        if server[0] == '.':
            continue

        server_path = '%s/.csv2//%s' % (gvar['home_dir'], server)
        if os.path.isdir(server_path):
            if 'server' not in gvar['command_args'] or server == gvar['command_args']['server']:
                _fd = open('%s/settings.yaml' % server_path)
                _settings = yaml.load(_fd.read())
                _fd.close()

                for key in sorted(_keys):
                    if key not in _settings:
                        if key == 'server':
                            _settings[key] = server
                
                _queryset.append({'fields': _settings})

    # Display results.
    show_table(gvar, _queryset, _keys, allow_null=False, title='Defaults:')

def set(gvar):
    """
    Modify settings.
    """

    mandatory = ['-s']
    required = []
    optional = ['*']
    not_optional = ['-br']

    if gvar['retrieve_options']:
        return mandatory + required + optional

    # Check for missing arguments or help required.
    check_keys(gvar, mandatory, required, optional, not_optional=not_optional, requires_server=False)

    # Make the server directory, if necessary.
    if not os.path.exists('%s/.csv2/%s' % (gvar['home_dir'], gvar['server'])):
        os.makedirs('%s/.csv2/%s' % (gvar['home_dir'], gvar['server']), mode=0o700)  

    # Write the settings file.
    _fd = open('%s/.csv2/%s/settings.yaml' % (gvar['home_dir'], gvar['server']), 'w')
    _fd.write(yaml.dump(gvar['user_settings']))
    _fd.close()
    os.chmod('%s/.csv2/default/settings.yaml' % gvar['home_dir'], 0o600)

<commit_msg>updating the defualts for a server makes it the default server<commit_after>from csv2_common import check_keys, show_table

import json
import os
import shutil
import yaml

def delete(gvar):
    """
    Delete settings.
    """

    mandatory = ['-s']
    required = []
    optional = ['-H', '-h', '-Y']

    if gvar['retrieve_options']:
        return mandatory + required + optional

    # Check for missing arguments or help required.
    check_keys(gvar, mandatory, required, optional, requires_server=False)

    if os.path.isdir('%s/.csv2/%s' % (gvar['home_dir'], gvar['server'])):
        # Confirm settings delete.
        if not gvar['user_settings']['yes']:
            print('Are you sure you want to delete the settings for server "%s"? (yes|..)' % gvar['server'])
            _reply = input()
            if _reply != 'yes':
                print('%s settings delete "%" cancelled.' % gvar['server'] % gvar['command_name'])
                exit(0)

        shutil.rmtree('%s/.csv2/%s' % (gvar['home_dir'], gvar['server']))
    else:
        print('Error: Settings for server "%s" do not exist.' % gvar['server'])
        exit(1)

def list(gvar):
    """
    List settings.
    """

    mandatory = []
    required = []
    optional = ['-H', '-h', '-NV', '-ok', '-r', '-s', '-V', '-VC']

    if gvar['retrieve_options']:
        return mandatory + required + optional

    # Check for missing arguments or help required.
    check_keys(gvar, mandatory, required, optional, requires_server=False)

    # Retrive all possible option names ordered by 'server' and then alphabetically.
    _keys = ['server,k']
    for _ix in range(len(gvar['command_keys'])):
        key = gvar['command_keys'][_ix][1][2:]
        if key not in _keys:
            _keys.append(key)
    _keys = [_keys[0]] + sorted(_keys[1:])

    # Build a queryset of the settings.
    _queryset = []
    for server in os.listdir('%s/.csv2' % gvar['home_dir']):
        if server[0] == '.':
            continue

        server_path = '%s/.csv2//%s' % (gvar['home_dir'], server)
        if os.path.isdir(server_path):
            if 'server' not in gvar['command_args'] or server == gvar['command_args']['server']:
                _fd = open('%s/settings.yaml' % server_path)
                _settings = yaml.load(_fd.read())
                _fd.close()

                for key in sorted(_keys):
                    if key not in _settings:
                        if key == 'server':
                            _settings[key] = server
                
                _queryset.append({'fields': _settings})

    # Display results.
    show_table(gvar, _queryset, _keys, allow_null=False, title='Defaults:')

def set(gvar):
    """
    Modify settings.
    """

    mandatory = ['-s']
    required = []
    optional = ['*']
    not_optional = ['-br']

    if gvar['retrieve_options']:
        return mandatory + required + optional

    # Check for missing arguments or help required.
    check_keys(gvar, mandatory, required, optional, not_optional=not_optional, requires_server=False)

    # Make the server directory, if necessary.
    if not os.path.exists('%s/.csv2/%s' % (gvar['home_dir'], gvar['server'])):
        os.makedirs('%s/.csv2/%s' % (gvar['home_dir'], gvar['server']), mode=0o700)  

    # Write the settings file.
    _fd = open('%s/.csv2/%s/settings.yaml' % (gvar['home_dir'], gvar['server']), 'w')
    _fd.write(yaml.dump(gvar['user_settings']))
    _fd.close()
    os.chmod('%s/.csv2/%s/settings.yaml' % (gvar['home_dir'], gvar['server']), 0o600)

    _fd = open('%s/.csv2/default_server' % gvar['home_dir'], 'w')
    _fd.write(gvar['server'])
    _fd.close()
    os.chmod('%s/.csv2/default_server' % gvar['home_dir'], 0o600)


{'fraction_non_alphanumeric': 0.16303895743795924, 'fraction_numerical': 0.0074864827394981286, 'mean_word_length': 2.488394584139265, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2097995', 'n_tokens_mistral': 2343, 'n_tokens_neox': 2267, 'n_words': 558}