<commit_before>"""
Functions related to the pScheduler REST and Plugin APIs
"""

import multiprocessing
import multiprocessing.dummy
import socket
import threading
import urlparse
import uuid

# HACK: BWCTLBC
import os


from .psdns import *
from .psurl import *


def api_root():
    "Return the standard root location of the pScheduler hierarchy"
    return '/pscheduler'

def api_this_host():
    "Return a fully-qualified name for this host"
    return socket.getfqdn()


def __host_per_rfc_2732(host):
    "Format a host name or IP for a URL according to RFC 2732"

    try:
        socket.inet_pton(socket.AF_INET6, host)
        return "[%s]" % (host)
    except socket.error:
        return host  # Not an IPv6 address


def api_replace_host(url_text, replacement):
    "Replace the host portion of a URL"

    url = list(urlparse.urlparse(url_text))
    if replacement is not None:
        url[1] = __host_per_rfc_2732(replacement)
    return urlparse.urlunparse(url)




def api_url(host = None,
            path = None,
            port = None,
            protocol = None
            ):
    """Format a URL for use with the pScheduler API."""

    host = 'localhost' if host is None else str(host)
    # Force the host into something valid for DNS
    # See http://stackoverflow.com/a/25103444/180674
    try:
        host = host.encode('idna')
    except UnicodeError:
        raise ValueError("Invalid host '%s'" % (host))
    host = __host_per_rfc_2732(host)

    if path is not None and path.startswith('/'):
        path = path[1:]

    if protocol is None:
        protocol = 'https'

    return protocol + '://' \
        + host \
        + ('' if port is None else (':' + str(port))) \
        + api_root() + '/'\
        + ('' if path is None else str(path))



def api_host_port(hostport):
    """Return the host and port parts of a host/port pair"""
    if hostport is None:
        return (None, None)
    parsed=urlparse.urlparse("bogus://%s" % (__host_per_rfc_2732(hostport)))
    return (None if parsed.hostname == "none" else parsed.hostname,
            parsed.port)


def api_url_hostport(hostport=None,
            path=None,
            protocol=None
            ):
    """Format a URL for use with the pScheduler API where the host name
    may include a port."""
    (host, port) = api_host_port(hostport)
    return api_url(host=host, port=port, path=path, protocol=protocol)

<commit_msg>Fix for bug in CentOS 6 version of urlparse when handling bracketed IPv6. Not a problem on CentOS 7. This is part of fix for perfsonar-mesh-config#95<commit_after>"""
Functions related to the pScheduler REST and Plugin APIs
"""

import multiprocessing
import multiprocessing.dummy
import socket
import threading
import urlparse
import uuid

# HACK: BWCTLBC
import os


from .psdns import *
from .psurl import *


def api_root():
    "Return the standard root location of the pScheduler hierarchy"
    return '/pscheduler'

def api_this_host():
    "Return a fully-qualified name for this host"
    return socket.getfqdn()


def __host_per_rfc_2732(host):
    "Format a host name or IP for a URL according to RFC 2732"

    try:
        socket.inet_pton(socket.AF_INET6, host)
        return "[%s]" % (host)
    except socket.error:
        return host  # Not an IPv6 address


def api_replace_host(url_text, replacement):
    "Replace the host portion of a URL"

    url = list(urlparse.urlparse(url_text))
    if replacement is not None:
        url[1] = __host_per_rfc_2732(replacement)
    return urlparse.urlunparse(url)




def api_url(host = None,
            path = None,
            port = None,
            protocol = None
            ):
    """Format a URL for use with the pScheduler API."""

    host = 'localhost' if host is None else str(host)
    # Force the host into something valid for DNS
    # See http://stackoverflow.com/a/25103444/180674
    try:
        host = host.encode('idna')
    except UnicodeError:
        raise ValueError("Invalid host '%s'" % (host))
    host = __host_per_rfc_2732(host)

    if path is not None and path.startswith('/'):
        path = path[1:]

    if protocol is None:
        protocol = 'https'

    return protocol + '://' \
        + host \
        + ('' if port is None else (':' + str(port))) \
        + api_root() + '/'\
        + ('' if path is None else str(path))



def api_host_port(hostport):
    """Return the host and port parts of a host/port pair"""
    if hostport is None:
        return (None, None)
    formatted_host = __host_per_rfc_2732(hostport)
    try:
        parsed=urlparse.urlparse("bogus://%s" % (formatted_host))
        if parsed.port is None: pass #simple test to trigger an error from urlparse on CentOS 6
    except ValueError as ve:
        #TODO: can remove this once we drop CentOS 6
        #python 2.6 urlparse does not properly handle bracketed IPv6, so we do that here
        if "]:" in formatted_host:
            formatted_host = formatted_host.replace("[", "")
            parts = formatted_host.split(']:')
            if len(parts) != 2:
                raise ve
            return parts
        elif formatted_host.endswith(']'):
            return formatted_host.replace('[',"").replace(']',""), None
        else:
            raise ve
        
    return (None if parsed.hostname == "none" else parsed.hostname,
            parsed.port)


def api_url_hostport(hostport=None,
            path=None,
            protocol=None
            ):
    """Format a URL for use with the pScheduler API where the host name
    may include a port."""
    (host, port) = api_host_port(hostport)
    return api_url(host=host, port=port, path=path, protocol=protocol)


{'fraction_non_alphanumeric': 0.10045742434904996, 'fraction_numerical': 0.015306122448979591, 'mean_word_length': 2.557571964956195, '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': '7383193', 'n_tokens_mistral': 1823, 'n_tokens_neox': 1723, 'n_words': 579}