import datetime
import errno
import os
import socket
from typing import List

def port_in_use(port: int):
    """
    Checks whether a port is available on the local system.
    """
    a_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    location = ("127.0.0.1", port)
    result_of_check = a_socket.connect_ex(location)

    return result_of_check == 0

def check_ports(port_list: List[int]):
    """
    Takes a list of integers and check whether those ports are available.

    Returns a list of ports which are busy.
    """
    return list(filter(port_in_use, port_list))

def exist_or_create(folder):
    if not os.path.exists(os.path.dirname(folder)):
        try:
            os.makedirs(os.path.dirname(folder))
        except OSError as exc:
            if exc.errno != errno.EEXIST:
                raise

def age_from_timestamp(timestamp):
    delta = datetime.timedelta(seconds=datetime.datetime.now(tz=datetime.timezone.utc).timestamp() - timestamp)
    if delta >= datetime.timedelta(days=1):
        result = f"{delta.days}d"
    elif delta >= datetime.timedelta(hours=1):
        result = f"{delta.seconds // 3600}h"
    elif delta >= datetime.timedelta(minutes=10):
        result = f"{delta.seconds // 60}m"

    elif delta >= datetime.timedelta(minutes=1):
        result = f"{delta.seconds // 60}m{delta.seconds % 60}s"
    else:
        result = f"{delta.seconds}s"
    return result


'''
İşlev: Ses dosyasından metin dosyasına dönüştürme.
Girdi: Ses dosyası (.wav .aiff .aif .aifc .flac)
Çıktı: Metin dosyası (.txt)
İşlem adımları;
1. Başla
2. Ses Dosyası seç
3. Seçilen sesi metne dönüştür
4. Metni dosyaya kaydet
5. Oluşturulan dosya içeriğini dosyayı terminalde göster
6. Dur

Kullandığım kaynaklar
https://www.thepythoncode.com/article/using-speech-recognition-to-convert-speech-to-text-python
https://medium.com/bili%C5%9Fim-hareketi/python-ile-ses-tan%C4%B1ma-uygulamas%C4%B1-olu%C5%9Fturma-3d0d972c62a6
'''

import time
import speech_recognition as sr
import codecs
import tkinter as tk
from tkinter import filedialog
from pathlib import Path

# Ses dosyası seç
root = tk.Tk()
root.withdraw()

# dosya yolu oluştur
file_path = filedialog.askopenfilename(filetypes=[("Ses dosyaları",".wav .aiff .aif .aifc .flac")])

# Seçilen sesi metne dönüştür
r = sr.Recognizer()
file = sr.AudioFile(file_path)
with file as source:
    r.adjust_for_ambient_noise(source)
    audio = r.record(source)
    result = r.recognize_google(audio, language='tr-TR')

# Metni dosyaya kaydet
pathName = Path(file_path)
pathName = pathName.name
fileName = str(int(time.time()))+"-"+pathName+".txt"
f = codecs.open(fileName, "a", "utf-8")
f.write(result)
f.close()

# Oluşturulan dosya içeriğini dosyayı terminalde göster
f = open(fileName, "r", encoding='utf-8')
print(f.read())


import json
import os
import re
import time
import traceback

import requests

import public

try:
    from BTPanel import cache
except:
    pass

