# Path: experiments/0.8/crosscompute/constants.py
# VIEW_NAMES = [
#     'text',
#     'number',
#     'markdown',
#     'table',
#     'image',
#     'map',
#     'electricity-network',
#     'file',
# ]
# DEFAULT_VIEW_NAME = 'text'
# PRINT_FORMAT_NAMES = [
#     'pdf'
# ]
# DEBUG_VARIABLE_DEFINITIONS = [{
#     'id': 'stdout',
#     'name': 'Standard Output',
#     'view': 'text',
#     'path': 'stdout.txt',
# }, {
#     'id': 'stderr',
#     'name': 'Standard Error',
#     'view': 'text',
#     'path': 'stderr.txt',
# }]
# 
# Path: experiments/0.8/crosscompute/exceptions.py
# class CrossComputeConnectionError(HTTPInternalServerError, CrossComputeError):
#     pass
# 
# class CrossComputeExecutionError(HTTPBadRequest, CrossComputeError):
#     pass
# 
# class CrossComputeImplementationError(
#         HTTPInternalServerError, CrossComputeError):
#     pass
# 
# class CrossComputeKeyboardInterrupt(KeyboardInterrupt, CrossComputeError):
#     pass
# 
# Path: experiments/0.8/crosscompute/macros.py
# def sanitize_name(name):
# def is_valid_name_character(x):
# def sanitize_json_value(value):
# def parse_number_safely(raw_value):
# def parse_number(raw_value):
# def split_path(path):
# def is_compatible_version(target_version_name, source_version_name):
# def get_plain_value(x):


import json
import requests
from sseclient import SSEClient
from ..constants import (
    BASH_CONFIGURATION_TEXT,
    CLIENT_URL,
    SERVER_URL)
from ..exceptions import (
    CrossComputeConnectionError,
    CrossComputeExecutionError,
    CrossComputeImplementationError,
    CrossComputeKeyboardInterrupt)
from ..macros import (
    get_environment_value)
            'url': 'could not connect to server ' + url})
    status_code = response.status_code
    d = {'url': url, 'token': token, 'statusCode': status_code}
    if status_code in [401, 403]:
        d['statusHelp'] = 'please check your server url and token'
        raise CrossComputeConnectionError(d)
    try:
        response_json = response.json()
    except ValueError:
        d['statusHelp'] = 'could not parse response as json'
        d['responseContent'] = response.content.decode('utf-8')
        raise CrossComputeConnectionError(d)
    if status_code != 200:
        if response_json:
            d.update(response_json)
        raise (
            CrossComputeExecutionError if status_code == 400 else
            CrossComputeImplementationError)(d)
    return response_json


def get_client_url():
    return get_environment_value('CROSSCOMPUTE_CLIENT', CLIENT_URL)


def get_server_url():
    return get_environment_value('CROSSCOMPUTE_SERVER', SERVER_URL)


def get_echoes_url():

# Path: experiments/0.8/crosscompute/constants.py
# VIEW_NAMES = [
#     'text',
#     'number',
#     'markdown',
#     'table',
#     'image',
#     'map',
#     'electricity-network',
#     'file',
# ]
# DEFAULT_VIEW_NAME = 'text'
# PRINT_FORMAT_NAMES = [
#     'pdf'
# ]
# DEBUG_VARIABLE_DEFINITIONS = [{
#     'id': 'stdout',
#     'name': 'Standard Output',
#     'view': 'text',
#     'path': 'stdout.txt',
# }, {
#     'id': 'stderr',
#     'name': 'Standard Error',
#     'view': 'text',
#     'path': 'stderr.txt',
# }]
# 
# Path: experiments/0.8/crosscompute/exceptions.py
# class CrossComputeConnectionError(HTTPInternalServerError, CrossComputeError):
#     pass
# 
# class CrossComputeExecutionError(HTTPBadRequest, CrossComputeError):
#     pass
# 
# class CrossComputeImplementationError(
#         HTTPInternalServerError, CrossComputeError):
#     pass
# 
# class CrossComputeKeyboardInterrupt(KeyboardInterrupt, CrossComputeError):
#     pass
# 
# Path: experiments/0.8/crosscompute/macros.py
# def sanitize_name(name):
# def is_valid_name_character(x):
# def sanitize_json_value(value):
# def parse_number_safely(raw_value):
# def parse_number(raw_value):
# def split_path(path):
# def is_compatible_version(target_version_name, source_version_name):
# def get_plain_value(x):


# Path: experiments/0.8/crosscompute/routines/connection.py
import json
import requests
from sseclient import SSEClient
from ..constants import (
    BASH_CONFIGURATION_TEXT,
    CLIENT_URL,
    SERVER_URL)
from ..exceptions import (
    CrossComputeConnectionError,
    CrossComputeExecutionError,
    CrossComputeImplementationError,
    CrossComputeKeyboardInterrupt)
from ..macros import (
    get_environment_value)

            'url': 'could not connect to server ' + url})
    status_code = response.status_code
    d = {'url': url, 'token': token, 'statusCode': status_code}
    if status_code in [401, 403]:
        d['statusHelp'] = 'please check your server url and token'
        raise CrossComputeConnectionError(d)
    try:
        response_json = response.json()
    except ValueError:
        d['statusHelp'] = 'could not parse response as json'
        d['responseContent'] = response.content.decode('utf-8')
        raise CrossComputeConnectionError(d)
    if status_code != 200:
        if response_json:
            d.update(response_json)
        raise (
            CrossComputeExecutionError if status_code == 400 else
            CrossComputeImplementationError)(d)
    return response_json


def get_client_url():
    return get_environment_value('CROSSCOMPUTE_CLIENT', CLIENT_URL)


def get_server_url():
    return get_environment_value('CROSSCOMPUTE_SERVER', SERVER_URL)


def get_echoes_url():

    server_url = get_environment_value('CROSSCOMPUTE_ECHOES', get_server_url())