"""NATS implementation of messenger."""

from dataclasses import asdict
import json
from typing import Callable, Dict, Optional

from logzero import logger
from pynats import NATSClient, NATSMessage

from blackcap.configs.base import BaseConfig
from blackcap.messenger.base import BaseMessenger
from blackcap.schemas.message import Message
from blackcap.utils.json_encoders import UUIDEncoder

class NATSMessenger(BaseMessenger):
    """NATS implementation of Messenger."""

    CONFIG_KEY_VAL = "NATS"

    def __init__(self: "NATSMessenger", config: BaseConfig) -> None:
        """Initialize Messenger with app config.

        Args:
            config (BaseConfig): Config to initialize messenger
        """
        self.config = config

    @property
    def client(self: "NATSMessenger") -> NATSClient:
        """NATS client object."""
        client = NATSClient(url=self.config.NATS_ENDPOINT, name=self.config.FLASK_APP)
        client.connect()
        return client

    def publish(self: "NATSMessenger", msg: Dict, topic_id: str) -> str:
        """Publish msg on the GCP Pub/Sub queue.

        Args:
            msg (Dict): Messsag to publish
            topic_id (str): Id of the topic

        Returns:
            str: Id of the published msg
        """
        # Msg must be a bytestring
        msg = json.dumps(msg, cls=UUIDEncoder).encode("utf-8")
        self.client.publish(subject=topic_id, payload=msg)
        self.client.close()
        return "ok"

    def subscribe(
        self: "NATSMessenger",
        callback: Callable,
        sub_id: str,
        timeout: Optional[float] = None,
    ) -> None:
        """Subscribe to a topic.

        Args:
            callback (Callable): Callback to invoke when a msg is received
            sub_id (str): Id of the topic.
            timeout (Union[float, None]): Time to wait for msgs. Defaults to None. # noqa: E501
        """
        try:
            client = self.client
            sub = client.subscribe(subject=sub_id, callback=callback)
            logger.info(f"Subscription created: {sub}")
            client.wait(count=None)
            client.close()
        except Exception as e:
            logger.error(
                f"NATSMessenger subscribe error while pulling messages. Error: {e}"
            )
            client.close()

    def parse_messenger_msg(
        self: "NATSMessenger", messenger_msg: NATSMessage
    ) -> Message:
        """Parse messenger msg to blackcap mesage schema.

        Args:
            messenger_msg (NATSMessage): NATSMessenger message

        Returns:
            Message: Parsed Message
        """
        return Message.parse_raw(messenger_msg.payload.decode("utf-8"))

    def echo_msg(self: "NATSMessenger", msg: NATSMessage) -> None:
        """Echo msgs to stdout.

        Args:
            msg (NATSMessage): Message to echo
        """
        print(asdict(msg))


import datetime
import re

def regex_replace(s, find, replace):
    """Find and replace using regular expressions

    Args:
        s (string): The string containing the text whose be replaced
        find (string): The regex pattern
        replace (string): The string whose replace the pattern

    Returns:
        string: The new string
    """
    return re.sub(find, replace, s)

def elapsed_time(s):
    """Format the elapsed time since a post was published

    Args:
        s (string): The date and time when the post was created

    Returns:
        string: The formated elapsed time
    """
    now = datetime.datetime.now()
    created_at = datetime.datetime.strptime(s, '%Y-%m-%dT%H:%M:%S')
    elapsed = (now - created_at).total_seconds()

    minute = 60
    hour = minute * 60
    day = hour * 24
    month = day * 30
    year = month * 12

    if elapsed <= 1:
        return 'há pouco'
    elif elapsed < minute:
        return 'há {0} segundos'.format(round(elapsed)) if round(elapsed) > 1 else 'há 1 segundo'
    elif elapsed < hour:
        return 'há {0} minutos'.format(round(elapsed / minute)) if round(elapsed / minute) > 1 else 'há 1 minuto'
    elif elapsed < day:
        return 'há {0} horas'.format(round(elapsed / hour)) if round(elapsed / hour) > 1 else 'há 1 hora'
    elif elapsed < month:
        return 'há {0} dias'.format(round(elapsed / day)) if round(elapsed / day) > 1 else 'há 1 dia'
    elif elapsed < year:
        return 'há {0} meses'.format(round(elapsed / month)) if round(elapsed / month) > 1 else 'há 1 mês'

    return 'há {0} anos'.format(round(elapsed / year)) if round(elapsed / year) > 1 else 'há 1 ano'


import torch
import numpy as np
import math
import random
from skimage.metrics import structural_similarity as SSIM
from skimage.metrics import peak_signal_noise_ratio as PSNR
def proto_with_quality(output, target, output_proto, target_proto, criterion, acc_proto, images, num_cluster):
    # InfoNCE loss
    loss = criterion(output, target)

    # ProtoNCE loss
    if output_proto is not None:
        loss_proto = 0
        for proto_out, proto_target in zip(output_proto, target_proto):
            loss_proto += criterion(proto_out, proto_target)
            accp = accuracy(proto_out, proto_target)[0]
            acc_proto.update(accp[0], images[0].size(0))

        # average loss across all sets of prototypes
        loss_proto /= len(num_cluster)
        loss += loss_proto

        # Quality loss
        im_q = torch.split(images[0], split_size_or_sections=1, dim=0)
        im_q = [torch.squeeze(im, dim=0) for im in im_q]
        im_k = torch.split(images[1], split_size_or_sections=1, dim=0)
        im_k = [torch.squeeze(im, dim=0) for im in im_k]

        l_psnr = []
        l_ssim = []
        for i in range(min(len(im_q), len(im_k))):
            k = im_k[i]
            q_index = random.randint(0,i-2)
            if q_index >= i:
                q_index += 1
            q = im_q[q_index]
            psnr_temp = PSNR(k,q)
            if psnr_temp >= 50:
                psnr_temp = 0
            elif psnr_temp <= 30:
                psnr_temp = 1
            else:
                psnr_temp = (50-psnr_temp)/20
            l_psnr.append(psnr_temp)
            l_ssim.append(1-SSIM(k,q))

        loss += np.mean(l_psnr)+np.mean(l_ssim)

    return loss

def accuracy(output, target, topk=(1,)):
    """Computes the accuracy over the k top predictions for the specified values of k"""
    with torch.no_grad():
        maxk = max(topk)
        batch_size = target.size(0)

        _, pred = output.topk(maxk, 1, True, True)
        pred = pred.t()
        correct = pred.eq(target.view(1, -1).expand_as(pred))

        res = []
        for k in topk:
            correct_k = correct[:k].view(-1).float().sum(0, keepdim=True)
            res.append(correct_k.mul_(100.0 / batch_size))
        return res


from contextlib import contextmanager

from ducktape.cluster.remoteaccount import LogMonitor

@contextmanager
def monitor_log(node, log, from_the_beginning=False):
    """
    Context manager that returns an object that helps you wait for events to
    occur in a log. This checks the size of the log at the beginning of the
    block and makes a helper object available with convenience methods for
    checking or waiting for a pattern to appear in the log. This will commonly
    be used to start a process, then wait for a log message indicating the
    process is in a ready state.

    See ``LogMonitor`` for more usage information.
    """
    try:
        offset = 0 if from_the_beginning else int(node.account.ssh_output("wc -c %s" % log).split()[0])
    except Exception:
        offset = 0
    yield LogMonitor(node.account, log, offset)


import databaseConnectivity as db
import os
import shutil
import datetime
import re
import sys
# Should be uncommented with Python 2.7
reload(sys)
sys.setdefaultencoding('utf8')
from botocore.errorfactory import ClientError

# Empty list to store S3obectKeys, documentIds, customerIds and source paths
documentIds = []
S3objectKeys = []
customerIds = []
sources = []

# VALID_CHARACTERS holds the valid characters that a file name can have
VALID_CHARACTERS = set('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!-_ .*\'()')

class InvalidDocuments:
    def __init__(self, customerId, referralId,employeeId,documentId,createdOn,comment,source):
        self.customerId = customerId
        self.referralId=referralId
        self.employeeId=employeeId
        self.documentId=documentId
        self.createdOn = createdOn
        self.comment=comment
        self.source = source

# Define setListNull: takes in variable list arguments and set their values to empty list
def setListNull(*args):
    for arg in args:
        arg[:] = []