class crontab:
    field = 'id,name,type,where1,where_hour,where_minute,echo,addtime,status,save,backupTo,sName,sBody,sType,urladdress,save_local,notice,notice_channel,db_type,split_type,split_value'

    def __init__(self):
        cront = public.M('crontab').order("id desc").field(self.field).select()
        if type(cront) == str:
            public.M('crontab').execute("ALTER TABLE 'crontab' ADD 'status' INTEGER DEFAULT 1", ())
            public.M('crontab').execute("ALTER TABLE 'crontab' ADD 'save' INTEGER DEFAULT 3", ())
            public.M('crontab').execute("ALTER TABLE 'crontab' ADD 'backupTo' TEXT DEFAULT off", ())
            public.M('crontab').execute("ALTER TABLE 'crontab' ADD 'sName' TEXT", ())
            public.M('crontab').execute("ALTER TABLE 'crontab' ADD 'sBody' TEXT", ())
            public.M('crontab').execute("ALTER TABLE 'crontab' ADD 'sType' TEXT", ())
            public.M('crontab').execute("ALTER TABLE 'crontab' ADD 'urladdress' TEXT", ())
            public.M('crontab').execute("ALTER TABLE 'crontab' ADD 'save_local' INTEGER DEFAULT 0", ())
            public.M('crontab').execute("ALTER TABLE 'crontab' ADD 'notice' INTEGER DEFAULT 0", ())
            public.M('crontab').execute("ALTER TABLE 'crontab' ADD 'notice_channel' TEXT DEFAULT ''", ())
            public.M('crontab').execute("ALTER TABLE 'crontab' ADD 'db_type' TEXT DEFAULT ''", ())
            public.M('crontab').execute("UPDATE 'crontab' SET 'db_type'='mysql' WHERE sType='database' and db_type=''",
                                        ())
            public.M('crontab').execute("ALTER TABLE 'crontab' ADD 'split_type' TEXT DEFAULT ''", ())
            public.M('crontab').execute("ALTER TABLE 'crontab' ADD 'split_value' INTEGER DEFAULT 0", ())

    # 设置置顶
    def set_task_top(self, get=None):
        """
        设置任务置顶，不传参数查询设置的计划任务列表
        :param get: task_id
        :return:
        """
        cron_task_top_path = '/www/server/panel/data/cron_task_top'
        if os.path.exists(cron_task_top_path):
            task_top = json.loads(public.readFile(cron_task_top_path))
        else:
            task_top = {'list': []}
        if get and hasattr(get, 'task_id'):
            task_top['list'] = [i for i in task_top['list'] if i != get['task_id']]
            task_top['list'].append(get['task_id'])
            public.writeFile(cron_task_top_path, json.dumps(task_top))
            return public.returnMsg(True, '设置置顶成功！')
        return task_top

    # 取消置顶
    def cancel_top(self, get):
        """
        取消任务置顶
        :param get:task_id
        :return:
        """
        cron_task_top_path = '/www/server/panel/data/cron_task_top'
        if os.path.exists(cron_task_top_path):
            task_top = json.loads(public.readFile(cron_task_top_path))
        else:
            return public.returnMsg(True, '取消置顶成功！')
        if hasattr(get, 'task_id'):
            task_top['list'].remove(get['task_id'])
            public.writeFile(cron_task_top_path, json.dumps(task_top))
            return public.returnMsg(True, '取消置顶成功！')
        else:
            return public.returnMsg(False, '请传入取消置顶ID！')

    # 取计划任务列表
    def GetCrontab(self, get):

        self.checkBackup()
        self.__clean_log()
        cront = public.M('crontab').order("id desc").field(self.field).select()
        #
        # # sqllit分组查询
        # if hasattr(get, 'group_by'):
        #     if get.group_by != '':
        #         cront = public.M('crontab').where('type=?', (get.group_by,)).select()
        data = []
        for i in range(len(cront)):
            tmp = {}
            tmp = cront[i]
            if cront[i]['type'] == "day":
                tmp['type'] = public.getMsg('CRONTAB_TODAY')
                tmp['cycle'] = public.getMsg('CRONTAB_TODAY_CYCLE',
                                             (str(cront[i]['where_hour']), str(cront[i]['where_minute'])))
            elif cront[i]['type'] == "day-n":
                tmp['type'] = public.getMsg('CRONTAB_N_TODAY', (str(cront[i]['where1']),))
                tmp['cycle'] = public.getMsg('CRONTAB_N_TODAY_CYCLE', (
                    str(cront[i]['where1']), str(cront[i]['where_hour']), str(cront[i]['where_minute'])))
            elif cront[i]['type'] == "hour":
                tmp['type'] = public.getMsg('CRONTAB_HOUR')
                tmp['cycle'] = public.getMsg('CRONTAB_HOUR_CYCLE', (str(cront[i]['where_minute']),))
            elif cront[i]['type'] == "hour-n":
                tmp['type'] = public.getMsg('CRONTAB_N_HOUR', (str(cront[i]['where1']),))
                tmp['cycle'] = public.getMsg('CRONTAB_N_HOUR_CYCLE',
                                             (str(cront[i]['where1']), str(cront[i]['where_minute'])))
            elif cront[i]['type'] == "minute-n":
                tmp['type'] = public.getMsg('CRONTAB_N_MINUTE', (str(cront[i]['where1']),))
                tmp['cycle'] = public.getMsg('CRONTAB_N_MINUTE_CYCLE', (str(cront[i]['where1']),))
            elif cront[i]['type'] == "week":
                tmp['type'] = public.getMsg('CRONTAB_WEEK')
                if not cront[i]['where1']: cront[i]['where1'] = '0'
                tmp['cycle'] = public.getMsg('CRONTAB_WEEK_CYCLE', (
                    self.toWeek(int(cront[i]['where1'])), str(cront[i]['where_hour']),
                    str(cront[i]['where_minute'])))
            elif cront[i]['type'] == "month":
                tmp['type'] = public.getMsg('CRONTAB_MONTH')
                tmp['cycle'] = public.getMsg('CRONTAB_MONTH_CYCLE', (
                    str(cront[i]['where1']), str(cront[i]['where_hour']), str(cront[i]['where_minute'])))

            log_file = '/www/server/cron/{}.log'.format(tmp['echo'])
            if os.path.exists(log_file):
                tmp['addtime'] = self.get_last_exec_time(log_file)
            data.append(tmp)
        top_list = self.set_task_top()['list']
        if top_list:
            top_list = top_list[::-1]
        top_data = [item for item in data if str(item['id']) in top_list]
        data1 = [item for item in data if str(item['id']) not in top_list]
        top_data.sort(key=lambda x: top_list.index(str(x['id'])))
        data = top_data + data1
        if hasattr(get, 'search'):
            if get.search != '':
                data = [
                    item for item in data if
                    get.search in item['name']
                    or get.search in item['sName']
                    or get.search in item['type']
                    or get.search in item['addtime']
                ]
        # count = len(data)
        # p = int(get['p']) if hasattr(get, 'p') else 1
        # rows = int(get['rows']) if hasattr(get, 'rows') else 10
        # data1 = public.get_page(count, p, rows)
        # data1['data'] = data[int(data1['shift']) * int(data1['row']):(int(data1['shift']) + 1) * int(data1['row'])]
        return data

    def get_backup_list(self, args):
        '''
            @name 获取指定备份任务的备份文件列表
            @author hwliang
            @param args<dict> 参数{
                cron_id<int> 任务ID 必填
                p<int> 页码 默认1
                rows<int> 每页显示条数 默认10
                callback<string> jsonp回调函数  默认为空
            }
            @return <dict>{
                page<str> 分页HTML
                data<list> 数据列表
            }
        '''

        p = args.get('p/d', 1)
        rows = args.get('rows/d', 10)
        tojs = args.get('tojs/s', '')
        callback = args.get('callback/s', '') if tojs else tojs

        cron_id = args.get('cron_id/d')
        count = public.M('backup').where('cron_id=?', (cron_id,)).count()
        data = public.get_page(count, p, rows, callback)
        data['data'] = public.M('backup').where('cron_id=?', (cron_id,)).limit(data['row'], data['shift']).select()
        return data

    def get_last_exec_time(self, log_file):
        '''
            @name 获取上次执行时间
            @author hwliang
            @param log_file<string> 日志文件路径
            @return format_date
        '''
        exec_date = ''
        try:
            log_body = public.GetNumLines(log_file, 20)
            if log_body:
                log_arr = log_body.split('\n')
                date_list = []
                for i in log_arr:
                    if i.find('★') != -1 and i.find('[') != -1 and i.find(']') != -1:
                        date_list.append(i)
                if date_list:
                    exec_date = date_list[-1].split(']')[0].split('[')[1]
        except:
            pass

        finally:
            if not exec_date:
                exec_date = public.format_date(times=int(os.path.getmtime(log_file)))
        return exec_date

    # 清理日志
    def __clean_log(self):
        try:
            log_file = '/www/server/cron'
            if not os.path.exists(log_file): return False
            for f in os.listdir(log_file):
                if f[-4:] != '.log': continue
                filename = log_file + '/' + f
                if os.path.getsize(filename) < 1048576 / 2: continue
                tmp = public.GetNumLines(filename, 100)
                public.writeFile(filename, tmp)
        except:
            pass

    # 转换大写星期
    def toWeek(self, num):
        wheres = {
            0: public.getMsg('CRONTAB_SUNDAY'),
            1: public.getMsg('CRONTAB_MONDAY'),
            2: public.getMsg('CRONTAB_TUESDAY'),
            3: public.getMsg('CRONTAB_WEDNESDAY'),
            4: public.getMsg('CRONTAB_THURSDAY'),
            5: public.getMsg('CRONTAB_FRIDAY'),
            6: public.getMsg('CRONTAB_SATURDAY')
        }
        try:
            return wheres[num]
        except:
            return ''

    # 检查环境
    def checkBackup(self):
        if cache.get('check_backup'): return None

        # 检查备份表是否正确
        if not public.M('sqlite_master').where('type=? AND name=? AND sql LIKE ?',
                                               ('table', 'backup', '%cron_id%')).count():
            public.M('backup').execute("ALTER TABLE 'backup' ADD 'cron_id' INTEGER DEFAULT 0", ())

        # 检查备份脚本是否存在
        filePath = public.GetConfigValue('setup_path') + '/panel/script/backup'
        if not os.path.exists(filePath):
            public.downloadFile(public.GetConfigValue('home') + '/linux/backup.sh', filePath)
        # 检查日志切割脚本是否存在
        filePath = public.GetConfigValue('setup_path') + '/panel/script/logsBackup'
        if not os.path.exists(filePath):
            public.downloadFile(public.GetConfigValue('home') + '/linux/logsBackup.py', filePath)
        # 检查计划任务服务状态
        import system
        sm = system.system()
        if os.path.exists('/etc/init.d/crond'):
            if not public.process_exists('crond'): public.ExecShell('/etc/init.d/crond start')
        elif os.path.exists('/etc/init.d/cron'):
            if not public.process_exists('cron'): public.ExecShell('/etc/init.d/cron start')
        elif os.path.exists('/usr/lib/systemd/system/crond.service'):
            if not public.process_exists('crond'): public.ExecShell('systemctl start crond')
        cache.set('check_backup', True, 3600)

    # 设置计划任务状态
    def set_cron_status(self, get):
        id = get['id']
        cronInfo = public.M('crontab').where('id=?', (id,)).field(self.field).find()
        status_msg = ['停用', '启用']
        status = 1
        if cronInfo['status'] == status:
            status = 0
            self.remove_for_crond(cronInfo['echo'])
        else:
            cronInfo['status'] = 1
            if not self.sync_to_crond(cronInfo):
                return public.returnMsg(False, '写入计划任务失败,请检查磁盘是否可写或是否开启了系统加固!')

        public.M('crontab').where('id=?', (id,)).setField('status', status)
        public.WriteLog('计划任务', '修改计划任务[' + cronInfo['name'] + ']状态为[' + status_msg[status] + ']')
        return public.returnMsg(True, '设置成功')

    # 修改计划任务
    def modify_crond(self, get):
        if hasattr(get, 'sType'):
            if get['sType'] == 'toShell':
                get.sBody = get.sBody.replace('\r\n', '\n')
        if len(get['name']) < 1:
            return public.returnMsg(False, 'CRONTAB_TASKNAME_EMPTY')
        id = get['id']
        cuonConfig, get, name = self.GetCrondCycle(get)
        cronInfo = public.M('crontab').where('id=?', (id,)).field(self.field).find()
        projectlog = self.modify_project_log_split(cronInfo, get)
        if projectlog.modify():
            return public.returnMsg(projectlog.flag, projectlog.msg)
        if not get['where1']: get['where1'] = get['week']
        del (cronInfo['id'])
        del (cronInfo['addtime'])
        cronInfo['name'] = get['name']
        if hasattr(get, 'sType'):
            if cronInfo['sType'] == "sync_time": cronInfo['sName'] = get['sName']
        cronInfo['type'] = get['type']
        cronInfo['where1'] = get['where1']
        cronInfo['where_hour'] = get['hour']
        cronInfo['where_minute'] = get['minute']
        cronInfo['save'] = get['save']
        cronInfo['backupTo'] = get['backupTo']
        cronInfo['sBody'] = get['sBody']
        cronInfo['urladdress'] = get['urladdress']
        columns = 'name,type,where1,where_hour,where_minute,save,backupTo,sName,sBody,urladdress'
        values = (get['name'], get['type'], get['where1'], get['hour'],
                  get['minute'], get['save'], get['backupTo'], cronInfo['sName'], get['sBody']
                  , get['urladdress'])
        if 'save_local' in get:
            columns += ",save_local, notice, notice_channel"
            values = (get['name'], get['type'], get['where1'], get['hour'],
                      get['minute'], get['save'], get['backupTo'], cronInfo['sName'], get['sBody'],
                      get['urladdress'], get['save_local'], get["notice"],
                      get["notice_channel"])
        self.remove_for_crond(cronInfo['echo'])
        if cronInfo['status'] == 0: return public.returnMsg(False, '当前任务处于停止状态,请开启任务后再修改!')
        if not self.sync_to_crond(cronInfo):
            return public.returnMsg(False, '写入计划任务失败,请检查磁盘是否可写或是否开启了系统加固!')
        public.M('crontab').where('id=?', (id,)).save(columns, values)

        public.WriteLog('计划任务', '修改计划任务[' + cronInfo['name'] + ']成功')
        return public.returnMsg(True, '修改成功')

    # 获取指定任务数据
    def get_crond_find(self, get):
        id = int(get.id)
        data = public.M('crontab').where('id=?', (id,)).field(self.field).find()
        return data

    # 同步到crond
    def sync_to_crond(self, cronInfo):
        if not 'status' in cronInfo: return False
        if 'where_hour' in cronInfo:
            cronInfo['hour'] = cronInfo['where_hour']
            cronInfo['minute'] = cronInfo['where_minute']
            cronInfo['week'] = cronInfo['where1']
        cuonConfig, cronInfo, name = self.GetCrondCycle(cronInfo)
        cronPath = public.GetConfigValue('setup_path') + '/cron'
        cronName = self.GetShell(cronInfo)
        if type(cronName) == dict: return cronName
        # cuonConfig += ' ' + cronPath + '/' + cronName + ' >> ' + cronPath + '/' + cronName + '.log 2>&1'

        if cronInfo['type'] == 'minute-n' and int(cronInfo['where1']) < 10:
            flock_name = cronPath + '/' + cronName + '.lock'
            public.writeFile(flock_name, '')
            os.system('chmod 777 {}'.format(flock_name))
            cuonConfig += ' flock -xn ' + cronPath + '/' + cronName + '.lock' + ' -c ' + cronPath + '/' + cronName + ' >> ' + cronPath + '/' + cronName + '.log 2>&1'
        else:
            cuonConfig += ' ' + cronPath + '/' + cronName + ' >> ' + cronPath + '/' + cronName + '.log 2>&1'
        wRes = self.WriteShell(cuonConfig)
        if type(wRes) != bool: return False
        self.CrondReload()
        return True

    # 添加计划任务
    def AddCrontab(self, get):
        if len(get['name']) < 1:
            return public.returnMsg(False, 'CRONTAB_TASKNAME_EMPTY')
        if get == 'toShell':
            get['sBody'] = get['sBody'].replace('\r\n', '\n')
        cuonConfig, get, name = self.GetCrondCycle(get)
        cronPath = public.GetConfigValue('setup_path') + '/cron'
        cronName = self.GetShell(get)
        if type(cronName) == dict: return cronName
        if get['type'] == 'minute-n' and int(get['where1']) < 10:
            flock_name = cronPath + '/' + cronName + '.lock'
            public.writeFile(flock_name, '')
            os.system('chmod 777 {}'.format(flock_name))
            cuonConfig += ' flock -xn ' + cronPath + '/' + cronName + '.lock' + ' -c ' + cronPath + '/' + cronName + ' >> ' + cronPath + '/' + cronName + '.log 2>&1'
        else:
            cuonConfig += ' ' + cronPath + '/' + cronName + ' >> ' + cronPath + '/' + cronName + '.log 2>&1'
        wRes = self.WriteShell(cuonConfig)
        if type(wRes) != bool: return wRes
        self.CrondReload()
        columns = 'name,type,where1,where_hour,where_minute,echo,addtime,\
                  status,save,backupTo,sType,sName,sBody,urladdress,db_type'
        values = (public.xssencode2(get['name']), get['type'], get['where1'], get['hour'],
                  get['minute'], cronName, time.strftime('%Y-%m-%d %X', time.localtime()),
                  1, get['save'], get['backupTo'], get['sType'], get['sName'], get['sBody'],
                  get['urladdress'], get.get("db_type"),)
        if "save_local" in get:
            columns += ",save_local,notice,notice_channel"
            values = (public.xssencode2(get['name']), get['type'], get['where1'], get['hour'],
                      get['minute'], cronName, time.strftime('%Y-%m-%d %X', time.localtime()),
                      1, get['save'], get['backupTo'], get['sType'], get['sName'], get['sBody'],
                      get['urladdress'], get.get("db_type"), get["save_local"], get['notice'], get['notice_channel'])
        addData = public.M('crontab').add(columns, values)
        public.add_security_logs('计划任务', '添加计划任务[' + get['name'] + ']成功' + str(values))
        if type(addData) == str:
            return public.returnMsg(False, addData)
        public.WriteLog('计划任务', '添加计划任务[' + get['name'] + ']成功')
        if addData > 0:
            result = public.returnMsg(True, 'ADD_SUCCESS')
            result['id'] = addData
            return result
        return public.returnMsg(False, 'ADD_ERROR')

    # 构造周期
    def GetCrondCycle(self, params):
        cuonConfig = ""
        name = ""
        if params['type'] == "day":
            cuonConfig = self.GetDay(params)
            name = public.getMsg('CRONTAB_TODAY')
        elif params['type'] == "day-n":
            cuonConfig = self.GetDay_N(params)
            name = public.getMsg('CRONTAB_N_TODAY', (params['where1'],))
        elif params['type'] == "hour":
            cuonConfig = self.GetHour(params)
            name = public.getMsg('CRONTAB_HOUR')
        elif params['type'] == "hour-n":
            cuonConfig = self.GetHour_N(params)
            name = public.getMsg('CRONTAB_HOUR')
        elif params['type'] == "minute-n":
            cuonConfig = self.Minute_N(params)
        elif params['type'] == "week":
            params['where1'] = params['week']
            cuonConfig = self.Week(params)
        elif params['type'] == "month":
            cuonConfig = self.Month(params)
        return cuonConfig, params, name

    # 取任务构造Day
    def GetDay(self, param):
        cuonConfig = "{0} {1} * * * ".format(param['minute'], param['hour'])
        return cuonConfig

    # 取任务构造Day_n
    def GetDay_N(self, param):
        cuonConfig = "{0} {1} */{2} * * ".format(param['minute'], param['hour'], param['where1'])
        return cuonConfig

    # 取任务构造Hour
    def GetHour(self, param):
        cuonConfig = "{0} * * * * ".format(param['minute'])
        return cuonConfig

    # 取任务构造Hour-N
    def GetHour_N(self, param):
        cuonConfig = "{0} */{1} * * * ".format(param['minute'], param['where1'])
        return cuonConfig

    # 取任务构造Minute-N
    def Minute_N(self, param):
        cuonConfig = "*/{0} * * * * ".format(param['where1'])
        return cuonConfig

    # 取任务构造week
    def Week(self, param):
        cuonConfig = "{0} {1} * * {2}".format(param['minute'], param['hour'], param['week'])
        return cuonConfig

    # 取任务构造Month
    def Month(self, param):
        cuonConfig = "{0} {1} {2} * * ".format(param['minute'], param['hour'], param['where1'])
        return cuonConfig

    # 取数据列表
    def GetDataList(self, get):
        try:
            data = {}
            if get['type'] == 'databases':
                data['data'] = public.M(get['type']).where("type=?", "MySQL").field('name,ps').select()
            else:
                data['data'] = public.M(get['type']).field('name,ps').select()
            for i in data['data']:
                if 'ps' in i:
                    try:
                        if i['ps'] is None: continue
                        i['ps'] = public.xsssec(i['ps'])  # 防止数据库为空时，xss防御报错  2020-11-25
                    except:
                        pass
            data['orderOpt'] = []
            import json
            tmp = public.readFile('data/libList.conf')
            if not tmp: return data
            libs = json.loads(tmp)
            for lib in libs:
                if not 'opt' in lib: continue
                filename = 'plugin/{}'.format(lib['opt'])
                if not os.path.exists(filename): continue
                tmp = {}
                tmp['name'] = lib['name']
                tmp['value'] = lib['opt']
                data['orderOpt'].append(tmp)
            return data
        except:
            return public.returnMsg(False,traceback.format_exc())

    # 取任务日志
    def GetLogs(self, get):
        try:
            id = get['id']
            echo = public.M('crontab').where("id=?", (id,)).field('echo').find()
            logFile = public.GetConfigValue('setup_path') + '/cron/' + echo['echo'] + '.log'
            if not os.path.exists(logFile): return public.returnMsg(False, 'CRONTAB_TASKLOG_EMPTY')
            log = public.GetNumLines(logFile, 2000)
            return public.returnMsg(True, public.xsssec(log))
        except:
            pass

    # 清理任务日志
    def DelLogs(self, get):
        try:
            id = get['id']
            echo = public.M('crontab').where("id=?", (id,)).getField('echo')
            logFile = public.GetConfigValue('setup_path') + '/cron/' + echo + '.log'
            os.remove(logFile)
            return public.returnMsg(True, 'CRONTAB_TASKLOG_CLOSE')
        except:
            return public.returnMsg(False, 'CRONTAB_TASKLOG_CLOSE_ERR')

    # 删除计划任务
    def DelCrontab(self, get):
        try:
            id = get['id']
            find = public.M('crontab').where("id=?", (id,)).field('name,echo').find()
            if not find: return public.returnMsg(False, '指定任务不存在!')
            if not self.remove_for_crond(find['echo']): return public.returnMsg(False,
                                                                                '无法写入文件，请检查是否开启了系统加固功能!')
            cronPath = public.GetConfigValue('setup_path') + '/cron'
            sfile = cronPath + '/' + find['echo']
            if os.path.exists(sfile): os.remove(sfile)
            sfile = cronPath + '/' + find['echo'] + '.log'
            if os.path.exists(sfile): os.remove(sfile)

            public.M('crontab').where("id=?", (id,)).delete()
            public.add_security_logs("删除计划任务", "删除计划任务:" + find['name'])
            public.WriteLog('TYPE_CRON', 'CRONTAB_DEL', (find['name'],))
            return public.returnMsg(True, 'DEL_SUCCESS')
        except:
            return public.returnMsg(False, 'DEL_ERROR')

    # 从crond删除
    def remove_for_crond(self, echo):
        file = self.get_cron_file()
        if not os.path.exists(file):
            return False
        conf = public.readFile(file)
        if not conf: return False
        if conf.find(str(echo)) == -1: return True
        rep = ".+" + str(echo) + ".+\n"
        conf = re.sub(rep, "", conf)
        try:
            if not public.writeFile(file, conf): return False
        except:
            return False
        self.CrondReload()
        return True

    # 取执行脚本
    def GetShell(self, param):
        # try:
        type = param['sType']
        if not 'echo' in param:
            cronName = public.md5(public.md5(str(time.time()) + '_bt'))
        else:
            cronName = param['echo']
        if type == 'toFile':
            shell = param.sFile
        else:
            head = "#!/bin/bash\nPATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin\nexport PATH\n"
            log = '-access_log'
            python_bin = "{} -u".format(public.get_python_bin())
            if public.get_webserver() == 'nginx':
                log = '.log'
            if type in ['site', 'path'] and param['sBody'] != 'undefined' and len(param['sBody']) > 1:
                exclude_files = ','.join([n.strip() for n in param['sBody'].split('\n') if n.strip()])
                head += f'export BT_EXCLUDE="{exclude_files}"\n'
            attach_param = " " + cronName
            wheres = {
                'path': head + python_bin + " " + public.GetConfigValue(
                    'setup_path') + "/panel/script/backup.py path " + param['sName'] + " " + str(
                    param['save']) + attach_param,
                'site': head + python_bin + " " + public.GetConfigValue(
                    'setup_path') + "/panel/script/backup.py site " + param['sName'] + " " + str(
                    param['save']) + attach_param,
                'database': head + python_bin + " " + public.GetConfigValue(
                    'setup_path') + "/panel/script/backup.py database " + param['sName'] + " " + str(
                    param['save']) + attach_param,
                'logs': head + python_bin + " " + public.GetConfigValue('setup_path') + "/panel/script/logsBackup " +
                        param['sName'] + " " + str(param['save']),
                'rememory': head + "/bin/bash " + public.GetConfigValue('setup_path') + '/panel/script/rememory.sh',
                'sync_time': head + python_bin + " " + public.GetConfigValue(
                    'setup_path') + "/panel/script/sync_time.py {}".format(param['sName']),
                'webshell': head + python_bin + " " + public.GetConfigValue(
                    'setup_path') + '/panel/class/webshell_check.py site ' + param['sName'] + ' ' + param['urladdress']
            }
            # 取消插件调用计划任务
            # if param['backupTo'] != 'localhost':
            #     cfile = public.GetConfigValue('setup_path') + "/panel/plugin/" + param['backupTo'] + "/" + param[
            #         'backupTo'] + "_main.py"
            #     if not os.path.exists(cfile): cfile = public.GetConfigValue('setup_path') + "/panel/script/backup_" + \
            #                                           param['backupTo'] + ".py"
            #     wheres = {
            #         'path': head + python_bin + " " + cfile + " path " + param['sName'] + " " + str(
            #             param['save']) + attach_param,
            #         'site': head + python_bin + " " + cfile + " site " + param['sName'] + " " + str(
            #             param['save']) + attach_param,
            #         'database': head + python_bin + " " + cfile + " database " + param['sName'] + " " + str(
            #             param['save']) + attach_param,
            #         'logs': head + python_bin + " " + public.GetConfigValue(
            #             'setup_path') + "/panel/script/logsBackup " + param['sName'] + " " + str(param['save']),
            #         'rememory': head + "/bin/bash " + public.GetConfigValue('setup_path') + '/panel/script/rememory.sh',
            #         'webshell': head + python_bin + " " + public.GetConfigValue(
            #             'setup_path') + '/panel/class/webshell_check.py site ' + param['sName'] + ' ' + param[
            #                         'urladdress']
            #     }

            try:
                shell = wheres[type]
            except:
                if type == 'toUrl':
                    shell = head + "curl -sS --connect-timeout 10 -m 3600 '" + param['urladdress'] + "'"
                else:
                    shell = head + param['sBody'].replace("\r\n", "\n")

            shell += '''
echo "----------------------------------------------------------------------------"
endDate=`date +"%Y-%m-%d %H:%M:%S"`
echo "★[$endDate] Successful"
echo "----------------------------------------------------------------------------"
'''
        cronPath = public.GetConfigValue('setup_path') + '/cron'
        if not os.path.exists(cronPath): public.ExecShell('mkdir -p ' + cronPath)
        file = cronPath + '/' + cronName
        public.writeFile(file, self.CheckScript(shell))
        public.ExecShell('chmod 750 ' + file)
        return cronName
        # except Exception as ex:
        # return public.returnMsg(False, 'FILE_WRITE_ERR' + str(ex))

    # 检查脚本
    def CheckScript(self, shell):
        keys = ['shutdown', 'init 0', 'mkfs', 'passwd', 'chpasswd', '--stdin', 'mkfs.ext', 'mke2fs']
        for key in keys:
            shell = shell.replace(key, '[***]')
        return shell

    # 重载配置
    def CrondReload(self):
        if os.path.exists('/etc/init.d/crond'):
            public.ExecShell('/etc/init.d/crond reload')
        elif os.path.exists('/etc/init.d/cron'):
            public.ExecShell('service cron restart')
        else:
            public.ExecShell("systemctl reload crond")

    # 将Shell脚本写到文件
    def WriteShell(self, config):
        u_file = '/var/spool/cron/crontabs/root'
        file = self.get_cron_file()
        if not os.path.exists(file): public.writeFile(file, '')
        conf = public.readFile(file)
        if type(conf) == bool: return public.returnMsg(False, '读取文件失败!')
        conf += config + "\n"
        if public.writeFile(file, conf):
            if not os.path.exists(u_file):
                public.ExecShell("chmod 600 '" + file + "' && chown root.root " + file)
            else:
                public.ExecShell("chmod 600 '" + file + "' && chown root.crontab " + file)
            return True
        return public.returnMsg(False, '文件写入失败,请检查是否开启系统加固功能!')

    # 立即执行任务
    def StartTask(self, get):
        echo = public.M('crontab').where('id=?', (get.id,)).getField('echo')
        execstr = public.GetConfigValue('setup_path') + '/cron/' + echo
        public.ExecShell('chmod +x ' + execstr)
        public.ExecShell('nohup ' + execstr + ' >> ' + execstr + '.log 2>&1 &')
        return public.returnMsg(True, 'CRONTAB_TASK_EXEC')

    # 获取计划任务文件位置
    def get_cron_file(self):
        u_path = '/var/spool/cron/crontabs'
        u_file = u_path + '/root'
        c_file = '/var/spool/cron/root'
        cron_path = c_file
        if not os.path.exists(u_path):
            cron_path = c_file

        if os.path.exists("/usr/bin/apt-get"):
            cron_path = u_file
        elif os.path.exists('/usr/bin/yum'):
            cron_path = c_file

        if cron_path == u_file:
            if not os.path.exists(u_path):
                os.makedirs(u_path, 472)
                public.ExecShell("chown root:crontab {}".format(u_path))
        if not os.path.exists(cron_path):
            public.writeFile(cron_path, "")
        return cron_path

    def modify_project_log_split(self, cronInfo, get):

        def _test_project_type(self, project_type):
            if project_type == "Node项目":
                return "nodojsModel"
            elif project_type == "Java项目":
                return "javaModel"
            elif project_type == "GO项目":
                return "goModel"
            elif project_type == "其他项目":
                return "otherModel"
            elif project_type == "Python项目":
                return "pythonModel"
            else:
                return None

        def the_init(self, cronInfo, get: dict):
            self.get = get
            self.cronInfo = cronInfo
            self.msg = ""
            self.flag = False
            name = get["name"]
            if name.find("运行日志切割") != -1:
                try:
                    project_type, project_name = name.split("]", 2)[1].split("[", 1)
                    project_type = self._test_project_type(project_type)
                except:
                    self.project_type = None
                    return
            else:
                self.project_type = None
                return

            self.project_type = project_type
            self.project_name = project_name
            conf_path = '{}/data/run_log_split.conf'.format(public.get_panel_path())
            data = json.loads(public.readFile(conf_path))
            self.log_size = int(data[self.project_name]["log_size"]) / 1024 / 1024

        def modify(self):
            from importlib import import_module
            if not self.project_type:
                return False
            if self.cronInfo["type"] != self.get['type']:
                self.msg = "运行日志切割不能修改执行周期的方式"
                return True
            get = public.dict_obj()
            get.name = self.project_name
            get.log_size = self.log_size
            if get.log_size != 0:
                get.hour = "2"
                get.minute = str(self.get['where1'])
            else:
                get.hour = str(self.get['hour'])
                get.minute = str(self.get['minute'])
            get.num = str(self.get["save"])

            model = import_module(".{}".format(self.project_type), package="projectModel")

            res = getattr(model.main(), "mamger_log_split")(get)
            self.msg = res["msg"]
            self.flag = res["status"]

            return True

        attr = {
            "__init__": the_init,
            "_test_project_type": _test_project_type,
            "modify": modify,
        }
        return type("ProjectLog", (object,), attr)(cronInfo, get)

    # 检查指定的url是否通
    def check_url_connecte(self, get):
        end_time = 0
        if 'url' not in get or get['url'] == '':
            return public.returnMsg(False, '请传入url!')
        try:
            start_time = time.time()
            response = requests.get(get.url, timeout=3)
            response.encoding = 'utf-8'
            end_time = time.time()
            if response.status_code == 200:
                return {'status': True, 'status_code': response.status_code,
                        'txt': public.xsssec(response.text),
                        'time': str(int(round(end_time - start_time, 2) * 1000)) + 'ms'}
            else:
                return {'status': False, 'status_code': response.status_code, 'txt': public.xsssec(response.text),
                        'time': str(int(round(end_time - start_time, 2) * 1000)) + 'ms'}
        except requests.exceptions.RequestException:
            return {'status': False, 'status_code': '', 'txt': '访问异常',
                    'time': '0ms'}

    # 获取各个类型数据库
    def GetDatabases(self, get):
        db_type = getattr(get, "db_type", "mysql")

        crontab_databases = public.M("crontab").field("id,sName").where("LOWER(type)=LOWER(?)", (db_type)).select()
        for db in crontab_databases:
            db["sName"] = set(db["sName"].split(","))

        if db_type == "redis":
            database_list = []
            cron_id = None
            for db in crontab_databases:
                if db_type in db["sName"]:
                    cron_id = db["id"]
                    break
            database_list.append({"name": "本地数据库", "ps": "", "cron_id": cron_id})
            return database_list

        databases = public.M("databases").field("name,ps").where("LOWER(type)=LOWER(?)", (db_type)).select()

        for database in databases:
            if database.get("name") is None: continue

            database["cron_id"] = []
            for db in crontab_databases:
                if database["name"] in db["sName"]:
                    database["cron_id"].append(db["id"])
        return databases


