<commit_before># -*- coding: utf-8 -*-
import os
import sys
import time
import json
import itertools
import threading
import traceback

import six
import yaml
import requests

from scalrctl import click, defaults, settings, commands

__author__ = '<NAME>, <NAME>'


class _spinner(object):

    @staticmethod
    def draw(event):
        cursor = itertools.cycle('|/-\\')
        while not event.isSet():
            sys.stdout.write(next(cursor))
            sys.stdout.flush()
            time.sleep(0.1)
            sys.stdout.write('\b')
        sys.stdout.write(' ')
        sys.stdout.flush()

    def __init__(self):
        self.event = threading.Event()
        self.thread = threading.Thread(target=_spinner.draw,
                                       args=(self.event,))
        self.thread.daemon = True

    def __enter__(self):
        self.thread.start()

    def __exit__(self, type, value, traceback):
        self.event.set()
        self.thread.join()


def _get_spec_path(api_level, extension):
    return os.path.join(defaults.CONFIG_DIRECTORY,
                        '{}.{}'.format(api_level, extension))


def _is_spec_exists(api_level, extension):
    return os.path.exists(_get_spec_path(api_level, extension))


def _load_yaml_spec(api_level):
    spec_url = "{0}://{1}/api/{2}.{3}.yml".format(settings.API_SCHEME,
                                                  settings.API_HOST,
                                                  api_level,
                                                  settings.API_VERSION)
    try:
        resp = requests.get(spec_url, verify=settings.SSL_VERIFY_PEER)
    except requests.exceptions.SSLError as e:
        import ssl
        if 'CertificateError' in str(e):
            sni_supported = None
            try:
                from OpenSSL._util import lib as _lib
                if _lib.Cryptography_HAS_TLSEXT_HOSTNAME:
                    sni_supported = True
            except ImportError:
                sni_supported = False
            if not sni_supported:
                errmsg = "\nError: Your Python version %s does not support SNI. " \
                         "This can be resolved by upgrading Python to version 2.7.9 or " \
                         "by installing pyOpenSSL>=17.3.0. Link for further reading: " \
                         "http://docs.python-requests.org/en/master/community/faq/#what-are-hostname-doesn-t-match-errors" \
                         " \nIf you are having problems installing pyOpenSSL try to upgrade pip first." % sys.version
                click.echo(errmsg)
                sys.exit()
    return resp.text if resp.status_code == 200 else None


def _read_spec(spec_path):
    text = None
    if os.path.exists(spec_path):
        with open(spec_path, "r") as fp:
            text = fp.read()
    return text


def _write_spec(spec_path, text):
    with open(spec_path, "w") as fp:
        fp.write(text)


def _update_spec(api_level):
    """
    Downloads yaml spec and converts it to JSON
    Both files are stored in configuration directory.
    """

    try:<commit_msg>ST-247<commit_after># -*- coding: utf-8 -*-
import os
import sys
import time
import json
import itertools
import threading
import traceback

import six
import yaml
import requests

from scalrctl import click, defaults, settings, commands

__author__ = '<NAME>, <NAME>'


class _spinner(object):

    @staticmethod
    def draw(event):
        cursor = itertools.cycle('|/-\\')
        while not event.isSet():
            sys.stdout.write(next(cursor))
            sys.stdout.flush()
            time.sleep(0.1)
            sys.stdout.write('\b')
        sys.stdout.write(' ')
        sys.stdout.flush()

    def __init__(self):
        self.event = threading.Event()
        self.thread = threading.Thread(target=_spinner.draw,
                                       args=(self.event,))
        self.thread.daemon = True

    def __enter__(self):
        self.thread.start()

    def __exit__(self, type, value, traceback):
        self.event.set()
        self.thread.join()


def _get_spec_path(api_level, extension):
    return os.path.join(defaults.CONFIG_DIRECTORY,
                        '{}.{}'.format(api_level, extension))


def _is_spec_exists(api_level, extension):
    return os.path.exists(_get_spec_path(api_level, extension))


def _load_yaml_spec(api_level):
    spec_url = "{0}://{1}/api/{2}.{3}.yml".format(settings.API_SCHEME,
                                                  settings.API_HOST,
                                                  api_level,
                                                  settings.API_VERSION)
    try:
        resp = requests.get(spec_url, verify=settings.SSL_VERIFY_PEER)
    except requests.exceptions.SSLError as e:
        import ssl
        if 'CertificateError' in str(e):
            sni_supported = None
            try:
                from OpenSSL._util import lib as _lib
                if _lib.Cryptography_HAS_TLSEXT_HOSTNAME:
                    sni_supported = True
            except ImportError:
                sni_supported = False
            if not sni_supported:
                errmsg = "\nError: Your Python version %s does not support SNI. " \
                         "This can be resolved by upgrading Python to version 2.7.9 or " \
                         "by installing pyOpenSSL>=17.3.0. More info on Requests website: " \
                         "http://docs.python-requests.org/en/master/community/faq/#what-are-hostname-doesn-t-match-errors" \
                         " \nIf you are having problems installing pyOpenSSL try to upgrade pip first." % sys.version[:5]
                click.echo(errmsg)
                sys.exit()
    return resp.text if resp.status_code == 200 else None


def _read_spec(spec_path):
    text = None
    if os.path.exists(spec_path):
        with open(spec_path, "r") as fp:
            text = fp.read()
    return text


def _write_spec(spec_path, text):
    with open(spec_path, "w") as fp:
        fp.write(text)


def _update_spec(api_level):
    """
    Downloads yaml spec and converts it to JSON
    Both files are stored in configuration directory.
    """

    try:
{'fraction_non_alphanumeric': 0.10074265418146594, 'fraction_numerical': 0.006134969325153374, 'mean_word_length': 1.8095238095238095, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '393102', 'n_tokens_mistral': 1802, 'n_tokens_neox': 1727, 'n_words': 430}