#!/usr/bin/env python
# -*- coding:utf8 -*-

import sys

# for python2
try:
    print (sys.getdefaultencoding())
    reload(sys)
    sys.setdefaultencoding('utf-8')
    print (sys.getdefaultencoding())
except:
    pass

import os
import socket
import threading
import signal
import time
import json
from ctypes import *
import platform

import loadlib
from udpvclient import DecoderClient as vidodec
import udpvclient
from udpvclient import EncoderClient as videoenc
from udpvclient import ShowThread as display
from udpaclient import DecoderClient as audiodec
from udpaclient import EncoderClient as audioenc
from udpaclient import PlayThread as player
from avstream import AVStream as avstream
#import udpbase as base

DECACTOR  = 128
MAX_CHANID = 0x10000 #8#1#0x10000
LOSS_RATE = 0.05 #0.2#0.4 #0.2  # 0.8 #0.6 #0.2
EXCHANGE = 0# 1#0#1
SHOW_WIDTH = loadlib.WIDTH
SHOW_HEIGHT = loadlib.HEIGHT
SHOW_TYPE = 0# 1
SHOW_POLL_TIME = 10  # 5
SVC_REFS = 2#16#2
QOS_LEVEL = 1#0#1#3#2#1#0

global_port0 = 8097
global_port1 = 8098
global_host = 'localhost'
# global_host = '172.16.0.17'
# global_host = '111.230.226.17'
if len(sys.argv) > 3:
    global_port1 = int(sys.argv[3])
if len(sys.argv) > 2:
    global_port0 = int(sys.argv[2])
if len(sys.argv) > 1:
    global_host = sys.argv[1]

def json2str(jsonobj):
    if sys.version_info >= (3, 0):
        json_str = json.dumps(jsonobj, ensure_ascii=False, sort_keys=False).encode('utf-8')
    else:
        json_str = json.dumps(jsonobj, encoding='utf-8', ensure_ascii=False, sort_keys=False)

    return json_str