import math
import random
from absl import logging
import tensorflow as tf

from ...utils.data_queue import DataQueue

def data_loader(dataset_builder, batch_size=16, num_threads=1):
    """ dataloader
    """
    num_samples = len(dataset_builder)
    if num_samples == 0:
        raise ValueError("num samples is empty")

    if num_threads == 1:
        def _gen_data():
            """ multi thread loader """
            for i in range(num_samples):
                yield dataset_builder[i]
    else:
        # multi-thread
        logging.info("loading data using %d threads" % num_threads)
        data_queue = DataQueue(
            lambda i: dataset_builder[i],
            capacity=4096,
            num_threads=num_threads,
            max_index=num_samples
        )
        def _gen_data():
            """ multi thread loader """
            for _ in range(num_samples):
                yield data_queue.get()

    # make dataset using from_generator
    dataset = tf.compat.v2.data.Dataset.from_generator(
        _gen_data,
        output_types=dataset_builder.sample_type,
        output_shapes=dataset_builder.sample_shape,
    )

    # Padding the features to its max length dimensions.
    dataset = dataset.padded_batch(
        batch_size=batch_size,
        padded_shapes=dataset_builder.sample_shape,
        drop_remainder=True,
    )

    # Prefetch to improve speed of input pipeline.
    dataset = dataset.prefetch(buffer_size=500)
    return dataset

