<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

#
# This file is subject to the terms and conditions defined in
# file 'LICENSE.md', which is part of this source code package.
#

import re
from os.path import expanduser, isfile
import yaml
from yaml import YAMLError

DEFAULT_KUBECONFIG = "{0}/.kube/config".format(expanduser("~"))
DEFAULT_API_HOST = "localhost:8888"
DEFAULT_API_VERSION = "v1"
DEFAULT_NAMESPACE = "default"

VALID_API_VERSIONS = ["v1"]

VALID_IP_RE = re.compile(r'^(http[s]?\:\/\/)?((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3})(:[0-9]+)?$')
VALID_HOST_RE = re.compile(r'^(http[s]?\:\/\/)?([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9]\.)*([A-Za-z]|[A-Za-z][A-Za-z\-]*[A-Za-z])(:[0-9]+)?$')


class K8sConfig:
    def __init__(self, kubeconfig=DEFAULT_KUBECONFIG, api_host=DEFAULT_API_HOST, auth=None, cert=None,
                 namespace=DEFAULT_NAMESPACE, pull_secret=None, token=None, version=DEFAULT_API_VERSION):
        """
        Pulls configuration from a kubeconfig file, if present, otherwise accepts user-defined parameters.s
        See http://kubernetes.io/docs/user-guide/kubeconfig-file/ for information on the kubeconfig file.

        :param kubeconfig: Absolute path to the kubeconfig file, if any.
        :param api_host: Absolute URI where the API server resides.
        :param auth: A tuple of (username, password) for basic authentication.
        :param namespace: The namespace to use. Defaults to 'default'
        :param pull_secret: The password to use when pulling images from the container repository.
        :param token: An authentication token. Mutually exclusive with 'auth'.
        :param version: The version of the API to target. Defaults to 'v1'.
        """

        dotconf = None
        if kubeconfig is not None:
            if not isfile(kubeconfig):
                raise SyntaxError('K8sConfig: kubeconfig: [ {0} ] doesn\'t exist.'.format(kubeconfig))
            try:
                with open(kubeconfig, 'r') as stream:
                    dotconf = yaml.load(stream)
            except YAMLError:
                raise SyntaxError('K8sConfig: kubeconfig: [ {0} ] is not a valid json file.'.format(kubeconfig))

        if dotconf is not None:
            # we're pulling configuration from a kubeconfig file

            self.api_host = None
            self.ca_cert = None
            self.auth = None
            self.token = None
            self.client_certificate = None
            self.client_key = None
            self.cert = None

            self.clusters = dotconf['clusters']
            self.contexts = dotconf['contexts']
            self.current_context = dotconf['current-context']
            self.preferences = dotconf['preferences']
            self.pull_secret = pull_secret
            self.users = dotconf['users']
            self.version = dotconf['apiVersion']

            for cluster in self.clusters:<commit_msg>K8sConfig: update error handling when reading from kubeconfig<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

#
# This file is subject to the terms and conditions defined in
# file 'LICENSE.md', which is part of this source code package.
#

import re
from os.path import expanduser, isfile
import yaml
from yaml import YAMLError

DEFAULT_KUBECONFIG = "{0}/.kube/config".format(expanduser("~"))
DEFAULT_API_HOST = "localhost:8888"
DEFAULT_API_VERSION = "v1"
DEFAULT_NAMESPACE = "default"

VALID_API_VERSIONS = ["v1"]

VALID_IP_RE = re.compile(r'^(http[s]?\:\/\/)?((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3})(:[0-9]+)?$')
VALID_HOST_RE = re.compile(r'^(http[s]?\:\/\/)?([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9]\.)*([A-Za-z]|[A-Za-z][A-Za-z\-]*[A-Za-z])(:[0-9]+)?$')


class K8sConfig:
    def __init__(self, kubeconfig=DEFAULT_KUBECONFIG, api_host=DEFAULT_API_HOST, auth=None, cert=None,
                 namespace=DEFAULT_NAMESPACE, pull_secret=None, token=None, version=DEFAULT_API_VERSION):
        """
        Pulls configuration from a kubeconfig file, if present, otherwise accepts user-defined parameters.s
        See http://kubernetes.io/docs/user-guide/kubeconfig-file/ for information on the kubeconfig file.

        :param kubeconfig: Absolute path to the kubeconfig file, if any.
        :param api_host: Absolute URI where the API server resides.
        :param auth: A tuple of (username, password) for basic authentication.
        :param namespace: The namespace to use. Defaults to 'default'
        :param pull_secret: The password to use when pulling images from the container repository.
        :param token: An authentication token. Mutually exclusive with 'auth'.
        :param version: The version of the API to target. Defaults to 'v1'.
        """

        dotconf = None
        if kubeconfig is not None:
            if not isfile(kubeconfig):
                raise IOError('K8sConfig: kubeconfig: [ {0} ] doesn\'t exist.'.format(kubeconfig))
            try:
                with open(kubeconfig, 'r') as stream:
                    dotconf = yaml.load(stream)
            except YAMLError as err:
                raise SyntaxError('K8sConfig: kubeconfig: [ {0} ] is not a valid YAML file.'.format(kubeconfig))

        if dotconf is not None:
            # we're pulling configuration from a kubeconfig file

            self.api_host = None
            self.ca_cert = None
            self.auth = None
            self.token = None
            self.client_certificate = None
            self.client_key = None
            self.cert = None

            self.clusters = dotconf['clusters']
            self.contexts = dotconf['contexts']
            self.current_context = dotconf['current-context']
            self.preferences = dotconf['preferences']
            self.pull_secret = pull_secret
            self.users = dotconf['users']
            self.version = dotconf['apiVersion']

            for cluster in self.clusters:
{'fraction_non_alphanumeric': 0.13336671674177933, 'fraction_numerical': 0.019195459856451345, 'mean_word_length': 2.6139927623642945, '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': '2096229', 'n_tokens_mistral': 1969, 'n_tokens_neox': 1897, 'n_words': 480}