class AVSession(threading.Thread):
    def __init__(self, handle, windId, params, nettime, width, height, taskId, isCreater):
        threading.Thread.__init__(self)
        self.status = 0
        self.is_creater = isCreater
        #self.handle = handle
        self.handle_size = 8
        self.handle = create_string_buffer(self.handle_size)
        #self.handle2 = create_string_buffer(self.handle_size)
        self.handle2 = self.handle
        self.windId = windId
        self.params = params
        self.nettime = nettime
        self.modeId = -1
        self.taskId = 0#taskId
        self.screen_width = 1280
        self.screen_height = 720
        (self.width, self.height) = (width, height)
        #(self.width, self.height) = (1920/2, 1080/2)
        self.conferenceInfo = params.get("conference")
        self.multdevice = params.get("multdevice")
        self.control = params.get("control")
        self.general = params.get("general")
        # if self.multdevice != None:
        #    print("creat_preview: self.multdevice= ", self.multdevice)
        self.avstream_thread_list = {}
        self.istream_name = ""
        self.ostream_name = ""

        ###
        # self.deviceList = []
        self.upStreamList = []
        self.downStreamList = []
        self.speakList = []  # 记录共享者的ID号
        self.upChanList = []
        self.upAudioChanList = []
        self.downChanList = []
        self.downAudioChanList = []
        self.vDevList = []
        self.aDevList = []
        self.clientList = []
        self.init_params()
        print("self.screen_width= ",  self.screen_width)
        print("self.width= ", self.width)
        (self.vrtaskId, self.aptaskId, self.mcuTaskId) = (-1, -1, -1)
        loadlib.gload.lib.api_set_time_offset(nettime)
        ###
        param = {}
        #param.update({"taskType": 0})
        param.update({"taskType": 1})
        param.update({"taskId": self.taskId})
        param.update({"aliveTaskNum": 128})
        param.update({"taskNum": 256})
        #param.update({"port": port})
        #param.update({"server_ip": server_ip})
        param_str = json2str(param)
        loadlib.gload.lib.api_pool_start(self.handle, param_str)
        #param.update({"taskType": 1})
        #param_str = json2str(param)
        #loadlib.gload.lib.api_pool_start(self.handle2, param_str)
        ###
        loadlib.gload.lib.api_av_sdl_init()
        self.init_video()
        self.init_audio()
        ###
        self.lock = threading.Lock()
        self.__flag = threading.Event()  # 用于暂停线程的标识
        self.__flag.set()  # 设置为True
        self.__running = threading.Event()  # 用于停止线程的标识
        self.__running.set()  # 将running设置为True
        self.pause()
    def __del__(self):
        print("AVSession del")
        n = len(self.clientList)
        if n > 0:
            for i in range(n):
                del self.clientList[0]
            del self.clientList

        n = len(self.rectList)
        if n > 0:
            for i in range(n):
                del self.rectList[0]
            del self.rectList

        n = len(self.upChanList)
        if n > 0:
            for i in range(n):
                del self.upChanList[0]
            del self.upChanList

        if self.conferenceInfo != None:
            del self.conferenceInfo
        if self.multdevice != None:
            del self.multdevice
        if self.control != None:
            del self.control
        if self.general != None:
            del self.general

    def init_params(self):
        self.rectList = []
        if self.general != None and self.general.get("mode") != None:
            if self.general.get("mode") != None:
                self.modeId = self.general["mode"]["modeId"]
                modePos = self.general["mode"]["modePos"]
                for thisRect in modePos:
                    chanId = thisRect["chanId"]
                    deviceId = thisRect["deviceId"]
                    devicePos = thisRect["pos"]
                    actor = thisRect.get("actor")
                    if actor == None:
                        actor = chanId
                    self.speakList.append(chanId)
                    (width, height) = (self.width, self.height)

                    self.rectList.append(devicePos)
                    print("init_params: deviceId= ", deviceId)
                    if deviceId >= 0:
                        deviceDict = self.multdevice[str(deviceId)]
                        if deviceDict != None:
                            videos = deviceDict["videos"]
                            if videos != None:
                                scapture = videos["capture"]["name"]
                                sresolution = videos["resolution"]["name"]
                                if sresolution not in ["其他"]:
                                    swidth = sresolution.split("x")[0]
                                    sheight = sresolution.split("x")[1]
                                    #(self.width, self.height) = (int(swidth), int(sheight))
                                    (width, height) = (int(swidth), int(sheight))
                                    (self.width, self.height) = (width, height)
                    self.clientList.append((chanId, actor, deviceId, devicePos, width, height))

        elif self.control != None:
            mode = self.control["mode"]
            rect = self.control["rect"]
            if mode != None and rect != None:
                # self.deviceNum = len(rect)
                #self.clientList = []
                for thisRect in rect:
                    chanId = thisRect["chanId"]
                    deviceId = thisRect["deviceId"]
                    devicePos = thisRect["pos"]
                    actor = thisRect.get("actor")
                    if actor == None:
                        actor = chanId
                    (width, height) = (self.width, self.height)
                    #self.clientList.append((chanId, actor, deviceId, devicePos))
                    self.rectList.append(devicePos)
                    if deviceId >= 0:
                        deviceDict = self.multdevice[str(deviceId)]
                        if deviceDict != None:
                            videos = deviceDict["videos"]
                            if videos != None:
                                scapture = videos["capture"]["name"]
                                sresolution = videos["resolution"]["name"]
                                if sresolution not in ["其他"]:
                                    swidth = sresolution.split("x")[0]
                                    sheight = sresolution.split("x")[1]
                                    #(self.width, self.height) = (int(swidth), int(sheight))
                                    (width, height) = (int(swidth), int(sheight))
                                    (self.width, self.height) = (width, height)
                    self.clientList.append((chanId, actor, deviceId, devicePos, width, height))
        if self.general != None:
            mode = self.general["mode"]
            if mode != None:
                if mode.get("modeId") != None:
                    self.modeId = mode["modeId"]
                if mode.get("modePos") != None:
                    self.rects = mode["modePos"]
                # print("AVSession: init: self.rects= ", self.rects)
            supprams = self.general["supprams"]
            if supprams != None:
                spatiallayer = int(supprams["spatiallayer"]["name"])
                print("AVSession: init_video: spatiallayer= ", spatiallayer)
                stream = supprams.get("stream")
                if stream != None:
                    streamValue = stream.get("value")
                    self.ostream_name = stream.get("name")

        if self.conferenceInfo != None:
            self.screen_width = self.conferenceInfo["width"]
            self.screen_height = self.conferenceInfo["height"]
    def init_video(self):
        sup = self.general["supprams"]
        serverAddr = sup["serverAddr"]
        host = serverAddr.split(":")[0]
        port = int(serverAddr.split(":")[1])
        osd_enable = sup["osd"]
        params = {}
        params.update({"taskId": self.taskId})
        params.update({"server_ip": host})
        params.update({"port": port})
        params.update({"screen_width": self.screen_width})
        params.update({"screen_height": self.screen_height})

        #params.update({"width": self.width}) #pixel_width for render sdlTexture
        #params.update({"height": self.height}) #pixel_height for render sdlTexture

        params.update({"width": self.screen_width})
        params.update({"height": self.screen_height})

        params.update({"win_id": self.windId})
        params.update({"osd_enable": osd_enable})

        #param_str = json2str(params)
        #self.vctaskId = loadlib.gload.lib.api_start_capture(self.handle, param_str)
        #print("init_video: self.vctaskId=", self.vctaskId)
        #self.taskId += 1
        #params.update({"taskId": self.taskId})
        param_str = json2str(params)
        self.vrtaskId = loadlib.gload.lib.api_start_render(self.handle, param_str)
        print("init_video: self.vrtaskId=", self.vrtaskId)
        self.taskId += 1
        return
    def init_audio(self):
        sup = self.general["supprams"]
        serverAddr = sup["serverAddr"]
        host = serverAddr.split(":")[0]
        port = int(serverAddr.split(":")[1])
        params = {}
        params.update({"taskId": self.taskId})
        params.update({"server_ip": host})
        params.update({"port": port})
        #param_str = json2str(params)
        #self.actaskId = loadlib.gload.lib.api_start_acapture(self.handle, param_str)
        #print("init_audio: self.actaskId=", self.actaskId)
        #self.taskId += 1
        #params.update({"taskId": self.taskId})
        param_str = json2str(params)
        self.aptaskId = loadlib.gload.lib.api_start_player(self.handle, param_str)
        print("init_audio: self.aptaskId=", self.aptaskId)
        self.taskId += 1
        return
    def poll_pos(self, rectList):
        #if self.video_thread_show != None:
        #    self.rectList = rectList
        #    self.video_thread_show.SetRects(self.rectList)
        if self.vrtaskId >= 0:
            loadlib.gload.lib.api_poll_rect(self.handle, self.vrtaskId)
        return
    def reset_pos(self, modeId, width, height):
        #if self.video_thread_show != None:
        #    self.rectList = rectList
        #    self.video_thread_show.SetRects(self.rectList)
        if self.vrtaskId >= 0:
            loadlib.gload.lib.api_reset_rect(self.handle, self.vrtaskId, modeId, width, height)
        return
    def create_clients(self):
        print("create_clients: start")
        #导播合成
        #无导播合成多屏
        #导播合成多屏
        #导播多流
        if len(self.clientList) > 0 and (self.multdevice != None): #len(self.deviceList) > 0: #导播多流
            # 创建流通道
            sup = self.general["supprams"]
            substream = ""
            if sup.get("substream") != None:
                substream = sup["substream"]["name"]
            for thisClient in self.clientList:
                handle = None
                chanId = thisClient[0]
                actor = thisClient[1]
                deviceId = int(thisClient[2])
                width = thisClient[4]
                height = thisClient[5]
                if deviceId >= 0:
                    deviceDict = self.multdevice[str(deviceId)]
                    thisDevice = deviceDict["videos"]
                    inStreamName = ""
                    rstreamTaskId = -1
                    if thisDevice != None:
                        scapture = thisDevice["capture"]["name"]
                        if scapture in ["FILE"]:
                            inStreamName = thisDevice["yuvfilename"]
                    print("create_clients: inStreamName=", inStreamName)
                    # thisDevice = deviceDict["audios"]
                    # if thisDevice != None:
                    #    srecorder = thisDevice["recorder"]["name"]
                    #    if srecorder in ["stream"]:
                    #        inStreamName = thisDevice["streamname"]
                    if inStreamName != "":
                        params = {}
                        params.update({"infile": inStreamName})
                        params.update({"taskId": self.taskId})
                        params.update({"width": width})
                        params.update({"height": height})
                        params.update({"scodec": "HCSVC"})
                        params.update({"bitrate": 3 * 1024 * 1024})
                        params.update({"mtu_size": 1100})
                        params.update({"denoise": 0})
                        params.update({"osd_enable": 0})
                        params.update({"framerate": 25})
                        params.update({"refs": 1})
                        param_str = json2str(params)
                        rstreamTaskId = loadlib.gload.lib.api_start_stream(self.handle2, param_str)
                        self.taskId += 1
                        ###
                    self.upStreamList.append(rstreamTaskId)
                else:
                    ##substream = "/home/gxh/works/substream_" + str(chanId) + ".flv" #test
                    if substream != "":
                        handle = create_string_buffer(self.handle_size)
                        params = {}
                        params.update({"outfile": substream})
                        param_str = json2str(params)
                        ret = loadlib.gload.lib.api_avstream_init(handle, param_str)
                        if ret < 0:
                            print("create_clients: api_avstream_init fail ! ")
                    self.downStreamList.append(handle)
            time.sleep(1)
            #先建立编码通道
            for thisClient in self.clientList:
                chanId = thisClient[0]
                actor = thisClient[1]
                deviceId = int(thisClient[2])
                devicePos = thisClient[3]
                width = thisClient[4]
                height = thisClient[5]
                if deviceId >= 0:
                    deviceDict = self.multdevice[str(deviceId)]  # self.deviceList[deviceId]
                    print("create_clients: deviceDict= ", deviceDict)
                    (audioTaskId, aDevTaskId) = self.create_up_audio(chanId, actor, devicePos, deviceDict)
                    (videoTaskId, vDevTaskId) = self.create_up_video(chanId, actor, devicePos, deviceDict, width, height)
                    #print("create_clients: audioTaskId= ", audioTaskId)
                    self.upChanList.append((videoTaskId, vDevTaskId))
                    self.upAudioChanList.append((audioTaskId, aDevTaskId))
                    if self.ostream_name != "" and False:
                        streamObj = avstream.AVStream(chanId, width, height)
                        streamObj.outfile = self.ostream_name
                        #streamObj.video_codec_handle = video_thread.get_codec_handle()
                        #streamObj.audio_codec_handle = audio_thread.get_codec_handle()
                        #streamObj.openstream()
                        self.avstream_thread_list.append(streamObj)

            # 再建立解码通道
            for thisClient in self.clientList:
                chanId = thisClient[0]
                deviceId = int(thisClient[2])
                devicePos = thisClient[3]
                width = thisClient[4]
                height = thisClient[5]
                if deviceId >= 0:
                    pass
                else:
                    audioTaskId = self.create_down_audio(chanId, devicePos)
                    videoTaskId = self.create_down_video(chanId, devicePos, width, height)
                    self.downChanList.append(videoTaskId)
                    self.downAudioChanList.append(audioTaskId)
                    print("create_clients: chanId= ", chanId)
        ###
        time.sleep(2)

        ###创建合成通道
        mcustream = ""
        sup = self.general["supprams"]
        if sup.get("stream") != None:
            mcustream = sup["stream"]["name"]
        #mcustream = "/home/gxh/works/mcustream_0" + ".flv"  # test
        print("create_clients:mcustream= ", mcustream)
        if mcustream != "":
            params = {}
            params.update({"outfile": mcustream})
            params.update({"taskId": self.taskId})
            #params.update({"width": self.width})#mcu_width
            #params.update({"height": self.height})#mcu_height
            params.update({"width": self.screen_width})  # mcu_width
            params.update({"height": self.screen_height})  # mcu_height
            params.update({"scodec": "HCSVC"})
            params.update({"bitrate": 3*1024*1024})
            params.update({"mtu_size": 1100})
            params.update({"denoise": 0})
            params.update({"osd_enable": 0})
            params.update({"framerate": 25})
            params.update({"refs": 1})
            param_str = json2str(params)
            self.mcuTaskId = loadlib.gload.lib.api_start_mcu(self.handle2, param_str)
            self.taskId += 1

        #关联设备
        #for (taskId, devTaskId) in self.upAudioChanList:
        for i in range(len(self.upAudioChanList)):
            (taskId, devTaskId) = self.upAudioChanList[i]
            #stream = self.upStreamList[i]
            rstreamTaskId = self.upStreamList[i]
            params = {}
            params.update({"codecTaskId": taskId})
            params.update({"captureTaskId": devTaskId})
            params.update({"renderTaskId": self.aptaskId})
            params.update({"isEncoder": 1})
            param_str = json2str(params)
            loadlib.gload.lib.api_set4codec_audio(self.handle, self.handle2, param_str)
            #if stream != None:
            #    loadlib.gload.lib.api_setstream2audio(self.handle, stream, param_str)
            if rstreamTaskId >= 0:
                params = {}
                params.update({"codecTaskId": taskId})
                params.update({"rstreamTaskId": rstreamTaskId})
                param_str = json2str(params)
                loadlib.gload.lib.api_setreadstream2audio(self.handle, self.handle2, param_str)

        ###
        # self.vctaskId, self.vrtaskId, self.actaskId, self.aptaskId
        # for (taskId, devTaskId) in self.upChanList:
        for i in range(len(self.upChanList)):
            (taskId, devTaskId) = self.upChanList[i]
            print("create_clients: taskId=", taskId)
            #stream = self.upStreamList[i]
            rstreamTaskId = self.upStreamList[i]
            params = {}
            params.update({"codecTaskId": taskId})
            params.update({"captureTaskId": devTaskId})
            params.update({"renderTaskId": self.vrtaskId})
            params.update({"isEncoder": 1})
            param_str = json2str(params)
            loadlib.gload.lib.api_set4codec_video(self.handle, self.handle2, param_str)
            #if stream != None:
            #    loadlib.gload.lib.api_setstream2video(self.handle, stream, param_str)
            if rstreamTaskId >= 0:
                params = {}
                params.update({"codecTaskId": taskId})
                params.update({"rstreamTaskId": rstreamTaskId})
                param_str = json2str(params)
                loadlib.gload.lib.api_setreadstream2video(self.handle, self.handle2, param_str)

        #for taskId in self.downAudioChanList:
        for i in range(len(self.downAudioChanList)):
            taskId = self.downAudioChanList[i]
            stream = self.downStreamList[i]
            params = {}
            params.update({"codecTaskId": taskId})
            params.update({"captureTaskId": -1})
            params.update({"renderTaskId": self.aptaskId})
            params.update({"isEncoder": 0})
            param_str = json2str(params)
            loadlib.gload.lib.api_set4codec_audio(self.handle, self.handle2, param_str)
            if stream != None:
                loadlib.gload.lib.api_setstream2audio(self.handle, stream, param_str)
        ###
        # for taskId in self.downChanList:
        for i in range(len(self.downChanList)):
            taskId = self.downChanList[i]
            stream = self.downStreamList[i]
            params = {}
            params.update({"codecTaskId": taskId})
            params.update({"captureTaskId": -1})
            params.update({"renderTaskId": self.vrtaskId})
            params.update({"isEncoder": 0})
            param_str = json2str(params)
            loadlib.gload.lib.api_set4codec_video(self.handle, self.handle2, param_str)
            if stream != None:
                loadlib.gload.lib.api_setstream2video(self.handle, stream, param_str)
        ###
        if mcustream != "":
            params = {}
            params.update({"mcuTaskId": self.mcuTaskId})
            params.update({"renderTaskId": self.vrtaskId})
            params.update({"playerTaskId": self.aptaskId})
            param_str = json2str(params)
            loadlib.gload.lib.api_set4mcu(self.handle, self.handle2, param_str)

    def create_up_video(self, chanId, actor, devicePos, deviceDict, width, height):
        print("create_up_video: chanId= ", chanId)
        (id, sessionId, selfmode) = (chanId, 100, 0)
        #if not self.is_creater:
        #    actor = 2
        conf = self.conferenceInfo
        sessionId = conf["sessionId"]
        sup = self.general["supprams"]
        spatiallayer = int(sup["spatiallayer"]["name"])
        refs = int(sup["temporallayer"]["name"])
        bwthreshold = 0.25
        if sup.get("bwadjust") != None:
            bwthreshold = float(sup["bwadjust"]["name"])
        print("create_up_video: bwthreshold=", bwthreshold)
        lossrate = 0.0
        if sup.get("lossrate") != None:
            lossrate = float(sup["lossrate"]["name"])
        print("create_up_video: lossrate=", lossrate)

        print("create_up_video: refs=", refs)
        serverAddr = sup["serverAddr"]
        host = serverAddr.split(":")[0]
        port = int(serverAddr.split(":")[1])
        selfmode = sup["selfmode"]
        enable_netack = sup["netack"]
        ###video
        (bitrate, mtu_size, fec_level, buffer_shift) = self.GetParams(refs, width, height)
        (input_name, device_name, input_format, yuvfilename) = ("", "", "", "")
        (scodec, fecenable, denoise, framerate) = ("", 0, 0, 25)
        devicetype = 4
        osd_enable = sup["osd"]
        if deviceDict != None:
            thisDevice = deviceDict["videos"]
            if thisDevice != None:
                sresolution = thisDevice["resolution"]["name"]
                if sresolution not in ["其他"]:
                    swidth = sresolution.split("x")[0]
                    sheight = sresolution.split("x")[1]
                    width = int(swidth)
                    height = int(sheight)
                    (bitrate, mtu_size, fec_level, buffer_shift) = self.GetParams(refs, width, height)
                sbitrate = thisDevice["bitrate"]["name"]
                if sbitrate not in ["default", "其他"]:
                    sbitrate = sbitrate.replace("kbps", "")
                    ibitrate = int(sbitrate)
                    bitrate = ibitrate * 1024
                scapture = thisDevice["capture"]["name"]
                print("create_up_video: scapture= ", scapture)
                if scapture in ["FILE"]:
                    yuvfilename = thisDevice["yuvfilename"]
                    if yuvfilename != None:
                        devicetype = -1
                elif (platform.system() == 'Windows'):
                    input_name = "dshow"
                    device_name = scapture
                    input_format = "mjpeg"
                elif "video" in scapture:
                    input_name = "v4l2"
                    device_name = "/dev/" + str(scapture.split(":")[0])
                    input_format = str(scapture.split(":")[1])
                elif "fb" in scapture:
                    input_name = "x11grab"
                    device_name = ":0.0"
                    input_format = "raw"
                else:
                    print("create_up_video: scapture= ", scapture)
                    input_name = "dshow"
                    device_name = scapture
                    input_format = "mjpeg"
                scodec = thisDevice["codec"]["name"]
                fecenable = thisDevice["fec"]
                denoise = thisDevice["denoise"]
                if denoise:
                    denoise = 2
                sframerate = thisDevice["framerate"]["name"]
                if "fps" in sframerate:
                    sframerate = sframerate.replace("fps", "")
                    framerate = int(sframerate)

        params = {}
        params.update({"actor": actor})
        params.update({"sessionId": sessionId})
        params.update({"chanId": chanId})
        params.update({"modeId": self.modeId})
        params.update({"avtype": 0})
        params.update({"selfmode": selfmode})
        params.update({"width": width})
        params.update({"height": height})
        params.update({"screen_width": self.screen_width})
        params.update({"screen_height": self.screen_height})
        params.update({"enable_netack": enable_netack})
        params.update({"testmode": 0})
        params.update({"nettime": self.nettime})
        params.update({"port": port})
        params.update({"server_ip": host})
        params.update({"idx": 0})
        params.update({"win_id": self.windId})
        params.update({"isEncoder": 1})
        params.update({"taskId": self.taskId})
        #
        params.update({"yuvfilename": yuvfilename})
        params.update({"input_name": input_name})
        params.update({"device_name": device_name})
        params.update({"input_format": input_format})
        params.update({"bitrate": bitrate})
        params.update({"mtu_size": mtu_size})
        params.update({"fecenable": fecenable})
        params.update({"fec_level": fec_level})
        params.update({"buffer_shift": buffer_shift})
        params.update({"scodec": scodec})
        params.update({"denoise": denoise})
        params.update({"osd_enable": osd_enable})
        params.update({"framerate": framerate})
        params.update({"refs": refs})
        params.update({"bwthreshold": bwthreshold})
        params.update({"lossrate": lossrate})

        print("create_up_video: yuvfilename= ", yuvfilename)
        vctaskId = -1
        if yuvfilename == "":
            param_str = json2str(params)
            vctaskId = loadlib.gload.lib.api_start_capture(self.handle, param_str)
            self.vDevList.append(vctaskId)
            self.taskId += 1

        params.update({"taskId": self.taskId})
        param_str = json2str(params)
        codecTaskId = loadlib.gload.lib.api_start_video(self.handle2, param_str)
        self.taskId += 1
        return (codecTaskId, vctaskId)

    def create_up_audio(self, chanId, actor, devicePos, deviceDict):
        (id, sessionId, selfmode) = (chanId, 100, 0)
        #if not self.is_creater:
        #    actor = 2
        conf = self.conferenceInfo
        sessionId = conf["sessionId"]
        sup = self.general["supprams"]
        spatiallayer = int(sup["spatiallayer"]["name"])
        refs = int(sup["temporallayer"]["name"])
        serverAddr = sup["serverAddr"]
        host = serverAddr.split(":")[0]
        port = int(serverAddr.split(":")[1])
        selfmode = sup["selfmode"]
        enable_netack = sup["netack"]
        lossrate = 0.0
        if sup.get("lossrate") != None:
            lossrate = float(sup["lossrate"]["name"])
        print("create_up_audio: lossrate=", lossrate)
        ###audio
        (bitrate, mtu_size, buffer_shift) = (24000, 300, 10)
        bitrate = int(deviceDict["audios"]["bitrate"]["name"])
        (input_name, device_name, yuvfilename) = ("", "", "")
        (scodec, fecenable, denoise) = ("", 0, 0)
        (in_channels, in_sample_rate, out_nb_samples) = (0, 0, 0)

        if deviceDict != None:
            thisDevice2 = deviceDict["videos"]
            if thisDevice2 != None:
                scapture = thisDevice2["capture"]["name"]
                if scapture in ["FILE"]:
                    yuvfilename = thisDevice2["yuvfilename"]
                    if yuvfilename != None:
                        devicetype = -1
            thisDevice = deviceDict["audios"]
            if thisDevice != None:
                sbitrate = thisDevice["bitrate"]["name"]
                if sbitrate not in ["default", "其他"]:
                    #sbitrate = sbitrate.replace("kbps", "")
                    bitrate = int(sbitrate)
                srecorder = thisDevice["recorder"]["name"]

                input_name = "alsa"
                if (platform.system() == 'Windows'):
                    input_name = "dshow"
                    print('Windows系统')
                elif (platform.system() == 'Linux'):
                    print('Linux系统')
                else:
                    print('其他')
                device_name = srecorder
                if srecorder not in ["default"]:
                    pass
                scodec = thisDevice["codec"]["name"]
                cchannels = thisDevice["channels"]["name"]
                channels = int(cchannels)
                caudiofmt = thisDevice["audiofmt"]["name"]
                cframesize = thisDevice["framesize"]["name"]
                framesize = int(cframesize)
                csamplerate = thisDevice["samplerate"]["name"]
                samplerate = int(csamplerate)
                in_channels = channels
                in_sample_rate = samplerate
                #audio_thread.in_sample_fmt = "AV_SAMPLE_FMT_S16"
                #audio_thread.out_nb_samples
                out_nb_samples = framesize
        #port = port + 1
        params = {}
        params.update({"actor": actor})
        params.update({"sessionId": sessionId})
        params.update({"chanId": chanId})
        params.update({"modeId": self.modeId})
        params.update({"avtype": 1})
        params.update({"selfmode": selfmode})
        #params.update({"width": self.width})
        #params.update({"height": self.height})
        params.update({"enable_netack": enable_netack})
        params.update({"testmode": 0})
        params.update({"nettime": self.nettime})
        params.update({"port": port + 1})
        params.update({"server_ip": host})
        params.update({"idx": 0})
        params.update({"win_id": self.windId})
        params.update({"isEncoder": 1})
        params.update({"taskId": self.taskId})
        #
        params.update({"yuvfilename": yuvfilename})
        params.update({"input_name": input_name})
        params.update({"device_name": device_name})
        params.update({"bitrate": bitrate})
        params.update({"mtu_size": mtu_size})
        params.update({"fecenable": fecenable})
        params.update({"buffer_shift": buffer_shift})
        params.update({"scodec": scodec})
        params.update({"denoise": denoise})
        params.update({"in_channels": in_channels})
        params.update({"in_sample_rate": in_sample_rate})
        params.update({"out_nb_samples": out_nb_samples})
        params.update({"lossrate": lossrate})

        actaskId = -1
        if yuvfilename == "":
            param_str = json2str(params)
            actaskId = loadlib.gload.lib.api_start_acapture(self.handle, param_str)
            self.vDevList.append(actaskId)
            self.taskId += 1

        params.update({"taskId": self.taskId})
        param_str = json2str(params)
        codecTaskId = loadlib.gload.lib.api_start_audio(self.handle2, param_str)
        self.taskId += 1
        return (codecTaskId, actaskId)

    def create_down_video(self, chanId, devicePos, width, height):
        print("create_down_video: chanId= ", chanId)
        (id, sessionId, actor) = (chanId, 100, DECACTOR)
        conf = self.conferenceInfo
        sessionId = conf["sessionId"]
        sup = self.general["supprams"]
        spatiallayer = int(sup["spatiallayer"]["name"])
        refs = int(sup["temporallayer"]["name"])
        osd_enable = sup["osd"]
        selfmode = sup["selfmode"]
        serverAddr = sup["serverAddr"]
        host = serverAddr.split(":")[0]
        port = int(serverAddr.split(":")[1])
        osd_enable = sup["osd"]
        enable_netack = sup["netack"]

        (bitrate, mtu_size, fec_level, buffer_shift) = self.GetParams(refs, width, height)
        params = {}
        params.update({"actor": actor})
        params.update({"sessionId": sessionId})
        params.update({"chanId": chanId})
        params.update({"modeId": self.modeId})
        params.update({"avtype": 0})
        params.update({"selfmode": selfmode})
        params.update({"enable_netack": enable_netack})
        params.update({"screen_width": self.screen_width})
        params.update({"screen_height": self.screen_height})
        params.update({"width": width})
        params.update({"height": height})
        params.update({"testmode": 0})
        params.update({"nettime": self.nettime})
        params.update({"port": port})
        params.update({"server_ip": host})
        params.update({"idx": 0})
        params.update({"win_id": self.windId})
        params.update({"isEncoder": 0})
        params.update({"taskId": self.taskId})
        params.update({"osd_enable": osd_enable})
        param_str = json2str(params)
        codecTaskId = loadlib.gload.lib.api_start_video(self.handle2, param_str)
        self.taskId += 1
        return codecTaskId

    def create_down_audio(self, chanId, devicePos):
        (id, sessionId, actor) = (chanId, 100, DECACTOR)
        conf = self.conferenceInfo
        sessionId = conf["sessionId"]
        sup = self.general["supprams"]
        selfmode = sup["selfmode"]
        enable_netack = sup["netack"]
        spatiallayer = int(sup["spatiallayer"]["name"])
        refs = int(sup["temporallayer"]["name"])
        serverAddr = sup["serverAddr"]
        host = serverAddr.split(":")[0]
        port = int(serverAddr.split(":")[1])
        ###audio
        (bitrate, mtu_size, buffer_shift) = (24000, 300, 11)
        #port = port + 1
        params = {}
        params.update({"actor": actor})
        params.update({"sessionId": sessionId})
        params.update({"chanId": chanId})
        params.update({"modeId": self.modeId})
        params.update({"avtype": 1})
        params.update({"selfmode": selfmode})
        params.update({"enable_netack": enable_netack})
        #params.update({"width": self.width})
        #params.update({"height": self.height})
        params.update({"testmode": 0})
        params.update({"nettime": self.nettime})
        params.update({"port": port + 1})
        params.update({"server_ip": host})
        params.update({"idx": 0})
        params.update({"win_id": self.windId})
        params.update({"isEncoder": 0})
        params.update({"taskId": self.taskId})
        param_str = json2str(params)
        codecTaskId = loadlib.gload.lib.api_start_audio(self.handle2, param_str)
        self.taskId += 1
        return codecTaskId

    def clients_stop(self):
        print("stop codec")
        for (taskId, devTaskId) in self.upChanList:
            loadlib.gload.lib.api_stop_task(self.handle2, taskId)
        for (taskId, devTaskId) in self.upAudioChanList:
            loadlib.gload.lib.api_stop_task(self.handle2, taskId)
        for taskId in self.downChanList:
            loadlib.gload.lib.api_stop_task(self.handle2, taskId)
        for taskId in self.downAudioChanList:
            loadlib.gload.lib.api_stop_task(self.handle2, taskId)

        for taskId in self.upStreamList:
            if taskId >= 0:
                loadlib.gload.lib.api_stop_task(self.handle2, taskId)

        #loadlib.gload.lib.api_pool_stop(self.handle2)
        #loadlib.gload.lib.api_stop_task(self.handle2, -1)

        print("stop capture and render/player")

        for taskId in self.vDevList:
            loadlib.gload.lib.api_stop_task(self.handle, taskId)
        for taskId in self.aDevList:
            loadlib.gload.lib.api_stop_task(self.handle, taskId)

        #音视频播放／渲染退出存在冲突
        print("clients_stop: self.aptaskId=", self.aptaskId)
        if self.aptaskId >= 0:
            loadlib.gload.lib.api_stop_task(self.handle, self.aptaskId)

        #time.sleep(1)
        print("clients_stop: self.vrtaskId=", self.vrtaskId)
        if self.vrtaskId >= 0:
            loadlib.gload.lib.api_stop_task(self.handle, self.vrtaskId)

        if self.mcuTaskId >= 0:
            loadlib.gload.lib.api_stop_task(self.handle, self.mcuTaskId)
        #loadlib.gload.lib.api_pool_stop(self.handle)

        print("clients_stop: over")
    def get_status(self):
        ret = 0
        self.lock.acquire()
        ret = self.status
        self.lock.release()
        return ret
    def set_status(self, value):
        self.lock.acquire()
        self.status = value
        self.lock.release()
    def set_winid(self, win_hnd):
        if win_hnd > 0:
            self.windId = win_hnd

    def close_session(self):
        self.clients_stop()
        print("close_session")
        #self.set_status(0)
        #while self.get_status() != 0:
        #    time.sleep(0.1)

    def stop(self):
        self.__flag.set()  # 将线程从暂停状态恢复, 如何已经暂停的话
        self.__running.clear()  # 设置为Fals

    def pause(self):
        self.__flag.clear()  # 设置为False, 让线程阻塞

    def resume(self):
        self.__flag.set()  # 设置为True, 让线程停止阻塞

    def run(self):
        self.create_clients()
        self.set_status(1)
        loadlib.gload.lib.api_pool_stop(self.handle)
        loadlib.gload.lib.api_stop_task(self.handle, -1)
        ###
        #for handle in self.upStreamList:
        #    if handle != None:
        #        loadlib.gload.lib.api_avstream_close(handle)
        for handle in self.downStreamList:
            if handle != None:
                loadlib.gload.lib.api_avstream_close(handle)

        ###
        self.set_status(0)

    def GetParams(self, refs, width, height):
        #(bitrate, mtu_size, fec_level, buffer_shift)
        #buffer_shift不能过于接近seqnum的最大值,不能超过14位
        ret = (3 * 1024 * 1024, 1400, 0, 10)
        (h, w) = (width, height)
        if (w * h) <= 176 * 144:
            fec_level = 2
            if refs <= 2:
                fec_level = 2
            ret = (256 * 1024, 300, fec_level, 10)
        elif (w * h) <= 352 * 288:
            fec_level = 2
            if refs <= 2:
                fec_level = 2
            ret = (512 * 1024, 400, fec_level, 10)
        elif (w * h) <= 640 * 480:
            fec_level = 2
            if refs <= 2:
                fec_level = 2
            ret = (800 * 1024, 600, fec_level, 11)
        elif (w * h) <= 704 * 576:
            fec_level = 2
            if refs <= 2:
                fec_level = 2
            ret = (1200 * 1024, 600, fec_level, 12)
        elif (w * h) <= 960 * 540:
            fec_level = 2
            if refs <= 2:
                fec_level = 2
            ret = (1500 * 1024, 720, fec_level, 12)
        elif (w * h) <= 1280 * 720:
            fec_level = 2
            if refs <= 2:
                fec_level = 2
            ret = (int(2.0 * 1024 * 1024), 800, fec_level, 13)
        elif (w * h) <= 1920 * 1080:
            fec_level = 2
            if refs <= 2:
                fec_level = 2
            ret = (3.0 * 1024 * 1024, 1100, fec_level, 14)
        else:
            fec_level = 2
            ret = (6 * 1024 * 1024, 1400, fec_level, 14)
        return ret

