from django.db import models
from django.conf import settings
from .choices import EXERCISE_TYPES,RECOVERY_TYPE

class Team(models.Model):
    name = models.CharField(max_length=45)
    logo = models.ImageField(default = 'default_pic.png',upload_to = "logo_pics")
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete = models.CASCADE)

    def __str__(self):
        return self.name

class Player(models.Model):
    owner = models.OneToOneField(settings.AUTH_USER_MODEL, related_name= "player_creator", on_delete = models.CASCADE)
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete = models.CASCADE)
    weight = models.FloatField()
    height = models.FloatField()
    shoe_size = models.FloatField()
    vetemenent_size = models.FloatField()
    number = models.IntegerField()
    post = models.IntegerField()
    foot_of_strong = models.CharField(max_length = 5)
    team = models.ForeignKey(Team, related_name='players',on_delete = models.CASCADE)

    def __str__(self):
        return ("{} {}".format(self.user.first_name,self.user.last_name))

class Exercise(models.Model):
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete = models.CASCADE,blank=True,null=True )
    exercise_name = models.CharField(max_length = 45)
    exercise_type = models.CharField(max_length = 45 ,choices = EXERCISE_TYPES)
    width = models.FloatField()
    height = models.FloatField()
    duration = models.FloatField()
    sequences = models.FloatField()
    recovery = models.FloatField()
    recovery_type  = models.CharField(max_length = 45 , choices = RECOVERY_TYPE)
    NMI = models.IntegerField()
    effort_perception = models.IntegerField()

class Session(models.Model):
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete = models.CASCADE)
    team = models.ForeignKey(Team, on_delete= models.CASCADE)
    session_date = models.DateTimeField()
    exercises = models.ManyToManyField(Exercise)

class Question(models.Model):
	question_label = models.CharField(max_length= 255)
	question_text  = models.TextField()
	question_scale = models.PositiveIntegerField()
	question_positive = models.BooleanField()

	def __str__(self):
		return self.question_label

class Survey(models.Model):
	timing_choices = (
			("Anterior","Anterior"),
			("Posterior","Posterior")
		)
	survey_label = models.CharField(max_length= 255)
	survey_questions = models.ManyToManyField(Question)
	survey_timing = models.CharField(max_length = 20, choices= timing_choices)

	def __str__(self):
		return self.survey_label

class Answer(models.Model):
    player = models.ForeignKey(Player, on_delete = models.CASCADE)
    session = models.ForeignKey(Session, on_delete = models.CASCADE)
    question = models.ForeignKey(Question,on_delete = models.CASCADE)
    question_answer = models.FloatField()
    answer_valid = models.BooleanField(default = True)

    def __str__(self):
        return self.question_answer

    class Meta:
        unique_together = ('player','session','question')


import glob2, threading, time
import multiprocessing as mp
from os.path import abspath, dirname, join, sep
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

jobs = []

class WatcherHandler(FileSystemEventHandler):
    """
    custom watchdog event handler
    """
    def __init__(self, watcher):
        self.watcher = watcher
        pass

    def on_modified(self, event):
        print('modified: %s' % (event.src_path))
        self.watcher.task_func()

class Watcher(object):
    """
    Provides a wat for each task to have it's own controlable class
    """
    def __init__(self, task_name, pattern, task_func):
        self.task_name = task_name
        self.pattern = pattern
        self.task_func = task_func

        self.directories = []
        self.paths = []

        self.build()

    def build(self):
        # get our paths from our glob
        paths = glob2.glob(self.pattern)

        for path in paths:
            fqn = abspath(path)
            parts = fqn.split(sep)
            directory = sep.join(parts[:-1])

            if directory not in self.directories:
                self.directories.append(directory)

            self.paths.append(fqn)

    def start(self):
        try:
            p = mp.Process(target=self._listener, name=self.task_name)
            p.daemon = True
            jobs.append(p)
            p.start()
        except KeyboardInterrupt:
            p.terminate()

        for i in range(len(jobs)):
            jobs[i].join()

    def _listener(self):
        observer = Observer()
        observer.daemon = True

        event_handler = WatcherHandler(self)

        for directory in self.directories:
            print(directory)
            print("Adding directory '{0}' to watch for files".format(directory))
            observer.schedule(event_handler, path = directory, recursive = False)

        observer.start()
        print('Monitoring for changes...')

        try:
            while 1:
                time.sleep(1)
        except KeyboardInterrupt:
            print('Unscheduling monitors...')

            observer.unschedule_all()
            observer.stop()

            print('Observer stopped')

def x():
    print('here x')

def x2():
    print('here x2')

if __name__ == '__main__':
    w = Watcher('testing', './testing/*.txt', x)
    w2 = Watcher('another', './testing/**/file[2-9].txt', x2)

    w.start()
    w2.start()


#file transfer code taken from https://www.thepythoncode.com/article/send-receive-files-using-sockets-python

# modules for file transfer
import socket
import tqdm
import os

filename = "appetite_data.txt"

SEPARATOR = "<SEPARATOR>"
BUFFER_SIZE = 4096

host = "192.168.1.9"

port = 5001

filesize = os.path.getsize(filename)

s = socket.socket()

s.connect((host, port))

s.send(f"{filename}{SEPARATOR}{filesize}".encode())

progress = tqdm.tqdm(range(filesize), f"Sending {filename}", unit="B", unit_scale=True, unit_divisor=1024)

with open(filename, "rb") as f:
    while True:
        bytes_read = f.read(BUFFER_SIZE)
        if not bytes_read:
            break
        s.sendall(bytes_read)
        progress.update(len(bytes_read))

s.close()


import logging
from logging.handlers import TimedRotatingFileHandler

import common_utils.ConfigurationSetups

import common_utils.constants
import configurations
import os

class LoggingConfig():
    """Retrieve the Logger Configurations from the project properties"""
    def __init__(self):
        config = configurations.projectConfigurations.config
        self.loggerName=config['Logs']['logger_default_name']
        self.logFilePath = config['Logs']['log_file_path']
        self.logFilePrefix = str(config['Logs']['log_file_prefix']+'-id-'+self.getUniqueId()+'.log')
        self.logFormatterString='[%(asctime)s] %(levelname)s in %(module)s: %(message)s'
        self.logTimeRotator=config['Logs']['log_time_rotator']
        self.logTimeInterval=int(config['Logs']['log_time_interval'])
        self.logFileHandlerLevel=self.__logMapper(config['Logs']['log_level_file'])
        self.logStreamHandlerLevel = self.__logMapper(config['Logs']['log_level_console'])
        self.logType =config['Logs']['logger_type']

    def getUniqueId(self):
        """
        Get the container Id from docker runtime. If not running on Docker get the process id

        :return: unique Id for logger name
        """
        if common_utils.constants.Constants.CONTAINER_ID not in os.environ.keys() or os.environ[
            common_utils.constants.Constants.CONTAINER_ID] is None:
            return str(os.getpid())
        else:
            return str(os.environ[common_utils.constants.Constants.CONTAINER_ID]) + '_' + str(os.getpid())

    def __logMapper(self, logLevel_str):
        """
        Set different logging level according to the given string

        :param logLevel_str: Given String for logging levl

        :return: logging level
        """
        if logLevel_str.upper() == "CRITICAL" or logLevel_str.upper() == "FATAL":
            return logging.CRITICAL
        elif logLevel_str.upper() == "ERROR":
            return logging.ERROR
        elif logLevel_str.upper() == "WARNING" or logLevel_str.upper() == "WARN":
            return logging.WARNING
        elif logLevel_str.upper() == "INFO":
            return logging.INFO
        elif logLevel_str.upper() == "DEBUG":
            return logging.DEBUG
        else:
            return logging.NOTSET

class MLLogging(metaclass=common_utils.ConfigurationSetups.Singleton):
    """A Singleton Class to initialize Logger which is to be used the project modules"""
    def __init__(self):
        """
        Initialize Logging Module with all the given configurations for this environment
        """
        MLLogging.loggerConfig = LoggingConfig()
        MLLogging.logger = initloggerConfiguration(logConfig=MLLogging.loggerConfig)

    def getLog(name=None):
        """
        Returns the logger that was created

        :return: return the created logger object
        """
        return MLLogging.logger

def initloggerConfiguration(logConfig=None):
    """
    Initialize a logger object from the configuration properties

    :param logConfig: logger configurations

    :return: logger object
    """
    logger = logging.getLogger(logConfig.loggerName)
    logger.setLevel(logging.DEBUG)
    if not os.path.exists(logConfig.logFilePath):
        os.makedirs(logConfig.logFilePath)
    if logConfig.logType == "TimedRotatingFileHandler":
        fh = logging.handlers.TimedRotatingFileHandler(logConfig.logFilePath + logConfig.logFilePrefix,when=logConfig.logTimeRotator,interval=logConfig.logTimeInterval)
    else:
        fh = logging.FileHandler(logConfig.logFilePath + logConfig.logFilePrefix)
    fh.setLevel(logConfig.logFileHandlerLevel)
    ch = logging.StreamHandler()
    ch.setLevel(logConfig.logStreamHandlerLevel)
    formatter = logging.Formatter(logConfig.logFormatterString)
    fh.setFormatter(formatter)
    ch.setFormatter(formatter)
    logger.addHandler(fh)
    logger.addHandler(ch)
    return logger

MLLogging()


import  xlrd
class read:
    def get_msg(self):
        book=xlrd.open_workbook(r'D:\shuju.xlsx')
        sheet=book.sheets()[0]
        return sheet
if __name__=='__main__':
    s=read().get_msg()
    # print(s)
    # print(s.nrows)
    # print(s.cell(3,1).value)

    for i in range(s.nrows):
        # for j in range(s.ncols):
            # print(s.cell(i,j).value,'\t',end='')
        # print('')
        li=s.row_values(i)
        print(li)


from __future__ import division
from scipy import misc
import numpy as np
import matplotlib.pyplot as plt
from scipy.fftpack import fft, ifft, fftfreq
from scipy.interpolate import interp1d
import math
import time

imagename='phantom256.gif'

liczba_emiterow = 600
rozpietosc_katowa_punktow = math.radians(150)  #w stopniach, w radianach: alfa*math.pi/180
kat_kroku = 1

def plotLineLow(x0, y0, x1, y1, img, img2, img_size):
    suma=0
    licznik=1
    dx = x1 - x0
    dy = y1 - y0
    yi = 1
    if dy < 0:
        yi = -1
        dy = -dy
    D = 2*dy - dx
    y = y0

    for x in range(x0,x1):
        if (x>0) and (x<img_size) and (y>0) and (y<img_size):
            suma=suma+img[x,y]
            img2[x, y] = 127
            licznik=licznik+1
        if D > 0:
           y = y + yi
           D = D - 2*dx
        D = D + 2*dy
    return suma/licznik

def plotLineHigh(x0, y0, x1,y1, img, img2, img_size):
    suma=0
    licznik=1
    dx = x1 - x0
    dy = y1 - y0
    xi = 1
    if dx < 0:
        xi = -1
        dx = -dx
    D = 2*dx - dy
    x = x0

    for y in range(y0,y1):
        if (x>0) and (x<img_size) and (y>0) and (y<img_size):
            suma=suma+img[x,y]
            img2[x, y] = 127
            licznik=licznik+1
        if D > 0:
            x = x + xi
            D = D - 2*dy
        D = D + 2*dx
    return suma/licznik

def bresenham(x0, y0, x1, y1, img, img2, img_size):
    x0 = round(x0)
    y0 = round(y0)
    x1 = round(x1)
    y1 = round(y1)
    suma=0
    if abs(y1 - y0) < abs(x1 - x0):
        if x0 > x1:
            suma=suma+plotLineLow(x1, y1, x0, y0, img, img2, img_size)
        else:
            suma=suma+plotLineLow(x0, y0, x1, y1, img, img2, img_size)
    else:
        if y0 > y1:
            suma=suma+plotLineHigh(x1, y1, x0, y0, img, img2, img_size)
        else:
            suma=suma+plotLineHigh(x0, y0, x1, y1, img, img2, img_size)
    return suma

