<commit_before>"""
Description:   Get and set global variables

Created:       2015-12-11
Last modified: 2015-12-11 22:16
"""
from os import path
from os import environ
from re import findall
from sys import stderr
import configparser

config_file = environ['HOME'] + '/.slurmy'
config      = configparser.ConfigParser()
defaults    = {}


def _create_config():
    """ Create a ~/.slurmy file """
    defaults['queue'] = {'max_jobs':     1000,  # Max number of jobs in queue
                         'sleep_len':    5,     # Between submission attempts (in seconds)
                         'queue_update': 20}    # Amount of time between getting fresh queue info (seconds)
    for k, v in defaults.items():
        config[k] = v
    with open(config_file, 'w') as outfile:
        config.write(outfile)
    stderr.write('Created the file ~/.slurmy with default variables. ' +
                 'Please review this file and edit your defaults\n')
    return defaults


def _get_config():
    """ Load defaults from ~/.slurmy """
    global defaults
    if path.isfile(config_file):
        config.read(config_file)
        for section in config.sections():
            defaults[section] = {}
            for k, v in config[section].items():
                defaults[section][k] = v
    else:
        # Make the config file
        defaults = _create_config()
    return defaults<commit_msg>Added ability to write new or updated config to config file<commit_after>"""
Description:   Get and set global variables

Created:       2015-12-11
Last modified: 2015-12-11 22:42
"""
from os import path
from os import environ
from re import findall
from sys import stderr
import configparser

config_file = environ['HOME'] + '/.slurmy'
config      = configparser.ConfigParser()
defaults    = {}


def _config_to_dict(config):
    """ Convert a config object into a dictionary """
    for section in config.sections():
        defaults[section] = {}
        for k, v in config[section].items():
            defaults[section][k] = v
    return defaults


def _create_config():
    """ Create a ~/.slurmy file """
    global defaults, config
    defaults['queue'] = {'max_jobs':     1000,  # Max number of jobs in queue
                         'sleep_len':    5,     # Between submission attempts (in seconds)
                         'queue_update': 20}    # Amount of time between getting fresh queue info (seconds)
    for k, v in defaults.items():
        config[k] = v
    with open(config_file, 'w') as outfile:
        config.write(outfile)
    stderr.write('Created the file ~/.slurmy with default variables. ' +
                 'Please review this file and edit your defaults\n')
    return defaults


def _get_config():
    """ Load defaults from ~/.slurmy """
    global defaults, config
    if path.isfile(config_file):
        config.read(config_file)
        defaults = _config_to_dict(config)
    else:
        defaults = _create_config()
    return defaults


def write_config(section, key, value):
    """ Write a config key to the ~/.slurmy file """
    global defaults, config
    config.read(config_file)
    config[section][key] = value
    with open(config_file, 'w') as outfile:
        config.write(outfile)
    return _get_config()
{'fraction_non_alphanumeric': 0.1044362292051756, 'fraction_numerical': 0.0166358595194085, 'mean_word_length': 2.556407447973713, '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': '2186985', 'n_tokens_mistral': 947, 'n_tokens_neox': 897, 'n_words': 312}