def addObjectKeyDocumentId(S3objectKey,documentId,customerId,source):
    documentIds.append(documentId)
    S3objectKeys.append(S3objectKey)
    customerIds.append(customerId)
    sources.append(source)

# Define copyFiles: copies file from the source to the destination
def copyFiles(src,dst,filename,conn):
    destination = os.path.abspath(dst+'/'+filename)
    shutil.copy(src,destination)

# Define createStructureCopyFile: Creates folder structure for a customer and calls copyFiles
def createStructureCopyFile(source,destination,filename,documentId,conn,customerId):
    src = source
    path = destination
    file = removeInvalidCharacters(filename)
    file = renameFile(path, file)

    if not(os.path.exists(path)):  #If destination doesn't exists create folder
        os.makedirs(path)
        copyFiles(src,path,file,conn)
        addObjectKeyDocumentId(path+'/'+file,documentId,customerId,'Appian')

    else:                          #If destination exists copy files from source
        copyFiles(src, path, file,conn)
        addObjectKeyDocumentId(path+'/'+file, documentId,customerId,'Appian')
    return file

def getFileName(bucketName,s3path,s3fileName,s3object):
    fileName = removeInvalidCharacters(s3fileName)
    fileName = renameFile(fileName, s3object, bucketName, s3path)
    return s3path + '/' +fileName,fileName

def uploadDocument(file,bucket,s3ObjectUpload,key):
    s3ObjectUpload.meta.client.upload_file(file,bucket,key,ExtraArgs={"ServerSideEncryption":"AES256"})

# Define logInvalidDocuments: Create enteries in DCM_InvalidDocuments tables for invalid documents

def logInvalidDocuments(conn,invalidDocuments):
    cursor = conn.cursor()
    cursor.execute("INSERT INTO DCM_InvalidDocuments(CustomerId,ReferralId,EmployeeId,DocumentId,CreatedOn,Comment,Source) VALUES (%d, %d, %d, %d, %s, %s, %s)",(invalidDocuments.customerId, invalidDocuments.referralId, invalidDocuments.employeeId, invalidDocuments.documentId, datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), invalidDocuments.comment, invalidDocuments.source))
    conn.commit()
    cursor.close()

def queryCustomerName(conn,customerId=None):
    cursor = conn.cursor()
    sql = """Select CustomerName from Customer """ + \
          ' WHERE Id = ' + str(customerId)

    cursor.execute(sql)
    data = cursor.fetchall()
    customer_name = ''
    for row in data:
        customer_name = str(row[0])

    cursor.close()
    return customer_name

# Define logToFile: Creates a log file
def logToFile(text,filePath):
    if text:
        with open(filePath, 'a') as logFile:
            print >>logFile, text #for version 2.7.5

# Define rreplace: Replaces the last occurrence 'old' string from string s by 'new'
def rreplace(s, old, new):
    return (s[::-1].replace(old,new, 1))[::-1]

# Define getExtension: Returns the filename and extension from a given file name
def splitFileName(filename):

    if os.path.splitext(filename)[1] == '':
        return filename,''
    else:
        dotIndex = filename.rfind('.')
        return filename[:dotIndex],filename[dotIndex + 1:]

# Define checkPath: Return True False based on whether the path exists or not
def checkPath(path,filename):
    pathExists = os.path.exists(r'{0}/{1}'.format(path,filename))
    return  pathExists

def find_between(s, start, end):
    number =(s.split(start))[-1].split(end)[0]
    return rreplace(s,number,str(int(number)+1))

# Define removeInvalidCharacters: Remove invalid characters which are not present in the valid characters list
def removeInvalidCharacters(filename):
    my_new_string = ''.join(filter(VALID_CHARACTERS.__contains__, filename))
    return my_new_string.replace("\\", "")

# Define renameFile: Checks if there exist a file of same name at the given path, If yes renames it
def renameFile(filename,s3,bucketName,s3folderPath):

    new_filename = filename
    key = s3folderPath + '/' + filename

    try:
        s3.head_object(Bucket=bucketName, Key=key)
        if not(re.search('_\([0-9]\)$', filename.split('.')[0])):
            if os.path.splitext(new_filename)[1] == '':
                new_filename = new_filename+'_(1)'
            else:
                name, extension = os.path.splitext(new_filename)
                new_filename = filename.replace(filename, name + '_(1)')

            new_filename=renameFile(new_filename,s3,bucketName,s3folderPath)
        else:
            new_filename=find_between(filename,'(',')')
            new_filename=renameFile(new_filename,s3,bucketName,s3folderPath)

    except ClientError:
        pass

    return new_filename


score = []
x = int(input("students number:"))
a = 0
b = 0
print(x)
for i in range(x):
    y = int(input("students score:"))
    score.append(y)

    b = b+y

print(score)
print("average score", b/x)

for i in score:
    print(i)

for i in score:
    if i > a:
        a = i
print("highest score",a)


from selenium.webdriver.common.by import By
from lib import Lib
from time import sleep
import pyautogui

