from PyQt5 import QtCore, QtGui, QtWidgets
from scipy import signal
import matplotlib.pyplot as plt
from scipy.fft import fft, fftfreq,ifft,fftshift,ifftshift,rfft,rfftfreq,irfft
import cmath
from PyQt5.QtWidgets import QFileDialog
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
import pandas as pd
from matplotlib.animation import FuncAnimation
import numpy as np
from matplotlib.figure import Figure
import scipy.io.wavfile
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtWidgets import QApplication, QMainWindow
from gui import Ui_MainWindow
import os

# class definition for application window components like the main_window_class
class ApplicationWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(ApplicationWindow,self).__init__()
        self.main_window_class = Ui_MainWindow()
        self.main_window_class.setupUi(self)
        self.main_window_class.actionNew.triggered.connect(self.open_newwindow)
        self.main_window_class.resume_button.clicked.connect(self.play_back)
        #self.main_window_class.lim.clicked.connect(self.limits)
        self.main_window_class.back_button.clicked.connect(self.backword)
        self.main_window_class.forword_button.clicked.connect(self.forword)
        self.main_window_class.zin.clicked.connect(self.zoomin)
        self.main_window_class.zout.clicked.connect(self.zoomout)
        self.sliders_list=self.main_window_class.sliderlist#,self.main_window_class.slider2,self.main_window_class.slider3,self.main_window_class.slider4,self.main_window_class.slider5,self.main_window_class.slider6,self.main_window_class.slider7,self.main_window_class.slider8,self.main_window_class.slider9,self.main_window_class.slider10] #(0+1)*1/(self.Ts*2*10)
        #i=0
        #fl=True
        #for slider_count in range(0,len(self.sliders_list)):
        #    print(slider_count)
        #    s= self.sliders_list[slider_count]
#
        #    s.valueChanged[int].connect(lambda value: print(s.objectName()) )#self.gain(value,slider_count/(self.Ts*2*10),(slider_count+1)/(self.Ts*2*10)))
        #slider_count=0
        #    #print(slider_count)
        #    #self.sliders_list[slider_count].valueChanged
        #fl=False
        #self.main_window_class.s1.slider1.valueChanged[int].connect(lambda value: self.gain(value,0,1/(self.Ts*2*10)))
        #self.main_window_class.slider2.valueChanged[int].connect(lambda value: self.gain(value,1/(self.Ts*2*10),2/(self.Ts*2*10)))
        #self.main_window_class.slider3.valueChanged[int].connect(lambda value: self.gain(value,2/(self.Ts*2*10)/10,3/(self.Ts*2*10)))
        #self.main_window_class.slider4.valueChanged[int].connect(lambda value: self.gain(value,3/(self.Ts*2*10),4/(self.Ts*2*10)))
        #self.main_window_class.slider5.valueChanged[int].connect(lambda value: self.gain(value,4/(self.Ts*2*10),5/(self.Ts*2*10)))
        #self.main_window_class.slider6.valueChanged[int].connect(lambda value: self.gain(value,5/(self.Ts*2*10),6/(self.Ts*2*10)))
        #self.main_window_class.slider7.valueChanged[int].connect(lambda value: self.gain(value,6/(self.Ts*2*10),7/(self.Ts*2*10)))
        #self.main_window_class.slider8.valueChanged[int].connect(lambda value: self.gain(value,7/(self.Ts*2*10),8/(self.Ts*2*10)))
        #self.main_window_class.slider9.valueChanged[int].connect(lambda value: self.gain(value,8/(self.Ts*2*10),9/(self.Ts*2*10)))
        #self.main_window_class.slider10.valueChanged[int].connect(lambda value: self.gain(value,9/(self.Ts*2*10),10/(self.Ts*2*10)))
        self.main_window_class.combobox_color.currentTextChanged.connect(self.spectrograph)
        self.main_window_class.combobox_interval.currentTextChanged.connect(self.change_interval)
        self.main_window_class.minimum_slider.sliderReleased.connect(self.spectrograph)
        self.main_window_class.max_freq_slider.sliderReleased.connect(self.spectrograph)
        #self.main_window_class.actionOpen_csv.triggered.connect(self.imp_csv)
        self.main_window_class.actionexport_pdf.triggered.connect(self.report)
        self.main_window_class.scrol_slider.valueChanged[int].connect(self.sl)
        self.main_window_class.actionOpen_WAV.triggered.connect(self.imp_audio)
        #self.main_window_class.apply.clicked.connect(self.apply_lim)
    def change_interval(self):
        #self.play_back()
        import time
        self.flag_start_stop=True
        self.interval_value=int(self.main_window_class.combobox_interval.currentText())
        #time.sleep(1)
        #self.flag_start_stop=False
        #time.sleep(1)
        self.play_back()

    def open_newwindow(self):
        child_window()
    def plot_time_domain(self,a=0.05,b=0.05):
        self.main_window_class.scrol_slider.setValue(self.frame_counter)
        range_min=2*int(((self.frame_counter-self.size_per_frame)+abs(self.frame_counter-self.size_per_frame))/2)
        range_max=2*self.frame_counter
        x=self.time[range_min:range_max]
        ya=self.amplitude_original[range_min:range_max]
        yb=np.real(self.amplitude_copy[range_min:range_max])
        self.main_window_class.fig.clear()
        self.main_window_class.fig.subplots(2,1,sharex=True)
        ax=self.main_window_class.fig.axes[0]
        ax2=self.main_window_class.fig.axes[1]
        ax.cla()
        ax2.cla()
        ax.margins(x=a,y=b)
        ax.grid(True)
        ax.set_facecolor((0.29,0.29,0.29))
        ax2.margins(x=a,y=b)
        ax2.set_facecolor((0.29,0.29,0.29))
        ax2.grid(True)
        if a==b:
            ax.set_ylim(min(self.amplitude_original),max(self.amplitude_original))
            if min(np.real(self.amplitude_copy)) == max(np.real(self.amplitude_copy)):
                ax2.set_ylim(min(self.amplitude_original),max(self.amplitude_original))
            else:
                ax2.set_ylim(min(np.real(self.amplitude_copy)),max(np.real(self.amplitude_copy)))
        ax.plot(x,ya,"yellow")
        ax2.plot(x,yb,"yellow")
        self.main_window_class.fig.canvas.draw()
        self.main_window_class.fig.canvas.flush_events()

    def spectrograph(self,processing=0):
        from scipy import signal
        from scipy.fft import fftshift
        import matplotlib.pyplot as plt
        lista=[plt]
        self.main_window_class.fig_spectrogram.clear()
        self.cmap_color = self.main_window_class.combobox_color.currentText()
        ax1 = self.main_window_class.fig_spectrogram.add_subplot(111)
        #scipy.signal.spectrogram(np.real(self.amplitude_copy), fs=1/self.Ts, window='tukey', 0.25, nperseg=None, noverlap=None, nfft=None, detrend='constant', return_onesided=True, scaling='density', axis=- 1, mode='psd')
        #ax1.specgram(np.real(self.amplitude_copy), Fs=1/self.Ts,cmap=self.cmap_color , NFFT=256, noverlap=256/2,)
        #if self.main_window_class.minimum_slider.value()/self.duration<self.main_window_class.max_freq_slider.value()/self.duration:
        #    ax1.set_ylim(self.main_window_class.minimum_slider.value()/self.duration,self.main_window_class.max_freq_slider.value()/self.duration)
        #self.main_window_class.fig_spectrogram.canvas.draw()
        f,t,DBS=self.dBS_calculation()
        if self.main_window_class.minimum_slider.value()>self.main_window_class.max_freq_slider.value():

            spectrogram=ax1.pcolormesh(t, f, DBS, shading='gouraud',cmap=self.cmap_color,vmin=np.min(DBS),vmax=np.max(DBS))
        else:
            spectrogram=ax1.pcolormesh(t, f, DBS, shading='gouraud',cmap=self.cmap_color,vmin=self.main_window_class.minimum_slider.value(),vmax=self.main_window_class.max_freq_slider.value())
        self.main_window_class.fig_spectrogram.colorbar(spectrogram)
        #print(self.main_window_class.minimum_slider.value(),self.main_window_class.max_freq_slider.value())
        self.main_window_class.fig_spectrogram.canvas.draw()
        self.main_window_class.fig_spectrogram.canvas.flush_events()
        if processing:
            if self.main_window_class.minimum_slider.value()>self.main_window_class.max_freq_slider.value():
                plt.pcolormesh(t, f, DBS, shading='gouraud',cmap=self.cmap_color,vmin=np.min(DBS),vmax=np.max(DBS))
            else:
                plt.pcolormesh(t, f, DBS, shading='gouraud',cmap=self.cmap_color,vmin=self.main_window_class.minimum_slider.value(),vmax=self.main_window_class.max_freq_slider.value())
    def variables(self):
        self.Ts = 0
        self.time = []
        self.amplitude_original = []
        self.amplitude_copy=0
        self.frequency_values=0
        self.frequency_values_copy=[]
        self.frequency_values_limits=[]
        self.frame_counter=25
        self.flag_start_stop=False
        self.cmap_color='spring'
        self.interval_value=50
        self.duration=0
        self.size_per_frame=25
    def dBS_calculation(self):
        f, t, Sxx = signal.spectrogram(np.real(self.amplitude_copy),1/self.Ts )
        #DBS=20*np.log10(Sxx)-40
        return f,t,Sxx
    def init(self):
        self.length_amp=len(self.amplitude_original)
        self.amplitude_copy=self.amplitude_original.copy()
        self.frequency_values=rfft(self.amplitude_original)
        self.frequency_values=np.array(self.frequency_values)
        DBS_VALUE=self.dBS_calculation()
        max_min_list_slider=[self.main_window_class.minimum_slider,self.main_window_class.max_freq_slider]
        self.initialize_max_min_sliders(DBS_VALUE[2],max_min_list_slider[0])
        self.initialize_max_min_sliders(DBS_VALUE[2],max_min_list_slider[1])
        self.main_window_class.minimum_slider.setValue(np.min(DBS_VALUE[2]))
        self.main_window_class.max_freq_slider.setValue(np.max(DBS_VALUE[2]))

        for i in range(0,len(self.frequency_values)):
            if np.abs(self.frequency_values[i])<1:
                self.frequency_values[i]=0
        self.frequency_values_copy=list(self.frequency_values).copy()
        self.frequency_values_limits=list(self.frequency_values).copy()
        self.main_window_class.scrol_slider.setMinimum(self.size_per_frame)
        self.main_window_class.scrol_slider.setMaximum(int(self.length_amp/2)-1)
    def initialize_max_min_sliders(self,DBS,slider):
        slider.setMinimum(np.min(DBS))
        slider.setMaximum(np.max(DBS))
    def imp_audio(self):
        self.variables()
        path_audio= QFileDialog.getOpenFileName(None, 'Open WAV ', '/home', "WAV (*.WAV)")[0]
        sampleRate, audioBuffer = scipy.io.wavfile.read(path_audio)
        self.Ts=1/sampleRate
        for i in range(0,len(audioBuffer)):
            self.amplitude_original.append(audioBuffer[i])
        self.time = np.arange(0,len(audioBuffer)*self.Ts,self.Ts)
        self.duration=len(audioBuffer)*self.Ts
        self.length_amp=len(self.amplitude_original)
        self.init()
        self.gain_ranges()
        self.play_back()
    #def imp_csv(self):
    #    self.variables()
    #    path_csv = QFileDialog.getOpenFileName(None, 'Open CSV ', '/home', "CSV (*.csv)")[0]
    #    ds_ecg= pd.read_csv(path_csv)
    #    self.time=ds_ecg.iloc[0:-1,1].values
    #    self.amplitude_original=ds_ecg.iloc[0:-1,2].values
    #    self.Ts=ds_ecg.iloc[0:1,3].values
    #    dur_Rec=ds_ecg.iloc[0:1,4].values
    #    self.duration=float(dur_Rec)
    #    self.Ts=float(self.Ts)
    #    self.length_amp=len(self.amplitude_original)
    #    self.init()
    #    self.gain_ranges()
    #    self.play_back()
    def gain_ranges(self):
        for slider_count in range(10):
            self.sliders_list[slider_count].set_range((slider_count)/(self.Ts*2*10),(slider_count+1)/(self.Ts*2*10))
    def play_back(self):
        self.plot_time_domain()
        self.spectrograph()
        self.main_window_class.fig.clear()
        if self.flag_start_stop == False:
            self.flag_start_stop=True
            c=self.frame_counter
            lines_bef = [ax.plot([],[])[0] for ax in self.main_window_class.fig.axes]
            def update_bef(i):
                self.main_window_class.scrol_slider.setValue(self.frame_counter)
                if not self.flag_start_stop or self.frame_counter>=(int(self.length_amp/2)-self.size_per_frame):
                    self.ani_st_bef.event_source.stop()
                    self.main_window_class.fig.canvas.flush_events()
                    self.plot_time_domain()
                else:
                    self.frame_counter=i+c
                    self.plot_time_domain()
                self.main_window_class.fig.canvas.flush_events()
                return lines_bef
            self.ani_st_bef = FuncAnimation(self.main_window_class.fig, update_bef,interval=int(self.main_window_class.combobox_interval.currentText()),frames=np.arange(0,int((self.length_amp/2))-25),blit=True,repeat=False)
        else:
            self.flag_start_stop=False
    def backword(self):
        if self.frame_counter>self.size_per_frame+10:
            self.frame_counter=self.frame_counter-10
            self.plot_time_domain()
        else:
            print("no enough points")
    def forword(self):
        if self.frame_counter<(self.length_amp/2-10):
            self.frame_counter=self.frame_counter+10
            self.plot_time_domain()
        else:
            print("no enough points")
    def zoomin(self):
        self.plot_time_domain(-0.3,0.05)
    def zoomout(self):
        self.plot_time_domain(0.05,1)

    def gain_calculation(self,i,value):
        self.frequency_values_copy[i]=self.frequency_values[i]*value

    def gain(self,value,minim,maxim):
        fr=rfftfreq(self.length_amp,self.Ts)
        minimum_index=int(minim*self.duration)
        #if maxim==1/(self.Ts*2):
        #    print("true")
        #    print(value)
        #    for i in range (minimum_index,len(self.frequency_values_copy)):
        #        self.gain_calculation(i,value)
        #else:
        maximum_index=int(maxim*self.duration)
        print(minimum_index,maximum_index)
        for i in range (minimum_index,maximum_index):
            self.gain_calculation(i,value)
        #plt.scatter(fr,np.abs(self.frequency_values_copy))
        #plt.show()
        #print(fr(int(maxim)))
        print(len(self.frequency_values_copy))
        self.frequency_values_copy[0]=0
        self.amplitude_copy=irfft(self.frequency_values_copy)
        self.spectrograph()
        self.plot_time_domain()
    def sl(self,value):
        if not self.flag_start_stop:
            self.frame_counter=value
            self.plot_time_domain()
    def report(self):
        fig=plt
        def Signalplot (a,b,c,d):
            plt.subplot(3, 1, 1)
            plt.title("before eq.")
            plt.xlabel('Time')
            plt.ylabel('self.amplitude_original')
            plt.plot(a, b)
            plt.subplot(3, 1, 2)
            plt.title("After eq.")
            plt.xlabel('Time')
            plt.ylabel('self.amplitude_original')
            plt.plot(a, c)
            plt.subplot(3, 1, 3)
            plt.title("Spectrogram")
            plt.xlabel('self.time')
            plt.ylabel('freq')
            #spectrogram_pdf=self.spectrograph(1)
            #f,t,DBS,pcolor=self.dBS_calculation()
            #plt.pcolormesh(t, f, DBS, shading='gouraud',cmap=self.cmap_color,vmin=self.main_window_class.minimum_slider.value(),vmax=self.main_window_class.max_freq_slider.value())
            self.spectrograph(1)
            #plt.specgram(np.real(c), Fs=1/self.Ts,cmap=d , NFFT=256, noverlap=256/2,)
        Signalplot(self.time,self.amplitude_original,self.amplitude_copy,self.cmap_color)
        fig.tight_layout()
        plt.savefig('C:/Users/abdoz/Downloads/Task eq/Report_2_Team_2.pdf')