if __name__ == "__main__":
    print("start avclient")

    print("end avclient")


#!/usr/bin/env python3

__author__ = 'jpleino1'

import pympi
import os
import sys
import re

def main(elan_file_path):
    eaf = pympi.Elan.Eaf(elan_file_path)
    abspath = os.path.abspath(elan_file_path)
    abspath_to, full_filename = os.path.split(abspath)
    filename, file_ext = os.path.splitext(full_filename)

    trn_name = os.path.join(abspath_to, "text")
    utt2spk_name = os.path.join(abspath_to, "utt2spk")
    spk2utt_name = os.path.join(abspath_to, "spk2utt")
    segments_name = os.path.join(abspath_to, "segments")
    reco2file_name = os.path.join(abspath_to, "reco2file_and_channel")
    wav_basepath = "data/src/wavs"
    wav_name = os.path.join(abspath_to, "wav.scp")

    columns = []
    numbers = 4 # For zfill, e.g., utterance 34 becomes 0034

    with open(trn_name, 'w', encoding="utf-8") as trn,\
        open(utt2spk_name, 'w', encoding="utf-8") as utt2spk,\
        open(spk2utt_name, 'w', encoding="utf-8") as spk2utt,\
        open(segments_name, 'w', encoding="utf-8") as segments,\
        open(reco2file_name, 'w', encoding="utf-8") as reco2file,\
        open(wav_name, 'w', encoding="utf-8") as wavs:

        for tier in eaf.tiers:
            try:
                for start, end, word in eaf.get_annotation_data_for_tier(tier):
                    columns.append([start, end, word])
            except:
                print(tier)
        columns = sorted(columns, key=lambda column: column[0])

        for count, column in enumerate(columns):
            utterance = filename + "-" + str(count).zfill(numbers)
            line_s = [utterance, filename, str(column[0]/1000.0), str(column[1]/1000.0)]

            segments.write(" ".join(line_s) + "\n")
            utt2spk.write(utterance + " " + utterance + "\n")
            spk2utt.write(utterance + " " + utterance + "\n")
            line_t = column[2]
            # Bit over zealous (ge) OK but (=ge) is not
            line_t = re.sub(r'\([^)]*\)', '', line_t)
            line_t = line_t.lower()
            line_t = re.sub(r"[^\w ']", " ", line_t, flags=re.UNICODE)
            line_t = line_t.strip()
            line_t = re.sub(' +', ' ', line_t)
            trn.write(utterance + " " + line_t + " " + "\n")
        wavs.write(filename + " " + wav_basepath + "/" + filename + ".wav" + "\n")
        reco2file.write(filename + " " + filename + " A\n")