def inversed_bresenham(x1,y1,x2,y2,sin,img2, img2_licznik,img_size):
    x1 = round(x1)
    y1 = round(y1)
    x2 = round(x2)
    y2 = round(y2)
    d=0
    dx=0
    dy=0
    ai=0
    bi=0
    xi=0
    yi=0
    x = x1
    y = y1
    if (x1 < x2):
         xi = 1
         dx = x2 - x1
    else:
         xi = -1
         dx = x1 - x2
    if (y1 < y2):
         yi = 1
         dy = y2 - y1
    else:
         yi = -1
         dy = y1 - y2
    if (dx > dy):
         ai = (dy - dx) * 2
         bi = dy * 2
         d = bi - dx
         while (x != x2):
             if (d >= 0):
                 x += xi
                 y += yi
                 d += ai
             else:
                 d += bi
                 x += xi
             if (x>0) and (x<img_size) and (y>0) and (y<img_size):
                 img2[x, y] = img2[x, y] + sin
                 img2_licznik[x, y] += 1
    else:
         ai = ( dx - dy ) * 2
         bi = dx * 2
         d = bi - dy
         while (y != y2):
             if (d >= 0):
                 x += xi
                 y += yi
                 d += ai
             else:
                 d += bi
                 y += yi
             if (x>0) and (x<img_size) and (y>0) and (y<img_size):
                 img2[x,y]=img2[x,y]+sin
                 img2_licznik[x,y] += 1

#Polozenie detektorow
def odbiornik(r, nr_odbiornika, alfa=0.0):
    x = r * math.cos(alfa - rozpietosc_katowa_punktow/2 + nr_odbiornika*rozpietosc_katowa_punktow/(liczba_emiterow-1))
    y = r * math.sin(alfa - rozpietosc_katowa_punktow/2 + nr_odbiornika*rozpietosc_katowa_punktow/(liczba_emiterow-1))
    return x+r, y+r     #srodek nie w (0,0) a (r,r)

# Pojedynczy emiter
def emiter(r, alfa=0.0):
    x = r * math.cos(alfa)
    y = r * math.sin(alfa)
    return x+r, y+r     #srodek nie w (0,0) a (r,r)

# Tyle samo emiterow co detektorow
def emitery(r, nr_odbiornika, alfa=0.0):
    x = r * math.cos(alfa + math.pi - rozpietosc_katowa_punktow / 2 + nr_odbiornika * rozpietosc_katowa_punktow / (liczba_emiterow - 1))
    y = r * math.sin(alfa + math.pi - rozpietosc_katowa_punktow / 2 + nr_odbiornika * rozpietosc_katowa_punktow / (liczba_emiterow - 1))

    return x+r, y+r     #srodek nie w (0,0) a (r,r)

def get_sinogram(img, img_size):
    theta = np.linspace(0., 180., math.floor(180/kat_kroku), endpoint=False)
    r = img_size/2

    sin_img = np.zeros((liczba_emiterow, len(theta)))
    index = 0
    for step in theta:
        emiters = []
        for i in range(liczba_emiterow):
            emiters.append(emitery(r=r, nr_odbiornika=i, alfa=math.radians(step)))

        odbiorniki = []
        for i in range(liczba_emiterow):
            odbiorniki.append(odbiornik(r=r, nr_odbiornika=i, alfa=math.radians(step)))

        img2 = np.copy(img)
        for i in range(liczba_emiterow):
            odbiornik_x, odbiornik_y = odbiorniki[i]
            emiter_x, emiter_y = emiters[liczba_emiterow - i - 1]
            sin_img[i][index] = bresenham(emiter_x, emiter_y, odbiornik_x, odbiornik_y, img, img2, img_size)
        index += 1
        #if krok%10 == 0:
        #    io.imsave("./bresenham/kat"+str(krok*kat_kroku)+".jpg", img2)
    return sin_img

def inversed_Radon(radon_image, theta, ax3, ax4):
    output_size = radon_image.shape[0]
    th = (np.pi / 180.0) * theta    # radians

    # uzupelnij zerami zeby rozmiar byl: potega dwojki <= 64
    projection_size_padded = max(64, int(2 ** np.ceil(np.log2(2 * radon_image.shape[0]))))
    pad_width = ((0, projection_size_padded - radon_image.shape[0]), (0, 0))
    img = np.pad(radon_image, pad_width, mode='constant', constant_values=0)

    f = fftfreq(projection_size_padded).reshape(-1, 1)   # digital frequency, discrete Fourier transform
    fourier_filter = 2 * np.abs(f)                       # ramp filter

    projection = fft(img, axis=0) * fourier_filter
    radon_filtered = np.real(ifft(projection, axis=0))
    radon_filtered = radon_filtered[:radon_image.shape[0], :]

    ax3.set_title("Filtered Radon")
    ax3.imshow(radon_filtered, cmap="gray")
    plt.show(block=False)
    plt.pause(0.01)

    reconstructed = np.zeros((output_size, output_size))
    mid_index = radon_image.shape[0] // 2
    [X, Y] = np.mgrid[0:output_size, 0:output_size]
    xpr = X - int(output_size) // 2
    ypr = Y - int(output_size) // 2

    # Reconstruct image by interpolation
    for i in range(len(theta)):
        t = ypr * np.cos(th[i]) - xpr * np.sin(th[i])
        x = np.arange(radon_filtered.shape[0]) - mid_index
        interpolant = interp1d(x, radon_filtered[:, i], kind='cubic',
                               bounds_error=False, fill_value=0)
        backprojected = interpolant(t)
        reconstructed += backprojected

        if (i%5 == 0):
            ax4.imshow(reconstructed, cmap="gray")
            plt.show(block=False)
            plt.pause(0.01)

    radius = output_size // 2
    reconstruction_circle = (xpr ** 2 + ypr ** 2) <= radius ** 2
    reconstructed[~reconstruction_circle] = 0.

    return reconstructed * np.pi / (2 * len(th))

image = misc.imread(imagename, flatten=True).astype('float64')
img_size = max(image.shape)
image = misc.imresize(image, (img_size, img_size))
sinogram = get_sinogram(image, img_size)

fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(12, 6))

ax1.set_title("Original image")
ax1.imshow(image, cmap="gray")
ax2.set_title("Radon transform\n(Sinogram)")
ax2.imshow(sinogram, cmap="gray", aspect='auto')
ax4.set_title("Reconstruction")

reconstruction_fbp = inversed_Radon(sinogram, np.linspace(0., 180., math.floor(180/kat_kroku), endpoint=False), ax3, ax4)
reconstruction_fbp = misc.imresize(reconstruction_fbp, (img_size,img_size))

error = reconstruction_fbp - image
print('FBP rms reconstruction error: %.3g' % np.sqrt(np.mean(error**2)))

ax3.set_title("Filtered Reconstruction")
ax3.imshow(reconstruction_fbp, cmap="gray")
plt.show()


"""
:mod:`tsne`

Returns t-SNE plot given a set of features.
"""

import numpy as np
from sklearn.manifold import TSNE

from . import plot

def reduce_features_in_two_dimensions_using_tsne(density_contrasts_with_label,
                                                 n_components=2, perplexity=50.0,
                                                 early_exaggeration=4.0, learning_rate=1000.0, n_iter=1000,
                                                 metric="euclidean", init="random",
                                                 random_state=None):
    """
    Tune t-SNE hyperparameters and reduce feature space to two dimensions using t-SNE.
    We use the t-SNE algorithm inbuilt in the scikit-learn package.

    Args:
        density_contrasts_with_label (ndarray): labeled density contrasts for in and out particles.
        n_components (int): Dimensions of reduced feature space. Default is 2.
        perplexity (float): Related to number of nearest neighbours used in other "manifold" algorithms.
           t-SNE not very sensitive to this. Usually in the range 5-50. Default is 50.
        early_exaggeration (float): Controls how tight clusters in original space will be in embedded space.
           t-SNE not very sensitive to this. Default is 4.0.
        learning_rate (float): This can be critical. Should be between 100 and 1000. Default is 1000.
        n_iter (int): Max number of iteration for optimization. Default is 1000.
        metric (str): Metric of similarity in feature space. Default is "euclidean", which is
             squared euclidean distance.
        init (str):"random" or "pca". Default is "random".
        random_state: Psuedo Random Number generator seed control.

    Returns:
        labeled_features_two_dimensions (2Darray): Two-dimensional features array which features as columns.
    """
    if len(density_contrasts_with_label[0]) > 52:
        raise ValueError("t-SNE dimensionality reduction doesn't work well with more than 50 features. Perform "
                         "further dimensionality reduction (e.g.PCA) before t-SNE plot.")

    tsne_algo = TSNE(n_components=n_components, perplexity=perplexity,
                                early_exaggeration=early_exaggeration, learning_rate=learning_rate, n_iter=n_iter,
                                metric=metric, init=init,
                                random_state=random_state)

    y = tsne_algo.fit_transform(density_contrasts_with_label[:, :-1])
    labeled_features_two_dimensions = np.column_stack((y, density_contrasts_with_label[:, -1]))
    return labeled_features_two_dimensions

def split_features_in_and_out(features_with_label):
    """Split labeled features ndarray in two ndarrays, features of 'in' particles and features of 'out' particles."""

    features_in = features_with_label[features_with_label[:, -1] == 1]
    features_out = features_with_label[features_with_label[:, -1] == -1]
    return features_in, features_out

def get_tsne_plot(features_with_label, n_components=2, perplexity=50.0,
                  early_exaggeration=4.0, learning_rate=1000.0, n_iter=1000,
                  metric="euclidean", init="random",
                  random_state=None, title=False):
    """
    Returns t-sne plot given a set of labeled features and t-SNE hyperparameters.
    It reduces the feature space to two-dim (see :func:`reduce_features_in_two_dimensions_using_tsne`),
    it splits the features in the two classes (see :func:`split_features_in_and_out`)
    and it gives the t-SNE plot using :func:`plot_tsne_features` in :mod:`plot`.
    """
    features_tsne = reduce_features_in_two_dimensions_using_tsne(features_with_label,
                                                                 n_components=n_components, perplexity=perplexity,
                                                                 early_exaggeration=early_exaggeration,
                                                                 learning_rate=learning_rate, n_iter=n_iter,
                                                                 metric=metric, init=init,
                                                                 random_state=random_state)
    features_in, features_out = split_features_in_and_out(features_tsne)

    fig = plot.plot_tsne_features(features_in, features_out)
    number_of_particles = len(features_tsne)
    number_of_filters = features_with_label.shape[1]-1

    if title is True:
        fig.suptitle("t-SNE of " + str(number_of_filters) +
                     " features for " + str(number_of_particles) + " particles")
    return fig


# -*- coding: utf-8 -*-
# !/usr/bin/env python -v
'''
Created on 2015-7-25

@author: Chen Lin
'''

import os
import codecs

import nltk
from nltk import word_tokenize

import utils

project_git_paht = 'F:/benchmark/Git/python'
project_path = os.path.join(os.path.dirname(__file__), '..')
log_path = os.path.join(project_path, "../log_data")
test_path = os.path.join(project_path, "../workdir")

key_word_in_commit = ['algorithm',
                      'deadlock', 'dead lock', ' race ', 'synchro', ' async', ' atomic',
                      'memory leak', 'null pointer', 'buffer overflow', 'heap overflow', 'dangling', 'integer overflow',
                      'garbage collection', 'garbage',
                      'exception', 'error handling', 'ValueError', 'value error', 'type error', 'TypeError',
                      'compilation error',
                      'copy paste', 'initialization', 'default value', 'missing ', 'variable resolution',
                      'compatibility',
                      'typo', 'syntax error', 'syntaxerror',
                      'unicode', 'encode', 'decode',
                      'security', 'secure', 'password', 'oauth', 'ssl', 'vulnerab',
                      'optimiz', 'performance', 'speed up', 'speed-up',
                      'reboot', 'crash', ' hang ', 'restart',
                      'document', 'docstring',
                      ' test',
                      ' add', ' remove', ' delet', ' move', ' refactor',
                      'fix', 'correct', 'bug', 'error', 'fault',
                      'merge',
                      'cleanup', 'clean up']