class TestsPage(Lib):
    'Coloquem os localizadores dos elementos aqui, vai facilitar!'
    URL = 'https://undbclassroom.undb.edu.br'
    __LOGIN_DIRETO_URL = 'https://undbclassroom.undb.edu.br/login/index.php#'
    __URL_ICON = 'https://cdn-icons-png.flaticon.com/512/1521/1521260.png'
    __EMAIL_TEXTBOX =  By.ID, 'username'
    __SENHA_TEXTBOX = By.ID, 'password'
    __ACESSAR_BUTTON = By.CLASS_NAME, 'btn-primary'
    __ESQUECEU_SENHA_BUTTON = By.XPATH, '//a[contains(text(), "Esqueceu o seu usuário ou senha?")]'
    __ID_USUARIO_TEXTBOX = By.ID, 'id_username'
    __BUSCAR_BUTTON = By.NAME, 'submitbuttonusername'
    __ID_EMAIL_TEXTBOX = By.ID, 'id_email'
    __ACESSAR_TEIA = By.XPATH, '//*[@id="page"]/div[2]/div/div/div[1]/div/a'
    __ACESSAR_PORTAL = By.XPATH, '//*[@id="page"]/div[2]/div/div/div[2]/div/a'
    __ACESSAR_SITE = By.XPATH, '//*[@id="page"]/div[2]/div/div/div[3]/div/a'
    __LEMBRAR_USUARIO = By.NAME, "rememberusername"
    __ACESSAR_LINK_PLAYSTORE = By.XPATH, '//*[@id="top-footer"]/div/div/div[1]/h4/a[1]/img'
    __CALENDARIO = By.XPATH, '//*[@id="nav-drawer"]/ul/li[3]/a'
    __SETA_CALENDARIO = By.CLASS_NAME, 'arrow_text'
    __ACESSAR_LINK_APPSTORE = By.XPATH, '//*[@id="top-footer"]/div/div/div[1]/h4/a[2]/img'
    __ACESSAR_EMAIL_INSTITUCIONAL = By.XPATH, '//*[@id="boxForm"]/div/div/div/a'
    __ACESSAR_BARRA_OPCOES = By.XPATH, '//*[@id="action-menu-toggle-1"]'
    __ACESSSAR_SAIR = By.XPATH, '//*[@id="action-menu-1-menu"]/a[7]'
    __EMAIL_GOOGLE = By.ID, 'identifierId'
    __BOTAO_PROXIMO = By.XPATH, '//*[@id="identifierNext"]/div/button'
    __DISCIPLINA_TESTAGEM = By.XPATH, '//*[@id="frontpage-course-list"]/div/div[1]/div[2]/div[3]/div/a'
    __CLICK_SARAIVA = By.XPATH, '//*[@id="module-120666"]/div/div/div[2]/div/a'
    __CLICK_PEARSON = By.XPATH, '//*[@id="module-120667"]/div/div/div[2]/div'
    __EMAIL_NAV = By.CLASS_NAME, 'fa-envelope'
    __NEW_EMAIL = By.CLASS_NAME, 'mail-navbar-menu-compose-link'
    __SEND_EMAIL = By.NAME, 'send'
    __ERROR_DESTINATARIO = By.ID, 'id_error_recipients'
    __PESQUISA_SATISFACAO = By.XPATH, '//*[@id="module-120665"]/div/div/div[2]/div/a/span'
    __RESPONDA_PESQUISA = By.XPATH, '//*[@id="region-main"]/div[1]/div/div/div[2]/div/a'
    __SIM_BUTTTON = By.ID, 'id_multichoice_16594_1'
    __NAO_BUTTTON = By.ID, 'id_multichoice_16594_2'
    __NOTIFICACOES = By.CLASS_NAME, 'slicon-bell'
    __PREFERENCIAS_NOTIFICACOES = By.CLASS_NAME, 'slicon-settings'
    __NOTIFICAO_WEB = By.CLASS_NAME, 'preference-state-status-container'
    __AV_QUALIS = By.XPATH, '//span[contains(text(), "Av Qualis - Produto")]'
    __ADICIONAR_ENVIO = By.XPATH, '//button[contains(text(), "Adicionar envio")]'
    __SALVAR_ENVIO = By.NAME, 'submitbutton'
    __ALERTA_ENVIO = By.CLASS_NAME, 'alert-danger'
    __PERFIL_USER = By.XPATH, '//*[@id="action-menu-toggle-1"]/span/span[2]/span/img'
    __PREFERENCIAS_USER = By.XPATH, '//*[@id="action-menu-1-menu"]/a[6]'
    __MODIFICAR_PERFIL = By.XPATH, '//*[@id="region-main"]/div/div/div/div/div[1]/div/div/div/div[1]/a'
    __SOLTAR_IMAGEM = By.CLASS_NAME, 'dndupload-arrow'
    __UTILIZAR_URL = By.XPATH, '//span[contains(text(), "Utilizar uma URL")]'
    __DOWNLOAD_IMAGE = By.CLASS_NAME, 'fp-login-submit'
    __IMAGEM = By.CLASS_NAME, 'fp-reficons2'
    __FILL_URL = By.XPATH, '//input[@id="fileurl"]'
    __ATV__TESTAGEM = By.XPATH, '//*[@id="module-122898"]/div/div/div[2]/div/a/span'
    __TEST_SEND_AGAIN = By.XPATH, '//*[@id="yui_3_17_2_1_1632675935201_861"]/a[1]/div[1]/div[3]'
    __NOTIFICATION_CHOICE = By.XPATH, '//*[@id="popover-region-container-6150a8ed2ee9f6150a8ed05dd017"]/div[2]/div/div[1]/div[1]/a[1]/div[1]/div[2]'
    __SEARCH_COURSE = By.XPATH, '//*[@id="shortsearchbox"]'
    __GO_TO_COURSE = By.XPATH, '//*[@id="coursesearch"]/fieldset/button'
    __TOTURIAL_COURSE = By.XPATH, '//*[@id="page"]/div[2]/div/div/div[4]/div/a'
    __NIVELAMENTO = By.XPATH, '//*[@id="module-120647"]/div/div/div[2]/div/a/span'
    __NIVELAMENTO_CHECK = By.XPATH, '//*[@id="q10210:3_answer0"]'
    __UNIDADE_APRENDIZADO = By.XPATH, '//*[@id="section-4"]/div/div[1]/div[2]/button'
    __CONF_ACESS = By.XPATH, '//*[@id="themesettings-control"]/span'
    __XPATH_CX_DISLX = By.XPATH, '//*[@id="fonttype"]'
    __XPATH_DISLX_SOP = By.XPATH, '//*[@id="fonttype"]/option[2]'
    __CLASS_PERFIL = By.CLASS_NAME, 'userpicture defaultuserpic'
    __XPATH_PREFERENCIAS = By.XPATH, '//*[@id="actionmenuaction-6"]'
    __XPATH_IDIOMA_PREFERIDO = By.XPATH, '//*[@id="region-main"]/div/div/div/div/div[1]/div/div/div/div[3]/a'
    __XPATH_CX_IDIOMA = By.XPATH, '//*[@id="id_lang"]'
    __XPATH_INGLES = By.XPATH, '//*[@id="id_lang"]/option[1]'
    __NAME_SAVE_IDIOMA = By.NAME, 'Salvar mudanças'
    __XPATH_ICON_MSG = By.XPATH, '//*[@id="message-drawer-toggle-614f5910d1996614f5910a546218"]'
    __XPATH_MSG_FAV = By.XPATH, '//*[@id="yui_3_17_2_1_1632590098721_72"]'
    __XPATH_MSG_SELF_USER = By.XPATH, '//*[@id="view-overview-favourites-target-614f5910d6a43614f5910a546229"]/div[2]/a/div[1]/p'
    __TESTE_SELF_TEXTBOX_ = By.XPATH, '//*[@id="message-drawer-614f5910d6a43614f5910a546229"]/div[3]/div[1]/div[1]/div[2]/textarea'
    __ICON_BARRA_LATERAL = By.CLASS_NAME, 'btn nav-link float-sm-left mr-1'
    __LINK_GEST_QUALD_SFT = By.LINK_TEXT, 'https://undbclassroom.undb.edu.br/course/view.php?id=3835'
    __ICON_PARTICIPANTES = By.LINK_TEXT, 'https://undbclassroom.undb.edu.br/user/index.php?id=3835'
    __LINK_MANUAL_DO_CASE = By.XPATH, '//*[@id="module-29817"]/div/div/div[2]/div/a/span'
    __XPATH_DOWNLOAD_CASE_PAPPER = By.XPATH, '//*[@id="icon"]/iron-icon'
    __LINK_MANUAL_PAPPER = By.XPATH, '//*[@id="module-29818"]/div/div/div[2]/div/a/span'
    __ICON_ID_NOTAS = By.ID, 'actionmenuaction-4'

    def __init__(self, driver):
        self.driver = driver

    def ct_0001(self, user, password):
        #Teste de login
        self.open_page(self.URL)
        self.fill(self.__EMAIL_TEXTBOX, user)
        self.fill(self.__SENHA_TEXTBOX, password)
        self.click(self.__ACESSAR_BUTTON)
        text = 'UNDB Classroom'
        assert text in self.driver.title

    def ct_0002(self, user):
        #Alteração de usuário ou senha pela identificação do usuário
        self.open_page(self.URL)
        self.click(self.__ESQUECEU_SENHA_BUTTON)
        self.fill(self.__ID_USUARIO_TEXTBOX, user)
        self.click(self.__BUSCAR_BUTTON)
        text = 'Se o usuário e o email estiverem corretos um email deve ter sido enviado a você.'
        assert text in self.driver.title

    def ct_0003(self, email):
        #Alteração de usuário ou senha pelo email institucional
        self.open_page(self.URL)
        self.click(self.__ESQUECEU_SENHA_BUTTON)
        self.fill(self.__ID_EMAIL_TEXTBOX, email)
        self.click(self.__BUSCAR_BUTTON)
        text = 'Se o usuário e o email estiverem corretos um email deve ter sido enviado a você.'
        assert text in self.driver.title

    def ct_0004(self):
        #Alteração de Gravatar
        self.ct_0001('insira o usuário!', 'insira sua senha!')
        self.click(self.__PERFIL_USER)
        self.click(self.__PREFERENCIAS_USER)
        self.click(self.__MODIFICAR_PERFIL)
        self.click(self.__SOLTAR_IMAGEM)
        self.click(self.__UTILIZAR_URL)
        self.click(self.__FILL_URL)
        self.fill(self.__FILL_URL, self.__URL_ICON)
        self.click(self.__DOWNLOAD_IMAGE)
        result = self.driver.find_element(*self.__IMAGEM).is_enabled()
        assert result is True

    def ct_0005(self):
        #Biblioteca Online Saraiva
        self.ct_0001('insira o usuário!', 'insira sua senha!')
        self.click(self.__DISCIPLINA_TESTAGEM)
        self.click(self.__CLICK_SARAIVA)
        text = 'Saraiva Online'
        assert text in self.driver.title

    def ct_0006(self):
        #Biblioteca Online Pearson
        self.ct_0001('insira o usuário!', 'insira sua senha!')
        self.click(self.__DISCIPLINA_TESTAGEM)
        self.click(self.__CLICK_PEARSON)
        text = 'Pearson'
        assert text in self.driver.title

    def ct_0007(self):
        #Envio de email sem destinatário
        self.ct_0001('insira o usuário!', 'insira sua senha!')
        self.click(self.__DISCIPLINA_TESTAGEM)
        self.click(self.__EMAIL_NAV)
        self.click(self.__NEW_EMAIL)
        self.click(self.__SEND_EMAIL)
        result = self.driver.find_element(*self.__ERROR_DESTINATARIO).is_enabled()
        assert result is True

    def ct_0008(self):
        #Teste de múltipla escolha. marcando apenas 1 alternativa
        self.ct_0001('insira o usuário!', 'insira sua senha!')
        self.click(self.__DISCIPLINA_TESTAGEM)
        self.click(self.__PESQUISA_SATISFACAO)
        self.click(self.__RESPONDA_PESQUISA)
        self.click(self.__SIM_BUTTTON)
        self.click(self.__NAO_BUTTTON)
        result = self.driver.find_element(*self.__SIM_BUTTTON).is_selected()
        assert result is False

    def ct_0009(self):
        #Desativar alguma notificação
        self.ct_0001('insira o usuário!', 'insira sua senha!')
        self.click(self.__NOTIFICACOES)
        self.click(self.__PREFERENCIAS_NOTIFICACOES)
        buttons = self.driver.find_elements(*self.__NOTIFICAO_WEB)
        buttons[0].click()
        result = buttons[0].is_enabled()
        assert result is True

    def ct_0010(self):
        #Verificação de sucesso no envio
        self.ct_0001('insira o usuário!', 'insira sua senha!')
        self.click(self.__DISCIPLINA_TESTAGEM)
        self.click(self.__AV_QUALIS)
        self.click(self.__ADICIONAR_ENVIO)
        self.click(self.__SALVAR_ENVIO)
        result = self.driver.find_element(*self.__ALERTA_ENVIO).is_enabled()
        assert result is True

    def ct_0011(self):
        #Verificação de sucesso no envio
        self.ct_0001('insira o usuário!', 'insira sua senha!')
        self.click(self.__DISCIPLINA_TESTAGEM)
        self.click(self.__AV_QUALIS)
        self.click(self.__ADICIONAR_ENVIO)
        self.click(self.__SALVAR_ENVIO)
        result = self.driver.find_element(*self.__ALERTA_ENVIO).is_enabled()
        assert result is False

    def ct_0012(self):
        #Verificação de sucesso no envio
        self.ct_0001('insira o usuário!', 'insira sua senha!')
        self.click(self.__DISCIPLINA_TESTAGEM)

        self.click(self.__ATV__TESTAGEM)
        self.click(self.__ADICIONAR_ENVIO)
        self.click(self.__SALVAR_ENVIO)
        result = self.driver.find_element(*self.__ALERTA_ENVIO).is_enabled()

        assert result is True

    def ct_0013(self):
        #Verificação de sucesso no envio
        self.ct_0001('insira o usuário!', 'insira sua senha!')
        self.click(self.__DISCIPLINA_TESTAGEM)

        self.click(self.__ATV__TESTAGEM)
        self.click(self.__ADICIONAR_ENVIO)
        self.click(self.__TEST_SEND_AGAIN)

        self.click(self.__ADICIONAR_ENVIO)
        self.click(self.__SALVAR_ENVIO)
        result = self.driver.find_element(*self.__ALERTA_ENVIO).is_enabled()

        assert result is True

    def ct_0014(self):
        #Verificação de sucesso no envio
        self.ct_0001('insira o usuário!', 'insira sua senha!')
        self.click(self.__ICON_NOTFICATION)

    def ct_0015(self):
        #Verificação de sucesso no envio
        self.ct_0001('insira o usuário!', 'insira sua senha!')
        self.click(self.__ICON_NOTFICATION)
        sleep(2)
        self.click(self.__NOTIFICATION_CHOICE)

    def ct_0016(self):
        #Verificação de sucesso no envio
        self.ct_0001('insira o usuário!', 'insira sua senha!')
        self.fill(self.__SEARCH_COURSE, 'Testagem')
        self.click(self.__GO_TO_COURSE)

    def ct_0017(self):
        #Verificação de sucesso no envio
        self.open_page(self.URL)
        self.click(self.__TOTURIAL_COURSE )

    def ct_0018(self):
        #Verificação de sucesso no envio
        self.ct_0001('insira o usuário!', 'insira sua senha!')
        self.click(self.__DISCIPLINA_TESTAGEM)
        self.click(self.__NIVELAMENTO)

    def ct_0019(self):
        #Verificação de sucesso no envio
        self.ct_0001('insira o usuário!', 'insira sua senha!')
        self.click(self.__DISCIPLINA_TESTAGEM)
        self.click(self.__NIVELAMENTO)
        self.click(self.__NIVELAMENTO_CHECK)

    def ct_0020(self):
        self.ct_0001('insira o usuário!', 'insira sua senha!')
        self.click(self.__DISCIPLINA_TESTAGEM)
        self.click(self.__UNIDADE_APRENDIZADO)

    def ct_0021(self):
        #Redirecionamento do link do site TEIA
        self.open_page(self.URL)
        sleep(2)
        self.click(self.__ACESSAR_TEIA)
        text = 'Espaço Teia'
        assert text in self.driver.title

    def ct_0022(self,):
        #Redirecionamento do link do site do portal academico
        self.open_page(self.URL)
        sleep(2)
        self.click(self.__ACESSAR_PORTAL)
        text = 'Portal do Aluno'
        assert text in self.driver.title

    def ct_0023(self,):
        #Redirecionamento do link do site da UNDB
        self.open_page(self.URL)
        sleep(2)
        self.click(self.__ACESSAR_SITE)
        text = 'Sou UNDB'
        assert text in self.driver.title

    def ct_0024(self,email):
        #Testagem de e-mail institucional
        self.ct_0029()
        self.fill(self.__EMAIL_GOOGLE, email)
        result = self.driver.find_element(*self.__BOTAO_PROXIMO).is_enabled()
        assert result is True

    def ct_0025(self,):
        #Checagem de identificação de usuário
        self.open_page(self.__LOGIN_DIRETO_URL)
        self.click(self.__LEMBRAR_USUARIO)
        result = self.driver.find_element(*self.__LEMBRAR_USUARIO).is_selected()
        assert result is True

    def ct_0026(self,):
        #Redirecionamento do site da google play store
        self.open_page(self.URL)
        self.click(self.__ACESSAR_LINK_PLAYSTORE)
        text = 'UNDB Classroom'
        assert text in self.driver.title

    def ct_0027(self,):
        #Chave de eventos e visualização mensal
        self.ct_0001('user', 'password')
        self.click(self.__CALENDARIO)
        result = self.driver.find_element(*self.__SETA_CALENDARIO).is_enabled()
        assert result is True

    def ct_0028(self,):
        #Redirecionamento do site da app store
        self.open_page(self.URL)
        self.click(self.__ACESSAR_LINK_APPSTORE)
        text = 'UNDB Classroom'
        assert text in self.driver.title

    def ct_0029(self,):
        #Testagem de login com e-mail institucional
        self.open_page(self.URL)
        self.click(self.__ACESSAR_EMAIL_INSTITUCIONAL)
        text = 'Fazer login nas Contas do Google'
        assert text in self.driver.title

    def ct_0030(self,):
        #Testagem caixa de comando "sair"
        self.ct_0001('user', 'password')
        self.click(self.__ACESSAR_BARRA_OPCOES)
        self.click(self.__ACESSSAR_SAIR )
        text = 'UNDB Classroom'
        assert text in self.driver.title

    def ctt_0031(self):
        #TESTAGEM DE FONTE DE DISLEXIA
        self.ct_0001('user', 'password')
        self.click(self.__CONF_ACESS)
        self.click(self.__XPATH_CX_DISLX)
        self.click(self.__XPATH_DISLX_SOP)
        self.click(self.__XPATH_CX_DISLX)

    def ctt_0032(self):
        #SALVAMENTO DA FONTE DE DISLEXIA
        self.ct_0001('user', 'password')
        self.click(self.__CONF_ACESSIBILIDADE)
        self.click(self.__XPATH_CX_DISLX)
        sleep(3)
        self.click(self.__XPATH_DISLX_SOP)
        sleep(3)
        self.click(self.__XPATH_CX_DISLX)
        sleep(2)
        pyautogui.moveTo(1280, 394)
        pyautogui.doubleClick()

    def ctt_0033(self):
        #TESTAGEM MUDAR IDIOMA DO SISTEMA
        self.ct_0001('user', 'password')
        self.click(self.__CLASS_PERFIL)
        self.click(self.__XPATH_PREFERENCIAS)
        self.click(self.__XPATH_IDIOMA_PREFERIDO)
        self.click(self.__XPATH_CX_IDIOMA)
        self.click(self.__XPATH_INGLES)
        self.click(self.__XPATH_CX_IDIOMA)
        self.click(self.__NAME_SAVE_IDIOMA)

    def ctt_0034(self, test_text):
        #TESTAR ENVIAR MENSAGEM PARA SI MESMO
        self.ct_0001('user', 'password')
        self.click(self.__XPATH_ICON_MSG)
        self.click(self.__XPATH_MSG_FAV)
        self.click(self.__XPATH_MSG_SELF_USER)
        self.fill(self.__TESTE_SELF_TEXTBOX_, test_text)

    def ctt_0035(self):
        #TESTAGEM DE MENU LATERAL
        self.ct_0001('user', 'password')
        self.click(self.__ICON_BARRA_LATERAL)

    def ctt_0036(self):
        #ACESSO AOS PARTICIPANTES DA DISCIPLINA
        self.ct_0001('user', 'password')
        self.click(self.__LINK_GEST_QUALD_SFT)
        self.click(self.__ICON_BARRA_LATERAL)
        self.click(self.__ICON_PARTICIPANTES)

    def ctt_0037(self):
        #PREFERÊNCIAS DE USUÁRIO
        self.ct_0001('user', 'password')
        self.click(self.__CLASS_PERFIL)
        self.click(self.__XPATH_PREFERENCIAS)

    def ctt_0038(self):
        #DOWNLOAD MANUAL DO CASE
        self.ct_0001('user', 'password')
        self.click(self.__LINK_MANUAL_DO_CASE)
        self.click(self.__XPATH_DOWNLOAD_CASE_PAPPER)
        pyautogui.press('Enter')

    def ctt_0039(self):
        #DOWNLOAD DO MANUAL DO PAPPER
        self.ct_0001('user', 'password')
        self.click(self.__LINK_MANUAL_PAPPER)
        self.click(self.__XPATH_DOWNLOAD_CASE_PAPPER)
        pyautogui.press('Enter')

    def ctt_0040(self):
        #VISUALIZAÇÃO DE NOTAS
        self.ct_0001('user', 'password')
        self.click(self.__CLASS_PERFIL)
        self.click(self.__ICON_ID_NOTAS)


