#!/usr/bin/env python3

from operator import add as op_add
import copy
import argparse
import time
import psutil
import memory_profiler
import pygame
import sys

parser = argparse.ArgumentParser()
parser.add_argument('-gui', default=False, required=False, action='store_true')
parser.add_argument('-diff', default=2)

def get_enemy(player):
    if player == 'a':
        return 'n'
    return 'a'

def draw_board(display, board):
    width_grid = height_grid = 100
    rects = []

    for i in range(8):
        for j in range(8):
            rect = pygame.Rect(i * (width_grid+1), j *
                               (height_grid+1), width_grid, height_grid)
            rects.append(rect)

            pygame.draw.rect(display, (255, 255, 255), rect)

            val = board.get_val(i, j)
            if val == 'a':
                pygame.draw.circle(display, (0, 0, 0),
                                   rect.center, width_grid // 2 - 2, 4)
            elif val == 'n':
                pygame.draw.circle(display, (0, 0, 0),
                                   rect.center, width_grid // 2 - 2)

    pygame.display.flip()
    return rects

class Game:
    MIN_PLAYER = None
    MAX_PLAYER = None

    def __init__(self):
        self.board = self.new_board()
        self.set_val(3, 3, 'a')
        self.set_val(3, 4, 'n')
        self.set_val(4, 3, 'n')
        self.set_val(4, 4, 'a')

    def __str__(self):
        string = '   a b c d e f g h\n'
        string += '  ' + '-' * 16 + '\n'
        for i in range(8):
            string += str(i) + ' |'
            for j in range(8):
                string += self.get_val(i, j) + ' '
            string += '\n'

        return string

    def new_board(self):
        return ['#'] * 64

    def get_val(self, i, j):
        if 0 <= i <= 7 and 0 <= j <= 7:
            return self.board[i*8 + j]
        return None

    def set_val(self, i, j, val):
        if 0 <= i <= 7 and 0 <= j <= 7:
            self.board[i*8 + j] = val
        else:
            return None

    def get_distance(self, pos_1, pos_2):
        # Line
        if pos_1[0] == pos_2[0]:
            return abs(pos_2[1] - pos_1[1])

        # Column
        if pos_1[1] == pos_2[1]:
            return abs(pos_2[0] - pos_1[0])

        # Diagonal; Does not matter what subtraction we do
        return abs(pos_2[0] - pos_1[0])

    def get_moves(self, player):
        '''
        Returns a list of all the valid moves
        '''
        enemy_val = get_enemy(player)

        # A valid move must be adjacent to an enemy piece

        # Get all enemies
        enemies = []
        for i in range(8):
            for j in range(8):
                if self.get_val(i, j) == enemy_val:
                    enemies.append((i, j))

        # For all enemies get empty adjacent positions
        possible_moves = []
        for enemy in enemies:
            i = enemy[0]
            j = enemy[1]
            # Top Left
            if self.get_val(i-1, j-1) == '#' and (i-1, j-1) not in possible_moves:
                possible_moves.append((i-1, j-1))
            # Top
            if self.get_val(i-1, j) == '#' and (i-1, j) not in possible_moves:
                possible_moves.append((i-1, j))
            # Top Right
            if self.get_val(i-1, j+1) == '#' and (i-1, j+1) not in possible_moves:
                possible_moves.append((i-1, j+1))
            # Left
            if self.get_val(i, j-1) == '#' and (i, j-1) not in possible_moves:
                possible_moves.append((i, j-1))
            # Right
            if self.get_val(i, j+1) == '#' and (i, j+1) not in possible_moves:
                possible_moves.append((i, j+1))
            # Bot Left
            if self.get_val(i+1, j-1) == '#' and (i+1, j-1) not in possible_moves:
                possible_moves.append((i+1, j-1))
            # Bot
            if self.get_val(i+1, j) == '#' and (i+1, j) not in possible_moves:
                possible_moves.append((i+1, j))
            # Bot Right
            if self.get_val(i+1, j+1) == '#' and (i+1, j+1) not in possible_moves:
                possible_moves.append((i+1, j+1))

        # Check if moves are valid
        valid_moves = []
        for move in possible_moves:
            valid, junk = self.valid_move(move, player)
            if valid and move not in valid_moves:
                valid_moves.append(move)

        return valid_moves

    def valid_move(self, piece, player):
        '''
        Returns True if a piece can be placed on the given position.
        Also if i can be placed, returns the position of the furthest valid ally (to form a line)
        '''
        # Move on all 8 axis
        # If you find an empty space or reach the end of the board, then abandon that axis
        # I think this should look like bf
        # Maybe this function should return the farthest valid allied piece, if there is any
        # Tuple: (pos, direction, enemies)
        enemy = get_enemy(player)
        max_piece = None
        max_enemies = 0
        valid = False
        queue = []
        # Top Left
        queue.append((piece, (-1, -1), 0))
        # Top
        queue.append((piece, (-1, 0), 0))
        # Top Right
        queue.append((piece, (-1, 1), 0))
        # Left
        queue.append((piece, (0, -1), 0))
        # Right
        queue.append((piece, (0, 1), 0))
        # Bot Left
        queue.append((piece, (1, -1), 0))
        # Bot
        queue.append((piece, (1, 0), 0))
        # Bot Right
        queue.append((piece, (1, 1), 0))

        while len(queue) > 0:
            curr = queue.pop(0)
            curr_pos = curr[0]
            curr_enemies = curr[2]
            # If we are on the starting piece, then increment with direction and continue
            if curr_pos == piece:
                curr_pos = tuple(map(op_add, curr[0], curr[1]))

            curr_pos_val = self.get_val(curr_pos[0], curr_pos[1])

            # Check if out of bounds or empty space
            if curr_pos_val == None or curr_pos_val == '#':
                continue

            # Check if curr_pos is a player piece, if it is make the move valid and check if that is the best distance
            elif curr_pos_val == player and curr_enemies > 0:
                valid = True
                if max_piece == None or curr_enemies > max_enemies:
                    max_piece = curr_pos
                    max_enemies = curr_enemies

            # Check if curr_pos is an enemy piece, if it is increment the position
            elif curr_pos_val == enemy:
                curr_enemies += 1
                new_pos = tuple(map(op_add, curr[0], curr[1]))
                queue.append((new_pos, curr[1], curr_enemies))

        return valid, max_piece

    def move(self, start, dest, player):
        '''
        Sets all the pieces in a line from start to dest to the value of the start
        '''
        #player = self.get_val(start[0], start[1])
        curr_pos = start

        step_i = 1
        if start[0] > dest[0]:
            step_i = -1
        elif start[0] == dest[0]:
            step_i = 0

        step_j = 1
        if start[1] > dest[1]:
            step_j = -1
        elif start[1] == dest[1]:
            step_j = 0

        while(curr_pos != dest):
            #curr_pos[0] += step_i
            #curr_pos[1] += step_j
            self.set_val(curr_pos[0], curr_pos[1], player)
            curr_pos = tuple(map(op_add, curr_pos, (step_i, step_j)))

        self.set_val(curr_pos[0], curr_pos[1], player)

    def final(self):
        '''
        If there are no more moves for both players, returns True. Else returns False
        '''
        return len(self.get_moves('a')) == 0 and len(self.get_moves('n')) == 0

    def count_pieces(self):
        '''
        Returns the number of white and black pieces
        '''
        num_a = 0
        num_n = 0
        for i in range(8):
            for j in range(8):
                val = self.get_val(i, j)
                if val == 'a':
                    num_a += 1
                elif val == 'n':
                    num_n += 1

        return num_a, num_n

    def get_winner(self):
        '''
        Returns the winner. If the game has not ended it returns None
        '''
        if not self.final():
            return None
        num_a, num_n = self.count_pieces()

        if num_a > num_n:
            return 'a'
        elif num_n > num_a:
            return 'n'
        else:
            return 'tie'

    def estimate_score(self, depth):
        final = self.final()
        score_max = 0
        score_min = 0
        if Game.MAX_PLAYER == 'a':
            score_max, score_min = self.count_pieces()
        else:
            score_min, score_max = self.count_pieces()

        # If game has ended
        if final:
            # If MAX wins
            if score_max > score_min:
                return 100 + depth
            # If MIN wins
            elif score_min > score_max:
                return -100 + depth
            # If TIE
            else:
                return 0
        else:
            return score_max - score_min

class State:
    def __init__(self, board, curr_player, depth, score=None):
        self.board = board
        self.curr_player = curr_player
        self.depth = depth
        self.score = score
        self.chosen_state = None

    def get_moves(self):
        enemy = get_enemy(self.curr_player)
        moves = self.board.get_moves(self.curr_player)

        move_states = []
        for move in moves:
            new_board = copy.deepcopy(self.board)
            junk, dest = new_board.valid_move(move, self.curr_player)
            new_board.move(move, dest, self.curr_player)
            move_states.append(State(new_board, enemy, self.depth-1))

        return move_states

def min_max(state):
    if state.depth == 0 or state.board.final() or len(state.get_moves()) == 0:
        state.score = state.board.estimate_score(state.depth)
        return state

    state.moves = state.get_moves()

    moves_score = [min_max(state) for state in state.moves]

    if state.curr_player == Game.MAX_PLAYER:
        state.chosen_state = max(moves_score, key=lambda x: x.score)
    else:
        state.chosen_state = min(moves_score, key=lambda x: x.score)

    state.score = state.chosen_state.score
    return state

def alpha_beta(alpha, beta, state):
    '''
    ceva debug stuff

    print(alpha, beta, state.depth)
    if alpha == 0 and beta == 2 and state.depth == 2:
        print('crash')
        print(state.curr_player)
        print(state.board)
        print('moves', len(state.get_moves()))
    '''

    if state.depth == 0 or state.board.final() or len(state.get_moves()) == 0:
        state.score = state.board.estimate_score(state.depth)
        return state

    if alpha > beta:
        return state

    state.moves = state.get_moves()

    if state.curr_player == Game.MAX_PLAYER:
        curr_score = float('-inf')

        for move in state.moves:
            new_state = alpha_beta(alpha, beta, move)
            if curr_score < new_state.score:
                state.chosen_state = new_state
                curr_score = new_state.score

            if alpha < new_state.score:
                alpha = new_state.score
                if alpha >= beta:
                    break

    elif state.curr_player == Game.MIN_PLAYER:
        curr_score = float('inf')

        for move in state.moves:
            new_state = alpha_beta(alpha, beta, move)

            if curr_score > new_state.score:
                state.chosen_state = new_state
                curr_score = new_state.score

            if beta > new_state.score:
                beta = new_state.score
                if alpha >= beta:
                    break

    state.score = state.chosen_state.score

    return state

def play_game(algo, player, depth, gui):
    enemy = get_enemy(player)

    Game.MIN_PLAYER = player
    Game.MAX_PLAYER = enemy
    board = Game()

    print('\nInitial:')
    print(str(board))

    state = State(board, 'n', depth)

    if gui:
        # Init pygame
        pygame.init()
        pygame.display.set_caption('Reversi')
        display = pygame.display.set_mode(size=(810, 810))

        rectangles = draw_board(display, board)
    while True:
        if state.curr_player == Game.MIN_PLAYER:
            # Player turn
            print('Player turn')
            valid_moves = state.board.get_moves(Game.MIN_PLAYER)
            print('valid moves:', len(valid_moves))
            for move in valid_moves:
                print(chr(move[1] + ord('a')), move[0])
            if len(valid_moves) == 0:
                print('No valid moves')
            else:
                while True:
                    if gui:
                        # Go through events
                        valid_move = False
                        for event in pygame.event.get():
                            if event.type == pygame.QUIT:
                                pygame.quit()
                                sys.exit()

                            elif event.type == pygame.MOUSEBUTTONDOWN:
                                pos = pygame.mouse.get_pos()

                                for rect in range(len(rectangles)):
                                    if rectangles[rect].collidepoint(pos):
                                        line = rect // 8
                                        col = rect % 8

                                        if (line, col) in valid_moves:
                                            valid_move = True
                                            break

                        if valid_move:
                            break

                    else:
                        # If not GUI
                        try:
                            player_input = input('\n> ')
                            col = player_input[0].lower()
                            line = int(player_input[1])

                            if 0 <= line <= 7 and 'a' <= col <= 'h':
                                # Transformam coloana in nr
                                col = ord(col) - ord('a')
                                if (line, col) in valid_moves:
                                    # Valid move
                                    break
                                else:
                                    # Invalid move
                                    print('Invalid move')
                            else:
                                print('Invalid input')

                        except Exception as e:
                            print('error %s\n' % e)

                junk, dest = state.board.valid_move(
                    (line, col), Game.MIN_PLAYER)
                print('move:', (line, col), dest)
                state.board.move((line, col), dest, Game.MIN_PLAYER)

        else:
            # CPU turn
            print('CPU turn')
            valid_moves = state.board.get_moves(Game.MAX_PLAYER)
            if len(valid_moves) == 0:
                print('No valid moves')
            else:
                t1 = time.time()

                new_state = None
                mem_usage = None
                if algo == 'minmax':
                    mem_usage, new_state = memory_profiler.memory_usage(
                        (min_max, [state]), retval=True, max_usage=True)
                else:
                    mem_usage, new_state = memory_profiler.memory_usage(
                        (alpha_beta, [-500, 500, state]), retval=True, max_usage=True)

                state.board = new_state.chosen_state.board

                t2 = time.time()
                dt = t2-t1
                print('Time to move:', round(dt * 1000), 'ms')

                if mem_usage != None:
                    print('Mem Usage:', mem_usage[0], 'MiB\n')

        # Print board, check winner and change player
        print(str(state.board))
        draw_board(display, state.board)
        num_a, num_n = state.board.count_pieces()
        print('White pieces:', num_a, '\nBlack pieces:', num_n, '\n')
        winner = state.board.get_winner()
        if winner != None:
            if winner == 'a':
                print('White has won')
            elif winner == 'b':
                print('Black has won')
            else:
                print('Tie')
            break
        state.curr_player = get_enemy(state.curr_player)

if __name__ == "__main__":
    args = parser.parse_args()

    while True:
        algo = input('1. Min Max\n2. Alpha Beta\n\n> ')
        if algo == '1':
            algo = 'minmax'
            break
        elif algo == '2':
            algo = 'alphabeta'
            break
        else:
            print('Invalid answer')

    while True:
        player = input('1. White\n2. Black\n\n> ')
        if player == '1':
            player = 'a'
            break
        elif player == '2':
            player = 'n'
            break
        else:
            print('Invalid answer')

    play_game(algo, player, int(args.diff), bool(args.gui))


# def reverseList(self, head: ListNode) -> ListNode:
#     #Initialize a new ListNode
#     prev = None
#     curr = head
#     while curr != None:
#         #storing where this node points
#         nextTemp = curr.next
#         #reversing the pointer to point to the previous node
#         curr.next = prev
#         #now this node is the new previous node
#         prev = curr
#         #get the stored next value we saved since there is no pointer to it anymore
#         curr = nextTemp
#     return prev

def reverseList(head):
    stack = []
    while head != None:
        stack.append(head)
        head = head.next
    dummy = ListNode(-1)
    head  = dummy
    for ln in stack:
        current = stack.pop()
        head.next = current.val
        head = head.next
    return dummy.next


import os

classSet = ['n02877765_', 'n02283201_', 'n01694178_', 'n01985128_', 'n02917067_', 'n01742172_', 'n02910353_', 'n02708093_', 'n01491361_', 'n02114367_', 'n02489166_', 'n02138441_', 'n01770081_', 'n02481823_', 'n02997607_', 'n02807133_', 'n01774384_', 'n01728572_', 'n01664065_', 'n02870880_', 'n01740131_', 'n01729322_', 'n02510455_', 'n02268443_']

blurLevels = [23, 11, 5, 3, 1]
root_dir = "/home/cnslab/imagenet_blur/gaussian/"

for i in range(len(blurLevels)): # different levels of blurring
    mult = blurLevels[i]
    print("Working with square kernel dimension = " + str(mult))
    for directory, subdirectories, files in os.walk(root_dir + 'imagenet_' + str(mult)):
        endPath = directory.split("/")[-1]
        if endPath in classSet:
            train = 1 # used to create a rough 50/50 split since it alternates every time
            for file in files:
                if file.lower().endswith('.jpeg') or file.lower().endswith('.jpg') or file.lower().endswith('.png'):
                    name = os.path.join(directory, file)
                    if train == 1:
                        if not os.path.exists('/home/cnslab/imagenet_blur24/gaussian/imagenet_' + str(mult) + '/train/' + endPath):
                            os.system('mkdir /home/cnslab/imagenet_blur24/gaussian/imagenet_' + str(mult) + '/train/' + endPath)
                        os.system("scp " + name + ' /home/cnslab/imagenet_blur24/gaussian/imagenet_' + str(mult) + '/train/' + endPath + '/' + file)
                        train *= -1
                    else:
                        if not os.path.exists('/home/cnslab/imagenet_blur24/gaussian/imagenet_' + str(mult) + '/val/' + endPath):
                            os.system('mkdir /home/cnslab/imagenet_blur24/gaussian/imagenet_' + str(mult) + '/val/' + endPath)
                        os.system("scp " + name + ' /home/cnslab/imagenet_blur24/gaussian/imagenet_' + str(mult) + '/val/' + endPath + '/' + file)
                        train *= -1


from odoo import models, fields, api, _
import xlwt
import io
import base64
from xlwt import easyxf
import datetime
import calendar
import os
from collections import defaultdict

class ReporteComisiones(models.TransientModel):
    _name = "reporte.comisiones"

    @api.model
    def _get_from_date(self):
        mes = datetime.date.today().strftime("%m")
        anio = datetime.date.today().strftime("%Y")
        return "%s-%s-01" % (anio, mes)

    from_date = fields.Date(string='Desde',default=_get_from_date)
    to_date = fields.Date(string='hasta',default=datetime.date.today())

    invoice_report_printed = fields.Boolean('Imprimir Reporte')
    invoice_summary_file = fields.Binary('Reporte Comisiones')
    file_name = fields.Char('Reporte')

    @api.multi
    def action_print_comision_summary(self):
        workbook = xlwt.Workbook()
        xlwt.add_palette_colour("gris", 0x21)
        workbook.set_colour_RGB(0x21, 191, 191, 191)

        worksheet_general = workbook.add_sheet('Reporte General')
        worksheet_detallado = workbook.add_sheet('Reporte Detallado')

        periodo_1 = "Comisiones: " + self.from_date +' - '+ self.to_date
        fecha_inicio = self.from_date
        fecha_fin = self.to_date

    ####################################    ESTILOS         ###########################################
        titulo_tabla = easyxf('font:height 200; align: horiz center, vert center, wrap yes; font:bold True, name Cambria;border: top_color black, left_color black, right_color black, bottom_color black, left thin,right thin,top thin,bottom thin;')
        cont_left_izde_arab = easyxf('font:height 190; align: vert center, wrap yes; font:name Cambria;border: left_color black, right_color black, top_color black, bottom_color black, left thin,right thin, top thin,bottom thin;')
        cont_center_izde_arab = easyxf('font:height 190; align: horiz center; font:name Cambria;border: left_color black, right_color black, top_color black, bottom_color black, left thin,right thin, top thin,bottom thin;')
        number_1 = easyxf('font:height 190; align: vert center, wrap yes; font:name Cambria; align: horiz right; border: left_color black, right_color black, top_color black, bottom_color black, left thin,right thin, top thin,bottom thin;', num_format_str= "#,##0.00")
        number_2 = easyxf('font:height 200, bold True; font:name Cambria; align: horiz right;border: left_color black, right_color black, top_color black, bottom_color black, left thin,right thin, top thin,bottom thin;', num_format_str= "#,##0.00")

    ####################################    CABECERAS         ###########################################
        worksheet_general.write(0, 1, periodo_1, easyxf('font:height 280; font:bold True, name Cambria;'))
        worksheet_general.write_merge(1, 1, 1, 3, 'Reporte de Comisiones', easyxf('font:height 360; align: horiz center; font:bold True, name Cambria;'))
        worksheet_general.write(3, 1, 'ITEM', titulo_tabla)
        worksheet_general.write(3, 2, 'VENDEDOR', titulo_tabla)
        worksheet_general.write(3, 3, 'COMISION', titulo_tabla)

        worksheet_general.col(1).width = 2500
        worksheet_general.col(2).width = 12000
        worksheet_general.col(3).width = 3500

        worksheet_detallado.write(0, 1, periodo_1, easyxf('font:height 280; font:bold True, name Cambria;'))
        worksheet_detallado.write_merge(1, 1, 1, 8, 'REPORTE DETALLADO DE COMISIONES', easyxf('font:height 360; align: horiz center; font:bold True, name Cambria;'))
        worksheet_detallado.write_merge(3, 4, 1, 1, 'ITEM', titulo_tabla)
        worksheet_detallado.write_merge(3, 4, 2, 2, 'VENDEDOR', titulo_tabla)
        worksheet_detallado.write_merge(3, 4, 3, 3, 'PRODUCTO', titulo_tabla)
        worksheet_detallado.write_merge(3, 4, 4, 4, 'MONTO VENDIDO', titulo_tabla)
        worksheet_detallado.write_merge(3, 4, 5, 5, 'MONTO RECTIFI.', titulo_tabla)
        worksheet_detallado.write_merge(3, 4, 6, 6, 'MONTO TOTAL', titulo_tabla)
        worksheet_detallado.write_merge(3, 4, 7, 7, 'COMISION', titulo_tabla)
        worksheet_detallado.write_merge(3, 4, 8, 8, 'TOTAL', titulo_tabla)
        worksheet_detallado.write_merge(3, 4, 9, 9, 'DETALLE VENTAS', titulo_tabla)
        worksheet_detallado.write_merge(3, 4, 10, 10, 'DETALLE RECTIFICATIVAS', titulo_tabla)

        worksheet_detallado.col(1).width = 2500
        worksheet_detallado.col(2).width = 10000
        worksheet_detallado.col(3).width = 14000
        worksheet_detallado.col(4).width = 3500
        worksheet_detallado.col(5).width = 3500
        worksheet_detallado.col(6).width = 3500
        worksheet_detallado.col(7).width = 3500
        worksheet_detallado.col(8).width = 3500
        worksheet_detallado.col(9).width = 8000
        worksheet_detallado.col(10).width = 8000
        worksheet_detallado.row(3).set_style(easyxf('font:height 240;'))
        worksheet_detallado.row(4).set_style(easyxf('font:height 240;'))

    ########################  Consultas  ########################
        obj_empleados = self.env['hr.employee'].search([('active', '=', True)])
        cont_general = 4
        cont_detallado = 5
        item_general = 0
        item_detallado = 0
        for empleado in obj_empleados:
            producto_precio = defaultdict(float)
            producto_nombre = defaultdict(str)
            producto_id = defaultdict(int)
            producto_pre_total = defaultdict(float)
            monto_vendido = defaultdict(float)
            monto_rectificativa = defaultdict(float)
            detalle_venta = defaultdict(str)
            detalle_rectificativa = defaultdict(str)
            ajajaja = ""
            item_general += 1
            comi_total = 0

            obj_factura_rectificativas = self.env['account.invoice'].search([('type', '=', 'out_refund'), ('state', 'in', ['open','paid']),
                                                                             ('date_invoice', '>=', fecha_inicio), ('date_invoice', '<=', fecha_fin),
                                                                             ])

            obj_facturas_ventas = self.env['account.invoice'].search([('date_invoice', '>=', fecha_inicio), ('date_invoice', '<=', fecha_fin),
                                                                     ('type', '=', 'out_invoice'), ('state', 'in', ['open','paid']),
                                                                     ('move_name', '!=', False), ('user_id.id', '=', empleado.user_id.id)])
            for factura in obj_facturas_ventas:
                obje_invoice_line_ids = self.env['account.invoice.line'].search([('invoice_id', '=', factura.id)])

                for line in obje_invoice_line_ids:
                    detalle_venta[line.product_id.product_tmpl_id.id] +=  factura.number + ' | '
                    producto_precio[line.product_id.product_tmpl_id.id] +=  (line.price_subtotal*(1/(line.invoice_id.currency_id.rate or 1)))
                    producto_nombre[line.product_id.product_tmpl_id.id] =  line.product_id.name
                    producto_id[line.product_id.product_tmpl_id.id] =  line.product_id.product_tmpl_id.id
                    producto_pre_total[line.product_id.product_tmpl_id.id] += (line.price_subtotal*(1/(line.invoice_id.currency_id.rate or 1)))
                    monto_vendido[line.product_id.product_tmpl_id.id] += (line.price_subtotal *(1/(line.invoice_id.currency_id.rate or 1)))

            for factura in obj_factura_rectificativas:
                obje_factura_inicial = self.env['account.invoice'].search([('number', '=', factura.origin), ('user_id.id', '=', empleado.user_id.id),('state', 'in', ['open','paid'])]) #

                for fac in obje_factura_inicial:
                    obje_rectificativa_invoice_line_ids = self.env['account.invoice.line'].search([('invoice_id.origin', '=', fac.number), ('invoice_id','=',factura.id)]) #
                    for line in obje_rectificativa_invoice_line_ids:
                        detalle_rectificativa[line.product_id.product_tmpl_id.id] +=  factura.number + ' | '
                        producto_precio[line.product_id.product_tmpl_id.id] -=  (line.price_subtotal*(1/(line.invoice_id.currency_id.rate or 1)))
                        producto_nombre[line.product_id.product_tmpl_id.id] =  line.product_id.name
                        producto_id[line.product_id.product_tmpl_id.id] =  line.product_id.product_tmpl_id.id
                        producto_pre_total[line.product_id.product_tmpl_id.id] -= (line.price_subtotal*(1/(line.invoice_id.currency_id.rate or 1)))
                        monto_rectificativa[line.product_id.product_tmpl_id.id] += (line.price_subtotal*(1/(line.invoice_id.currency_id.rate or 1)))

            inicio = cont_detallado
            for i in list(producto_id):
                item_detallado += 1
                aux_comision = 0
                total = producto_pre_total[i]
                multiplo=total/(abs(total) or 1)
                porcen = 0
                aux_total = abs(total)
                aux_anterior = 0
                obj_comisiones = self.env['product.porcentaje.line'].search([('product_id.id', '=', producto_id[i])])
                comisiones_porcen = defaultdict(float)
                comisiones_monto = defaultdict(int)
                for com in obj_comisiones:
                    if abs(total) >= com.porcentaje_id.name:
                        aux_total -= (com.porcentaje_id.name-aux_anterior)
                        porcen = porcen + ((com.porcentaje_id.name-aux_anterior)*com.monto/100)
                        aux_anterior = com.porcentaje_id.name
                        aux_comision = com.monto
                    elif abs(total)<com.porcentaje_id.name and abs(total)>=aux_anterior:
                        porcen = porcen + (aux_total*com.monto/100)
                        aux_total -= (com.porcentaje_id.name-aux_anterior)
                        aux_comision = com.monto
                        aux_anterior = com.porcentaje_id.name
                if  aux_total >0:
                    porcen = porcen +(aux_total*aux_comision/100)

                comi_total += porcen*multiplo

                worksheet_detallado.write(cont_detallado, 1, str(item_detallado).zfill(2), cont_center_izde_arab) #TIPO DE TABLA (2)
                worksheet_detallado.write(cont_detallado, 2, empleado.user_id.name, cont_left_izde_arab) #TIPO DE TABLA (2)
                worksheet_detallado.write(cont_detallado, 3, producto_nombre[i], cont_left_izde_arab) #TIPO DE TABLA (2)
                worksheet_detallado.write(cont_detallado, 4, monto_vendido[i], number_1)
                worksheet_detallado.write(cont_detallado, 5, monto_rectificativa[i], number_1)
                worksheet_detallado.write(cont_detallado, 6, total, number_1)
                worksheet_detallado.write(cont_detallado, 7, porcen*multiplo, number_1)
                worksheet_detallado.write(cont_detallado, 9,  detalle_venta[i], cont_left_izde_arab)
                worksheet_detallado.write(cont_detallado, 10,  detalle_rectificativa[i], cont_left_izde_arab)
                cont_detallado += 1
            fin = cont_detallado -1
            if inicio<=fin:
                worksheet_detallado.write_merge(inicio, fin, 8, 8, comi_total, number_1)
                #worksheet_detallado.write_merge(inicio, fin, 9, 9, 'VENTAS: ' + detalle_venta[i] + ' \n ' +'RECTIFICATIVAS: '+ detalle_rectificativa[i], cont_left_izde_arab)
            worksheet_general.write(cont_general, 1, str(item_general).zfill(2), cont_center_izde_arab)
            worksheet_general.write(cont_general, 2, empleado.user_id.name, cont_left_izde_arab)
            worksheet_general.write(cont_general, 3, comi_total, number_1)
            cont_general += 1
    ####################################    FINAL    ###################################################
        fp = io.BytesIO()
        workbook.save(fp)
        excel_file = base64.encodestring(fp.getvalue())
        self.invoice_summary_file = excel_file
        self.file_name =  periodo_1 +'.xls'
        self.invoice_report_printed = True
        fp.close()
        return {
                'view_mode': 'form',
                'res_id': self.id,
                'res_model': 'reporte.comisiones',
                'view_type': 'form',
                'type': 'ir.actions.act_window',
                'context': self.env.context,
                'target': 'new',
        }


import numpy as np
from skimage import data, exposure
from PIL import Image
import cv2
import csv

#zmienne globalne, do przechowywania wzorc�w literek - ka�dy index to inna linijka

chars=['G','9','8','7','6','5','4','3','2','1','0','Z','Y','X','W','V',
       'U','T','S','R','Q','P','O','N','H','F','E','D','B','M','L','K','J','C','A','I']
horz = []
vert = []

def load32(all):
    for i in range(36):
        horz.append(all[i][:32])
        vert.append(all[i][32:])

#wczytujemy do struktur dane
def load_csv(file):
    with open(file,'r') as filey:
        for line in filey:
            line=line.split('|') #nie wiem co ta linijka robi, ale jak j� usun�, to si� crashuje program
            line[-1]=line[-1][:-1]
            for lines in line:
                lines=lines.split(',')
                lines=list(filter(None,lines)) #usuni�cie warto�ci None (NULL dla cpp, nil dla PASCALa)
                horz.append(lines[1:17])
                vert.append(lines[17:])

class Plate:
    h=0; w=0; img=np.ndarray(0); letters=[np.ndarray(0)]; name='';
	#inicjujemy wszystkie zmienne
    def __init__(self,name):
        self.name=name
        self.img=data.imread('plates/'+name+'.jpg', as_grey=True).copy()
        self.h,self.w=self.img.shape[:2]
		#rzutujemy na warto�ci 0-255
        self.img = exposure.rescale_intensity(self.img, out_range=(0,255)).copy()
		#rozmycie gaussa, by pozby� si� ma�ego syfu
        self.img = cv2.GaussianBlur(self.img,(5,5),0)
    def thresh(self):
        for row in range(self.h):
            for value in range(self.w):
				#warto�� thresholdu ustawiona tak, by jak najlepiej (bez ��cze�) oznacza�a literki
                if self.img[row][value] < np.percentile(self.img,75):
                    self.img[row][value]=0
                else:
                    self.img[row][value]=255
	#1. z 0-255 na 0-1 2. podnosimy do kwadratu 3. z 0-1 na 0-255
    def invertngamma(self):
        for row in range(self.h):
            for value in range(self.w):
                self.img[row][value]=((1-self.img[row][value]/255)**2)*255
    def save(self): Image.fromarray(self.img).convert('RGB').save('plates/'+self.name+'2.jpg')
	#wyci�ganie liter z tablic. najwa�niejsza cz��
    def save_letters(self):
		#musimy rzutowa� warto�ci na cv2_int8, �eby traktowa�o nparray'e jako obrazy
        self.img=cv2.convertScaleAbs(self.img)
		#cnt to kolekcja kontur�w
        _,cnt,_= cv2.findContours(self.img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
        print(self.name)
        for contour in cnt:
			#dla ka�dego znalezionego kontura znajdujemy minimalny opisuj�cy prostok�t
            rect=cv2.minAreaRect(contour)
			#poni�ej zabawa ze wsp�rz�dnymi
            box=np.int0(cv2.boxPoints(rect))
            tx=[x[0] for x in box]
            ty=[y[1] for y in box]
            boxx=min(tx),min(ty),max(tx),max(ty)
            temp_w=np.int0(max(tx)-min(tx))
            temp_h=np.int0(max(ty)-min(ty))
			#wy�uskujemy te kontury o rozmiarze min 5x15
            if temp_w>4:
                if temp_h>15:
					#pod zmienn� rcc zapisujemy aktualny prostok�t zawieraj�cy w sobie literk�
                    rcc=self.img[min(ty):max(ty),min(tx):max(tx)].copy()
					#jeszcze raz threshold z typem float32 (kolejne wymogi Open CV)
                    for x in range(temp_h):
                        for y in range(temp_w):
                            if rcc[x][y]>0: rcc[x][y]=np.float32(255)
                            else: rcc[x][y]=np.float32(0)
					#erozja dla poprawienia literki (cz�sto wycina 'chude' chary)
                    rcc = cv2.erode(rcc,(2,2),iterations=1)
					#normalizacja do rozmiaru 16x16 (dla wzorca 16x16)
                    rcc = cv2.resize( rcc, (16,16), interpolation=cv2.INTER_AREA)
					#obliczanie histogram�w dla wyci�tego symbolu
                    t_horz = np.sum(rcc == 0, axis=0)
                    t_vert = np.sum(rcc == 0, axis=1)
					#inicjalizacja zmiennych do funkcji dopasowuj�cej - spory error, bo szukamy minimum
                    t_error=100000000;t_id=6;wx=1;wy=1;t_err2=10000000;t_id2=0;
                    #dla ka�dego wzorca symbolu
                    for l in range(36):
                    #konwertujemy stringi na inty
                        horz[l]=[int(x) for x in horz[l]]
                        vert[l]=[int(x) for x in vert[l]]
						#wyliczamy r�nic� mi�dzy wzorcem a symbolem
                        t_x = t_horz - horz[l]
						#wszystkie r�nice podnosimy do kwadratu
                        t_x = [x*x for x in t_x]
						#i sumujemy
                        e_x = np.sum(t_x)
						#jw
                        t_y = t_vert - vert[l]
                        t_y = [y*y for y in t_y]
                        e_y = np.sum(t_y)
                        if e_x+e_y < t_error:
							#wskazujemy nowy b��d oraz id literki
                            t_err2=t_error
                            t_id2=t_id
                            t_error = (wx*e_x)+(wy*e_y)
                            t_id=l
                    print(np.sqrt(t_error), chars[t_id])
					#wypisanie na obrazie literki - przesuni�cie o min(tx) pozwala zachowa� przesuni�cie z tablicy rejestracyjnej
                    img=cv2.imread('out/'+self.name+'.jpg',1)
                    cv2.putText(img, chars[t_id], (50+min(tx),50), cv2.FONT_ITALIC, 1, (0,255,0), 2)
                    cv2.imwrite('out/'+self.name+'.jpg', img)
					#wypisujemy w konsoli najbardziej pasuj�cy symbol

class IMGArray:
    nparray=np.ndarray(0); h=0; w=0; ndim=0; cnt=np.ndarray(0); temp=np.array(0); bw=np.array(0); plate=(0,np.ndarray(0));address='x'
    def __init__(self,address):
	#wczytujemy obraz do pami�ci
        self.address=address
        self.temp = data.imread('img3/P60400'+address+'.jpg', as_grey=True).copy();
        self.bw = data.imread('img3/P60400'+address+'.jpg', as_grey=True).copy();
        self.nparray = data.imread('img3/P60400'+address+'.jpg').copy();
        self.ndim=self.nparray.ndim
        self.h,self.w=self.nparray.shape[:2]
    def save(self,name): Image.fromarray(self.nparray).convert('RGB').save('img2/'+name+'.jpg')
	#wyszukujemy kraw�dzie, a nast�pnie 'zamykamy' obszary z wieloma kraw�dziami, bia�ymi prostok�tami o wymiarach 30x5
    def change(self):
        self.temp = cv2.GaussianBlur(self.temp,(5,5),0)
        self.temp = cv2.Sobel(self.temp, cv2.CV_8U, 1, 0, ksize=3)
        _,self.temp=cv2.threshold(self.temp,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
        temp_se = cv2.getStructuringElement(cv2.MORPH_RECT,(30,5))
        self.temp = cv2.morphologyEx(self.temp,cv2.MORPH_CLOSE, temp_se)
	#zwracamy kolekcj� kontur�w
    def find_cnt(self): _,self.cnt,_ = cv2.findContours(self.temp,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
    def color_cnt(self):
        for contour in self.cnt:
			#przetwarzamy pocz�tkowo ka�dy element kolekcji, w poszukiwaniu tablicy
            rect=0;box=0;tx=0;ty=0;
            rect=cv2.minAreaRect(contour)
            box=np.int0(np.around(cv2.boxPoints(rect)))
            tx=[x[0] for x in box]
            ty=[y[1] for y in box]
            boxx=(min(tx),min(ty),max(tx),max(ty))
            temp_w=np.int0(max(tx)-min(tx))
            temp_h=np.int0(max(ty)-min(ty))
			#sprawdzamy warunki pocz�tkowe, kt�re zwi�zane s� z tablicami rejestracyjnymi
            if(temp_w*temp_h > 5000):
                if(temp_w>temp_h):
                    if(temp_h<50):
                        if(temp_w<250):
                            print('{} {}'.format(temp_w,temp_h))
							#zaznaczamy na r�owo tablic� na obrazie
                            cv2.drawContours(self.nparray,[box],0,(255,0,255),2)
                            input=Image.open('img3/P60400'+self.address+'.jpg')
                            self.plate=input.crop(boxx)
                            #wycinamy i zapisujemy tablic� jako BW
                            self.plate.save('plates/'+self.address+'.jpg')

#generuje obrazy tablic rejestracyjnych. wymagane za 1. razem
def init_plates(imgs):
    for value in imgs:
        newimage=IMGArray(value)
        newimage.change()
        newimage.find_cnt()
        newimage.color_cnt()
        newimage.save(value)

def clear_out(img):
    temp = data.imread('img2/' + img + '.jpg').copy();
    Image.fromarray(temp).convert('RGB').save('out/' + img + '.jpg')

if __name__ == '__main__':
    numbers = ['03','05','06','12', '13', '15','16', '21', '27', '29', '30', '42', '51', '55', '59', '63', '64', '66']
    #init_plates(numbers)
    load_csv('nn16.csv')
    #load32()
    for plate in numbers:
        clear_out(plate)
        newplate=Plate(plate)
        newplate.invertngamma()
        newplate.thresh()
        newplate.save_letters()
        newplate.save()


from flask import Flask
from flask_session import Session
from flask_pymongo import PyMongo
from source.Todo import todo
from flask import g
import pymongo
import config
import configparser

#app init
app = Flask(__name__)
#get session config
app.config.from_pyfile('config.py')

Session(app)
#blueprint register
app.register_blueprint(todo,url_prefix='/todo')
#get db parameter
config = configparser.ConfigParser()
config.read('parameter.ini')
server = config.get('DB','host')
port = config.getint('DB','port')

@app.before_request
def before_request():
    #open db connection & set into g
    if 'db' not in g:
        client = pymongo.MongoClient(host=server, port=port)
        g.db = client

@app.teardown_request
def teardown_request(exception):
    #close db connection & remove from g
    if hasattr(g,"db"):
        g.db.close()
        g.pop('db', None)

#action in the middle of application and server(werkzeug)
class AppMiddleware(object):
    def __init__(self, app, script_name=''):
        self.app = app
        self.script_name = script_name

    def __call__(self, environ, start_response):
        '''
        script_name = self.script_name
        if self.script_name:
            environ['SCRIPT_NAME'] = script_name

            path_info = environ['PATH_INFO']
            if path_info.startswith(script_name):
                environ['PATH_INFO'] = path_info[len(script_name):]
        '''
        return self.app(environ, start_response)

app.wsgi_app = AppMiddleware(app.wsgi_app)

if __name__ == '__main__':
    app.run(host='0.0.0.0',debug=True)


from flask import Flask, render_template, request, redirect, session
import random
app = Flask(__name__)
app.secret_key = "heythere"

@app.route('/')
def index():
    if 'num' not in session:
        session['num'] = random.randrange(0,101)
    if 'guess' not in session:
        answer= ""
    else:
        if str(session['guess']) == str(session['num']):
            answer = "You are right!!! " + str(session['num']) + " is the number!"
            session.pop('num')
            session.pop('guess')
            return render_template("results.html", answer=answer)
        else:
            if int(session['guess']) < int(session['num']):
                answer = "You are too low."
            else:
                answer = "You are too high."
            session.pop('guess')
    return render_template("index.html", answer=answer)

@app.route('/results', methods=['POST'])
def results():
    session['guess'] = request.form['guess']
    return redirect('/')

@app.route('/index2', methods=['POST'])
def index2():
    return redirect('/')

app.run(debug=True)


#environmental_reports

import collections
import csv
from map_to_drive import map_to_drive #path to Project Data folder

def environmental_reports(well_data_headings, well_data):

	print('\n================= Environmental Report Module =================\n')
	print('The module outputs all the environmental incidents associated with the selected wells and facilities')

	well_environmental_reports = collections.OrderedDict() # a dictionary filled with report data registered to each well environmental_reports[well]
	facility_environmental_reports = collections.OrderedDict()
	selected_environmental_reports = collections.OrderedDict()

	file_location = map_to_drive() + "/Project Data/geoSCOUT_data/post 2005 environmental_reports.csv"

	with open(file_location) as f:
		reader = csv.reader(f)
		for row in reader:

			well_indexs = []
			facility_indexs = []

			if row[0] == 'Release Number':
				environmental_data_headings = row

				for i in range(0,len(environmental_data_headings)):
					if environmental_data_headings[i] == 'License Number':
						env_license_index = i
					if environmental_data_headings[i] == 'License Type':
						type_index = i
					if environmental_data_headings[i] == 'Incident Date':
						date_index = i
					if environmental_data_headings[i] == 'Injury Number':
						injury_index = i
					if environmental_data_headings[i] == 'Fatality Number':
						fatality_index = i

			else:

				#we need to split the licenses for each 'released number' which contains multiple licenses
				license_types = row[type_index].split(';')
				licenses = row[env_license_index].replace(' ', '').split(';')
				dates = row[date_index].split(';')

				#print(license_types)
				#print(licenses)

				#of the licenses we are only interested in well license currently
				for i in range(0,len(license_types)):
					if license_types[i] == 'Well Licence':
						well_indexs.append(i)
					if license_types[i] == 'Facility Licence':
						facility_indexs.append(i)
					#print(license_types)
					#print(indexs)

				#Look through all the well licenses and append them to a dict
				for i in range(0,len(well_indexs)):
					try:
						well_environmental_reports[licenses[well_indexs[i]]].append(row)
					except:
						well_environmental_reports[licenses[well_indexs[i]]] = [row]

				for i in range(0,len(facility_indexs)):
					try:
						facility_environmental_reports[licenses[facility_indexs[i]]].append(row)
					except:
						facility_environmental_reports[licenses[facility_indexs[i]]] = [row]

		'''
		for license in well_environmental_reports:
			print('\n')
			print(license)
			print(well_environmental_reports[license])
			print('\n')
		'''

	print('\n~~~~~~~~~~~~~~ Report Data Available ~~~~~~~~~~~~~~\n')
	print(environmental_data_headings)

	#now looking through the well liceenses from the general well data
	for i in range(0,len(well_data_headings)):
		if well_data_headings[i] == 'Lic/WA/WID/Permit #':
			well_license_index = i

	wells_with_reports = []
	injury_array = []
	fatality_array = []

	for well in well_data:

		#license number varies depending on which source of wells,
		#we need to ad a zero for the montney well set

		if __name__ == '__main__':
			license = '0' + well_data[well][well_license_index]
		else:
			license =  well_data[well][well_license_index]

		#if the well license matches and environmental report license
		if license in well_environmental_reports:
			selected_environmental_reports[well] = well_environmental_reports[license]
			wells_with_reports.append(well)

			#print('\n')
			print(well)
			print(license)
			print('\n')
			#print(well_environmental_reports[license])
			#print('\n')

			for i in range(0, len(well_environmental_reports[license])):
				injuries = well_environmental_reports[license][i][injury_index]
				fatalities = well_environmental_reports[license][i][fatality_index]
				print('Injuries:  ' +str(injuries))
				print('Fatalities:  ' + str(fatalities))
				for j in range(0,len(injuries.split(';'))):
					injury_array.append(int(injuries.split(';')[j]))
					fatality_array.append(int(fatalities.split(';')[j]))

	#print indicent type and indicent value for the slected wells next to eachother
	'''
	for well in selected_environmental_reports:
		print('\n')
		for i in range(0,len(selected_environmental_reports[well])):
			for j in range(0,len(environmental_data_headings)):
				print(environmental_data_headings[j] + '   ' + selected_environmental_reports[well][i][j])
	'''

	print('\n~~~~~~~~~~~ SUMMARY ~~~~~~~~~~~')
	print('\nWells/facilities with associated environmental reports:  ' + str(len(wells_with_reports)))
	print('Total number of Injeries Associated With Wells:  ' + str(sum(injury_array)))
	print('Total number of Fatalities Associated With Wells:  ' + str(sum(fatality_array)))
	print('Note - Ensure they are associated with well and not something else')

	return selected_environmental_reports

if __name__ == "__main__":

	from get_well_data import get_formation_well_data

	#MONTNEYY ONLY!!!!!
	print('\nImporting Montney Well Data\n') #MONTNEY
	well_data_function = get_formation_well_data() # MONTNEY
	well_data_headings = well_data_function[0] # MONTNEY
	well_data = well_data_function[1] # MONTNEY

	environmental_reports(well_data_headings, well_data)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import pprint

def ferz(n, m):
    kletka = [['+'] * (m+1) for i in range(n+1)]

    for i in range(n-1, -1, -1):
        for j in range(m-1, -1, -1):
            if (kletka[i+1][j+1] == '+' and
               kletka[i][j+1] == '+' and
               kletka[i+1][j] == '+' ):
                kletka[i][j] = '-'

    return kletka

tmp = ferz(5, 2)
for line in tmp:
    str_line = ''
    for j in line:
        str_line = ''.join((str_line, j, ' '))
    print(str_line)


from django.core.management.base import BaseCommand
from ss411.ssoffices.models import SsOffice
import os
import csv

class Command(BaseCommand):
    help = ('our help string comes here')

    def _fix_recs(self):

        rows = SsOffice.objects.all()
        for row in rows:
            # correct displayname

            # # correct uncapitalized state names
            # padded_state_code = f" {row.state.lower()} "
            # if row.display_name.find(padded_state_code) >= 0:
            #     new_display_name  = row.display_name.replace(padded_state_code, f" {row.state} ")
            #     row.display_name = new_display_name
            #     row.save()

            # if len(row.display_name.split(" ")) > 3:
            #     word2 = row.display_name.split(" ")[1]
            #     new_display_name = row.display_name.replace(word2, word2.capitalize())
            #     row.display_name = new_display_name
            #
            # if len(row.display_name.split(" ")) > 4:
            #     word3 = row.display_name.split(" ")[2]
            #     new_display_name = row.display_name.replace(word3, word3.capitalize())
            #     row.display_name = new_display_name

            for s in ('North', 'South', 'East', 'West'):
                if row.display_name.find(s.lower()) >= 0:
                    row.display_name = row.display_name.replace(s.lower(), s.upper())

            row.save()

    def _import_recs(self):
        fn = "foimportastext.csv"
        ScriptDir = os.path.dirname(os.path.abspath(__file__))
        filepath = os.path.join(ScriptDir, fn)
        with open(filepath, "r") as data:
            for o in csv.DictReader(data):
                try:
                    newSSO = SsOffice()
                    #office_code as is
                    setattr(newSSO, "type", "FO")
                    setattr(newSSO, "ssa_office_code", o.get("ssa_office_code", ""))
                    setattr(newSSO, "ssa_office_name", o.get("ssa_office_name", ""))
                    setattr(newSSO, "address1", o.get("address1", "").title())
                    setattr(newSSO, "address2", o.get("address2", "").title())
                    setattr(newSSO, "city", o.get("city", "").title())
                    setattr(newSSO, "state", o.get("state", ""))
                    setattr(newSSO, "zipcode", o.get("zipcode", ""))
                    pn = o.get("tel_public", "")
                    new_pn = pn.replace("(",'+1').replace(") ","").replace('-',"")
                    setattr(newSSO, "tel_public", new_pn)
                    #set slug to the SSA's official name for the office plus '-fo'
                    setattr(newSSO, "slug", newSSO.ssa_office_name.lower().replace(" ", "-")+"-fo")
                    setattr(newSSO, "display_name", newSSO.ssa_office_name.capitalize()+' FO')
                    setattr(newSSO, 'ssa_last_updated', '2020-10-01')
                    newSSO.save()
                except Exception as e:
                    print("Oops!", e.__class__, "occurred.")
                    print(newSSO)
                    print("Next entry.")
                    print()

    def handle(self, *args, **options):
        self._fix_recs()


#!/usr/bin/python

#import requests
import sys, os, socket

#------------ /-/ ------------#

if (len(sys.argv) != 2) or (sys.argv[1] == '-h'):
	print('Este script necesita un argumento para funcionar\n')
	print('python requester.py <file>\n')
	print('\targ1: fichero con IPs para checkear')
	exit(1)

ips = open(str(sys.argv[1]), 'r')

os.system('rm -f output')
os.system('echo "" > output')
output = open('output', 'w')

request = []

for ip in ips.readlines():
  c = 0

  try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(3)
    s.connect((ip, 443))
    c = 1
    output.write('IP: ' + ip.strip('\n') + ' - Hit - Service on port 443\n')

  except ConnectionRefusedError:
    output.write('IP: ' + ip.strip('\n') + ':443 - Miss - Connection Refused\n')

  except socket.gaierror:
    output.write('IP: ' + ip.strip('\n') + ':443 - Miss - gaierror\n')

  except socket.timeout:
    output.write('IP: ' + ip.strip('\n') + ':443 - Miss - timeout\n')

  finally:
    s.close()

  try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(3)
    s.connect((ip, 80))
    output.write('IP: ' + ip.strip('\n') + ' - Hit - Service on port 80\n')

    if c == 1:
      c = 3
    else:
      c = 2

  except ConnectionRefusedError:
    output.write('IP: ' + ip.strip('\n') + ':80 - Miss - Connection Refused\n')

  except socket.gaierror:
    output.write('IP: ' + ip.strip('\n') + ':80 - Miss - gaierror\n')

  except socket.timeout:
    output.write('IP: ' + ip.strip('\n') + ':80 - Miss - timeout\n')

  finally:
    s.close()

  t = (ip,c)
  request.append(t)

print(request)

#111 -> ConnectionRefusedError
#-2  -> gaierror
#timeout -> timeout

ips.close()
output.close()

"""
  except Exception as e:
    print(e)
    r = requests.get('http://' + ip, timeout=3)
    output.write(ip.strip('\n') + 'http ' + str(r) + '\n')

for ip in ips.readlines():
  try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((ip, 443))

  except:
    print('a')

"""


import pathlib
from datetime import datetime
from datetime import timedelta
from uuid import uuid4

import pytest
import xmlschema

from cisu.src.cisu.entities.edxl_entity import EdxlEntity
from cisu.src.cisu.entities.cisu_entity import CisuEntity, AddressType, MessageType, Status, AckMessage, \
    Recipients
from cisu.src.cisu.entities.cisu_entity import Recipient
from cisu.src.cisu.entities.commons import DateType
from cisu.src.cisu.entities.commons.common_alerts import AnyURI
from cisu.src.cisu.factories.cisu_factory import MessageCisuFactory
from cisu.src.cisu.factories.edxl_factory import EdxlMessageFactory
import xml.dom.minidom
from tempfile import NamedTemporaryFile

def test_build_ack_message():
    ack_message_id = str(uuid4())
    edxl: EdxlEntity = EdxlMessageFactory.create(
        uuid=str(uuid4()),
        date_time_sent=DateType(datetime.now()),
        date_time_expires=DateType(datetime.now() + timedelta(days=1)),
        distribution_status="Actual",
        distribution_kind="Report",
        sender_id=str(uuid4()),
        receivers_address=["sgc-enki"],
        resource=CisuEntity(
            message=MessageCisuFactory.create(
                uuid=str(uuid4()),
                sender=AddressType("sgc-enki", AnyURI("uri")),
                sent_at=DateType(datetime.now()),
                msg_type=MessageType.ACK,
                status=Status.SYSTEM,
                recipients=Recipients([
                    Recipient("sgc-enki", AnyURI("uri"))
                ]),
                choice=AckMessage(ackMessageId=ack_message_id)
            )
        )
    )

    assert isinstance(edxl.resource.message.choice, AckMessage)
    assert edxl.resource.message.choice.ackMessageId == ack_message_id

filenames = list(pathlib.Path(pathlib.Path(__file__).parent.absolute()).glob("data/*.xml"))

@pytest.mark.parametrize("filename", filenames)
def test_build_ack_message_from_another_message(filename):
    dom = xml.dom.minidom.parse(str(filename))
    other_edxl = EdxlEntity.from_xml(dom)
    ack_sender = AddressType("test", AnyURI("test"))
    ack_message = EdxlMessageFactory.build_ack_from_another_message(
        sender_address=ack_sender,
        other_message=other_edxl,
    )

    assert isinstance(ack_message.resource.message.choice, AckMessage)
    assert ack_message.resource.message.choice.ackMessageId == other_edxl.resource.message.messageId
    assert ack_message.resource.message.recipients == Recipients([
        other_edxl.resource.message.sender
    ])
    assert ack_message.senderID == ack_sender.URI.path_name
    assert ack_message.resource.message.sender == ack_sender

@pytest.mark.parametrize("filename", filenames)
def test_edxl_xml_generation(filename):
    dom = xml.dom.minidom.parse(str(filename))
    other_edxl = EdxlEntity.from_xml(dom)

    ack_message = EdxlMessageFactory.build_ack_from_another_message(
        sender_address=AddressType("test", AnyURI("test")),
        other_message=other_edxl,
    )

    schema_path = pathlib.Path(pathlib.Path(__file__).parent.absolute(), "../../src/cisu/entities/schema/cisu.xsd")

    xml_string = ack_message.resource.message.to_xml()
    temp_file = NamedTemporaryFile(mode="w", suffix=".xml")
    with open(temp_file.name, "w") as f:
        f.write(xml_string)
    my_schema = xmlschema.XMLSchema(str(schema_path))
    print(my_schema.validate(temp_file.name))
    assert my_schema.is_valid(temp_file.name)
    temp_file.close()


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

# ---------------------------------------------------------------------
# Copyright (c) 2012 Michael Hull.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#  - Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  - Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ----------------------------------------------------------------------

from morphforge.morphology.ui.morphmaths import MorphologyForRenderingOperator

from morphforge.morphology.visitor import DictBuilderSectionVisitorHomo

import numpy
import numpy as np

class MatPlotLibViewer(object):

    """
    Plot Projections of a morphology onto XY, XZ, and YZ axes.
    """

    plot_views = [0, 1, 2]

    _projMatXY = numpy.array([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]])
    _projMatXZ = numpy.array([[0.0, 0.0, 1.0], [0.0, 1.0, 0.0], [1.0, 0.0, 0.0]])
    _projMatYZ = numpy.array([[1.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, 1.0, 0.0]])

    _figure_projections = {0: _projMatXY, 1: _projMatXZ, 2: _projMatYZ}
    _figure_labels = {0: ('X', 'Y'), 1: ('Z', 'Y'), 2: ('X', 'Z')}
    _figure_positions = {0:221, 1:222, 2:223}
    _figure_titles = {0:'View From Above', 1:'View From Side', 2:'View From Front'}

    def __init__(self, morph, use_pca=True, fig_kwargs=None):

        if morph == None:
            raise ValueError('No Cell')

        self.morph = morph

        self.fig = None
        self.fig_kwargs = fig_kwargs if fig_kwargs is not None else {}
        self.subplots = {}
        self.normaliser = None

        self.build_plot(use_pca)

    def build_draw_sub_plot(self, rotatedSectionDict, fig, i, plotLims):
        import pylab
        from matplotlib.path import Path
        from matplotlib import patches

        subplotnum = self._figure_positions[i]
        title = self._figure_titles[i]
        proj_matrix = self._figure_projections[i]
        labels = self._figure_labels[i]

        ax = fig.add_subplot(subplotnum, aspect='equal')

        # Find the depth extremes for coloring:

        (z_min, z_max) = (None, None)
        for seg in self.morph:
            xyzProj = numpy.dot(proj_matrix, rotatedSectionDict[seg])
            z_min = (xyzProj[2] if not z_min else min(z_min, xyzProj[2]))
            z_max = (xyzProj[2] if not z_max else max(z_max, xyzProj[2]))
        zrange = z_max - z_min

        for seg in self.morph:
            xyzProj = numpy.dot(proj_matrix, rotatedSectionDict[seg])
            xy_proj = numpy.array([xyzProj[0], xyzProj[1]])

            xyz_proj_parent = numpy.dot(proj_matrix, rotatedSectionDict[seg.parent])
            xy_proj_parent = numpy.array([xyz_proj_parent[0], xyz_proj_parent[1]])

            color = str((xyzProj[2] - z_min) / zrange) if zrange > 0.001 else 'grey'

            linewidth = (seg.d_r + seg.p_r) / 2.0 * 2.0

            # Test if we have just tried to draw a point, if so then draw a circle:
            if numpy.linalg.norm(xy_proj - xy_proj_parent) < 0.0001:
                try:
                    ax.add_patch(pylab.Circle(xy_proj, radius=linewidth/2.0, color=color))
                    ax.plot(xy_proj[0], xy_proj[1], '+', markersize=linewidth/2.0, color='red')
                except:
                    pass
            else:

                # Simple version, which doesn't work so well:
                # ax.plot([xy_proj[0], xy_proj_parent[0]], [xy_proj[1], xy_proj_parent[1]], linewidth=linewidth, color=color)

                # More complex patch version:
                joining_vec = xy_proj - xy_proj_parent
                joining_vec_norm = joining_vec / numpy.linalg.norm(joining_vec)

                perp_vec = np.array((joining_vec_norm[1], joining_vec_norm[0] * -1))

                assert np.fabs(np.dot(joining_vec_norm, perp_vec)) < 0.01

                # The points:
                p1 = xy_proj + (perp_vec * seg.d_r)
                p2 = xy_proj - (perp_vec * seg.d_r)

                p3 = xy_proj_parent - (perp_vec * seg.p_r)
                p4 = xy_proj_parent + (perp_vec * seg.p_r)

                verts = [p1, p2, p3, p4, (0, 0)]
                codes = [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY]
                path = Path(verts, codes)
                patch = patches.PathPatch(path, facecolor=color, lw=1)
                ax.add_patch(patch)

        ax.set_title(title)
        ax.set_xlim(plotLims)
        ax.set_ylim(plotLims)
        ax.set_xlabel(labels[0])
        ax.set_ylabel(labels[1])

        from matplotlib.ticker import MaxNLocator
        ax.xaxis.set_major_locator(MaxNLocator(4))
        ax.yaxis.set_major_locator(MaxNLocator(4))

        ax.grid(True)
        return ax

    def build_plot(self, usePCA):
        import pylab

        self.normaliser = MorphologyForRenderingOperator(self.morph,
                                                         usePCA=usePCA)

        # Find the Point that is the furthest distance way from
        # the centre when the cell is centred and rotated:
        rotator = lambda s: self.normaliser(s.get_distal_npa3())

        rotated_section_dict = DictBuilderSectionVisitorHomo(morph=self.morph, functor=rotator) ()

        # Add in the parents manually:
        dummy_scetion = self.morph._dummysection
        rotated_section_dict[dummy_scetion] = self.normaliser(dummy_scetion.get_distal_npa3())

        max_axis = max([numpy.linalg.norm(rotated_section_pt) for rotated_section_pt in rotated_section_dict.values()])
        plot_lims = (max_axis * -1.1, max_axis * 1.1)

        max_x = max([numpy.fabs(rotated_section_pt[0]) for rotated_section_pt in rotated_section_dict.values()])
        max_y = max([numpy.fabs(rotated_section_pt[1]) for rotated_section_pt in rotated_section_dict.values()])
        max_z = max([numpy.fabs(rotated_section_pt[2]) for rotated_section_pt in rotated_section_dict.values()])

        maxes = [max_x, max_y, max_z]

        # allMax = max(maxes)
        for i in self.plot_views:
            maxes[i] = maxes[i] + 0.2 * max([max_x, max_y, max_z])

        self.fig = pylab.figure(**self.fig_kwargs) #figsize=(7, 7))
        self.fig.subplots_adjust(
            left=0.05,
            top=0.95,
            right=0.95,
            bottom=0.05,
            wspace=0.15,
            hspace=0.15,
            )

        self.subplots = {}
        for i in self.plot_views:
            self.subplots[i] = self.build_draw_sub_plot(rotated_section_dict, self.fig, i, plot_lims)


from urllib.request import Request, urlopen
import json
req = Request('https://msesandbox.cisco.com/api/contextaware/v1/maps/info/DevNetCampus/DevNetBuilding/DevNetZone')
req.add_header('Authorization', 'Basic bGVhcm5pbmc6bGVhcm5pbmc==')
req.add_header('Accept', 'application/json')
response = urlopen(req)
responseString = response.read().decode("utf-8")
#print(responseString)
jsonObject = json.loads(responseString)
print(json.dumps(jsonObject, sort_keys=True, indent=4))
response.close()


class Solution:
    def isSelfCrossing(self, distance) -> bool:
        x, y = 0, 0
        d = [[0, 1], [-1, 0], [0, -1], [1, 0]]
        a = [[None, None, None] for i in range(6)]
        if len(distance) < 4:
            return False
        a[2] = [x, y, y + distance[0]]
        y += distance[0]
        a[1] = [y, x, x - distance[1]]
        x -= distance[1]
        a[0] = [x, y, y - distance[2]]
        y -= distance[2]
        for i, dis in enumerate(distance):
            if i < 3:
                continue
            for j in range(5, 0, -1):
                a[j] = a[j - 1]
            x_new, y_new = x + d[i % 4][0] * dis, y + d[i % 4][1] * dis
            a[0] = [y, x, x_new] if (i % 4) % 2 else [x, y, y_new]
            if (a[3][0] - a[0][1]) * (a[3][0] - a[0][2]) <= 0 and (a[0][0] - a[3][1]) * (a[0][0] - a[3][2]) <= 0:
                return True
            if a[4][0] is not None and a[0][0] == a[4][0] and (a[4][1] - a[0][1]) * (a[4][1] - a[0][2]) <= 0:
                return True
            if a[5][0] is not None and (a[0][0] - a[5][1]) * (a[0][0] - a[5][2]) <= 0 and (a[5][0] - a[0][1]) * (
                    a[5][0] - a[0][2]) <= 0:
                return True
            x, y = x_new, y_new
        return False

s = Solution()
print(s.isSelfCrossing([3, 3, 3, 2, 1, 1]))
print(s.isSelfCrossing([1, 2, 3, 4]))
print(s.isSelfCrossing([1, 1, 2, 2, 1, 1]))
print(s.isSelfCrossing([2, 1, 1, 2]))


import unittest
from unittest.mock import Mock

from pyats.topology import Device

from genie.metaparser.util.exceptions import SchemaEmptyParserError

from genie.libs.parser.iosxr.ping import Ping

class TestShowBfdSessionDestinationDetailss(unittest.TestCase):

    device = Device(name='aDevice')

    empty_device_output = {'execute.return_value': '''
      '''}

    expected_parsed_output = {
                  'ping': {
                     'address': '10.4.1.1',
                     'data_bytes': 100,
                     'repeat': 100,
                      'result_per_line': ['!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!',
                                          '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'],
                     'statistics': {
                        'received': 100,
                        'round-trip': {
                        'avg_ms': 14,
                        'max_ms': 2,
                        'min_ms': 1
                        },
                        'send': 100,
                        'success_rate_percent': 100.0
                     },
                     'timeout_secs': 2
                  }
               }

    device_output = {'execute.return_value': '''
      Type escape sequence to abort.
      Sending 100, 100-byte ICMP Echos to 10.4.1.1, timeout is 2 seconds:
      !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      Success rate is 100 percent (100/100), round-trip min/avg/max = 1/2/14 ms
    '''}

    @classmethod
    def setUpClass(cls):
        cls.maxDiff = None

    def test_ping_1(self):

        self.device = Mock(**self.device_output)
        obj = Ping(device=self.device)
        parsed_output = obj.parse(addr='10.4.1.1', source='10.4.1.2', count=100)
        self.assertEqual(parsed_output, self.expected_parsed_output)

    def test_show_bfd_destination_details_empty_output(self):

        self.device = Mock(**self.empty_device_output)
        obj = Ping(device=self.device)
        with self.assertRaises(SchemaEmptyParserError):
            parsed_output = obj.parse(addr=None)

if __name__ == '__main__':
    unittest.main()


from z3 import *
from itertools import product
from hjk.CEGIS import *
from operator import eq
import time

start = 0
end = 0
T = 60*3

def b2t_search(bmExpr, checker):
    funcDefine = ' '.join(['define-fun', checker.synFunction.name, '(', ' '.join(list(map(lambda l: '(' + ' '.join(l) + ')', checker.synFunction.argList))), ')', checker.synFunction.retSort])
    intSet = [[],[]]
    boolSet = [[],[]]
    i2iops = []
    b2bops = []
    i2bops = []
    b2iops = ['ite']
    exprs = [intSet, boolSet]
    ops = [i2iops, b2bops, i2bops, b2iops]

    progSize = 1

    for e in bmExpr[1][4][0][2][:-1]:
        if isinstance(e, tuple):
            intSet[1].append(e[1])
        elif isinstance(e, list):
            if e[0] in ['+', '-', '*', '/', 'mod']:
                i2iops.append(e[0])
            elif e[0] in ['<=', '>=', '<', '>', '=']:
                i2bops.append(e[0])
            elif e[0] in ['or', 'and']:
                b2bops.append(e[0])
        elif isinstance(e, str):
            intSet[1].append(e)

    for e in bmExpr[1][4][1][2]:
        if isinstance(e, tuple):
            intSet[1].append(e[1])
        elif isinstance(e, list):
            if e[0] in ['+', '-', '*', '/', 'mod']:
                i2iops.append(e[0])
            elif e[0] in ['<=', '>=', '<', '>', '=']:
                i2bops.append(e[0])
            elif e[0] in ['or', 'and']:
                b2bops.append(e[0])
        elif isinstance(e, str):
            intSet[1].append(e)

    while True:
        mc = MyChecker('', checker)
        c = 1
        for ie in intSet[progSize]:

            program = expr2prog(funcDefine, ie)
            mc.program = program
            #print(program)
            """
            (conflict, counterexample) = mc.check()
            print("%d %s" %(c, program))
            c += 1

            if not conflict:
                if counterexample == None:
                    print(program)
                    return
                else:
                    mc.pts.append(counterexample)
            """
            counterexample = checker.check(program)
            if counterexample is None:
                return program

            #print("%d %s" %(c, program))
            c += 1

        progSize += 1
        enum_exprs(exprs, progSize, ops)

        cut_branch(exprs, progSize)

    #print('Not found!!!')

def enum_exprs(exprs, progsize, ops):
    i2iops = ops[0]
    b2bops = ops[1]
    i2bops = ops[2]
    b2iops = ops[3]
    intSet = exprs[0]
    boolSet = exprs[1]
    intSet.append([])
    boolSet.append([])
    if progsize <= 1:
        for ps1 in range(1, progsize-1):
            ps2 = progsize-1-ps1
            intSet[progsize].extend(list(product(i2iops, intSet[ps1], intSet[ps2])))

    for ps1 in range(1, progsize-1):
        for ps2 in range(1, progsize-ps1-1):
            ps3 = progsize-1-ps1-ps2
            intSet[progsize].extend(list(product(b2iops, boolSet[ps1], intSet[ps2], intSet[ps3])))

    for ps1 in range(1, progsize-1):
        ps2 = progsize-1-ps1
        boolSet[progsize].extend(list(product(i2bops, intSet[ps1], intSet[ps2])))
    """
    for ps1 in range(1, progsize-1):
        ps2 = progsize-1-ps1
        boolSet[progsize].extend(list(product(b2bops, boolSet[ps1], boolSet[ps2])))
    """

def expr2prog(funcDefine, expr):
    return '(' + funcDefine + ' ' + muti2str(expr) + ')'

def muti2str(t):
    if not isinstance(t, tuple) and not isinstance(t, list):
        return str(t)
    return '('+' '.join(list(map(lambda x: muti2str(x), t)))+')'

def cut_branch(exprs, progSize):
    intSet = exprs[0][progSize]
    boolSet = exprs[1][progSize]

    intSet = list(filter(intfilter, intSet))

    boolSet = list(filter(boolfilter, boolSet))
    exprs[0][progSize] = intSet
    exprs[1][progSize] = boolSet

def intfilter(ie):
    if not isinstance(ie, list):
        return True
    op = ie[0]
    if op == 'ite':
        if eq(ie[2], ie[3]):
            return False
    return True

def boolfilter(be):
    if isinstance(be[1], int) or isinstance(be[2], int):
        return False
    if eq(be[1], be[2]):
        return False
    return True


#
#  Module: strips
#
#  This module provides classes for representing STRIPS-based
#  planning domains:
#     Predicate - used to represent conditions in states and operators
#     Operator  - used to represent STRIPS operators
#     STRIPS - a "SearchDomain" for planning with STRIPS operators
#
#  (c) Luis Seabra Lopes
#  Inteligência Artificial & Introducao a Inteligencia Artificial, 2019
#

import sys
from tree_search import *
from functools import reduce
from itertools import product
import math

comp_menor = lambda x,y: True if x<y else False

def sort(list, sorted_list):
    pos=0
    menor_pos=0
    menor = list[0]
    if len(list)>2:
        for value in list[1:]: #skips the first element cause its already stored
            pos+=1
            if  not comp_menor(menor, value):
                menor_pos=pos
                menor = value
        sorted_list.append(menor)
        list.pop(menor_pos)
        return(sort(list, sorted_list))
    elif len(list)==2:
        if comp_menor(list[0], list[1]):
            sorted_list.append(list[0])
            sorted_list.append(list[1])
        else:
            sorted_list.append(list[1])
            sorted_list.append(list[0])
        return sorted_list
    else:
        return list

def menor(lista):
    sorted_list=[]
    return sort(lista, sorted_list)[0]

def second_menor(lista):
    sorted_list=[]
    return sort(lista, sorted_list)[1]

def distance(p1, p2):
    return math.sqrt( ((p1[0]-p2[0])**2)+((p1[1]-p2[1])**2) )

# Predicates used to describe states, preconditions and effects
class Predicate:
    def __str__(self):
        argsstr = args2string(self.args)
        return type(self).__name__ + "(" + argsstr + ")"
    def __repr__(self):
        return str(self)
    def __eq__(self,predicate):   # allows for comparisons with "==", etc.
        return str(self)==str(predicate)

    def __hash__(self):
       return hash(str(self))

    def substitute(self,assign): # Substitute the arguments in a predicate by constants according to a given assignment (i.e. a dictionary)
        la = self.args
        return type(self)(assign[la[0]])

# STRIPS operators
# -- operators for a specific domain will be subclasses
# -- concrete actions will be instances of specific operators
class Operator:

    def __init__(self,args,pc,neg,pos):
        self.args = args
        self.pc  = pc
        self.neg = neg
        self.pos = pos
    def __str__(self):
        return type(self).__name__ + '(' + args2string(self.args) + ')'
    def __repr__(self):
        argsstr = args2string(self.args)
        return type(self).__name__ + "(" + argsstr + ")"

    # Produce a concrete action by instanciating a specific
    # operator (i.e. the "Operator" subclass where the method was
    # called) for the arguments given in "args"
    # ( returns None if the action is not applicable in the given "state" )
    @classmethod
    def instanciate(cls,args):
        if len(args)!=len(cls.args):
            return None
        assign = dict(zip(cls.args, args))
        pc  = [ p.substitute(assign) for p in cls.pc ]
        neg = [ p.substitute(assign) for p in cls.neg ]
        pos = [ p.substitute(assign) for p in cls.pos ]
        return cls(args,pc,neg,pos)

# Search domains based on STRIPS actions
class STRIPS(SearchDomain):

    # constructor
    def __init__(self):
        pass

    # list of applicable actions in a given "state"
    def actions(self, state):
        keeper_pos = get_keeper_pos(state)[0]
        constants = state_constants(state)
        operators = Operator.__subclasses__()
        actions = []
        for op in operators:
            #A linha seguinte remove de lassign todos os predicados onde se verifiquem as seguintes condições
            #X não foi mapeado para uma posição que é a do keeper
            #Y foi mapeado para uma posição que não é contígua a X
            #Z foi mapeado para uma posição que não é contígua a Y e a 2 blocos de X
            lassign = [ assign for assign in assignments(op.args,constants, keeper_pos) if assign['X']==keeper_pos and distance(assign['Y'], keeper_pos)==1 and ('Z' not in list(assign.keys()) or (distance(assign['Z'], keeper_pos) == 2 and distance(assign['Z'], assign['Y'])==1 ))]

            for assign in lassign:
                argvalues = [assign[a] for a in op.args]
                action = op.instanciate(argvalues)
                if all(c in state for c in action.pc):
                    #print(action)
                    actions.append(action)
        #print(actions)
        return actions

    # Result of a given "action" in a given "state"
    # ( returns None, if the action is not applicable in the state)
    def result(self, state, action):
        #efeitos negativos
        newstate = [p for p in state if p not in action.neg] #Apaga do estado anterior (ou não adiciona ao novo estado, depende de como quisermos ver) as propriedades que deixam de ser verdade ao executar a ação
        #efeitos positivos
        newstate.extend(action.pos)#Adiciona aos estado atual as propriedades que passam a ser verdade ao executar a ação
        return set(newstate)

    def cost(self, state, action):
        if type(action).__name__ == 'Move_Keeper':
            return 2
        return 1

    def heuristic(self, state, goal):
        #return -1*len([p for p in goal if p in state])
        #return len([p for p in goal if p in state])
        #return 0

        distances=[]
        box_distances = []
        keeper_distances = []
        boxes_pos= [p.args for p in state if type(p).__name__ == 'Box_On' or type(p).__name__ == 'Box_On_Goal'] #todas as caixas
        box_not_on_goal_pos = [p.args for p in state if type(p).__name__ == 'Box_On' ] #todas as caixas not on goal
        #print(box_not_on_goal_pos)
        box_on_goal_pos = [p.args for p in state if type(p).__name__ == 'Box_On_Goal'] #caixas no goal
        print(box_on_goal_pos)
        goals_pos=[pos.args for pos in goal] #todos os goals
        free_goals_pos=[pos for pos in goals_pos if pos not in boxes_pos] #goals que não têm caixas
        keeper_distance_to_every_box=[]
        shortest_distance_to_box=10000
        #Calcula menor distância do keeper a uma caixa
        for pos in boxes_pos:
            shortest_distance_to_box=min([shortest_distance_to_box, distance(get_keeper_pos(state)[0], pos)])
        #calcula a distância mínima de cada caixa a um goal livre
        for pos in box_not_on_goal_pos:
            box_distances_to_every_goal = []
            for goal in free_goals_pos:
                box_distances_to_every_goal.append(distance(pos, goal))
            try:
                box_distances.append(menor(box_distances_to_every_goal))
            except IndexError:
                pass

        distances.append(shortest_distance_to_box)
        distances[0:0]=box_distances[:]
        soma = sum(distances)
        #print(soma)
        return soma
        """

        goals_pos=[pos.args for pos in goal] #todos os goals
        boxes_pos= [p.args for p in state if type(p).__name__ == 'Box_On' or type(p).__name__ == 'Box_On_Goal'] #todas as caixas
        unplaced_boxes = [p.args for p in state if type(p).__name__ == 'Box_On' ] #todas as caixas not on goal
        unfilled_goals = [pos for pos in goals_pos if pos not in boxes_pos] #goals que não têm caixas

        if not unplaced_boxes:
            return 0
        if not unfilled_goals:
            return 0

        player_dist = min([distance(get_keeper_pos(state)[0], box) for box in unplaced_boxes])
        box_distances = sum([min([distance(box, goal) for goal in unfilled_goals]) for box in unplaced_boxes])

        return player_dist + box_distances
        """

    # Checks if a given "goal" is satisfied in a given "state"
    def satisfies(self, state, goal):
        return all(p in state for p in goal)

# Auxiliary functions

def get_keeper_pos(state):
    return [pred.args for pred in state if type(pred).__name__ == 'Keeper_On_Box_Allowed' or type(pred).__name__ == 'Keeper_On_Box_Not_Allowed' ]
def state_constants(state):
    return [pred.args for pred in state]

def assignments(lvars,lconsts, keeper_pos):
    lcombs = product(lconsts,repeat=len(lvars))

    makeassign = lambda comb : dict(zip(lvars,comb))
    return list(map(makeassign,lcombs))

def args2string(args):
    if args == []:
        return ""
    return str(args)


from django.urls import path
from .views import EmpleadoCrear, EmpleadoDelete, EmpleadoEdit, EmpleadoList, GastosList, RubroGastoList,RubroGastoEdit,RubroGastoCrear,RubroGastoDelete, RubroVentaEdit,RubroVentaList,RubroVentaCrear,RubroVentaDelete,SubrubroGastoList,SubrubroGastoCrear,SubrubroGastoEdit,SubrubroGastoDelete, UmedidaList,UmedidaCrear,UmedidaEdit,UmedidaDelete,ClienteList,ClienteCrear,ClienteEdit,ClienteDelete,ProveedorList,ProveedorCrear,ProveedorEdit,ProveedorDelete,GastosList,GastoCrear,GastoEdit,GastoDelete,listado_gastos

app_name = 'economico'

urlpatterns = [
    path('rubro/gastos/', RubroGastoList.as_view(), name='rubro-gasto'),
    path('rubro/gastos/crear/', RubroGastoCrear.as_view(), name='rubro-gasto-crear'),
    path('rubro/gastos/editar/<int:pk>/', RubroGastoEdit.as_view(), name='rubro-gasto-edit'),
    path('rubro/gastos/borrar/<int:pk>/', RubroGastoDelete.as_view(), name='rubro-gasto-delete'),

    path('subrubro/gastos/', SubrubroGastoList.as_view(), name='subrubro-gasto'),
    path('subrubro/gastos/crear/', SubrubroGastoCrear.as_view(), name='subrubro-gasto-crear'),
    path('subrubro/gastos/editar/<int:pk>/', SubrubroGastoEdit.as_view(), name='subrubro-gasto-edit'),
    path('subrubro/gastos/borrar/<int:pk>/', SubrubroGastoDelete.as_view(), name='subrubro-gasto-delete'),

    path('rubro/ventas/', RubroVentaList.as_view(), name='rubro-venta'),
    path('rubro/ventas/crear/', RubroVentaCrear.as_view(), name='rubro-venta-crear'),
    path('rubro/ventas/editar/<int:pk>/', RubroVentaEdit.as_view(), name='rubro-venta-edit'),
    path('rubro/ventas/borrar/<int:pk>/', RubroVentaDelete.as_view(), name='rubro-venta-delete'),

    path('datos/u_medida/', UmedidaList.as_view(), name='dato-umedida'),
    path('datos/u_medida/crear/', UmedidaCrear.as_view(), name='dato-umedida-crear'),
    path('datos/u_medida/editar/<int:pk>/', UmedidaEdit.as_view(), name='dato-umedida-edit'),
    path('datos/u_medida/borrar/<int:pk>/', UmedidaDelete.as_view(), name='dato-umedida-delete'),

    path('datos/empleados/', EmpleadoList, name='dato-empleado'),
    path('datos/empleados/crear/', EmpleadoCrear.as_view(), name='dato-empleado-crear'),
    path('datos/empleados/editar/<int:pk>/', EmpleadoEdit.as_view(), name='dato-empleado-edit'),
    path('datos/empleados/borrar/<int:pk>/', EmpleadoDelete.as_view(), name='dato-empleado-delete'),

    path('datos/clientes/', ClienteList, name='dato-cliente'),
    path('datos/clientes/crear/', ClienteCrear.as_view(), name='dato-cliente-crear'),
    path('datos/clientes/editar/<int:pk>/', ClienteEdit.as_view(), name='dato-cliente-edit'),
    path('datos/clientes/borrar/<int:pk>/', ClienteDelete.as_view(), name='dato-cliente-delete'),

    path('datos/proveedores/', ProveedorList, name='dato-proveedor'),
    path('datos/proveedores/crear/', ProveedorCrear.as_view(), name='dato-proveedor-crear'),
    path('datos/proveedores/editar/<int:pk>/', ProveedorEdit.as_view(), name='dato-proveedor-edit'),
    path('datos/proveedores/borrar/<int:pk>/', ProveedorDelete.as_view(), name='dato-proveedor-delete'),

    path('gastos/', GastosList, name='gasto'),
    path('listado/gastos', listado_gastos, name='listado-gastos'),
    path('gastos/nuevo/', GastoCrear.as_view(), name='gasto-crear'),
    path('gastos/editar/<int:pk>/', GastoEdit.as_view(), name='gasto-edit'),
    path('gastos/eliminar/<int:pk>/', GastoDelete.as_view(), name='gasto-delete'),
]


#Para ejecutar python APY.py extension_audio user
#Para ejecutar python APY.py wav Issac
import csv
import os
import sys
import datetime
import pandas as pd
from glob import glob
from os import system
from pydub import AudioSegment
from pydub.playback import play
from inaSpeechSegmenter import Segmenter

def getFolders(name):
    folders = []
    for x in glob(name):
        folders.append(x)
    return folders

def WriteResult(data,user):
    df = pd.DataFrame(data, columns = ['Audio','Genero','Edad','Region'])
    dt = datetime.datetime.now()
    filename = user+'_Reporte_'+str(dt)
    df.to_csv(filename+'.csv', index = None, header=True)
    df.to_json(filename +'.json',orient='records')
    system("clear")
    print(df)

def Gender(folders,ext,seg):
    list_result = []
    for folder in folders:
        name = folder.split("/")[-1]
        audio = folder + "/individuales/" + name + "01"+ext
        audio = folder
        result = seg(audio)
        list_result.append(result[0][0])
    return list_result

def Range_old(folders,ext):
    list_result = []
    for folder in folders:
        name = folder.split("/")[-1]
        audio = folder + "/comunes/" + name + "01"+ext
        audio = folder
        sound = AudioSegment.from_wav(audio)
        system("clear")
        play(sound)
        print("Rango de edades")
        print("a) 19-21")
        print("b) 22-24")
        print("c) 25-28")
        print("d) 29-32")
        print("e) 33-36")
        print("f) 37-40")
        print("g) 41-44")
        print("h) 45-48")
        print("i) 49-51")
        edad = input("Ingrese rango:")
        list_result.append(edad)
    return list_result

def Reporte(folders,G,E,R):
    Rango = {"a": "19-21","b":"22-24","c":"25-28","d": "29-32",
        "e":"33-36","f":"37-40","g":"41-44","h":"45-48","i":"49-51","":"-"}
    Generos = {"male":"Hombre","female":"Mujer"}
    list_result = []
    i = 0
    for folder in folders:
        name = folder.split("/")[-1]
        try:
            Genero = Generos[G[i]]
        except:
            Genero = '-'
        try:
            Edad = Rango[E[i]]
        except:
            Edad = '-'
        list_result.append([name,Genero,Edad,R])
        i = i + 1

    return list_result
def main():
    list_result = []
    seg = Segmenter(detect_gender= True)
    ext = "."+str(sys.argv[1])
    user =  str(sys.argv[2])
    Path = os.getcwd()+"/CorpusMe/*"
    folders = getFolders(Path)
    folders = sorted(folders)
    Edades = Range_old(folders, ext)
    Generos = Gender(folders, ext,seg)
    data = Reporte(folders,Generos,Edades,'Mexico')
    WriteResult(data,user)

main()


def setup():
    size(500, 500)
    smooth()
    background(255)
    strokeWeight(1)

counter1 = 0
counter = 0
cx = cy = 250
cRadius = 100

def draw():

    global counter1, counter, cx, cy, cRadius
    stroke(0, 30)
    nx = sin(counter1)*cRadius + cx
    ny = cos(counter1)*cRadius + cy

    x1 = nx - sin(counter)*350
    y1 = ny - cos(counter)*350
    x2 = nx + sin(counter)*350
    y2 = ny + cos(counter)*350
    bezier(x1, y1, x1 + 100, y1 - 100, x2 - 100, y2 + 100, x2, y2)

    counter += 0.1
    if counter > 2*PI:
        counter = 0

    counter1 = counter1 + mouseX/float((width*2))
    if counter1 > 2*PI:
        counter1 = 0

def keyPressed():
    if key== "s": saveFrame("myProcessing.png")