def window():
    app = QApplication(sys.argv)
    win = ApplicationWindow()
    win.show()
    sys.exit(app.exec_())
    return win

def child_window():
    win = ApplicationWindow()
    win.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    ases=window()

{'time': 'time', 'signal': 'scipy.signal', 'fftshift': 'scipy.fft.fftshift', 'plt': 'matplotlib.pyplot'}


from builtins import str
from builtins import object
import sys
import threading

from queue import PriorityQueue
from collections import namedtuple
from datetime import datetime, timedelta

import pytz
import html

from sqlalchemy import Column, Integer, ForeignKey, Unicode, Enum, func
from monitorrent.db import Base, DBSession, row2dict, UTCDateTime
from monitorrent.utils.timers import timer

class Logger(object):
    def started(self, start_time):
        """
        """

    def finished(self, finish_time, exception):
        """
        """

    def info(self, message):
        """
        """

    def failed(self, message):
        """
        """

    def downloaded(self, message, torrent):
        """
        """

class Engine(object):
    def __init__(self, logger, clients_manager):
        """

        :type logger: Logger
        :type clients_manager: plugin_managers.ClientsManager
        """
        self.log = logger
        self.clients_manager = clients_manager

    def find_torrent(self, torrent_hash):
        return self.clients_manager.find_torrent(torrent_hash)

    def add_torrent(self, filename, torrent, old_hash):
        """
        :type filename: str
        :type old_hash: str | None
        :type torrent: Torrent
        :rtype: datetime
        """
        existing_torrent = self.find_torrent(torrent.info_hash)
        if existing_torrent:
            self.log.info(u"Torrent %s already added" % filename)
        elif self.clients_manager.add_torrent(torrent.raw_content):
            old_existing_torrent = self.find_torrent(old_hash) if old_hash else None
            if old_existing_torrent:
                self.log.info(u"Updated %s" % filename)
            else:
                self.log.info(u"Add new %s" % filename)
            if old_existing_torrent:
                if self.remove_torrent(old_hash):
                    self.log.info(u"Remove old torrent %s" %
                                  html.escape(old_existing_torrent['name']))
                else:
                    self.log.failed(u"Can't remove old torrent %s" %
                                    html.escape(old_existing_torrent['name']))
            existing_torrent = self.find_torrent(torrent.info_hash)
        if not existing_torrent:
            raise Exception('Torrent {0} wasn\'t added'.format(filename))
        return existing_torrent['date_added']

    def remove_torrent(self, torrent_hash):
        return self.clients_manager.remove_torrent(torrent_hash)

class ExecuteSettings(Base):
    __tablename__ = "settings_execute"

    id = Column(Integer, primary_key=True)
    interval = Column(Integer, nullable=False)
    last_execute = Column(UTCDateTime, nullable=True)

class Execute(Base):
    __tablename__ = 'execute'

    id = Column(Integer, primary_key=True)
    start_time = Column(UTCDateTime, nullable=False)
    finish_time = Column(UTCDateTime, nullable=False)
    status = Column(Enum('finished', 'failed'), nullable=False)
    failed_message = Column(Unicode, nullable=True)

class ExecuteLog(Base):
    __tablename__ = 'execute_log'

    id = Column(Integer, primary_key=True)
    execute_id = Column(ForeignKey('execute.id'))
    time = Column(UTCDateTime, nullable=False)
    message = Column(Unicode, nullable=False)
    level = Column(Enum('info', 'warning', 'failed', 'downloaded'), nullable=False)

class DbLoggerWrapper(Logger):
    def __init__(self, logger, log_manager, settings_manager=None):
        """
        :type logger: Logger | None
        :type log_manager: ExecuteLogManager
        :type settings_manager: SettingsManager | None
        """
        self._log_manager = log_manager
        self._logger = logger
        self._settings_manager = settings_manager

    def started(self, start_time):
        self._log_manager.started(start_time)
        if self._logger:
            self._logger.started(start_time)

    def finished(self, finish_time, exception):
        self._log_manager.finished(finish_time, exception)
        if self._logger:
            self._logger.finished(finish_time, exception)
        if self._settings_manager:
            self._log_manager.remove_old_entries(self._settings_manager.remove_logs_interval)

    def info(self, message):
        self._log_manager.log_entry(message, 'info')
        if self._logger:
            self._logger.info(message)

    def failed(self, message):
        self._log_manager.log_entry(message, 'failed')
        if self._logger:
            self._logger.failed(message)

    def downloaded(self, message, torrent):
        self._log_manager.log_entry(message, 'downloaded')
        if self._logger:
            self._logger.downloaded(message, torrent)