class BaseDatasetBuilder:
    """ base dataset """

    def __init__(self):
        self.entries = []
        self.speakers = []

    def __getitem__(self, index):
        raise NotImplementedError

    def __len__(self):
        return len(self.entries)

    @property
    def entries_list(self):
        """ return the entries list """
        return self.entries

    @property
    def sample_type(self):
        """ example types """
        raise NotImplementedError

    @property
    def sample_shape(self):
        """ examples shapes """
        raise NotImplementedError

    @property
    def sample_signature(self):
        """ examples signature """
        raise NotImplementedError

    def as_dataset(self, batch_size=16, num_threads=1):
        """ return tf.data.Dataset object """
        return data_loader(self, batch_size, num_threads)

    def shard(self, num_shards, index):
        """ Creates a Dataset that includes only 1/num_shards of this dataset """
        if index >= num_shards:
            raise ValueError("the index should smaller the num_shards")
        logging.info("Creates the sub-dataset which is the %d part of %d" % (index, num_shards))
        original_entries = self.entries
        self.entries = []
        total_samples = (len(original_entries) // num_shards) * num_shards
        for i in range(total_samples):
            if i % num_shards == index:
                self.entries.append(original_entries[i])
        return self

    def batch_wise_shuffle(self, batch_size=64):
        """Batch-wise shuffling of the data entries.

        Each data entry is in the format of (audio_file, file_size, transcript).
        If epoch_index is 0 and sortagrad is true, we don't perform shuffling and
        return entries in sorted file_size order. Otherwise, do batch_wise shuffling.

        Args:
            batch_size: an integer for the batch size. default=64
        """
        if len(self.entries) == 0:
            return self
        logging.info("perform batch_wise_shuffle with batch_size %d" % batch_size)
        max_buckets = int(math.floor(len(self.entries) / batch_size))
        total_buckets = [i for i in range(max_buckets)]
        random.shuffle(total_buckets)
        shuffled_entries = []
        for i in total_buckets:
            shuffled_entries.extend(self.entries[i * batch_size : (i + 1) * batch_size])
        shuffled_entries.extend(self.entries[max_buckets * batch_size :])
        self.entries = shuffled_entries
        return self

    # pylint: disable=unused-argument
    def compute_cmvn_if_necessary(self, is_necessary=True):
        """ vitural interface """
        return self


import re

from tkinter import *
from tkinter import messagebox as mb
from config.config import reg_rus, reg_eng
from tools.tool import get_gamma

from tools.cipher import Gamma

class MainForm(Frame):
    # Main form init
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        self.parent.resizable(width=False, height=False)
        self.canvas = Canvas(self.parent)

        self.frame = Frame(self.parent, bg='#fafafa')

        self.form_w = 500
        self.form_h = 400
        self.isRus = True

        self.centerWindow()
        self.initUI()
        self.showUI()

    # Init Widgets
    def initUI(self):
        # Key frame
        self.key_frame = Frame(self.parent, bg='#fafafa')
        self.key_label = Label(self.frame, text='Ввод ключа:', bg='#fafafa', font=20)
        self.scroll_key = Scrollbar(self.key_frame, orient='horizontal')

        # Only upper for Entry
        def to_uppercase_key(*args):
            varKey.set(varKey.get().upper())

        varKey = StringVar(self.parent)
        try:
            varKey.trace_add('write', to_uppercase_key)
        except AttributeError:
            varKey.trace('w', to_uppercase_key)
        self.key_input = Entry(self.key_frame, justify='center', textvariable=varKey,
                               xscrollcommand=self.scroll_key.set)

        # Entering text frame
        self.text_frame = Frame(self.parent, bg='#fafafa')
        self.text_label = Label(self.frame, text='Ввод текста:', bg='#fafafa', font=20)
        self.scroll_txt = Scrollbar(self.text_frame)
        self.text_input = Text(self.text_frame, name="txt_tag", yscrollcommand=self.scroll_txt.set)

        # Output text frame
        self.out_frame = Frame(self.parent, bg='#fafafa')
        self.out_label = Label(self.frame, text='Результат:', bg='#fafafa', font=20)
        self.scroll_out = Scrollbar(self.out_frame)
        self.out_input = Text(self.out_frame, yscrollcommand=self.scroll_out.set)

        # Button call the cipher
        self.btn_enc = Button(self.frame, width=14, text='За/Расшифровать', bg='gray', command=self.btn_encode)
        # Button call the change language
        self.btn_key = Button(self.frame, width=14, text='Случ. ключ', bg='gray', command=self.btn_randkey)
        # Button call the change language
        self.btn_cl = Button(self.frame, width=14, text='Сменить язык', bg='gray', command=self.btn_changlang)
        # Button call the convert chars code to bin
        self.btn_bc = Button(self.frame, width=14, text='Двоичный ключ', bg='gray', command=self.btn_symcode)
        # Button call the convert bin code to chars
        self.btn_sc = Button(self.frame, width=14, text='Символьный ключ', bg='gray', command=self.btn_bincode)
        # Button clear
        self.btn_c = Button(self.frame, width=14, text='Очистка', bg='gray', command=self.btn_clear)

    # Show Widgets
    def showUI(self):
        self.canvas.pack()
        self.parent.title('Шифр Гаммирование')
        self.frame.place(relx=0, rely=0, relwidth=1, relheight=1)

        # Init key block frame
        self.key_frame.place(relx=0.25, rely=0.04, relwidth=0.48, relheight=0.09)
        self.key_label.place(x=20, y=12)
        self.scroll_key.pack(side=BOTTOM, fill=X)
        self.key_input.place(x=0, y=0, width=250)

        # Init Entries block frame
        self.text_frame.place(relx=0.05, rely=0.33, relwidth=0.9, relheight=0.25)
        self.text_label.place(x=20, y=105)
        self.scroll_txt.pack(side=RIGHT, fill=Y)
        self.text_input.place(x=0, y=0, height=100, width=435)

        # Init output frame
        self.out_frame.place(relx=0.05, rely=0.70, relwidth=0.9, relheight=0.25)
        self.out_label.place(x=20, y=250)
        self.scroll_out.pack(side=RIGHT, fill=Y)
        self.out_input.place(x=0, y=0, height=100, width=435)

        # Init buttons
        self.btn_enc.place(x=380, y=20)
        self.btn_key.place(x=380, y=55)
        self.btn_cl.place(x=380, y=90)
        self.btn_bc.place(x=260, y=55)
        self.btn_sc.place(x=260, y=90)
        self.btn_c.place(x=140, y=90)

    # Centring Form window
    def centerWindow(self):
        sw = self.parent.winfo_screenwidth()
        sh = self.parent.winfo_screenheight()

        x = (sw - self.form_w) / 2
        y = (sh - self.form_h) / 2
        self.parent.geometry('%dx%d+%d+%d' % (self.form_w, self.form_h, x, y))

    # Click encode/decode
    def btn_encode(self):
        key = self.key_input.get()
        txt = self.text_input.get("1.0", "end-1c").upper()
        self.text_input.delete(1.0, END)
        self.text_input.insert(1.0, txt)

        # Check empty widgets
        if key == '' or txt == '':
            if self.isRus:
                mb.showerror("Ошибка", "Пустая строка!")
            else:
                mb.showerror("Error", "Empty string!")
            return

        # Check outside the alphabet
        if self.isRus:
            reg_val_key = re.sub(reg_rus, '', key)
            reg_val_txt = re.sub(reg_rus, '', txt)
        else:
            reg_val_key = re.sub(reg_eng, '', key)
            reg_val_txt = re.sub(reg_eng, '', txt)

        # There are extra characters
        if len(reg_val_key) > 0 or len(reg_val_txt) > 0:
            if self.isRus:
                mb.showerror("Ошибка",
                             repr(f"Используются посторонние символы: {''.join(set(reg_val_key + reg_val_txt))}"))
            else:
                mb.showerror("Error", repr(f"Foreign characters are used: {''.join(set(reg_val_key + reg_val_txt))}"))
            return

        # Check end line
        txt = txt.replace("\n", "")
        cipher = Gamma(key=key, isRus=self.isRus)

        self.out_input.delete(1.0, END)
        self.out_input.insert(1.0, cipher.encode(txt))

    # Click for random gamma
    def btn_randkey(self):
        txt_lenght = len(self.text_input.get("1.0", "end-1c"))
        if txt_lenght != 0:
            cipher = Gamma(isRus=self.isRus)
            self.key_input.delete(0, END)
            self.key_input.insert(0, cipher.bin2char(get_gamma(txt_lenght * 6)))
        else:
            if self.isRus:
                mb.showerror("Ошибка", "Введите текст!")
            else:
                mb.showerror("Error", "Entry text!")

    # Click for change language
    def btn_changlang(self):
        self.isRus = not self.isRus
        if self.isRus:
            self.parent.title('Шифр Гаммирования')
            self.key_label.config(text='Ввод ключа:')
            self.text_label.config(text='Ввод текста:')
            self.out_label.config(text='Результат:')
            self.btn_enc.config(text='За/Расшифровать')
            self.btn_key.config(text='Случ. ключ')
            self.btn_cl.config(text='Сменить язык')
            self.btn_bc.config(text='Двоичный ключ')
            self.btn_sc.config(text='Символьный ключ')
            self.btn_c.config(text='Очистка')
        else:
            self.parent.title('Cipher Gamming')
            self.key_label.config(text='Entry Key:')
            self.text_label.config(text='Entry Text:')
            self.out_label.config(text='Result:')
            self.btn_enc.config(text='Encode/Decode')
            self.btn_key.config(text='Random Key')
            self.btn_cl.config(text='Change Language')
            self.btn_bc.config(text='Binary key')
            self.btn_sc.config(text='Symbol key')
            self.btn_c.config(text='Clear')

    # Clear Text Widgets
    def btn_clear(self):
        self.key_input.delete(0, END)
        self.text_input.delete("1.0", END)
        self.out_input.delete("1.0", END)

    # Convert binary code to symbols
    def btn_bincode(self):
        key = self.key_input.get()
        txt = self.text_input.get("1.0", "end-1c")
        out = self.out_input.get("1.0", "end-1c")
        if key == '' or txt == '' or out == '':
            if self.isRus:
                mb.showerror("Ошибка", "Пустая строка!")
            else:
                mb.showerror("Error", "Empty string!")
            return

        if len(key) % 6 != 0:
            if self.isRus:
                mb.showerror("Ошибка", "Не правильная длина кода!")
            else:
                mb.showerror("Error", "Incorrect length code!")
            return
        unq_sym = ''.join(set(key))
        if unq_sym != '01' and unq_sym != '10':
            if self.isRus:
                mb.showerror("Ошибка", "Некорректный код!")
            else:
                mb.showerror("Error", "Incorrect code!")
            return

        cipher = Gamma(isRus=self.isRus)
        self.key_input.delete(0, END)
        self.key_input.insert(0, cipher.bin2char(key))

        self.text_input.delete("1.0", END)
        self.text_input.insert("1.0", cipher.bin2char(txt))

        self.out_input.delete("1.0", END)
        self.out_input.insert("1.0", cipher.bin2char(out))

    # Convert symbols code to binary
    def btn_symcode(self):
        key = self.key_input.get()
        txt = self.text_input.get("1.0", "end-1c")
        out = self.out_input.get("1.0", "end-1c")
        if key == '' or txt == '' or out == '':
            if self.isRus:
                mb.showerror("Ошибка", "Пустая строка!")
            else:
                mb.showerror("Error", "Empty string!")
            return

        cipher = Gamma(isRus=self.isRus)
        self.key_input.delete(0, END)
        self.key_input.insert(0, cipher.char2bin(key))

        self.text_input.delete("1.0", END)
        self.text_input.insert("1.0", cipher.char2bin(txt))

        self.out_input.delete("1.0", END)
        self.out_input.insert("1.0", cipher.char2bin(out))


import os
import sys
import scipy.sparse as ssp
from scipy.spatial.distance import cosine
from sklearn.metrics import classification_report

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../")

from pickle import load, dump
from feature_extract.tf_kdl_weight import TFKLD
from feature_extract.dimention_reduction import DimReduction
from feature_extract.preprocess import my_tokenizer

if __name__ == "__main__":

    tfkdl_params_path = "../data/tfkdl_params.pickle"
    tfkdl_params = load(open(tfkdl_params_path, "rb"))

    countvector_model = tfkdl_params["countvector_model"]
    tfkdl_weight = tfkdl_params["weight"]

    tfkdl_object = TFKLD(None)

    dataset_path = "../data/data_tfkdl.txt"
    datasetT, datasetL = tfkdl_object.loadtext(dataset_path)
    datasetT = countvector_model.transform(datasetT)
    datasetT = ssp.lil_matrix(datasetT).todense()

    datasetT_weight = tfkdl_object.weighting_internal(datasetT, tfkdl_weight)

    #dr = DimReduction(datasetT_weight, 200)
    #W, H = dr.svd()
    W = datasetT_weight

    nrow, ndim = W.shape

    pred_val = []
    cosine_sim_list = []

    for index in range(0, nrow, 2):
        sent_1 = W[index, :]
        sent_2 = W[index + 1, :]

        label = datasetL[index // 2]

        cosine_val = 1 - cosine(sent_1, sent_2)

        cosine_sim_list.append(cosine_val)

        if cosine_val >= 0.5:
            pred_val.append(1)
        else:
            pred_val.append(0)

    dump(cosine_sim_list, open("../data/tfkdl_pred_withoutdr", "wb"))

    print(classification_report(datasetL, pred_val))


from datetime import datetime, timedelta
import requests
from colorama import Fore

class PolygonAPI:
    def __init__(self, url, key):
        # Inicialización de variables de instancia
        self.end_date = None
        self.start_date = None
        self.ticker = None

        # Declaración de variables privadas
        self.__url = url
        self.__api_key = key

    def get(self):
        self.ticker = self.get_ticker()

        while True:
            self.start_date = self.get_date("Ingrese la fecha de inicio (YYYY-MM-DD):\n")
            self.end_date = self.get_date("Ingrese la fecha de fin (YYYY-MM-DD):\n")
            if self.start_date <= self.end_date:
                break
            else:
                print(f'{Fore.YELLOW}ERROR: La fecha de inicio no puede ser mayor a la fecha de fin{Fore.RESET}')

        header = {
            "Authorization": "Bearer " + self.__api_key
        }

        print("Obteniendo datos...")

        request_url = self.__url + "/aggs/ticker/" + self.ticker + "/range/1/day/" + self.start_date + "/" + \
                             self.end_date + "?adjusted=true&sort=asc&limit=15000"
        try:
            response = requests.get(request_url, headers=header)
            response.raise_for_status()
        except requests.exceptions.Timeout:
            print(f'{Fore.YELLOW}Error de Timeout{Fore.RESET}')
            return 0
        except requests.exceptions.TooManyRedirects as err:
            print(f"{Fore.YELLOW}ERROR:{Fore.RESET}", err)
            return 0
        except requests.exceptions.HTTPError as err:
            print(f"{Fore.YELLOW}ERROR:{Fore.RESET}", err)
            return 0
        except requests.exceptions.RequestException:
            print(f"{Fore.YELLOW}ERROR: El servidor no responde. Verifique la conexión{Fore.RESET}")
            return 0
        except:
            print(f'{Fore.YELLOW}Error desconocido{Fore.RESET}')
            return 0
        else:
            if response.json()['resultsCount'] == 0:
                print(f'{Fore.YELLOW}No se obtuvieron resultados, verifique ticker y rango de fechas{Fore.RESET}')
                return 0
            else:
                return response.json()

    def get_ticker(self):
        ticker = input("Ingrese el ticker a pedir: \n").upper()
        return ticker

    def get_date(self, text):
        while True:
            date = input(text)
            date_now = datetime.today()

            # Se verifica que la fecha ingresada tenga el formato correcto
            try:
                date = datetime.strptime(date, '%Y-%m-%d')
            except ValueError:
                print("Formato de fecha incorrecto, debe ingresarse como YYYY-MM-DD")
            else:
                if date.date() > date_now.date():
                    print("La fecha ingresada no puede ser mayor a la fecha actual")
                if date < (date_now - timedelta(days=2*365)):
                    # Limitación cuenta Polygon
                    print("La subscripción gratuita no entrega registros de más de 2 años de antigüedad")
                else:
                    return str(date.date())


import random

from pyglet.shapes import Batch
from cv2 import Mat
import pyglet

from Ball import Ball
from Detection import Detection
from AppState import AppState
import Config as C

class Game:
  def __init__(self, width: int, height: int) -> None:
    self.width = width
    self.height = height
    self.batch = Batch()
    self.detection = Detection()
    self.color_list = C.Color.LIST

  def _create_ball(self, *_) -> None:
    colour = random.choice(self.color_list)
    y = random.randint(C.Ball.RADIUS_MAX * 2, self.height - C.Ball.RADIUS_MAX * 2)
    velocity = random.randint(C.Ball.VELOCITY_MIN, C.Ball.VELOCITY_MAX)

    ball = Ball(width=self.width, x=C.Ball.START_X, y=y, velocity=velocity, colour=colour, batch=self.batch)

    self.balls.append(ball)

  def _end_game(self, *_) -> None:
    self.state = AppState.END

  def init(self) -> None:
    self.balls = []
    self.score = 0
    self.state = AppState.START

    pyglet.clock.unschedule(self._create_ball)
    pyglet.clock.schedule_interval(self._create_ball, 1) #create a randomly assigned ball every 1 second
    pyglet.clock.schedule_once(self._end_game, C.Game.GAME_OVER_TIME) #automatically end the game after x seconds

  def run(self, thresh: Mat) -> None:
    for ball in self.balls:
      #code reference: https://github.com/ITT23/assignment-04-markers-and-computer-vision-tina-e/blob/master/ar_game/Ball.py
      hit = thresh[int(self.height - ball.y)][int(ball.x)]

      if hit:
        self.score += 1
        ball.delete()
        self.balls.remove(ball)

      else:
        ball.move()

    self.batch.draw()


import requests
import time
from bs4 import BeautifulSoup
from selenium import webdriver

#Headless 하는 법
options = webdriver.ChromeOptions()
options.headless = True
options.add_argument("window-size=1920x1080")

url = "https://play.google.com/store/movies/top"

browser = webdriver.Chrome(r"C:\Users\CKIRUser\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.7\jin\chromedriver.exe", options=options) #options는 여기 붙여주면 됨.

browser.get(url)

interval = 2

prev_height = browser.execute_script("return document.body.scrollHeight")

while True:
    #아래로 스크롤 내리
    browser.execute_script("window.scrollTo(0,document.body.scrollHeight)")
    time.sleep(interval)
    curr_height = browser.execute_script("return document.body.scrollHeight")
    if prev_height == curr_height:
        break
    prev_height = curr_height
print("scroll complete")

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

movies = soup.find_all("div", attrs={"class":"Vpfmgd"})

# print(len(movies)) #10개 나옴 -> 태그가 다름

for movie in movies:

    #할인 전 가격
    original_price = movie.find("span", attrs={"class":"SUZt4c djCuy"})
    if original_price:
        original_price = original_price.get_text()
        title = movie.find("div",attrs={"class":"WsMG1c nnK0zc"}).get_text()
    else:
        #print(title,"할인되지 않은 영화 제외")
        continue

    #할인 된 가격
    sale_price = movie.find("span", attrs={"class":"VfPpfd ZdBevf i5DZme"}).get_text()

    #링크
    link = movie.find("a", attrs={"class":"JC71ub"})["href"]
    # 올바른 링크 = url + link

    print("제목 : {}".format(title))
    print("할인 전 금액 : {}".format(original_price))
    print("할인 후 금액 : {}".format(sale_price))
    print("링크 : {}".format(url+link))
    print("-"*10)

browser.quit()


import logging
import platform
import time
from collections import OrderedDict

from common import checkhost
from common import iptools
from common import nem_exceptions
from common import netstat
from common import profiler
from common.nem_exceptions import SysmonitorException
from mist import system_resource

# Soglie di sistema
# ------------------------------------------------------------------------------
# Massima quantità di host in rete
TH_HOST = 1
# Minima memoria disponibile
th_avMem = 134217728
# Massimo carico percentuale sulla memoria
th_memLoad = 95
# Massimo carico percentuale sulla CPU
th_cpu = 85

logger = logging.getLogger(__name__)

class SysMonitor(object):
    def __init__(self, check_speed=False, bw_up=1000, bw_down=1000, ispid='fub001'):
        self._check_speed = check_speed
        self._bw_up = bw_up
        self._bw_down = bw_down
        self._ispid = ispid
        self._netstat = netstat.Netstat(iptools.get_dev())
        self._checks = OrderedDict([(system_resource.RES_OS, self.check_os),
                                    (system_resource.RES_CPU, self.checkcpu),
                                    (system_resource.RES_RAM, self.checkmem),
                                    (system_resource.RES_ETH, self.is_ethernet_active),
                                    (system_resource.RES_WIFI, self.checkwireless),
                                    (system_resource.RES_HOSTS, self.checkhosts),
                                    (system_resource.RES_TRAFFIC, self.check_traffic)])

    def checkres(self, res):
        return self._checks[res]()

    def log_interfaces(self):
        all_devices = profiler.get_all_devices()
        for device in all_devices:
            logger.info("============================================")
            device_dict = device.dict()
            for key in device_dict:
                logger.info("| %s : %s" % (key, device_dict[key]))
            logger.info("============================================")

    def check_os(self):
        value = 'unknown'
        try:
            value = platform.platform()
            info = ("Sistema Operativo %s" % value)
            status = True
        except Exception as e:
            info = e
            status = False
        return system_resource.SystemResource(system_resource.RES_OS, status, value, info)

    def checkcpu(self):
        value = -1
        try:
            value = profiler.cpu_load()
            if value < 0 or value > 100:
                raise SysmonitorException('Valore di occupazione della cpu non conforme.', nem_exceptions.BADCPU)

            if value > th_cpu:
                raise SysmonitorException('CPU occupata', nem_exceptions.WARNCPU)
            info = 'Utilizzato il %s%% del processore' % value
            status = True
        except Exception as e:
            info = e
            status = False
        return system_resource.SystemResource(system_resource.RES_CPU, status, value, info)

    def checkmem(self):
        try:
            av_mem = profiler.total_memory()
            if av_mem < th_avMem:
                logger.debug("Memoria disponibile: %2f" % av_mem)
                if av_mem < 0:
                    raise SysmonitorException('Valore di memoria disponibile non conforme.', nem_exceptions.BADMEM)
                else:
                    raise SysmonitorException('Memoria disponibile non sufficiente.', nem_exceptions.LOWMEM)

            mem_load = profiler.percentage_ram_usage()
            if mem_load < 0 or mem_load > 100:
                logger.debug("Memoria occupata: %d%%" % mem_load)
                raise SysmonitorException('Valore di occupazione della memoria non conforme.',
                                          nem_exceptions.INVALIDMEM)
            if mem_load > th_memLoad:
                logger.debug("Memoria occupata: %d%%" % mem_load)
                raise SysmonitorException('Memoria occupata.', nem_exceptions.OVERMEM)

            info = 'Utilizzato il %s%% di %d GB della memoria' % (mem_load, av_mem / (1000 * 1000 * 1000))
            status = True
        except Exception as e:
            mem_load = -1
            info = e
            status = False
        return system_resource.SystemResource(system_resource.RES_RAM, status, mem_load, info)

    def checkwireless(self):
        try:
            if profiler.is_wireless_active():
                raise SysmonitorException('Wireless LAN attiva.', nem_exceptions.WARNWLAN)
            else:
                value = 0
                info = 'Dispositivi wireless non attivi.'
                status = True
        except SysmonitorException as e:
            value = 1
            info = e
            status = False
        except Exception as e:
            logger.error("ERRORE", exc_info=True)
            value = -1
            info = e
            status = False
        return system_resource.SystemResource(system_resource.RES_WIFI, status, value, info)

    def checkhosts(self, use_arp=True):
        value = None
        try:
            ip = iptools.getipaddr()
            mask = iptools.get_network_mask(ip)
            dev = iptools.get_dev(ip=ip)

            logger.info("Indirizzo ip/mask: %s/%d, device: %s" % (ip, mask, dev))

            if iptools.is_public_ip(ip):
                status = True
                info = 'La scheda di rete in uso ha un IP pubblico. Non controllo il numero di dispositivi in rete.'
            else:
                if mask != 0:
                    value = checkhost.count_hosts(ip, mask, self._bw_up, self._bw_down, self._ispid, use_arp)
                    logger.info('Trovati %d dispositivi in rete.', value)
                    if value < 0:
                        raise SysmonitorException('Impossibile determinare il numero di dispositivi in rete.',
                                                  nem_exceptions.BADHOST)
                    elif value == 0:
                        if use_arp:
                            logger.warning("Passaggio a PING per controllo dispositivi in rete")
                            return self.checkhosts(False)
                        else:
                            raise SysmonitorException('Non risulta nessun dispositivo collegato in rete, '
                                                      'verifica connessione al router.',
                                                      nem_exceptions.BADHOST)
                    elif value > TH_HOST:
                        raise SysmonitorException('Ci sono {} altri dispositivi collegati alla tua '
                                                  'rete, scollegali.'.format(value - 1), nem_exceptions.TOOHOST)
                    else:
                        status = True
                        info = 'Trovati %d dispositivi in rete.' % value
                else:
                    raise SysmonitorException('Impossibile recuperare il valore della maschera dell\'IP: %s' % ip,
                                              nem_exceptions.BADMASK)
        except Exception as e:
            info = e
            status = False
        return system_resource.SystemResource(system_resource.RES_HOSTS, status, value, info)

    def is_ethernet_active(self):
        value = 0
        status = False
        info = ''
        try:
            devices = profiler.get_all_devices()
            for device in devices:
                dev_type = device.type
                if dev_type == 'Ethernet 802.3':
                    if device.is_enabled and device.is_active:
                        value = 1
                        info = 'Dispositivi ethernet attivi.'
                        status = True
                        if self._check_speed:
                            if (device.speed * 1000 < self._bw_up) or (device.speed * 1000 < self._bw_down):
                                raise SysmonitorException(("Dispositivi ethernet attivi, ma la scheda di rete di "
                                                           "{0}Mb/s non è sufficiente per misurare correttamente il "
                                                           "profilo riportato in fase "
                                                           "di registrazione.").format(device.speed),
                                                          nem_exceptions.WARNETH)
            if value == 0:
                raise SysmonitorException("Dispositivi ethernet non attivi o non presenti.", nem_exceptions.WARNETH)
        except Exception as e:
            info = e
            status = False
        return system_resource.SystemResource(system_resource.RES_ETH, status, value, info)

    def check_traffic(self, sec=2):

        value = 'unknown'
        try:
            # TODO: check for modified ip or dev
            ip = iptools.getipaddr()
            dev = iptools.get_dev(ip=ip)
            logger.debug("getting stats from dev " + dev)
            start_rx_bytes = self._netstat.get_rx_bytes()
            start_tx_bytes = self._netstat.get_tx_bytes()
            logger.debug("start rx %d, start tx %d" % (start_rx_bytes, start_tx_bytes))
            start_time = time.time()
            time.sleep(sec)
            end_rx_bytes = self._netstat.get_rx_bytes()
            end_tx_bytes = self._netstat.get_tx_bytes()
            measure_time_millis = (time.time() - start_time) * 1000
            logger.debug("end rx %d, end tx %d" % (end_rx_bytes, end_tx_bytes))
            logger.debug("total time millis %d" % measure_time_millis)

            up_kbps = (end_tx_bytes - start_tx_bytes) * 8 / measure_time_millis
            down_kbps = (end_rx_bytes - start_rx_bytes) * 8 / measure_time_millis
            if (up_kbps < 0) or (down_kbps < 0):
                raise SysmonitorException("Ottenuto valore di traffico negativo, potrebbe dipendere dall'azzeramento "
                                          "dei contatori.",
                                          nem_exceptions.FAILREADPARAM)
            value = (down_kbps, up_kbps)
            info = ("{0:.1f} kbps in download e {1:.1f} kbps in upload di traffico globale attuale sull'interfaccia "
                    "di rete in uso.".format(down_kbps, up_kbps))

            if int(up_kbps) < 20 and int(down_kbps) < 200:
                value = 'LOW'
            elif int(up_kbps) < 180 and int(down_kbps) < 1800:
                value = 'MEDIUM'
            else:
                value = 'HIGH'
            if value != 'LOW':
                raise Exception(info)
            status = True
        except Exception as e:
            info = e
            status = False
        return system_resource.SystemResource(system_resource.RES_TRAFFIC, status, value, info)

    def mediumcheck(self):
        self.checkcpu()
        self.checkmem()
        self.checkwireless()

    def checkall(self, use_arp=True):
        self.mediumcheck()
        self.checkhosts(use_arp)
        # TODO: Reinserire questo check quanto corretto il problema di determinazione del dato
        # checkdisk()


# -*- coding: utf-8 -*-
import sys
import codecs
from telethon import TelegramClient, sync
import re

# These example values won't work. You must get your own api_id, phone_number and
# api_hash from https://my.telegram.org, under API Development.
api_id = 111111
api_hash = 'eXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
phone_number = '+YYXXXXXXXXX'

# Start the telegram client
client = TelegramClient('session_id', api_id, api_hash)

# Check if it is already authorized
client.connect()
if not client.is_user_authorized():
    client.send_code_request(phone_number)
    me = client.sign_in(phone_number, input('Enter code: '))

# Get the chat_ids for the channels to do the search
hackplayers_id = client.get_entity('hackplayers').id
fwhibbit_id = client.get_entity('Fwhibbit').id
chat_ids = [hackplayers_id, fwhibbit_id]

# Define the search terms
search_terms = ['#ROJIRRINS', '#AZULONES', '#DULCINES', '#LECTURASENELROCA', '#ATMOSFERAPWN']

for search in search_terms:
    f= open(search.replace('#', '') + ".txt","w+")

    for chat in chat_ids:
        all_messages = client.get_messages(chat, None, search=search)
        for message in all_messages:
            f.write(str(message.message.replace('\n', ' ').encode('utf-8')))
            f.write('\n')

    f.close()


import threading, base64, json, jwt
import streamlit as st
from PIL import Image

def check_token_access(menuid):

    sighKey = "insert_sign_key_here"

    current_url = st.experimental_get_query_params()
    token = current_url.get("token", None)

    claims = None  # claims 변수를 미리 초기화
    try:
        claims = jwt.decode(token[0], base64.b64decode(sighKey), algorithms=['HS256'])
        if "menuList" not in claims:
            raise Exception("menu 정보가 없는 토큰입니다.")
    except jwt.ExpiredSignatureError:
        print("토큰이 만료되었습니다.")
    except jwt.InvalidTokenError:
        print("유효하지 않은 토큰입니다.")
    except Exception as e:
        print(str(e))

    if claims is not None:
        Subject = claims["sub"]

        menu_list_json = json.loads(claims['menuList'])

        menu_ids = [item['menuId'] for item in menu_list_json]
        #st.experimental_set_query_params(urlEncodedToken="")
        if menuid in menu_ids:
            #st.experimental_set_query_params(urlEncodedToken="")
            return True
        else:
            image = Image.open('./../../accessdenied403.png')
            st.image(image, width=70)
            st.title("403 Forbidden")
            st.markdown("# Permission Denied \n You do not have permission to access on this page.")
            st.markdown("Please go back to the portal below:")
            link = 'https://icebergdev.gscaltex.com/login'
            st.markdown(link, unsafe_allow_html=True)
            #st.experimental_set_query_params(urlEncodedToken="")
            exit()

    else:
        image = Image.open('./../../accessdenied403.png')
        st.image(image, width=70)
        st.title("403 Forbidden")
        st.write("Permission Denied \n You do not have permission to access on this page.")
        st.markdown("Please go back to the portal below:")
        link = 'https://icebergdev.gscaltex.com/'
        st.markdown(link, unsafe_allow_html=True)
        #st.experimental_set_query_params(urlEncodedToken="")
        exit()


#!/usr/bin/python3
"""
Contains Base class
"""
import json

class Base:
    """Base class"""
    __nb_objects = 0

    def __init__(self, id=None):
        """Constructor"""
        if id is not None:
            self.id = id
        else:
            Base.__nb_objects += 1
            self.id = Base.__nb_objects

    @staticmethod
    def to_json_string(list_dictionaries):
        """returns the json string representation
        of its argument"""
        if list_dictionaries is None:
            return "[]"
        else:
            return json.dumps(list_dictionaries)

    @classmethod
    def save_to_file(cls, list_objs):
        """saves list_objs to json file"""
        obj = []
        filename = cls.__name__ + ".json"

        if list_objs is not None:
            for instance in list_objs:
                obj.append(instance.to_dictionary())

        with open(filename, mode='w') as json_file:
            json_file.write(cls.to_json_string(obj))

    @staticmethod
    def from_json_string(json_string):
        """return the list of the JSON string representation"""
        if json_string == "" or json_string is None:
            return []
        else:
            return json.loads(json_string)

    @classmethod
    def create(cls, **dictionary):
        """returns an instance with attributes set
        using **dictionary"""
        if cls.__name__ == "Rectangle":
            dummy = cls(dictionary["width"], dictionary["height"])
        else:
            dummy = cls(dictionary["size"])

        dummy.update(**dictionary)
        return dummy

    @classmethod
    def load_from_file(cls):
        """loads JSON file and returns a list of instances"""
        filename = cls.__name__ + ".json"

        try:
            with open(filename, mode='r', encoding="UTF-8") as name:
                # convert list in filename and save to obj_dict
                obj_dict = cls.from_json_string(name.read())

            instances = []
            # create instances from obj_dict dictionaries
            for instance in obj_dict:
                instances.append(cls.create(**instance))
            return instances
        except FileNotFoundError:
            return []


#!/usr/bin/env python3

"""
DAG Factory methods to create an EMR Cluster, run steps and terminate cluster
Developer: Gokturk (GT) Yurtyapan
e-mail: gt.yurtyapan@starz.com
Date: 2019-04-09
"""

from datetime import datetime, timedelta
import airflow
import json
from airflow import DAG
from airflow.contrib.operators.emr_create_job_flow_operator \
    import EmrCreateJobFlowOperator
from airflow.contrib.operators.emr_add_steps_operator \
    import EmrAddStepsOperator
from airflow.contrib.sensors.emr_step_sensor import EmrStepSensor
from airflow.contrib.operators.emr_terminate_job_flow_operator \
    import EmrTerminateJobFlowOperator

def run_emr_job(current_dag, cluster_name, task_gen_name, aws_connection, emr_connection,
                script_location, library_location, region='us-east-1'):
    """
    Creates the EMR cluster, runs the step and terminates the cluster when it is completed
    current_dag: DAG that is created by the user
    cluster_name: Name given to cluster by the user
    task_gen_name: A general name for the task being done. This is used to name different tasks
    aws_connection: Connection to AWS for account credentials
    emr_connection: Name of Airflow connection storing EMR configuration details
    script_location: S3 location of the xcript to be run
    library_location: S3 location of the library being used to run spark-submit
    region: AWS region where the cluster is being created
    """

    # Name of the new cluster being created
    job_flow_overrides = {
        'Name': cluster_name
    }

    # name of task creating the cluster
    create_cluster_task_name = task_gen_name + "_create_cluster"

    # Task that creates the cluster
    cluster_creator = EmrCreateJobFlowOperator(
        task_id= create_cluster_task_name,
        job_flow_overrides=job_flow_overrides,
        aws_conn_id=aws_connection,
        emr_conn_id=emr_connection,
        dag=current_dag
    )

    # script-runner.jar file location is region specific
    script_runner_jar = 's3://' + region + '.elasticmapreduce/libs/script-runner/script-runner.jar'

    # Step description
    step_definition = [
        {
            'Name': task_gen_name,
            'ActionOnFailure': 'CONTINUE',
            'HadoopJarStep': {
                'Jar': script_runner_jar,
                'Args': [script_location, library_location, '']
            }
        }
    ]

    # Task that terminates the cluster
    cluster_remover = EmrTerminateJobFlowOperator(
        task_id=task_gen_name + "_remove_cluster",
        job_flow_id="{{ task_instance.xcom_pull('" + create_cluster_task_name + "', key='return_value') }}",
        aws_conn_id=aws_connection,
        dag=current_dag
    )

    # Add the step and step checker tasks
    add_step_to_emr(cluster_creator, task_gen_name, step_definition, cluster_remover,
                    create_cluster_task_name, aws_connection, current_dag)

def add_step_to_emr(cluster_create_task, task_identifier, step_params, cluster_remover, task_create_cluster,
                    aws_connection, dag):
    """
    In case we need to add multiple steps to the cluster
    cluster_create_task: ID of task that creates a cluster
    task_identifier: ID of step
    step_params: parameters to pass to the step
    cluster_remover: task that terminates the cluster
    task_create_cluster: task that creates the cluster
    aws_connection: Connection to AWS for account credentials
    dag: DAG that is created by the user
    """
    step_adder = EmrAddStepsOperator(
        task_id=task_identifier,
        job_flow_id="{{ task_instance.xcom_pull('" + task_create_cluster + "', key='return_value') }}",
        aws_conn_id=aws_connection,
        steps=step_params,
        dag=dag
    )

    step_checker = EmrStepSensor(
        task_id=task_identifier + '_watch_step',
        job_flow_id="{{ task_instance.xcom_pull('" + task_create_cluster + "', key='return_value') }}",
        step_id="{{ task_instance.xcom_pull('" + task_identifier + "', key='return_value')[0] }}",
        aws_conn_id=aws_connection,
        dag=dag
    )

    cluster_create_task.set_downstream(step_adder)
    step_adder.set_downstream(step_checker)
    step_checker.set_downstream(cluster_remover)


from PyQt5.QtCore import QSize, Qt, QRect
from PyQt5.QtGui import QIcon, QPixmap, QFont
from PyQt5.QtWidgets import QMainWindow, QWidget, QLabel, QGridLayout, QSystemTrayIcon, qApp, QAction, QMenu, QDesktopWidget

from current import Current, History
from lat_lon import lat_lon
from search import Search
from sqlbase import conn
from watch import Watch

class Weather(QMainWindow):

    def __init__(self):
        QMainWindow.__init__(self)

        self.setMinimumSize(QSize(540, 240))
        self.setWindowTitle("Senex")
        self.setAttribute(Qt.WA_TranslucentBackground, True)
        self.setWindowFlags(Qt.SplashScreen | Qt.FramelessWindowHint)
        with conn:
            cur = conn.cursor()
            cur.execute('SELECT * FROM settings;')
            (color, position) = cur.fetchone()
            cur.execute('SELECT timezone, lat, lon FROM info;')
            info = cur.fetchone()
        self.change_color(color)
        self.position(position)
        del color, position

        self.timezone = info[0] if info else 0
        self.geo = (info[1], info[2]) if info else None

        self.centralwidget = QWidget(self)
        self.centralwidget.setObjectName("centralwidget")

        self.layoutWidget = QWidget(self.centralwidget)
        self.layoutWidget.setGeometry(QRect(3, 2, 515, 235))
        self.layoutWidget.setObjectName("layoutWidget")

        self.gridLayout = QGridLayout(self.layoutWidget)
        self.gridLayout.setAlignment(Qt.AlignLeft)
        self.gridLayout.setContentsMargins(0, 0, 0, 0)
        self.gridLayout.setObjectName("gridLayout")

        tempfont = QFont()
        tempfont.setPointSize(40)
        cityfont = QFont()
        cityfont.setPointSize(16)
        midfont = QFont()
        midfont.setPointSize(12)
        smallfont = QFont()
        smallfont.setPointSize(10)

        self.datetime = QLabel(self.layoutWidget)
        self.datetime.setFont(midfont)
        self.datetime.setObjectName("datetime")
        self.gridLayout.addWidget(self.datetime, 0, 0, 1, 5)

        self.city = QLabel(self.layoutWidget)
        self.city.setFont(cityfont)
        self.city.setObjectName("city")
        self.gridLayout.addWidget(self.city, 1, 0, 1, 5)

        self.temp = QLabel(self.layoutWidget)
        self.temp.setFont(tempfont)
        self.temp.setAlignment(Qt.AlignCenter)
        self.temp.setObjectName("temp")
        self.gridLayout.addWidget(self.temp, 2, 0, 2, 1)

        self.icon = QLabel(self.layoutWidget)
        self.icon.setObjectName("icon")
        self.gridLayout.addWidget(self.icon, 2, 1, 2, 2)

        self.weather = QLabel(self.layoutWidget)
        self.weather.setFont(midfont)
        self.weather.setObjectName("weather")
        self.gridLayout.addWidget(self.weather, 2, 3, 1, 2)

        self.feels = QLabel(self.layoutWidget)
        self.feels.setFont(midfont)
        self.feels.setObjectName("feels")
        self.gridLayout.addWidget(self.feels, 3, 3, 1, 2)

        self.sunrise = QLabel(self.layoutWidget)
        self.sunrise.setFont(midfont)
        self.sunrise.setObjectName("sunrise")
        self.gridLayout.addWidget(self.sunrise, 4, 0, 1, 3)

        self.sunset = QLabel(self.layoutWidget)
        self.sunset.setFont(midfont)
        self.sunset.setObjectName("sunset")
        self.gridLayout.addWidget(self.sunset, 4, 3, 1, 2)

        self.wind = QLabel(self.layoutWidget)
        self.wind.setFont(midfont)
        self.wind.setObjectName("wind")
        self.gridLayout.addWidget(self.wind, 5, 0, 1, 2)

        self.humidity = QLabel(self.layoutWidget)
        self.humidity.setFont(midfont)
        self.humidity.setObjectName("humidity")
        self.gridLayout.addWidget(self.humidity, 5, 2, 1, 2)

        self.pressure = QLabel(self.layoutWidget)
        self.pressure.setFont(midfont)
        self.pressure.setObjectName("pressure")
        self.gridLayout.addWidget(self.pressure, 5, 4, 1, 1)

        self.yandex = QLabel(self.layoutWidget)
        self.yandex.setFont(smallfont)
        self.yandex.setObjectName("yandex")
        self.gridLayout.addWidget(self.yandex, 6, 0, 1, 5)

        self.today = QLabel(self.layoutWidget)
        self.today.setFont(smallfont)
        self.today.setObjectName("today")
        self.gridLayout.addWidget(self.today, 7, 0, 1, 5)

        self.tomorrow = QLabel(self.layoutWidget)
        self.tomorrow.setFont(smallfont)
        self.tomorrow.setObjectName("tomorrow")
        self.gridLayout.addWidget(self.tomorrow, 8, 0, 1, 5)

        self.setCentralWidget(self.centralwidget)

        self.tray_icon = QSystemTrayIcon(self)
        self.tray_icon.setIcon(QIcon('icons/01d.png'))

        position_action = QMenu("Положение виджета", self)
        color_action = QMenu("Цвет текста", self)
        city_action = QAction("Выбрать город", self)
        update_action = QAction("Обновить местоположение", self)
        quit_action = QAction("Закрыть Senex", self)

        tray_menu = QMenu()
        tray_menu.addMenu(position_action)
        tray_menu.addMenu(color_action)
        tray_menu.addAction(city_action)
        tray_menu.addAction(update_action)
        tray_menu.addAction(quit_action)

        center_action = QAction("По центру", self)
        upleft_action = QAction("Слева вверху", self)
        downleft_action = QAction("Слева внизу", self)
        upright_action = QAction("Справа вверху", self)
        downright_action = QAction("Справа внизу", self)

        position_action.addAction(center_action)
        position_action.addAction(upleft_action)
        position_action.addAction(downleft_action)
        position_action.addAction(upright_action)
        position_action.addAction(downright_action)

        white_action = QAction("Белый", self)
        black_action = QAction("Черный", self)
        gray_action = QAction("Серый", self)
        red_action = QAction("Красный", self)
        orange_action = QAction("Оранжевый", self)
        yellow_action = QAction("Желтый", self)
        green_action = QAction("Зеленый", self)
        blue_action = QAction("Синий", self)
        indigo_action = QAction("Индиго", self)
        violet_action = QAction("Фиолетовый", self)

        color_action.addAction(white_action)
        color_action.addAction(black_action)
        color_action.addAction(gray_action)
        color_action.addAction(red_action)
        color_action.addAction(orange_action)
        color_action.addAction(yellow_action)
        color_action.addAction(green_action)
        color_action.addAction(blue_action)
        color_action.addAction(indigo_action)
        color_action.addAction(violet_action)

        self.tray_icon.setContextMenu(tray_menu)
        self.tray_icon.show()

        self.history = History(mainwindow=self)
        self.watch = Watch(mainwindow=self)
        self.current = Current(mainwindow=self)
        self.search = Search(mainwindow=self)

        self.watch.run()
        self.history.run()
        self.current.run()

        center_action.triggered.connect(lambda: self.position('center'))
        upleft_action.triggered.connect(lambda: self.position('upleft'))
        downleft_action.triggered.connect(lambda: self.position('downleft'))
        upright_action.triggered.connect(lambda: self.position('upright'))
        downright_action.triggered.connect(lambda: self.position('downright'))

        white_action.triggered.connect(lambda: self.change_color('white'))
        black_action.triggered.connect(lambda: self.change_color('black'))
        gray_action.triggered.connect(lambda: self.change_color('gray'))
        red_action.triggered.connect(lambda: self.change_color('red'))
        orange_action.triggered.connect(lambda: self.change_color('orange'))
        yellow_action.triggered.connect(lambda: self.change_color('yellow'))
        green_action.triggered.connect(lambda: self.change_color('green'))
        blue_action.triggered.connect(lambda: self.change_color('blue'))
        indigo_action.triggered.connect(lambda: self.change_color('indigo'))
        violet_action.triggered.connect(lambda: self.change_color('violet'))

        city_action.triggered.connect(lambda: self.search.show())

        update_action.triggered.connect(lambda: self.current.new_values())

        quit_action.triggered.connect(qApp.quit)

        self.show()

    def seticon(self, path):
        self.icon.setPixmap(QPixmap(path))
        self.tray_icon.setIcon(QIcon(path))

    def change_color(self, color):
        self.setStyleSheet("QLabel { color: " + color + " }")
        with conn:
            cur = conn.cursor()
            cur.execute(f'UPDATE settings SET color = "{color}";')
            conn.commit()

    def position(self, pos):
        """ 'center'    - по центру
            'upleft'    - левый верхний угол
            'downleft'  - левый нижний угол
            'upright'   - правый верхний угол
            'downright' - правый нижний угол (стандартная позиция)"""

        _screen = QDesktopWidget().availableGeometry()
        width, height = (_screen.width(), _screen.height())
        x = width - 540
        y = height - 240
        if pos == 'center':
            x, y = x // 2, y // 2
        elif pos == 'upleft':
            x, y = 0, 0
        elif pos == 'downleft':
            x, y = 0, y
        elif pos == 'upright':
            x, y = x, 0
        self.move(x, y)
        with conn:
            cur = conn.cursor()
            cur.execute(f'UPDATE settings SET position = "{pos}";')
            conn.commit()
        del _screen, width, height, x, y, pos

if __name__ == "__main__":
    import sys

    from PyQt5.QtWidgets import QApplication

    app = QApplication(sys.argv)
    main = Weather()
    sys.exit(app.exec())


import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import RANSACRegressor

from function_tools import root_mean_square as rms

# 绘制结果
def basic_visualize(x, y, x_n, y_m):
    plt.scatter(x_n, y_m, color='b', label='Points with Noise')
    plt.plot(x, y, 'r', label='Original Line')
    plt.xlabel('x')
    plt.ylabel('y')

def func_visualize(x, k, b, string):
    plt.plot(x, k * x + b, color='darkorange', label= string + ' Fitted line')
    plt.legend()
    plt.title(string + ' Line Fitting')
    plt.show()

    print("k_fit =", k)
    print("b_fit =", b)

# 最小二乘拟合
def least_sqaure(x, y):
    A = np.vstack([x, np.ones(num_points)]).T
    k_fit, b_fit = np.linalg.lstsq(A, y, rcond=None)[0]

    print("最小二乘拟合直线的参数:")
    func_visualize(x, k_fit, b_fit, 'Least Squares')

    return k_fit * x + b_fit

# RANSAC拟合
def ransac(x,y):
    X = x.reshape(-1, 1)
    ransac = RANSACRegressor()
    ransac.fit(X, y)

    k_fit, b_fit = ransac.estimator_.coef_[0], ransac.estimator_.intercept_

    print("ransac拟合直线的参数:")
    func_visualize(x, k_fit, b_fit, 'RANSAC')
    return k_fit * x + b_fit

# 随机抽样数据
def generate_data(a, b, c, num_points, noise):
    x_oringal = np.linspace(-10, 10, num_points)
    y_oringal = -(a * x_oringal + c) / b
    # 添加噪声
    noise_n = np.random.normal(0, noise, num_points)
    noise_m = np.random.normal(0, noise, num_points)
    x_n = x_oringal + noise_n
    y_m = y_oringal + noise_m

    return x_oringal,y_oringal,x_n,y_m

num_points = 100

def main():
    a_given, b_given, c_given, noise = 2.0, -1.0, -6.0, 1
    x, y, x_n, y_m = generate_data(a_given, b_given, c_given, num_points, noise)
    y_right = a_given * x_n + c_given

    basic_visualize(x, y, x_n, y_m)
    y_predict = least_sqaure(x_n, y_m)
    print(f"RMS误差评价为：{rms(y_right, y_predict)}")
    print("")
    basic_visualize(x, y, x_n, y_m)
    y_predict = ransac(x_n, y_m)
    print(f"RMS误差评价为：{rms(y_right, y_predict)}")

if __name__ == "__main__":
    main()

Essential math for AI/2021112845-张智雄-实验一/linear.py


import numpy as np
import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from params import *  # Left, Right

#image = cv2.imread('test_images/warp_img_with_lines.jpg')  #straight_lines1  test5
test_image = cv2.imread('test_images/test5.jpg') #straight_lines1  test5

# Convert to RGB
test_image = cv2.cvtColor(test_image, cv2.COLOR_BGR2RGB)

def colour_extraction(image, r_thresh =  ([220, 255]), s_thresh = ([120, 255])):
    hls = cv2.cvtColor(image, cv2.COLOR_BGR2HLS)

    H = hls[:,:,0]
    L = hls[:,:,1]  # For shadows
    S = hls[:,:,2]  # general good detection

    R = image[:,:,0]
    G = image[:,:,1]  # For shadows
    B = image[:,:,2]

    #plt.imshow(G)
    #plt.show()

    #---------------------------- R-CHANNEL-FILTER -----------------------------
    retval, r_binary = cv2.threshold(R.astype('uint8'), r_thresh[0], r_thresh[1], cv2.THRESH_BINARY)
    #r_binary = np.zeros_like(R)
    #r_binary[(R >= thresh[0]) & (R <= thresh[1])] = 1
    #---------------------------- S-CHANNEL-FILTER -----------------------------

    retval, s_binary = cv2.threshold(S.astype('uint8'), s_thresh[0], s_thresh[1], cv2.THRESH_BINARY)
    #s_binary = np.zeros_like(R)
    #s_binary[(S >= thresh[0]) & (S <= thresh[1])] = 1

    #---------------------------- COMBINING CHANNELS ---------------------------

    # Combine the two images to a binary thresholds
    combined_binaries_255 = cv2.bitwise_or(r_binary, s_binary)
    combined_binaries = np.zeros_like(combined_binaries_255)
    combined_binaries[(combined_binaries_255 > 0)] = 1

    if show == True:
        # Plot results line extraction
        f, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(20, 8))
        f.tight_layout()
        ax1.imshow(s_binary,  cmap='gray')
        ax1.set_title('R-Binary', fontsize=20)
        ax2.imshow(r_binary, cmap='gray')
        ax2.set_title('S-Binary', fontsize=20)
        ax3.imshow(combined_binaries, cmap='gray')
        ax3.set_title('Combined', fontsize=20)
        plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.)
        plt.show()

    return combined_binaries

