<commit_before>from __future__ import print_function, division, absolute_import

import logging
import os
import sys

from .compatibility import FileExistsError, logging_names

logger = logging.getLogger(__name__)

config = {}

try:
    import yaml
except ImportError:
    pass
else:
    dirname = os.path.dirname(__file__)
    default_path = os.path.join(dirname, 'config.yaml')
    dask_config_path = os.path.join(os.path.expanduser('~'), '.dask', 'config.yaml')

    def ensure_config_file(destination=dask_config_path):
        if not os.path.exists(destination):
            import shutil
            if not os.path.exists(os.path.dirname(destination)):
                try:
                    os.mkdir(os.path.dirname(destination))
                except FileExistsError:
                    pass
            # Atomically create destination.  Parallel testing discovered
            # a race condition where a process can be busy creating the
            # destination while another process reads an empty config file.
            tmp = '%s.tmp.%d' % (destination, os.getpid())
            shutil.copy(default_path, tmp)
            try:
                os.rename(tmp, destination)
            except OSError:
                os.remove(tmp)

    def load_config_file(config, path=dask_config_path):
        if not os.path.exists(path):
            path = default_path
        with open(path) as f:
            text = f.read()
            config.update(yaml.load(text))

    try:
        ensure_config_file()
        load_config_file(config)
    except OSError as e:<commit_msg>Except EnvironmentError when writing config file (#1083)<commit_after>from __future__ import print_function, division, absolute_import

import logging
import os
import sys

from .compatibility import FileExistsError, logging_names

logger = logging.getLogger(__name__)

config = {}

try:
    import yaml
except ImportError:
    pass
else:
    dirname = os.path.dirname(__file__)
    default_path = os.path.join(dirname, 'config.yaml')
    dask_config_path = os.path.join(os.path.expanduser('~'), '.dask', 'config.yaml')

    def ensure_config_file(destination=dask_config_path):
        if not os.path.exists(destination):
            import shutil
            if not os.path.exists(os.path.dirname(destination)):
                try:
                    os.mkdir(os.path.dirname(destination))
                except FileExistsError:
                    pass
            # Atomically create destination.  Parallel testing discovered
            # a race condition where a process can be busy creating the
            # destination while another process reads an empty config file.
            tmp = '%s.tmp.%d' % (destination, os.getpid())
            shutil.copy(default_path, tmp)
            try:
                os.rename(tmp, destination)
            except OSError:
                os.remove(tmp)

    def load_config_file(config, path=dask_config_path):
        if not os.path.exists(path):
            path = default_path
        with open(path) as f:
            text = f.read()
            config.update(yaml.load(text))

    try:
        ensure_config_file()
        load_config_file(config)
    except EnvironmentError as e:
{'fraction_non_alphanumeric': 0.08788819875776398, 'fraction_numerical': 0.0012422360248447205, 'mean_word_length': 2.256825075834176, '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': '75771', 'n_tokens_mistral': 901, 'n_tokens_neox': 868, 'n_words': 227}