<commit_before># -*- coding: utf-8 -*-
import getpass
import glob
import logging
import os
import pathlib
import sys
import warnings
from collections import OrderedDict
from dataclasses import asdict, dataclass
from pprint import pprint

import box
import ruamel
from ruamel.yaml import YAML

from cellpy.exceptions import ConfigFileNotRead, ConfigFileNotWritten
from cellpy.parameters import prms

DEFAULT_FILENAME_START = ".cellpy_prms_"
DEFAULT_FILENAME_END = ".conf"
USE_MY_DOCUMENTS = False

DEFAULT_FILENAME = DEFAULT_FILENAME_START + "default" + DEFAULT_FILENAME_END

# logger = logging.getLogger(__name__)

yaml = YAML()


def get_user_name():
    """get the user name of the current user (cross platform)"""
    return getpass.getuser()


def create_custom_init_filename(user_name=None):
    """creates a custom prms filename"""
    if user_name is None:
        return DEFAULT_FILENAME_START + get_user_name() + DEFAULT_FILENAME_END
    else:
        return DEFAULT_FILENAME_START + user_name + DEFAULT_FILENAME_END


def get_user_dir_and_dst(init_filename=None):
    """gets the name of the user directory and full prm filepath"""
    if init_filename is None:
        init_filename = create_custom_init_filename()
    user_dir = get_user_dir()
    dst_file = user_dir / init_filename
    return user_dir, dst_file


def get_user_dir():
    """gets the name of the user directory"""
    # user_dir = pathlib.Path(os.path.abspath(os.path.expanduser("~")))
    user_dir = pathlib.Path().home().resolve()
    if os.name == "nt" and USE_MY_DOCUMENTS:
        _user_dir = user_dir / "documents"
        if _user_dir.is_dir():
            user_dir = _user_dir
    return user_dir


def _write_prm_file(file_name=None):
    logging.debug("saving configuration to %s" % file_name)
    config_dict = _pack_prms()

    try:
        with open(file_name, "w") as config_file:
            yaml.allow_unicode = True
            yaml.default_flow_style = False
            yaml.explicit_start = True
            yaml.explicit_end = True
            yaml.dump(config_dict, config_file)
    except ruamel.yaml.YAMLError:
        raise ConfigFileNotWritten


def _update_prms(config_dict):
    logging.debug("updating parameters")
    logging.debug(f"new prms: {config_dict}")
    for key in config_dict:
        if hasattr(prms, key):
            _config_attr = getattr(prms, key)
            for k in config_dict[key]:
                z = config_dict[key][k]
                if isinstance(z, dict):
                    y = getattr(_config_attr, k)
                    z = box.Box({**y, **z})
                if isinstance(z, ruamel.yaml.comments.CommentedMap):
                    z = box.Box(z)
                setattr(_config_attr, k, z)
        else:
            logging.info("\n  not-supported prm: %s" % key)


def _convert_instruments_to_dict(x):
    # Converting instruments to dictionary (since it contains box.Box objects)
    d = asdict(x)
    for k, v in d.items():

        try:
            d[k] = v.to_dict()
        except AttributeError:
            pass

    return d


def _convert_to_dict(x):
    try:
        dictionary = x.to_dict()
    except AttributeError:
        dictionary = asdict(x)
    return dictionary


def _pack_prms():
    """if you introduce new 'save-able' parameter dictionaries, then you have
    to include them here"""

    config_dict = {
        "Paths": _convert_to_dict(prms.Paths),
        "FileNames": _convert_to_dict(prms.FileNames),
        "Db": _convert_to_dict(prms.Db),
        "DbCols": _convert_to_dict(prms.DbCols),
        "DataSet": _convert_to_dict(prms.DataSet),
        "Reader": _convert_to_dict(prms.Reader),
        "Instruments": _convert_instruments_to_dict(prms.Instruments),
        "Batch": _convert_to_dict(prms.Batch),
    }
    return config_dict


def _read_prm_file(prm_filename):
    """read the prm file"""
    logging.debug("Reading config-file: %s" % prm_filename)
    try:
        with open(prm_filename, "r") as config_file:
            prm_dict = yaml.load(config_file)

    except yaml.YAMLError as e:
        raise ConfigFileNotRead from e
    else:
        if isinstance(prm_dict, dict):
            _update_prms(prm_dict)
        else:
            print(type(prm_dict))


def _read_prm_file_without_updating(prm_filename):
    """read the prm file but do not update the params"""
    logging.debug("Reading config-file: %s" % prm_filename)
    try:
        with open(prm_filename, "r") as config_file:
            prm_dict = yaml.load(config_file)

    except yaml.YAMLError as e:<commit_msg>bugfix (yaml error - correct import)<commit_after># -*- coding: utf-8 -*-
import getpass
import glob
import logging
import os
import pathlib
import sys
import warnings
from collections import OrderedDict
from dataclasses import asdict, dataclass
from pprint import pprint

