'''
Цель урока: Настроить управление, добавить возможность установки бомбы и настроить ее взаимодействие с другими объектами.
- Повторение
- Проверка домашнего задания
- Настроить управление Бомбермэном
- Настроить взаимодействие Бомбермэна со стенами
- Создать бомбы
- Добавить огонь от бомбы
- Создать финал игры
- Добавить бонусы (дополнительное задание)
- Сделать выводы к уроку
- Получить домашнее задание

'''

import arcade
import random
import animate
import time

# задаем ширину, высоту и заголовок окна

SCREEN_TITLE = "Bomberman"
CELL_WIDTH = 60
CELL_HEIGHT = 60
ROW_COUNT = 11
COLUMN_COUNT = 11
SCREEN_WIDTH = CELL_WIDTH * COLUMN_COUNT
SCREEN_HEIGHT = CELL_HEIGHT * ROW_COUNT

PLAYER1_SPEED = 10
PLAYER2_SPEED = 10

PLAYER1_BOMB_COUNT = 1
PLAYER2_BOMB_COUNT = 1

PLAYER1_POWER = 3
PLAYER2_POWER = 3

def difference(coordinate, distance):
    return coordinate * distance + distance/2

def justify_x(position_x):
    for x in range(COLUMN_COUNT):
        cell_center_x = difference(x, CELL_WIDTH)
        if position_x - cell_center_x <= CELL_WIDTH / 2:
            return cell_center_x

def justify_y(position_y):
    for y in range(ROW_COUNT):
        cell_center_y = difference(y, CELL_HEIGHT)
        if position_y - cell_center_y <= CELL_HEIGHT / 2:
            return cell_center_y

def bonus_add(image_name, spritelist, block):
    bonus = Bonus(image_name, 1)
    bonus.center_x = block.center_x
    bonus.center_y = block.center_y
    spritelist.append(bonus)

class Bomb(animate.Animate):
    def __init__(self):
        super().__init__('Bomb/Bomb_f00.png',0.7)
        for i in range(3):
            self.append_texture(arcade.load_texture(f'Bomb/Bomb_f0{i}.png'))
        self.bomb_timer = time.time()
        self.power = 3

    def update(self):
        if time.time() - self.bomb_timer >3:
            exp = Explosion()
            exp.center_x = self.center_x
            exp.center_y = self.center_y
            window.explosions.append(exp)
            left = True
            right = True
            top = True
            bottom = True
            self.kill()
            for i in range(1,  self.power+1):
                if left:
                    exp1 = Explosion()
                    exp1.center_x = exp.center_x - CELL_WIDTH * i
                    exp1.center_y = exp.center_y
                    window.explosions.append(exp1)
                    if exp1.check():
                        left = False
                if right:
                    exp2 = Explosion()
                    exp2.center_x = exp.center_x + CELL_WIDTH * i
                    exp2.center_y = exp.center_y
                    window.explosions.append(exp2)
                    if exp2.check():
                        right = False
                if bottom:
                    exp3 = Explosion()
                    exp3.center_x = exp.center_x
                    exp3.center_y = exp.center_y - CELL_HEIGHT * i
                    window.explosions.append(exp3)
                    if exp3.check():
                        bottom = False
                if top:
                    exp4 = Explosion()
                    exp4.center_x = exp.center_x
                    exp4.center_y = exp.center_y + CELL_HEIGHT * i
                    window.explosions.append(exp4)
                    if exp4.check():
                        top = False

class Bomberman(animate.Animate):
    def __init__(self, speed, bomb_count, power):
        super().__init__("Bomberman/Front/Bman_F_f00.png",0.5)
        # Front
        self.walk_down_frames = []
        for i in range(8):
            self.walk_down_frames.append(arcade.load_texture(f"Bomberman/Front/Bman_F_f0{i}.png"))
        # Back
        self.walk_up_frames = []
        for i in range(8):
            self.walk_up_frames.append(arcade.load_texture(f"Bomberman/Back/Bman_B_f0{i}.png"))
        # Right
        self.walk_right_frames = []
        for i in range(8):
            self.walk_right_frames.append(arcade.load_texture(f"Bomberman/Side/Bman_S_f0{i}.png"))
        # Left
        self.walk_left_frames = []
        for i in range(8):
            self.walk_left_frames.append(arcade.load_texture(f"Bomberman/Side/Bman_S_f0{i}.png",
            flipped_horizontally = True)
            )
        self.direction = 4
        self.motion = 0
        self.speed = speed
        self.bomb_count = bomb_count
        self.power = power
        self.win = False

    def costume_change(self):
        if self.direction == 1:
            self.textures = self.walk_left_frames
        elif self.direction == 2:
            self.textures = self.walk_right_frames
        elif self.direction == 3:
            self.textures = self.walk_up_frames
        elif self.direction == 4:
            self.textures = self.walk_down_frames

    def update(self):
        self.center_x += self.change_x
        self.center_y += self.change_y
        if self.left < 0:
            self.left = 0
        if self.right > SCREEN_WIDTH:
            self.right = SCREEN_WIDTH
        if self.bottom < 0:
            self.bottom = 0
        if self.top > SCREEN_HEIGHT:
            self.top = SCREEN_HEIGHT
        self.collisions(window.solid_blocks)
        self.collisions(window.explodable_blocks)

    def to_up(self):
        if not self.motion:
            self.motion = 1
            self.direction = 3
            self.change_y = self.speed

    def to_down(self):
        if not self.motion:
            self.motion = 1
            self.direction = 4
            self.change_y = -self.speed

    def to_left(self):
        if not self.motion:
            self.motion = 1
            self.direction = 1
            self.change_x = -self.speed

    def to_right(self):
        if not self.motion:
            self.motion = 1
            self.direction = 2
            self.change_x = self.speed

    def to_stop(self):
        self.change_x = 0
        self.change_y = 0
        self.direction = 0
        self.motion = 0

    def collisions(self, spritelist):
        block_hit = arcade.check_for_collision_with_list(
        self,
        spritelist
        )
        if len(block_hit) > 0:
            for block in block_hit:
                if self.direction == 3 and self.top >= block.bottom:
                    self.top = block.bottom
                elif self.direction == 4 and self.bottom <= block.top:
                    self.bottom = block.top
                elif self.direction == 2 and self.right >= block.left:
                    self.right = block.left
                elif self.direction == 1 and self.left <= block.right:
                    self.left = block.right
        bombs_up = arcade.check_for_collision_with_list(
            self,
            window.bomb_power_up
        )
        flame_up = arcade.check_for_collision_with_list(
            self,
            window.flame_power_up
        )
        speed_up = arcade.check_for_collision_with_list(
            self,
            window.speed_power_up
        )
        if len(bombs_up) > 0:
            self.bomb_count += 1
            for bonus in bombs_up:
                bonus.kill()
        if len(flame_up) > 0:
            self.power += 1
            for bonus in flame_up:
                bonus.kill()
        if len(speed_up) > 0:
            self.speed += 1
            for bonus in speed_up:
                bonus.kill()

class SolidBlock(arcade.Sprite):
    def __init__(self):
        super().__init__('Blocks/SolidBlock.png', 1)

class ExplodableBlock(arcade.Sprite):
    def __init__(self):
        super().__init__('Blocks/ExplodableBlock.png', 1)

class Bonus(arcade.Sprite):
    def __init__(self, image, scale):
        super().__init__(image, scale)

class Explosion(animate.Animate):
    def __init__(self):
        super().__init__('Flame/Flame_f00.png', 0.7)
        for i in range(5):
            self.append_texture(arcade.load_texture(f'Flame/Flame_f0{i}.png'))
        self.explosion_timer = time.time()

    def update(self):
        if time.time() - self.explosion_timer > 2:
            self.kill()

    def check(self):
        hits = arcade.check_for_collision_with_list(
            self,
            window.solid_blocks
        )
        return len(hits) > 0