if __name__ == "__main__":
    main(sys.argv[1])


import argparse
import os
from loguru import logger
import sys
sys.path.append('..')

import torch
from torch import nn

from models.basic.conv import SiLU
from utils.misc import load_weight, replace_module
from config import build_config
from models import build_model

def make_parser():
    parser = argparse.ArgumentParser("FreeYOLO ONNXRuntime")
    # basic
    parser.add_argument("--output-name", type=str, default="yolo_free_large.onnx",
                        help="output name of models")
    parser.add_argument('-size', '--img_size', default=640, type=int,
                        help='the max size of input image')
    parser.add_argument("--input", default="images", type=str,
                        help="input node name of onnx model")
    parser.add_argument("--output", default="output", type=str,
                        help="output node name of onnx model")
    parser.add_argument("-o", "--opset", default=11, type=int,
                        help="onnx opset version")
    parser.add_argument("--batch-size", type=int, default=1,
                        help="batch size")
    parser.add_argument("--dynamic", action="store_true", default=False,
                        help="whether the input shape should be dynamic or not")
    parser.add_argument("--no-onnxsim", action="store_true", default=False,
                        help="use onnxsim or not")
    parser.add_argument("-f", "--exp_file", default=None, type=str,
                        help="experiment description file")
    parser.add_argument("-expn", "--experiment-name", type=str, default=None)
    parser.add_argument("opts", default=None, nargs=argparse.REMAINDER,
                        help="Modify config options using the command-line")
    parser.add_argument("--decode_in_inference", action="store_true", default=False,
                        help="decode in inference or not")
    parser.add_argument('--save_dir', default='../weights/onnx/', type=str,
                        help='Dir to save onnx file')

    # model
    parser.add_argument('-v', '--version', default='yolo_free_large', type=str,
                        help='build yolo')
    parser.add_argument('--weight', default=None,
                        type=str, help='Trained state_dict file path to open')
    parser.add_argument('-ct', '--conf_thresh', default=0.1, type=float,
                        help='confidence threshold')
    parser.add_argument('-nt', '--nms_thresh', default=0.5, type=float,
                        help='NMS threshold')
    parser.add_argument('--topk', default=100, type=int,
                        help='topk candidates for testing')
    parser.add_argument("--no_decode", action="store_true", default=False,
                        help="not decode in inference or yes")
    parser.add_argument('-nc', '--num_classes', default=80, type=int,
                        help='topk candidates for testing')

    return parser