from bs4 import BeautifulSoup

excluidos = ['Adamo',
'Todos',
'Agrale',
'Ariel',
'Asia',
'Avallone',
'Bianco',
'BRM',
'Caterham',
'CBT',
'Chamonix',
'Chana',
'Changan',
'Cross Lander',
'Daewoo',
'Daihatsu',
'DeLorean',
'Effa',
'Engesa',
'Enseada',
'Envemo',
'Farus',
'Geely',
'Gurgel',
'Hafei',
'Hennessey',
'Hofstetter',
'Iveco',
'Jinbei',
'JPX',
'Koenigsegg',
'KTM',
'Lada',
'Lafer',
'Lincoln',
'Lobini',
'Mahindra',
'Mercury',
'MG',
'Miura',
'Plymouth',
'Pontiac',
'Ragge',
'Rely',
'Saab',
'Saleen',
'Santa Matilde',
'Saturn',
'Seat',
'Shineray',
'SSC',
'TAC',
'W Motors']

def getFabri(html12):
    doc = BeautifulSoup(html12, "html.parser")

    links = doc.find_all(["font"], style="font-size: 15px; font-family:arial; color:black;")

    links_reais = []

    for link in links:
        a_tags = link.find_all('a')
        for a_tag in a_tags:
            text = a_tag.get_text()
            if text not in excluidos:
                links_reais.append(a_tag.get('href'))

    return links_reais