bug_keyword = ['type error', 'typeerror', 'value error', 'valueerror', 'keyerror', 'RuntimeError', 'runtime error',
               'unicode error', 'unicodeerror', 'EncodeError', 'DecodeError',
               'ioerror', 'io error', 'import error', 'importerror', 'AttributeError', 'attribute error',
               'OverflowError', 'MemoryError', 'ValidationError ', 'IntegrityError ', 'ZeroDivisionError', 'NameError',
               'BadHeaderError', 'LookupError', 'RedirectCycleError', 'IndexError']
error_keyword = ['StandardError', 'StopIteration', 'GeneratorExit', 'KeyboardInterrupt',
                 'StopAsyncIteration',
                 'OverflowError', 'ZeroDivisionError', 'FloatingPointError',
                 'BufferError',
                 'LookupError', 'IndexError', 'KeyError',
                 'AssertionError',
                 'AttributeError',
                 'TypeError',
                 'ValueError',
                 'EOFError',
                 'ImportError',
                 'MemoryError', 'ReferenceError',
                 'NameError', 'UnboundLocalError',
                 'RuntimeError', 'NotImplementedError', 'RecursionError',
                 'SyntaxError', 'IndentationError', 'TabError',
                 'UnicodeError', 'UnicodeEncodeError', 'UnicodeDecodeError', 'UnicodeTranslateError',
                 'DeprecationWarning', 'PendingDeprecationWarning', 'RuntimeWarning',
                 'SyntaxWarning', 'UserWarning', 'FutureWarning', 'ImportWarning',
                 'UnicodeWarning', 'BytesWarning', 'ResourceWarning',
                 'EnvironmentError', 'IOError', 'OSError', 'WindowsError', 'VMSError', 'SystemError', 'BlockingIOError',
                 'ChildProcessError', 'ConnectionError', 'BrokenPipeError', 'ConnectionAbortedError',
                 'ConnectionRefusedError', 'ConnectionResetError', 'FileExistsError', 'FileNotFoundError',
                 'InterruptedError', 'IsADirectoryError', 'NotADirectoryError', 'PermissionError',
                 'ProcessLookupError', 'TimeoutError'
                 ]
issue_type = ['Algo', 'Conc', 'Mem', 'Prog', 'Typo', 'Codec', 'Sec', 'Perf', 'Fail', 'Unkn']
activity_type = ['Implementation',  # fea,int,plat
                 'Maintenance',  # bug,dbg,mntn,cross
                 'ModuleManagement',  # add,mmod,rmod,split
                 'Legal',  # lic
                 'NonFunctionalSrcChange',  # cln,trpl,rfct,indent
                 'SCSManagement',  # brnch,ext,merge,ver,scs
                 'MetaProgram']  # bui,tst,doc,data,intl
commit_type = ['Branch', 'BugFix', 'Build', 'CleanUp', 'Lega', 'Cross', 'Data', 'Debug', 'Documentation',
               'External', 'FeatureAdd', 'Indentation', 'Initialization', 'Internationalization',
               'SourceControl', 'Maintenance', 'Merge', 'ModuleAdd', 'ModuleMove', 'ModuleRemove',
               'PlatformSpecific', 'Refactoring', 'Rename', 'Testing', 'TokenReplace', 'Versioning']
defect_category = ['DEFECTs', 'CRASHs', 'MEMORYs', 'PERFORMANCEs', 'TYPOs', 'CODECs']
behavior_category = ['CORRECTION', 'DOCUMENT', 'TESTING', 'REFACTORING']

def get_src_path(project):
    return os.path.join(project_git_paht, project)

def contain_word(keyword):
    if keyword in key_word_in_commit:
        return True
    return False

def get_raw(projectName):
    logs = utils.get_logs(log_path)
    project_log_file = logs[projectName]
    text_path = nltk.data.find(project_log_file)
    raw = open(text_path, 'rU').read().decode('utf-8', 'ignore').lower()
    return raw

def set_project_info(projectName):
    number_key_words = {}
    average_length_of_a_commit_log = 0
    average_number_of_files_a_commit = 0
    average_number_of_source_files_a_commit = 0
    members = []
    lifespan = 0

def init_project_info():
    info = {}
    # 'keywords' is excluded
    info['metaTag'] = ['authors', 'number_of_commits', 'commit_length', 'number_of_files', 'number_of_source_files',
        'number_of_non_source_files', 'avgLoC', 'avgNoF', 'avgNoSF', 'avgNoNSF', 'Algo', 'Conc', 'Mem', 'Prog',
        'Typo', 'Codec', 'Compat', 'Sec', 'Perf', 'Fail', 'Unkn', 'BugFix', 'Testing', 'Documentation', 'Refactoring',
        'FixTest', 'FixAndTest', 'AugmentTest', 'OtherTest', 'ModifyTest', 'ErrorCategory', 'BugCategory']

    info['authors'] = set()
    info['number_of_commits'] = 0
    info['keywords'] = {k: 0 for k in key_word_in_commit}
    info['commit_length'] = 0
    info['number_of_files'] = 0
    info['number_of_source_files'] = 0
    info['number_of_non_source_files'] = 0
    info['avgLoC'] = 0.0
    info['avgNoF'] = 0.0
    info['avgNoSF'] = 0.0
    info['avgNoNSF'] = 0.0
    info['ActivityCategory'] = ['BugFix', 'Documentation', 'Refactoring']
    for tag in info['ActivityCategory']:
        info[tag] = []
    info['ErrorCategory'] = ['StopIteration', 'StopAsyncIteration', 'ArithmeticError', 'AssertionError', 'AttributeError',
        'BufferError', 'EOFError', 'ImportError', 'LookupError', 'MemoryError', 'NameError', 'ReferenceError',
        'RuntimeError', 'SyntaxError', 'TypeError', 'ValueError', 'UnicodeError', 'Warning']
    for tag in info['ErrorCategory']:
        info[tag] = []
    info['BugCategory'] = ['Algo', 'Conc', 'Mem', 'Prog', 'Typo', 'Codec', 'Compat', 'Sec', 'Perf', 'Fail', 'Unkn']
    for tag in info['BugCategory']:
        info[tag] = []
    info['TestCategory'] = ['FixTest', 'FixAndTest', 'AugmentTest', 'OtherTest', 'ModifyTest']
    for tag in info['TestCategory']:
        info[tag] = []
    return info

def get_a_commit_data(aCommit):
    '''
        parameter aCommit is a list containing two data
        aCommit[0] is commit-data: parent_tag;tag;author;date_time;log_title;log_body
        aCommit[1] is modified file list
    '''
    commitData = {}
    commitData['sourceFiles'] = []
    commitData['otherFiles'] = []
    commitData['parent_tag'] = ''
    commitData['current_tag'] = ''
    commitData['author'] = ''
    commitData['date_time'] = ''
    commitData['log_title'] = ''
    commitData['log_body'] = ''
    commitData['tags'] = []
    commitData['tags_on_error'] = []
    # tmp[0] is meta-data: parent_tag;tag;author;date_time;log_title;log_body
    # tmp[1] is modified file list
    tmp = aCommit.split("****")
    meta_data = tmp[0].split(";;")
    # print(meta_data)
    if len(meta_data) > 4:
        commitData['parent_tag'] = meta_data[0].strip()
        commitData['current_tag'] = meta_data[1].strip()
        commitData['author'] = meta_data[2].strip()
        commitData['date_time'] = meta_data[3].strip()
        commitData['log_title'] = meta_data[4].strip()
        # some commits have no log body
        if len(meta_data) > 5:
            commitData['log_body'] = meta_data[5].strip()
        else:
            commitData['log_body'] = ''
    if len(tmp) > 1:
        modified_files = tmp[1]
        (commitData['sourceFiles'], commitData['otherFiles']) = count_files(modified_files)
    if not commitData['current_tag'] is '':
        commitData['tags'] = tag_a_commit(commitData)
        commitData['tags_on_error'] = tag_commit_by_buildin_exception(commitData)
    # print(commitData['current_tag'] + " : " + str(commitData['tags']))
    return commitData

def tag_commit_by_buildin_exception(log):
    title = log['log_title']
    body = log['log_body']
    commit = title + ' ' + body
    tag = set()
    if commit.find('stopiteration')>=0:
        tag.add('StopIteration')
    if commit.find('StopAsyncIteration')>=0:
        tag.add('StopAsyncIteration')
    if commit.find('floatingpointerror')>=0 or commit.find('overflowerror')>=0 or commit.find('zerodivisionerror')>=0:
        tag.add('ArithmeticError')
    if commit.find('assertionerror')>=0:
        tag.add('AssertionError')
    if commit.find('attributeerror')>=0:
        tag.add('AttributeError')
    if commit.find('buffererror')>=0:
        tag.add('BufferError')
    if commit.find('eoferror')>=0:
        tag.add('EOFError')
    if commit.find('importerror')>=0:
        tag.add('ImportError')
    if commit.find('lookuperror')>=0 or commit.find('indexerror')>=0 or commit.find('keyerror')>=0:
        tag.add('LookupError')
    if commit.find('memoryerror')>=0:
        tag.add('MemoryError')
    if commit.find('nameerror')>=0 or commit.find('unboundlocalerror')>=0:
        tag.add('NameError')
    if commit.find('referenceerror')>=0:
        tag.add('ReferenceError')
    if commit.find('runtimeerror')>=0 or commit.find('notimplementederror')>=0 or commit.find('recursionerror')>=0:
        tag.add('RuntimeError')
    if commit.find('syntaxerror')>=0 or commit.find('indentationerror')>=0 or commit.find('taberror')>=0:
        tag.add('SyntaxError')
    if commit.find('typeerror')>=0:
        tag.add('TypeError')
    if commit.find('valueerror')>=0:
        tag.add('ValueError')
    if commit.find('unicodeerror')>=0 or commit.find('unicodedecodeerror')>=0 or commit.find(
        'unicodeencodeerror')>=0 or commit.find('unicodetranslateerror')>=0:
        tag.add('UnicodeError')
    if commit.find('deprecationwarning')>=0 or commit.find('pendingdeprecationwarning')>=0 or commit.find(
        'runtimewarning')>=0 or commit.find('syntaxwarning')>=0 or commit.find('userwarning')>=0 or commit.find(
        'futurewarning')>=0 or commit.find('importwarning')>=0 or commit.find('unicodewarning')>=0 or commit.find(
        'byteswarning')>=0 or commit.find('resourcewarning')>=0:
        tag.add('Warning')
    return tag

