<commit_before>#!/usr/bin/env python
"""Configuration related functionality for scriptorium."""

import os
import os.path
import shutil
import yaml

import scriptorium

_DEFAULT_DIR = os.path.join(os.path.expanduser("~"), '.scriptorium')
_DEFAULT_CFG = os.path.join(_DEFAULT_DIR, 'config')

def read_config():
    """Read configuration values for scriptorium."""
    if not os.path.exists(_DEFAULT_CFG):
        tdir = os.path.join(_DEFAULT_DIR, 'templates')
        #For old users, copy templates to new default directory
        here = os.path.dirname(os.path.realpath(__file__))
        old_templates = os.path.join(here, '..', 'templates')
        if os.path.exists(old_templates):
            print('Migrating templates to new directory structure.')
            shutil.copytree(old_templates, tdir)
            print('Templates now in {0}. You should review and remove them from {1}'.format(old_templates, tdir))
        else:
            os.makedirs(tdir)
        scriptorium.TEMPLATE_DIR = tdir
    else:
        with open(_DEFAULT_CFG, 'r') as cfg_fp:
            cfg = yaml.load(cfg_fp)
            scriptorium.TEMPLATE_DIR = cfg['TEMPLATE_DIR']

def save_config():
    """Save configuration values for scriptorium."""
    with open(_DEFAULT_CFG, 'w') as cfg_fp:
        cfg = {'TEMPLATE_DIR': scriptorium.TEMPLATE_DIR}
        yaml.dump(cfg, cfg_fp)<commit_msg>Fix reading config and initializing template directory<commit_after>#!/usr/bin/env python
"""Configuration related functionality for scriptorium."""

import os
import yaml

import scriptorium

_DEFAULT_DIR = os.path.join(os.path.expanduser("~"), '.scriptorium')
_DEFAULT_CFG = os.path.join(_DEFAULT_DIR, 'config')

def read_config():
    """Read configuration values for scriptorium."""
    try:
        with open(_DEFAULT_CFG, 'Ur') as cfg_fp:
            cfg = yaml.load(cfg_fp)
            scriptorium.TEMPLATE_DIR = cfg['TEMPLATE_DIR']
    except EnvironmentError:
        tdir = os.path.join(_DEFAULT_DIR, 'templates')
        if not os.path.exists(tdir):
            os.makedirs(tdir)
        scriptorium.TEMPLATE_DIR = tdir
        #Save configuration from first time
        save_config()

def save_config():
    """Save configuration values for scriptorium."""
    with open(_DEFAULT_CFG, 'w') as cfg_fp:
        cfg = {'TEMPLATE_DIR': scriptorium.TEMPLATE_DIR}
        yaml.dump(cfg, cfg_fp)
{'fraction_non_alphanumeric': 0.11762225969645869, 'fraction_numerical': 0.0008431703204047217, 'mean_word_length': 3.2450805008944545, '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': '2810493', 'n_tokens_mistral': 737, 'n_tokens_neox': 727, 'n_words': 179}