from datetime import datetime
import json
import logging

from bs4 import BeautifulSoup

from db.models import Victim
from net.proxy import Proxy
from .sitecrawler import SiteCrawler

class Ragnar(SiteCrawler):
    actor = "Ragnar"

    def scrape_victims(self):
        with Proxy() as p:
            r = p.get(f"{self.url}", headers=self.headers)

            soup = BeautifulSoup(r.content.decode(), "html.parser")

            script_list = soup.find_all("script")
            # they include the list in javascript code instead of HTML
            # So we have to parse it
            js_victims_raw = ""
            js_marker = "var post_links = "

            for script in script_list:
                script = str(script)
                if js_marker in script:
                    js_victims_raw = script
                    break

            if not js_victims_raw:
                raise Exception(f"js victim list not found (tried to locate '{js_marker}')")

            raw_victim_list = js_victims_raw.split(f"{js_marker}[{{")[1].split(
                "}]"
            )[0]
            victim_list = json.loads(f"[{{{raw_victim_list}}}]")

            for victim in victim_list:
                victim_name = victim["title"]

                if "-" in victim_name:
                    victim_name = victim_name[:victim_name.find("-")]

                published = int(victim["timestamp"])
                published_dt = datetime.utcfromtimestamp(published)

                victim_leak_site = self.url + "/?" + victim["link"] + "/"

                q = self.session.query(Victim).filter_by(
                    url=victim_leak_site, site=self.site)

                if q.count() == 0:
                    # new victim
                    v = Victim(name=victim_name, url=victim_leak_site, published=published_dt,
                               first_seen=datetime.utcnow(), last_seen=datetime.utcnow(), site=self.site)
                    self.session.add(v)
                    self.new_victims.append(v)
                else:
                    # already seen, update last_seen
                    v = q.first()
                    v.last_seen = datetime.utcnow()
                # add the org to our seen list
                self.current_victims.append(v)
            self.session.commit()

        self.site.last_scraped = datetime.utcnow()

        # just for good measure
        self.session.commit()


# Get, change, or verify the version number stored in src/franz/_init__.py
# Usage: python version.py <command> [<args...>]
# Commands:
#   get: print the version number to stdout
#   set V: set the version number to V
#   next: Increment the fifth version segment,
#         make sure 'dev0' is at the end.
#   undev: Strip 'devX' from the version number.
#   verify-dev: Exit with non-zero status if the version
#               does not end in '.devX'.
#   verify-not-dev: Exit with non-zero status if the
#                   version ends in .devX.
import os.path
import re
import sys

BASE_DIR = os.path.dirname(os.path.realpath(__file__)) if '__file__' in globals() else os.getcwd()

INIT = os.path.join(BASE_DIR, 'src', 'franz', '__init__.py')

pattern = re.compile(r'__version__\s*=\s*.*$', re.M)

def get_version():
    """Read the version number from sources."""
    sys.path.insert(0, os.path.join(BASE_DIR, 'src'))
    version_module = __import__('franz')
    return version_module.__version__

def set_version(v):
    """Change the version number to V."""
    old = get_version()
    sys.stderr.write('%s -> %s\n' % (old, v))
    with open(INIT, 'r+') as f:
        text = f.read()
        text = pattern.sub("__version__ = %r" % v, text)
        f.seek(0)
        f.truncate()
        f.write(text)

def remove_dev(v):
    """Remove the devX suffix from V if it is there."""
    if is_dev(v):
        return '.'.join(v.split('.')[:-1])
    return v

def inc(v):
    """Increment the patch level in V."""
    segments = [int(s) for s in remove_dev(v).split('.')]
    # Advance the patch level
    segments[2] = segments[2] + 1
    # Construct a new version string in MAJOR.MINOR.PATCH.dev0 format
    return '.'.join(list(map(str, segments[0:3])) + ['dev0'])

def is_dev(version):
    """Check if a given version number is a dev version."""
    return re.match(r'^.*\.dev\d+$', version)

def main(args):
    """Entry point."""
    cmd = args[1] if len(args) > 1 else 'get'
    if cmd == 'get':
        sys.stdout.write(get_version())
    elif cmd == 'set':
        set_version(args[2])
    elif cmd == 'next':
        set_version(inc(get_version()))
    elif cmd == 'undev':
        set_version(remove_dev(get_version()))
    elif cmd == 'verify-dev':
        if not is_dev(get_version()):
            sys.stderr.write('Expected a .dev0 version number.\n')
            sys.exit(1)
    elif cmd == 'verify-not-dev':
        if is_dev(get_version()):
            sys.stderr.write('Expected a release (non .dev) version number.\n')
            sys.exit(1)
    else:
        sys.stderr.write('Unknown command: %s\n' % cmd)
        sys.exit(1)