def tag_a_commit(log):
    title = log['log_title']
    body = log['log_body']
    commit = title + ' ' + body
    tag = set()
    ##classify bugs
    tests = 0
    otherTests = 0
    for file in log['sourceFiles']:
        if file.find('test') >= 0:
            tests += 1
    for file in log['otherFiles']:
        if file.find('test') >= 0:
            otherTests += 1
    if (commit.find('fix') >= 0 or commit.find('error') >= 0 or commit.find('bug') >= 0 or commit.find(
            'fault') >= 0) and len(log['sourceFiles']) > 0:
        # tag activity BugFix
        tag.add('BugFix')
        # tag bug cause
        if commit.find('algorithm') >= 0 or commit.find('logic') >= 0:
            tag.add('Algo')
        elif commit.find('deadlock') >= 0 or commit.find('dead lock') >= 0 or commit.find(
                ' race ') >= 0 or commit.find('synchro') >= 0 or commit.find(' async') >= 0 or commit.find(
            ' atomic') >= 0 or commit.find('thread') >= 0:
            tag.add('Conc')
        elif commit.find('memory leak') >= 0 or commit.find('null pointer') >= 0 or commit.find(
                'buffer overflow') >= 0 or commit.find('heap overflow') >= 0 or commit.find(
            'dangling') >= 0 or commit.find('integer overflow') >= 0 or commit.find(
            'garbage') >= 0 or commit.find('overflowerror') >= 0 or commit.find('overflow error') >= 0:
            tag.add('Mem')
        elif commit.find('exception') >= 0 or commit.find('error handling') >= 0 or commit.find(
                'ValueError') >= 0 or commit.find('value error') >= 0 or commit.find(
            'type error') >= 0 or commit.find('TypeError') >= 0 or commit.find(
            'compilation error') >= 0 or commit.find('initialization') >= 0 or commit.find(
            'default value') >= 0 or commit.find('variable resolution') >= 0 or commit.find(
            'missing ') >= 0 or commit.find('handle ') >= 0 or commit.find('handling') >= 0 or commit.find(
            'wrong ') >= 0 or commit.find('exceed') >= 0:
            tag.add('Prog')
        elif commit.find('typo') >= 0 or commit.find('syntax error') >= 0 or commit.find('syntaxerror') >= 0:
            tag.add('Typo')
        elif commit.find('unicode') >= 0 or commit.find('encode') >= 0 or commit.find('decode') >= 0:
            tag.add('Codec')
        elif commit.find('2to3') >= 0 or commit.find('compatibility') >= 0:
            tag.add('Compat')
        # tag bug impact
        if commit.find('security') >= 0 or commit.find('password') >= 0 or commit.find(
                'oauth') >= 0 or commit.find('ssl') >= 0 or commit.find('secure') >= 0 or commit.find(
            'vulnera') >= 0:
            tag.add('Sec')
        elif commit.find('optimiz') >= 0 or commit.find('performance') >= 0 or commit.find(
                'speed up') >= 0 or commit.find('speed-up') >= 0:
            tag.add('Perf')
        elif commit.find('reboot') >= 0 or commit.find('crash') >= 0 or commit.find(' hang ') >= 0 or commit.find(
                'restart') >= 0:
            tag.add('Fail')
        if len(tag) <= 1:  # tag must has at least a tag 'BugFix'
            # print(commit)
            tag.add('Unkn')
        if tests > 0 and tests < len(log['sourceFiles']):
            tag.add('FixAndTest')
        elif tests > 0:
            tag.add('FixTest')
    elif len(log['sourceFiles']) == 0 and otherTests > 0:
        tag.add('ModifyTest')
    elif len(log['sourceFiles']) > 0:
        if tests > 0 and tests < len(log['sourceFiles']):
            tag.add('AugmentTest')
        elif tests > 0:
            tag.add('ModifyTest')
    elif otherTests > 0:
        tag.add('OtherTest')

    if commit.find('document') >= 0 or commit.find('docstring') >= 0 or commit.find(' doc ') >= 0 or commit.find(
            'doc:') >= 0:
        tag.add('Documentation')
    if commit.find('refactor') >= 0:
        tag.add('Refactoring')

    return tag

def count_keywords(statistics, log):
    for keyword in key_word_in_commit:
        if log.find(keyword) >= 0:
            statistics['keywords'][keyword] += 1

def count_files(modified_files):
    others = []
    sources = []
    files = modified_files.split("\n")
    for file in files:
        if not file is None:
            if file.endswith(".py"):
                sources.append(file)
            elif file is not '':
                others.append(file)
    return (sources, others)

def get_commit_statistics(projectName):
    info = init_project_info()
    commits = get_commits(projectName)
    if len(commits) == 0:
        return info
    for commit in commits:
        info['number_of_commits'] += 1
        info['commit_length'] += len(commit['log_body'])
        info['number_of_source_files'] += len(commit['sourceFiles'])
        info['number_of_non_source_files'] += len(commit['otherFiles'])
        if not commit['author'] is '':
            info['authors'].add(commit['author'])
        count_keywords(info, commit['log_title'] + ' ' + commit['log_body'])
        for tag in commit['tags']:
            info[tag].append((commit['parent_tag'], commit['current_tag']))
        for tag in commit['tags_on_error']:
            info[tag].append((commit['parent_tag'], commit['current_tag']))
    info['number_of_files'] = info['number_of_source_files'] + info['number_of_non_source_files']
    info['avgLoC'] = info['commit_length'] / info['number_of_commits']
    info['avgNoF'] = float(info['number_of_files']) / info['number_of_commits']
    info['avgNoSF'] = float(info['number_of_source_files']) / info['number_of_commits']
    info['avgNoNSF'] = float(info['number_of_non_source_files']) / info['number_of_commits']

    return info

def get_commits(projectName):
    logs = []
    raw = get_raw(projectName).encode('utf-8')
    commits = raw.split("********")
    logs = [get_a_commit_data(commit) for commit in commits[1:]]
    return logs

def get_words(projectName):
    raw = get_raw(projectName)
    tokens = word_tokenize(raw)
    filted_tokens = []
    for token in tokens:
        if token.endswith(".py"): continue
        if token.find("//") >= 0: continue
        if len(token) < 2: continue
        filted_tokens.append(token)
    tag_words = nltk.pos_tag(filted_tokens)
    filter = set(
        ['JJ', 'JJR', 'JJS', 'LS', 'MD', 'NN', 'NNS', 'NNP', 'NNPS', 'RB', 'RBR', 'RBS', 'RP', 'SYM', 'VB', 'VBD',
         'VBG', 'VBN', 'VBP', 'VBZ', 'WRB'])
    words = [w[0].lower() for w in tag_words if w[1] in filter]
    return words

def get_sentences(projectName):
    raw = get_raw(projectName)
    sents = nltk.sent_tokenize(raw)
    return sents

def ouput_classified_bugs(project, output):
    print('parsing projects: ', project)
    info = get_commit_statistics(project)
    line = 'bug category,parent tag,current tag\n'
    for tag in info['BugCategory']:
        for log in info[tag]:
            line += tag + ',' + str(log[0]) + ',' + str(log[1]) + '\n'
    fp = codecs.open(output, 'w', 'utf-8')
    fp.write(line)
    fp.close()

def ouput_fix_test(project, output):
    print('parsing projects: ', project)
    info = get_commit_statistics(project)
    line = 'test_category,parent tag,current tag\n'
    for tag in info['TestCategory']:
        for log in info[tag]:
            line += tag + ',' + str(log[0]) + ',' + str(log[1]) + '\n'
    fp = codecs.open(output, 'w', 'utf-8')
    fp.write(line)
    fp.close()

def ouput_classfied_errors(project, output):
    print('parsing projects: ', project)
    info = get_commit_statistics(project)
    line = 'error_category,parent tag,current tag\n'
    for tag in info['ErrorCategory']:
        for log in info[tag]:
            line += tag + ',' + str(log[0]) + ',' + str(log[1]) + '\n'
    fp = codecs.open(output, 'w', 'utf-8')
    fp.write(line)
    fp.close()

#projects = ["sublime-text-git"]
#projects = ['django']
#projects = utils.get_project_names('F:/benchmark/Git/python')
# cmd = 'git archive --format=tar 1.0 | gzip > v1.0.tar.gz'

#projects = ["django"]
# for project in projects:
    # output = os.path.join(test_path, project + '_classified_errors.csv')
    # ouput_classfied_errors(project,output)
    # pass


from bs4 import BeautifulSoup
import requests
import os
import codecs
class PageCheck:

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

    def pageExiste(self):
        existe=False
        response=requests.get(self.url)
        status_code=response.status_code
        if (status_code==200):
            regex=self.url.split(".")
            if "wikipedia" in regex:
                existe=True

        return existe

    def urlChek(self):
        regex1=self.url.split(":")
        regex=" ".join(regex1)
        if(regex.startswith("https")==False):
            urlp="https://en.wikipedia.org/wiki/"+ self.url

        elif(self.pageExiste()):
            urlp=self.url
        else:
            urlp=" "
        return urlp


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

import re
from copy import deepcopy
from collections import OrderedDict
from pyeda.inter import exprvar
from pyeda.inter import expr2truthtable
from pynq import Register
from pynq import MMIO
from .constants import *

__author__ = "Parimal Patel"
__copyright__ = "Copyright 2018, Xilinx"
__email__ = "pynq_support@xilinx.com"

class BooleanGenerator:
    """Class for the Boolean generator.

    This class can implement any combinational function. Since
    each LUT5 takes 5 inputs, the basic function that users can implement is
    5-input, 1-output boolean function. However, by concatenating multiple
    LUT5 together, users can implement complex boolean functions.

    There are 16 5-LUTs, so users can implement at most 16 basic boolean
    functions at a specific time.

    Attributes
    ----------
    expressions : list/dict
        The boolean expressions, each expression being a string.
    input_pins : list
        A list of input pins used by the generator.
    output_pins : list
        A list of output pins used by the generator.

    """

    def __init__(self, intf_spec_name='BG_SPECIFICATION'):
        """Return a new Boolean generator object.

        The available input pins are data pins DIN0 - DIN15,

        The available output pins can be DOUT0-DOUT15.

        The input boolean expression can be of the following format:
        `DOUT4 = DIN0 & DIN1 | DIN2`.

        If no input boolean expression is specified, the default function
        implemented is `DIN0 & DIN1 & DIN2 & DIN3`.

        Parameters
        ----------
        No parameters are required

        """
        if type(intf_spec_name) is str:
            self.intf_spec = eval(intf_spec_name)
        elif type(intf_spec_name) is dict:
            self.intf_spec = intf_spec_name
        else:
            raise ValueError("Interface specification has to be str or dict.")

        self.bg_mmio = MMIO(0x43c00000,(4*64))
        self.bg_sel_mmio = MMIO(0x41200000)
		# Parameters to be cleared at reset
        self.expressions = dict()
        self.output_pins = list()
        self.input_pins = list()

    def setup(self, expressions):
        """Configure CFGLUTs with new boolean expression.

        Parameters
        ----------
        expressions : list/dict
            The boolean expression to be configured.

        """

        if isinstance(expressions, list):
            for i in range(len(expressions)):
                self.expressions['Boolean expression {}'.format(i)] = \
                    deepcopy(expressions[i])
        elif isinstance(expressions, dict):
            self.expressions = deepcopy(expressions)
        else:
            raise ValueError("Expressions must be list or dict.")

        mailbox_addr = self.bg_mmio.base_addr
        mailbox_regs = [Register(addr) for addr in range(
            mailbox_addr, mailbox_addr + 4 * 64, 4)]
        for offset in range(0, 32, 2):
            mailbox_regs[offset][31:0] = 0x1FFFFFF

        for index in range (0,32):
            self.bg_mmio.write((index*4),mailbox_regs[index][31:0])
        self.bg_mmio.write((62*4),0x0000ffff)
        self.bg_mmio.write((63*4),(0x0000ffff | 0x80000000))
        self.bg_mmio.write((63*4), 0) # was 0x0000ffff)

        for expr_label, expression in self.expressions.items():
            if not isinstance(expression, str):
                raise TypeError("Boolean expression has to be a string.")

            if "=" not in expression:
                raise ValueError(
                    "Boolean expression must have form Output = Function.")

            # Parse boolean expression into output & input string
            expr_out, expr_in = expression.split("=")
            expr_out = expr_out.strip()
            if expr_out in self.output_pins:
                raise ValueError("The same output pin should not be driven by "
                                 "multiple expressions.")
            self.output_pins.append(expr_out)
            if expr_out in self.intf_spec['output_pins']:
                output_pin_num = self.intf_spec[
                    'output_pins'][expr_out]
            else:
                raise ValueError("Invalid output pin {}.".format(expr_out))

            # Parse the used pins preserving the order
            non_unique_inputs = re.sub("\W+", " ", expr_in).strip().split(' ')
            unique_input_pins = list(OrderedDict.fromkeys(non_unique_inputs))
            if not 1 <= len(unique_input_pins) <= 5:
                raise ValueError("Expect 1 - 5 inputs for each LUT.")
            input_pins_with_dontcares = unique_input_pins[:]
            self.input_pins += unique_input_pins
            self.input_pins = list(set(self.input_pins))

            # Need 5 inputs - any unspecified inputs will be don't cares
            for i in range(len(input_pins_with_dontcares), 5):
                expr_in = '(({0}) & X{1})|(({0}) & ~X{1})'.format(
                    expr_in, i)
                input_pins_with_dontcares.append('X{}'.format(i))

            # Map to truth table
            p0, p1, p2, p3, p4 = map(exprvar, input_pins_with_dontcares)
            expr_p = expr_in

            # Use regular expression to match and replace whole word only
            for orig_name, p_name in zip(input_pins_with_dontcares,
                                         ['p{}'.format(i) for i in range(5)]):
                expr_p = re.sub(r"\b{}\b".format(orig_name), p_name, expr_p)

            truth_table = expr2truthtable(eval(expr_p))

            # Parse truth table to send
            truth_list = str(truth_table).split("\n")
            truth_num = 0
            for i in range(32, 0, -1):
                truth_num = (truth_num << 1) + int(truth_list[i][-1])

            # Get current boolean generator bit enables
            bit_enables = self.bg_mmio.read(62*4)
            cfg_enables = self.bg_mmio.read(63*4)

            # Generate the input selects based on truth table and bit enables
            truth_table_inputs = [str(inp) for inp in truth_table.inputs]
            for i in range(5):
                lsb = i * 5
                msb = (i + 1) * 5 - 1
                if truth_table_inputs[i] in unique_input_pins:
                    if truth_table_inputs[i] in self.intf_spec[
                        'input_pins'] and truth_table_inputs[i] \
                            in self.intf_spec['input_pins']:
                        input_pin_ix = self.intf_spec[
                            'input_pins'][truth_table_inputs[i]]
                    else:
                        raise ValueError("Invalid input pin "
                                         "{}.".format(truth_table_inputs[i]))
                else:
                    input_pin_ix = 0x1f
                mailbox_regs[output_pin_num * 2][msb:lsb] = input_pin_ix

            mailbox_regs[output_pin_num * 2 + 1][31:0] = truth_num
            mailbox_regs[62][31:0] = bit_enables
            mailbox_regs[62][output_pin_num] = 0

            mailbox_regs[63][31:0] = cfg_enables
            mailbox_regs[63][output_pin_num] = 1

        int_to_write_62=int(mailbox_regs[62][31:0])
        int_to_write_63=int(mailbox_regs[63][31:0])
        for index in range (0,32):
            self.bg_mmio.write((index*4),(mailbox_regs[index][31:0]))
        self.bg_mmio.write((62*4), int_to_write_62)
        self.bg_mmio.write((63*4),(int_to_write_63 | 0x80000000))
        self.bg_mmio.write((63*4), int_to_write_63)

    def read(self, regnum):
        """Read Boolean Generator memory mapped registers.

        Parameters
        ----------
        regnum : integer

        """
        if type(regnum) is int:
            self.reg_num = regnum
        else:
            raise ValueError("Register number must be integer")

        if regnum not in range (0,64):
            raise ValueError("Register number must be in range of 0-63")

        return (self.bg_mmio.read(self.reg_num*4))

    def clear(self, expressions):
        """Clear output pins so new function on same pin can be defined.

        Parameters
        ----------
        expressions : list/dict
            Clear output pins so previosly configured output pins can be used.

        """

        if isinstance(expressions, list):
            for i in range(len(expressions)):
                self.expressions['Boolean expression {}'.format(i)] = \
                    deepcopy(expressions[i])
        elif isinstance(expressions, dict):
            self.expressions = deepcopy(expressions)
        else:
            raise ValueError("Expressions must be list or dict.")

        self.output_pins = list()


