<commit_before># -*-coding:Utf-8 -*-

"""Contains functions which make requests to the server"""

# Compatibility 2.7-3.4
from __future__ import absolute_import
from __future__ import unicode_literals
from builtins import input

import sys, locale
import requests, re

from . import user as user
from . import target as target
from . import usergroup as usergroup
from . import targetgroup as targetgroup
import config

def ask_confirmation(prompt_confirmation):
    """Same as input() but check if user key in a correct input,
    return True if the user confirms, false otherwise.
    """
    # Hack for the sake of compatibility between 2.7 and 3.4
    sys.stdout.write(prompt_confirmation)
    confirmation = str.upper(input(""))

    # Loop until user types [y/N]
    while confirmation != "Y" and confirmation != "N" and confirmation:
        print("You didn't type 'Y' or 'N', please try again.")
        sys.stdout.write(prompt_confirmation)
        confirmation = str.upper(input(""))

    if confirmation == "Y":
        return True

    return False


def get(url):
    """Send the GET request to the server and print a result"""
    try:
        r = requests.get(url, verify=config.certificate_path)
    except requests.RequestException as e:
        print("ERROR: " + str(e.message))
    else:
        print(r.text)

        if r.status_code == requests.codes.ok:
            return 0

    return 1


def list(obj):
    """Get the list of all objects of this type"""
    return get(config.url_passhport + obj + "/list")


def search(obj, param):
    """Get the list of objects matching the pattern"""
    if isinstance(param["<pattern>"], bytes):
        param["<pattern>"] = param["<pattern>"].decode("utf8")

    return get(config.url_passhport + obj + "/search/" + param["<pattern>"])


def checkaccess(obj, param):
    """Check access to the target with a name or hostname with this pattern"""
    if isinstance(param["<pattern>"], bytes):
        param["<pattern>"] = param["<pattern>"].decode("utf8")

    return get(config.url_passhport + obj + "/checkaccess/" + param["<pattern>"])


def show(obj, param):
    """Get data of the given object"""
    if isinstance(param["<name>"], bytes):
        param["<name>"] = param["<name>"].decode("utf8")

    return get(config.url_passhport + obj + "/show/" + param["<name>"])


def delete(obj, param):
    """Deletion on passhportd via API"""
    if isinstance(param["<name>"], bytes):
        param["<name>"] = param["<name>"].decode("utf8")

    if show(obj, {"<name>": param["<name>"]}) == 0:
        if "-f" in param or "--force" in param:
            return get(config.url_passhport + obj + "/delete/" + \
                       param["<name>"])
        else:
            confirmed = ask_confirmation(
                "Are you sure you want to delete " + \
                param["<name>"] + "? [y/N] ")

            if confirmed:
                return get(config.url_passhport + obj + "/delete/" + \
                    param["<name>"])
            else:
                print("Operation aborted.")

    return None


def post(url, data):
    """Send the POST request to the server and print a result"""
    try:
        r = requests.post(url, data = data, verify=config.certificate_path)
    except requests.RequestException as e:
        print("ERROR: " + str(e.message))
    else:
        print(r.text)

        if r.status_code == requests.codes.ok:
            # Copy sshkey on new targets
            AUTO_DEPLOY_SSHKEY = False
            if re.findall("\/target\/create", url) and AUTO_DEPLOY_SSHKEY:
                print("Trying to deploy sshkey")
            return 0
        #TODO first connection to a target to deploy a key
        # if url  content target
        # ask_user_if_first_connection
        # connect / prompt pass
        # done => print result.

    return 1
<commit_msg>Issue #218<commit_after># -*-coding:Utf-8 -*-

"""Contains functions which make requests to the server"""

# Compatibility 2.7-3.4
from __future__ import absolute_import
from __future__ import unicode_literals
from builtins import input

import sys, locale
import requests, re

from . import user as user
from . import target as target
from . import usergroup as usergroup
from . import targetgroup as targetgroup
import config