# noinspection PyMethodMayBeStatic
class ExecuteLogManager(object):
    _execute_id = None

    def started(self, start_time):
        if self._execute_id is not None:
            raise Exception('Execute already in progress')

        with DBSession() as db:
            # default values for not finished execute is failed and finish_time equal to start_time
            execute = Execute(start_time=start_time, finish_time=start_time, status='failed')
            db.add(execute)
            db.commit()
            self._execute_id = execute.id

    def finished(self, finish_time, exception):
        if self._execute_id is None:
            raise Exception('Execute is not started')

        with DBSession() as db:
            # noinspection PyArgumentList
            execute = db.query(Execute).filter(Execute.id == self._execute_id).first()
            execute.status = 'finished' if exception is None else 'failed'
            execute.finish_time = finish_time
            if exception is not None:
                execute.failed_message = html.escape(str(exception))
        self._execute_id = None

    def log_entry(self, message, level):
        if self._execute_id is None:
            raise Exception('Execute is not started')

        with DBSession() as db:
            execute_log = ExecuteLog(execute_id=self._execute_id, time=datetime.now(pytz.utc),
                                     message=message, level=level)
            db.add(execute_log)

    def get_log_entries(self, skip, take):
        with DBSession() as db:
            downloaded_sub_query = db.query(ExecuteLog.execute_id, func.count(ExecuteLog.id).label('count'))\
                .group_by(ExecuteLog.execute_id, ExecuteLog.level)\
                .having(ExecuteLog.level == 'downloaded')\
                .subquery()
            failed_sub_query = db.query(ExecuteLog.execute_id, func.count(ExecuteLog.id).label('count'))\
                .group_by(ExecuteLog.execute_id, ExecuteLog.level)\
                .having(ExecuteLog.level == 'failed')\
                .subquery()

            result_query = db.query(Execute, downloaded_sub_query.c.count, failed_sub_query.c.count)\
                .outerjoin(failed_sub_query, Execute.id == failed_sub_query.c.execute_id)\
                .outerjoin(downloaded_sub_query, Execute.id == downloaded_sub_query.c.execute_id)\
                .order_by(Execute.finish_time.desc())\
                .offset(skip)\
                .limit(take)

            result = []
            for execute, downloads, fails in result_query.all():
                execute_result = row2dict(execute)
                execute_result['downloaded'] = downloads or 0
                execute_result['failed'] = fails or 0
                execute_result['is_running'] = execute.id == self._execute_id
                result.append(execute_result)

            execute_count = db.query(func.count(Execute.id)).scalar()

        return result, execute_count

    def remove_old_entries(self, prune_days):
        # SELECT id FROM execute WHERE start_time <= datetime('now', '-10 days') ORDER BY id DESC LIMIT 1
        with DBSession() as db:
            prune_date = datetime.now(pytz.utc) - timedelta(days=prune_days)
            execute_id = db.query(Execute.id)\
                .filter(Execute.start_time <= prune_date)\
                .order_by(Execute.id.desc())\
                .limit(1)\
                .scalar()

            if execute_id is not None:
                db.query(ExecuteLog)\
                    .filter(ExecuteLog.execute_id <= execute_id)\
                    .delete(synchronize_session=False)

                db.query(Execute)\
                    .filter(Execute.id <= execute_id)\
                    .delete(synchronize_session=False)

    def is_running(self, execute_id=None):
        if execute_id is not None:
            return self._execute_id == execute_id
        return self._execute_id is not None

    def get_execute_log_details(self, execute_id, after=None):
        with DBSession() as db:
            filters = [ExecuteLog.execute_id == execute_id]
            if after is not None:
                filters.append(ExecuteLog.id > after)
            log_entries = db.query(ExecuteLog).filter(*filters).all()
            return [row2dict(e) for e in log_entries]

    def get_current_execute_log_details(self, after=None):
        if self._execute_id is None:
            return None

        return self.get_execute_log_details(self._execute_id, after)

class EngineRunner(threading.Thread):
    RunMessage = namedtuple('RunMessage', ['priority', 'ids'])
    StopMessage = namedtuple('StopMessage', ['priority'])

    def __init__(self, logger, trackers_manager, clients_manager, **kwargs):
        """
        :type logger: Logger
        :type trackers_manager: plugin_managers.TrackersManager
        :type clients_manager: plugin_managers.ClientsManager
        """
        interval_param = kwargs.pop('interval', None)
        last_execute_param = kwargs.pop('last_execute', None)

        super(EngineRunner, self).__init__(**kwargs)
        self.logger = logger
        self.trackers_manager = trackers_manager
        self.clients_manager = clients_manager
        self.is_executing = False
        self.is_stoped = False
        self._interval = float(interval_param) if interval_param else 7200
        self._last_execute = last_execute_param
        self.message_box = PriorityQueue()

        self.timer_cancel = None
        self._create_timer()

        self.start()

    @property
    def interval(self):
        return self._interval

    @interval.setter
    def interval(self, value):
        self._interval = value
        self._create_timer()

    @property
    def last_execute(self):
        return self._last_execute

    @last_execute.setter
    def last_execute(self, value):
        self._last_execute = value

    # noinspection PyBroadException
    def run(self):
        while not self.is_stoped:
            msg = self._receive()

            if isinstance(msg, EngineRunner.StopMessage):
                self.is_stoped = True
                self.timer_cancel()
                return

            ids = msg.ids \
                if isinstance(msg, EngineRunner.RunMessage) \
                else None

            try:
                self._execute(ids=ids)
            except:
                pass

    def stop(self):
        self.message_box.put(EngineRunner._stop_message())

    def execute(self, ids):
        if not self.is_executing:
            msg = EngineRunner._run_message(ids=ids)
            self.message_box.put_nowait(msg)

    def _create_timer(self):
        def timer_fn():
            msg = EngineRunner._run_message()
            self.message_box.put_nowait(msg)

        if self.timer_cancel is not None:
            self.timer_cancel()

        self.timer_cancel = timer(self.interval, timer_fn)

    def _receive(self):
        return self.message_box.get(block=True)

    # noinspection PyBroadException
    def _execute(self, ids=None):
        caught_exception = None
        self.is_executing = True
        try:
            self.logger.started(datetime.now(pytz.utc))
            self.trackers_manager.execute(Engine(self.logger, self.clients_manager), ids)
        except:
            caught_exception = sys.exc_info()[0]
        finally:
            self.is_executing = False
            self.last_execute = datetime.now(pytz.utc)
            self.logger.finished(self.last_execute, caught_exception)
        return True

    @staticmethod
    def _run_message(ids=None):
        return EngineRunner.RunMessage(priority=1, ids=ids)

    @staticmethod
    def _stop_message():
        return EngineRunner.StopMessage(priority=0)

class DBEngineRunner(EngineRunner):
    DEFAULT_INTERVAL = 7200

    def __init__(self, logger, trackers_manager, clients_manager, **kwargs):
        """
        :type logger: Logger
        :type trackers_manager: plugin_managers.TrackersManager
        :type clients_manager: plugin_managers.ClientsManager
        """
        execute_settings = self._get_execute_settings()
        super(DBEngineRunner, self).__init__(logger,
                                             trackers_manager,
                                             clients_manager,
                                             interval=execute_settings.interval,
                                             last_execute=execute_settings.last_execute,
                                             **kwargs)

    @property
    def interval(self):
        return self._interval

    @interval.setter
    def interval(self, value):
        self._interval = value
        self._create_timer()
        self._update_execute_settings()

    @property
    def last_execute(self):
        return self._last_execute

    @last_execute.setter
    def last_execute(self, value):
        self._last_execute = value
        self._update_execute_settings()

    def _update_execute_settings(self):
        with DBSession() as db:
            settings_execute = db.query(ExecuteSettings).first()
            if not settings_execute:
                settings_execute = ExecuteSettings()
                db.add(settings_execute)
            settings_execute.interval = self._interval
            settings_execute.last_execute = self._last_execute

    def _get_execute_settings(self):
        with DBSession() as db:
            settings_execute = db.query(ExecuteSettings).first()
            if not settings_execute:
                settings_execute = ExecuteSettings(interval=self.DEFAULT_INTERVAL, last_execute=None)
            else:
                db.expunge(settings_execute)
        return settings_execute


import logging

class Test:
    def __init__(self, documents):
        self.documents = documents
        self.correct_states = 0
        self.total_states = sum([len(document) for document in documents])
        self.correct_documents = 0
        self.total_documents = len(documents)

    def test_decoder(self, decoder, lowercase=False):
        self.correct_states = 0
        self.correct_documents = 0
        for i, document in enumerate(self.documents):
            logging.info("Decoder test for document %s", i)
            observations = []
            true_states = []
            for observation, state in document:
                observations.append(observation)
                true_states.append(state)
            if lowercase:
                observations = [observation.lower() for observation in observations]
            decoded_sequence = decoder.decode(observations)
            if decoded_sequence == true_states:
                self.correct_documents += 1
            for i, decoded_state in enumerate(decoded_sequence):
                if decoded_state == true_states[i]:
                    self.correct_states += 1
        return self.correct_states/self.total_states, self.correct_documents/self.total_documents


import pdfplumber
import os
import concurrent.futures
from functools import wraps
import time
import pandas as pd
import pickle
from datetime import date

def timeit(method):
    @wraps(method)
    def wrapper(*args, **kwargs):
        start_time = time.time()
        result = method(*args, **kwargs)
        end_time = time.time()
        print(f"{method.__name__} => {(end_time-start_time)*1000} ms")

        return result

    return wrapper

@timeit
def extract_pdf(filename, directory):
    filename = os.fsdecode(filename)
    allpages_text = []
    data = []
    if filename.endswith('.pdf'):
        with pdfplumber.open(directory + filename) as pdf:
            for page in pdf.pages:
                text = page.extract_text()
                allpages_text.append(text)
            allpages_text = '-'.join(allpages_text)
            data.append(allpages_text)
            data.append(filename)

            return data

@timeit
def extract_all(directory):

    '''
    Uses extract_pdf to extract multiple PDFs within a directory.
    '''

    data = []

    with concurrent.futures.ProcessPoolExecutor(max_workers=8) as executor:

        futures = {executor.submit(extract_pdf, filename, directory): filename for filename in os.listdir(directory)}

        for future in concurrent.futures.as_completed(futures):
            try:
                result = future.result()
                data.append(result)
            except Exception as exc:
                print("There was an error. It is defined as {}".format(exc))
    return data

@timeit
def extract_all_singleprocess(directory):
    return [extract_pdf(filename, directory) for filename in os.listdir(directory)]

if __name__ == '__main__':
    # filename = '224.pdf'
    directory = r'../files/' # r'C:/Users/peter/research_files/supreme_court/'

    results = extract_all(directory)
    with open(r'../data/pdfextracts_{}.pkl'.format(date.today()), 'wb') as file:
        pickle.dump(results, file)


import os
import time
from datetime import datetime

from cnaas_nac.db.accounting import Accounting
from cnaas_nac.db.nas import NasPort
from cnaas_nac.db.user import Reply, User, UserInfo
from cnaas_nac.tools.log import get_logger

logger = get_logger()
ONE_MONTH = 2629800

def users_cleanup():
    if "NO_CLEANUP" in os.environ:
        logger.info("Aborting cleanup.")
        return ""

    users = User.get()

    logger.info("Cleaning up users...")

    for user in users:
        username = user["username"]

        # Skip enabled users
        if user["op"] != "":
            logger.info("User {} active, skipping".format(username))
            continue

        userinfo = UserInfo.get([username])
        authdate = userinfo[username]["authdate"]

        if authdate is None or authdate == "":
            logger.info("No timestamp for user {}, skipping".format(username))
            continue

        authdate_epoch = int(authdate.timestamp())
        current_epoch = int(time.time())

        if current_epoch - authdate_epoch >= ONE_MONTH:
            logger.info("Removing user {}".format(user["username"]))
            User.delete(user["username"])
            Reply.delete(user["username"])
            NasPort.delete(user["username"])
        else:
            logger.info("Keeping user {}".format(user["username"]))

    return ""

def accounting_cleanup():
    removed_items = 0

    logger.info("Cleaning up accounting...")

    if "NO_CLEANUP" in os.environ:
        logger.info("Aborting cleanup.")
        return ""

    for account in Accounting.get():
        if "acctstoptime" in account:
            acctstoptime = account["acctstoptime"]
        else:
            continue

        if not acctstoptime:
            continue

        epoch_stop = round(datetime.strptime(
            acctstoptime, "%Y-%m-%d %H:%M:%S+00:00").timestamp())
        current_epoch = int(time.time())

        if current_epoch - epoch_stop >= ONE_MONTH:
            try:
                Accounting.delete(account["username"], account["acctstoptime"])
            except Exception:
                logger.info("Failed to remove account: {} {}".format(
                    account["username"], account["acctstoptime"]))
                continue
            removed_items += 1

    logger.info(f"Cleaned up {removed_items} items from radacct")