from typing import List, Tuple

import labop
import uml

def protocol_make_decision_node(
    self: labop.Protocol,
    primary_incoming_node: uml.ActivityNode,
    decision_input_behavior: uml.Behavior = None,
    decision_input_source: uml.ActivityNode = None,
    outgoing_targets: List[Tuple[uml.ValueSpecification, uml.ActivityNode]] = None,
):
    """
    Make a uml:DecisionNode using optionally specified incoming and outgoing nodes.
    Returns a uml:DecisionNode and a set of edges connecting incoming and outgoing nodes.

    Args:
        primary_incoming_source_node (uml:ActivityNode): primary incoming edge type (control or object) determines output edge types
        decision_input_source_node (uml:ActivityNode, optional): Used to evaluate guards. Defaults to None.
        outgoing_targets (List[Tuple[uml.ValueSpecification, uml.ActivityNode]], optional): List of pairs of guards and nodes for outgoing edges. Defaults to None.
    """

    assert primary_incoming_node
    primary_incoming_flow = (
        uml.ControlFlow(source=primary_incoming_node)
        if isinstance(primary_incoming_node, uml.ControlNode)
        else uml.ObjectFlow(source=primary_incoming_node)
    )
    self.edges.append(primary_incoming_flow)

    decision_input = None

    if decision_input_behavior:
        input_pin_map = {}
        decision_input_control = None
        if decision_input_source:
            input_pin_map["decision_input"] = decision_input_source
        if primary_incoming_node:
            if isinstance(primary_incoming_node, uml.ObjectNode):
                input_pin_map["primary_input"] = primary_incoming_node
            else:
                # Make a ControlFlow so that decision_input executes first
                decision_input_control = uml.ControlFlow(source=primary_incoming_node)

        decision_input = self.execute_primitive(
            decision_input_behavior, **input_pin_map
        )
        if decision_input_control:
            decision_input_control.target = decision_input
            self.edges.append(decision_input_control)

    decision_input_flow = None
    if decision_input_source:
        decision_input_flow = uml.ObjectFlow(source=decision_input_source)
        self.edges.append(decision_input_flow)

    decision = uml.DecisionNode(
        decision_input=decision_input_behavior, decision_input_flow=decision_input_flow
    )
    self.nodes.append(decision)

    if decision_input:
        # Flow that communicates the return value of the decision_input behavior execution to the decision
        decision_input_to_decision_flow = uml.ObjectFlow(
            source=decision_input.output_pin("return"), target=decision
        )
        self.edges.append(decision_input_to_decision_flow)

    primary_incoming_flow.target = decision
    if decision_input_flow:
        decision_input_flow.target = decision

    # Make edges for outgoing_targets
    if outgoing_targets:
        for (guard, target) in outgoing_targets:
            decision.add_decision_output(self, guard, target)

    return decision

labop.Protocol.make_decision_node = (
    protocol_make_decision_node  # Add to class via monkey patch
)

def decision_node_add_decision_output(self, protocol, guard, target):
    """Attach a guarded edge between DecisionNode and target.

    Args:
        protocol (labop.Protocol): The protocol with the self DecisionNode.
        guard (primitive type): edge guard
        target (uml.ActivityNode): edge target
    """

    kwargs = {"source": self, "target": target}
    kwargs["guard"] = uml.literal(guard)
    outgoing_edge = (
        uml.ObjectFlow(**kwargs)
        if isinstance(self.get_primary_incoming_flow(protocol).source, uml.ObjectNode)
        else uml.ControlFlow(**kwargs)
    )
    protocol.edges.append(outgoing_edge)

uml.DecisionNode.add_decision_output = (
    decision_node_add_decision_output  # Add to class via monkey patch
)

def decision_node_get_primary_incoming_flow(self, protocol):
    try:
        primary_incoming_flow = next(
            e
            for e in protocol.edges
            if e.target is not None
            and e.target.lookup() == self
            and e != self.decision_input_flow
            and not (
                isinstance(e.source.lookup(), uml.OutputPin)
                and e.source.lookup().get_parent().behavior == self.decision_input
            )
        )
        return primary_incoming_flow
    except StopIteration as e:
        raise Exception(
            f"Could not find primary_incoming edge for DecisionNode: {self.identity}"
        )

uml.DecisionNode.get_primary_incoming_flow = (
    decision_node_get_primary_incoming_flow  # Add to class via monkey patch
)

def decision_node_get_decision_input_node(self):
    if hasattr(self, "decision_input") and self.decision_input:
        return self.decision_input
    else:
        # primary input flow leads to decision
        primary_incoming_flow = self.get_primary_incoming_flow(self.protocol())
        return primary_incoming_flow.source.lookup().get_decision_input_node()

uml.DecisionNode.get_decision_input_node = (
    decision_node_get_decision_input_node  # Add to class via monkey patch
)

def fork_node_get_decision_input_node(self):
    [fork_input_edge] = [e for e in self.protocol().edges if e.target.lookup() == self]
    decision_input_node = fork_input_edge.source.lookup().get_decision_input_node()
    return decision_input_node

uml.ForkNode.get_decision_input_node = (
    fork_node_get_decision_input_node  # Add to class via monkey patch
)

def output_pin_get_decision_input_node(self):
    return self.get_parent()

uml.OutputPin.get_decision_input_node = (
    output_pin_get_decision_input_node  # Add to class via monkey patch
)


import os
from typing import List, Tuple

from nazurin.models import Caption, Illust, Image
from nazurin.utils import Request
from nazurin.utils.exceptions import NazurinError

class Twitter(object):
    async def getTweet(self, status_id: int):
        """Get a tweet from API."""
        # Old: 'https://syndication.twitter.com/tweets.json?ids='+ status_id +'&lang=en'
        api = 'https://cdn.syndication.twimg.com/tweet?id=' + str(
            status_id) + '&lang=en'
        async with Request() as request:
            async with request.get(api) as response:
                if response.status == 404:
                    raise NazurinError('Tweet not found or unavailable.')
                tweet = await response.json()
                return tweet

    async def fetch(self, status_id: int) -> Illust:
        """Fetch & return tweet images and information."""
        tweet = await self.getTweet(status_id)
        imgs = self.getImages(tweet)
        caption = self.buildCaption(tweet)
        return Illust(imgs, caption, tweet)

    def getImages(self, tweet) -> List[Image]:
        """Get all images in a tweet."""
        if 'photos' not in tweet.keys():
            raise NazurinError('No photo found.')
        photos = tweet['photos']
        imgs = list()
        for photo in photos:
            filename, url, thumbnail = self.parseUrl(photo['url'])
            imgs.append(
                Image('twitter - ' + tweet['id_str'] + ' - ' + filename,
                      url,
                      thumbnail,
                      width=photo['width'],
                      height=photo['height']))
        return imgs

    def buildCaption(self, tweet) -> Caption:
        return Caption({
            'url': f"https://twitter.com/{tweet['user']['screen_name']}/status/{tweet['id_str']}",
            'author': f"{tweet['user']['name']} #{tweet['user']['screen_name']}",
            'text': tweet['text']
        })

    def parseUrl(self, src: str) -> Tuple[str, str, str]:
        """Get filename, original file url & thumbnail url of the original image

        eg:

            src: 'https://pbs.twimg.com/media/DOhM30VVwAEpIHq.jpg'

            return: 'DOhM30VVwAEpIHq.jpg', 'https://pbs.twimg.com/media/DOhM30VVwAEpIHq?format=jpg&name=orig', 'https://pbs.twimg.com/media/DOhM30VVwAEpIHq?format=jpg&name=large'

        Doc: https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/entities-object
        """
        basename = os.path.basename(src)
        filename, extension = os.path.splitext(basename)
        url = 'https://pbs.twimg.com/media/' + filename + '?format=' + extension[
            1:]
        return basename, (url + '&name=orig'), (url + '&name=large')


"main rendering script"

import os
from math import cos, pi, sin

import matplotlib.pyplot as plt
from direct.actor.Actor import Actor
from direct.interval.IntervalGlobal import Sequence
from direct.showbase.ShowBase import ShowBase
from direct.task import Task
from panda3d.core import Filename, Material, NodePath, Point3