if __name__ == '__main__':
    main(sys.argv)


import stripe
import json

from django.conf import settings
from django.shortcuts import redirect

from rest_framework.decorators import api_view,permission_classes
from rest_framework.response import Response
from rest_framework import status
from rest_framework.permissions import IsAuthenticated

from .models import CustomerPayment
from accounts.models import EmailUser

FRONTEND_SUBSCRIPTION_SUCCESS_URL = settings.SUBSCRIPTION_SUCCESS_URL
FRONTEND_SUBSCRIPTION_CANCEL_URL = settings.SUBSCRIPTION_FAILED_URL

webhook_secret = settings.STRIPE_WEBHOOK_SECRET
stripe.api_key = settings.STRIPE_SECRET_KEY

@api_view(['POST'])
@permission_classes([IsAuthenticated])
def create_payment(request):
    try:

        price_id = ''

        if request.data['plan'] == 'basic':
            price_id = 'price_1LD7XcKPyJLjGRo4peZvp8ed'

        if request.data['plan'] == 'premium':
            price_id = 'price_1LD7Y5KPyJLjGRo4B150gCta'

        if CustomerPayment.objects.filter(customer=request.user).exists():
            return Response(status=status.HTTP_409_CONFLICT, data={"detail": "You Already have a subscription , First Cancel that then Subscribe to New one!"})
        else:
            checkout_session = stripe.checkout.Session.create(
            line_items=[
                {
                    'price': price_id,
                    'quantity': 1
                }
            ],
            mode='subscription',
            success_url=FRONTEND_SUBSCRIPTION_SUCCESS_URL +
            "/{CHECKOUT_SESSION_ID}",
            cancel_url=FRONTEND_SUBSCRIPTION_CANCEL_URL,
            customer_email=request.user.email

        )

       # return redirect(checkout_session.url, code=303)
        return Response({"url":checkout_session.url})
    except Exception as err:
        raise err

@api_view(['POST'])
@permission_classes([IsAuthenticated])
def save_customer(request):
    session_id = request.data['session_id']
    session = stripe.checkout.Session.retrieve(session_id)

    customer = stripe.Customer.retrieve(session.customer)

    subscription_details = stripe.Subscription.list(customer=customer)

    subscriptionid = subscription_details.data[0].id
    customerid = subscription_details.data[0].customer
    priceid = subscription_details.data[0].plan.id
    productid = subscription_details.data[0].plan.product

    res = {
        "subscriptionid": subscriptionid,
        "customerid": customerid,
        "priceid" : priceid,
        "productid": productid
    }

    customer_payment = CustomerPayment()
    customer_user = EmailUser.objects.get(email=request.user.email)

    customer_payment.customer = customer_user
    customer_payment.subscriptionid = subscriptionid
    customer_payment.customerid = customerid
    customer_payment.priceid = priceid
    customer_payment.productid = productid

    customer_payment.save()

    return Response(json.dumps(res))

@api_view(['POST','GET'])
@permission_classes([IsAuthenticated])
def cancel_subscription(request):

    customer_payment = CustomerPayment.objects.get(customer=request.user)

    subscription_id = customer_payment.subscriptionid

    stripe.Subscription.delete(subscription_id)

    customer_payment.delete()

    return Response(status=status.HTTP_202_ACCEPTED)

@api_view(['POST', 'GET'])
@permission_classes([IsAuthenticated])
def get_payment_status(request):
    plan = ''
    try:
        customer_payment = CustomerPayment.objects.get(customer=request.user)

        if customer_payment.priceid == 'price_1LD7XcKPyJLjGRo4peZvp8ed':
            plan = 'basic'

        if customer_payment.priceid == 'price_1LD7Y5KPyJLjGRo4B150gCta':
            plan = 'premium'

    except Exception as e :
       plan = 'free'

    return Response({"plan":plan})


# this is the launcher of the batch all series script
# to be executed on a cluster login node
import os

alldatasetsNameFile = "../Results/UCR/workableDatasets.txt"
alldatasetsSizeFile = "../Results/UCR/workableDatasets_size.txt"
with open(alldatasetsNameFile,'r') as f:
    datasets = f.read().strip().split('\n')
with open(alldatasetsSizeFile,'r') as f:
    datasizes = f.read().strip().split('\n')
datasizes = [int(x) for x in datasizes]

for i in range(len(datasets)):
    if (datasizes[i]<1000):
        print(datasets[i]+' full run.')
        os.system("srun -n 32 -N 2 -w c[30-77] -l python3 batchAllSeries " + str(i))
    else:
        print(datasets[i]+' stage_1 run.')
        os.system("srun -n 32 -N 2 -w c[30-77] -l python3 batchAllSeries_stage1 " + str(i))


from torchvision import transforms
from torchvision.datasets import MNIST
import torch
from PIL import Image
import numpy as np
from tqdm import tqdm

class MNISTInvase(MNIST):
    def __init__(self, *args, **kwargs):
        super(MNISTInvase, self).__init__(*args, **kwargs)

    def __getitem__(self, index):
        img, target = self.data[index], self.targets[index]

        # doing this so that it is consistent with all other datasets
        # to return a PIL Image
        img = Image.fromarray(img.numpy(), mode='L')

        if self.transform is not None:
            img = self.transform(img)

        if self.target_transform is not None:
            target = self.target_transform(target)

        img = img.view(-1)
        # Below -1 is due to G being undefined
        return img, target, -1

def one_hot(arr):
    temp = torch.zeros((arr.shape[0], arr.max() + 1))
    temp[torch.arange(arr.shape[0]), arr] = 1
    return temp

def get_mnist(args):
    base_path = "./data-dir"
    batch_size = args.batch_size if args.batch_size else 256
    test_batch_size = args.batch_size if args.batch_size else 512
    num_workers = args.workers if args.workers else 4

    transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize((0.1307,), (0.3081,))
        ])
    train_data = MNISTInvase(base_path, train=True, download=True,
                             transform=transform)
    train_data.means = (0.1307,)
    train_data.stds = (0.3081,)
    train_data.bounds = [0, 1]
    train_data.input_size = 784
    train_data.output_size = 10

    train_data.targets = one_hot(train_data.targets)

    test_data = MNISTInvase(base_path, train=False,
                            transform=transform)
    test_data.targets = one_hot(test_data.targets)
    train_loader = torch.utils.data.DataLoader(train_data, batch_size=batch_size,
                                               shuffle=True, num_workers=num_workers)
    test_loader = torch.utils.data.DataLoader(test_data, batch_size=test_batch_size,
                                              shuffle=False, num_workers=num_workers)

    return train_loader, test_loader


import store
import os
import FTP_Cryptography
import bson
from bson.binary import Binary
import mongodb
from cryptography.fernet import Fernet

files = mongodb.db.files

FILE_DATA = store.FILE_DATA
INFO_FILE = store.INFO_FILE

def check_file_name(file_name):
    return os.path.exists(file_name)

def send_file(socket, file_name):
    file = open(file_name, "rb")
    size = int(os.stat(file_name).st_size)
    socket.send(str(size).encode())

    LISTEN_MSG = socket.recv(1024)

    data = file.read(1024)
    while data:
        socket.send(data)
        data = file.read(1024)

def recv_file(socket, file_name):
    MSG = socket.recv(1024)
    size = int(MSG.decode())

    socket.send("continue".encode())

    time_loop = int(size / 1024)

    if os.path.exists(file_name):
        os.remove(file_name)
    data = b''
    i = 1
    file = open(file_name, "wb")
    while i <= time_loop:
        data = socket.recv(1024)
        file.write(data)
        i = i + 1
    last = size - time_loop*1024
    data = socket.recv(last)
    file.write(data)
    return

# UPLOAD -------------------------------------------------
def send_file_upload(socket, file_name, cryptography):
    file = open(file_name, "rb")
    size = int(os.stat(file_name).st_size)

    INFO_FILE["size"] = size
    INFO_FILE["cryptography"] = cryptography
    INFO_FILE_MSG = store.send_repr(INFO_FILE)
    socket.send(INFO_FILE_MSG)

    LISTEN_MSG = socket.recv(1024)

    data = file.read(1024)
    while data:
        socket.send(data)
        data = file.read(1024)