@logger.catch
def main():
    args = make_parser().parse_args()
    logger.info("args value: {}".format(args))
    device = torch.device('cpu')

    # config
    cfg = build_config(args)

    # build model
    model = build_model(
        args=args,
        cfg=cfg,
        device=device,
        num_classes=args.num_classes,
        trainable=False
        )

    # load trained weight
    model = load_weight(model=model, path_to_ckpt=args.weight)
    model = model.to(device).eval()

    # replace nn.SiLU with SiLU
    model = replace_module(model, nn.SiLU, SiLU)

    logger.info("loading checkpoint done.")
    dummy_input = torch.randn(args.batch_size, 3, args.img_size, args.img_size)

    # save onnx file
    save_path = os.path.join(args.save_dir, str(args.opset))
    os.makedirs(save_path, exist_ok=True)
    output_name = os.path.join(save_path, args.output_name)

    torch.onnx._export(
        model,
        dummy_input,
        output_name,
        input_names=[args.input],
        output_names=[args.output],
        dynamic_axes={args.input: {0: 'batch'},
                      args.output: {0: 'batch'}} if args.dynamic else None,
        opset_version=args.opset,
    )

    logger.info("generated onnx model named {}".format(output_name))

    if not args.no_onnxsim:
        import onnx

        from onnxsim import simplify

        input_shapes = {args.input: list(dummy_input.shape)} if args.dynamic else None

        # use onnxsimplify to reduce reduent model.
        onnx_model = onnx.load(output_name)
        model_simp, check = simplify(onnx_model,
                                     dynamic_input_shape=args.dynamic,
                                     input_shapes=input_shapes)
        assert check, "Simplified ONNX model could not be validated"

        # save onnxsim file
        save_path = os.path.join(save_path, 'onnxsim')
        os.makedirs(save_path, exist_ok=True)
        output_name = os.path.join(save_path, args.output_name)
        onnx.save(model_simp, output_name)
        logger.info("generated simplified onnx model named {}".format(output_name))

if __name__ == "__main__":
    main()


import DataBase
import ShowAll

class DeleteAnime:
    def __init__(self):
        self.__database = DataBase.DataBase()

    def delete_one_anime(self, update, context):
        anime_id = update.callback_query.data.split('/')[1]
        chat_id = update.effective_chat.id
        message_id = str(update.callback_query.message.message_id)

        self.__database.delete_anime_from_links(chat_id, anime_id)

        settings = self.__database.select_chat_settings(chat_id)
        __message_pagination_ids = settings[0][2]
        __pagination_pages = settings[0][3]
        message_ids = __message_pagination_ids.split(';')
        message_ids.remove(message_id)
        self.__database.settings_update_mess_ids(chat_id, ';'.join(message_ids), __pagination_pages)

        context.bot.deleteMessage(
            update.effective_chat.id,
            message_id,
        )

        __show_all = ShowAll.ShowAll(chat_id)
        __show_all.show_all(update, context)


from typing import Any, Dict, Optional, Union

import torch

from modelscope.metainfo import Pipelines
from modelscope.models.multi_modal import HiTeAForAllTasks
from modelscope.outputs import OutputKeys
from modelscope.pipelines.base import Model, Pipeline
from modelscope.pipelines.builder import PIPELINES
from modelscope.preprocessors import HiTeAPreprocessor, Preprocessor
from modelscope.utils.constant import Tasks
from modelscope.utils.logger import get_logger

logger = get_logger()

@PIPELINES.register_module(
    Tasks.video_captioning, module_name=Pipelines.video_captioning)
class VideoCaptioningPipeline(Pipeline):

    def __init__(self,
                 model: Union[Model, str],
                 preprocessor: Optional[Preprocessor] = None,
                 **kwargs):
        """
        use `model` and `preprocessor` to create a video captioning pipeline for prediction
        Args:
            model: model id on modelscope hub.
        """
        super().__init__(model=model, preprocessor=preprocessor, **kwargs)
        self.model.eval()
        if preprocessor is None:
            if isinstance(self.model, HiTeAForAllTasks):
                self.preprocessor = HiTeAPreprocessor(self.model.model_dir)

    def _batch(self, data):
        if isinstance(self.model, HiTeAForAllTasks):
            from transformers.tokenization_utils_base import BatchEncoding
            batch_data = dict(train=data[0]['train'])
            batch_data['video'] = torch.cat([d['video'] for d in data])
            question = {}
            for k in data[0]['question'].keys():
                question[k] = torch.cat([d['question'][k] for d in data])
            batch_data['question'] = BatchEncoding(question)
            return batch_data
        else:
            return super()._collate_batch(data)

    def forward(self, inputs: Dict[str, Any],
                **forward_params) -> Dict[str, Any]:
        with torch.no_grad():
            return super().forward(inputs, **forward_params)

    def postprocess(self, inputs: Dict[str, Any]) -> Dict[str, Any]:
        return inputs


# SuperFastPython.com
# scan a range of port numbers on a host concurrently
from socket import AF_INET
from socket import SOCK_STREAM
from socket import socket
from multiprocessing.pool import ThreadPool

# returns True if can connect, False otherwise
def test_port_number(host, port):
    # create and configure the socket
    with socket(AF_INET, SOCK_STREAM) as sock:
        # set a timeout of a few seconds
        sock.settimeout(3)
        # connecting may fail
        try:
            # attempt to connect
            sock.connect((host, port))
            # a successful connection was made
            return True
        except:
            # ignore the failure
            return False

# scan port numbers on a host
def port_scan(host, ports):
    print(f'Scanning {host}...')
    # create the thread pool
    with ThreadPool(len(ports)) as pool:
        # prepare arguments for starmap
        args = [(host,p) for p in ports]
        # dispatch all tasks
        results = pool.starmap(test_port_number, args)
        # report results and port numbers together
        for port,is_open in zip(ports,results):
            if is_open:
                print(f'> {host}:{port} open')

# protect the entry point
if __name__ == '__main__':
    # define host and port numbers to scan
    host = 'python.org'
    ports = range(1024)
    # test the ports
    port_scan(host, ports)


# Import pylons modules
from pylons import request, response, session, tmpl_context as c, config
from pylons.controllers.util import abort, redirect_to
from pylons.decorators import jsonify
# Import system modules
import logging; log = logging.getLogger(__name__)
import shutil
# Import custom modules
from fp import model
from fp.model import meta
from fp.lib import helpers as h, folder_store
from fp.lib.base import BaseController, render

class WindowController(BaseController):

    def index(self):
        c.regions = meta.Session.query(model.Region).filter_by(is_complete=True).all()
        c.windows = meta.Session.query(model.Window).filter_by(is_complete=True).all()
        c.window_jobs = meta.Session.query(model.Job).filter_by(type=model.job_sampleWindows)
        return render('/windows/index.mako')

    def add(self, url=None):
        """
        Add a job to define windows
        """
        # If the user is not logged in, return
        if not h.isPerson():
            return 'Please log in to perform this action.'
        # Load parameters from request.POST
        parameterByName = {
            'example count per region': int(request.POST['exampleCountPerRegion']),
            'multispectral pixel shift value': int(request.POST['multispectralPixelShiftValue']),
            'shift count': int(request.POST['shiftCount']),
        }
        # Create a job in the database with type job_sampleWindows
        job = model.Job(model.job_sampleWindows, session['personID'])
        job.setInput(dict(
            windowName=request.POST['windowName'],
            regionID=request.POST['regionID'],
            parameterByName=parameterByName,
        ))
        meta.Session.add(job)
        meta.Session.commit()
        # Redirect
        redirect_to(h.decodeURL(url) or url_for('window_index'))

    @jsonify
    def delete(self):
        # If the person is not logged in, return
        if not h.isPerson():
            return dict(isOk=0, message='You must be logged in to perform this action.')
        # !!! Assert ownership
        # Load
        windowID = request.POST.get('windowID')
        # Delete from database
        meta.Session.execute(model.windows_table.delete().where(model.Window.id==windowID))
        # Delete from file system
        shutil.rmtree(folder_store.WebStore(config['storage_path']).getWindowPath(windowID))
        # Commit
        meta.Session.commit()
        # Return
        return dict(isOk=1)


def read_stations(station_filename):
    lines = open(station_filename).read().splitlines()
    table = [x.split(",") for x in lines if x != ""]
    station_fees = {}
    station_indexes = {}
    station_fees = {}
    for i in range(len(table)):
        temp_station_fees = []
        for j in range(1, len(table[0])):
            temp_station_fees.append(int(table[i][j]))
        station_fees[table[i][0]] = temp_station_fees
        station_indexes[table[i][0]] = i
    return station_fees, station_indexes

def read_seats(seat_filename):
    lines = open(seat_filename).read().splitlines()
    seats = {}
    seat_classes = []
    for x in lines:
        seats[x] = []
        temp_seat_class = int(x[0])
        if not (temp_seat_class in seat_classes):
            seat_classes.append(temp_seat_class)
    return seats, seat_classes

def read_reserved_tickets(ticket_filename, seats):
    lines = open(ticket_filename).read().splitlines()
    table = [x.split(",") for x in lines if x != ""]
    for row in table:
        seat_num = row[0]
        pax = {'origin': row[1], 'dest': row[2]}
        seats[seat_num].append(pax)