class renderEngine(ShowBase):
    """
    the main renderer for pandas

    the costom rendering engien parameters
    """
    def __init__(self, higthMap):
        ShowBase.__init__(self)
        "initate pandas"

        self.boxes = []
        """a 3d list containning the rendered boxes"""
        self.gems = []
        """a 1d list containing the gem actors"""
        self.showHigtMap(higthMap)
        # set inital camera location
        self.camera.setPos(0,0,1000)

    def showHigtMap(self, hightMap):
        """create render objects from hightmap

        given a hightmap the function crates cubes stackt on top of each other to render the board
        for every position in the hightmap
        its not very optimized

        will alsow create list of gems called gems and add them bla bla bla

        @param hightmap: the hightmap we get render data from
        @type hightmap: hightmap object
        """
        #create every box and write it to boxes simultainously generate the box list using append
        for x in range(hightMap._arr.shape[0]):
            self.boxes.append([])
            for y in range(hightMap._arr.shape[1]):
                self.boxes[-1].append([])
                for z in range(int(hightMap._arr[x,y]+1)):
                    #crate the actor
                    actor = loader.loadModel("models/box")
                    actor.setScale(1,1,1)
                    actor.setPos(x,y,z)
                    actor.setTexture(renderEngine.getBlockTex(hightMap,x, y,z),2)
                    # add actor to render tree
                    actor.reparentTo(self.render)

        #crate the gems
        for x, y in hightMap.gemPoses:
            try:
                z = hightMap._arr[x,y]
            except:
                print(f"invalid gem location, location is {x}, {y} of map of shape {hightMap._arr.shape}")

            gemActor = loader.loadModel("models/smiley")
            gemActor.setPos(x+.5,y+.5,z+1.5)
            gemActor.setScale(.5,.5,.5)
            gemActor.reparentTo(self.render)
            self.gems.append(gemActor)

    @staticmethod
    def getBlockTex(hightmap, *loc):
        """returns the texture of the block

        given the position and hmap we crate a pandas texture to set the actor to
        this is an indev function and is subject to change

        @param hightmap: the hmap data to get data from
        @type hightmap: higthmap

        @param loc: the position of the block whos tex wer setting
        @type loc: 3 intagers

        @return: a pandas3d texture for the objects
        """
        # get panda path of this files dir
        path = Filename.fromOsSpecific(os.path.dirname(os.path.abspath(__file__)))
        # get witch text to load
        # it top most block make grass else dirt and bottom most wather
        imag = "wather" if loc[-1] == 0 else ("grass" if loc[-1] == hightmap[loc[:-1]] else "dirt")
        tex  = loader.loadTexture(f"{path}/blocks/{imag}.jpg")
        return tex

    def addPawn(self, pawn):
        """add a pawn to render scene using its actor to represent it

        @param pawn: the pawn to be added
        @type pawn: a pawn object defined int pawns.pawn
        """
        pawn.actor.reparentTo(self.render)
        pawn._setPawnLoc(pawn.pos)
        pawn._setPawnRenderRotation()


"""
remarkable.

Usage:
  remarkable [options] remark <path-to-markdown-file> [<title>]
  remarkable [options] reveal <path-to-markdown-file> <title>

  remarkable -h | --help

Options:
  --debug               Debug.

  -h --help             Show this screen.
"""
import logging
import os
import shutil
import sys

from docopt import docopt
from jinja2 import Environment, PackageLoader
from snide import Deck

import remarkable

log = logging.getLogger(__name__)

def render_template(template_name, context):
    return Environment(
        loader=PackageLoader('remarkable')
    ).get_template(template_name).render(context)

def ask(question):
    '''Display a Y/n question prompt, and return a boolean'''
    while True:
        print
        input_ = raw_input('%s [Y/n] ' % question)
        input_ = input_.strip().lower()
        if input_ in ('y', 'yes', ''):
            return True
        if input_ in ('n', 'no'):
            return False
        print('Invalid selection')

def render_template_directory(deck):
    output_directory = deck.title

    if os.path.exists(output_directory):
        if sys.stdout.isatty():
            if ask('%s already exists, shall I delete it?'):
                shutil.rmtree(output_directory)
        else:
            shutil.rmtree(output_directory)

    # copy support files to output directory
    template_directory_path = (
        '%s/templates/%s' %
        (remarkable.__path__[0], deck.presentation_type)
    )
    shutil.copytree(
        template_directory_path,
        output_directory,
    )

    # write index to output directory
    write_file(
        '%s/index.html' % output_directory,

        render_template(
            '%s/index.html' % deck.presentation_type,
            dict(slides=deck.slides),
        ),
    )

    return output_directory

def read_file(file_name):
    return open(file_name).read().decode('utf-8')

def write_file(file_name, content):
    with open(file_name, 'w') as html_file:
        html_file.write(content.encode('utf-8'))
    log.info('Created %s' % file_name)

def remark(arguments):
    html = render_template(
        'remark/index.html',
        dict(markdown=read_file(arguments['<path-to-markdown-file>'])),
    )

    title = arguments.get('<title>', 'Remark Presentation')
    write_file(
        '%s.html' % (title if title else 'remark'),
        html,
    )

def reveal(arguments):
    render_template_directory(Deck(
        arguments.get('<title>', 'Reveal Presentation'),
        read_file(arguments['<path-to-markdown-file>']),
        'reveal'
    ))

def main():
    arguments = docopt(__doc__, version=remarkable.__version__)

    logging.basicConfig(
        level=logging.DEBUG if arguments['--debug'] else logging.INFO
    )
    log.debug('arguments: %s', arguments)

    commands = ['remark', 'reveal']
    for command in commands:
        if arguments[command]:
            globals()[command](arguments)


## IMPORTS

import sys
sys.path.append('../api/sql wrapper')
import sql_wrapper as wrapper

from urllib.request import urlopen
import urllib
import requests
import webbrowser
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup

from geopy.geocoders import Nominatim

def population_density(city='aurangabad'):
    '''
    GIves the population density of a city
    :param city: cite name
    :return: population density
    '''
    return 366

def calculate_drift(todays_intentensity, yesterdays_intensity, ppd):
    '''
    Calculates drift for a region
    :param todays_intentensity: intensity value for today
    :param yesterdays_intensity: intensity value for yesterday
    :param ppd: population density of that region
    :return: drift
    '''
    return (todays_intentensity - yesterdays_intensity) / ppd

def calculate_intensity(count, ppd, last_drift, last_intensity):
    '''
    calculates intensity for given parameters
    :param count: count of patient
    :param ppd: population density
    :param last_drift: drift of yesterday
    :param last_intensity: drift of today
    :return: intensity
    '''
    current_factor = count / ppd
    inflation_factor = last_drift * last_intensity

    return current_factor + inflation_factor

def threshold(intensity):
    '''
    Gives threshold for epidemic
    :param intensity: today's intensity
    :return: Boolean whether intensity is above threshold or not
    '''
    return intensity >= 0.01

def intensity_and_drift_of_dataframe(df, last_intensity, last_drift):
    '''
    Calculates intensity, drift and epidemic threshold of complete dataframe
    :param df: pandas dataframe
    :param last_intensity: intensity of yesterday
    :param last_drift: drift for yesterday
    :return: (intensity, drift, boolean for epidemic)
    '''

    count = len(df)
    ppd = population_density()

    intensity = calculate_intensity(count, ppd, last_drift, last_intensity)
    drift = calculate_drift(intensity, last_intensity, ppd)
    is_epidemic = threshold(intensity)

    return intensity, drift, is_epidemic

def get_surrounding_pincodes(pincode, sqlWrapper=None):
    '''
    Returns a list of surrounding pincodes of a given pincode
    :param pincode: pincode of the region
    :param sqlWrapper: Optional, if already made an sqlWrapper
    :return: list of surrounding pincodes
    '''
    if sqlWrapper is None:
        sqlWrapper = wrapper.SqlWrapper(password='')
    query = "select pincode from district_pincode where pincode like '" + str(pincode)[:4] + "%'"
    l = sqlWrapper.custom_query(query, single=True)
    return l

def internet_on():
    try:
        response = urlopen('https://www.google.com/', timeout=10)
        return True
    except:
        return False

def geopy_latlong_from_pincode(pincode):
    '''
    Get latlong of a pincode using geopy
    :param pincode: pincode of the area
    :return: (latitude, longitude)
    '''
    geolocator = Nominatim(user_agent="default")
    location = geolocator.geocode(str(pincode), timeout=40)
    if location is None:
        return pincode_to_coordinates(pincode)
    return (location.latitude, location.longitude)

def pincode_to_coordinates(pincode):
    '''
        Function to get coordinates from a zipcode using seleniuim and scraping google searches
        Parameters:
            1. pincode: pincode of the area whose coordinates is to be searched
        Returns:
            tuple of coordinates if found else None
    '''

    if pincode == "" or pincode is None:
        raise Exception('Pincode cannot be empty')

    #     req = requests.get('http://clients3.google.com/generate_204')
    if not internet_on():
        raise Exception('Error: Internet is not connected')

    text = str(pincode) + ' coordinates'
    text = urllib.parse.quote_plus(text)
    url = 'https://google.com/search?q=' + text

    options = Options()
    options.add_argument('--headless')
    options.add_argument('--disable-gpu')
    driver = webdriver.Chrome(r'C:\chromedriver\chromedriver.exe', chrome_options=options)

    driver.get(url)

    soup = BeautifulSoup(driver.page_source, "lxml")

    for g in soup.findAll('div', {'class': 'Z0LcW'}):
        if ' E' in str(g.text):
            driver.quit()
            lat, long = g.text.split(', ')
            lat = lat.replace('° N', '').strip()
            long = long.replace('° E', '').strip()
            return (lat, long)
    driver.quit()
    return None

def min_max_normalize(vals):
    '''
    Normalizes a pandas series according to min max normalization
    :param vals: pandas series
    :return: narmalized pandas series
    '''
    minimum = vals.min()
    maximum = vals.max()
    denominator = maximum - minimum
    if denominator <=0:
        denominator = 1
    return (vals - minimum) / denominator

# print(pincode_to_coordinates(431002))


#-*- encoding: UTF-8 -*-
import logging as log
from OpenGL.GL import *
import cyglfw3 as glfw
import numpy as np
from Shader import Shader
from scipy.misc import imread

class ComputeShader(object):

	def __init__(self):
		Shader.shaderFolder	= 'view/opengl/computeShader'
		self.setup()

		filename = '/home/dimitri/Desktop/resize_im4.jpg'
		img = np.flipud( imread(filename) )
		img = np.dstack( (img, np.ones(img.shape[:2], dtype=np.float32)) )
		h, w = img.shape[:2]
		self.h, self.w = h,w

		self.read_tex = self.setupTexture(0, w, h, img)
		self.write_tex = self.setupTexture(1, w, h, None)

		self.computeShader = Shader('simple.cs')
		self.renderShader = Shader('quad.vs', 'quad.frag')
		self.computeShader.compile()
		self.renderShader.compile()
		self.__previous_second = glfw.GetTime()
		self.__frame_count = 0.0
		self.__window_title = 'ComputeShader'

	def setup(self):
		if not glfw.Init():
			log.critical('GLFW initialization failed!')
			exit(-1)
		self.window = glfw.CreateWindow(224, 224, '', None)
		if not self.window:
			log.critical('Could not create window!')
			glfw.Terminate()
			exit(-1)
		glfw.MakeContextCurrent(self.window)
		glewExperimental = True
		glfw.SetFramebufferSizeCallback(self.window, self.__framebuffer_size_callback__)

	def __framebuffer_size_callback__(self, window, width, height):
		glViewport(0,0,width, height)

	def __update_fps_counter__(self):
		current_second = glfw.GetTime()
		elapsed_seconds = current_second - self.__previous_second
		if elapsed_seconds > 1.0:
			self.__previous_second = current_second
			fps = float(self.__frame_count) / float(elapsed_seconds)
			glfw.SetWindowTitle(self.window, 'FPS: %.2f' % (fps))
			self.__frame_count = 0.0
		self.__frame_count += 1.0

	def setupTexture(self, bind_point, w, h, img):
		tex = glGenTextures(1)
		glBindTexture(GL_TEXTURE_2D, tex)
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, img)
		if img is None:
			glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, 140*np.ones((h,w,4), dtype=np.uint8))
		access = GL_WRITE_ONLY if img is None else GL_READ_ONLY
		glBindImageTexture(bind_point, tex, 0, GL_FALSE, 0, access, GL_RGBA32F)
		glBindTexture(GL_TEXTURE_2D, 0)
		return tex

	def logComputeCapInfo(self):
		x = glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_COUNT, 0)[0]
		y = glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_COUNT, 1)[0]
		z = glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_COUNT, 2)[0]
		log.info( 'Max global work group size x: %d, y: %d, z: %d' % (x,y,z))
		x = glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, 0)[0]
		y = glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, 1)[0]
		z = glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, 2)[0]
		log.info( 'Max local work group size x: %d, y: %d, z: %d' % (x,y,z) )
		max_inv = glGetIntegerv(GL_MAX_COMPUTE_WORK_GROUP_INVOCATIONS)
		log.info( 'Max local work group invocations: %d' % max_inv )
		shMemSize = glGetIntegerv(GL_MAX_COMPUTE_SHARED_MEMORY_SIZE)
		log.info( 'Max shared memory size: %d' % shMemSize )

	def loop(self):
		while not glfw.WindowShouldClose(self.window):
			self.computeShader.use()
			glDispatchCompute(7, 7, 1)
			#glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT)

			glBindTexture(GL_TEXTURE_2D, self.write_tex)
			self.renderShader.use()
			glDrawArrays(GL_TRIANGLES, 0, 6)
			glBindTexture(GL_TEXTURE_2D, 0)

			glfw.SwapBuffers(self.window)
			self.__update_fps_counter__()
			glfw.PollEvents()
			if glfw.GetKey(self.window, glfw.KEY_ESCAPE) == glfw.PRESS:
				glfw.SetWindowShouldClose(self.window, 1)

	def cleanup(self):
		glfw.Terminate()