if __name__ == "__main__":
    users_cleanup()
    accounting_cleanup()


import socket

from eventlet.event import Event
from eventlet.greenpool import GreenPool
import eventlet
import pytest

from kombu import Connection

from nameko import exceptions
from nameko import service
from nameko.testing import TestProxy
from nameko.timer import timer

def test_service(get_connection):
    class Controller(object):
        def test_method(self, context, foo):
            return foo + 'spam'

    srv = service.Service(
        Controller, connection_factory=get_connection,
        exchange='testrpc', topic='test', )
    srv.start()
    eventlet.sleep()

    try:
        test = TestProxy(get_connection, timeout=3).test
        ret = test.test_method(foo='bar')

        assert ret == 'barspam'
    finally:
        srv.kill()

def test_service_socket_error():

    class FailConnection(Connection):
        # a connection that will give a socket.error
        # when the service starts
        def drain_events(self, *args, **kwargs):
            raise socket.error

    def get_connection():
        return FailConnection(transport='memory')

    class Controller(object):
        def test_method(self, context, foo):
            return foo + 'spam'

    srv = service.Service(
        Controller, connection_factory=get_connection,
        exchange='testrpc', topic='test', )

    try:
        srv.start()
        eventlet.sleep()
        with pytest.raises(socket.error):
            # we expect that a socket.error has been raised
            # within the startup greenthread
            # calling .wait() should raise this returned error
            srv.greenlet.wait()

    finally:
        srv.kill()

def test_service_doesnt_exhaust_pool(get_connection):
    POOLSIZE = 10

    class Controller(object):
        def test_method(self, context, foo):
            return foo + 'spam'

    srv = service.Service(
        Controller, connection_factory=get_connection,
        exchange='testrpc', topic='test', poolsize=POOLSIZE)
    srv.start()
    eventlet.sleep()

    try:
        with eventlet.Timeout(10):
            for i in range(POOLSIZE * 2):
                test = TestProxy(get_connection, timeout=3).test
                test.test_method(foo='bar')

    finally:
        srv.kill()

def test_exceptions(get_connection):
    class Controller(object):
        def test_method(self, context, **kwargs):
            raise KeyError('foo')

    srv = service.Service(
        Controller, connection_factory=get_connection,
        exchange='testrpc', topic='test', )
    srv.start()
    eventlet.sleep()

    try:
        test = TestProxy(get_connection, timeout=3).test
        with pytest.raises(exceptions.RemoteError):
            test.test_method()

        with pytest.raises(exceptions.RemoteError):
            test.test_method_does_not_exist()
    finally:
        srv.kill()

def test_service_wait(get_connection):
    # TODO: this is a coverage test,
    #       I am not sure the behavior we test is actually what we want.

    spam_continue = Event()
    spam_called = Event()

    class Foobar(object):
        def spam(self, context, **kwargs):
            spam_called.send(1)
            # lets wait until we are told to stop,
            # pretending we are working really hard
            spam_continue.wait()

    srv = service.Service(
        Foobar, connection_factory=get_connection,
        exchange='testrpc', topic='test', )

    srv.start()
    eventlet.sleep()

    try:
        with eventlet.timeout.Timeout(5):
            foobar = TestProxy(get_connection, timeout=3).test
            # lets make sure we actually have a worker currently
            # processing an RPC call
            rpc_call = eventlet.spawn(foobar.spam)
            spam_called.wait()

            # we are in the middle of an RPC call, this will make sure
            # that wait() will have to wait for the worker and the queue
            kill_thread = eventlet.spawn(srv.kill)
            # Let the worker finish it's job
            spam_continue.send(1)
            rpc_call.wait()
            kill_thread.wait()

    except eventlet.timeout.Timeout as t:
        pytest.fail('waiting for death of service timed out: {}'.format(t))
    finally:
        srv.kill()

def test_service_wait_consumer_dies(get_connection):
    srv = service.Service(
        object, connection_factory=get_connection,
        exchange='testrpc', topic='test', )

    srv.start()
    eventlet.sleep()
    srv.greenlet.kill()
    # we don't expect any errors while waiting
    srv.kill()

def test_service_cancells_timers(get_connection):
    timer_evt = Event()

    class Foobar(object):
        @timer(0)
        def shrub(self):
            try:
                timer_evt.send(True)
            except:
                pass

    srv = service.Service(
        Foobar, connection_factory=get_connection,
        exchange='testrpc', topic='test', )

    srv.start()
    # wait for the timer to be called once
    timer_evt.wait()
    # this should stop the timers and we should not get any errors
    srv.kill()
    assert len(srv._timers) == 0

def test_service_link(get_connection):
    srv = service.Service(
        object, connection_factory=get_connection,
        exchange='testrpc', topic='test', )

    srv.start()

    complete_called = []

    def complete(gt, spam, foo):
        assert spam == 'spam'
        assert foo == 'bar'
        assert gt == srv.greenlet
        complete_called.append(True)

    srv.link(complete, 'spam', foo='bar')

    srv.kill()

    assert complete_called == [True]

def test_service_cannot_be_started_twice(get_connection):
    srv = service.Service(
        object, connection_factory=get_connection,
        exchange='testrpc', topic='test')

    srv.start()
    with pytest.raises(RuntimeError):
        srv.start()

    srv.kill()

def test_service_requeues_if_out_of_workers(get_connection):
    foobar_continue = Event()
    foobar_called = Event()

    class Foobar(object):
        def spam(self, context):
            foobar_called.send(True)
            with eventlet.Timeout(2):
                foobar_continue.wait()

    s1 = service.Service(
        Foobar, connection_factory=get_connection,
        exchange='testrpc', topic='test', poolsize=1)

    s1.start()
    eventlet.sleep()

    foobar = TestProxy(get_connection, timeout=3).test

    w1 = eventlet.spawn(foobar.spam)
    foobar_called.wait()
    # the servcie should now be busy and requeue the next request
    w2 = eventlet.spawn(foobar.spam)
    eventlet.sleep()

    # we create a second service to pick up the message
    spam_received = Event()

    class Spam(object):
        def spam(self, context):
            spam_received.send(True)

    s2 = service.Service(
        Spam, connection_factory=get_connection,
        exchange='testrpc', topic='test', poolsize=1)

    s2.start()
    eventlet.sleep()
    # wait for the 2nd service to pick up the message
    with eventlet.Timeout(2):
        assert spam_received.wait()

    # be nice to the first service and tell it to stop being busy
    foobar_continue.send(True)

    w1.wait()
    w2.wait()
    s1.kill()
    s2.kill()

def test_service_custom_pool(get_connection):
    class Foobar(object):
        def spam(self, context, **kwargs):
            pass

    class MyPool(object):
        _pool = GreenPool(size=10)
        count = 0

        def spawn(self, *args, **kwargs):
            self.count += 1
            return self._pool.spawn(*args, **kwargs)

        def waitall(self):
            return self._pool.waitall()

        @property
        def size(self):
            return self._pool.size

        def free(self):
            return self._pool.free()

        def running(self):
            return self._pool.running()

    pool = MyPool()
    srv = service.Service(
        Foobar, connection_factory=get_connection,
        exchange='testrpc', topic='test', pool=pool)

    srv.start()
    srv.consume_ready.wait()

    foobar = TestProxy(get_connection, timeout=3).test
    foobar.spam()

    srv.kill()
    assert pool.count == 1

def test_force_kill(get_connection):
    spam_continue = Event()
    spam_called = Event()

    class Foobar(object):
        def spam(self, context):
            spam_called.send(1)
            spam_continue.wait()

    srv = service.Service(
        Foobar, connection_factory=get_connection,
        exchange='testrpc', topic='test', poolsize=1)
    srv.start()
    eventlet.sleep()

    foobar = TestProxy(get_connection, timeout=3).test
    eventlet.spawn(foobar.spam)

    spam_called.wait()
    # spam will not complete, so we force it
    srv.kill(force=True)

    assert srv.greenlet.dead

def test_force_kill_no_workers(get_connection):
    spam_continue = Event()
    spam_called = Event()

    class Foobar(object):
        def spam(self, context):
            spam_called.send(1)
            spam_continue.wait()

    srv = service.Service(
        Foobar, connection_factory=get_connection,
        exchange='testrpc', topic='test', poolsize=1)
    srv.start()
    eventlet.sleep()

    foobar = TestProxy(get_connection, timeout=3).test
    eventlet.spawn(foobar.spam)

    spam_called.wait()
    spam_continue.send(2)
    # spam has completed - but force anyway
    srv.kill(force=True)

    assert srv.greenlet.dead

def test_service_dos(get_connection):
    spam_continue = Event()
    spam_called = Event()

    class Foobar(object):
        def spam(self, context, do_wait=True):
            if do_wait:
                spam_called.send(1)
                spam_continue.wait()

    s1 = service.Service(
        Foobar, connection_factory=get_connection,
        exchange='testrpc', topic='test', poolsize=1)
    s1.start()
    eventlet.sleep()

    foobar = TestProxy(get_connection, timeout=10).test
    # lets call spam such that it will block until we tell it to continue
    spam_call_1 = eventlet.spawn(foobar.spam, do_wait=True)
    eventlet.sleep()
    spam_called.wait()

    # At this point we will have exhausted it's prefetch count and worker-pool
    # s1 should not accept any more RPCs as it is busy processing spam().
    # Lets kick off two more RPC anyways, which should just wait in the queue
    spam_call_2 = eventlet.spawn(foobar.spam, do_wait=False)
    spam_call_3 = eventlet.spawn(foobar.spam, do_wait=False)
    eventlet.sleep()

    # lets fire up another service, which should take care of the 2 extra calls
    s2 = service.Service(
        Foobar, connection_factory=get_connection,
        exchange='testrpc', topic='test')

    s2.start()
    s2.consume_ready.wait()
    eventlet.sleep()

    try:
        with eventlet.timeout.Timeout(5):
            # as soon as s2 is up and running, the 2 extra calls should
            # get a reply
            spam_call_2.wait()
            spam_call_3.wait()

            # we can let the first call continue now
            spam_continue.send(1)
            spam_call_1.wait()
    except eventlet.timeout.Timeout as t:
        pytest.fail('waiting for 2nd server timed out: {}'.format(t))
    finally:
        s1.kill()
        s2.kill()


#!/usr/bin/env python

import argparse
import os
import time
from pprint import pprint

import googleapiclient.discovery
import google.auth
import google.oauth2.service_account as service_account

#
# Use Google Service Account - See https://google-auth.readthedocs.io/en/latest/reference/google.oauth2.service_account.html#module-google.oauth2.service_account
#
credentials = service_account.Credentials.from_service_account_file(filename='service-credentials.json')
project = os.getenv('GOOGLE_CLOUD_PROJECT') or 'datacenter5'
service = googleapiclient.discovery.build('compute', 'v1', credentials=credentials)

bucket = 'lab-5bucket'
zone = 'us-central1-f'
disk = 'demo-2'

def list_instances(compute, project, zone):
    result = compute.instances().list(project=project, zone=zone).execute()
    return result['items'] if 'items' in result else None

