<commit_before>import collections
import copy
import json
import os

import pkg_resources
import toml

from dcos import constants, jsonitem, util
from dcos.errors import DCOSException

logger = util.get_logger(__name__)


def uses_deprecated_config():
    """Returns True if the configuration for the user's CLI
    is the deprecated 'global' config instead of the cluster
    specific config
    """

    global_config = get_global_config_path()
    cluster_config = get_clusters_path()
    return not os.path.exists(cluster_config) and os.path.exists(global_config)


def get_global_config_path():
    """Returns the path to the deprecated global DCOS config file.

    :returns: path to the DCOS config file
    :rtype: str
    """

    default_path = os.path.join(get_config_dir_path(), "dcos.toml")
    return os.environ.get(constants.DCOS_CONFIG_ENV, default_path)


def get_global_config(mutable=False):
    """Returns the deprecated global DCOS config file

    :param mutable: True if the returned Toml object should be mutable
    :type mutable: boolean
    :returns: Configuration object
    :rtype: Toml | MutableToml
    """

    return load_from_path(get_global_config_path(), mutable)


def get_attached_cluster_path():
    """
    The attached cluster is denoted by a file named "attached" in one of the
    cluster directories. Ex: $DCOS_DIR/clusters/CLUSTER_ID/attached

    :returns: path to the director of the attached cluster
    :rtype: str | None
    """

    path = get_clusters_path()
    if not os.path.exists(path):
        return None

    attached = None
    clusters = os.listdir(get_clusters_path())
    cluster_envvar = os.environ.get(constants.DCOS_CLUSTER)

    for c in clusters:
        cluster_path = os.path.join(path, c)
        name = get_config_val("cluster.name",
                              load_from_path(
                                  os.path.join(cluster_path, "dcos.toml")))
        if cluster_envvar is not None \
           and (cluster_envvar == c or cluster_envvar == name):
            return cluster_path
        if os.path.exists(os.path.join(
                cluster_path, constants.DCOS_CLUSTER_ATTACHED_FILE)):
            attached = cluster_path

    if attached is not None:
        return attached

    # if only one cluster, set as attached
    if len(clusters) == 1:
        cluster_path = os.path.join(path, clusters[0])
        util.ensure_file_exists(os.path.join(
            cluster_path, constants.DCOS_CLUSTER_ATTACHED_FILE))
        return cluster_path

    return None


def get_clusters_path():
    """
    :returns: path to the directory of cluster configs
    :rtype: str
    """

    return os.path.join(get_config_dir_path(), constants.DCOS_CLUSTERS_SUBDIR)


def get_config_path():
    """Returns the path to the DCOS config file of the attached cluster.
    If still using "global" config return that toml instead

    :returns: path to the DCOS config file
    :rtype: str
    """

    if uses_deprecated_config():
        return get_global_config_path()
    else:
        cluster_path = get_attached_cluster_path()
        return os.path.join(cluster_path, "dcos.toml")


def get_config_dir_path():
    """
    :returns: path to the DC/OS data directory<commit_msg>Print a clear error when no cluster is attached<commit_after>import collections
import copy
import json
import os

import pkg_resources
import toml

from dcos import constants, jsonitem, util
from dcos.errors import DCOSException

logger = util.get_logger(__name__)


def uses_deprecated_config():
    """Returns True if the configuration for the user's CLI
    is the deprecated 'global' config instead of the cluster
    specific config
    """

    global_config = get_global_config_path()
    cluster_config = get_clusters_path()
    return not os.path.exists(cluster_config) and os.path.exists(global_config)


def get_global_config_path():
    """Returns the path to the deprecated global DCOS config file.

    :returns: path to the DCOS config file
    :rtype: str
    """

    default_path = os.path.join(get_config_dir_path(), "dcos.toml")
    return os.environ.get(constants.DCOS_CONFIG_ENV, default_path)


def get_global_config(mutable=False):
    """Returns the deprecated global DCOS config file

    :param mutable: True if the returned Toml object should be mutable
    :type mutable: boolean
    :returns: Configuration object
    :rtype: Toml | MutableToml
    """

    return load_from_path(get_global_config_path(), mutable)


def get_attached_cluster_path():
    """
    The attached cluster is denoted by a file named "attached" in one of the
    cluster directories. Ex: $DCOS_DIR/clusters/CLUSTER_ID/attached

    :returns: path to the director of the attached cluster
    :rtype: str | None
    """

    path = get_clusters_path()
    if not os.path.exists(path):
        return None

    attached = None
    clusters = os.listdir(get_clusters_path())
    cluster_envvar = os.environ.get(constants.DCOS_CLUSTER)

    for c in clusters:
        cluster_path = os.path.join(path, c)
        name = get_config_val("cluster.name",
                              load_from_path(
                                  os.path.join(cluster_path, "dcos.toml")))
        if cluster_envvar is not None \
           and (cluster_envvar == c or cluster_envvar == name):
            return cluster_path
        if os.path.exists(os.path.join(
                cluster_path, constants.DCOS_CLUSTER_ATTACHED_FILE)):
            attached = cluster_path

    if attached is not None:
        return attached

    # if only one cluster, set as attached
    if len(clusters) == 1:
        cluster_path = os.path.join(path, clusters[0])
        util.ensure_file_exists(os.path.join(
            cluster_path, constants.DCOS_CLUSTER_ATTACHED_FILE))
        return cluster_path

    return None


def get_clusters_path():
    """
    :returns: path to the directory of cluster configs
    :rtype: str
    """

    return os.path.join(get_config_dir_path(), constants.DCOS_CLUSTERS_SUBDIR)


def get_config_path():
    """Returns the path to the DCOS config file of the attached cluster.
    If still using "global" config return that toml instead

    :returns: path to the DCOS config file
    :rtype: str
    """

    if uses_deprecated_config():
        return get_global_config_path()
    else:
        cluster_path = get_attached_cluster_path()
        if not cluster_path:
            raise DCOSException("No cluster is currently attached.")
        return os.path.join(cluster_path, "dcos.toml")


def get_config_dir_path():
    """
    :returns: path to the DC/OS data directory
{'fraction_non_alphanumeric': 0.08107293550331525, 'fraction_numerical': 0.0006027727546714888, 'mean_word_length': 2.97663271420012, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5425141', 'n_tokens_mistral': 2033, 'n_tokens_neox': 1965, 'n_words': 582}