class Game(arcade.Window):
    def __init__(self, width, height, title):
        super().__init__(width, height, title)
        #текстуры
        self.bg = arcade.load_texture('Blocks/BackgroundTile.png')
        self.win1 = arcade.load_texture('win/win1.png')
        self.win2 = arcade.load_texture('win/win2.png')
        #спрайтлисты
        self.solid_blocks = arcade.SpriteList()
        self.explodable_blocks = arcade.SpriteList()
        self.bombs_player1 = arcade.SpriteList()
        self.bombs_player2 = arcade.SpriteList()
        self.explosions = arcade.SpriteList()
        self.bomb_power_up = arcade.SpriteList()
        self.flame_power_up = arcade.SpriteList()
        self.speed_power_up = arcade.SpriteList()
        #спрайты
        self.bomberman = Bomberman(PLAYER1_SPEED, PLAYER1_BOMB_COUNT, PLAYER1_POWER)
        self.bomberman2 = Bomberman(PLAYER2_SPEED, PLAYER2_BOMB_COUNT, PLAYER2_POWER)
        #поля
        self.game = True
        #инициализация спрайтов
        self.setup()

    def setup(self):
        for y in range(ROW_COUNT):
            for x in range(COLUMN_COUNT):
                if x % 2 == 1 and y % 2 == 1:
                    solid_block = SolidBlock()
                    solid_block.center_x = difference(x, CELL_WIDTH)
                    solid_block.center_y = difference(y, CELL_HEIGHT)
                    self.solid_blocks.append(solid_block)
                elif random.randint(1, 2) == 1:
                    if (not (x == 0 and y <= 2)
                    and not (y == 0 and x <= 2)
                    and not (x == ROW_COUNT - 1 and y >= COLUMN_COUNT - 3)
                    and not (y == COLUMN_COUNT - 1 and x >= ROW_COUNT - 3)):
                        exp_block = ExplodableBlock()
                        exp_block.center_x = difference(x, CELL_WIDTH)
                        exp_block.center_y = difference(y, CELL_WIDTH)
                        self.explodable_blocks.append(exp_block)
                        bonus_place = random.randint(1, 6)
                        if bonus_place == 1:
                            bonus_add(
                                'Powerups/BombPowerup.png',
                                self.bomb_power_up,
                                exp_block
                            )
                        elif bonus_place == 2:
                            bonus_add(
                                'Powerups/SpeedPowerup.png',
                                self.speed_power_up,
                                exp_block
                            )
                        elif bonus_place == 3:
                            bonus_add(
                                'Powerups/FlamePowerup.png',
                                self.flame_power_up,
                                exp_block
                            )

        x = SCREEN_WIDTH / COLUMN_COUNT - CELL_WIDTH / 2
        y = SCREEN_HEIGHT / ROW_COUNT - CELL_HEIGHT / 2
        self.bomberman.set_position(x, y)
        x2 = SCREEN_HEIGHT - CELL_HEIGHT / 2
        y2 = SCREEN_WIDTH - CELL_WIDTH / 2
        self.bomberman2.set_position(x2, y2)
        self.bomberman2.color = (255,0,0)

    def draw_background(self):
        for y in range(ROW_COUNT):
            for x in range(COLUMN_COUNT):
                arcade.draw_texture_rectangle(
                   difference(x, CELL_WIDTH),
                    difference(y, CELL_WIDTH),
                    CELL_WIDTH, CELL_HEIGHT, self.bg
                )

    def on_draw(self):
        self.clear()
        self.draw_background()
        self.solid_blocks.draw()
        self.bomb_power_up.draw()
        self.flame_power_up.draw()
        self.speed_power_up.draw()
        self.explodable_blocks.draw()
        self.bombs_player1.draw()
        self.bombs_player2.draw()
        self.bomberman.draw()
        self.bomberman2.draw()
        self.explosions.draw()

        if self.bomberman.win:
            arcade.draw_texture_rectangle(
                SCREEN_WIDTH / 2,
                SCREEN_HEIGHT / 2,
                SCREEN_WIDTH,
                SCREEN_HEIGHT,
                self.win1
            )
            self.game = False
        if self.bomberman2.win:
            arcade.draw_texture_rectangle(
                SCREEN_WIDTH / 2,
                SCREEN_HEIGHT / 2,
                SCREEN_WIDTH,
                SCREEN_HEIGHT,
                self.win2
            )
            self.game = False

    def update(self, delta_time):
        if self.game:
            self.bomberman.update_animation(delta_time)
            self.bomberman2.update_animation(delta_time)
            self.bomberman.update()
            self.bomberman2.update()
            self.bombs_player1.update()
            self.bombs_player1.update_animation(delta_time)
            self.bombs_player2.update()
            self.bombs_player2.update_animation(delta_time)
            self.explosions.update()
            self.explosions.update_animation()
            for flame in self.explosions:
                explosions = arcade.check_for_collision_with_list(
                    flame,
                    self.explodable_blocks)
                if len(explosions) > 0:
                    for block in explosions:
                        block.kill()
                hit_list = arcade.check_for_collision_with_list(
                        flame,
                        self.solid_blocks)
                if len(hit_list) > 0:
                    flame.kill()
                if arcade.check_for_collision(flame, self.bomberman):
                    self.bomberman.color = (0,0,0)
                    self.bomberman2.win = True
                if arcade.check_for_collision(flame, self.bomberman2):
                    self.bomberman2.color = (0,0,0)
                    self.bomberman.win = True

    def on_key_press(self, key, modifiers):
        if self.game:
            if key == arcade.key.LEFT:
                self.bomberman.to_left()
            elif key == arcade.key.RIGHT:
                self.bomberman.to_right()
            elif key == arcade.key.UP:
                self.bomberman.to_up()
            elif key == arcade.key.DOWN:
                self.bomberman.to_down()
            self.bomberman.costume_change()
            if key == arcade.key.SPACE:
                if len(self.bombs_player1) < self.bomberman.bomb_count:
                    bomb = Bomb()
                    bomb.center_x = justify_x(self.bomberman.center_x)
                    bomb.center_y = justify_y(self.bomberman.center_y)
                    bomb.power = self.bomberman.power
                    self.bombs_player1.append(bomb)

            if key == arcade.key.A:
                self.bomberman2.to_left()
            elif key == arcade.key.D:
                self.bomberman2.to_right()
            elif key == arcade.key.W:
                self.bomberman2.to_up()
            elif key == arcade.key.S:
                self.bomberman2.to_down()
            self.bomberman2.costume_change()
            if key == arcade.key.E:
                if len(self.bombs_player2) < self.bomberman2.bomb_count:
                    bomb = Bomb()
                    bomb.center_x = justify_x(self.bomberman2.center_x)
                    bomb.center_y = justify_y(self.bomberman2.center_y)
                    bomb.power = self.bomberman2.power
                    self.bombs_player2.append(bomb)

    def on_key_release(self, key, modifiers):
        if key == arcade.key.LEFT or key == arcade.key.RIGHT or key == arcade.key.UP or key == arcade.key.DOWN:
            self.bomberman.to_stop()
        if key == arcade.key.A or key == arcade.key.D or key == arcade.key.W or key == arcade.key.S:
            self.bomberman2.to_stop()