def ask_confirmation(prompt_confirmation):
    """Same as input() but check if user key in a correct input,
    return True if the user confirms, false otherwise.
    """
    # Hack for the sake of compatibility between 2.7 and 3.4
    sys.stdout.write(prompt_confirmation)
    confirmation = str.upper(input(""))

    # Loop until user types [y/N]
    while confirmation != "Y" and confirmation != "N" and confirmation:
        print("You didn't type 'Y' or 'N', please try again.")
        sys.stdout.write(prompt_confirmation)
        confirmation = str.upper(input(""))

    if confirmation == "Y":
        return True

    return False


def get(url):
    """Send the GET request to the server and print a result"""
    try:
        r = requests.get(url, verify=config.certificate_path, timeout=3)
    except requests.exceptions.Timeout:
        print("ERROR: Connection timed out. Check your configuration.")
    except requests.exceptions.ConnectionError as e:
        print("ERROR: Connection error. Check your configuration.\n" + str(e))
    except requests.exceptions.TooManyRedirects:
        print("ERROR: Too many redirects.")
    except requests.exceptions.RequestException as e:
        print("Error: " + str(e))
    else:
        print(r.text)

        if r.status_code == requests.codes.ok:
            return 0

    return 1


def list(obj):
    """Get the list of all objects of this type"""
    return get(config.url_passhport + obj + "/list")


def search(obj, param):
    """Get the list of objects matching the pattern"""
    if isinstance(param["<pattern>"], bytes):
        param["<pattern>"] = param["<pattern>"].decode("utf8")

    return get(config.url_passhport + obj + "/search/" + param["<pattern>"])


def checkaccess(obj, param):
    """Check access to the target with a name or hostname with this pattern"""
    if isinstance(param["<pattern>"], bytes):
        param["<pattern>"] = param["<pattern>"].decode("utf8")

    return get(config.url_passhport + obj + "/checkaccess/" + param["<pattern>"])


def show(obj, param):
    """Get data of the given object"""
    if isinstance(param["<name>"], bytes):
        param["<name>"] = param["<name>"].decode("utf8")

    return get(config.url_passhport + obj + "/show/" + param["<name>"])


def delete(obj, param):
    """Deletion on passhportd via API"""
    if isinstance(param["<name>"], bytes):
        param["<name>"] = param["<name>"].decode("utf8")

    if show(obj, {"<name>": param["<name>"]}) == 0:
        if "-f" in param or "--force" in param:
            return get(config.url_passhport + obj + "/delete/" + \
                       param["<name>"])
        else:
            confirmed = ask_confirmation(
                "Are you sure you want to delete " + \
                param["<name>"] + "? [y/N] ")

            if confirmed:
                return get(config.url_passhport + obj + "/delete/" + \
                    param["<name>"])
            else:
                print("Operation aborted.")

    return None


def post(url, data):
    """Send the POST request to the server and print a result"""
    try:
        r = requests.post(url, data = data, verify=config.certificate_path)
    except requests.exceptions.Timeout:
        print("ERROR: Connection timed out. Check your configuration.")
    except requests.exceptions.ConnectionError as e:
        print("ERROR: Connection error. Check your configuration.\n" + str(e))
    except requests.exceptions.TooManyRedirects:
        print("ERROR: Too many redirects.")
    except requests.exceptions.RequestException as e:
        print("Error: " + str(e))
    else:
        print(r.text)

        if r.status_code == requests.codes.ok:
            # Copy sshkey on new targets
            AUTO_DEPLOY_SSHKEY = False
            if re.findall("\/target\/create", url) and AUTO_DEPLOY_SSHKEY:
                print("Trying to deploy sshkey")
            return 0
        #TODO first connection to a target to deploy a key
        # if url  content target
        # ask_user_if_first_connection
        # connect / prompt pass
        # done => print result.

    return 1

{'fraction_non_alphanumeric': 0.12900545929266555, 'fraction_numerical': 0.004747211013529551, 'mean_word_length': 2.6307626023265835, 'pattern_counts': {'":': 4, '<': 43, '<?xml version=': 0, '>': 45, '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': '4814509', 'n_tokens_mistral': 2490, 'n_tokens_neox': 2386, 'n_words': 772}