def recv_file_upload(socket, file_name):
    MSG = socket.recv(1024)
    MSG = store.recv_repr(MSG)
    size = MSG["size"]
    cryptography = MSG["cryptography"]

    socket.send("start".encode())

    time_loop = int(size / 1024)

    file = b''
    data = b''
    i = 1
    while i <= time_loop:
        data = Binary(socket.recv(1024))
        file = file + data
        i = i + 1
    last = size - time_loop*1024
    data = socket.recv(last)
    file = file + data
    insert_file(file_name, file, size, cryptography)
    return

def insert_file(file_name, file, size, cryptography):
    global FILE_DATA
    result = check_file_name_db(file_name)

    if(result != False):
        myquery = {"filename": file_name}
        files.delete_one(myquery)

    FILE_DATA = {
        "_id": file_name,
        "filename": file_name,
        "file": file,
        "size": size,
        "cryptography": cryptography,
    }

    files.insert_one(FILE_DATA)

# DOWNLOAD -------------------------------------------------
def check_file_name_db(filename):
    check_files = {}
    list_files = files.find({"filename": filename})
    for file in list_files:
        check_files = file
    if (check_files != {}):
        return check_files
    else:
        return False

def send_file_download(socket, file_name, cryptography):
    file = check_file_name_db(file_name)
    if(file != False):
        INFO_FILE["size"] = file["size"]
        INFO_FILE["cryptography"] = file["cryptography"]
        INFO_FILE_MSG = store.send_repr(INFO_FILE)
        socket.send(INFO_FILE_MSG)

        LISTEN_MSG = socket.recv(1024)

        socket.send(file['file'])

def recv_file_download(socket, file_name):
    MSG = socket.recv(1024)
    MSG = store.recv_repr(MSG)
    size = MSG["size"]
    cryptography = MSG["cryptography"]

    socket.send("start".encode())

    time_loop = int(size / 1024)

    if os.path.exists(file_name):
        os.remove(file_name)
    save_file = open(file_name, "wb")

    file = b''
    data = b''
    i = 1

    file = Binary(socket.recv(size))

    if(cryptography == True):
        key = FTP_Cryptography.load_key()
        f = Fernet(key)
        data = f.decrypt(file)
    else:
        data = file

    save_file.write(data)


"""
Dataloader building logic.
Author: JiaWei Jiang

This file contains the basic logic of building dataloaders for training
and evaluation processes.
"""
from typing import Any, Union

import numpy as np
import pandas as pd
from omegaconf.dictconfig import DictConfig
from torch.utils.data import DataLoader

from .dataset import DemoDataset

def build_dataloader(
    data: Union[pd.DataFrame, np.ndarray], data_split: str, dataset_cfg: DictConfig, **dataloader_cfg: Any
) -> DataLoader:
    """Cretae and return dataloader.

    Args:
        data: data to be fed into torch Dataset
        data_split: data split
        dataset_cfg: hyperparameters of dataset
        dataloader_cfg: hyperparameters of dataloader

    Returns:
        dataloader: dataloader
    """
    collate = None
    shuffle = dataloader_cfg["shuffle"] if data_split == "train" else False
    dataloader = DataLoader(
        DemoDataset(data, **dataset_cfg),
        batch_size=dataloader_cfg["batch_size"],
        shuffle=shuffle,
        num_workers=dataloader_cfg["num_workers"],
        collate_fn=collate,
        pin_memory=dataloader_cfg["pin_memory"],
        drop_last=dataloader_cfg["drop_last"],
    )

    return dataloader


from optparse import OptionParser
from bigdl.dataset import mnist
from bigdl.dataset.transformer import *
from bigdl.nn.layer import *
from bigdl.nn.criterion import *
from bigdl.optim.optimizer import *
from bigdl.util.common import *

def build_model(class_num):
    model = Sequential()
    model.add(Reshape([1, 28, 28]))
    model.add(SpatialConvolution(1, 6, 5, 5))
    model.add(Tanh())
    model.add(SpatialMaxPooling(2, 2, 2, 2))
    model.add(Tanh())
    model.add(SpatialConvolution(6, 12, 5, 5))
    model.add(SpatialMaxPooling(2, 2, 2, 2))
    model.add(Reshape([12 * 4 * 4]))
    model.add(Linear(12 * 4 * 4, 100))
    model.add(Tanh())
    model.add(Linear(100, class_num))
    model.add(LogSoftMax())
    return model

def get_mnist(sc, data_type="train", location="/tmp/mnist"):

    (images, labels) = mnist.read_data_sets(location, data_type)
    images = sc.parallelize(images)
    labels = sc.parallelize(labels)
    # Target start from 1 in BigDL
    record = images.zip(labels).map(lambda features_label:
                                    Sample.from_ndarray(features_label[0], features_label[1] + 1))
    return record

if __name__ == "__main__":
    parser = OptionParser()
    parser.add_option("-a", "--action", dest="action", default="train")
    parser.add_option("-b", "--batchSize", type=int, dest="batchSize", default="128")
    parser.add_option("-o", "--modelPath", dest="modelPath", default="lenet5/model.470")
    parser.add_option("-c", "--checkpointPath", dest="checkpointPath", default="lenet5")
    parser.add_option("-t", "--endTriggerType", dest="endTriggerType", default="epoch")
    parser.add_option("-n", "--endTriggerNum", type=int, dest="endTriggerNum", default="20")

    (options, args) = parser.parse_args(sys.argv)

    sc = SparkContext(appName="lenet5", conf=create_spark_conf())
    init_engine()

    if options.action == "train":
        def get_end_trigger():
            if options.endTriggerType.lower() == "epoch":
                return MaxEpoch(options.endTriggerNum)
            else:
                return MaxIteration(options.endTriggerNum)

        train_data = get_mnist(sc, "train").map(
            normalizer(mnist.TRAIN_MEAN, mnist.TRAIN_STD))
        test_data = get_mnist(sc, "test").map(
            normalizer(mnist.TEST_MEAN, mnist.TEST_STD))

        optimizer = Optimizer(
            model=build_model(10),
            training_rdd=train_data,
            criterion=ClassNLLCriterion(),
            optim_method=SGD(learningrate=0.01, learningrate_decay=0.0002),
            end_trigger=get_end_trigger(),
            batch_size=options.batchSize)
        optimizer.set_validation(
            batch_size=options.batchSize,
            val_rdd=test_data,
            trigger=EveryEpoch(),
            val_method=[Top1Accuracy()]
        )
        optimizer.set_checkpoint(EveryEpoch(), options.checkpointPath)
        trained_model = optimizer.optimize()
        parameters = trained_model.parameters()
    elif options.action == "test":
        # Load a pre-trained model and then validate it through top1 accuracy.
        test_data = get_mnist(sc, "test").map(
            normalizer(mnist.TEST_MEAN, mnist.TEST_STD))
        model = Model.load(options.modelPath)
        results = model.test(test_data, options.batchSize, [Top1Accuracy()])
        for result in results:
            print(result)
    sc.stop()


##read the markers from file and search in Graingenes.Each one seek for five times.
#If one makrer hasn't been found even by five times,it will be recorded in the file.
import re
import requests
def get_marker(url):
    r = requests.get(url, timeout=20)
    t = r.text
    seq_compiles = re.compile("PCR primers.+?\n")
    primer_list = re.findall(seq_compiles,t)
    if primer_list:
        for i in primer_list:
            primer = re.sub('<.+?>','',i)
            return primer

lost=open("f:/ljy/atom/lost.txt",'w',encoding='utf-8')

with open('F:/ljy/atom/markerf.txt', encoding='utf-8') as m:
    marker_line = m.read().splitlines()

    with open('F:/ljy/atom/primer1.txt', 'w', encoding='utf-8') as f:
        for i in marker_line:
            key = i
            headers = {'user-agent': "Chrome"}
            url = f"https://wheat.pw.usda.gov/cgi-bin/GG3/report.cgi?class=marker;query=*{key}*;name={key}"
            count =0
            while count < 5:
                try:

                    f.write(f'{key}:',get_marker(url))
                    count=6
                except:
                    count = count + 1
            if count ==5:
                print(f"{key} is lost with three times of search.")
                lost.write(f"{key}")
                lost.write("\n")