def create_instance(compute, project, zone, name, bucket):
    # Get the latest Debian Jessie image.
    image_response = compute.images().getFromFamily(
        project='ubuntu-os-cloud', family='ubuntu-1804-lts').execute()
    source_disk_image = image_response['selfLink']

    # Configure the machine
    machine_type = "zones/%s/machineTypes/f1-micro" % zone
    startup_script = open(
        os.path.join(
            os.path.dirname(__file__), 'startup-script.sh'), 'r').read()
    image_url = "http://storage.googleapis.com/gce-demo-input/photo.jpg"
    image_caption = "Ready for dessert?"

    config = {
        'name': name,
        'machineType': machine_type,

        # Specify the boot disk and the image to use as a source.
        'disks': [
            {
                'boot': True,
                'autoDelete': True,
                'initializeParams': {
                    'sourceImage': source_disk_image,
                }
            }
        ],

        # Specify a network interface with NAT to access the public
        # internet.
        'networkInterfaces': [{
            'network': 'global/networks/default',
            'accessConfigs': [
                {'type': 'ONE_TO_ONE_NAT', 'name': 'External NAT'}
            ]
        }],

        # Allow the instance to access cloud storage and logging.
        'serviceAccounts': [{
            'email': 'default',
            'scopes': [
                'https://www.googleapis.com/auth/devstorage.read_write',
                'https://www.googleapis.com/auth/logging.write'
            ]
        }],

        # Metadata is readable from the instance and allows you to
        # pass configuration from deployment scripts to instances.
        'metadata': {
            'items': [{
                # Startup script is automatically executed by the
                # instance upon startup.
                'key': 'startup-script',
                'value': startup_script
            }, {
                'key': 'url',
                'value': image_url
            }, {
                'key': 'text',
                'value': image_caption
            }, {
                'key': 'bucket',
                'value': bucket
            }]
        }
    }

    return compute.instances().insert(
        project=project,
        zone=zone,
        body=config).execute()
# [END create_instance]

# [START delete_instance]
def delete_instance(compute, project, zone, name):
    return compute.instances().delete(
        project=project,
        zone=zone,
        instance=name).execute()
# [END delete_instance]

# [START wait_for_operation]
def wait_for_operation(compute, project, zone, operation):
    print('Waiting for operation to finish...')
    while True:
        result = compute.zoneOperations().get(
            project=project,
            zone=zone,
            operation=operation).execute()

        if result['status'] == 'DONE':
            print("done.")
            if 'error' in result:
                raise Exception(result['error'])
            return result

        time.sleep(1)
# [END wait_for_operation]

# [START run]
def main(project, bucket, zone, instance_name, wait=True):
    compute = googleapiclient.discovery.build('compute', 'v1')

    print('Creating instance.')

    operation = create_instance(compute, project, zone, instance_name, bucket)
    wait_for_operation(compute, project, zone, operation['name'])

    instances = list_instances(compute, project, zone)

    print('Instances in project %s and zone %s:' % (project, zone))
    for instance in instances:
        print(' - ' + instance['name'])

main(project, bucket, zone, 'via-service-2')

# snapshot_body = {
# 	"name": "base-snapshot-demo-2"
#     # TODO: Add desired entries to the request body.
# }

# request = service.disks().createSnapshot(project=project, zone=zone, disk=disk, body=snapshot_body)
# response = request.execute()

print("Your running instances are:")
for instance in list_instances(service, project, 'us-west1-b'):
    print(instance['name'])


import numpy as np
import cv2
import time
from grabscreen.grabscreen import grab_screen

def capture_full_screen():

    captured_screen = grab_screen(region=(0,27,640,425))
    rgb_full_screen = cv2.cvtColor(captured_screen, cv2.COLOR_BGR2RGB)
    return rgb_full_screen

#drivingwindow
def capture_drivingwindow():

    captured_screen = grab_screen(region=(0,140,640,245))
    rgb_drivingwindow = cv2.cvtColor(captured_screen, cv2.COLOR_BGR2RGB)
    return rgb_drivingwindow

#race map
def capture_map():

    captured_screen = grab_screen(region=(0,27,170,100))
    rgb_map = cv2.cvtColor(captured_screen, cv2.COLOR_BGR2RGB)
    return rgb_map

#scoreboard
def capture_scoreboard():

    captured_screen = grab_screen(region=(490,31,630,88))
    rgb_scoreboard = cv2.cvtColor(captured_screen, cv2.COLOR_BGR2RGB)
    return rgb_scoreboard

#speedometer
def capture_speedometer():

    captured_screen = grab_screen(region=(205,250,255,275))
    rgb_speedometer = cv2.cvtColor(captured_screen, cv2.COLOR_BGR2RGB)
    return rgb_speedometer

#health meter
def capture_healthmeter():

    captured_screen = grab_screen(region=(268,250,435,270))
    rgb_healthmeter = cv2.cvtColor(captured_screen, cv2.COLOR_BGR2RGB)
    return rgb_healthmeter

#RPM gauge
def capture_RPMgauge():

    captured_screen = grab_screen(region=(255,295,385,395))
    rgb_RPMgauge = cv2.cvtColor(captured_screen, cv2.COLOR_BGR2RGB)
    return rgb_RPMgauge

#Steering wheel
def capture_steeringwheel():

    captured_screen = grab_screen(region=(180,270,460,420))
    rgb_steeringwheel = cv2.cvtColor(captured_screen, cv2.COLOR_BGR2RGB)
    return rgb_steeringwheel

#Gear box to shift gears
def capture_gears():

    captured_screen = grab_screen(region=(525,325,610,410))
    rgb_gears = cv2.cvtColor(captured_screen, cv2.COLOR_BGR2RGB)
    return rgb_gears

#Left rearview mirror
def capture_leftRvm():

    captured_screen = grab_screen(region=(0,250,130,300))
    rgb_leftRvm = cv2.cvtColor(captured_screen, cv2.COLOR_BGR2RGB)
    return rgb_leftRvm

#Right rearview mirror
def capture_rightRvm():

    captured_screen = grab_screen(region=(510,250,(510+130),300))
    rgb_rightRvm = cv2.cvtColor(captured_screen, cv2.COLOR_BGR2RGB)
    return rgb_rightRvm

if __name__ == "__main__":

    while True:

        #Capture game window and convert from BGR to RGB
        full_screen = capture_full_screen()
        drivingwindow = capture_drivingwindow()
        map_image = capture_map()
        scoreboard = capture_scoreboard()
        speedometer = capture_speedometer()
        healthmeter = capture_healthmeter()
        RPMgauge = capture_RPMgauge()
        steeringwheel = capture_steeringwheel()
        gears = capture_gears()
        leftRvm = capture_leftRvm()
        rightRvm = capture_rightRvm()

        #cv2.imshow('full screen',full_screen)
        cv2.imshow('driving window',drivingwindow)
        #cv2.imshow('map',map_image)
        #cv2.imshow('scoreboard',scoreboard)
        #cv2.imshow('speedometer',speedometer)
        #cv2.imshow('healthmeter',healthmeter)
        #cv2.imshow('RPMgauge',RPMgauge)
        cv2.imshow('steering wheel',steeringwheel)
        #cv2.imshow('gears',gears)
        #cv2.imshow('leftRvm',leftRvm)
        #cv2.imshow('rightRvm',rightRvm)

        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break


# Claudia Trejo, 2017.05.23
# Chapter 2  Writting Test Using unittest

# Test suites
import unittest
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from builtins import classmethod

class HomePageTest(unittest.TestCase):

	@classmethod
	def setUpClass(cls):
		URL = "https://magento.com/products/community-edition"
		#Create a new Firefox session
		cls.driver = webdriver.Firefox()
		cls.driver.implicitly_wait(30)
		cls.driver.maximize_window()
		# Navigate to the application home page
		cls.driver.get(URL)
		cls.driver.title

	def test_email_field(self):
		# check contact field exists on Home page
		self.assertTrue(self.is_element_present(By.NAME, "Email"))

	def test_menu_option(self):
		# check menu options exists on Home page
		self.assertTrue(self.is_element_present(By.ID, "block-tb-megamenu-menu-magento-header-primary-v3"))

	def test_email_field_empty_message(self):
		# check content of Stay Informed block on Home page
		email_field_box = \
			self.driver.find_element_by_css_selector\
				("") #//*[@class='mktoButton']
		email_field_icon.click()

		email_field_message = \
				self.driver.find_element_by_css_selector("").text #//*[@name='Email']
		self.assertEqual\
			("Must be valid email.example@yourdomain.com", email_field_message)

	@classmethod
	def tearDownClass(cls):
			#close the browser window
			cls.driver.quit()

	def is_element_present(self, how, what):
		"""
        Utility method to check presence of an element on page
        :params how: By locator type
        :params what: locator value
        """
		try:
			self.driver.find_element(by=how, value=what)
		except NoSuchElementException as e:
			return False
		return True

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


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

# --------------------------------------------------------------------------- #
__author__ = "Juan Luis Padilla Salomé"
__copyright__ = "Copyright 2021"
__credits__ = ["University of Cantabria", "Pablo Abad", "Pablo Prieto"]
__license__ = "GPL"
__version__ = "1.0.0"
__maintainer__ = "Juan Luis Padilla Salomé"
__email__ = "juan-luis.padilla@alumnos.unican.es"
__status__ = "Production"
# --------------------------------------------------------------------------- #

# --------------------------------------------------------------------------- #
if __name__ == "__main__":
    """
    TODO
    """

    # standard library
    import numpy as np
    import os

    # Forces the program to execute on CPU
    os.environ['CUDA_VISIBLE_DEVICES'] = '0'

    # Just disables the warning, doesn't take advantage of AVX/FMA to run faster
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

    # 3rd party packages
#    import tensorflow as tf
    from tensorflow import keras

    # ---------------- Setting up the inter and intra threads --------------- #
    # inter = tf.config.threading.get_inter_op_parallelism_threads()
    # intra = tf.config.threading.get_intra_op_parallelism_threads()

#    inter = 1
#    intra = 1

#    tf.config.threading.set_inter_op_parallelism_threads(inter)
#    tf.config.threading.set_intra_op_parallelism_threads(intra)