window = Game(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
arcade.run()


"""
-*- coding: UTF-8 -*-
@Time : 2023/3/21  19:10
@Description :
@Author : Huizhi XU
"""

import configparser
import os.path

# Open a configuration file
config = configparser.ConfigParser()
file_path = os.path.dirname(os.path.abspath('.')) + 'config.ini' #返回上级目录再访问某文件（路径根据实际情况自定义）
config.read(file_path)
open_ai_key = str(config.get("open_ai", "OPENAI_API_KEY"))
serapi_key = str(config.get("serp_api", "SERPAPI_API_KEY"))


#!/usr/bin/env python3
import hashlib
import json
import os.path
import threading
import time
import webbrowser
from tkinter.filedialog import askdirectory, asksaveasfilename
from tkinter.messagebox import askokcancel, showerror, showinfo, askyesno

import _tkinter
import pyperclip
from ttkbootstrap import *
from ttkbootstrap import Treeview
from ttkbootstrap.tooltip import ToolTip
from ttkbootstrap.window import Window

from Columns import columns_settings
from Dragable import make_draggable
from FDType import GetType
from Permissions import Permission
from TableMarkers import marks
from Table_Rearrange import Rearrange
from extensions import extensions
from images import ant_logo_data_medium, ant_logo_data_big, github_logo_data, instagram_logo_data
from list2csv import pylist2csv
from utils import File

class ab:
    name = "Ants Blueprint"
    version = "2.0.0"

def AntsBlueprint():
    ants_blueprint = Window(themename='darkly')  # (cerculean, morph, darkly, superhero)
    default_title = f"{ab.name}"  # | {ab.version}"
    ants_blueprint.title(default_title)
    height = ants_blueprint.winfo_screenheight()
    width = ants_blueprint.winfo_screenwidth()
    ants_blueprint.geometry(
        "%sx%s+%s+%s" % (width // 2, height // 2, width // 4, height // 5))  # width // 2, height // 2,

    icon = PhotoImage(data=ant_logo_data_big)
    ants_blueprint.call('wm', 'iconphoto', ants_blueprint._w, icon)

    ab_menu = Menu(ants_blueprint, tearoff=False)

    style = ttk.Style(ants_blueprint)
    # style.configure("TNotebook", tabposition="sw")

    Jobs_manager = Notebook(ants_blueprint, takefocus=False, padding=3)
    Jobs_manager.pack(fill='both', expand=True)

    Jobs_manager.add(Frame(), text='➕ 0')
    Jobs_manager.tab(0, state='disabled')

    Instractions_Frame = Frame()
    Instractions_Frame.place(relx=0.5, rely=0.5, anchor="center")

    Texts_frame = Frame(Instractions_Frame)
    Texts_frame.pack(expand=True)

    ant_logo_photo_object = PhotoImage(data=ant_logo_data_medium)
    Label(Texts_frame, image=ant_logo_photo_object).pack(expand=True, pady=15)

    Label(Texts_frame, text='Ants Blueprint', foreground="#2b73fe", font=("Cascadia Mono ExtraLight", 25)).pack(
        expand=True)

    Label(Instractions_Frame, text='All right reserved to Virus (C) forever').pack(side='bottom')
    Label(Instractions_Frame, foreground="#343432", text='2.0.0v').pack(side='bottom')

    Social_media_frame = Frame(Instractions_Frame)
    Social_media_frame.pack(side='bottom', pady=25)
    ###
    ###
    instagram_logo = PhotoImage(data=instagram_logo_data)
    Instargam = Label(Social_media_frame, image=instagram_logo, cursor='hand2')
    Instargam.bind('<Button-1>', lambda a: webbrowser.open('https://instagram.com/secvirus'))
    Instargam.pack(side='left', padx=10)
    ###
    ###
    github_logo = PhotoImage(data=github_logo_data)
    Github = Label(Social_media_frame, image=github_logo, cursor='hand2')
    Github.bind('<Button-1>', lambda a: webbrowser.open('https://github.com/isecvirus'))
    Github.pack(side='left', padx=10)

    def Job():
        Unattached = {}  # {<item-id>: <index>}
        Marked = {}
        directories_found = IntVar(value=0)
        files_found = IntVar(value=0)
        estimated = StringVar(value="0.000")
        Paths_dict = {}

        job_frame = Frame(ants_blueprint)
        # anchor: n, ne, e, se, s, sw, w, nw, or center

        headers = tuple([str(c) for c in columns_settings.keys()])
        header_sorting = {}.fromkeys(headers, "ascending")

        settings_frame = Frame(job_frame)
        settings_frame.pack(side='top', fill='x', pady=3, padx=3)

        isBrowse = BooleanVar(value=False)
        Browse = Checkbutton(settings_frame, text="Browse", variable=isBrowse, bootstyle="success-toolbutton",
                             cursor='hand2', takefocus=False)
        ToolTip(Browse, "Allow browsing table data while operation is running")
        Browse.pack(side='left', padx=1)

        isFollowLinks = BooleanVar(value=True)
        FollowLinks = Checkbutton(settings_frame, text="Follow Links", variable=isFollowLinks,
                                  bootstyle="success-toolbutton", cursor='hand2', takefocus=False)
        ToolTip(Browse, "Follow File/Directory (Hard Link)")
        FollowLinks.pack(side='left', padx=1)

        isDirectories = BooleanVar(value=True)
        Directories = Checkbutton(settings_frame, text="Directories", variable=isDirectories,
                                  bootstyle="success-toolbutton", cursor='hand2', takefocus=False)
        ToolTip(Browse, "Search for Directories?")
        Directories.pack(side='left', padx=1)

        isFiles = BooleanVar(value=True)
        Files = Checkbutton(settings_frame, text="Files", variable=isFiles, bootstyle="success-toolbutton",
                            cursor='hand2', takefocus=False)
        ToolTip(Browse, "Search for Files?")
        Files.pack(side='left', padx=1)

        statics_frame = Frame(settings_frame)
        statics_frame.pack(side='right', fill='x', expand=True)

        directories_found_lbl = Label(statics_frame, text="Directories: 0", anchor="center", justify="center")
        directories_found_lbl.pack(side='left', fill='x', expand=True)

        files_found_lbl = Label(statics_frame, text="Files: 0", anchor="center", justify="center")
        files_found_lbl.pack(side='left', fill='x', expand=True)

        total_found_lbl = Label(statics_frame, text="Total: 0", anchor="center", justify="center")
        total_found_lbl.pack(side='left', fill='x', expand=True)

        estimated_lbl = Label(statics_frame, text=f"Estimated: {estimated.get()}", anchor="center", justify="center")
        estimated_lbl.pack(side='left', fill='x', expand=True)

        table_frame = Frame(job_frame)
        table_frame.pack(side='top', fill='both', expand=True, padx=3, pady=3)
        table_SB_Y = Scrollbar(table_frame, orient="vertical", takefocus=True, bootstyle='rounded')

        table = Treeview(table_frame, columns=headers, show="headings", bootstyle='default',
                         yscrollcommand=table_SB_Y.set)
        table.pack(side='left', fill='both', expand=True)

        table_SB_Y.config(command=table.yview)
        table_SB_Y.pack(side='right', fill='y')

        def Statics_Update():
            estimated_lbl.config(text=f"Estimated: {estimated.get()}")
            directories_found_lbl.config(text=f"Directories: {directories_found.get()}")
            files_found_lbl.config(text=f"Files: {files_found.get()}")
            total_found_lbl.config(text=f"Total: {directories_found.get() + files_found.get()}")

        for name in headers:
            column_width = int(columns_settings[name]['width'])
            minWidth = int(columns_settings[name]['minwidth'])
            stretch = columns_settings[name]['stretch']
            anchor = columns_settings[name]['anchor']

            table.heading(name, text=name)
            table.column(name, width=column_width, minwidth=minWidth, anchor=anchor, stretch=stretch)

        def do_insert(object: str):
            index = (len(table.get_children()) + 1)
            fType = GetType(object=object)

            perm = Permission(object=object)
            fPermission = perm.ogo  # OWNER,GROUP,OTHERS
            fOwner = perm.Owner()
            fGroup = perm.Group()
            fOthers = perm.Others()

            file_obj = File(object)
            fSize = file_obj.file_size()
            fDevice = file_obj.Device()
            fCreation = file_obj.creation()
            fModify = file_obj.modified()
            fAccess = file_obj.access()
            fInode = file_obj.Inode()
            fHardLink = file_obj.Hard_Link()
            fUID = file_obj.UserId()
            fGID = file_obj.GroupId()

            table.insert(parent='', index='end', values=(
                index, object, fType, fSize, fPermission, fOwner, fGroup, fOthers, fDevice, fInode, fHardLink, fUID,
                fGID,
                fCreation, fModify, fAccess))
            Statics_Update()

            if not isBrowse.get():
                table.see(item=table.get_children()[-1])
                table.focus(item=table.get_children()[-1])

        bottom_frame = Frame(job_frame)
        bottom_frame.pack(side='bottom', fill='x')

        def InnerMapping_dir(root, dirs):
            for dirname in dirs:
                if not process.isRunning:
                    break
                dir = os.path.join(root, dirname).replace(os.sep * 2, os.sep)
                if os.path.exists(dir):
                    do_insert(dir)
                    directories_found.set(value=directories_found.get() + 1)
                Statics_Update()

        def InnerMapping_file(root, files):
            for filename in files:
                if not process.isRunning:
                    break
                file = os.path.join(root, filename).replace(os.sep * 2, os.sep)
                if os.path.exists(file):
                    do_insert(file)
                    files_found.set(value=files_found.get() + 1)
                Statics_Update()

        class process:
            def stop(self):
                process.isRunning = False
                Scan_Btn.config(text='Scan', bootstyle='default',
                                command=lambda: threading.Thread(target=scan_ab).start())

            isRunning = False

        process = process()

        def scan_ab():  # ab=Ants Blueprint
            if process.isRunning == False:
                if len(str(Path_entry.get())) > 0:
                    confirm = askokcancel(title='Scan', message='Start scanning operation?')
                    if confirm:
                        path = Path_entry.get()  # .replace(os.sep*2, os.sep)
                        if os.path.exists(path):
                            directories_found.set(value=0)
                            files_found.set(value=0)
                            Unattached.clear()
                            table.delete(*table.get_children())
                            process.isRunning = True
                            Scan_Btn.config(command=lambda: process.stop(), text='Stop', bootstyle='danger')
                            FollowLinks.config(state="disabled", cursor='')
                            Directories.config(state="disabled", cursor='')
                            Files.config(state="disabled", cursor='')
                            Statics_Update()
                            timer_start = time.time()

                            for root, dirs, files in os.walk(Path_entry.get(), followlinks=isFollowLinks.get()):
                                if not process.isRunning: break  # if stopped then break loop
                                if isDirectories.get():
                                    InnerMapping_dir(root=root, dirs=dirs)
                                if isFiles.get():
                                    InnerMapping_file(root=root, files=files)
                                estimated.set(value="{:.3f}".format(time.time() - timer_start))
                            process.stop()

                            FollowLinks.config(state="normal", cursor='hand2')
                            Directories.config(state="normal", cursor='hand2')
                            Files.config(state="normal", cursor='hand2')
                        else:
                            showerror(title="Error!", message="'%s' Not found!" % path)

        def Popup(event):
            """
            Table regions:
                HEADING, CELL and NOTHING
                ---
                heading --> Table header
                cell    --> Table item
                nothing --> Table body (empty spaces in table)

            ┌───────────┬───────────┬───────────┐
            │  header1  │  header2  │  header3  │ <-- Heading (Table header)
            ├───────────┼───────────┼───────────┤
            │  value1   │  value2   │  value3   │ <-- Cell (Table item)
            │  value1   │  value2   │  value3   │ <-- Cell (Table item)
            │  value1   │  value2   │  value3   │ <-- Cell (Table item)
            ├───────────┴───────────┴───────────┤
            │                                   │ <- Nothing (Table body)
            │                                   │ <- Nothing (Table body)
            └───────────────────────────────────┘

            Identifiers: table.identify(x=x, y=y, component=[identifier])
            1. region
            2. item
            3. column
            4. row
            5. element
            """
            x = event.x
            y = event.y
            clicked = table.identify_region(x=x, y=y)

            if clicked == "nothing":
                # nothing_menu = Menu(ants_blueprint, tearoff=0)
                # nothing_menu.post(x=event.x_root, y=event.y_root)
                pass
            elif clicked == "heading":
                pass
            elif clicked == "cell":
                clicked_id = table.identify(x=x, y=y, component='row')
                data = table.item(item=clicked_id)['values']
                dict_data = dict(zip([header.lower() for header in headers], data))

                table.selection_set(clicked_id)
                table.focus_set()
                table.focus(clicked_id)

                cell_menu = Menu(ants_blueprint, tearoff=0)

                showMenu = Menu(ants_blueprint, tearoff=0)
                cell_menu.add_cascade(menu=showMenu, label="Show",
                                      state="disabled" if len(Unattached) < 1 else "normal",
                                      accelerator=str(len(Unattached)))

                def Show(item):
                    table.reattach(parent='', item=item, index=Unattached[item])
                    del Unattached[item]
                    Rearrange(table)

                def AddShow_opt(row):
                    showMenu.add_command(label=table.item(item=row)['values'][1],
                                         command=lambda: Show(row))  # show by file name

                for row in list(Unattached.keys()):
                    AddShow_opt(row)

                def Deattache():
                    y = event.y
                    column = table.identify_row(y=y)
                    Unattached[clicked_id] = table.get_children().index(clicked_id)
                    table.detach(column)
                    Rearrange(table)

                cell_menu.add_command(label="Hide", command=lambda: Deattache())

                def Mark(mark):
                    table.tag_configure(mark, background=marks[mark]['color'])
                    table.item(item=clicked_id, tags=mark)
                    Marked[clicked_id] = mark

                markMenu = Menu(ants_blueprint, tearoff=0)
                cell_menu.add_cascade(menu=markMenu, label="Mark")
                cell_menu.add_separator()
                markVar = StringVar(value='')
                if clicked_id in Marked:
                    markVar.set(value=Marked[clicked_id])

                def AddMarker(marker_id):
                    markMenu.add_radiobutton(label=marker_id, value=marker_id, variable=markVar,
                                             background=marks[marker_id]['color'], command=lambda: Mark(marker_id),
                                             columnbreak=0, foreground=marks[marker_id]['text_foreground'],
                                             accelerator=marks[marker_id]['desc'], hidemargin=False)

                for mark in list(marks.keys()):
                    AddMarker(mark)
                markMenu.add_separator()

                def Mark_Clear():
                    if clicked_id in Marked:
                        table.item(item=clicked_id, tags="")
                        Marked.pop(clicked_id)

                markMenu.add_command(label="Clear", command=lambda: Mark_Clear())

                column_menu = Menu(ants_blueprint, tearoff=0)
                cell_menu.add_cascade(menu=column_menu, label="Column")

                def new_column_menu(header):
                    value = dict_data[header]
                    inner_menu = Menu(ants_blueprint, tearoff=0)
                    column_menu.add_cascade(menu=inner_menu, label=header)

                    def copy(data):
                        pyperclip.copy(data)

                    inner_menu.add_command(label="copy", command=lambda: copy(value))

                    if header == "file":  # not only files both (file and directories)
                        inner_menu.add_separator()

                        filename = dict_data[header]

                        extension = os.path.splitext(filename)[-1][1:].upper()
                        details_state = "disabled"
                        if extension in extensions:
                            details_state = "normal"

                        def Details(filename):
                            extension = os.path.splitext(filename)[-1][1:].upper()
                            if extension in extensions:
                                prefix = '- '
                                mb_content = ''.join([prefix + e + "\n" for e in
                                                      extensions[extension]['descriptions']])  # messagebox content
                                showinfo(title=f"{extension} details", message=mb_content)

                        inner_menu.add_command(label="details", command=lambda: Details(value), state=details_state)

                        checksum_state = 'disabled'
                        if os.path.isfile(filename):
                            checksum_state = "normal"

                        def Hasher(filename, type):
                            def Hash():
                                with open(filename, 'r', errors="ignore") as file_content:
                                    showinfo(title=f"{os.path.split(filename)[-1]} {type} hash",
                                             message=hashlib.new(name=type,
                                                                 data=file_content.read().encode()).hexdigest())
                                file_content.close()

                            hash_menu.add_command(label=type, command=lambda: threading.Thread(target=Hash).start())

                        hash_menu = Menu(cell_menu, tearoff=0)
                        inner_menu.add_cascade(menu=hash_menu, label="Checksum", state=checksum_state)
                        hash_types = [
                            "md5", "sha128", "sha224",
                            "sha256", "sha384", "sha512",
                            "sha3_128", "sha3_224", "sha3_256",
                            "sha3_384", "sha3_512"
                        ]
                        for type in hash_types:
                            Hasher(filename=filename, type=type)

                for header, value in dict_data.items():
                    new_column_menu(header=header)
                cell_menu.add_separator()

                export_menu = Menu(ants_blueprint, tearoff=False)
                cell_menu.add_cascade(label="Export", menu=export_menu)

                def Export_csv():
                    all_data = [table.item(item=id)['values'] for id in table.get_children()]

                    csv_file_location = asksaveasfilename(title=f'export {len(all_data)} in (CSV) format to:',
                                                          filetypes=[('Comma Separated Values data', '*.csv')],
                                                          defaultextension="csv")
                    if csv_file_location:
                        pylist2csv(data=all_data, output_filename=csv_file_location)

                def Export_json():
                    all_data = [table.item(item=id)['values'] for id in table.get_children()]

                    json_file_location = asksaveasfilename(title=f'export {len(all_data)} in (JSON) format to:',
                                                           filetypes=[('JavaScript Object Notation', '*.json')],
                                                           defaultextension="json")
                    if json_file_location:
                        with open(file=json_file_location, mode="w", errors="ignore") as json_file:
                            json_file.write(json.dumps(all_data))
                        json_file.close()

                export_menu.add_command(label="csv", command=lambda: Export_csv())
                export_menu.add_command(label="json", command=lambda: Export_json())
                cell_menu.add_separator()

                def Delete():
                    confirm = askyesno(title="Delete?!", message="You're about to delete data number %s" % (
                            table.get_children().index(clicked_id) + 1))
                    if confirm:
                        table.delete(clicked_id)
                        Rearrange(table)

                cell_menu.add_command(label="Delete", command=lambda: Delete())

                cell_menu.post(x=event.x_root, y=event.y_root)

        table.bind("<Button-3>", Popup)

        def Sort_table(ev):
            """
            This func will sort the table based on last click.
            if [HEADER] clicked once will be sorted ascending(a-z0-9),
            twice will be sorted descending(z-a9-0), etc..
            """
            x = ev.x

            column_name = table.column(column=table.identify_column(x=x))['id']  # it's titled (Ex: Column)
            index = list(columns_settings).index(column_name)
            all_data = [table.item(item=id)['values'] for id in table.get_children()]
            sort_by = header_sorting[column_name]  # ascending(a-z0-9), descending(z-a9-0)

            if sort_by == "ascending":
                reverse = False
                header_sorting[column_name] = "descending"
            else:  # sort_by == "descending"
                reverse = True
                header_sorting[column_name] = "ascending"

            all_data = sorted(all_data, key=lambda x: x[index], reverse=reverse)
            table.delete(*table.get_children())
            for d in all_data:
                table.insert(parent='', values=d, index='end')
            # Rearrange(table)

        table.bind("<Double-Button-1>", Sort_table)
        ###############################

        Scan_Btn = Button(bottom_frame, text='Scan', command=lambda: threading.Thread(target=scan_ab).start(),
                          cursor='hand2', takefocus=False)
        Scan_Btn.pack(side='right', fill='x', padx=3)

        Path_entry = Entry(bottom_frame)
        Path_entry.insert(0, os.getcwd())
        Path_entry.pack(side='right', fill='x', expand=True)

        def Select_path():
            new_dir = askdirectory(title='Select a directory to scan', mustexist=True)
            if new_dir:
                Path_entry.delete(0, 'end')
                Path_entry.insert(0, new_dir)

        Select_path_Btn = Button(bottom_frame, text='Path:', bootstyle='link', takefocus=False, cursor='hand2',
                                 command=lambda: Select_path())
        Select_path_Btn.pack(side='right', padx=3)

        return job_frame

    def New_job_add(event):
        """
        add, configure, cget, forget, hide, identify, index, insert, instate, select, state, tab, or tabs
        element or tab
        """

        x = event.x
        y = event.y
        tab = Jobs_manager.tk.call(Jobs_manager._w, "identify", "tab", x, y)

        if tab == 0 and not Rename_isPoped.get():
            # Jobs_manager.hide(1)
            Jobs_manager.add(Job(), text='Job %s' % (len(Jobs_manager.tabs())))
            Jobs_manager.select(Jobs_manager.tabs()[-1])
            Update_informations()

    Jobs_manager.bind('<Button-1>', New_job_add, add='add-new-job')

    Rename_isPoped = BooleanVar(value=False)

    def Jobs_manager_Popup(e):
        x = e.x
        y = e.y
        tab_index = Jobs_manager.tk.call(Jobs_manager._w, "identify", "tab", x, y)
        tab_element = Jobs_manager.identify(x=x, y=y)  # label, padding, tab, image
        focused_frame = Jobs_manager.nametowidget(e.widget.select())
        if isinstance(tab_index, int):
            tab_name = Jobs_manager.tab(tab_id=tab_index)['text']
            if tab_index >= 1 and not Rename_isPoped.get():  # check if the pressed tab is not the first one (cuz it's the adding button)
                jm_menu = Menu(ants_blueprint, tearoff=False)

                def Rename():
                    Rename_isPoped.set(value=True)
                    main_frame = Frame(ants_blueprint, padding=5)
                    main_frame.place(x=x, y=y)

                    make_draggable(main_frame)

                    submit = Button(main_frame, text="✔", bootstyle='success-link', takefocus=False, cursor="hand2")
                    submit.pack(side='left')

                    new_name = Entry(main_frame)
                    new_name.pack(side='left', pady=3)

                    new_name.insert(0, tab_name)
                    new_name.focus_set()

                    def Confirm():
                        nn = new_name.get()
                        if len(str(nn)):
                            Jobs_manager.tab(tab_id=tab_index, text=nn)

                    submit.config(command=lambda: Confirm())

                    def Close():
                        main_frame.destroy()
                        submit.pack_forget()
                        new_name.pack_forget()
                        close.pack_forget()
                        Rename_isPoped.set(value=False)

                    close = Button(main_frame, text="✘", bootstyle='danger-link', takefocus=False, cursor="hand2",
                                   command=lambda: Close())
                    close.pack(side='right')

                jm_menu.add_command(label="Rename", command=lambda: Rename())

                def do_Close():
                    try:
                        confirm = askokcancel(title="Close?", message=f"{tab_name!r} will be closed!")
                        if confirm:
                            Jobs_manager.forget(tab_id=focused_frame)
                            Update_informations()
                    except _tkinter.TclError:
                        pass

                jm_menu.add_command(label="Close", command=lambda: do_Close(), foreground="red")
                jm_menu.post(x=e.x_root, y=e.y_root)

    Jobs_manager.bind('<Button-3>', Jobs_manager_Popup)

    def Update_informations():
        num = len(Jobs_manager.tabs()) - 1  # [1:]
        Jobs_manager.tab(0, text='➕ ' + str(num))  # updates the + button text

    def Title_OnTab_Change(e):
        try:
            tab_name = Jobs_manager.tab(tab_id=Jobs_manager.select())['text']
            new_title = default_title + f" ({tab_name})"

            ants_blueprint.title(new_title)
        except _tkinter.TclError:
            pass

    Jobs_manager.bind("<<NotebookTabChanged>>", Title_OnTab_Change)
    ants_blueprint.configure(menu=ab_menu)
    ants_blueprint.mainloop()

AntsBlueprint()


import json
import boto3
import os
from boto3.dynamodb.conditions import Key, Attr

table_name = os.environ['CINEMA_TABLE']

dynamodb = boto3.resource('dynamodb')
cinema_table = dynamodb.Table(table_name)

def add_reservation(event, context):
    # show_id, person_id -> body
    body = json.loads(event["body"])
    cinema_table.update_item(
        Key={
            'pk': 'show_' + body["show_id"],
            'sk': 'person_' + body["person_id"]
        }
    )
    print(json.dumps(event))
    return {
        'statusCode': 200,
        'body': json.dumps({
            "success": True
        })
    }
def display_show(event, context):
    # /show/{show_id}
    path_parameters = event["pathParameters"]
    show_id = path_parameters["show_id"]
    response = cinema_table.query(
        KeyConditionExpression=Key('pk').eq('show_' + show_id)
    )
    items = response['Items']
    print(items)
    return {
        'statusCode': 200,
        'body': json.dumps({
            "success": True,
            "list": items
        })
    }

def display_movie(event, context):
    # /?movie_id={movie_id}
    query_params = event["queryStringParameters"]
    movie_id = query_params["movie_id"]
    response = cinema_table.query(
        KeyConditionExpression=Key('pk').eq('movie_' + movie_id)
    )
    items = response['Items']
    print(items)
    return {
        'statusCode': 200,
        'body': json.dumps({
            "success": True,
            "list": items
        })
    }


from flask import Flask, render_template, Response
import cv2
import numpy as np
from tensorflow.keras.applications.resnet50 import preprocess_input
from tensorflow.keras.preprocessing import image
from tensorflow.keras.models import load_model

app = Flask(__name__)
model = load_model(r"C:\Users\ashis\OneDrive\Desktop\dataset2\Saved models\ResNet50_faceMask.h5")
cap = cv2.VideoCapture(0)  # use 0 for default camera, or specify a camera index if you have multiple cameras

# Define the class labels
class_labels = {'Mask': 0, 'No mask': 1}
labels = dict((v, k) for k, v in class_labels.items())
# your code for starting the process
def start_process():
    pass

# your code for stopping the process
def stop_process():
    # add your code here
    cap.release()
    pass

@app.route('/')
def index():
    return render_template('index.html')

# function to handle the video feed
def gen():
    # add your code to generate the video feed
    while True:
        success, frame = cap.read()
        if not success:
            break
        else:
            # Resize frame to match the input size of the model
            frame = cv2.resize(frame, (224, 224))

            # Preprocess the frame for inference
            x = image.img_to_array(frame)
            x = np.expand_dims(x, axis=0)
            x = preprocess_input(x)

            # Make predictions
            preds = model.predict(x)
            pred_classes = np.argmax(preds, axis=1)

            # Map predicted classes to their corresponding class labels
            pred_class_label = labels[pred_classes[0]]
            # Get the percentage of certainty (confidence score) for the prediction
            confidence = preds[0][pred_classes[0]] * 100

            # Change color to red if predicted class is "No Mask"
            if pred_class_label == "No mask":
                color = (0, 0, 255)  # Red color
            else:
                color = (0, 255, 0)  # Green color

            # Draw the predicted class label and confidence score on the frame
            text = f"{pred_class_label}: {confidence:.2f}%"
            cv2.putText(frame, text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, color, 2)

            # Encode the frame as a jpeg image
            ret, buffer = cv2.imencode('.jpg', frame)
            frame = buffer.tobytes()

            # Yield the frame as a multipart response
            yield (b'--frame\r\n'
                   b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')

@app.route('/video_feed')
def video_feed():
    return Response(gen(), mimetype='multipart/x-mixed-replace; boundary=frame')

@app.route('/start')
def start():
    start_process()
    return "Process started"

@app.route('/stop')
def stop():
    stop_process()
    return "Process stopped"

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


from google.appengine.api import memcache
from google.appengine.ext import db
import database
import time
import logging

'''The method gets the cache of page with pagename. If not cached yet, it reads from data base'''
def cache_get(pagename):
    result = memcache.get(pagename)
    if result is None:  #no cookie
        page = db.GqlQuery("select * from Page WHERE pagename=:1" , pagename).get()
        if page == None:  #no data or cookie
            return (None, None)
        else:
            time_last = page.created
            memcache.set(pagename, (page, time_last))   #the cache is stored with additional information of last time cache was edited'
    else:
        page, time_last = result

    return (page, time_last)

'''The method edits the page object. It basically adds a new page object into database each time edited.
   It also update the cache of the page with pagename to the latest edit'''
def cache_edit(pagename, content):
    time_last = time.time()
    page = database.Page(pagename = pagename, content = content)
    page.put()
    memcache.set(pagename, (page, content))

    return (page, time_last)


from typing import List, Optional, Union
from fastapi import (
    Depends,
    HTTPException,
    Response,
    APIRouter,
)
from queries.post import (
    Error,
    PostIn,
    PostOut,
    PostRepository,
    PostOutWithoutUser,
    PostOutwithPics,
    PostOutWithPicsMore,
)
from token_auth import get_current_user

router = APIRouter()

@router.post("/api/posts", response_model=Union[PostOut, Error])
def create_post(
    post: PostIn,
    account_data: dict = Depends(get_current_user),
    repo: PostRepository = Depends(),
):
    return repo.create(post, account_data.id)

@router.get(
    "/api/user/posts", response_model=Union[List[PostOutWithPicsMore], Error]
)
def get_posts_user(
    response: Response,
    account_data: dict = Depends(get_current_user),
    repo: PostRepository = Depends(),
) -> PostOutWithPicsMore:
    post = repo.get_user_posts(account_data.id)
    if post is None:
        response.status_code = 404
    return post

@router.get("/posts", response_model=Union[List[PostOutWithPicsMore], Error])
def get_all_posts(
    repo: PostRepository = Depends()
):
    return repo.get_all()

@router.get(
    "/searchpost/{post_title}", response_model=Optional[PostOutWithPicsMore]
    )
def get_post_title(
    post_title: str,
    response: Response,
    repo: PostRepository = Depends(),
) -> PostOutwithPics:
    post = repo.get_post_by_title(post_title)
    if post is None:
        response.status_code = 404
    return post

@router.delete("/posts/{post_id}", response_model=Union[str, Error])
def delete_post(
    post_id: int,
    account_data: dict = Depends(get_current_user),
    repo: PostRepository = Depends(),
):
    result = repo.delete_user_post(post_id, account_data.id)
    if result is None:
        raise HTTPException(state_code=404, detail="Post not found")
    return {"message": "Post deleted successfully"}

@router.put("/posts/{post_id}", response_model=Union[List[PostOut], Error])
def update_post(
    post_id: int,
    post: PostIn,
    repo: PostRepository = Depends(),
) -> Union[Error, PostOut]:
    return repo.update_post(post_id, post)

@router.get("/post/{post_id}", response_model=Optional[PostOutWithoutUser])
def get_post(
    post_id: int,
    response: Response,
    repo: PostRepository = Depends(),
) -> PostOutWithoutUser:
    post = repo.get_one_post(post_id)
    if post is None:
        response.status_code = 404
    return post


import scrapy

class LanguageDailySpider(scrapy.Spider):
    name = 'common_german_words'
    start_urls = [
        'http://languagedaily.com/learn-german/vocabulary/common-german-words',
        'http://languagedaily.com/learn-german/vocabulary/most-common-german-words-2'
    ] + list([
        f'http://languagedaily.com/learn-german/vocabulary/common-german-words-{page}'
        for page in range(3, 13)
    ])

    def parse(self, response):
        for row in response.css('.jsn-article-content table tr'):
            columns = row.css('td ::text').extract()
            if 'Rank' not in columns[0]:
                yield {
                    'rank': int(columns[0].strip().replace('.', '')),
                    'german_word': columns[1].strip(),
                    'english_translation': columns[2].strip(),
                    'part_of_speech': columns[3].strip(),
                    'url': response.url
                }


from Optimizer import sort_array_by_col
import numpy as np
import matplotlib.pyplot as plt
import copy

class MultiObjectiveOptimizer:
    def __init__(self, x, fitness, n_generations=5, population_size=5, n_objectives=2, constraint=None,
                 generation_func=None, constraint_func_input="design"):
        """Initialize the optimizer
        x: list that contains a series of dictionaries that define the x values for the optimizer where
        type is either continuous or integer. and min and max are the minimum and maximum values of the variable this
        should follow the form [{'type': ('integer' or 'continuous'), 'bounds': (min, max)]] with a new dict for each
        x value

        fitness: function that accepts x array in the same order as given in the x input, and returns the fitness of
        each design.

        n_generations: the number of generations to carry out

        population_size: the number of members in the population at each generation

        n_objectives: the number of objectives that are returned by the fitness function

        constraint: function that accepts x array in the same order as given in x and returns a vector that contains
        the constraint violation state of each constraint where >0 suggests that the constraint is violated and <=0
        suggests that the constraint is satisfied.

        generation_func: function that is called with each passing generation.  calls the generation function with
        the current population of the algorithm.  This is useful for things like plotting the generation at each
        iteration of the algorithm, but could be used for other fun things

        constraint_func_input: string, either "design" or "full" if design the constraint function is called with just
        the design, if full the constraint function is called with a 1d array where the 0th value is the maximin fitness
        of the design, the next n values are the n objectives returned by the fitness function, and the last n values
        are the n values that define the design.  It is useful to define full if the fitness and constraints are based
        on the same values and they are somewhat expensive to obtain.
        """
        self.num_generations = n_generations
        self.num_population = np.trunc(population_size)
        if self.num_population % 2 != 0:
            self.num_population += 1
        self.num_population = int(self.num_population)
        self.num_x = len(x)
        self.fitness_func = fitness
        self.constraint_func = constraint
        self.x_def = x
        self.num_objectives = n_objectives
        self.generation_call = generation_func
        self.constraint_func_input = constraint_func_input

        self.tournament_size = 2
        self.crossover_prob = 0.5
        self.mutation_prob = 0.13
        self.cross_eta = 0.5
        self.mutation_beta = 0.13

        # initialize the parent array
        # stored in the form ([fitness, x1, x2, x3, ..., xn])
        self.population = np.zeros((self.num_population, self.num_x + n_objectives + 1))
        for i in range(self.num_population):
            x_new = np.zeros(self.num_x)
            for j, val in enumerate(x):
                if val['type'] == 'integer':
                    x_new[j] = np.random.randint(val['bounds'][0], val['bounds'][1] + 1)
                elif val['type'] == 'continuous':
                    x_new[j] = np.random.uniform(val['bounds'][0], val['bounds'][1])
                else:
                    print("error unknown variable type")
            self.population[i, 1:n_objectives+1] = self.fitness_func(x_new)
            self.population[i, n_objectives+1:] = x_new
        self.population = sort_array_by_col(self.population, 0)
        self.population = self.calc_maximin(self.population)
        self.population = self.apply_constraints(self.population)
        if self.generation_call is not None:
            self.generation_call(self.population)
        return

    def select_parents(self):
        """select the parents from the current population of the optimization"""
        # randomize the order of the population
        np.random.shuffle(self.population)
        # preallocate the array to hold the parents
        parents = np.zeros_like(self.population)
        # self.population = self.calc_maximin(self.population)
        # select random people from the population for tournament selection
        for row in range(parents.shape[0]):
            rand_indicies = np.random.randint(parents.shape[0], size=self.tournament_size)
            competitors = self.population[rand_indicies]
            sorted_competitors = sort_array_by_col(competitors, 0)
            parents[row, :] = sorted_competitors[0]
        return parents

    def calc_maximin(self, population):
        """calculates the maximin values for each point of the supplied population.  Uses a bunch of information stored
        in the class, so probably not a good idea to pass in random populations, unless you know what you're doing."""
        for idx in range(population.shape[0]):
            # get function values
            fVals = copy.deepcopy(population[:, 1:self.num_objectives+1])
            for col_idx in range(self.num_objectives):
                test_val = fVals[idx, col_idx]
                fVals[:, col_idx] = -(fVals[:, col_idx] - test_val)
            fVals = np.delete(fVals, idx, 0)
            population[idx, 0] = np.nanmax(np.nanmin(fVals, 1))
        return population

    def apply_constraints(self, population):
        """ applies appropriate penalties for designs that are outside of the permissible bounds.  Requires that a
        constraint function be defined that returns the constraints in a row vector"""
        if self.constraint_func is None:
            return population
        max_fitness = np.nanmax(population[:, 0])
        for row in population:
            design = row[self.num_objectives+1:]
            if self.constraint_func_input == "design":
                cons = self.constraint_func(design)
            elif self.constraint_func_input == "full":
                cons = self.constraint_func(row)
            else:
                print("unrecognized constraint input term check constraint_func_input argument at initialization")
                quit()
            if np.max(cons) > 0:
                row[0] = max_fitness + np.max(cons)
        return population

    def find_min(self):
        generations = []
        for generation in range(self.num_generations):
            # select reproducing parents
            parents = self.select_parents()
            children = np.zeros_like(parents)
            # for each set of parents
            for idx in range(0, parents.shape[0], 2):
                child1 = copy.deepcopy(parents[idx, self.num_objectives+1:])
                child2 = copy.deepcopy(parents[idx+1, self.num_objectives+1:])
                for x_idx in range(len(child1)):
                    crossover = np.random.random()
                    mutate1 = np.random.random()
                    mutate2 = np.random.random()
                    if crossover < self.crossover_prob:
                        # perform the crossover
                        self.crossover(child1, child2, x_idx)
                    if mutate1 < self.mutation_prob:
                        child1 = self.mutate(child1, x_idx, self.x_def[x_idx]['bounds'],
                                             self.x_def[x_idx]['type'], generation)
                    if mutate2 < self.mutation_prob:
                        child2 = self.mutate(child2, x_idx, self.x_def[x_idx]['bounds'],
                                             self.x_def[x_idx]['type'], generation)
                # put the children into the children array
                child1_fitness = self.fitness_func(child1)
                child2_fitness = self.fitness_func(child2)
                children[idx, 1:self.num_objectives+1] = child1_fitness
                children[idx, self.num_objectives+1:] = child1
                children[idx + 1, 1:self.num_objectives+1] = child2_fitness
                children[idx + 1, self.num_objectives+1:] = child2
            # perform elitism
            population_pool = np.append(parents, children, axis=0)
            population_pool = self.calc_maximin(population_pool)
            population_pool = self.apply_constraints(population_pool)
            sorted_pool = sort_array_by_col(population_pool, 0)
            self.population = sorted_pool[0:self.num_population]
            # generations.append(copy.deepcopy(self.population))
            print(generation)
            if self.generation_call is not None:
                self.generation_call(self.population)
        return self.population

    def crossover(self, child1, child2, x_idx):
        """
        Performs crossover between the two children at the specifiec index.  The children must be numpy arrays or
        some other object that is mutable so that the changes persist
        :param child1: Child 1 to be crossed a numpy array of the values
        :param child2: Child 2 to be crossed a numpy array of the values
        :param x_idx: Index location for the crossover.
        :return: none
        """
        r = np.random.random()
        if r <= 0.5:
            a = ((2 * r) ** (1 / self.cross_eta)) / 2
        else:
            a = 1 - ((2 - 2 * r) ** (1 / self.cross_eta)) / 2
        child1_val = child1[x_idx]
        child2_val = child2[x_idx]
        y1 = a * child1_val + (1 - a) * child2_val
        y2 = (1 - a) * child1_val + a * child2_val
        child1[x_idx] = y1
        child2[x_idx] = y2
        # truncate the values if needed
        if self.x_def[x_idx]['type'] == 'integer':
            child1[x_idx] = int(np.round(child1[x_idx]))
            child2[x_idx] = int(np.round(child2[x_idx]))

    def mutate(self, child, idx, bounds, type, generation):
        """
        Perform a mutation on the child at the specified location
        :param child: array of values that represent the child to be mutated
        :param idx: the index where the mutation should occur
        :param bounds: bound of the value that is being mutated
        :param type: Type of the variable
        :param generation: generation number for the mutation.
        :return: the mutated child
        """
        min = bounds[0]
        max = bounds[1]
        r = np.random.uniform(min, max)
        alpha = (1 - (generation) / self.num_generations) ** self.mutation_beta
        if r <= child[idx]:
            child[idx] = min + (r - min) ** alpha * (child[idx] - min) ** (1 - alpha)
        else:
            child[idx] = max - (max - r) ** alpha * (max - child[idx]) ** (1 - alpha)
        if type == 'integer':
            child[idx] = np.round(child[idx])
        return child

def calc_fitness(x):
    width = x[0]
    height = x[1]

    length = 20
    force = 10
    modulus = 39000000
    density = 4.65 / 16

    max_weight = 200
    max_defl = 2

    deflection = (force * length**3) / (3 * modulus * (width * height**3)/12)
    # deflection = saturate(deflection, max_defl)/max_defl
    weight = length * width * height * density
    # weight = saturate(weight, max_weight)/max_weight
    return [deflection, weight]

def calc_fitness2(x):
    width = x[0]
    height = x[1]

    length = 20
    force = 10
    modulus = 39000000
    density = 4.65 / 16

    max_weight = 200
    max_defl = 2

    deflection = (force * length**3) / (3 * modulus * (width * height**3)/12)
    # deflection = saturate(deflection, max_defl)/max_defl
    weight = length * width * height * density
    # weight = saturate(weight, max_weight)/max_weight
    return [deflection]

def calc_constraints(x):
    width = x[0]
    height = x[1]

    length = 20
    force = 10
    modulus = 39000000
    density = 4.65 / 16

    max_deflection = 0.04
    max_weight = 10
    max_stress = 36000

    moment = length * force
    I = (width * height ** 3) / 12
    stress = moment * (height / 2) / I
    stress_constraint = (stress - max_stress) / max_stress

    deflection = (force * length ** 3) / (3 * modulus * (width * height ** 3) / 12)
    deflection_constraint = (deflection - max_deflection) / max_deflection

    weight = length * width * height * density
    weight_constraint = (weight - max_weight) / max_weight
    return [deflection_constraint, weight_constraint, stress_constraint]

def saturate(val, max_val):
    if val > max_val:
        val = max_val
    return val

if __name__ == "__main__":
    z = [{'type': 'continuous', 'bounds': (0, 2)},
         {'type': 'continuous', 'bounds': (0, 2)}]
    n_gen = 100
    opt = MultiObjectiveOptimizer(z, calc_fitness2, n_objectives=1, population_size=10, n_generations=n_gen,
                                  constraint=calc_constraints)
    # opt.calc_maximin(opt.population)
    generations = opt.find_min()

    # x_start = generations[0][:, 1]
    # y_start = generations[0][:, 2]
    #
    # x_middle = generations[int(n_gen/2)-1][:, 1]
    # y_middle = generations[int(n_gen/2)-1][:, 2]
    #
    # x_end = generations[-1][:, 1]
    # y_end = generations[-1][:, 2]

    # plt.plot(x_start, y_start, 'o', label='1st generation')
    # plt.plot(x_middle, y_middle, 'o', label='middle generation', alpha=0.5)
    # plt.plot(x_end, y_end, 'o', label='last generation', alpha=0.5)
    # plt.xlabel('deflection')
    # plt.ylabel('weight')
    # plt.title('weight vs. deflection pareto front')
    # # plt.xlim((0, 0.025))
    # # plt.ylim((0, 10))
    # plt.legend()
    # plt.show()

    plt.ion()
    for i, generation in enumerate(generations):
        # if i > 5:
        #     points.clear()
        points = plt.plot(generation[:, 1], generation[:, 2], 'o')
        plt.xlim((0, 0.05))
        plt.ylim((0, 3))
        plt.xlabel('deflection')
        plt.ylabel('weight')
        plt.title('weight vs. deflection pareto front')
        # plt.xlim((0, 1))
        # plt.ylim((0, 1))
        plt.pause(0.5)


# -*- coding: utf-8 -*-
import scrapy
from myDouban.items import MydoubanItem
# from scrapy.Request import Request

class DoubanSpider(scrapy.Spider):
    name = 'douban'
    allowed_domains = ['www.douban.com']

    start_urls = ['https://music.douban.com/chart']

    def parse(self, response):
        print("]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]")
        print(response.body.decode())
        for item in response.css(".intro-play"):
            title=item.xpath("p/text()").extract()[0];
            href=item.xpath("::h3/text").extract()[0];
            onetime=MydoubanItem();
            onetime["title"]=title;
            onetime["href"]=href;
            yield onetime

        #     yield Request(url=item_details_url, meta={'item': item},callback=self.parse_details)
        # def parse_details(self,response):
        #     print("***********************************")


import cv2 as cv
import numpy as np
import math
import walk
# def extract(src,dst):
# 	wmrgb=cv.imread(src)
# 	wmyuv=cv.cvtColor(wmrgb,cv.COLOR_RGB2YUV)
# 	wmf=wmyuv.astype('float32')
# 	part8x8rownum=int(wmf.shape[0]/8)
# 	part8x8colnum=int(wmf.shape[1]/8)
# 	r=5
# 	fingernum=230
# 	extractxydict=walk.findpoint((3,4,-1),r)
# 	finishfinger=np.zeros([fingernum,fingernum,3],np.uint8)
# 	i,j=0,0
# 	count=0
# 	flag=0
# 	for parti in range(part8x8rownum):
# 		for partj in range(part8x8colnum):
# 			part8x8=cv.dct(wmf[8*parti:8*parti+8,8*partj:8*partj+8,0])
# 			if (part8x8.shape[0]<8)|(part8x8.shape[1]<8):
# 				continue
# 			for t in range(r):
# 				if (i==fingernum):
# 					break
# 				rx,ry=extractxydict[t]
# 				#观察r1和r2的大小关系,得出水印像素点黑白情况
# 				r1=part8x8[rx,ry]
# 				r2=part8x8[7-rx,7-ry]#r1的中心对称格子
# 				if r1>r2:
# 					finishfinger[i,j]=0#黑
# 				else:
# 					if r1<r2:
# 						finishfinger[i,j]=255#白
# 				j+=1
# 				if (j==fingernum):
# 					j=0
# 					i=i+1
# 				count+=1
# 	print('finishfinger=  ' ,finishfinger)
# 	# print("countextract=",count)
# 	cv.imwrite(dst,(finishfinger),[int(cv.IMWRITE_JPEG_QUALITY),100])
def extract(src,dst,host_image):
	host=cv.imread(host_image)
	#Lưu ảnh gốc từ RGB sang YUV
	hostyuv=cv.cvtColor(host,cv.COLOR_RGB2YUV)
	#Chuyển đổi giá trị ảnh sang float 32
	hostf=hostyuv.astype('float32')

	# print('host= ',hostf.shape)
	wmrgb=cv.imread(src)
	wmyuv=cv.cvtColor(wmrgb,cv.COLOR_RGB2YUV)
	wmf=wmyuv.astype('float32')
	# print('wm=  ',wmf.shape)
	part8x8rownum=int(wmf.shape[0]/8)
	part8x8colnum=int(wmf.shape[1]/8)
	fingernum=230
	# extractxydict=walk.findpoint((3,4,-1),r)
	finishfinger=np.zeros([fingernum,fingernum],np.float32)
	x,y=0,0
	count=0
	flag=0
	beta = 0.01
	for parti in range(part8x8rownum):
		for partj in range(part8x8colnum):
			part8x8=cv.dct(wmf[8*parti:8*parti+8,8*partj:8*partj+8,0])
			# print('part8x8= ',part8x8)
			host_part8x8= cv.dct(hostf[8*parti:8*parti+8,8*partj:8*partj+8,0])
			if (part8x8.shape[0]<8)|(part8x8.shape[1]<8):
				continue
			for i in range(3):
				for j in range(3,6):
					if x == 230:
						break
					else:
						# print('part8x8[i,j] = ',part8x8[i,j])
						finishfinger[x,y] = (part8x8[i,j]- host_part8x8[i,j])/beta
						# print('part8x8= ',part8x8[i,j])
						# print('host_part8x8 = ',host_part8x8[i,j])
						# print(finishfinger[x,y])
						y+=1
						if y == 230:
							y=0
							x+=1
	 # float conversion/scale
	# dct_bsrc = cv.dct(imf)
	# finishfinger = cv.idct(imf)*255.0
	finishfinger = (cv.idct(finishfinger))
	for i in range(finishfinger.shape[0]):
		for j in range(finishfinger.shape[1]):
			if abs(finishfinger[i,j])>128:
				finishfinger[i,j]= abs(finishfinger[i,j])
			else:
				finishfinger[i,j] = 0
	# finishfinger= abs(finishfinger)
	# finishfinger = np.uint8(dct_bsrc)*255.0
	# print('finishfinger= ',finishfinger )
	# print('finishfinger=  ',finishfinger.shape)
	# print("countextract=",count)
	cv.imwrite(dst,(finishfinger),[int(cv.IMWRITE_JPEG_QUALITY),100])
def main():
	for x in range(6):
		host_image = 'host.jpg'
		wmname="finishwm"+str(x)
		exname=str(x)+"extractfinger"
		# extract(wmname+".jpg",exname+".jpg")# thuật toán của tác giả
		extract(wmname+".jpg",exname+".jpg",host_image)
		img=cv.imread(exname+".jpg")
		cv.namedWindow(exname,0)
		k=240
		cv.resizeWindow(exname,k,int(k*img.shape[0]/img.shape[1]))
		cv.imshow(exname,img)
		cv.waitKey(0)
if __name__ == '__main__':
	main()


import math

import torch

class GroupingWrapper(torch.utils.data.Dataset):
    def __init__(self, dataset, csi, group_size, num_samples=-1, seed=1):
        super().__init__()
        self.dataset = dataset
        self.group_size = group_size

        self.csi = csi

        if num_samples == -1:
            self.indices = torch.combinations(
                torch.arange(0, len(dataset)), r=self.group_size, with_replacement=False
            )
            self.num_samples = self.indices.size(0)
        elif num_samples == 0:
            self.num_samples = math.ceil(float(len(self.dataset)) / self.group_size)
            num_new_elements = len(self.dataset)
            if len(self.dataset) % self.group_size != 0:
                num_new_elements += self.group_size - len(self.dataset) % self.group_size

            self.indices = torch.arange(0, num_new_elements) % len(self.dataset)
            self.indices = self.indices.reshape(-1, self.group_size)
        else:
            self.num_samples = num_samples
            # Generate random combinations of indices
            r = torch.randint(0, len(dataset), (3 * self.num_samples, self.group_size))
            self.indices = r.unique(sorted=False, dim=0)[: self.num_samples, :].long()

            if self.indices.size(0) < self.num_samples:
                self.num_samples = self.indices.size(0)

        shuffled_indices = torch.randperm(
            len(dataset), generator=torch.Generator().manual_seed(seed)
        )

        self.indices = shuffled_indices[self.indices]

    def __getitem__(self, idx):
        """if self.with_replacement:"""

        indices = self.indices[idx]
        X = []
        y_single = torch.zeros(
            (self.group_size,),
            dtype=torch.long,
        )
        for i in range(self.group_size):
            x, cur_y = self.dataset.__getitem__(indices[i])
            X.append(x)
            if cur_y == 1:
                y_single[i] = 1

        return (torch.stack(X, dim=0), self.csi[idx])

    def __len__(self):

        return self.num_samples


import cv2
from datetime import datetime
import numpy as np
import os
import re
import sys
from tqdm import tqdm

from .detect import Detect

class Camera:
    """Manage and control the USB camera"""

    # -----------------
    # class attributes
    # -----------------

    # video source
    cam_num = 1 # 1 is the usb cam on thor
    video_path = None

    # camera configuration
    fps = None
    image_size = None
    record = None
    show_view = None
    tensor_image_size = None

    # streamers and recorder
    stream = None
    video_recorder = None
    video_detect_recorder = None

    def __init__(self, cam_num = 1, video_path = None):
        """Initialize Camera Class"""

        # set class variables
        self.cam_num = cam_num
        self.video_path = video_path

    def configure(self, fps = 30, image_size = [640, 480], record = False,
            show_view = False, tensor_image_size = [800, 600]):
        """configure camera specifications"""
        self.fps = fps
        self.image_size = (image_size[0], image_size[1])
        self.record = record
        self.show_view = show_view
        self.tensor_image_size = tensor_image_size

    def detect(self):
        """
        TODO Finish Documentation Numpy Style
        """

        if self.video_path is None: # stream from camera
            self.stream = cv2.VideoCapture(self.cam_num)
        elif self.video_path is not None: # stream from video
            self.stream = cv2.VideoCapture(self.video_path)
        else: # unexpected error
            print("An unexpected error occurred when starting video stream!")
            sys.exit(0)

        if self.record:
            self.startVideoRecorder()
            self.startVideoDetectionRecorder()

        # initialize detector
        detector = Detect()

        # start the streaming loop
        try:
            while(self.stream.isOpened()):
                # capture frame by frame
                ret, frame = self.stream.read()

                # make sure the frames are reading
                if not ret:
                    print("Unable to receive frame.")
                    break

                # send the frame through the object detector
                detect_frame = detector.inference(frame)

                # check if user wants to record
                if self.record:
                    self.video_recorder.write(frame)
                    self.video_detect_recorder.write(detect_frame)

                # check if user wants to see the cam's view
                if self.show_view:
                    cv2.imshow("Your Eye", frame)
                    cv2.imshow("God's Eye", detect_frame)

                # run until key press 'q'
                QUIT_KEY = 'q'
                if cv2.waitKey(1) == ord(QUIT_KEY):
                    break
        except KeyboardInterrupt:
            pass

        # release the capture
        self.stream.release()
        if self.record:
            self.video_recorder.release()
            self.video_detect_recorder.release()
        cv2.destroyAllWindows()

    def startVideoDetectionRecorder(self, path=None, image_size=None):
        """create recorder for detection video stream"""

        # set attributes for video recorder
        CODEC = 'XVID'
        fourcc = cv2.VideoWriter_fourcc(*CODEC)
        date = datetime.now()

        if image_size is not None:
            self.image_size = image_size

        if path is None:
            # create video recorder
            self.video_detect_recorder = cv2.VideoWriter('videos/recording_detect_'
                                + str(date.month) + '-'
                                + str(date.day) + '-'
                                + str(date.year) + '_'
                                + str(date.hour) + '-'
                                + str(date.minute) + '-'
                                + str(date.second) + '_'
                                + '.avi', fourcc, self.fps, self.image_size)
        else:
            # define regex to extract group number
            r_group = re.compile(r'(?<=/)group[0-9]{3}(?=/)')

            # get file name and group from video path
            base_name = "inference_{}".format(os.path.basename(self.video_path))
            group = re.findall(r_group, self.video_path)[0]

            # define output directory
            output_dir = 'output'

            # if output dir does not exist, make it
            if not os.path.isdir(output_dir):
                os.mkdir(output_dir)

            # if group in output dir does not exist, make it
            elif not os.path.isdir(os.path.join(output_dir, group)):
                os.mkdir(os.path.join(output_dir, group))
            else:
                pass

            # define video recorder
            self.video_detect_recorder = cv2.VideoWriter(
                                            os.path.join(output_dir,
                                                         group,
                                                         base_name),
                                            fourcc,
                                            self.fps,
                                            self.image_size)

            # end function
            return

    def startVideoRecorder(self):
        """create recorder for video stream"""

        # set attributes for video recorder
        CODEC = 'XVID'
        fourcc = cv2.VideoWriter_fourcc(*CODEC)
        date = datetime.now()

        # create video recorder
        self.video_recorder = cv2.VideoWriter('videos/recording_'
                                + str(date.month) + '-'
                                + str(date.day) + '-'
                                + str(date.year) + '_'
                                + str(date.hour) + '-'
                                + str(date.minute) + '-'
                                + str(date.second) + '_'
                                + '.avi', fourcc, self.fps, self.image_size)

    def startVideoStream(self):
        """load video stream from video or camera"""

        if self.video_path is None: # stream from camera
            stream = cv2.VideoCapture(self.cam_num)
        elif self.video_path is not None: # stream from video
            stream = cv2.VideoCapture(self.video_path)
        else: # unexpected error
            print("An unexpected error occurred when starting video stream!")
            sys.exit(0)

        # verify stream functionality
        if not stream.isOpened():
            print("ERROR: unable to load video stream.")
            sys.exit(0)

        # set stream to class attribute
        self.stream = stream

        # create video recorder
        if self.record:
            self.startVideoRecorder()

        # start the streaming loop
        try:
            while(self.stream.isOpened()):
                # capture frame by frame
                ret, frame = self.stream.read()

                # make sure the frames are reading
                if not ret:
                    print("Unable to receive frame.")
                    break

                if self.record:
                    self.video_recorder.write(frame)

                # show frame
                if self.show_view:
                    cv2.imshow("Current View", frame)

                # run until key press 'q'
                QUIT_KEY = 'q'
                if cv2.waitKey(1) == ord(QUIT_KEY):
                    break
        except KeyboardInterrupt:
            pass

        # release the capture
        self.stream.release()
        if self.record:
            self.video_recorder.release()
        cv2.destroyAllWindows()

    def process_prerecorded(self):
        """run prerecorded videos through ml pipeline"""

        # start video stream
        self.stream = cv2.VideoCapture(self.video_path)

        # get length of video
        num_frames = self.stream.get(cv2.CAP_PROP_FRAME_COUNT)

        # initialize detector
        detector = Detect()

        # define tqdm object
        progressBar = tqdm(total=num_frames, ncols=100)

        # start the streaming loop
        try:
            while(self.stream.isOpened()):
                # capture frame by frame
                ret, frame = self.stream.read()

                # make sure the frames are reading
                if ret:
                    # check if video recorder has been initialized
                    if self.video_detect_recorder is None:
                        # define video recorder
                        self.startVideoDetectionRecorder(
                            path = self.video_path,
                            image_size = (frame.shape[1], frame.shape[0]))

                    # send the frame through the object detector
                    detect_frame = detector.inference(frame)

                    # update progress bar
                    progressBar.update(1)

                    # record detected frame
                    self.video_detect_recorder.write(detect_frame)
                elif not ret:
                    break
        except KeyboardInterrupt:
            pass

        # release the capture
        self.stream.release()
        self.video_detect_recorder.release()

        # end function
        return


from ..parsing import json

from aiohttp.test_utils import AioHTTPTestCase
from dataclasses import dataclass, field

from ..tests.test_case import unittest_run_loop

@dataclass
class TestRequest():
    path: str
    method: str = "GET"
    headers: dict = field(default_factory=dict)
    cookies: dict = field(default_factory=dict)
    body: str = None

@dataclass
class TestResponse():
    statusCode: int
    message: str = ""
    headers: dict = field(default_factory=dict)
    body: str = None

class HttpTest():
    def __init__(
        self,
        request,
        response,
        setUpAsync=None,
        tearDownAsync=None,
        *args,
        **kwargs,
    ):
        super().__init__(*args, **kwargs)
        self.request = request
        self.response = response
        self.setUpAsync = setUpAsync
        self.tearDownAsync = tearDownAsync

    def generateTestCase(self, appClass, settings):
        expectedRequest = self.request
        expectedResponse = self.response

        theSettings = settings

        class TestCase(AioHTTPTestCase):
            _setUpAsync = self.setUpAsync
            _tearDownAsync = self.tearDownAsync
            settings = theSettings

            async def get_application(self):
                return appClass(settings, logging=False)

            async def compareResponses(self, expected, received):
                if expected.statusCode is not None:
                    self.assertEqual(
                        expectedResponse.statusCode,
                        received.status,
                    )
                if expected.body is not None:
                    receivedBody = await received.text()
                    # check json contents, rather than raw equality
                    if "application/json" in received.headers.get(
                        "Content-Type"
                    ):
                        expectedJson = json.loads(expected.body)
                        receivedJson = json.loads(receivedBody)
                        self.assertDictEqual(expectedJson, receivedJson)
                    else:
                        self.assterEqual(expected.body, receivedBody)

            @unittest_run_loop
            async def testRequest(self):
                if self._setUpAsync is not None:
                    await self._setUpAsync()
                response = await self.client.request(
                    method=expectedRequest.method,
                    path=expectedRequest.path,
                    data=expectedRequest.body,
                    headers=expectedRequest.headers,
                )
                await self.compareResponses(
                    expectedResponse,
                    response,
                )
                return

        return TestCase


# -*- coding: utf-8 -*-
"""
DataDictionary model.
"""
import os
from io import BytesIO, StringIO

from django.core.files.uploadedfile import InMemoryUploadedFile
from django.db.models.signals import post_save, pre_save
from django.db import transaction
from django.utils import timezone
from django.utils.translation import gettext as _

import openpyxl
import unicodecsv as csv
from floip import FloipSurvey
from kombu.exceptions import OperationalError
from pyxform.builder import create_survey_element_from_dict
from pyxform.utils import has_external_choices
from pyxform.xls2json import parse_file_to_json
from pyxform.xls2json_backends import xlsx_value_to_str

from onadata.apps.logger.models.xform import XForm, check_version_set, check_xform_uuid
from onadata.apps.logger.xform_instance_parser import XLSFormError
from onadata.libs.utils.cache_tools import (
    PROJ_BASE_FORMS_CACHE,
    PROJ_FORMS_CACHE,
    safe_delete,
)
from onadata.libs.utils.model_tools import get_columns_with_hxl, set_uuid

def is_newline_error(e):
    """
    Return True is e is a new line error based on the error text.
    Otherwise return False.
    """
    newline_error = (
        "new-line character seen in unquoted field - do you need"
        " to open the file in universal-newline mode?"
    )
    return newline_error == str(e)

def process_xlsform(xls, default_name):
    """
    Process XLSForm file and return the survey dictionary for the XLSForm.
    """
    # FLOW Results package is a JSON file.
    if xls.name.endswith("json"):
        return FloipSurvey(xls).survey.to_json_dict()

    file_object = xls
    if xls.name.endswith("csv"):
        file_object = None
        if not isinstance(xls.name, InMemoryUploadedFile):
            file_object = StringIO(xls.read().decode("utf-8"))
    try:
        return parse_file_to_json(xls.name, file_object=file_object)
    except csv.Error as e:
        if is_newline_error(e):
            xls.seek(0)
            file_object = StringIO("\n".join(xls.read().splitlines()))
            return parse_file_to_json(
                xls.name, default_name=default_name, file_object=file_object
            )
        raise e

# adopted from pyxform.utils.sheet_to_csv
# pylint: disable=too-many-branches,too-many-locals
def sheet_to_csv(xls_content, sheet_name):
    """Writes a csv file of a specified sheet from a an excel file

    :param xls_content: Excel file contents
    :param sheet_name: the name of the excel sheet to generate the csv file

    :returns: a (StringIO) csv file object
    """
    workbook = openpyxl.load_workbook(xls_content)

    sheet = workbook.get_sheet_by_name(sheet_name)

    if not sheet or sheet.max_column < 2:
        raise Exception(_(f"Sheet <'{sheet_name}'> has no data."))

    csv_file = BytesIO()

    writer = csv.writer(csv_file, encoding="utf-8", quoting=csv.QUOTE_ALL)
    mask = [v and len(v.strip()) > 0 for v in list(sheet.values)[0]]

    for row in sheet.iter_rows(values_only=True):
        row_values = []
        try:
            for val in row:
                if val is not None:
                    val = xlsx_value_to_str(val)
                    val = val.strip()
                row_values.append(val)
        except TypeError:
            continue

        if not all(v is None for v in row_values):
            writer.writerow([v for v, m in zip(row_values, mask) if m])
    return csv_file

def upload_to(instance, filename, username=None):
    """
    Return XLSForm file upload path.
    """
    if instance:
        username = instance.xform.user.username
    return os.path.join(username, "xls", os.path.split(filename)[1])

class DataDictionary(XForm):  # pylint: disable=too-many-instance-attributes
    """
    DataDictionary model class.
    """

    def __init__(self, *args, **kwargs):
        self.instances_for_export = lambda d: d.instances.all()
        self.has_external_choices = False
        self._id_string_changed = False
        super().__init__(*args, **kwargs)

    def __str__(self):
        return getattr(self, "id_string", "")

    def save(self, *args, **kwargs):
        skip_xls_read = kwargs.get("skip_xls_read")

        if self.xls and not skip_xls_read:
            default_name = None if not self.pk else self.survey.xml_instance().tagName
            survey_dict = process_xlsform(self.xls, default_name)
            if has_external_choices(survey_dict):
                self.has_external_choices = True
            survey = create_survey_element_from_dict(survey_dict)
            survey = check_version_set(survey)
            if get_columns_with_hxl(survey.get("children")):
                self.has_hxl_support = True
            # if form is being replaced, don't check for id_string uniqueness
            if self.pk is None:
                new_id_string = self.get_unique_id_string(survey.get("id_string"))
                self._id_string_changed = new_id_string != survey.get("id_string")
                survey["id_string"] = new_id_string
                # For flow results packages use the user defined id/uuid
                if self.xls.name.endswith("json"):
                    self.uuid = FloipSurvey(self.xls).descriptor.get("id")
                    if self.uuid:
                        check_xform_uuid(self.uuid)
            elif self.id_string != survey.get("id_string"):
                raise XLSFormError(
                    _(
                        (
                            "Your updated form's id_string '%(new_id)s' must match "
                            "the existing forms' id_string '%(old_id)s'."
                            % {
                                "new_id": survey.get("id_string"),
                                "old_id": self.id_string,
                            }
                        )
                    )
                )
            elif default_name and default_name != survey.get("name"):
                survey["name"] = default_name
            else:
                survey["id_string"] = self.id_string
            self.json = survey.to_json_dict()
            self.xml = survey.to_xml()
            self.version = survey.get("version")
            self.last_updated_at = timezone.now()
            self.title = survey.get("title")
            self.mark_start_time_boolean()
            set_uuid(self)
            self.set_uuid_in_xml()
            self.set_hash()

        if "skip_xls_read" in kwargs:
            del kwargs["skip_xls_read"]

        super().save(*args, **kwargs)

    def file_name(self):
        return os.path.split(self.xls.name)[-1]

# pylint: disable=unused-argument
def set_object_permissions(sender, instance=None, created=False, **kwargs):
    """
    Apply the relevant object permissions for the form to all users who should
    have access to it.
    """
    if instance.project:
        # clear cache
        safe_delete(f"{PROJ_FORMS_CACHE}{instance.project.pk}")
        safe_delete(f"{PROJ_BASE_FORMS_CACHE}{instance.project.pk}")

    # seems the super is not called, have to get xform from here
    xform = XForm.objects.get(pk=instance.pk)

    if created:
        # pylint: disable=import-outside-toplevel
        from onadata.libs.permissions import OwnerRole

        OwnerRole.add(instance.user, xform)

        if instance.created_by and instance.user != instance.created_by:
            OwnerRole.add(instance.created_by, xform)

        # pylint: disable=import-outside-toplevel
        from onadata.libs.utils.project_utils import (
            set_project_perms_to_xform_async,
        )  # noqa

        try:
            transaction.on_commit(
                lambda: set_project_perms_to_xform_async.delay(
                    xform.pk, instance.project.pk
                )
            )
        except OperationalError:
            # pylint: disable=import-outside-toplevel
            from onadata.libs.utils.project_utils import (
                set_project_perms_to_xform,
            )  # noqa

            set_project_perms_to_xform(xform, instance.project)

    if hasattr(instance, "has_external_choices") and instance.has_external_choices:
        instance.xls.seek(0)
        f = sheet_to_csv(instance.xls, "external_choices")
        f.seek(0, os.SEEK_END)
        size = f.tell()
        f.seek(0)

        # pylint: disable=import-outside-toplevel
        from onadata.apps.main.models.meta_data import MetaData

        data_file = InMemoryUploadedFile(
            file=f,
            field_name="data_file",
            name="itemsets.csv",
            content_type="text/csv",
            size=size,
            charset=None,
        )

        MetaData.media_upload(xform, data_file)

post_save.connect(
    set_object_permissions,
    sender=DataDictionary,
    dispatch_uid="xform_object_permissions",
)

# pylint: disable=unused-argument
def save_project(sender, instance=None, created=False, **kwargs):
    """
    Receive XForm project to update date_modified field of the project and on
    the next XHR request the form will be included in the project data.
    """
    instance.project.save()

pre_save.connect(
    save_project, sender=DataDictionary, dispatch_uid="save_project_datadictionary"
)


from datetime import datetime
from session import Session

if __name__ == '__main__':
    print(f'[ START SESSION {datetime.now().strftime("%Y-%m-%d %H:%M:%S")} ]')

    session = Session()

    data = {
        'text': 'python AND ((drf OR restframework) OR django OR backend)',
        'date_from': datetime.now().strftime("%Y-%m-%dT00:00:00"),
        'area': ['66', '1', '2'],  # поиск по Нижнему Новгороду, Москве и СПб
    }

    session.run(data_to_send=data)


import re
from collections import namedtuple
from datetime import datetime

from flask import Blueprint, jsonify, request

from connection import Connection
from db.db_wrapper import DBWrapper
from app.views.utils import create_product_dict

bp = Blueprint('product', __name__, url_prefix='/product')
db_conn = Connection.instance()
product_table = DBWrapper(db_conn, 'product')

@bp.route('/', methods=['POST'])
def create():
    """Add new product"""
    request_data = request.get_json()
    name = request_data['name']
    price = request_data['price']
    # specify columns to insert data into
    Columns = namedtuple('Columns', 'name price date_created')
    new_product = Columns(
        name=name,
        price=price,
        date_created=str(datetime.now())
    )
    product_table.create(new_product)
    return jsonify(
        message="Product Successful"
    ), 201

@bp.route('/', methods=['GET'])
def all_products():
    """Get all products"""
    Columns = namedtuple('Columns', 'id name price date_created')
    product_list = []
    for product in product_table.select_all(Columns):
        product_list.append({
            'id': product[0],
            'name': product[1],
            'price': product[2],
            'date_created': product[3]
        })
    return jsonify(
        products=product_list
    ), 200

@bp.route('/<int:product_id>', methods=['GET'])
def single_product(product_id):
    """return a single product given its id"""
    Columns = namedtuple('Columns', 'id')
    single_product = Columns(id=product_id)
    selected_product = product_table.select(single_product)
    product_dict = create_product_dict(selected_product)
    if product_dict is None:
        return jsonify(message="Product not found"), 404
    return jsonify(
        product=product_dict
    ), 200

@bp.route('/<int:product_id>', methods=['DELETE'])
def delete_product(product_id):
    """delete product from system"""
    Columns = namedtuple('Columns', 'id')
    del_product = Columns(id=product_id)
    product_table.delete(del_product)
    return jsonify(
        message=f"Product {product_id} has been deleted"
    ), 200

@bp.route('/<int:product_id>', methods=['PUT'])
def update_product(product_id):
    """update a product given its id"""
    request_data = request.get_json()
    name = request_data['name']
    price = request_data['price']
    Row_update = namedtuple('Row_Update', 'id name price')
    product_update = Row_update(id=product_id, name=name, price=price)
    product_table.update(product_update)
    return jsonify(
        message=f"Product {product_id} has been updated"
    ), 200