import os
from config import Config
import numpy as np
from bert_model import BertModel
import tensorflow as tf

def train():
    """
    train the model on toy task:
        learn to count,sum up input, and distinct whether the total value of input is below or greater than a threshold.
    usage:
        use train() to train the model, and it will save checkpoint to file system;
        use predict() to make a prediction based on the trained model.
    """
    # 1.init config and model
    config = Config()
    threshold = (config.sequence_length / 2) + 1  # 有何用？
    model = BertModel(config)
    gpu_config = tf.ConfigProto()
    gpu_config.gpu_options.allow_growth = True
    saver = tf.train.Saver()
    # save_path = config.ckpt_dir + "model.ckpt"
    save_path = os.path.join(config.ckpt_dir, 'model.ckpt')
    # batch_size = 8
    if not os.path.exists(config.ckpt_dir):
        os.makedirs(config.ckpt_dir)
    with tf.Session(config=gpu_config) as sess:
        sess.run(tf.global_variables_initializer())
        if os.path.exists(config.ckpt_dir):  #
            saver.restore(sess, tf.train.latest_checkpoint(save_path))
        for i in range(10000):
            # 2.feed data
            input_x = np.random.randn(config.batch_size, config.sequence_length)  # [None, self.sequence_length]
            input_x[input_x >= 0] = 1
            input_x[input_x < 0] = 0
            input_y = generate_label(input_x, threshold)
            # p_mask_lm = [i for i in range(batch_size)]
            # 3.run session to train the model, print some logs.
            loss, _ = sess.run([model.loss_val, model.train_op],
                               feed_dict={model.input_x: input_x,
                                          model.input_y: input_y,
                                          # model.p_mask_lm: p_mask_lm,
                                          model.dropout_keep_prob: config.dropout_keep_prob})
            print(i, "loss:", loss, "-------------------------------------------------------")
            if i == 300:
                print("label[0]:", input_y[0])
                print("input_x:", input_x)
            if i % 500 == 0:
                saver.save(sess, save_path, global_step=i)

def predict():
    """
    use saved checkpoint from model to make prediction, and print it,
    to see whether it is able to do toy task successfully.
    :return:
    """
    config = Config()
    threshold = (config.sequence_length / 2) + 1
    config.batch_size = 1
    model = BertModel(config)
    gpu_config = tf.ConfigProto()
    gpu_config.gpu_options.allow_growth = True
    saver = tf.train.Saver()
    ckpt_dir = config.ckpt_dir
    print("ckpt_dir:", ckpt_dir)
    with tf.Session(config=gpu_config) as sess:
        sess.run(tf.global_variables_initializer())
        saver.restore(sess, tf.train.latest_checkpoint(ckpt_dir))
        for i in range(100):
            # 2.feed data
            input_x = np.random.randn(config.batch_size, config.sequence_length)  # [None, self.sequence_length]
            input_x[input_x >= 0] = 1
            input_x[input_x < 0] = 0
            target_label = generate_label(input_x, threshold)
            input_sum = np.sum(input_x)
            # 3.run session to train the model, print some logs.
            logit, prediction = sess.run([model.logits, model.predictions],
                                         feed_dict={model.input_x: input_x,
                                                    model.dropout_keep_prob: config.dropout_keep_prob})
            print("target_label:", target_label, ";input_sum:", input_sum, "threshold:", threshold,
                  ";prediction:", prediction)
            print("input_x:", input_x, ";logit:", logit)

def generate_label(input_x, threshold):
    """
    generate label with input
    :param input_x: shape of [batch_size, sequence_length]
    :param threshold:
    :return: y:[batch_size]
    """
    batch_size, sequence_length = input_x.shape
    y = np.zeros((batch_size, 2))
    for i in range(batch_size):
        input_single = input_x[i]
        _sum = np.sum(input_single)
        if i == 0: print("sum:", _sum, ";threshold:", threshold)
        y_single = 1 if _sum > threshold else 0
        y[i] = [0, 1] if y_single == 1 else [1, 0]
    return y


import io
from typing import BinaryIO, ClassVar

from ._fastobo import FastoboSerializer
from .base import BaseSerializer

class OboSerializer(FastoboSerializer, BaseSerializer):

    format = "obo"

    def dump(self, file):
        writer = io.TextIOWrapper(file)
        try:
            # dump the header
            if self.ont.metadata:
                header = self._to_header_frame(self.ont.metadata)
                file.write(str(header).encode("utf-8"))
                if self.ont._terms or self.ont._relationships:
                    file.write(b"\n")
            # dump terms
            if self.ont._terms:
                for i, (id, data) in enumerate(self.ont._terms.items()):
                    frame = self._to_term_frame(data)
                    file.write(str(frame).encode("utf-8"))
                    if i < len(self.ont._terms) - 1 or self.ont._relationships:
                        file.write(b"\n")
            # dump typedefs
            if self.ont._relationships:
                for i, (id, data) in enumerate(self.ont._relationships.items()):
                    frame = self._to_typedef_frame(data)
                    file.write(str(frame).encode("utf-8"))
                    if i < len(self.ont._relationships) - 1:
                        file.write(b"\n")
        finally:
            writer.detach()


from utilities import PrintException, removeNS, addPrefixes
from config import namespaces, profileWelcome, profileDefinitions, sparql, ignore
import json
import sys
import os
from SPARQLWrapper import JSON, SPARQLWrapper
from datetime import datetime, timedelta
import re

sparqlData = "http://206.167.181.124:7200/repositories/era-dd"
sparqlData = SPARQLWrapper(sparqlData)
sparqlData.setReturnFormat(JSON)
with open ('modifyDD.txt' , 'w+') as out:
	for subgraph in ['community', 'collection', 'generic', 'thesis', 'oai_pmh', 'oai_etdms', 'instances']:
		#out.write("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> prefix ual: <http://terms.library.ualberta.ca/> prefix ualid: <http://terms.library.ualberta.ca/id>" + "\n")
		sparqlData.setQuery("prefix ual: <http://terms.library.ualberta.ca/> select  * where {graph ual:%s  {?s ?p ?o} }" %(subgraph))
		r = sparqlData.query().convert()['results']['bindings']
		for triple in r:
			subject = triple['s']['value'].lower()
			predicate = triple['p']['value']
			objectt = triple['o']['value']
			print (objectt)
			sparql = "http://206.167.181.124:7200/repositories/test/statements"
			sparql = SPARQLWrapper(sparql)
			sparql.setMethod('POST')
			if triple['o']['type'] == "uri":
				print ("uri")
				query = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> prefix ual: <http://terms.library.ualberta.ca/> prefix ualid: <http://terms.library.ualberta.ca/id> INSERT DATA {GRAPH ual:%s {<%s> <%s> <%s>} } " % (subgraph, subject, predicate, objectt)
			if triple['o']['type'] == "literal":
				print (subject, " is a literal")
				query = 'PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> prefix ual: <http://terms.library.ualberta.ca/> prefix ualid: <http://terms.library.ualberta.ca/id> INSERT DATA {GRAPH ual:%s {<%s> <%s> "%s"} } ; ' % (subgraph, subject, predicate, objectt)
			#out.write(query + "\n")
			sparql.setQuery(query)
			sparql.query()
			#query = "INSERT DATA {GRAPH ual:%s {<%s> <http://terms.library.ualberta.ca/dataDictionaryLabel> <%s>} } ; " % (subgraph, subject, triple['s']['value'])
			#out.write(query + "\n")


# (C) Copyright 2016 Hewlett Packard Enterprise Development Company LP

import os
import shutil
import tempfile
import unittest

from monasca_setup.detection.plugins import json_plugin

class TestJsonPlugin(unittest.TestCase):

    def setUp(self):
        unittest.TestCase.setUp(self)
        self.plugin_obj = json_plugin.JsonPlugin('temp_dir')
        self.varcachedir = tempfile.mkdtemp()

    def tearDown(self):
        shutil.rmtree(self.varcachedir)

    def test_var_cache_exists(self):
        json_plugin.VAR_CACHE_DIR = self.varcachedir
        self.plugin_obj._detect()
        result = self.plugin_obj.build_config()
        self.assertTrue(self.plugin_obj.available)
        self.assertEqual(result['json_plugin']['instances'],
                         [{'name': self.varcachedir,
                            'metrics_dir': self.varcachedir}])

    def test_var_cache_not_exists(self):
        json_plugin.VAR_CACHE_DIR = os.path.join(self.varcachedir, 'dummy')
        self.plugin_obj._detect()
        self.assertFalse(self.plugin_obj.available)

    def test_dependencies_installed(self):
        self.assertTrue(self.plugin_obj.dependencies_installed())


# -*- coding: utf-8 -*-
from json import dumps
from unittest import TestCase

from projects.api.main import app
from projects.controllers.utils import uuid_alpha
from projects.database import engine
from projects.object_storage import BUCKET_NAME

EXPERIMENT_ID = str(uuid_alpha())
NAME = "foo"
POSITION = 0
EXPERIMENT_ID_2 = str(uuid_alpha())
NAME_2 = "foo 2"
POSITION_2 = 1
PROJECT_ID = str(uuid_alpha())
TEMPLATE_ID = str(uuid_alpha())
TASK_ID = str(uuid_alpha())
OPERATOR_ID = str(uuid_alpha())
OPERATOR_ID_2 = str(uuid_alpha())
OPERATOR_ID_3 = str(uuid_alpha())
OPERATOR_ID_4 = str(uuid_alpha())
DEPENDENCY_ID = str(uuid_alpha())
IS_ACTIVE = True
PARAMETERS = {"coef": 0.1}
PARAMETERS_JSON = dumps(PARAMETERS)
DESCRIPTION = "long foo"
IMAGE = "platiagro/platiagro-notebook-image-test:0.2.0"
COMMANDS = ["CMD"]
COMMANDS_JSON = dumps(COMMANDS)
ARGUMENTS = ["ARG"]
ARGUMENTS_JSON = dumps(ARGUMENTS)
TAGS = ["PREDICTOR"]
TAGS_JSON = dumps(TAGS)
TASKS_JSON = dumps([TASK_ID])
EXPERIMENT_NOTEBOOK_PATH = f"minio://{BUCKET_NAME}/tasks/{TASK_ID}/Experiment.ipynb"
DEPLOYMENT_NOTEBOOK_PATH = f"minio://{BUCKET_NAME}/tasks/{TASK_ID}/Deployment.ipynb"
CREATED_AT = "2000-01-01 00:00:00"
CREATED_AT_ISO = "2000-01-01T00:00:00"
UPDATED_AT = "2000-01-01 00:00:00"
UPDATED_AT_ISO = "2000-01-01T00:00:00"
NAME_COPYFROM = "TEST_COPY"