#    print("inter_op_parallelism_threads = {}\nintra_op_parallelism_threads = "
#          "{}".format(tf.config.threading.get_inter_op_parallelism_threads(),
#                      tf.config.threading.get_intra_op_parallelism_threads()))
    # ----------------------------------------------------------------------- #

    # ----------------------------------------------------------------------- #
    # Loads the my_callbacks library
    # ----------------------------------------------------------------------- #
    import sys
    import pathlib

    # Absolute path to this script
    MY_PAPI_DIR = pathlib.Path(__file__).absolute()
    # Now, we have to move to the root of this workspace ([prev. path]/TFG)
    MY_PAPI_DIR = MY_PAPI_DIR.parent.parent.parent.parent.parent.absolute()
    # From the root (TFG/) access to my_papi dir. and its content
    MY_PAPI_DIR = MY_PAPI_DIR / "my_papi"
    # Folder where the configuration files are located
    CFG_DIR = MY_PAPI_DIR / "conf"
    # Folder where the library is located
    LIB_DIR = MY_PAPI_DIR / "lib"
    # Folder where the source codes are located
    SRC_DIR = MY_PAPI_DIR / "src"

    # Add the source path and import the script
    sys.path.insert(0, str(SRC_DIR))
    from MyPapi import *

    # ----------------------------------------------------------------------- #
    # Params for the measure
    # ----------------------------------------------------------------------- #
    # Path to the library, needed to create an object of class my_papi
    libname = LIB_DIR / "libmy_papi.so"

    # Load a file with the events
    events_file = CFG_DIR / "events_node_mnist_test.cfg"

    # Output file with the measures
    train_output_file = "out/mnist_train_each_batch.csv"
    test_output_file = "out/mnist_test_each_batch.csv"
    predict_output_file = "out/mnist_predict_each_batch.csv"
    # train_output_file = test_output_file = predict_output_file = None
    # ----------------------------------------------------------------------- #

    # ------------------- Load the dataset and define it -------------------- #
    # Fetch and load common datasets, including Fashion MNIST
    # It has the exact same format as MNIST (70.000 grayscale image of 28 x
    # 28 pixels each, with 10 classes).
    fashion_mnist = keras.datasets.fashion_mnist

    # Images represented as a 28 x 28 array
    # Pixel intensities are represented as integers (from 0 to 255)

    # The dataset is already split inot a training set and a test set
    (X_train_full, Y_train_full), (X_test, Y_test) = fashion_mnist.load_data()

    # We'll create a validation set which will contain 5,000 images, and
    # the test set will contain 10,000 images. So, the others 55.000 images
    # will be used as trainning set
    X_valid, X_train = X_train_full[:5000] / 255., X_train_full[5000:] / 255.
    Y_valid, Y_train = Y_train_full[:5000], Y_train_full[5000:]
    X_test = X_test / 255.

    # List of class names
    class_names = ["T-shirt/top", "Trouser", "Pullover", "Dress", "Coat",
                   "Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot"]
    # ----------------------------------------------------------------------- #

    # ------------- Creating the model using the Sequential API ------------- #
    # 1. Creates a Sequential model. Layers connected sequentially
    model = keras.models.Sequential()

    # 2. Adds a flatten layer whose role is to convert each input image
    # into a 1D array.
    model.add(keras.layers.Flatten(input_shape=[28, 28]))

    # 3. Adds a dense hidden layer with 300 neurons using the ReLU
    # activation function. Each one manages its own weight matrix.
    model.add(keras.layers.Dense(300, activation="relu"))

    # 4. Adds a second dense hidden layer with 100 neurons using the ReLU
    # activation function.
    model.add(keras.layers.Dense(100, activation="relu"))

    # 5. Finaly, add a dense output layer with 10 neurons (one per class),
    # using the softmax activation function (beacuse the classes are
    # exclusive).
    model.add(keras.layers.Dense(10, activation="softmax"))

    # Compiling
    model.compile(loss="sparse_categorical_crossentropy",
                  optimizer="sgd",
                  metrics=["accuracy"])
    # ----------------------------------------------------------------------- #

    # Creates a callback from my_papi library
    my_callbacks = MeasureOnEachBatch(lib_path=str(libname),
                                      events_file=str(events_file),
                                      output_file=str(train_output_file))

    # ----------------------------------------------------------------------- #
    # ! TRAIN
    # ----------------------------------------------------------------------- #
    history = model.fit(x=X_train,
                        y=Y_train,
                        batch_size=110,
                        epochs=30,
                        verbose=1,
                        callbacks=my_callbacks,
                        validation_split=0.,
                        validation_data=None,
                        shuffle=True,
                        class_weight=None,
                        sample_weight=None,
                        initial_epoch=0,
                        steps_per_epoch=None,
                        validation_steps=None,
                        validation_batch_size=None,
                        validation_freq=1,
                        max_queue_size=10,
                        workers=1,
                        use_multiprocessing=False)
    # ----------------------------------------------------------------------- #
    # ! END TRAIN
    # ----------------------------------------------------------------------- #

    # # ----------------------------------------------------------------------- #
    # # ! TEST
    # # ----------------------------------------------------------------------- #
    # model.evaluate(x=X_test,
    #                y=Y_test,
    #                batch_size=None,
    #                verbose=1,
    #                sample_weight=None,
    #                steps=None,
    #                callbacks=my_callbacks,
    #                max_queue_size=10,
    #                workers=1,
    #                use_multiprocessing=False,
    #                return_dict=False)
    # # ----------------------------------------------------------------------- #
    # # ! END TEST
    # # ----------------------------------------------------------------------- #

    # # Get the last n items of the test dataset
    # n_items = 15
    # X_new = X_test[:n_items]

    # # ----------------------------------------------------------------------- #
    # # ! PREDICT
    # # ----------------------------------------------------------------------- #
    # y_proba = model.predict(x=X_new,
    #                         batch_size=None,
    #                         verbose=0,
    #                         steps=None,
    #                         callbacks=my_callbacks,
    #                         max_queue_size=10,
    #                         workers=1,
    #                         use_multiprocessing=False)
    # # ----------------------------------------------------------------------- #
    # # ! END PREDICT
    # # ----------------------------------------------------------------------- #

    # # Check if the prediction is correct:
    # # From the list, get the option which has most probab.
    # y_pred = np.argmax(y_proba, axis=-1)
    # # We can get the correspondences between class name and num
    # pred = np.array(class_names)[y_pred]

    # # And check if it its correct
    # y_new = Y_test[:n_items]
    # sol = np.array(class_names)[y_new]

    # print("Clothes predicted: {}\nClothes tested: {}".format(pred, sol))

    my_callbacks.finalize_measure()

    # MyPapi.dash_table_by_cpus_static(str(output_file))


from datetime import datetime, timedelta

def get_current_date_str():
    now = datetime.now()

    ret = now.strftime("%Y%m%d")

    return ret

def get_delta_date_str(delta):
    now = datetime.now()

    delta_days = timedelta(days=1) * delta

    ret = (now + delta_days).strftime("%Y%m%d")

    return ret

def get_current_hour_str():
    now = datetime.now()

    ret = now.strftime("%Y%m%d%H")

    return ret

def get_delta_hour_str(delta):
    now = datetime.now()

    delta_hours = timedelta(hours=1) * delta

    ret = (now + delta_hours).strftime("%Y%m%d%H")

    return ret

# yqc_2020080618.0.log
def make_log_file_names():
    last_hour = get_delta_hour_str(-1)
    log_file_name = 'yqc_' + str(last_hour) + '.*.log'
    return log_file_name

def main():
    current_date_str = get_current_date_str()
    print(f"current_date_str={current_date_str}")

    delta_day_str = get_delta_date_str(-1)
    print(f"delta_day_str={delta_day_str}")

    print("-" * 160)

    current_hour_str = get_current_hour_str()
    print(f"current_hour_str={current_hour_str}")

    delta_hour_str = get_delta_hour_str(-1)
    print(f"delta_hour_str={delta_hour_str}")

    print("-" * 160)
    log_file_name = make_log_file_names()
    print(f"log_file_name={log_file_name}")

if __name__ == '__main__':
    main()


import random

import numpy as np
from tqdm import tqdm

from .operations import Duplicate, Dropout

class PipelineResultsIterator:
    """
    Iterator for img+DM pairs on pipeline's output.
    """
    def __init__(self, images_and_density_maps, total_samples, verbose=True):
        """
        Create an iterator for img+DM pairs coming from the pipeline.

        :param images_and_density_maps: Iterator of preprocessed img+DM pairs.
        :param total_samples: Total expected number of samples on output.
        :param verbose: Whether to display a progress bar.
        """
        self._images_and_density_maps = images_and_density_maps
        self._progress = tqdm(total=total_samples) if verbose and total_samples is not None else None

    def __iter__(self):
        """ Return itself """
        return self

    def __next__(self):
        """ Return next img+DM pair, updating the progress bar if it's enabled """
        next_result = next(self._images_and_density_maps)
        if self._progress is not None:
            self._progress.update(1)
        return next_result

class Pipeline:
    """
    Pipelines define the data preprocessing and augmentation tasks, starting from loading the original data, through
    various transformations, up to showing and saving the results. They provide an easy to define and adjust workflow,
    that can be stored and summarized with ease.

    Unless loading the whole dataset is needed at some point, they provide means of loading just the required minimum,
    optimizing memory usage.

    First, a data loader must be selected. Such a loader needs to return an iterable of pairs of images and
    corresponding density maps. Then, a list of operations is prepared. This includes duplicating, cropping, flipping,
    saving results to files, etc. Finally, such a pipeline is executed to gather the results.
    """
    def __init__(self, loader, operations):
        """
        Create a new pipeline that loads the data using the given `loader` and performs `operations` on them.

        :param loader: Loader that loads pairs of images and corresponding density maps, providing an iterable of such data.
        :param operations: List of operations that will be executed on the loaded data.
        """
        self.loader = loader
        self.operations = operations
        self.requires_full_dataset_in_memory = any([op.requires_full_dataset_in_memory for op in operations])

    def get_input_samples_number(self):
        """ Get number of samples the loader will load """
        return self.loader.get_number_of_loadable_samples()

    def get_expected_output_samples_number(self):
        """ Starting with the input samples number, check for operations modifying the number and calculate the final size """
        output_samples_num = self.get_input_samples_number()
        for operation in self.operations:
            if type(operation) is Duplicate:
                output_samples_num *= operation.duplicates_num
            elif type(operation) is Dropout:
                output_samples_num *= operation.probability
        return output_samples_num

    def _connect_operations(self):
        """
        Connect the loader with all the prepared operations sequentially to create a working pipeline.

        :return: Iterable of img+DM pairs.
        """
        images_and_density_maps = self.loader.load()
        for operation in self.operations:
            images_and_density_maps = operation.execute(images_and_density_maps)
        return images_and_density_maps

    def execute_generate(self, seed=None):
        """
        Execute the pipeline and return an iterable of the preprocessed, augmented data samples. Minimizes peak
        memory usage when there is no bottleneck in the pipeline. If you wish to preprocess everything in one go and
        have a list of the results, please consider using `execute_collect`.

        :param seed: Random seed. When it's not None, it allows reproducibility of the results.
        :return: Iterable of preprocessed data.
        """
        if seed is not None:
            random.seed(seed)
            np.random.seed(seed)

        images_and_density_maps = self._connect_operations()

        return PipelineResultsIterator(images_and_density_maps, self.get_expected_output_samples_number(), False)

    def execute_collect(self, seed=None, return_np_arrays=False, verbose=True):
        """
        Execute the pipeline and return lists of the preprocessed, augmented data samples (one list for images,
        the other for density maps). In opposition to `execute_generate`, this method performs all operations on
        the whole dataset in one go.

        :param seed: Random seed. When it's not None, it allows reproducibility of the results.
        :param return_np_arrays: Whether to use np.arrays (ndarrays) to store images and density maps or Python lists. Generally, np.arrays are more useful when training a model but they don't support elements of varying size (search for 'ragged array'), so for safety, Python lists are the default output.
        :param verbose: If true, display a progress bar.
        :return: List of preprocessed data.
        """
        if seed is not None:
            random.seed(seed)
            np.random.seed(seed)

        images_and_density_maps = self._connect_operations()

        results = PipelineResultsIterator(images_and_density_maps, self.get_expected_output_samples_number(), verbose)
        images, density_maps = zip(*results)
        if return_np_arrays:
            return np.array(images), np.array(density_maps)
        else:
            return images, density_maps

    def summary(self):
        """
        Print a summary of the pipeline.
        """
        width = 80
        print("*" * width)
        print("Pipeline summary")
        print("*" * width)
        print("")
        print("Operations:")
        for i, op in enumerate(self.operations):
            print(f"{str(i)}. {str(op)}")
        print("")
        print(f"Requires full dataset in memory: {self.requires_full_dataset_in_memory}")