def read_origin():
    while True:
        origin = int(input(f"Enter origin (0-{len(station_indexes) - 1}): "))
        if 0 <= origin < len(station_indexes):
            return origin
        print("Invalid origin station index.")

def read_dest(origin_index):
    while True:
        dest = int(
            input(f"Enter destination (0-{len(station_indexes) - 1}): "))
        if origin_index < dest < len(station_indexes):
            return dest
        print("Invalid destination station index.")

def get_station_index(station_indexes, _station_name):
    return station_indexes[_station_name]

def get_station_name(station_indexes, _station_index):
    for key, value in station_indexes.items():
        if _station_index == value:
            return key

def show_seats(seats, station_indexes):
    for i in seats:
        text = f"{i}:"
        for j in seats[i]:
            origin = j['origin']
            origin_index = station_indexes[origin]
            dest = j['dest']
            dest_index = station_indexes[dest]
            text += f" [{origin}({origin_index})-{dest}({dest_index})],"
        print(text)

def check_routes_available(station_indexes, ticket_list,
                           origin_index, dest_index):
    for j in ticket_list:
        routes = range(station_indexes[j['origin']],
                       station_indexes[j['dest']] + 1)
        reserved = range(origin_index, dest_index + 1)
        if list(set(routes) & set(reserved)):
            return False
    return True

def choose_available_seat(available_seats):
    while True:
        seat = input("Select seat: ")
        if seat in available_seats:
            return seat
        print("Invalid seats.")
        print(f"Available seats: {available_seats}")

def update_seat(station_indexes, origin_index, dest_index, ticket_list):
    ticket_list.append(
        {'origin': get_station_name(station_indexes, origin_index),
         'dest': get_station_name(station_indexes, dest_index)})
    return ticket_list

def get_ticket_price(station_indexes, station_fees, origin_index, dest_index,
                     seat_class):
    seat_class -= 1
    origin = get_station_name(station_indexes, origin_index)
    dest = get_station_name(station_indexes, dest_index)
    return station_fees[dest][seat_class] - station_fees[origin][seat_class]

def reserve(train_seats, train_seat_classes, station_indexes, station_fees):
    origin_index = read_origin()
    dest_index = read_dest(origin_index)

    available = [i for i in train_seats.keys() if check_routes_available(
        station_indexes, train_seats[i], origin_index, dest_index) is True]

    if not available:
        print("Sorry. No available seat.")

    else:
        print(f"Available seats: {available}")
        price = [get_ticket_price(station_indexes, station_fees, origin_index,
                                  dest_index, i)
                 for i in train_seat_classes]
        [print(f"Class {i} Ticket price = {price[i - 1]}") for i in
         train_seat_classes]
        seat = choose_available_seat(available)
        print(f"The selected seat = {seat}")
        print(f"The ticket price = {price[int(seat[0]) - 1]}")
        train_seats[seat] = update_seat(station_indexes, origin_index,
                                        dest_index, train_seats[seat])

def read_canceled_seat(train_seats):
    while True:
        seat = input("Enter seat to cancel: ")
        if seat in train_seats.keys():
            return seat
        print("Invalid seat.")

def remove_ticket(train_seats, station_indexes, seat_str, origin_index,
                  dest_index):
    """ Function remove_ticket does the followings:
        1. get the ticket list of specific seat
        2. check whether ticket between origin and dest station indexes exists \
            inside the ticket list from 1.
        3. if ticket between origin and dest station indexes exists, \
            remove such ticket from the ticket list and return True
        4. if ticket between origin and dest station indexes does not exist, \
            return False

        :param train_seats: dictionary of seats
        :param station_indexes: dictionary of station indexes
        :param seat_str: string
        :param origin_index: int
        :param dest_index: int
        :return: boolean value
    """
    for j in train_seats[seat_str]:
        if station_indexes[j['origin']] == origin_index and \
                station_indexes[j['dest']] == dest_index:
            train_seats[seat_str].remove(j)
            return True
    return False

def cancel(train_seats, station_indexes):
    """ Function cancel does the followings:
        1. read the canceled seat from user
        2. read the origin and destination station index of canceled ticket
        3. display tickets reserved under the canceled seat
        3. if ticket removal is successful,  \
            display updated tickets under the canceled seat
        4.  if ticket removal is not successful (or ticket does not exist \
            in the ticket list, report to user

        :param train_seats: dictionary of seats
        :param station_indexes: dictionary of station indexes
        :return: nothing
    """
    print(f"Seats are {[i for i in train_seats.keys()]}")
    seat_str = read_canceled_seat(train_seats)
    origin_index = read_origin()
    dest_index = read_dest(origin_index)

    text = f"Tickets issued at {seat_str}:"
    for j in train_seats[seat_str]:
        origin = j['origin']
        origin_number = station_indexes[origin]
        dest = j['dest']
        dest_number = station_indexes[dest]
        text += f" [{origin}({origin_number})-{dest}({dest_number})],"
    print(text)

    if remove_ticket(train_seats, station_indexes, seat_str,
                     origin_index, dest_index):
        print("After cancellation:")
        text = f"Tickets issued at {seat_str}:"
        for j in train_seats[seat_str]:
            origin = j['origin']
            origin_number = station_indexes[origin]
            dest = j['dest']
            dest_number = station_indexes[dest]
            text += f" [{origin}({origin_number})-{dest}({dest_number})],"
        print(text)

    else:
        print(f"Ticket does not exist at {seat_str}")

def show_ticket_prices(train_seats, station_indexes, station_fees):
    """ Display reserved tickets from all seats, along with TICKET PRICE
        Use information of ticket list inside each seat from dictionary of
            seats, dictionary of station indexes, and diction of ticket fees
            to help display.

        :param train_seats: dictionary of seats
        :param station_indexes: dictionary of station indexes
        :param station_fees: dictionary of ticket fees
        :return: nothing
    """
    for i in train_seats:
        text = f"{i}:"
        for j in train_seats[i]:
            origin = j['origin']
            origin_index = station_indexes[origin]
            dest = j['dest']
            dest_index = station_indexes[dest]
            price = get_ticket_price(station_indexes, station_fees,
                                     origin_index, dest_index, int(i[0]))
            text += f" [{origin}({origin_index})-{dest}({dest_index})" \
                    f"-{price}],"
        print(text)

def clear_tickets(train_seats):
    """ Set ticket list of each seat to be empty list

        :param train_seats: dictionary of seats
        :return: nothing
    """
    print("After clearing all tickets")
    for i in train_seats.keys():
        train_seats[i].clear()
    show_seats(train_seats, station_indexes)

station_fees, station_indexes = read_stations('south_stations.txt')
# print(station_fees)        # you can uncomment this line to see output
# print(station_indexes)     # you can uncomment this line to see output
TOTAL_NUM_STATIONS = len(station_indexes)
# print(TOTAL_NUM_STATIONS)  # you can uncomment this line to see output

train_seats, train_seat_classes = read_seats('south_train_seats.txt')
# print(train_seats)         # you can uncomment this line to see output
# print(train_seat_classes)  # you can uncomment this line to see output

read_reserved_tickets('south_reserved_tickets.txt', train_seats)
# print(train_seats)  # you can uncomment this line to see output

while True:
    print()
    print('1. Show seats')
    print('2. Reserve ticket')
    print('3. Cancel ticket')
    print('4. Show ticket prices')
    print('5. Clear all tickets')
    print('6. Exit')
    choice = int(input('Enter your choice: '))
    if choice == 1:
        show_seats(train_seats, station_indexes)
    elif choice == 2:
        reserve(train_seats, train_seat_classes, station_indexes, station_fees)
    elif choice == 3:
        cancel(train_seats, station_indexes)
    elif choice == 4:
        show_ticket_prices(train_seats, station_indexes, station_fees)
    elif choice == 5:
        clear_tickets(train_seats)
    elif choice == 6:
        break
    else:
        print("Invalid choice. Choose again.")

if __name__ == '__main__':
    import doctest

    doctest.testmod()


"""Utilities used by other modules in cis-bidsify."""
import os
import sys
import shutil
import tarfile
import subprocess
from pathlib import Path

import pydicom

def run(command, env=None):
    """Run a command with specific environment information.

    Parameters
    ----------
    command: command to be sent to system
    env: parameters to be added to environment
    """
    merged_env = os.environ
    if env:
        merged_env.update(env)

    process = subprocess.Popen(
        command,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        shell=True,
        env=merged_env,
    )
    while True:
        line = process.stdout.readline()
        line = line.decode("utf-8")
        sys.stdout.write(line)
        sys.stdout.flush()
        if line == "" and process.poll() is not None:
            break

    if process.returncode != 0:
        raise Exception(
            f"Non zero return code: {process.returncode}\n"
            f"{command}\n\n{process.stdout.read()}"
        )
    return process.returncode

def load_dicomdir_metadata(dicomdir):
    """Grab data from dicom directory of a given type (tar, tar.gz, directory).

    Parameters
    ----------
    dicomdir: Directory containing dicoms for processing

    Returns
    -------
    data : dicom header
        DICOM information from first dicom in directory.
    """
    if dicomdir.is_file() and dicomdir.suffix in (".gz", ".tar"):
        open_type = "r"
        if dicomdir.suffix == ".gz":
            open_type = "r:gz"
        with tarfile.open(dicomdir, open_type) as tar:
            dicoms = [mem for mem in tar.getmembers() if mem.name.endswith(".dcm")]
            f_obj = tar.extractfile(dicoms[0])
            data = pydicom.read_file(f_obj)
    elif dicomdir.is_dir():
        dcm_files = list(Path(dicomdir).glob("**/*.dcm"))
        f_obj = dcm_files[0].as_posix()
        data = pydicom.read_file(f_obj)
    else:
        raise ValueError(
            "dicomdir is neither file nor directory, "
            "dicomdir must be a tarball (.tar), "
            "gzipped tarball (.tar.gz) or a folder structure containing dicoms (.dcm) files"
        )
    return data