import box
import ruamel
from ruamel.yaml import YAML
from ruamel.yaml.error import YAMLError

from cellpy.exceptions import ConfigFileNotRead, ConfigFileNotWritten
from cellpy.parameters import prms

DEFAULT_FILENAME_START = ".cellpy_prms_"
DEFAULT_FILENAME_END = ".conf"
USE_MY_DOCUMENTS = False

DEFAULT_FILENAME = DEFAULT_FILENAME_START + "default" + DEFAULT_FILENAME_END

# logger = logging.getLogger(__name__)

yaml = YAML()


def get_user_name():
    """get the user name of the current user (cross platform)"""
    return getpass.getuser()


def create_custom_init_filename(user_name=None):
    """creates a custom prms filename"""
    if user_name is None:
        return DEFAULT_FILENAME_START + get_user_name() + DEFAULT_FILENAME_END
    else:
        return DEFAULT_FILENAME_START + user_name + DEFAULT_FILENAME_END


def get_user_dir_and_dst(init_filename=None):
    """gets the name of the user directory and full prm filepath"""
    if init_filename is None:
        init_filename = create_custom_init_filename()
    user_dir = get_user_dir()
    dst_file = user_dir / init_filename
    return user_dir, dst_file


def get_user_dir():
    """gets the name of the user directory"""
    # user_dir = pathlib.Path(os.path.abspath(os.path.expanduser("~")))
    user_dir = pathlib.Path().home().resolve()
    if os.name == "nt" and USE_MY_DOCUMENTS:
        _user_dir = user_dir / "documents"
        if _user_dir.is_dir():
            user_dir = _user_dir
    return user_dir


def _write_prm_file(file_name=None):
    logging.debug("saving configuration to %s" % file_name)
    config_dict = _pack_prms()

    try:
        with open(file_name, "w") as config_file:
            yaml.allow_unicode = True
            yaml.default_flow_style = False
            yaml.explicit_start = True
            yaml.explicit_end = True
            yaml.dump(config_dict, config_file)
    except YAMLError:
        raise ConfigFileNotWritten


def _update_prms(config_dict):
    logging.debug("updating parameters")
    logging.debug(f"new prms: {config_dict}")
    for key in config_dict:
        if hasattr(prms, key):
            _config_attr = getattr(prms, key)
            for k in config_dict[key]:
                z = config_dict[key][k]
                if isinstance(z, dict):
                    y = getattr(_config_attr, k)
                    z = box.Box({**y, **z})
                if isinstance(z, ruamel.yaml.comments.CommentedMap):
                    z = box.Box(z)
                setattr(_config_attr, k, z)
        else:
            logging.info("\n  not-supported prm: %s" % key)


def _convert_instruments_to_dict(x):
    # Converting instruments to dictionary (since it contains box.Box objects)
    d = asdict(x)
    for k, v in d.items():

        try:
            d[k] = v.to_dict()
        except AttributeError:
            pass

    return d


def _convert_to_dict(x):
    try:
        dictionary = x.to_dict()
    except AttributeError:
        dictionary = asdict(x)
    return dictionary


def _pack_prms():
    """if you introduce new 'save-able' parameter dictionaries, then you have
    to include them here"""

    config_dict = {
        "Paths": _convert_to_dict(prms.Paths),
        "FileNames": _convert_to_dict(prms.FileNames),
        "Db": _convert_to_dict(prms.Db),
        "DbCols": _convert_to_dict(prms.DbCols),
        "DataSet": _convert_to_dict(prms.DataSet),
        "Reader": _convert_to_dict(prms.Reader),
        "Instruments": _convert_instruments_to_dict(prms.Instruments),
        "Batch": _convert_to_dict(prms.Batch),
    }
    return config_dict


def _read_prm_file(prm_filename):
    """read the prm file"""
    logging.debug("Reading config-file: %s" % prm_filename)
    try:
        with open(prm_filename, "r") as config_file:
            prm_dict = yaml.load(config_file)

    except YAMLError as e:
        raise ConfigFileNotRead from e
    else:
        if isinstance(prm_dict, dict):
            _update_prms(prm_dict)
        else:
            print(type(prm_dict))


def _read_prm_file_without_updating(prm_filename):
    """read the prm file but do not update the params"""
    logging.debug("Reading config-file: %s" % prm_filename)
    try:
        with open(prm_filename, "r") as config_file:
            prm_dict = yaml.load(config_file)

    except YAMLError as e:
{'fraction_non_alphanumeric': 0.09504627109417528, 'fraction_numerical': 0.00021774632553075667, 'mean_word_length': 2.851572327044025, 'pattern_counts': {'":': 16, '<': 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': '5604448', 'n_tokens_mistral': 3032, 'n_tokens_neox': 2905, 'n_words': 721}