from django.contrib.sitemaps import GenericSitemap

from avis.models import Avis, Produit, Structure
from config.sitemaps import StaticViewSitemap
from music.models import Playlist, Musique

avis_dict = {
    'queryset': Avis.objects.all(),
    'date_field': 'date_creation',
}

produit_dict = {
    'queryset': Produit.objects.all(),
    'date_field': 'date_creation',
}

structure_dict = {
    'queryset': Structure.objects.all(),
    'date_field': 'date_creation',
}

palylist_dict = {
    'queryset': Playlist.objects.all(),
    'date_field': 'date_creation',
}

musique_dict = {
    'queryset': Musique.objects.all(),
    'date_field': 'date_creation',
}

sitemaps = {
    'sitemaps': {
        'static': StaticViewSitemap,
        'avis': GenericSitemap(avis_dict, priority=0.6),
        'produits': GenericSitemap(produit_dict, priority=0.4),
        'structures': GenericSitemap(structure_dict, priority=0.5),
        'playlists': GenericSitemap(palylist_dict, priority=0.5),
        'musiques': GenericSitemap(musique_dict, priority=0.4)
    }
}


from __future__ import print_function
import json
from datetime import timedelta
import sys
import os
import base64
import datetime
import hashlib
import hmac
import json
import boto3
import decimal
from botocore.exceptions import ClientError
from boto3.dynamodb.conditions import Key, Attr
import img2pdf
import cv2
import base64
import time
from PIL import Image
import numpy as np
import urllib

def lambda_handler(event, context):
    print(json.dumps(event))

    s3client = boto3.client('s3')
    if len(event) > 0:
        orders = json.loads(event['body'])
    final_printouts_arr = []
    for order in orders:

        epoch_time = int(time.time())
        frontimageSource = order['front']
        frontimageSource = urllib.urlopen(frontimageSource)
        img_array = np.array(
            bytearray(
                frontimageSource.read()),
            dtype=np.uint8)
        front_img = cv2.imdecode(img_array, -1)  # cv2.imread(frontimageSource)
        front_img = cv2.cvtColor(front_img, cv2.COLOR_BGR2RGB)

        backimageSource = order['back']
        backimageSource = urllib.urlopen(backimageSource)
        img_array = np.array(bytearray(backimageSource.read()), dtype=np.uint8)
        back_img = cv2.imdecode(img_array, -1)  # cv2.imread(backimageSource)
        back_img = cv2.cvtColor(back_img, cv2.COLOR_BGR2RGB)

        vertical_image = back_img.copy()
        #vertical_image = cv2.flip(back_img, -1)

        backim = Image.fromarray(vertical_image)
        print(type(backim))

        backim = backim.resize((1500, 2100), Image.ANTIALIAS)
        quality_val = 100

        backim.save(
            '/tmp/' + str(order['order_no']) + "_back" +
            str(epoch_time) +
            ".jpg",
            'JPEG',
            quality=quality_val)

        print(type(vertical_image))

        frontim = Image.fromarray(front_img)
        print(type(frontim))

        frontim = frontim.resize((1500, 2100), Image.ANTIALIAS)
        quality_val = 100

        frontim.save(
            '/tmp/' + str(order['order_no']) + "_front" +
            str(epoch_time) +
            ".jpg",
            'JPEG',
            quality=quality_val)

        basewidth = 1270
        img = Image.open('/tmp/' + str(order['order_no']) +
                         "_front" + str(epoch_time) + ".jpg")
        wpercent = (basewidth / float(img.size[0]))
        hsize = int((float(img.size[1]) * float(wpercent)))
        img = img.resize((basewidth, hsize), Image.ANTIALIAS)
        img.save('/tmp/' + str(order['order_no']) +
                 "_front" + str(epoch_time) + ".jpg")

        baseheight = 1780
        img = Image.open('/tmp/' + str(order['order_no']) +
                         "_front" + str(epoch_time) + ".jpg")
        hpercent = (baseheight / float(img.size[1]))
        wsize = int((float(img.size[0]) * float(hpercent)))
        img = img.resize((wsize, baseheight), Image.ANTIALIAS)
        img.save('/tmp/' + str(order['order_no']) +
                 "_front" + str(epoch_time) + ".jpg")

        basewidth = 1270
        img = Image.open('/tmp/' + str(order['order_no']) +
                         "_back" + str(epoch_time) + ".jpg")
        wpercent = (basewidth / float(img.size[0]))
        hsize = int((float(img.size[1]) * float(wpercent)))
        img = img.resize((basewidth, hsize), Image.ANTIALIAS)
        img.save('/tmp/' + str(order['order_no']) +
                 "_back" + str(epoch_time) + ".jpg")

        baseheight = 1780
        img = Image.open('/tmp/' + str(order['order_no']) +
                         "_back" + str(epoch_time) + ".jpg")
        hpercent = (baseheight / float(img.size[1]))
        wsize = int((float(img.size[0]) * float(hpercent)))
        img = img.resize((wsize, baseheight), Image.ANTIALIAS)
        img.save('/tmp/' + str(order['order_no']) +
                 "_back" + str(epoch_time) + ".jpg")

        front_final = '/tmp/' + str(order['order_no']) + \
            "_front" + str(epoch_time) + ".jpg"
        back_final = '/tmp/' + str(order['order_no']) + "_back" + \
            str(epoch_time) + ".jpg"

        print(
            str(order['order_no']) + "_front" +
            str(epoch_time) +
            ".jpg",
            str(order['order_no']) + "_back" +
            str(epoch_time) +
            ".jpg")
        final_printouts_arr.append('/tmp/' + str(order['order_no']) + "_front" +
                                   str(epoch_time) +
                                   ".jpg")
        final_printouts_arr.append('/tmp/' + str(order['order_no']) + "_back" +
                                   str(epoch_time) +
                                   ".jpg")

        cdynamodb = boto3.resource('dynamodb', region_name='us-west-2')
        ctable = cdynamodb.Table('orders')

        response = ctable.update_item(
            Key={'order_no': order['order_no']},
            UpdateExpression="set order_processed = :statusi",
            ExpressionAttributeValues={
                ':statusi': True

            },
            ReturnValues="UPDATED_NEW"
        )

        print(response)

    if len(final_printouts_arr) > 0:
        epoch_time = int(time.time())
        pdf_bytes = img2pdf.convert(final_printouts_arr)
        s3client.put_object(
            Body=pdf_bytes,
            Bucket='giftomize-order-prints',
            Key=str(epoch_time) + ".pdf")

    out = {}

    if len(final_printouts_arr) > 0:

        out['statusCode'] = 200
        out['body'] = json.dumps({'printpdf': "https://s3-us-west-2.amazonaws.com/giftomize-order-prints/" +
                                  str(epoch_time) + ".pdf"})
        header = {}
        header['Access-Control-Allow-Origin'] = "*"
        header['Access-Control-Allow-Headers'] = "X-Requested-With"
        out['headers'] = header

    else:
        out['statusCode'] = 500
        out['body'] = ""
        header = {}
        header['Access-Control-Allow-Origin'] = "*"
        header['Access-Control-Allow-Headers'] = "X-Requested-With"
        out['headers'] = header

    print(out)
    return out


#!/usr/bin/env python

import os, base64

VERSIONEER_b64 = """
@VERSIONEER-INSTALLER@
"""

v = base64.b64decode(VERSIONEER_b64)
if os.path.exists("versioneer.py"):
    print("overwriting existing versioneer.py")
with open("versioneer.py", "wb") as f:
    f.write(v)
print("versioneer.py (@VERSIONEER-VERSION@) installed into local tree")
print("Now please follow instructions in the docstring.")


import cv2

import sys
argvs = sys.argv
argc = len(argvs)

FPST = 50 # fps/1000

ORG_WINDOW_NAME = "org"
GRAY_WINDOW_NAME = "gray"
CANNY_WINDOW_NAME = "canny"
LAP_WINDOW_NAME = "lap"

#ORG_FILE_NAME = "sample.mxg"
org = cv2.VideoCapture(argvs[1])
cannyw = cv2.VideoWriter('output.avi',-1, 15.0, (1280,960))

end_flag, c_frame = org.read()
height, width, channels = c_frame.shape

cv2.namedWindow(ORG_WINDOW_NAME)
cv2.namedWindow(GRAY_WINDOW_NAME)
cv2.namedWindow(CANNY_WINDOW_NAME)
#cv2.namedWindow(LAP_WINDOW_NAME)

while end_flag == True:
    g_frame = cv2.cvtColor(c_frame, cv2.COLOR_BGR2GRAY)
    canny_frame = cv2.Canny(g_frame, 50, 110)
    #lap_frame = cv2.Laplacian(g_frame, cv2.CV_32F)

    cv2.imshow(ORG_WINDOW_NAME, c_frame)
    cv2.imshow(GRAY_WINDOW_NAME, g_frame)
    cv2.imshow(CANNY_WINDOW_NAME, canny_frame)
    #cv2.imshow(LAP_WINDOW_NAME, lap_frame)

    key = cv2.waitKey(FPST)
    if key == 27:
        break
    if key == ord(' '):
        cv2.imwrite('canny.jpg',canny_frame)

    cannyw.write(canny_frame)
    end_flag, c_frame = org.read()

cv2.destroyAllWindows()
org.release()


# This script should be used a PDAL filter.python
# The idea is to check if a point belongs to a projected polygon
# and, in case it is inside the polygon with the specified coordinates,
# it should be marked in any way (at the moment PointSourceId is set
# to some arbitrary value

# Created by Serge Shmygelskyy aka Shmyg

import numpy
from shapely.geometry import Point
from shapely.geometry.polygon import Polygon

def apply_filter(ins,outs):

    xCoordinate = ins['X'].astype('float64')
    yCoordinate = ins['Y'].astype('float64')

    point = Point([[xCoordinate],[yCoordinate]])
    polygon = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)])

    if polygon.contains(point) :
         point_source_id = ins['PointSourceId'] + 66

    outs['PointSourceId'] = point_source_id

    return True


import django_filters
from django_filters import DateFilter, CharFilter
from .models import OrgProfile, OrgNews, OrgRapport, OrgData, OrgResearch, OrgJob, OrgFundingOpp, OrgCapacityOpp, DevOrgOpp, PersFundingOpp
from django.utils.translation import gettext_lazy as _

