<commit_before># coding=utf-8

"""
Class for YAML-based data files
"""
import os

from ruamel import yaml
from typing import Any, List, Dict

from ultros.core.storage import manager as m
from ultros.core.storage.base import MutableAbstractDictFunctionsMixin, MutableAbstractItemAccessMixin
from ultros.core.storage.data.base import DataFile

__author__ = "<NAME>"


class YAMLData(DataFile, MutableAbstractItemAccessMixin, MutableAbstractDictFunctionsMixin):
    """
    Class for YAML-based data files
    """

    def __init__(self, owner: Any, manager: "m.StorageManager", path: str, *args: List[Any], **kwargs: Dict[Any, Any]):
        self.data = {}

        super().__init__(owner, manager, path, *args, **kwargs)

    def load(self):
        if os.path.exists(self.path):
            with open(self.path, "r") as fh:
                self.data = yaml.load(fh, version=(1, 2))
        else:
            self.data = {}

    def save(self):
        with open(self.path, "w") as fh:
            yaml.dump(self.data, fh)

    def reload(self):
        self.unload()
        self.load()

    def unload(self):
        self.clear()

    # region: Dict functions

    def clear(self):
        return self.data.clear()

    def copy(self):
        return self.data.copy()

    def get(self, key, default=None):
        return self.data.get(key, default)

    def items(self):
        return self.data.items()

    def keys(self):
        return self.data.keys()

    def pop(self, key, default=None):
        return self.data.pop(key, default)

    def popitem(self):
        return self.data.popitem()

    def setdefault(self, key, default=None):
        if key not in self.data:
            self.data[key] = default
            return default

        return self.data[key]

    def update(self, other):
        return self.data.update(other)

    def values(self):
        return self.data.values()

    # endregion

    # Item access functions

    def __contains__(self, key):
        """
        Wrapper for `dict.__contains__()`
        """

        return self.data.__contains__(key)

    def __delitem__(self, key):
        """
        Wrapper for `dict.__delitem__()`
        """

        del self.data[key]

    def __getitem__(self, key):
        """
        Wrapper for `dict.__getitem__()`
        """

        return self.data.__getitem__(key)

    def __iter__(self):
        """
        Wrapper for `dict.__iter__()`
        """

        return self.data.__iter__()

    def __len__(self):
        """
        Wrapper for `dict.__len__()`
        """

        return self.data.__len__()

    def __setitem__(self, key, value):
        """
        Wrapper for `dict.__getitem__()`
        """

        return self.data.__setitem__(key, value)
<commit_msg>Squelch unsafe YAML loader warning for data files<commit_after># coding=utf-8

"""
Class for YAML-based data files
"""
import os

from ruamel import yaml
from typing import Any, List, Dict

from ultros.core.storage import manager as m
from ultros.core.storage.base import MutableAbstractDictFunctionsMixin, MutableAbstractItemAccessMixin
from ultros.core.storage.data.base import DataFile

__author__ = "<NAME>"


class YAMLData(DataFile, MutableAbstractItemAccessMixin, MutableAbstractDictFunctionsMixin):
    """
    Class for YAML-based data files
    """

    def __init__(self, owner: Any, manager: "m.StorageManager", path: str, *args: List[Any], **kwargs: Dict[Any, Any]):
        self.data = {}

        super().__init__(owner, manager, path, *args, **kwargs)

    def load(self):
        if os.path.exists(self.path):
            with open(self.path, "r") as fh:
                self.data = yaml.load(fh, version=(1, 2), Loader=yaml.Loader)
        else:
            self.data = {}

    def save(self):
        with open(self.path, "w") as fh:
            yaml.dump(self.data, fh)

    def reload(self):
        self.unload()
        self.load()

    def unload(self):
        self.clear()

    # region: Dict functions

    def clear(self):
        return self.data.clear()

    def copy(self):
        return self.data.copy()

    def get(self, key, default=None):
        return self.data.get(key, default)

    def items(self):
        return self.data.items()

    def keys(self):
        return self.data.keys()

    def pop(self, key, default=None):
        return self.data.pop(key, default)

    def popitem(self):
        return self.data.popitem()

    def setdefault(self, key, default=None):
        if key not in self.data:
            self.data[key] = default
            return default

        return self.data[key]

    def update(self, other):
        return self.data.update(other)

    def values(self):
        return self.data.values()

    # endregion

    # Item access functions

    def __contains__(self, key):
        """
        Wrapper for `dict.__contains__()`
        """

        return self.data.__contains__(key)

    def __delitem__(self, key):
        """
        Wrapper for `dict.__delitem__()`
        """

        del self.data[key]

    def __getitem__(self, key):
        """
        Wrapper for `dict.__getitem__()`
        """

        return self.data.__getitem__(key)

    def __iter__(self):
        """
        Wrapper for `dict.__iter__()`
        """

        return self.data.__iter__()

    def __len__(self):
        """
        Wrapper for `dict.__len__()`
        """

        return self.data.__len__()

    def __setitem__(self, key, value):
        """
        Wrapper for `dict.__getitem__()`
        """

        return self.data.__setitem__(key, value)

{'fraction_non_alphanumeric': 0.1245977833392921, 'fraction_numerical': 0.0010725777618877368, 'mean_word_length': 2.211825487944891, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5199902', 'n_tokens_mistral': 1769, 'n_tokens_neox': 1653, 'n_words': 424}