<commit_before>import os
import yaml
import toml
import errno
import shutil
import json
import grp

from pwd import getpwnam
from collections import OrderedDict

from moulinette import m18n
from moulinette.core import MoulinetteError

# Files & directories --------------------------------------------------


def read_file(file_path):
    """
    Read a regular text file

    Keyword argument:
        file_path -- Path to the text file
    """
    assert isinstance(
        file_path, str
    ), "Error: file_path '%s' should be a string but is of type '%s' instead" % (
        file_path,
        type(file_path),
    )

    # Check file exists
    if not os.path.isfile(file_path):
        raise MoulinetteError("file_not_exist", path=file_path)

    # Open file and read content
    try:
        with open(file_path, "r") as f:
            file_content = f.read()
    except IOError as e:
        raise MoulinetteError("cannot_open_file", file=file_path, error=str(e))
    except Exception as e:
        raise MoulinetteError(
            "unknown_error_reading_file", file=file_path, error=str(e)
        )

    return file_content


def read_json(file_path):
    """
    Read a json file

    Keyword argument:
        file_path -- Path to the json file
    """

    # Read file
    file_content = read_file(file_path)

    # Try to load json to check if it's syntaxically correct
    try:
        loaded_json = json.loads(file_content)
    except ValueError as e:
        raise MoulinetteError("corrupted_json", ressource=file_path, error=str(e))

    return loaded_json


def read_yaml(file_path):
    """
    Safely read a yaml file

    Keyword argument:
        file_path -- Path to the yaml file
    """

    # Read file
    file_content = read_file(file_path)

    # Try to load yaml to check if it's syntaxically correct
    try:
        loaded_yaml = yaml.safe_load(file_content)
    except Exception as e:
        raise MoulinetteError("corrupted_yaml", ressource=file_path, error=str(e))

    return loaded_yaml


def read_toml(file_path):
    """<commit_msg>[enh] read_yaml support stream<commit_after>import os
import yaml
import toml
import errno
import shutil
import json
import grp

from pwd import getpwnam
from collections import OrderedDict

from moulinette import m18n
from moulinette.core import MoulinetteError

# Files & directories --------------------------------------------------


def read_file(file_path):
    """
    Read a regular text file

    Keyword argument:
        file_path -- Path to the text file
    """
    assert isinstance(
        file_path, str
    ), "Error: file_path '%s' should be a string but is of type '%s' instead" % (
        file_path,
        type(file_path),
    )

    # Check file exists
    if not os.path.isfile(file_path):
        raise MoulinetteError("file_not_exist", path=file_path)

    # Open file and read content
    try:
        with open(file_path, "r") as f:
            file_content = f.read()
    except IOError as e:
        raise MoulinetteError("cannot_open_file", file=file_path, error=str(e))
    except Exception as e:
        raise MoulinetteError(
            "unknown_error_reading_file", file=file_path, error=str(e)
        )

    return file_content


def read_json(file_path):
    """
    Read a json file

    Keyword argument:
        file_path -- Path to the json file
    """

    # Read file
    file_content = read_file(file_path)

    # Try to load json to check if it's syntaxically correct
    try:
        loaded_json = json.loads(file_content)
    except ValueError as e:
        raise MoulinetteError("corrupted_json", ressource=file_path, error=str(e))

    return loaded_json


def read_yaml(file_):
    """
    Safely read a yaml file

    Keyword argument:
        file -- Path or stream to the yaml file
    """

    # Read file
    file_path = file_ if isinstance(file_, str) else file_.name
    file_content = read_file(file_) if isinstance(file_, str) else file_

    # Try to load yaml to check if it's syntaxically correct
    try:
        loaded_yaml = yaml.safe_load(file_content)
    except Exception as e:
        raise MoulinetteError("corrupted_yaml", ressource=file_path, error=str(e))

    return loaded_yaml


def read_toml(file_path):
    """
{'fraction_non_alphanumeric': 0.09969576410016381, 'fraction_numerical': 0.000936110461034402, 'mean_word_length': 2.5557404326123128, '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': '100150', 'n_tokens_mistral': 1330, 'n_tokens_neox': 1286, 'n_words': 401}