def clean_tempdirs(output_dir, sub, ses):
    """Clean up working directories (.heudiconv and .tmp).

    If all work is complete, this will return the
    directory to BIDS standard (removing .heudiconv and .tmp directories).

    Parameters
    ----------
    output_dir: Path object of bids directory
    sub: Subject ID
    ses: Session ID, if required
    """
    for root in [".heudiconv"]:
        if ses:
            if root == ".heudiconv":
                print(
                    "Removing Temp Directory: ", output_dir / root / sub / f"ses-{ses}"
                )
                shutil.rmtree(output_dir / root / sub / f"ses-{ses}")
            else:
                print("Removing Temp Directory: ", output_dir / root / sub / ses)
                shutil.rmtree(output_dir / root / sub / ses)
        if (output_dir / root / sub).is_dir():
            print("Removing Temp Directory: ", output_dir / root / sub)
            shutil.rmtree(output_dir / root / sub)
        if (output_dir / root).is_dir():
            if not (output_dir / root).iterdir():
                print("Removing Temp Directory: ", output_dir / root)
                shutil.rmtree((output_dir / root))


import random, os

class TreasureHunter:
    jumlah_treasure = 0
    map_data = []
    posisi_user = []
    posisi_jalan = []
    posisi_treasure = []
    option = {
        1: 'Print Map',
        2: 'Print Map with Treasure',
        3: 'Print Treasure Position',
        4: 'Re-position of Treasure',
        5: 'Play Time!',
        0: 'Exit'
    }

    def __init__(self):
        f = open("treasure_map.txt", 'r')
        self.maps = f.read()
        self.map_data = []

        for corX in self.maps.split('\n'):
            mapped = []
            for corY in corX:
                mapped.append(corY)
            self.map_data.append(mapped)

    def load_game(self, jumlah_treasure=1):
        self.jumlah_treasure = jumlah_treasure
        self.load_coordinate()
        self.load_treasure(jumlah_treasure)
        self.pick_option()

    def pick_option(self):
        os.system('cls' if os.name == 'nt' else 'clear')
        for op in self.option.keys():
            print(str(op) + ' : ' + self.option[op])

        input_option = int(input('Option :'))

        if input_option == 1:
            self.print_maps(treasure=False)

        if input_option == 2:
            self.print_maps(treasure=True)

        if input_option == 3:
            print(self.posisi_treasure)

        if input_option == 4:
            self.input_treasure()

        if input_option == 5:
            self.play_game()

        if input_option == 0:
            exit()

        input('Press any key to return back')
        return self.pick_option()

    def play_game(self):
        self.print_maps(treasure=False, clear=False)
        self.print_maps(treasure=True, clear=False)
        print('Treasure position : '+ str(self.posisi_treasure))

        posX = self.posisi_user[0][0]
        posY = self.posisi_user[0][1]

        moving_operation = []
        moving_operation.append(['up/north','UP',False])
        moving_operation.append(['right/east','RIGHT',True])
        moving_operation.append(['down/south','DOWN',True])

        result = None

        for operation in moving_operation:
            result = self.set_updated_coordinate(posX, posY, operation[0], operation[1], operation[2])

            if result:
                posX = result[0]
                posY = result[1]
            else:
                break

        if result and result in self.posisi_treasure:
            print('Congratz, You just found the treasure ...')
        elif result:
            print('Nothing here, try again ...')
        else:
            print('Road is blocked...')

    def set_updated_coordinate(self, posX, posY, pos_text='up/north', pos='UP', ignore_wall=True):
        posCombine = [posX,posY]
        possible_count = '0-'+str(self.count_coordinate_possibily(posCombine,pos=pos,ignore_wall=ignore_wall))
        moving = input(pos_text+' ('+possible_count+'):')

        is_blocked = self.is_route_blocked(posCombine,int(moving),pos)

        if is_blocked:
            return None

        add = int(moving)

        if pos == 'UP':
            posX -= add
        elif pos == 'RIGHT':
            posY += add
        elif pos == 'DOWN':
            posX += add

        return [posX,posY]

    def is_route_blocked(self, coordinate, move, pos = 'UP'):
        posX = coordinate[0]
        posY = coordinate[1]

        for add in range(1,move+1):
            if pos == 'UP':
                posX = coordinate[0] - add
            elif pos == 'RIGHT':
                posY = coordinate[1] + add
            elif pos == 'DOWN':
                posX = coordinate[0] + add

            if posX < 0 or posX > len(self.map_data) or posY <0 or posY > len(self.map_data[0]):
                return True

            if self.map_data[posX][posY] == '#':
                return True
        return False

    def count_coordinate_possibily(self, coordinate, pos = 'UP', ignore_wall=True):
        found = 0
        move = 1
        posX = 0
        posY = 0

        while True:
            if pos == 'UP':
                posX = coordinate[0]-move
                posY = coordinate[1]
            if pos == 'RIGHT':
                posX = coordinate[0]
                posY = coordinate[1]+move
            if pos == 'DOWN':
                posX = coordinate[0]+move
                posY = coordinate[1]

            # if position is negative of more than length of array then break
            if posX < 0 or posY < 0 or posX >= len(self.map_data) or posY >= len(self.map_data[0]):
                if ignore_wall:
                    move -= 3
                break

            # if target position is not possible road then break
            if ignore_wall:
                if self.map_data[posX][posY] == '.' or self.map_data[posX][posY] == '$' or self.map_data[posX][posY] == '#':
                    found +=1
            else:
                if self.map_data[posX][posY] == '#':
                    break
                if self.map_data[posX][posY] == '.' or self.map_data[posX][posY] == '$':
                    found +=1
            move +=1

        return found

    '''
        print map
        treasure : show position of all treasure with $ symbol
    '''
    def print_maps(self, treasure=False, clear=True):
        if clear:
            os.system('cls' if os.name == 'nt' else 'clear')

        if treasure:
            print('>> Map With Treasure <<')
        else:
            print('>> Map <<')

        for rows in range(len(self.map_data)):
            output = ''
            for column in range(len(self.map_data[0])):
                if treasure and [rows,column] in self.posisi_treasure:
                    output += '$'
                else:
                    output += self.map_data[rows][column]
            print(output)

    '''
        load coordinate for avaiable road and position of user

        X = User Position
        . = Avaiable position

        if user position not found, we generated random user
        if user have more than 1 position, we will discard others
    '''
    def load_coordinate(self):
        for row in range(len(self.map_data)):
            for column in range(len(self.map_data[row])):
                if self.map_data[row][column] == 'X':
                    self.posisi_user.append([row,column])
                if self.map_data[row][column] == '.':
                    self.posisi_jalan.append([row,column])

        if len(self.posisi_user) == 0:
            posT = random.randint(0, len(self.posisi_jalan)-1)

            pX = self.posisi_jalan[posT][0]
            pY = self.posisi_jalan[posT][1]

            self.map_data[pX][pY] = 'X'
            self.posisi_user.append([pX,pY])

        # if user more than 1
        if len(self.posisi_user) > 1:
            temp_post = []
            temp_post.append(self.posisi_user[0])

            for pos in range(1,len(self.posisi_user)):
                pX = self.posisi_jalan[pos][0]
                pY = self.posisi_jalan[pos][1]

                self.map_data[pX][pY] = '.'

            self.posisi_user = temp_post

    '''
        Generate random treasure position by input

        num : number of treasure
    '''
    def load_treasure(self, num):
        # if num > possible road count, then num = length of possible road array
        if num > len(self.posisi_jalan):
            num = len(self.posisi_jalan)

        while not len(self.posisi_treasure) == num:
            # Generated random number
            posT = random.randint(0, len(self.posisi_jalan)-1)

            if not posT in self.posisi_treasure:
                self.posisi_treasure.append(self.posisi_jalan[posT])

    def input_treasure(self):
        treasure_count = input('Treasure Count (empty: default 1) :')
        self.posisi_treasure = []

        if not treasure_count == '':
            self.jumlah_treasure = int(treasure_count)

        self.load_treasure(self.jumlah_treasure)
        self.print_maps(treasure=True, clear=False)


from nltk.stem import WordNetLemmatizer

from nltk.corpus import wordnet as wn

from nltk.stem.lancaster import LancasterStemmer
from nltk.stem.porter import PorterStemmer
from nltk.stem import SnowballStemmer

from nlp.Line import Line
from nlp.string_utils import unshorten_line
from nlp.tagging import tag_line

class Lemmatiser:

    not_important_words = ['\'', '\'s', '$', '%', '&', '.']

    def __init__(self):
        self.wnl = WordNetLemmatizer()
        self.lancaster_stemmer = LancasterStemmer()
        self.porter_stemmer = PorterStemmer()
        self.snowball_stemmer = SnowballStemmer('english')

        wn.ensure_loaded()

    def contractions_filter(self, sentence: str):
        words = sentence.split()
        filtered_words = []
        for word in words:
            if word.lower() in self.contractions:
                filtered_words.extend(self.contractions[word.lower()])
                continue
            filtered_words.append(word)

        return ' '.join(filtered_words)

    def lemmatise_line(self, line: Line):
        line = tag_line(line)
        for word in line.words:
            word.lemm.append(word.original)

            if word.processed not in word.lemm:
                word.lemm.append(word.processed)

            precessed_lemm = self.wnl.lemmatize(word.processed, pos=word.get_wn_pos_tag())
            if precessed_lemm not in word.lemm:
                word.lemm.append(precessed_lemm)

            for tag in [wn.NOUN, wn.VERB, wn.ADJ, wn.ADV]:
                lemm = self.wnl.lemmatize(word.processed, pos=tag)
                if lemm not in word.lemm:
                    word.lemm.append(lemm)

            lancaster_stem = self.lancaster_stemmer.stem(word.processed)
            if precessed_lemm not in word.lemm:
                word.lemm.append(lancaster_stem)

            porter_stem = self.porter_stemmer.stem(word.processed)
            if precessed_lemm not in word.lemm:
                word.lemm.append(porter_stem)

            snowball_stem = self.snowball_stemmer.stem(word.processed)
            if precessed_lemm not in word.lemm:
                word.lemm.append(snowball_stem)

        return line


# coding: utf-8
from __future__ import absolute_import
from django.utils.timezone import now, get_current_timezone
from django.dispatch import receiver
from django.utils.translation import ugettext as _

from flier.models import Recipient, RecipientStatus
from flier.mails import (models, utils)
from flier.backends import BackendSignal

from celery import shared_task
# from celery.utils.log import get_task_logger

import traceback
from logging import getLogger

logger = getLogger('flier')

def make_eta(when=None):
    '''ETA(estimated time of arrival) time

        :param when:  :py:class:`datetime.datetime`
        :return: :py:class:`datetime.datetime` with timezone
    '''
    when = when or now()
    return when.tzinfo and when or get_current_timezone().localize(when)