with open('F:/ljy/atom/marker2.txt', encoding='utf-8') as m:
    marker_line = m.read().splitlines()
    compiles = re.compile("5'.+?3'")
    with open('F:/ljy/atom/primer2.txt', 'w', encoding='utf-8') as f:
        for i in marker_line:
            key = i
            headers = {'user-agent': "Chrome"}
            url = f"https://wheat.pw.usda.gov/cgi-bin/GG3/report.cgi?class=marker;query=*{key}*;name={key}"
            count =0
            while count < 5:
                try:

                    f.write(f'{key}:',get_marker(url))
                    count=6
                except:
                    count = count + 1
            if count ==5:
                print(f"{key} is lost with five times of search.")
                lost.write(f"{key}")
                lost.write("\n")

lost.close()

print('OK!')


import numpy as np
import random
import re

from rasa_core.policies.policy import Policy
from rasa_core.actions.action import ACTION_LISTEN_NAME

#----------------------------------------------------------------------
# gReflections, a translation table used to convert things you say
#    into things the computer says back, e.g. "I am" --> "you are"
#----------------------------------------------------------------------
gReflections = {
"am"   : "are",
"was"  : "were",
"i"    : "you",
"i'd"  : "you would",
"i've"  : "you have",
"i'll"  : "you will",
"my"  : "your",
"are"  : "am",
"you've": "I have",
"you'll": "I will",
"your"  : "my",
"yours"  : "mine",
"you"  : "me",
"me"  : "you"
}

#----------------------------------------------------------------------
# gPats, the main response table.  Each element of the list is a
#  two-element list; the first is a regexp, and the second is a
#  list of possible responses, with group-macros labelled as
#  %1, %2, etc.
#----------------------------------------------------------------------
gPats = [
[r'(.*) (suicide|kill myself)(.*)',
[ "I'm really sorry to hear that. I think you should get some help. Try visiting www.mentalhealthhelpline.ca/",
  "Please be careful. Try seeking help here: https://suicidepreention.ca/need-help/",
  "I'm getting worried. You should try contacting someone at 1-866-925-5454 or good2talk.ca"]],

[r'quit',
[  "Thank you for talking with me.",
    "Good-bye.",
    "Thank you, that will be $150.  Have a good day!"]]
]

class ElizaPolicy(Policy):
    def __init__(self, featurizer=None, max_history=None):
        self.keys = list(map(lambda x:re.compile(x[0], re.IGNORECASE),gPats))
        self.values = list(map(lambda x:x[1],gPats))
        super(ElizaPolicy, self).__init__(featurizer, max_history)

    def predict_action_probabilities(self, tracker, domain):
        if tracker.latest_action_name == ACTION_LISTEN_NAME:
            txt = tracker.latest_message.text
            response = self.respond(txt)

            return np.zeros(domain.num_actions)
        else:
            return np.zeros(domain.num_actions)

    def train(self, X, domain, **kwargs):
        # type: (ndarray, List[int], Domain, **Any) -> None
        """Trains the policy on given training data."""
        pass

    #----------------------------------------------------------------------
    # translate: take a string, replace any words found in dict.keys()
    #  with the corresponding dict.values()
    #----------------------------------------------------------------------
    def translate(self, str, dict):
        words = str.lower().split()
        keys = dict.keys();
        for i in range(0,len(words)):
            if words[i] in keys:
                words[i] = dict[words[i]]
        return ' '.join(words)

    #----------------------------------------------------------------------
    #  respond: take a string, a set of regexps, and a corresponding
    #    set of response lists; find a match, and return a randomly
    #    chosen response from the corresponding list.
    #----------------------------------------------------------------------
    def respond(self, str):
        # find a match among keys
        for i in range(0, len(self.keys)):
            match = self.keys[i].match(str)
            if match:
                # found a match ... stuff with corresponding value
                # chosen randomly from among the available options
                resp = random.choice(self.values[i])
                # we've got a response... stuff in reflected text where indicated
                pos = resp.find('%')
                while pos > -1:
                    num = int(resp[pos+1:pos+2])
                    resp = resp[:pos] + \
                        self.translate(match.group(num),gReflections) + \
                        resp[pos+2:]
                    pos = resp.find('%')
                # fix munged punctuation at the end
                if resp[-2:] == '?.': resp = resp[:-2] + '.'
                if resp[-2:] == '??': resp = resp[:-2] + '?'
                return resp
        return ''

    @classmethod
    def load(cls, path, featurizer, max_history):
        return cls()


import os
import json
import time
from S3utility.s3_notification_info import parse_activity_data
from provider import digest_provider, download_helper, email_provider, utils
from activity.objects import Activity

class activity_ValidateDigestInput(Activity):
    "ValidateDigestInput activity"

    def __init__(self, settings, logger, client=None, token=None, activity_task=None):
        super(activity_ValidateDigestInput, self).__init__(
            settings, logger, client, token, activity_task
        )

        self.name = "ValidateDigestInput"
        self.version = "1"
        self.default_task_heartbeat_timeout = 30
        self.default_task_schedule_to_close_timeout = 60 * 30
        self.default_task_schedule_to_start_timeout = 30
        self.default_task_start_to_close_timeout = 60 * 5
        self.description = (
            "Download digest file input from the bucket, parse it, check for "
            + "valid data, and raise an error if it is invalid."
        )

        # Track some values
        self.input_file = None
        self.digest = None

        # Local directory settings
        self.directories = {
            "TEMP_DIR": os.path.join(self.get_tmp_dir(), "tmp_dir"),
            "INPUT_DIR": os.path.join(self.get_tmp_dir(), "input_dir"),
        }

        # Track the success of some steps
        self.statuses = {"build": None, "valid": None, "email": None}

        # Load the config
        self.digest_config = digest_provider.digest_config(
            self.settings.digest_config_section, self.settings.digest_config_file
        )

    def do_activity(self, data=None):
        """
        Activity, do the work
        """
        if self.logger:
            self.logger.info("data: %s" % json.dumps(data, sort_keys=True, indent=4))

        self.make_activity_directories()

        # parse the data with the digest_provider
        real_filename, bucket_name, bucket_folder = parse_activity_data(data)

        # Download from S3
        self.input_file = download_helper.download_file_from_s3(
            self.settings,
            real_filename,
            bucket_name,
            bucket_folder,
            self.directories.get("INPUT_DIR"),
        )

        # Parse input and build digest
        self.statuses["build"], self.digest = digest_provider.build_digest(
            self.input_file,
            self.directories.get("TEMP_DIR"),
            self.logger,
            self.digest_config,
        )

        # Approve files for emailing
        self.statuses["valid"], error_messages = digest_provider.validate_digest(
            self.digest
        )

        if not self.statuses.get("build") or not self.statuses.get("valid"):
            # Send error email
            self.statuses["email"] = self.email_error_report(
                real_filename, error_messages
            )
            self.log_statuses(self.input_file)
            return self.ACTIVITY_PERMANENT_FAILURE

        self.log_statuses(self.input_file)
        return True

    def log_statuses(self, input_file):
        "log the statuses value"
        self.logger.info(
            "%s for input_file %s statuses: %s"
            % (self.name, str(input_file), self.statuses)
        )

    def email_error_report(self, filename, error_messages):
        "send an email on error"
        datetime_string = time.strftime(utils.DATE_TIME_FORMAT, time.gmtime())
        body = email_provider.simple_email_body(datetime_string, error_messages)
        subject = error_email_subject(filename)
        sender_email = self.settings.digest_sender_email

        recipient_email_list = email_provider.list_email_recipients(
            self.settings.digest_validate_error_recipient_email
        )

        connection = email_provider.smtp_connect(self.settings, self.logger)
        # send the emails
        for recipient in recipient_email_list:
            # create the email
            email_message = email_provider.message(subject, sender_email, recipient)
            email_provider.add_text(email_message, body)
            # send the email
            email_provider.smtp_send(
                connection, sender_email, recipient, email_message, self.logger
            )
        return True

def error_email_subject(filename):
    "email subject for an error email"
    return "Error processing digest file: {filename}".format(filename=filename)