#--------------------------- GRADIENT -----------------------------------------
def gradient_extraction(img, gauss_kernel = 15, sobel_kernel=3, orient = 'both', thresh=(20, 255)):
    # Apply the following steps to img
    #  Convert to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    # Take the gradient in x, y direction separately, or in both directions. Transform the result to an absolute value
    cv2.GaussianBlur(gray, (gauss_kernel, gauss_kernel), 0)

    if orient == 'x':
        sob = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=sobel_kernel)
    elif orient == 'y':
        sob = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=sobel_kernel)
    else:
        sob = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=sobel_kernel)
        sob_y = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=sobel_kernel)
        # calculate the direction of the gradient
    abs_sob = np.abs(sob)
    #abs_sob_y = np.abs(sob_y)
    #gradient_direction = np.arctan2(sob_abs_y, sob_abs_x)
    scaled_sob= np.uint8(255*abs_sob/np.max(abs_sob))
    #scaled_sob = cv2.convertScaleAbs(sob, alpha=1, beta=0)

    # Create a binary mask
    binary = np.zeros_like(scaled_sob)
    binary[(scaled_sob >= thresh[0]) & (scaled_sob <= thresh[1])] = 1
    return binary

"""

#plt.figure(figsize=(30, 8))
plt.subplots(2, 4, figsize = (20, 7))
for i in range(1,9):
    thresh = ([30, 255])
    thresh[0] = 5 + 2 * i
    binary = gradient_extraction(test_image, sobel_kernel=3, orient='x', thresh=thresh)
    plt.subplot(2, 4, i)
    if i == 1:
        plt.imshow(test_image)
    else:
        plt.imshow(binary, cmap='gray')
    plt.title('Sobel, Lower Thresh = {}'.format(thresh[0]))
    plt.axis('off')
    plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
plt.show()

"""