DEPENDENCIES_EMPTY = []
DEPENDENCIES_EMPTY_JSON = dumps(DEPENDENCIES_EMPTY)
DEPENDENCIES_OP_ID = [OPERATOR_ID]
DEPENDENCIES_OP_ID_JSON = dumps(DEPENDENCIES_OP_ID)

class TestExperiments(TestCase):
    def setUp(self):
        self.maxDiff = None
        conn = engine.connect()
        text = (
            f"INSERT INTO tasks (uuid, name, description, image, commands, arguments, tags, experiment_notebook_path, deployment_notebook_path, is_default, created_at, updated_at) "
            f"VALUES ('{TASK_ID}', '{NAME}', '{DESCRIPTION}', '{IMAGE}', '{COMMANDS_JSON}', '{ARGUMENTS_JSON}', '{TAGS_JSON}', '{EXPERIMENT_NOTEBOOK_PATH}', '{DEPLOYMENT_NOTEBOOK_PATH}', 0, '{CREATED_AT}', '{UPDATED_AT}')"
        )
        conn.execute(text)
        text = (
            f"INSERT INTO projects (uuid, name, created_at, updated_at) "
            f"VALUES ('{PROJECT_ID}', '{NAME}', '{CREATED_AT}', '{UPDATED_AT}')"
        )
        conn.execute(text)

        text = (
            f"INSERT INTO experiments (uuid, name, project_id, position, is_active, created_at, updated_at) "
            f"VALUES ('{EXPERIMENT_ID}', '{NAME}', '{PROJECT_ID}', '{POSITION}', 1, '{CREATED_AT}', '{UPDATED_AT}')"
        )
        conn.execute(text)

        text = (
            f"INSERT INTO experiments (uuid, name, project_id, position, is_active, created_at, updated_at) "
            f"VALUES ('{EXPERIMENT_ID_2}', '{NAME_2}', '{PROJECT_ID}', '{POSITION_2}', 1, '{CREATED_AT}', '{UPDATED_AT}')"
        )
        conn.execute(text)

        text = (
            f"INSERT INTO operators (uuid, experiment_id, task_id, parameters, created_at, updated_at, dependencies) "
            f"VALUES ('{OPERATOR_ID}', '{EXPERIMENT_ID}', '{TASK_ID}', '{PARAMETERS_JSON}', '{CREATED_AT}', '{UPDATED_AT}', '{DEPENDENCIES_EMPTY_JSON}')"
        )
        conn.execute(text)

        text = (
            f"INSERT INTO operators (uuid, experiment_id, task_id, parameters, created_at, updated_at, dependencies) "
            f"VALUES ('{OPERATOR_ID_2}', '{EXPERIMENT_ID}', '{TASK_ID}', '{PARAMETERS_JSON}', '{CREATED_AT}', '{UPDATED_AT}', '{DEPENDENCIES_OP_ID_JSON}')"
        )
        conn.execute(text)

        text = (
            f"INSERT INTO templates (uuid, name, tasks, created_at, updated_at) "
            f"VALUES ('{TEMPLATE_ID}', '{NAME}', '{TASKS_JSON}', '{CREATED_AT}', '{UPDATED_AT}')"
        )
        conn.execute(text)
        conn.close()

    def tearDown(self):
        conn = engine.connect()

        text = f"DELETE FROM templates WHERE uuid = '{TEMPLATE_ID}'"
        conn.execute(text)

        text = f"DELETE FROM operators WHERE experiment_id = '{EXPERIMENT_ID}'"
        conn.execute(text)

        text = f"DELETE FROM operators WHERE experiment_id =" \
               f" (SELECT uuid FROM experiments where name = '{NAME_COPYFROM}')"
        conn.execute(text)

        text = f"DELETE FROM experiments WHERE project_id in ('{PROJECT_ID}')"
        conn.execute(text)

        text = f"DELETE FROM projects WHERE uuid = '{PROJECT_ID}'"
        conn.execute(text)

        text = f"DELETE FROM tasks WHERE uuid = '{TASK_ID}'"
        conn.execute(text)

        conn.close()

    def test_list_experiments(self):
        with app.test_client() as c:
            rv = c.get("/projects/unk/experiments")
            result = rv.get_json()
            expected = {"message": "The specified project does not exist"}
            self.assertDictEqual(expected, result)
            self.assertEqual(rv.status_code, 404)

            rv = c.get(f"/projects/{PROJECT_ID}/experiments")
            result = rv.get_json()
            self.assertIsInstance(result, list)

    def test_create_experiment(self):
        with app.test_client() as c:
            rv = c.post("/projects/unk/experiments", json={})
            result = rv.get_json()
            expected = {"message": "The specified project does not exist"}
            self.assertDictEqual(expected, result)
            self.assertEqual(rv.status_code, 404)

            rv = c.post(f"/projects/{PROJECT_ID}/experiments", json={})
            result = rv.get_json()
            expected = {"message": "name is required"}
            self.assertDictEqual(expected, result)
            self.assertEqual(rv.status_code, 400)

            rv = c.post(f"/projects/{PROJECT_ID}/experiments", json={
                "name": NAME,
            })
            result = rv.get_json()
            expected = {"message": "an experiment with that name already exists"}
            self.assertDictEqual(expected, result)
            self.assertEqual(rv.status_code, 400)

            rv = c.post(f"/projects/{PROJECT_ID}/experiments", json={
                "name": "test",
            })
            result = rv.get_json()
            expected = {
                "name": "test",
                "projectId": PROJECT_ID,
                "position": 2,
                "isActive": IS_ACTIVE,
                "operators": [],
            }
            # uuid, created_at, updated_at are machine-generated
            # we assert they exist, but we don't assert their values
            machine_generated = ["uuid", "createdAt", "updatedAt"]
            for attr in machine_generated:
                self.assertIn(attr, result)
                del result[attr]
            self.assertDictEqual(expected, result)

        """Copy operators for a given experiment"""
        with app.test_client() as c:
            rv = c.post(f"/projects/{PROJECT_ID}/experiments", json={
                "name": f"{NAME_COPYFROM}",
                "copy_from": f"{EXPERIMENT_ID}"
            })
            self.assertEqual(rv.status_code, 200)

            rv = c.post(f"/projects/{PROJECT_ID}/experiments", json={
                "name": f"TESCOPY",
                "copy_from": f"4555"
            })
            self.assertEqual(rv.status_code, 400)

    def test_get_experiment(self):
        with app.test_client() as c:
            rv = c.get(f"/projects/foo/experiments/{EXPERIMENT_ID}")
            result = rv.get_json()
            expected = {"message": "The specified project does not exist"}
            self.assertDictEqual(expected, result)
            self.assertEqual(rv.status_code, 404)

            rv = c.get(f"/projects/{PROJECT_ID}/experiments/foo")
            result = rv.get_json()
            expected = {"message": "The specified experiment does not exist"}
            self.assertDictEqual(expected, result)
            self.assertEqual(rv.status_code, 404)

            rv = c.get(f"/projects/{PROJECT_ID}/experiments/{EXPERIMENT_ID}")
            result = rv.get_json()
            expected = {
                "uuid": EXPERIMENT_ID,
                "name": NAME,
                "projectId": PROJECT_ID,
                "position": POSITION,
                "isActive": IS_ACTIVE,
                "operators": result['operators'],
                "createdAt": CREATED_AT_ISO,
                "updatedAt": UPDATED_AT_ISO,
            }
            self.assertDictEqual(expected, result)

    def test_update_experiment(self):
        with app.test_client() as c:
            rv = c.patch(f"/projects/foo/experiments/{EXPERIMENT_ID}", json={})
            result = rv.get_json()
            expected = {"message": "The specified project does not exist"}
            self.assertDictEqual(expected, result)
            self.assertEqual(rv.status_code, 404)

            rv = c.patch(f"/projects/{PROJECT_ID}/experiments/foo", json={})
            result = rv.get_json()
            expected = {"message": "The specified experiment does not exist"}
            self.assertDictEqual(expected, result)
            self.assertEqual(rv.status_code, 404)

            rv = c.patch(f"/projects/{PROJECT_ID}/experiments/{EXPERIMENT_ID}", json={
                "name": NAME_2,
            })
            result = rv.get_json()
            expected = {"message": "an experiment with that name already exists"}
            self.assertDictEqual(expected, result)
            self.assertEqual(rv.status_code, 400)

            rv = c.patch(f"/projects/{PROJECT_ID}/experiments/{EXPERIMENT_ID}", json={
                "templateId": "unk",
            })
            result = rv.get_json()
            expected = {"message": "The specified template does not exist"}
            self.assertDictEqual(expected, result)
            self.assertEqual(rv.status_code, 400)

            rv = c.patch(f"/projects/{PROJECT_ID}/experiments/{EXPERIMENT_ID}", json={
                "unk": "bar",
            })
            self.assertEqual(rv.status_code, 400)

            # update experiment using the same name
            rv = c.patch(f"/projects/{PROJECT_ID}/experiments/{EXPERIMENT_ID}", json={
                "name": NAME,
            })
            self.assertEqual(rv.status_code, 200)

            rv = c.patch(f"/projects/{PROJECT_ID}/experiments/{EXPERIMENT_ID}", json={
                "name": "bar",
            })
            result = rv.get_json()
            expected = {
                "uuid": EXPERIMENT_ID,
                "name": "bar",
                "projectId": PROJECT_ID,
                "position": POSITION,
                "isActive": IS_ACTIVE,
                "operators": result['operators'],
                "createdAt": CREATED_AT_ISO,
            }
            machine_generated = ["updatedAt"]
            for attr in machine_generated:
                self.assertIn(attr, result)
                del result[attr]
            self.assertDictEqual(expected, result)
            self.assertEqual(rv.status_code, 200)

            rv = c.patch(f"/projects/{PROJECT_ID}/experiments/{EXPERIMENT_ID}", json={
                "templateId": TEMPLATE_ID,
            })
            result = rv.get_json()
            expected = {
                "uuid": EXPERIMENT_ID,
                "name": "bar",
                "projectId": PROJECT_ID,
                "position": POSITION,
                "isActive": IS_ACTIVE,
                "createdAt": CREATED_AT_ISO,
            }
            result_operators = result["operators"]
            machine_generated = ["updatedAt", "operators"]
            for attr in machine_generated:
                self.assertIn(attr, result)
                del result[attr]
            self.assertDictEqual(expected, result)
            expected = [{
                "taskId": TASK_ID,
                "experimentId": EXPERIMENT_ID,
                "parameters": {},
                "dependencies": [],
                "positionX": None,
                "positionY": None,
                "deploymentId": None
            }]
            machine_generated = ["uuid", "createdAt", "updatedAt"]
            for attr in machine_generated:
                for operator in result_operators:
                    self.assertIn(attr, operator)
                    del operator[attr]
            self.assertListEqual(expected, result_operators)

    def test_delete_experiment(self):
        with app.test_client() as c:
            rv = c.delete(f"/projects/foo/experiments/{EXPERIMENT_ID}")
            result = rv.get_json()
            expected = {"message": "The specified project does not exist"}
            self.assertDictEqual(expected, result)
            self.assertEqual(rv.status_code, 404)

            rv = c.delete(f"/projects/{PROJECT_ID}/experiments/unk")
            result = rv.get_json()
            expected = {"message": "The specified experiment does not exist"}
            self.assertDictEqual(expected, result)
            self.assertEqual(rv.status_code, 404)

            rv = c.delete(f"/projects/{PROJECT_ID}/experiments/{EXPERIMENT_ID}")
            result = rv.get_json()
            expected = {"message": "Experiment deleted"}
            self.assertDictEqual(expected, result)