def get_object(instance, model_class):
    mail = isinstance(instance, model_class) and \
        model_class.objects.get(id=instance.id) or \
        model_class.objects.get(id=instance)
    # return mail.instance        # return subclass instance if exists
    return mail

def get_return_path_and_to(sender, mail, recipient):

    if isinstance(recipient, basestring):
        to = models.Address.objects.get_or_create(
            address=recipient)[0]
        return_path = utils.to_return_path(
            'adhoc', sender.id, mail.id, to.id)

    elif isinstance(recipient, models.Recipient) and \
            recipient.mail == mail:
        to = recipient.to
        return_path = recipient.return_path

    return (return_path, to, )

@shared_task
def enqueue_mails():
    '''Enqueue mails '''
    mails = models.Mail.objects.queueing_set()
    for mail in mails:
        mail.enqueue()
        logger.info(u'{0} has been enqueued task_id={1}'.format(
            mail.id, mail.task_id))

    logger.info(
        u'enqueue_mails:{0} mails has been enqueued .'.format(mails.count()))

@shared_task
def send_mail(mail, withbreak=True):
    '''Send mail

    :param Mail mail:  :ref:`emailqueue.models.Mail` or id
    '''
    mail = get_object(mail, models.Mail)
    job = getattr(send_mail, 'request', None)
    logger.warn('send_mail(Mail:{})'.format(mail.id))

    if job:
        for cancel in mail.instance.mailcancel_set.filter(
                task_id=send_mail.request.id):
            cancel.cancel()
            logger.warn(u'{0} has been canceled'.format(job.id))
            return

    if mail.status == mail.STATUS_DISABLED:
        # Already completed
        logger.warn(u"{} sent_at:{} status:{} subject:{}".format(
            _("send_mail:This mail is not in sending queue."),
            mail.sent_at, mail.get_status_display(), mail.subject))
        return

    if mail.sent_at:
        # Already completed
        logger.warn(u"{0} sent_at:{1} status:{2} subject:{3}".format(
            _("send_mail:This message has been already processed"),
            mail.sent_at, mail.get_status_display(), mail.subject,))
        return

    # BEGIN:
    if mail.status == mail.STATUS_QUEUED:
        mail.provide()

    sender = mail.sender.instance               # Actual Sender
    recipients = mail.active_recipients()       # Mail Recipient list
    logger.warn(_("send_mail:Sending sender:{} recipeints:{}").format(
        sender.address, recipients.count()))

    for recipient in recipients:

        # INTERUPTED:
        if withbreak and mail.delay():    # make this Mail pending state
            logger.info(_("send_mail:Mail({0}) is delayed").format(mail.id))
            # enqueue the other task for sending this Mail
            if job:
                mail.enqueue()
            # terminate this task
            return

        # Get latest Mail.status
        mail.refresh_from_db()
        if mail.status != mail.STATUS_SENDING:
            logger.warn(
                _("send_mail:Mail<{}> has been interrupted status:{}").format(
                    mail.id, mail.get_status_display()))
            return

        # Send mail to each recipient
        msg = mail.instance.create_message(recipient)
        msg.send()

        # Wait
        sender.wait()

    # END: completed sending
    logger.warn(
        _("send_mail: completed for Mail({})").format(mail.id,))
    mail.complete()

@receiver(BackendSignal.sent_signal)
def on_sent(sender=None, from_email=None, to=None, message_id=None, key=None,
            status_code='sent', message='', **kwargs):

    recipient = Recipient.objects.filter(message_id=message_id).first()
    if not recipient:
        logger.error(u"Recpient for message-id({}) does not exists".format(
            message_id))
        return

    try:
        recipient.key = key
        recipient.sent_at = now()
        recipient.status = RecipientStatus.objects.get_status(status_code)
        recipient.message = message
        recipient.save()
    except:
        status_code = 'post send error'
        recipient.status = RecipientStatus.objects.get_status(status_code)
        recipient.sent_at = now()
        recipient.message = message + "\n" + traceback.format_exc()
        recipient.save()

@receiver(BackendSignal.failed_signal)
def on_failed(sender=None, from_email=None, to=None, message_id=None, key=None,
              status_code='sent', message='', **kwargs):
    Recipient.objects.filter(message_id=message_id).update(
        key=key,
        sent_at=now(),
        status=RecipientStatus.objects.get_status(status_code),
        message=message)


import itertools
def findsubsets(s, n):
 	return itertools.combinations(s,n)

f=0
s=list(map(int,input("enter no:-").split(' ')))
target=int(input("enter target:-"))
for i in range(2,len(s)+1):
	ans=list(findsubsets(s,i))
	for x in ans:
		if sum(x)==target:
			f+=1
			print("Yes!")
			break
	if f!=0:
		break
if f!=0:
	for x in s:
		if target%x==0:
			print("yes!")
			exit(0)
else:
	print("No!")


from flask import Flask, render_template , request
from flask_bootstrap import Bootstrap
import os;
import subprocess;

app = Flask(__name__)

# Make sure to install 'Flask-Bootstrap'
Bootstrap(app)

@app.route('/')
def index():
    with open('all_command.txt' , 'r') as f:
        content = [line.strip() for line in f]
    name = []
    command = []

    for item in content:
        spliter = item.split(',')
        name.append(spliter[0])
        command.append(spliter[1])
    output_value = zip(name,command)
    return render_template('index.html',output_value=output_value)

@app.route('/execute')
def execute():
    command = request.args.get('command')
    execute_command = command.split( )
    execution = subprocess.check_output(execute_command, shell=True)
    # return render_template('result.html',command=command,execution=execution)
    return render_template('result.html', command = command , execution = execution)

if __name__ == "__main__":
    app.run(debug=True,use_reloader=True)


lista = [10, 5, 8, 20, 50, 10, 5, 8, 8, 60, 10, 5, 5, 3, 50]
posicoesPares = []
maiorElemento = 0
somaPositivos = 0

i = 0
print("--------------------------")
while i < len(lista):

    if maiorElemento < lista[i]:
        maiorElemento = lista[i]
    if lista[i] > 0:
        somaPositivos += lista[i]
    if lista[i] % 2 == 0:
        posicoesPares.append(i)

    i += 1
print(f"=> Maior elemento: {maiorElemento}")
print("--------------------------")
print(f"=> Soma dos elementos positivos: {somaPositivos}")

i = 0
print("--------------------------")
print(f"=> Cinco primeiros elementos:")
while i < 5:
    print(lista[i])
    i += 1

print("--------------------------")
print(f"=> Posições dos números pares: {posicoesPares}")

Engenharia de Software/Algoritmos - Aula 12/Lista 12/ElementosLista.py


"""
@Time ： 2023/2/14 下午6:10
@Auth ： Sek Hiunam
@File ：model.py
@Desc ：
"""
import torch
import torch.nn as nn
from torch_geometric.nn.conv import GatedGraphConv
from torch_geometric.nn import GlobalAttention,AttentionalAggregation
from torch_geometric.nn import global_mean_pool
# from torch_geometric.utils import softmax
from torch.nn.functional import softmax

class GGNN(nn.Module):
    def __init__(self,out_channels,hidden,num_layers,device):
        super(GGNN, self).__init__()
        self.out_channels = out_channels
        self.num_layers = num_layers
        self.hidden = hidden

        self.ggnn = GatedGraphConv(out_channels=out_channels,num_layers=num_layers).to(device)
        self.soft_attention = AttentionalAggregation(gate_nn=nn.Linear(out_channels, 1), nn=None).to(device)
        self.ggnn_2 = GatedGraphConv(out_channels=out_channels,num_layers=num_layers).to(device)
        self.tanh = nn.Tanh().to(device)
        self.linear_1 = torch.nn.Linear(hidden, out_channels).to(device)

        # self.soft_attention = nn.Sequential(
        #     nn.Linear(self.out_channels, self.out_channels),
        #     nn.LeakyReLU(),
        #     nn.Linear(self.out_channels, 1),
        #     nn.Sigmoid()
        # )
        # self.out = nn.Sequential(
        #     nn.Linear(self.out_channels, self.out_channels),
        #     nn.LeakyReLU(),
        #     nn.Linear(self.out_channels, 1),
        #     nn.Tanh(),
        # )

        self.device = device

    def forward(self,data):
        # x,edge_index = x["x"],x["edge_index"]
        x, edge_index,batch = data.x.to(self.device), data.edge_index.to(self.device),data.batch.to(self.device)

        # x_ggnn = torch.nn.functional.pad(x, (0, self.hidden), "constant", 0) # x.shape[-1] == self.out_channel + self.hidden

        x_ggnn = self.ggnn(x,edge_index).to(self.device) # output: node features (|V|,out_channel)

        # Step 3: catenate the GGNN output with the original input
        # x = torch.cat((x_ggnn, x), -1)

        # Step 4: pass this through the attention layer : torch_geometric.nn.aggr.AttentionalAggregation
        output = self.soft_attention(x_ggnn, batch).to(self.device)

        # x_ggnn_2 = self.tanh(x_ggnn_2).to(self.device)
        x_ggnn_2 = self.tanh(self.linear_1(x_ggnn)).to(self.device)
        batch_sum = torch.zeros(batch.max()+1, x_ggnn_2.shape[1]).to(self.device)
        batch_sum.scatter_add_(0, batch.repeat(x_ggnn_2.shape[1], 1).t(), x_ggnn_2)
        output_2 = batch_sum

        output = output * output_2
        output = self.tanh(output).to(self.device)
        # print(output.shape) # [batch_size,output_dim]

        # shall we add a tanh layer?

        return output


import argparse
import os
import subprocess

def run(args):
    top = args.dir
    if top is None:
        top = '.'
        print('Using current directory')

    for folder in os.listdir(top):
        if not folder.startswith(args.folder_prefix):
            continue

        for file in os.listdir(os.path.join(top, folder)):
            # let's grab the .gdb file
            if not file.endswith('.gdb'):
                continue
            subprocess.run(["ogr2ogr", os.path.join(top, folder, 'shape'), os.path.join(top, folder, file)])

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--dir',
                        type=str,
                        help='Directory to run in',
                        default=None
                        )
    parser.add_argument('--folder-prefix',
                        type=str,
                        help='Folder prefix for folders to convert files in',
                        required=True,
                        )
    args = parser.parse_args()
    run(args)