class OrgsFilter(django_filters.FilterSet):
    # langue
    # language = CharFilter(field_name="language",label='Langue')
    # auther = CharFilter(field_name="auther", lookup_expr='gte',label='Auteur')
    # category = CharFilter(field_name="category", label='Catégorie')
    # sub_category = CharFilter(label='Sous-Catégorie')

    start_date = CharFilter(field_name="date_of_establishment",
                            lookup_expr='gte', label=_('تاريخ التأسيس / من :'))
    end_date = CharFilter(field_name="date_of_establishment",
                          lookup_expr='lte', label=_('إلى :'))
    name = CharFilter(field_name="name",
                      lookup_expr='icontains', label=_('اسم المنظمة'))
    start_date_pub = DateFilter(field_name="created_at",
                                lookup_expr='gte', label=_('تاريخ نشر الخبر / من :'))
    end_date_pub = DateFilter(field_name="created_at",
                              lookup_expr='lte', label=_('إلى :'))
    # description = CharFilter(field_name="description",
    #                          lookup_expr='icontains', label='Description')

    class Meta:
        model = OrgProfile
        fields = [
            'user',
            'name',
            'position_work',
            'work_domain',
            'start_date',
            'end_date',
            'target_cat',
            'published_at',
        ]

        # exclude = ['date_published', 'date_updated', 'likes', 'is_published']

class OrgsNewsFilter(django_filters.FilterSet):

    title = CharFilter(field_name="title",
                       lookup_expr='icontains', label=_('كلمات من عنوان الخبر'))
    content = CharFilter(field_name="content",
                         lookup_expr='icontains', label=_('كلمات من تفاصيل الخبر'))

    start_date_pub = DateFilter(field_name="created_at",
                                lookup_expr='gte', label=_('تاريخ نشر الخبر / من :'))
    end_date_pub = DateFilter(field_name="created_at",
                              lookup_expr='lte', label=_('إلى :'))

    class Meta:
        model = OrgNews
        fields = [
            'user',
            'org_name',
            'title',
            'content',
            'start_date_pub',
            'end_date_pub',
        ]

class OrgsRapportFilter(django_filters.FilterSet):

    title = CharFilter(field_name="title",
                       lookup_expr='icontains', label=_('كلمات من عنوان التقرير'))

    start_date_pub = DateFilter(field_name="created_at",
                                lookup_expr='gte', label=_('تاريخ نشر التقرير / من :'))
    end_date_pub = DateFilter(field_name="created_at",
                              lookup_expr='lte', label=_('إلى :'))

    class Meta:
        model = OrgRapport
        fields = [
            'user',
            'org_name',
            'title',
            'domain',
            'start_date_pub',
            'end_date_pub',
        ]

class OrgsDataFilter(django_filters.FilterSet):

    title = CharFilter(field_name="title",
                       lookup_expr='icontains', label=_('كلمات من عنوان البيان'))

    start_date_pub = DateFilter(field_name="created_at",
                                lookup_expr='gte', label=_('تاريخ نشر البيان / من :'))
    end_date_pub = DateFilter(field_name="created_at",
                              lookup_expr='lte', label=_('إلى :'))

    class Meta:
        model = OrgData
        fields = [
            'user',
            'org_name',
            'title',
            'start_date_pub',
            'end_date_pub',
        ]

class OrgsMediaFilter(django_filters.FilterSet):

    title = CharFilter(field_name="title",
                       lookup_expr='icontains', label=_('كلمات من عنوان المحتوى'))

    start_date_pub = DateFilter(field_name="created_at",
                                lookup_expr='gte', label=_('تاريخ نشر المحتوى / من :'))
    end_date_pub = DateFilter(field_name="created_at",
                              lookup_expr='lte', label=_('إلى :'))

    class Meta:
        model = OrgData
        fields = [
            'user',
            'org_name',
            'title',
            'start_date_pub',
            'end_date_pub',
        ]

class OrgsResearchFilter(django_filters.FilterSet):
    name_entity = CharFilter(field_name="name_entity",
                             lookup_expr='icontains', label=_('كلمات من اسم الجهة'))
    title = CharFilter(field_name="title",
                       lookup_expr='icontains', label=_('كلمات من عنوان البحث'))

    start_date_pub = DateFilter(field_name="created_at",
                                lookup_expr='gte', label=_('تاريخ نشر البحث / من :'))
    end_date_pub = DateFilter(field_name="created_at",
                              lookup_expr='lte', label=_('إلى :'))

    class Meta:
        model = OrgResearch
        fields = [
            'user',
            'name_entity',
            'title',
            'domaine',
            'start_date_pub',
            'end_date_pub',
        ]
# resources filters
# job filters

class OrgsJobsFilter(django_filters.FilterSet):

    title = CharFilter(field_name="job_title",
                       lookup_expr='icontains', label=_('كلمات من فرصة العمل  '))
    job_type = CharFilter(field_name="job_title",
                          lookup_expr='icontains', label=_('نوع الوظيفة'))
    experience = CharFilter(field_name="experience",
                            lookup_expr='icontains', label=_('الخبرة'))
    # position_work = CharFilter(field_name="job_country",
    #                            lookup_expr='icontains', label=_('مكان العمل'))
    city_work = CharFilter(field_name="job_country",
                           lookup_expr='icontains', label=_('المحافظة'))
    job_domain = CharFilter(field_name="job_domain",
                            lookup_expr='icontains', label=_('مجال العمل'))
    start_date_pub = DateFilter(field_name="created_at",
                                lookup_expr='gte', label=_('تاريخ نشر فرصة العمل / من :'))
    end_date_pub = DateFilter(field_name="created_at",
                              lookup_expr='lte', label=_('إلى :'))

    class Meta:
        model = OrgJob
        fields = [
            'user',
            'org_name',
            'job_title',
            'job_type',
            'experience',
            'position_work',
            'city_work',
            'job_domain',
            'start_date_pub',
            'end_date_pub',
        ]
# org funding filters

class OrgsFundingFilter(django_filters.FilterSet):

    name_funding = CharFilter(field_name="name_funding",
                              lookup_expr='icontains', label=_('الجهة المانحة'))
    # job_country = CharFilter(field_name="position_work",
    #                          lookup_expr='icontains', label=_('مكان العمل'))
    # job_city = CharFilter(field_name="city_work",
    #                       lookup_expr='icontains', label=_('المحافظة'))
    # work_domain = CharFilter(field_name="work_domain",
    #                          lookup_expr='icontains', label=_('مجال العمل'))
    funding_amounte = CharFilter(field_name="funding_amounte",
                                 lookup_expr='icontains', label=_('حسب المبلغ المالي'))
    start_date_pub = DateFilter(field_name="created_at",
                                lookup_expr='gte', label=_('تاريخ نشر المنحة  / من :'))
    end_date_pub = DateFilter(field_name="created_at",
                              lookup_expr='lte', label=_('إلى :'))

    class Meta:
        model = OrgFundingOpp
        fields = [
            'user',
            'name_funding',
            'funding_org_description',
            'work_domain',
            'position_work',
            'city_work',
            'funding_dead_date',
            'funding_period',
            'funding_amounte',
            'funding_description',
            'funding_conditions',
            'publish',
            'created_at',
            'published_at',
            'updated_at'
        ]
# org capacity filters

class OrgsCapacityFilter(django_filters.FilterSet):

    title_capacity = CharFilter(field_name="title_capacity",
                                lookup_expr='icontains', label=_('عنوان الفرصة '))
    start_date_pub = DateFilter(field_name="created_at",
                                lookup_expr='gte', label=_('تاريخ نشر المنحة  / من :'))
    end_date_pub = DateFilter(field_name="created_at",
                              lookup_expr='lte', label=_('إلى :'))

    class Meta:
        model = OrgCapacityOpp
        fields = [
            'user',
            'org_name',
            'name_capacity',
            'title_capacity',
            'capacity_description',
            'capacity_type',
            'position_work',
            'city_work',
            'capacity_domain',
            'capacity_dead_date',
            'capacity_reqs',
            'capacity_guid',
            'capacity_url',
        ]
# org devs filters

class OrgsDevFilter(django_filters.FilterSet):

    title_dev = CharFilter(field_name="title_dev",
                           lookup_expr='icontains', label=_('عنوان المادة'))
    dev_description = CharFilter(field_name="dev_description",
                                 lookup_expr='icontains', label=_('لمحة عن الجهة'))

    start_date_pub = DateFilter(field_name="created_at",
                                lookup_expr='gte', label=_('تاريخ نشر المنحة  / من :'))
    end_date_pub = DateFilter(field_name="created_at",
                              lookup_expr='lte', label=_('إلى :'))

    class Meta:
        model = DevOrgOpp
        fields = [
            'user',
            'org_name',
            'name_dev',
            'title_dev',
            'dev_date',
            'subject',
            'dev_description',
            'start_date_pub',
            'end_date_pub',
        ]

class PersoFundFilter(django_filters.FilterSet):

    start_date_pub = DateFilter(field_name="created_at",
                                lookup_expr='gte', label=_('تاريخ نشر المنحة  / من :'))
    end_date_pub = DateFilter(field_name="created_at",
                              lookup_expr='lte', label=_('إلى :'))

    class Meta:
        model = PersFundingOpp
        fields = [
            'user',
            'category',
            'fund_type',
            'domain',
            'start_date_pub',
            'end_date_pub',
        ]


#
# My Toolkits:
#
# @author: Eric Li
# 1. 日志logging：
#

import logging, time
import os
import yaml
import time
import logging
import logging.config

__all__ = ['print_time', 'load_config', 'modify_config']

# 增加如下代码， 并配置config.yaml文件，使用_logger.info()等。
# import logging.config
# logging.config.dictConfig(load_config('./conf/train_config.yaml')['train_logging'])

def load_config(data_path):
    with open(data_path, 'r', encoding='UTF-8') as f:
        # conf = yaml.load(f, Loader=yaml.FullLoader)
        conf = yaml.load(f)
        return conf

# configure_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../conf/train_config.yaml')
# config_ = load_config(configure_path)

# 写入yaml文件：会去掉yaml的注释？？
def modify_config(conf_file, para_name, para_value):
    conf = load_config(conf_file)
    conf[para_name] = para_value

    with open(conf_file, "w", encoding="utf-8") as f:
        yaml.dump(conf, f)
        # yaml.dump(conf, f, Dumper=yaml.RoundTripDumper)
        return True

def print_time(f):
    """ 装饰器：记录函数运行时间
        from print_time import print_time as pt

        @pt
        def work(...):
            print('work is running')

        word()
        # work is running
        # --> RUN TIME: <work> : 2.8371810913085938e-05

    """
    def fi(*args, **kwargs):
        s = time.time()
        res = f(*args, **kwargs)
        print('--> RUN TIME: <%s> : %6.2f' % (f.__name__, time.time() - s))
        return res

    return fi

# 日志记录函数初始化：记录级别 DEBUG < INFO < WARNING < ERROR < CRITICAL
def init_logging(logfile="run.log", loglevel=logging.DEBUG):
    LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
    DATE_FORMAT = "[%Y-%m-%d %H:%M:%S]"

    logging.basicConfig(filename=logfile, level=loglevel, format=LOG_FORMAT, datefmt=DATE_FORMAT)
    return True

def write_log(msg, log=logging.INFO, show=False, *args, **kwargs):
    # initLogging(logfile=logfile, loglevel=loglevel)
    if log == logging.DEBUG:
        logging.debug(msg, *args, **kwargs)
    elif log == logging.CRITICAL:
        logging.critical(msg, *args, **kwargs)
    elif log == logging.WARNING:
        logging.warning(msg, *args, **kwargs)
    elif log == logging.ERROR:
        logging.error(msg, *args, **kwargs)
    else:
        logging.info(msg, *args, **kwargs)

    if show:
        print(msg, *args, **kwargs)