import discord
import os
import pyscreenshot as ImageGrab
import pyautogui
import time
from PIL import Image
from datetime import datetime

def findScreen():
    #used to find the emulator's GUI and create an offset for screencapping / clicking methods
    #start = pyautogui.locateCenterOnScreen('C:\\Users\\aritter\\Pictures\\upper screen.png')
    #print(start)
    #topFind = pyautogui.locateOnScreen('C:\\Users\\aritter\\Pictures\\upper screen.png', grayscale=True, confidence=.5)
    #botFind = pyautogui.locateOnScreen('C:\\Users\\aritter\\Pictures\\lower screen.png', confidence=.5)
    #print(topFind)
    #print(botFind)
    print("looking for screen")
    fileFind = pyautogui.locateOnScreen('D:\\EO\\yggdroid\\assets\\citrafileanchor.png', grayscale=False, confidence=.7)
    print(fileFind)
    xCitra, yCitra = pyautogui.locateCenterOnScreen('D:\\EO\\yggdroid\\assets\\fruitanchor.png')
    pyautogui.moveTo(xCitra, yCitra)
    #pyautogui.click(x, y)
    #print(topFind(top))
    #pyautogui.moveTo(fileFind)#Moves the mouse to the coordinates of the image
    #fileFind.show()
    return (xCitra, yCitra)

def topScreen():
    #grabs the top 3DS screen
    #TODO: dynamic variables
    citraLoc = findScreen()
    #topScreen = ImageGrab.grab(bbox=((citraLoc[0]-3), (citraLoc[1]+41), (citraLoc[0]+565), (citraLoc[1]+378)))
    topScreen = ImageGrab.grab(bbox=(0, 208, 1066, 847))
    return topScreen

def bottomScreen():
    #grabs the bottom 3DS screen
    #TODO: dynamic variables
    citraLoc = findScreen()
    #bottomScreen = ImageGrab.grab(bbox=((citraLoc[0]+59), (citraLoc[1]+379), (citraLoc[0]+509), (citraLoc[1]+716)))
    bottomScreen = ImageGrab.grab(bbox=(1067, 208, 1919, 847))
    return bottomScreen

def bothScreen():
    #grabs both screens, stitches then stitches them together
    topSc = topScreen()
    botSc = bottomScreen()
    topWd, topHt = topSc.size
    botWd, botHt = botSc.size
    #print(width)
    #print(height)
    bothScreen = Image.new('RGB', (topWd,(topHt+botHt)))
    bothScreen.paste(topSc)
    botOffset = (topWd/2) - (botWd/2)
    placementWBot = (1597-1119)/2
    bothScreen.paste(botSc, (round(botOffset), (topHt)))
    #bothScreen.paste(botSc, (x_offset, topWd/2), (y_offset, topHt))
    #bothScreen.show()
    return bothScreen

def makeFile():
    inputsList = ["up", "down", "left", "right", "a", "b", "x", "y", "start", "tapscreen", "dragscreen", "showTop", "showBottom", "showBoth", "write", "confirm"]
    gamesList = ["EO4", "EOV", "EOX", "EOU", "EOU2"]
    counter = 1
    counter1 = 0
    #winner = vote
    print(os.getcwd())
    #filePath = (os.path.join(os.getcwd(), "buttonLog" + winner + ".xlsx")
    filePath = (os.path.join(os.getcwd(), "buttonUserLog.xlsx"))
    if not os.path.exists('D:\\EO\\yggdroid\\buttonUserLog.xlsx'):
        workbook = xlsxwriter.Workbook(filePath)
        for game in gamesList:
            worksheet = workbook.add_worksheet(game)
            #counter1+=1
            for input in inputsList:
                worksheet.write(0, counter, (inputsList[(counter-1)]))
                print("Putting " + str(inputsList[(counter-1)]) + " in coloumn " + str(counter))
                counter+=1
            counter=1
        workbook.close()

class MyClient(discord.Client):
    async def on_ready(self):
        print('Logged in as')
        print(self.user.name)
        print(self.user.id)
        print('------')
    async def on_message(self, message):
        print(str(message.channel))
        #inputsList = ["up", "up_arrow", "down", "down_arrow", "left", "left_arrow", "right", "right_arrow", "a", "a_button", "b", "b_button", "x", "x_button", "y", "y_button", "start", "start_button", "tapscreen", "click", "dragscreen", "click"]
        #buttonPresses = ["up", "up_arrow", "down", "down_arrow", "left", "left_arrow", "right", "right_arrow", "a", "a_button", "b", "b_button", "x", "x_button", "y", "y_button", "start", "start_button", "tapscreen", "click", "dragscreen", "click"]
        inputsList = ["up", "down", "left", "right", "a", "b", "x", "y", "start", "select", "rb", "lb", "tapscreen", "dragscreen", "showtop", "showbottom", "showboth", "findscreen", "write", "confirm", "backspace"]
        #               0       1       2       3     4    5    6    7        8         9    10    11          12            13         14            15          16            17
        #buttonPresses = ["up_arrow","down_arrow","left_arrow","right_arrow","a_button","b_button","x_button","y_button","start_button","click","click"]
        buttonPresses = ["T", "G", "F", "H", "A", "S", "Z", "X", "M", "N", "W", "Q"]
        buttonLocations = ["1", "1130", "430","2", "1190", "430","3", "1260", "430","4", "1330", "430","5", "1390", "430","6", "1460", "430","7", "1530", "430","8", "1590", "430","9", "1660", "430","0", "1730", "430","-", "1790", "430","~", "1860", "430","q", "1160", "510","w", "1230", "510","e", "1290", "510","r", "1360", "510","t", "1430", "510","y", "1490", "510","u", "1560", "510","i", "1630", "510","o", "1690", "510","p", "1760", "510","a", "1130", "560","s", "1190", "560","d", "1260", "560","f", "1330", "560","g", "1390", "560","h", "1460", "560","j", "1530", "560","k", "1590", "560","l", "1660", "560",";", "1730", "560",":", "1790", "560","z", "1230", "630","x", "1290", "630","c", "1360", "630","v", "1430", "630","b", "1490", "630","n", "1560", "630","m", "1630", "630",",", "1690", "630",".", "1760", "630","[", "1260", "690","]", "1330", "690"," ", "1560", "690","!", "1790", "690","?", "1860", "690", "confirm", "1490", "790", "backspace", "1860", "510"]
        if message.author.id == self.user.id:
            return
        else:
            print("going into if")
            if str(message.channel) == "deep_city" or "report-channel-test":
                if message.content.casefold().startswith('~!setrole'):
                    requestedRole = message.content.casefold().split('~!setrole')[1:][0].lstrip()
                    selectedRole = list(filter(lambda role: role.name.casefold() == requestedRole.casefold(), message.guild.roles))[0]
                    print(selectedRole)
                    try:
                        await message.author.add_roles(selectedRole)
                        await message.channel.send('Hi ' + str(message.author.name) + '! I have added the role ' + str(selectedRole) + ' for you. '.format(message))
                    except Exception as e:
                        await message.channel.send('Sorry ' + str(message.author.name) + '! I wasn\'t able to set your role. Please reach out to the mod team so they can fix this.'.format(message))
                elif message.content.casefold().startswith('~!removerole'):
                    requestedRole = message.content.casefold().split('~!removerole')[1:][0].lstrip()
                    selectedRole = list(filter(lambda role: role.name.casefold() == requestedRole.casefold(), message.guild.roles))[0]
                    print(selectedRole)
                    try:
                        await message.author.remove_roles(selectedRole)
                        await message.channel.send('Hi ' + str(message.author.name) + '! I have removed the role ' + str(selectedRole) + ' for you. '.format(message))
                    except Exception as e:
                        await message.channel.send('Sorry ' + str(message.author.name) + '! I wasn\'t able to remove your role. Please reach out to the mod team so they can fix this.'.format(message))
                elif message.content.casefold().startswith('~!listroles'):
                    roleList = message.guild.roles
                    listFile = open("newFile.txt", 'w+')
                    for i in list(reversed(roleList)):
                        listFile.write(str(i) + '\n')
                    listFile = open("newFile.txt", "r")
                    toWrite = listFile.read()
                    toWrite = toWrite.split("beaned")[1]
                    listFile.close()
                    listFile = open("newFile.txt", "r")
                    sendFile = open("sendFile.txt", "w+")
                    toWrite = listFile.read()
                    toWrite = str(toWrite.split("beaned")[1])
                    toWrite = toWrite.replace("@everyone", "")
                    sendFile.write(toWrite)
                    listFile.close()
                    sendFile.close()
                    with open("sendFile.txt", "r") as sendFile:
                        try:
                            await message.channel.send(sendFile.read())
                        except Exception as e:
                            await message.channel.send('Sorry ' + str(message.author.name) + '! I wasn\'t able to list the roles. Please reach out to the mod team so they can fix this.'.format(message))
                    os.remove("sendFile.txt")
                    os.remove("newFile.txt")
            if (((str(message.channel.id)) == "685369497095503876") or ((str(message.channel.name)) == "yggtest")):
                    print(str(message))
                    print("found channel")
                    #if str(message).lower().strip().includes("") list.index(element)
                    command = str(message.content).lower().split()[0]
                    try:
                        commandAttr = str(message.content).split()[1:]
                        print("commandAttr is " + str(commandAttr))
                    except:
                        print("no attributes")
                    print("found command: " + command)
                    if any(usersInput in command for usersInput in inputsList):
                        print ("input in inputs list")
                        if command == ("buttons"):
                            buttonsShow = "```"
                            for each in inputsList[:12]:
                                buttonsShow = buttonsShow + each + ":" + buttonPresses[inputsList.index(each)] + "\n"
                            buttonsShow = buttonsShow + "```"
                            await message.channel.send(buttonsShow.format(message))
                        if inputsList.index(command) < 12:
                            print("input below 14")
                            #pyautogui.press(buttonPresses[inputsList.index(command)])
                            pyautogui.keyDown(buttonPresses[inputsList.index(command)])
                            time.sleep(.2)
                            print('up')
                            pyautogui.keyUp(buttonPresses[inputsList.index(command)])
                            print("input pressed was: " + buttonPresses[inputsList.index(command)])
                        else:
                            print(command + "not in inputsList")
                        if command.startswith("show"):
                            if command == ("showtop"):
                                showImage = topScreen()
                            elif command == ("showbottom"):
                                showImage = bottomScreen()
                            elif command == ("showboth"):
                                showImage = bothScreen()
                            now = datetime.now()
                            saveName = now.strftime("%d%m%Y%H%M%S") + ".png"
                            showImage.save(saveName)
                            await message.channel.send(file=discord.File(saveName))
                        #elif command.startswith("click"):
                        #    pyautogui.moveTo(100, 200, 2)
                        #    pyautogui.click()
                        #elif command.startswith("drag"):
                        #    pyautogui.moveTo(100, 200, 2)
                        #    pyautogui.dragTo(100, 200, button='left')
                        #elif command == ("findscreen"):
                        #    findScreen()
                        else:
                            print("couldn't find in second block")

                        '''elif not str(message.channel) == "deep_city":
                            if str(message.author.id) == "an":
                                print("in if 1")
                                if len(message.attachments) > 0:
                                    print("in if 2")
                                    await message.channel.send('<:eN:eID>')
                                    print("in after await")'''
                        print("check startWrite")
                        if command.startswith("write"):
                            print("trying split")
                            #command = commandAttr.split()
                            '''try:
                                command = command.split("write ")[1]
                                print("split space")
                            except:
                                try:
                                    print(command)
                                    command = command.split("write")[1]
                                    print("split no space")
                                    print(command)
                                except:
                                    print("couldn't split write")'''
                            print("check len")
                            if len(commandAttr) < 8:
                                print("len good, " + str(len(commandAttr)))
                                print(commandAttr)
                                print("going into char")
                                #newList =
                                for item in commandAttr:
                                    for char in item:
                                        print(char)
                                        if char.isupper():
                                            #pyautogui.moveTo(buttonLocations.index(command)+1, buttonLocations.index(command)+2)
                                            #[inputsList.index(each))
                                            #print("char is upper")
                                            whereis = str(buttonLocations.index(str(char.lower())))
                                            xLoc = (buttonLocations[int(whereis)+1])
                                            yLoc = (buttonLocations[int(whereis)+2])
                                            #print("char location: x" + ((buttonLocations[int(whereis)+1]) + " y" + (buttonLocations[int(whereis)+2])))
                                            print("Pressing SHIFT to UPPER")
                                            pyautogui.moveTo(x=1160, y=690)
                                            #pyautogui.click(clicks=2)
                                            pyautogui.mouseDown(); pyautogui.mouseUp()
                                            time.sleep(.3)
                                            print("Pressed SHIFT")
                                            #print("In Upper")
                                            #print("Char: " + char + "\nxLoc: " + xLoc + "\nyLoc: " + yLoc)
                                            pyautogui.moveTo(x=int(xLoc), y=int(yLoc))
                                            #pyautogui.click()
                                            pyautogui.mouseDown(); pyautogui.mouseUp()
                                            #pyautogui.mouseUp()
                                            time.sleep(.3)
                                            print("Pressing SHIFT to capitalize")
                                            '''pyautogui.moveTo(x=1160, y=690)
                                            #pyautogui.click(clicks=2)
                                            pyautogui.mouseDown(); pyautogui.mouseUp()
                                            #pyautogui.mouseUp()
                                            time.sleep(.5)
                                            print("Pressed SHIFT to LOWER")'''
                                        elif char.islower():
                                            #print("char is lower")
                                            whereis = str(buttonLocations.index(str(char.lower())))
                                            xLoc = (buttonLocations[int(whereis)+1])
                                            yLoc = (buttonLocations[int(whereis)+2])
                                            #print("In Lower")
                                            #print("Char: " + char + "\nxLoc: " + xLoc + "\nyLoc: " + yLoc)
                                            pyautogui.moveTo(x=int(xLoc), y=int(yLoc))
                                            pyautogui.mouseDown()
                                            pyautogui.mouseUp()
                                        else:
                                            #print("char is symbol")
                                            whereis = str(buttonLocations.index(str(char.lower())))
                                            xLoc = (buttonLocations[int(whereis)+1])
                                            yLoc = (buttonLocations[int(whereis)+2])
                                            #print("Symbol")
                                            pyautogui.moveTo(x=int(xLoc), y=int(yLoc))
                                            pyautogui.mouseDown()
                                            pyautogui.mouseUp()
                                        time.sleep(.1)
                                        #print("MoveTo")
                                        #print("Char: " + char + "\nxLoc: " + xLoc + "\nyLoc: " + yLoc)
                                        #print(command)
                            else:
                                print("Max name length is 8")
                        else:
                            print("didn't begin with write")
                        if command == "confirm":
                            whereis = str(buttonLocations.index("confirm"))
                            xLoc = (buttonLocations[int(whereis)+1])
                            yLoc = (buttonLocations[int(whereis)+2])
                            pyautogui.moveTo(x=int(xLoc), y=int(yLoc))
                            pyautogui.mouseDown()
                            pyautogui.mouseUp()
                        if command == "backspace":
                            whereis = str(buttonLocations.index("backspace"))
                            xLoc = (buttonLocations[int(whereis)+1])
                            yLoc = (buttonLocations[int(whereis)+2])
                            pyautogui.moveTo(x=int(xLoc), y=int(yLoc))
                            pyautogui.mouseDown()
                            pyautogui.mouseUp()
            print("no matches found")
            #print(str(message.channel.id))
client = MyClient()
client.run('NTMxNjE4NjEwMzQzNDQ0NTEy.XmIdBA.B5v1GO2g0mmvoAg-pK_P1z08xRM')

#    if inputsList.index(command) < 14:
#        print("yes")
#    elif inputsList.index(command) >= 14:
#        print("maybe")

#selectedRole is the value we want
#list
#filtering
#lambda role doesn't equal anything, declaring name of argument
#current item in the list turns into the current item in the list


from sessions.models import Session, SessionTimeError
from test_helpers.test_base import BaseTestCase

#________________________________________________________________
#
# Unit Tests
#________________________________________________________________

class SessionsModelTests(BaseTestCase):
    def test_session_name(self):
        session = Session.create(name="Name")
        self.assertTrue(session.get_name() == "Name")

    def test_get_start_time(self):
        session = Session.create(name="Name")
        self.assertTrue(session.get_start_time() == 0, "Session should init with a session time of 0")
        session.start_time = 15.0
        self.assertTrue(session.get_start_time() == 15.0, "Session should get a session time of 15.0 once the parameter is changed")

    def test_set_start_time(self):
        session = Session.create(name="Name")
        session.set_start_time(10.0)
        self.assertTrue(session.get_start_time() == 10.0, "Session should be able to set a session time")

    def test_get_time_elapsed(self):
        session = Session.create(name="Name")
        session.start_time = 15.0
        reading_time = 20.0
        result = session.get_time_elapsed(reading_time)
        self.assertTrue(result == 5.0, "Should be able to get a difference in start time and time elapsed values")

        reading_time = 10.0
        self.assertRaises(SessionTimeError, session.get_time_elapsed, reading_time)

    def test_get_reading_time(self):
        session = Session.create(name="Name")
        session.start_time = 15.0
        time_elapsed = 10.0
        result = session.get_reading_time(time_elapsed)
        self.assertTrue(result == 25.0, "Should add together the start time and time elapsed values")

#________________________________________________________________
#
# Builders to use in Unit Tests
#________________________________________________________________

def build_session_model(base_name):
    session = Session.create(name="%sSessionModel" % base_name)
    return session


#!/usr/bin/env python

__author__ = "frankojis"

from flask import render_template, Blueprint, request, jsonify
import time
import threading
from datetime import datetime, timedelta

from core import utils
from core.fuel.facebook import CREDENTIALS_FILE, APP, validator, scraper, posts as FBPosts
from config import LISTENER_INI
from core.exhaust import posts, mongo

listener = Blueprint('listener', __name__, url_prefix='/listener', static_folder='static', template_folder='templates')

@listener.route("/")
def do_checks():
    status = utils.check_config(CREDENTIALS_FILE, APP)
    return render_template("precheck.html", modal=not status)

@listener.route("/update_credentials", methods=['POST'])
def update_credentials():
    access_token = request.form['token']
    page_id = request.form['id']
    validity = validator.validate_token(access_token, page_id)
    return validity

@listener.route("/db_connection/<action>", methods=['POST'])
def db_connection(action):
    from core.exhaust import mongo

    Mongo = mongo.Mongo()
    action_status = 'Error'
    if action == "clear":
        action_status = Mongo.delete_collections()
    elif action == "retrieve":
        action_status = Mongo.get_lastScrapeTime(APP)
        if action_status is None:
            action_status = Mongo.delete_collections()
    time.sleep(1)
    return action_status

@listener.route("/kickstart", methods=['POST'])
def environment_kicker():
    from core.engine import kicker

    kicker.submit_spark()
    time.sleep(10)
    kicker.flume_invoker()
    return "started"

@listener.route("/start_scrape/<timestamp>", methods=['POST'])
def start_scraper(timestamp):
    fb_creds = utils.get_params(CREDENTIALS_FILE, APP)
    days_back = utils.get_params(LISTENER_INI, 'GraphAPI')['days_ago']
    if timestamp != '0':
        start_time = timestamp
    else:
        start_time = (datetime.now() - timedelta(int(days_back))).strftime('%s')
    try:
        fb_posts = scraper.Scraper(fb_creds['page_id'], start_time)
        scraper_thread = threading.Thread(target=fb_posts.scrape_data, name="listener", args=("posts", ))
        scraper_thread.setDaemon("True")
        scraper_thread.start()
        time.sleep(1)
        return "initiated"
    except Exception:
        return "failure"

@listener.route("/graphs")
def display_graphs():
    page_info = utils.get_params(CREDENTIALS_FILE, APP)
    return render_template("graphs.html", page=page_info)

@listener.route("/trendplot")
def generate_trends():
    trends_list = posts.get_postTrends()
    if trends_list is not None:
        return jsonify(trends=trends_list)
    else:
        return "Error"

@listener.route("/polarityplot")
def generate_polarities():
    polarities, polarities_count = posts.get_postPolarities()
    if polarities is not None:
        return jsonify(polarities=polarities, count=polarities_count)
    else:
        return "Error"

@listener.route("/varietyplot")
def generate_varieties():
    varieties_list = posts.get_postVarietiesPercent()
    if varieties_list is not None:
        return jsonify(varieties=varieties_list)
    else:
        return "Error"

@listener.route("/graphs/<int:postts>")
def display_intoposts(postts):
    datestring = utils.get_datestring(postts)
    postdata = posts.get_postdata(datestring)
    page_info = utils.get_params(CREDENTIALS_FILE, APP)
    return render_template("posts.html", page=page_info, post=postdata)

@listener.route("/graphs/<postid>")
def get_comments(postid):
    comments_data = posts.get_postComments(postid)
    if comments_data['comments_count'] != 0:
        if comments_data['sentiment_analysed'] == 0:
            mongo.Mongo().update_document("COMMENT_SENTIMENT_FLAG", key='post_id', value=postid, document={'flag': 0})
            push_status = FBPosts.Posts(None, None, 'posts').push_lazycomments(comments_data)
            if push_status == "success":
                c_flag = 0
                while c_flag != 1:
                    time.sleep(1)
                    c_flag = mongo.Mongo().get_flagStatus(postid)
                    if c_flag == 'Error':
                        return 'Error'
                comments_data = posts.get_postComments(postid)
            else:
                return "Error"
        if comments_data is not None:
            if comments_data['comments_count'] != '0':
                refined_comments_data = dict()
                refined_comments_data['count'] = comments_data['comments_count']
                comments = list()
                for comment in comments_data['comments_data']:
                    comment_data = dict()
                    comment_data['message'] = comment['message']
                    comment_data['likes'] = comment['like_count']
                    comment_data['sentiment'] = comment['sentiment']
                    comment_data['comment_ts'] = utils.get_jsunixtimestamp(comment['created_time'])
                    comments.append(comment_data)
                refined_comments_data['data'] = comments
                return jsonify(comments=refined_comments_data)
            else:
                return "zero"
        else:
            return "Error"
    else:
        return "zero"

@listener.route("/postreach/<postid>")
def get_uniquereach(postid):
    attr_dicttuple = posts.get_attrdata(postid)
    counters = dict()
    unique_likes, unique_comments = set(), set()
    for key, vtuple in attr_dicttuple.iteritems():
        counters[key] = vtuple[1]
        if key == 'likes' and vtuple[0] is not None:
            for data in vtuple[0]:
                unique_likes.add(data['id'])
        elif key == 'comments' and vtuple[0] is not None:
            for data in vtuple[0]:
                unique_comments.add(data['from']['id'])
    unique_reach = unique_likes.union(unique_comments)
    did_both = len(unique_likes.intersection(unique_comments))
    json_set = utils.jsonifier(unique_reach, 'store')
    mongo.Mongo().update_document("UNIQUE_REACH", key='post_id', value=postid, document={'user_ids': json_set})
    return jsonify(reach=(len(unique_reach), did_both, counters))


# Don't forget to change this file's name before submission.
import sys
import os
import enum
import socket
import threading

class Constants:
    RQ_LEN = 3

    MAX_RECEIVE = 4096

    DECODE = 'unicode_escape'

    DEF_PORT: int = 80

    class Index:
        MVP = 0  # Method Path Version
        PHH = 1  # Port Headers Host

        METHOD = 0
        PATH = 1
        VERSION = 2

    class Attributes:
        HTTP = 'http'
        HTTP_V0 = HTTP + '/1.0'
        HTTP_V1 = HTTP + '/1.1'
        HOST = 'Host'

    class Delimiters:
        RQ_ENDING = '\r\n\r\n'
        RQ_LINES_DELIMITER = '\r\n'
        PATH = '/'
        HOST = ':'
        SPACE = ' '

    class Operations:
        INVALID_INPUT = {}
        GOOD = {'get'}
        NOT_SUPPORTED = {'put', 'post', 'delete', 'head'}
        PLACEHOLDER = {}

    class Responses:
        BAD = (400, 'Bad Request')
        NOT_SUPPORTED = (501, 'Not Supported')

        class Index(int):
            CODE = 0
            MSG = 1

class Cache:
    """
    Represents a Cache object that caches request so it can
    be handled later on and no data loss occurs
    """

    def __init__(self):
        self.__cache = dict()

    def check(self, response):
        if response.requested_host is not None and response.requested_path is not None:
            url = response.requested_host + response.requested_path
            if url in self.__cache.keys():
                return self.__cache[url]
        return None

    def cache(self, response, r):
        if response.requested_host is not None and response.requested_path is not None:
            self.__cache[response.requested_host + response.requested_path] = r

class HttpRequestState(enum.Enum):
    """
    The values here have nothing to do with
    response values i.e. 400, 502, ..etc.
    Leave this as is, feel free to add yours.
    """
    INVALID_INPUT = 0
    NOT_SUPPORTED = 1
    GOOD = 2
    PLACEHOLDER = -1

class HttpRequestInfo(object):
    """
    Represents a HTTP request information

    Since you'll need to standardize all requests you get
    as specified by the document, after you parse the
    request from the TCP packet put the information you
    get in this object.

    To send the request to the remote server, call to_http_string
    on this object, convert that string to bytes then send it in
    the socket.

    client_address_info: address of the client;
    the client of the proxy, which sent the HTTP request.

    requested_host: the requested website, the remote website
    we want to visit.

    requested_port: port of the webserver we want to visit.

    requested_path: path of the requested resource, without
    including the website name.

    NOTE: you need to implement to_http_string() for this class.
    """

    def __init__(self, client_info, method: str, requested_host: str,
                 requested_port: int,
                 requested_path: str,
                 headers: list):
        self.method = method
        self.client_address_info = client_info
        self.requested_host = requested_host
        self.requested_port = requested_port
        self.requested_path = requested_path
        # Headers will be represented as a list of lists
        # for example ["Host", "www.google.com"]
        # if you get a header as:
        # "Host: www.google.com:80"
        # convert it to ["Host", "www.google.com"] note that the
        # port is removed (because it goes into the request_port variable)
        self.headers = headers

    def to_http_string(self):
        """
        Convert the HTTP request/response
        to a valid HTTP string.
        As the protocol specifies:

        [request_line]\r\n
        [header]\r\n
        [headers..]\r\n
        \r\n

        (just join the already existing fields by \r\n)

        You still need to convert this string
        to byte array before sending it to the socket,
        keeping it as a string in this stage is to ease
        debugging and testing.
        """
        http = f'{self.method} {self.requested_path} ' \
               + Constants.Attributes.HTTP_V0.upper() \
               + Constants.Delimiters.RQ_LINES_DELIMITER
        for h in self.headers:
            http += f'{h[0]}: {h[1]}' + Constants.Delimiters.RQ_LINES_DELIMITER
        return http + Constants.Delimiters.RQ_LINES_DELIMITER

    def to_byte_array(self, http_string):
        """
        Converts an HTTP string to a byte array.
        """
        return bytes(http_string, 'UTF-8')

    def display(self):
        print(f"Client:", self.client_address_info)
        print(f"Method:", self.method)
        print(f"Host:", self.requested_host)
        print(f"Port:", self.requested_port)
        stringified = [": ".join([k, v]) for (k, v) in self.headers]
        print("Headers:\n", "\n".join(stringified))

class HttpErrorResponse(object):
    """
    Represents a proxy-error-response.
    """

    def __init__(self, code, message):
        self.code = code
        self.message = message

    def to_http_string(self):
        return f'[{str.upper(self.message)}: {self.code}] '

    def to_byte_array(self, http_string):
        """
        Converts an HTTP string to a byte array.
        """
        return bytes(http_string, 'UTF-8')

    def display(self):
        print(self.to_http_string())

def entry_point(proxy_port_number):
    """
    Entry point, start your code here.

    Please don't delete this function,
    but feel free to modify the code
    inside it.
    """
    start(setup_sockets(proxy_port_number))

def setup_sockets(proxy_port_number):
    """
    Socket logic MUST NOT be written in the any
    class. Classes know nothing about the sockets.

    But feel free to add your own classes/functions.

    Feel free to delete this function.
    """
    print("Starting HTTP proxy on port:", proxy_port_number)

    # when calling socket.listen() pass a number
    # that's larger than 10 to avoid rejecting
    # connections automatically.

    proxy_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    proxy_socket.bind(('localhost', proxy_port_number))
    proxy_socket.listen(20)
    return proxy_socket

def start(proxy_socket: socket):
    cache = Cache()
    try:
        while True:
            client, address = proxy_socket.accept()
            threading.Thread(target=client_handler, args=(client, address, cache)).start()
    except KeyboardInterrupt:
        proxy_socket.close()

def client_handler(client: socket, address, c: Cache):
    proxy_response = http_request_pipeline(address, receive_client_request(client))
    if isinstance(proxy_response, HttpErrorResponse):
        client.sendto(proxy_response.to_byte_array(proxy_response.to_http_string()), address)
    else:
        response = c.check(proxy_response)
        if response is None:
            response = server_handler(proxy_response)
            c.cache(proxy_response, response)
        if response is not None:
            for proxy_response in response:
                client.sendto(proxy_response, address)
    client.close()

def receive_client_request(s: socket) -> str:
    request = ""
    while request.find(Constants.Delimiters.RQ_ENDING) == -1:
        request += http_request_decode(s.recv(Constants.MAX_RECEIVE))
    return request

def server_handler(response: HttpRequestInfo):
    server_socket = setup_server_socket(response)
    if server_socket is None:
        return
    return receive_server_response(server_socket, response)

def setup_server_socket(response: HttpRequestInfo) -> socket:
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        ip = socket.gethostbyname(response.requested_host)
    except TypeError or TimeoutError:
        return None
    server_socket.connect((ip, response.requested_port))
    return server_socket

def receive_server_response(s: socket, response: HttpRequestInfo) -> list:
    s.send(response.to_byte_array(response.to_http_string()))
    received_data = s.recv(Constants.MAX_RECEIVE)
    server_response = []
    while len(received_data) > 0:
        server_response.append(received_data)
        received_data = s.recv(Constants.MAX_RECEIVE)
    s.close()
    return server_response

def generate_error(state: HttpRequestState) -> HttpErrorResponse:
    return {
        HttpRequestState.INVALID_INPUT: HttpErrorResponse(
            Constants.Responses.BAD[Constants.Responses.Index.CODE],
            Constants.Responses.BAD[Constants.Responses.Index.MSG])
    }.get(state, HttpErrorResponse(Constants.Responses.NOT_SUPPORTED[Constants.Responses.Index.CODE],
                                   Constants.Responses.NOT_SUPPORTED[Constants.Responses.Index.MSG]))

def http_request_pipeline(source_addr, http_raw_data):
    """
    HTTP request processing pipeline.

    - Validates the given HTTP request and returns
      an error if an invalid request was given.
    - Parses it
    - Returns a sanitized HttpRequestInfo

    returns:
     HttpRequestInfo if the request was parsed correctly.
     HttpErrorResponse if the request was invalid.

    Please don't remove this function, but feel
    free to change its content
    """
    state = check_http_request_validity(http_raw_data)
    if state is not HttpRequestState.GOOD:
        return generate_error(state)
    return sanitize_http_request(parse_http_request(source_addr, http_raw_data))

def http_request_decode(http_raw_data: bytearray) -> str:
    return http_raw_data.decode(Constants.DECODE)

def parse_http_request(source_addr, http_raw_data: str) -> HttpRequestInfo:
    """
    This function parses a "valid" HTTP request into an HttpRequestInfo
    object.
    """
    request = parse_request(http_raw_data)

    method, requested_path, version = parse_MVP(request)
    requested_port, headers, requested_host = parse_PHH(request)

    return HttpRequestInfo(source_addr, method.upper(), requested_host, requested_port, requested_path.strip(), headers)

def parse_request(request: str) -> list:
    return list(filter(None, request.split(Constants.Delimiters.RQ_LINES_DELIMITER)))

def parse_MVP(request):
    mpv = request[Constants.Index.MVP].split(Constants.Delimiters.SPACE)
    if mpv.__len__() != Constants.RQ_LEN:
        print('[ERROR] Request length does not match.')
        print(f'[VERBOSE] Expected 3 args but got {mpv.__len__()} instead.')
        exit(-1)
        return
    return mpv

def parse_PHH(request):
    phh = request[Constants.Index.PHH:]

    port: int = Constants.DEF_PORT
    headers: list = []
    host = None

    for attribute in phh:
        h = parse_header(attribute)
        headers.append(h)
        if h[0] == Constants.Attributes.HOST:
            host, port = parse_host_port(h)
    return port, headers, host

def parse_header(header) -> list:
    h: list = header.split(Constants.Delimiters.HOST)
    h = [i.strip() if type(i) == str else str(i) for i in h]
    return h

def parse_host_port(header: list):
    hp: list = header[1].split(Constants.Delimiters.HOST)
    return hp[0], (int(hp[1]) if len(hp) > 2 else Constants.DEF_PORT)

def split_http_request(http_request: str):
    try:
        headers = http_request.split(Constants.Delimiters.RQ_LINES_DELIMITER)[Constants.Index.PATH:]
        method = http_request.split(Constants.Delimiters.SPACE)[Constants.Index.METHOD]
        path = http_request.split(Constants.Delimiters.SPACE)[Constants.Index.PATH]
        version = http_request.split(Constants.Delimiters.SPACE)[Constants.Index.VERSION]
    except ValueError or IndexError:
        exit(-1)
        return
    return headers, method, path, version

def format_http_raw_data(http_raw_data: str) -> str:
    return http_raw_data.lower()

def check_http_request_validity(http_raw_data: str) -> HttpRequestState:
    """
    Checks if an HTTP request is valid

    returns:
    One of values in HttpRequestState
    """
    http_request = format_http_raw_data(http_raw_data)

    state = check_request_line_validity(http_request.split("\r\n")[0])
    if state != HttpRequestState.GOOD:
        return state

    headers, method, path, version = split_http_request(format_http_raw_data(http_raw_data))

    state = check_request_ending_validity(http_raw_data)
    if state != HttpRequestState.GOOD:
        return state
    state = check_headers_validity(headers)
    if state != HttpRequestState.GOOD:
        return state
    state = check_host_validity(path, headers)
    if state != HttpRequestState.GOOD:
        return state
    state = check_version_validity(version)
    if state != HttpRequestState.GOOD:
        return state
    state = check_method_validity(method)
    if state != HttpRequestState.GOOD:
        return state
    return HttpRequestState.GOOD

def check_request_line_validity(request: str) -> HttpRequestState:
    if request is None \
            or not request.strip() \
            or len(request.split(Constants.Delimiters.SPACE)) != Constants.RQ_LEN:
        return HttpRequestState.INVALID_INPUT
    return HttpRequestState.GOOD

def check_request_ending_validity(string: str) -> HttpRequestState:
    return HttpRequestState.INVALID_INPUT \
        if string[-Constants.Delimiters.RQ_ENDING.__len__():] != Constants.Delimiters.RQ_ENDING \
        else HttpRequestState.GOOD

def check_method_validity(method: str) -> HttpRequestState:
    if method in Constants.Operations.GOOD:
        return HttpRequestState.GOOD
    elif method in Constants.Operations.NOT_SUPPORTED:
        return HttpRequestState.NOT_SUPPORTED
    elif method in Constants.Operations.PLACEHOLDER:
        return HttpRequestState.PLACEHOLDER
    return HttpRequestState.INVALID_INPUT

def check_headers_validity(headers: list) -> HttpRequestState:
    return HttpRequestState.INVALID_INPUT if headers is None else HttpRequestState.GOOD

def check_version_validity(version: str) -> HttpRequestState:
    v = version.strip()[:version.find(Constants.Delimiters.RQ_LINES_DELIMITER)]
    return HttpRequestState.INVALID_INPUT \
        if v != Constants.Attributes.HTTP_V0 and v != Constants.Attributes.HTTP_V1 \
        else HttpRequestState.GOOD

def check_host_validity(path: str, headers: list) -> HttpRequestState:
    def check_all_hosts_validity(hds: list) -> bool:
        for h in hds:
            line = h.split(Constants.Delimiters.HOST)
            if line[0].strip() == Constants.Attributes.HOST.lower():
                return True
        return False

    valid_host = True
    if path.startswith(Constants.Delimiters.PATH):
        valid_host = check_all_hosts_validity(headers)
    return HttpRequestState.GOOD if valid_host else HttpRequestState.INVALID_INPUT

def sanitize_http_request(request_info: HttpRequestInfo):
    """
    Puts an HTTP request on the sanitized (standard) form
    by modifying the input request_info object.

    for example, expand a full URL to relative path + Host header.

    returns:
    nothing, but modifies the input object
    """
    path = request_info.requested_path

    if path.__contains__(Constants.Attributes.HTTP):
        start_index, end_index = get_sanitization_indices(path)
        host = path[start_index:end_index]

        request_info.requested_path = sanitize_path(path[end_index:])

        if host.__contains__(Constants.Delimiters.HOST):
            host, port = sanitize_host_port(host)
            request_info.requested_port = port

        if request_info.requested_host is None:
            request_info.headers.insert(0, (Constants.Attributes.HOST, host))
            request_info.requested_host = host

    return request_info

def sanitize_host_port(path):
    element = list(filter(None, path.split(Constants.Delimiters.HOST)))
    return element[0], int(element[1])

def sanitize_path(path):
    if not path:
        return Constants.Delimiters.PATH
    return path

def get_sanitization_indices(path: str):
    end_index = path.find(Constants.Delimiters.PATH, 8)
    start_index = path.find(Constants.Delimiters.PATH * 2) + 2
    return start_index, end_index

#######################################
# Leave the code below as is.
#######################################

def get_arg(param_index, default=None):
    """
        Gets a command line argument by index (note: index starts from 1)
        If the argument is not supplies, it tries to use a default value.

        If a default value isn't supplied, an error message is printed
        and terminates the program.
    """
    try:
        return sys.argv[param_index]
    except IndexError as e:
        if default:
            return default
        else:
            print(e)
            print(
                f"[FATAL] The comand-line argument #[{param_index}] is missing")
            exit(-1)  # Program execution failed.

def main():
    """
    Please leave the code in this function as is.

    To add code that uses sockets, feel free to add functions
    above main and outside the classes.
    """
    print("\n\n")
    print("*" * 50)
    print(f"[LOG] Printing command line arguments [{', '.join(sys.argv)}]")
    print("*" * 50)

    # This argument is optional, defaults to 18888
    proxy_port_number = get_arg(1, 18888)
    entry_point(proxy_port_number)

if __name__ == "__main__":
    main()


# coding: utf-8

# In[1]:

def alignSagittalPlane(T):

    # CANONICAL BASE (i,j,k) (HOMOGENEOUS COORDINATES)
    c = np.array(
        [[0,1,0,0],
         [0,0,1,0],
         [0,0,0,1],
         [1,1,1,1]])

    # FIND BASE V
    V_ = np.dot(T,c)
    V_ = V_[:3,1:] - V_[:3,0].reshape(3,1)

    V = np.zeros((3,3))
    V[np.arange(3),np.argmax(np.abs(V_), axis=1)] = 1
    V_[V_==0] = 1
    V = V * (V_/np.abs(V_))

    # DESIRED BASE W
    W = np.array([[1,0,0],
                  [0,0,-1],
                  [0,-1,0]])

    R = np.dot(np.linalg.inv(W), V)
    r = np.diag(np.ones(4))
    r[:3,:3] = R
    return r

# In[ ]:


import serial
import logging
import threading
import concurrent.futures

# if received execution done signal 'E', write messageline is_done True
# if new command string put to messageline, write messageline is_ready True
# if command line is sent, make is_done and is_ready False

class messageLine():
    def __init__(self):
        self.message = ""
        self.is_done = False
        self.is_ready = False
        self.event = threading.Event()
        self._lock = threading.Lock()

def sendMessage(msgLine, ser):
    while True:
        if msgLine.is_done and msgLine.is_ready:
            with msgLine._lock:
                ser.write(msgLine.message.encode())
                logging.info("Message sent: %s", msgLine.message[0:-1])

                msgLine.is_done = False
                msgLine.is_ready = False

        if msgLine.event.is_set():
            break

def receiveMessage(msgLine, ser):
    while True:
        if ser.in_waiting:
            with msgLine._lock:
                message = ser.readline().decode()[0:-2]
                logging.info("Message received: %s", message)

                if message == "Initialized" or len(message) == 1 and message[0] == 'E':
                    msgLine.is_done = True
                elif len(message) == 1 and message[0] == 'T':
                    break

    logging.info("Terminating serial communication")
    msgLine.event.set()

def putMessage(msgLine, string):
    msgLine.message = string + '\n'
    msgLine.is_ready = True

if __name__ == "__main__":
    format = "%(asctime)s: %(message)s"
    logging.basicConfig(format = format, level = logging.INFO, datefmt="%H:%M:%S")

    arduino = serial.Serial('COM7', 9600)
    logging.info("Initializing arduino")

    msg_line = messageLine()

    with concurrent.futures.ThreadPoolExecutor(max_workers = 2) as executor:
        executor.submit(receiveMessage, msg_line, arduino)
        executor.submit(sendMessage, msg_line, arduino)

        while True:
            string = input()
            putMessage(msg_line, string)


from typing import AsyncGenerator, List
import sweeppy

from model import SweepSample

sweep_motor_speed = 2
sweep_sample_rate = 750

async def sweep_scan_generator(usb_port: str) -> AsyncGenerator[List[SweepSample], None]:
    with sweeppy.Sweep(usb_port) as sweep:
        sweep.set_motor_speed(sweep_motor_speed)
        sweep.set_sample_rate(sweep_sample_rate)
        sweep.start_scanning()

        for item in sweep.get_scans():
            yield list(map(lambda scan: SweepSample(scan.angle, scan.distance, scan.signal_strength), item.samples))


#-*-coding: cp1251 -*-
from pygame import *
import os
import core
init()
bg=50,50,50
fc=155,155,155
wh=255,255,255
bl=0,0,0
FormSurf=Surface((100,100))
FormSurf.fill(wh)
# Font3=font.Font(None,24)#Courier New
# Font3=font.SysFont('couriernew', 14, bold=True, italic=False)
Font3=font.SysFont('arial', 14, bold=False, italic=False)
text_surf=Font3.render("text",1,fc,wh)
FormSurf.blit(text_surf,(20,20))
TEXTEDIT=6
OK_BUT=4
CANCEL_BUT=5
CONTENT=8
SCROLLBAR=9

class Form:
	def __init__(self):
		self.Dirlist=[]# список файлов у контента
		self.Sbarlist=[]# И список объектов у скролл бара
		self.Focus=None
		self.background=(0,0,0)
		self.type='form'
		self.Elements=[]# Список объектов
		self.width=0
		self.height=0
		self.position=0
		self.MainSurf=None
		self.FlagTakeScroll=False
		self.Delta=0
	def Render(self):
		color=self.background
		width=self.width
		height=self.height
		self.MainSurf=Surface((width,height))
		self.MainSurf.fill(color)
		for i in self.Elements:
			# ЛУЧШЕ ДЛЯ КАЖДОГО ЭЛЕМЕНТА ВЫЗЫВАТЬ СВОЮ ФУНКЦИЮ ОТРИСОВКИ
			if i.type=='label':
				backcolor=i.background
				# С чего вдруг перестало кодироватьсЯ?
				# print "i.text= ",i.text
				text=unicode(i.text,encoding='cp1251')
				# text=text
				text_surf=Font3.render(text,1,bl,backcolor)
				self.MainSurf.blit(text_surf,i.position)
				# А как же рамка на кнопках? Эта визуализация зависит от типа - например, метка простая
				i.width=text_surf.get_width()
				i.height=text_surf.get_height()
			elif i.type=='button':
				draw.rect(self.MainSurf,wh,(i.background),0)
				draw.rect(self.MainSurf,(127,127,127),(i.shadow),3)
				draw.rect(self.MainSurf,bl,(i.border),1)
				text=unicode(i.text,encoding='cp1251')
				text_surf=Font3.render(text,1,bl,wh)
				x=(i.width/2-text_surf.get_width()/2)+i.position[0]#ошибка?
				y=(i.height/2-text_surf.get_height()/2)+i.position[1]
				t_pos=(x,y)
				self.MainSurf.blit(text_surf,t_pos)
			elif i.type=='textedit':
				draw.rect(self.MainSurf,wh,(i.position[0]-1,i.position[1]-1,i.width,i.height),0)
				draw.rect(self.MainSurf,(127,127,127),(i.position[0]-1,i.position[1]-1,i.width,i.height),1)
				text=i.text[0]
				text_surf=Font3.render(text,1,bl,wh)
				self.MainSurf.blit(text_surf,(i.position[0],i.position[1]+4))
			elif i.type=='content':
				pass
				i.getList()
				self.Dirlist=i.drawList()
				draw.rect(self.MainSurf,wh,((i.position),(i.width,i.height)),0)
				self.MainSurf.blit(i.surf,(i.position[0]+2,i.position[1]))
			elif i.type=='scrollbar':
				self.MainSurf.blit(i.left,(i.position))
				self.MainSurf.blit(i.right,(i.position[0]+i.width-16,i.position[1]))
				i.barPosition=(i.position[0]+16+i.bar_width*self.Elements[CONTENT].CURPAGE,i.position[1])
				self.MainSurf.blit(i.bar,i.barPosition)
				self.Sbarlist=[]
				self.Sbarlist.append(Rect((i.position[0]+212,i.position[1]+184),(15,15)))
				self.Sbarlist.append(Rect((i.position[0]+i.width-16+212,i.position[1]+184),(15,15)))
				self.Sbarlist.append(Rect((i.barPosition[0]+212,i.barPosition[1]+184),(i.bar_width,15)))
	def Control(self,e):#obj=textedit01
		if e.type==MOUSEBUTTONDOWN:
			obj=self.Focus
			if self.Focus[0]==TEXTEDIT:
				obj=self.Elements[6]# Это не нужно, нужно перенаправить на обработчик
			elif self.Focus[0]==CONTENT:#Имена файлов
				obj=self.Elements[CONTENT]
				if self.Focus[1]!=-1:
					text=obj.list[self.Focus[1]]
					obj2=self.Elements[TEXTEDIT]
					NameFile=obj.path+os.sep+text
					if not os.path.isdir(NameFile):
						NameExt=os.path.splitext(NameFile)[1]
						if NameExt==".map":
							text1=unicode(text,encoding='cp1251')
							obj2.text[0]=text1
					if text=="..":
						uppath=os.path.split(obj.path)[0]
						obj.path=uppath
						obj.path2=core.GetFilteredDirList(uppath,".map")[:]
						obj.path2.insert(0,"..")
						obj.LASTPAGE=len(obj.path2)/17
						obj.getList()
						self.Elements[CONTENT].CURPAGE=0
						self.Elements[CONTENT].LASTPAGE=len(self.Elements[CONTENT].path2)/17
						self.Elements[CONTENT].Dirlist=self.Elements[CONTENT].drawList()# ?
						self.Elements[SCROLLBAR].bar_width=(obj.width-32)/(self.Elements[CONTENT].LASTPAGE+1)
						self.Elements[SCROLLBAR].drawBar()
						self.Elements[10].text=self.Elements[CONTENT].path# ОШИБКА!
					else:
						old_path=obj.path
						new_path=os.path.split(obj.path)[0]
						if old_path==new_path:
							path_temp=obj.path+text# ERROR!
						else:
							path_temp=obj.path+os.sep+text# ERROR!
						if os.path.isdir(path_temp):# Если это каталог - заходим внутрь = получаем новый список
							obj.path=path_temp
							uppath=obj.path
							obj.path2=''
							obj.path2=core.GetFilteredDirList(uppath,".map")[:]
							obj.path2.insert(0,"..")
							obj.getList()
							self.Elements[CONTENT].LASTPAGE=len(self.Elements[CONTENT].path2)/17
							self.Elements[CONTENT].CURPAGE=0
							self.Elements[CONTENT].Dirlist=self.Elements[CONTENT].drawList()
							self.Elements[SCROLLBAR].bar_width=(obj.width-32)/(self.Elements[CONTENT].LASTPAGE+1)
							self.Elements[SCROLLBAR].drawBar()
							self.Elements[10].text=self.Elements[CONTENT].path
					# должны перерисовать форму (желательно только текстедит)
					self.Render()
			elif self.Focus[0]==SCROLLBAR:# Стрелки и бар
				if self.Focus[1]==0:
					if self.Elements[CONTENT].CURPAGE!=0:
						self.Elements[CONTENT].CURPAGE-=1
				elif self.Focus[1]==1:
					if self.Elements[CONTENT].LASTPAGE!=self.Elements[CONTENT].CURPAGE:
						self.Elements[CONTENT].CURPAGE+=1
				elif self.Focus[1]==2:
					self.FlagTakeScroll=True
					obj=self.Elements[SCROLLBAR]
				self.Elements[SCROLLBAR].drawBar()
				self.Render()
				draw.rect(self.MainSurf,(0,255,0),((obj.barPosition),(obj.bar_width,obj.height)),0)
		if e.type==MOUSEBUTTONUP:
			self.FlagTakeScroll=False
			self.Delta=0
		if e.type==KEYDOWN:
			if e.key==K_ESCAPE:
				going=False#?
			else:
				obj=self.Elements[TEXTEDIT]
				obj.redraw(e,self)#можно передать и sc
		if e.type==MOUSEMOTION:
			if self.FlagTakeScroll:
				dx=e.dict['rel'][0]
				dy=e.dict['rel'][1]
				self.Delta+=dx
				if abs(self.Delta)>self.Elements[SCROLLBAR].bar_width:
					obj=self.Elements[SCROLLBAR]
					if self.Delta<0:
						if self.Elements[CONTENT].CURPAGE!=0:
							self.Elements[CONTENT].CURPAGE-=1
					elif self.Delta>0:
						if self.Elements[CONTENT].LASTPAGE!=self.Elements[CONTENT].CURPAGE:
							self.Elements[CONTENT].CURPAGE+=1
					obj.barPosition=(obj.position[0]+16+obj.bar_width*self.Elements[CONTENT].CURPAGE,obj.position[1])
					self.Elements[SCROLLBAR].drawBar()
					self.Render()
					self.Delta=0

	def getObj(self,e):
		x=e.dict['pos'][0]
		y=e.dict['pos'][1]
		id,id1=-1,-1
		for i in self.Elements:
			delta_x=self.position[0]
			delta_y=self.position[1]
			r=Rect((i.position[0]+delta_x,i.position[1]+delta_y),(i.width,i.height))
			if (r.collidepoint(x, y)):
				id=self.Elements.index(i)
				if id==8:
					for j in self.Dirlist:
						if j.collidepoint(x,y):
							id1=self.Dirlist.index(j)
				elif id==9:
					for j in self.Sbarlist:
						if j.collidepoint(x,y):
							id1 = self.Sbarlist.index(j)
		return(id,id1)
class Label:
	text=''
	type='label'
	width=0
	height=0
	background=(0,0,0)
	def getSize(self):
		text=unicode(self.text,encoding='cp1251')
		text_surf=Font3.render(self.text,1,bl,fc)
		self.width=text_surf.get_width()
		self.height=text_surf.get_height()

class Button:
	text=''
	type='button'
	color=(0,0,0)
	width=0
	height=0
	position=()
	background=0
	border=0
	shadow=0
	def getSize(self):
		text=unicode(self.text,encoding='cp1251')
		text_surf=Font3.render(self.text,1,bl,fc)
		self.width=text_surf.get_width()+4
		self.height=text_surf.get_height()+4
class Textedit:
	def __init__(self):
		self.type='textedit'
		self.text=['']
		self.last_cur_x=0
		self.lasr_cur_y=0
		self.cur_x=0
		self.cur_y=0# Если многострочный
		self.max_w=0
		self.width=0
		self.height=0
		self.position=()
		self.begin=0
		self.end=0
		self.wrap=False
		self.clearSurf=0
	def redraw(self,e,form):
		k1=e.unicode
		text=self.text[0]
		cur=self.cur_x
		if e.key==K_BACKSPACE:
			if cur>0:
				txt1=text[:cur-1]
				txt2=text[cur:]
				txt=txt1+txt2
				self.text[0]=txt[:]
				cur-=1
				if cur<self.begin:
					self.begin-=1# НЕКРАСИВО ВЫГЛЯДИТ - ничего не меняется. Наверное и правда придется показывать больше символов
				self.ShowText(form)
		elif e.key==K_DELETE:
			if len(text)>cur:
				txt1=text[:cur]
				txt2=text[cur+1:]
				txt=txt1+txt2
				self.text[0]=txt[:]
				self.ShowText(form)
		elif e.key==K_RETURN:# относится к кнопке ОК
			pass
		elif e.key==K_UP:# игнор
			pass
		elif e.key==K_DOWN:# игнор
			pass
		elif e.key==K_RIGHT:# переместить курсор направо
			pass
			self.DrawCur(form,wh)
			if len(text)>cur:
				cur+=1
			if cur>self.end:
				self.begin+=1
			# print "Right",self.begin,cur
			self.ShowText(form)
		elif e.key==K_LEFT:# переместить курсор налево перерисовать строку ограничить видимостью
			pass
			self.DrawCur(form,wh)
			if cur>0:
				cur-=1
			if cur<self.begin:
				self.begin-=1
			self.ShowText(form)
		elif e.key==K_HOME:# переместиться на начало строки отобразить то что будет видимо
			pass
			cur=0
		elif e.key==K_END:# переместиться на конец строки
			pass
			cur=len(text)
		elif e.key in range(39,123) or (e.key in range(39,123) and e.mod==1) or e.key==32 or (e.key==32 and e.mod==1):
			txt1=text[:cur]#left side
			txt2=text[cur:]
			txt=txt1+k1+txt2
			self.text[0]=txt[:]
			cur+=1
			self.ShowText(form)
		self.cur_x=cur
		self.DrawCur(form,bl)
	def DrawCur(self,form,color):
		if self.cur_x<self.end:
			txt=self.text[0][self.begin:self.cur_x]
		else:
			txt=self.text[0][self.begin:self.end]
		w=Font3.render(txt,1,bl,wh).get_width()
		x=self.position[0]
		y=self.position[1]
		draw.line(form.MainSurf,color,(x+w,y+4),(x+w,y+17),1)
	def ShowText(self,form):
		txt=self.text[0][self.begin:self.cur_x]
		w=Font3.render(txt,1,bl,wh).get_width()
		while w>self.width:# если от начала то курсора больше ширины в символах чем поле то:
			txt=self.text[0][self.begin:self.cur_x]
			w=Font3.render(txt,1,bl,wh).get_width()
			if w>self.width:
				self.begin+=1# ищем такое начало, от которого ширина станет меньше
		txt=self.text[0][self.begin:]
		w=Font3.render(txt,1,bl,wh).get_width()
		end=len(self.text[0])
		if w>self.width-10:
			for i in range(17,end):
				txt=self.text[0][self.begin:self.begin+i]
				w=Font3.render(txt,1,bl,wh).get_width()
				if w>self.width-10:
					break
			self.end=self.begin+i-1
		else:
			self.end=len(self.text[0])
		text_surf=Font3.render(self.text[0][self.begin:self.end],1,bl,wh)
		form.MainSurf.blit(self.clearSurf,(self.position))#-стирание поля
		form.MainSurf.blit(text_surf,(self.position[0],self.position[1]+4))
		return 0

class Content:
	content=[]
	type='content'
	path2=''
	path=''
	width=0
	height=0
	position=()
	CURPAGE=0
	LASTPAGE=0
	list=[]
	surf=0
	def getList(self):
		Addr=self.CURPAGE*16
		self.list=[]
		if len(self.path2)>Addr+16:
			self.list=self.path2[Addr:Addr+16]
		else:
			self.list=self.path2[Addr:]
	def drawList(self):
		l=[]
		l1=[]
		max_w=0
		for i in self.list:
			# print i
			text=unicode(i,encoding='cp1251')
			# print text
			# print text.encode('cp1251')
			text_surf=Font3.render(text,1,bl,wh)
			l.append(text_surf)
			w=text_surf.get_width()
			if max_w<w:
				max_w=w
		y=0
		self.surf=Surface((max_w,len(self.list)*16))
		self.surf.fill(wh)
		for i in l:
			self.surf.blit(i,(0,y))
			y+=16
			r=i.get_rect()
			r[0]=self.position[0]+212+2
			r[1]=y-16+self.position[1]+184
			l1.append(r)
		return l1

class Scrollbar:
	width=0
	height=0
	left=0
	right=0
	bar=0
	position=()
	type='scrollbar'
	bar_width=0
	barPosition=(0,0)
	def drawBar(self):
		self.left=Surface((15,15))
		self.right=Surface((15,15))
		self.bar=Surface((self.bar_width,15))
		self.left.fill(wh)
		self.right.fill(wh)
		pointlist1=((1,8),(14,1),(14,14))
		pointlist2=((1,1),(14,8),(1,14))
		draw.polygon(self.left, bl, pointlist1, 0)
		draw.polygon(self.right, bl, pointlist2, 0)


from sets import Set
from heapq import heappush, heappop

INF = int(1e9)

def getShortestPath(graph, startNode, finishNode):
	# initiation
	graph.initShortestPathValues(INF)
	graph.setShortestPathValue(startNode, 0)
	heap = []
	heappush(heap, (0 + graph.getHeuristicValue(startNode), startNode))
	trace = {startNode: (-1, -1)}
	done = Set()

	# iteration
	while len(heap) > 0:
		fu, u = heappop(heap)
		done.add(u)
		if (u == finishNode):
			break
		du = graph.getShortestPathValue(u)
		if fu > du + graph.getHeuristicValue(u):
			continue
		edges = graph.getAdjacentList(u)
		for edge in edges:
			v = edge.to
			if v in done:
				continue
			dv = graph.getShortestPathValue(v)
			tmp = du + edge.cost
			if tmp < dv:
				graph.setShortestPathValue(v, tmp)
				trace[v] = (u, edge.id)
				heappush(heap, (tmp + graph.getHeuristicValue(v), v))

	# return the answer
	dist = graph.getShortestPathValue(finishNode)
	path = []
	u = finishNode
	while u in trace:
		edgeID = trace[u][1]
		if edgeID >= 0:
			path.append(edgeID)
		u = trace[u][0]
	path.reverse()
	return (dist, path)


import random

from board import Board
from player import Player

class GameModel(object):
    """Docstring"""

    class GameMode:
        """Enumeration for keeping track of the game mode"""
        SETUP = 0
        MAIN = 1

    DEFAULT_OPTIONS = {
        "num_players": 4
    }

    # TODO: Place this in a global constants structure
    PLAYER_COLORS = ['red', 'green', 'blue', 'yellow']

    def __init__(self):
        """Initializes the game object"""
        self.active_player = None
        self.board = Board()
        self.players = []
        self.num_players = 0
        self.mode = None

    def create_new_game(self, options=None):
        """Wipes current gamestate and begins a brand new game"""
        if not options:
            options = self.DEFAULT_OPTIONS

        self.players = []
        self.num_players = options['num_players']

        random.shuffle(self.PLAYER_COLORS)

        for n in xrange(self.num_players):
            color = self.PLAYER_COLORS[n]
            self.players.append(Player(None, color, n + 1))

        self.mode = self.GameMode.SETUP
        self.active_player = random.randint(1, self.num_players)

    def get_active_player(self):
        return self.active_player

    def set_active_player(self, player_id):
        """Sets the active player to player_id. Returns true if successful"""
        result = False
        if 1 <= player_id <= self.num_players:
            self.active_player = player_id
            result = True

        return result

    def start_next_turn(self):
        """
        Ends the active players turn and starts the next players turn.
        Returns the new active player.
        """
        self.active_player += 1
        if self.active_player > self.num_players:
            self.active_player = 1
        return self.active_player

    def build_road(self, player_id, edge_id):
        """
        Builds a road for the specified player at the specified position.
        Returns true if successful, false if not

        Arguments:
        player_id   -- the player number to build the road for
        position    -- the position of the road (an edge)
        """
        result = False
        if player_id != self.active_player:
            result = False
        else:
            edge = self.board.get_edge(edge_id)
            if not edge.get_owner():
                node_neighbors = edge.get_node_neighbors()
                edge_neighbors = edge.get_edge_neighbors()
                for node in node_neighbors.extend(edge_neighbors):
                    owner_id = node.get_owner()
                    if owner_id == player_id:
                        self.board.place_road(edge_id, player_id)
                        result = True
                        break

        return result

    def build_settlement(self, player_id, node_id):
        """
        Builds a settlement for the specified player at the specified position.
        Returns true if successful, false if not

        Arguments:
        player_id      -- the player number to build the road for
        node_id    -- the position of the settlement (a vertex)
        """

        result = False

        if player_id == self.active_player:
            node = self.board.get_node(node_id)
            can_place = False
            if not node.get_owner():
                has_a_neighbor = False
                for node in node.get_node_neighbors():
                    if node.get_owner():
                        has_a_neighbor = True
                        break
                if not has_a_neighbor:
                    can_place = True

            if can_place:
                self.board.place_settlement(node_id, player_id)
                result = True

        return result


import numpy as np
import math
import copy
from random import randint
'''
'''

def AveDist(row1,row2,Width,tCntrs,r,c):

    AvgdRow=[]
    tAvgdRow=[]
    wtVector=[]
    Wt1=1.0
    Wt2=1.0
    print("Calculate new row using ")
    print(tCntrs)
    for jj in range(Width):
       facter=tCntrs[jj].count('_')
       wtVector.append(facter+1)
    print(wtVector)
    print(tCntrs)

    print("Given row and column ",r," and ",c)
    Wt1=wtVector[r]
    Wt2=wtVector[c]

    print("Wts are ")
    print(tCntrs[r],"has Wt1 ",Wt1)
    print(tCntrs[c],"has Wt2:",Wt2)

    for pt in range(Width):
       if (row1[pt]==0.0 or row2[pt] == 0.0):
          element=0
       else:
          element=((row1[pt]*Wt1)+(row2[pt]*Wt2))/(Wt1+Wt2)
          print(row1[pt],"*",Wt1," plus ",row2[pt],"*",Wt2," / ",Wt1,"+",Wt2,"=",element)

          telement=((row1[pt])+(row2[pt]))/(Wt1*Wt2)
          print(row1[pt]," plus ",row2[pt]," / ",Wt1,"*",Wt2,"=",telement)
       AvgdRow.append(element)
    print("just computed ",AvgdRow)
    return AvgdRow

def WAve(Row,Col,Data):
    print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
    print("Row is ", Row)
    print("Col is ", Col)
    print("Data is ")
    print(Data)

def gapCompress(aDict):
    cntr=0
    nogapsDict={}
    thegap=-1
    entryCnt=len(aDict)+1
    maxV=-1

    # find the largest key
    for key, value in aDict.items():
        if key > maxV:
            maxV=key
    # if the max key matches the size, its compressed already
    if maxV==len(aDict)-1:
         return aDict

    print("Size and max key not are equal, contin")
    for i in range(entryCnt):
        try:
           print("load ",aDict[i]," into tofix ",i)
        except:
           print("failed")
           print("Gap at ",i)
           thegap=i

    if thegap > 0:
         for kk in range(thegap):
             nogapsDict[kk] = aDict[kk]
         for jj in range(thegap,len(aDict)):
             nogapsDict[jj] = aDict[jj+1]

         ## for key, value in centerLookup.items():
         print(nogapsDict)
         return nogapsDict
    else:
         return aDict

def ParseU(astring):
    facter=astring.count('_')
    return facter

def MinPos(pData):

    LowestV=10000000
    for i in range(len(pData)):
        for j in range(len(pData)):
            if pData[i][j] < LowestV and pData[i][j] > 0.0:
                LowestV=pData[i][j]
                XyPos=(i,j)

    return LowestV, XyPos

class ParameterClass:
    K=0
    M=0
    Beta=0

def readData():
    '''
    Open hardcoded file, parse data anticipataed but may change
    Load just data into a numpy array,
    '''
    #f = open('book.dat', 'r')   #Smaller dataset
    #f = open('wk2_2.dat', 'r')  #Larger test dataset
    #f = open('data1.dat', 'r')   #Smaller dataset
    f = open('data2.dat', 'r')   #Smaller dataset
    #f = open('youtube.dat', 'r')   #Smaller dataset
    #f = open('test.dat', 'r')   # use on test after renaming weird names
    cnt=0
    dataCnt=0
    clist=[] # list of centers, initially one per data point
    lookupC={} # dict to track position of combined centers
    print("Loading data . . .")
    # K is used to grab the first K lines as clusters, don't consider them
    # Data at this time, that may not be correct
    for line in f:
        xx=line.rstrip() # get rid of cr
        if cnt==0:       # known line to contain only K and M
           n=int(xx)
           ##Use n, to Dim numpy array to build, delete row 0 later
           npDataArr = np.zeros(n, dtype=np.float)
        elif cnt >= 1:
           ##Parse data, put rows in list, list appended to a numpy array
           w=xx.split(" ")
           rhead=str(cnt-1)
           clist.append(rhead)
           lookupC[cnt-1]=rhead
           new_list = []
           for item in w:
               new_list.append(float(item))
           npDataArr = np.vstack((npDataArr,new_list))
           dataCnt+=1
        else:
           print("Something is wrong, bail")

        cnt+=1

    ### remove phoney line 0 that established shape.  Find a better way later
    npDataArr=np.delete(npDataArr, 0, 0)
    print("Read ",dataCnt," Datapoints")
    return lookupC, clist, npDataArr

def main():
    #Use a class so pointers to objects persist as they are passed by reference
    Params=ParameterClass()
    Params.M=0
    Params.K=0
    Params.Beta=0

    # read file, load data into n x n array npData
    # also return trackCenters, names of each row

    centerLookup,trackCenters,npData= readData() #load Data and Params from file

    finalPath=[]
    answer=[]
    T={}
    tlist=[]
    trackFactr={}
    for j in trackCenters:
        tlist.append(j)
        trackFactr[j]=1

    T['root']=tlist
    n=len(trackCenters)
    ##  generate two numbers, that are not equal, between 0 and n
    ##  create a new node name string looking up what is in centerlookup

    while len(trackCenters) > 1:
        print("*** Top of the loop   *****")
        print(npData)
        print("TrackCenters is ")
        print(trackCenters)
        print("centerLookup is ")
        print(centerLookup)
        ####
        #1 #
        ##################################################
        # get the smallest value and its position, row wise
        # 1 Search for smallest value, keep it Row, Col tuple Pos
        ##################################################
        SmallV, Pos = MinPos(npData)
        Row=int(Pos[0])
        Col=int(Pos[1])
        print("&&&  Smallest value is now ",SmallV)
        ####
        #3 #
        ##################################################
        # Coalse row with d function, here min from each
        ##############

        w=len(npData[0])
        aveArray=AveDist(npData[Row],npData[Col],w,centerLookup,Row,Col)

        NewRow=np.array(aveArray)

        ####
        #2 #
        ##################################################
        #  Row is first row and Col is also a row later
        #  that is to be joined with Row. Both Row combine
        ##################################################

        ## create new point string, Cnew
        c1=centerLookup[Row]
        c2=centerLookup[Col]
        Coalest1=0
        Coalest2=0
        Coalest1=c1.count('_')
        Coalest2=c2.count('_')
        print("C1 is ",c1)
        print("Cf1 is ",Coalest1)
        print("C2 is ",c2)
        print("Cf2 is ",Coalest2)

        if Coalest1==0 and Coalest2==0:
            Cnew=c1+'_'+c2
        elif Coalest2 >= Coalest1:
            Cnew=c1+'_'+c2
        else:
            Cnew=c2+'_'+c1

        answer.append(Cnew)
        cRow=str(Row)
        cCol=str(Col)

        for node in centerLookup.values():
            if node==str(Row):
                ARow=centerLookup[Row]

        for node in centerLookup.values():
            if node==str(Col):
                ACol=centerLookup[Col]

        # delete old ones from centerLookup dictionary
        del centerLookup[Row]

        del centerLookup[Col]

        # add new ones
        centerLookup[Row]=Cnew

        # re order centerLookup to tuck in gaps
        #for key, value in centerLookup.items():
        #    print(key," ",value)
        centerLookup=gapCompress(centerLookup)

        cntr=0
        tempD={}
        for key, value in centerLookup.items():
            tempD[cntr]=value
            cntr+=1
        centerLookup.clear()
        centerLookup=tempD

        # remove deletes by value
        trackCenters.remove(str(c1))
        trackCenters.remove(str(c2))

        trackCenters.append(Cnew)

        ####
        #4 #
        ##################################################
        #Replace row with newly calced row
        #

        npData=np.insert(npData,Row,[NewRow],axis=0)

        npData=np.delete(npData,Row+1,axis=0)

        ####
        #5 #
        ##################################################

        npData=np.insert(npData,Row,[NewRow],axis=1)

        npData=np.delete(npData,Row+1,axis=1)
        ####

        ####
        #6 #
        ##################################################

        npData=np.delete(npData,Col,axis=0)

        npData=np.delete(npData,Col,axis=1)

    print("Done ")
    for line in answer:
        out=line.split('_')
        outline=""
        for o in out:
            oo=int(o)+1
            outline+=str(oo)+" "

        print(outline[:-1])

    f = open("answer.dat","w")
    for pair in answer:
        aline=""
        p=pair.split('_')
        for j in p:
            ii=int(j)+1
            aline+=(str(ii)+" ")
        f.write(str(aline))
        f.write("\n")
    f.close()

    #for pair in answer:
    #    p=pair.split('_')
    #    for j in p:
    #        k=int(j)+1
    #        print(k,end=" ")
    #    print()

if __name__ == "__main__":
    main()


#!/usr/bin/python

import urllib.request
from urllib.parse import urlparse
import os.path

visited = set() #Set of visited links, in order to not revisit any webpage

def pywget(url, depth):
    if depth < 0:
        return None
    try:
        visited.add(url)
        path = get_file_path(url)
        filename = get_file_name(url, path)  #returns the filename wth corresponding # added if it already exists
        extension = filename.split('/')[-1].split('.')[-1]  # will be in formatu domain/file.extension so split at the / then at the .

        if extension == 'html':  # only find the links and change code if its a webpage
            code = urllib.request.urlopen(url).read().decode('UTF-8')
            code = rewrite_code(code, url, path, depth)  # converts all links to relative links

            with open(path + '/' + filename, 'w') as f:
                    f.write(code)
        else: # an image, doesnt need to follow the links
            urllib.request.urlretrieve(url, path + '/' + filename)
    except:
        print("There was an error")
        raise

def get_file_name(url, path):
    full_filename = urllib.parse.urlsplit(url).path.split('/')[-1]  # returns a tuple of url, gets path from tuple, splits at '/' and gets the final index (as this will be the filename)
    full_filename = full_filename.split('.')  # since the format is still file.extension, split it at the fullstop (into a list) so we can get either by index

    if len(full_filename) <= 1:  # error checking: not a valid file, since no extension
        return None

    filename = full_filename[0]
    extension = full_filename[1]
    original_filename = filename
    count = 1
    while os.path.isfile(path + '/' + filename + '.' + extension):  # if the file already exists
        filename = original_filename + '_' + str(count)  # add a number to the end until the file doesnt exist
        count += 1
    return filename + '.' + extension

def get_file_path(url):
    path = urllib.parse.urlsplit(url).path.split('/')
    path.remove('')  # removes blank space
    netloc = urllib.parse.urlsplit(url).netloc #domain
    path.insert(0, netloc)  # adds the domain to the front
    path.pop()  # last item is file name which should not be included in path
    path = '/'.join(path)

    if not os.path.exists(path):  # makes sure the path exists
        os.makedirs(path, exist_ok=True)
    return path

def rewrite_code(source, url, rootpath, depth):
    netloc = urllib.parse.urlsplit(url).netloc  # Gets the domain of the url (eg homepages.ecs.vuw.ac.nz)
    source = source.split() #convert to list for easier modification
    for i in range(len(source)):
        word = source[i]
        if word.startswith('href') or word.startswith('src'):
            split = word.split('"')  # so we can easily access the link
            word = split[1]  # location of the link
            if not word.startswith('http'):  # if its a relative link, convert to absolute link so we can deal with them all the same way
                word = urllib.parse.urljoin(url, word)
            if netloc in word:  # only save/change links from local domain
                path = get_file_path(word)
                name = get_file_name(word, path)
                path = path[len(rootpath) + 1:]  # checks if theres a subdirectory, +1 to cut off the '/' from the front
                if len(path) > 1:  # must be in a subdirectory
                    name = path + '/' + name  # so add the subdirectory into the name

                source[i] = split[0] + '"' + name + '"' + split[2]

                if word not in visited: #recurse
                    pywget(word, depth-1)

    source = ' '.join(source)  # convert back to string
    return source


# encoding: utf-8
# Copyright (c) 2017 Mahdi bahri
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import csv
import re
import json
contacts = "contacts.csv" # google contacts csv here exemple : contacts.csv

csvfile = open(contacts, 'r')
#get emails by columns : "E-mail Address" , "E-mail 2 Address" and "E-mail 3 Address"
columns = ("E-mail Address","E-mail 2 Address","E-mail 3 Address")
#read csv
csv = csv.DictReader( csvfile, columns)
id = 0
lists = []
for i in csv:
	id= id+1
	for d in i:
		try:
			lists.append(i[d][11])
		except :
			pass
ds=[]
for a in lists:
	#check email format
	EMAIL_REGEX = re.compile(r"[^@]+@[^@]+\.[^@]+")
	if  EMAIL_REGEX.match(a):
		ds.append(a)
#write to output file as json
with open('contact.json','a')as fs:
		json.dump(ds, fs)


#	ZUMA Open FPGA Overlay
#	Configuration File
from structs import *
#ZUMA Architecture Configuration
params = Arch()

#Width of the configuration port and addresses (in bits)
params.config_width = 32
params.config_addr_width = 32
#Cluster inputs
params.I = 28
#LUTs per cluster
params.N = 8
#LUT Size
params.K = 6
#Routing Channel Length
params.L = 2
#Routing Channel Width
params.W = 104
#ArrayDimensions
params.X = 2
params.Y = 2
#Cluster input and output flexibilities
params.fc_in = 6
params.fc_in_type = 'abs'
params.fc_out = 10
params.fc_out_type = 'abs'

#use vpr 7
params.vpr7 = True
#close network support
params.UseClos = False
#Should the parser build large permutation around all IO to fix their position in the fpga_input/fpga_output arrays?
params.orderedIO = True

#if you want to dump the node graph and mapped node graph in a readable format.
#EXPERIMENTAL:If graphviz is turned on there is also a graphical dump of the node graph.
#WARNING:graphviz could freeze the build process if the graph is too big.
params.dumpNodeGraph = False
params.graphviz = False

#activate the timing analysis. See ../TIMING README
params.sdf = False
#path of the two sdf files needed for timing analysis. see ../TIMING README
params.sdfFileName = "../final_no_buffer.sdf"
params.sdfFlipflopFileName = "../final_with_buffer.sdf"
#time scale of your timing information. here its ps
params.timeScale = 1.0/1000000000000
params.timeFormat = "ps"

#used for the timing analysis.
#The prefix of the components which is the path of your zuma instance
#in the sdf file.
#E.g the component zuma_top/zuma_i is translated to zuma_top_zuma_i_ in the sdf file.
params.instancePrefix = "zuma_top_zuma_i_"


# Author: F. Alex Wolf (http://falexwolf.de)
"""Logging and Profiling
"""

import os
import time
import datetime
from . import settings as sett

verbosity_levels_from_strings = {
    'error': 0,
    'warn': 1,
    'info': 2,
    'hint': 3,
}

def m(*msg, v='info', t=False, m=False, r=False, end='\n'):
    """Write message to log output.

    Log output defaults to standard output but can be set to a file
    by setting `sc.sett.log_file = 'mylogfile.txt'`.

    v/verbosity : {'error', 'warn', 'info', 'hint'} or int, (default: 'info')
        0/'error', 1/'warn', 2/'info', 3/'hint' or integer 4.
    t/timing : bool, optional (default: False)
        Print timing information.
    m/memory : bool, optional (default: Faulse)
        Print memory information.
    r/reset : bool, optional (default: False)
        Reset timing and memory measurement. Is automatically reset
        when passing one of t or m.
    end : str (default: '\n')
        As in python builtin print function.
    """
    if isinstance(v, str):
        v = verbosity_levels_from_strings[v]
    if isinstance(sett.verbosity, str):
        global_v = verbosity_levels_from_strings[sett.verbosity]
    else:
        global_v = sett.verbosity
    if v == 3:  # insert "--> " before hints
        msg = ('-->',) + msg
    if v >= 4:
        msg = ('   ',) + msg
    if v <= global_v:
        if not t and not m and len(msg) > 0:
            sett.mi(*msg, end=end)
        if r:
            sett._previous_memory_usage, _ = get_memory_usage()
            sett._previous_time = time.time()
        if t:
            elapsed = get_passed_time()
            msg = msg + ('({})'.format(sett._sec_to_str(elapsed)),)
            sett.mi(*msg, end=end)
        if m:
            sett.mi(format_memory_usage(get_memory_usage()), msg='' if t else msg, end=end)

def get_passed_time():
    now = time.time()
    elapsed = now - sett._previous_time
    sett._previous_time = now
    return elapsed

def get_memory_usage():
    import psutil
    process = psutil.Process(os.getpid())
    meminfo_attr = ('memory_info' if hasattr(process, 'memory_info')
                    else 'get_memory_info')
    mem = getattr(process, meminfo_attr)()[0] / 2**30  # output in GB
    mem_diff = mem
    if sett._previous_memory_usage != -1:
        mem_diff = mem - sett._previous_memory_usage
    sett._previous_memory_usage = mem
    return mem, mem_diff

def format_memory_usage(mem_usage, msg='', newline=False):
    mem, diff = mem_usage
    string = (('\n' if newline else '')
              + msg + (' \n... ' if msg != '' else '')
              + 'Memory usage: current {:.2f} GB, difference {:+.2f} GB'
              .format(mem, diff))
    return string

def print_memory_usage(msg='', newline=False):
    string = format_memory_usage(get_memory_usage(), msg, newline)
    sett.mi(string)

def print_version_and_date():
    from . import __version__
    sett.mi('Running Scanpy version', __version__, 'on {}.'.format(get_date()))

def get_date():
    return datetime.datetime.now().strftime("%Y-%m-%d %H:%M")


import json
import pytest

from unittest import mock
from dcicutils import ecr_utils as ecr_utils_module
from dcicutils.ecr_utils import ECRUtils, ECRTagWatcher
from dcicutils.docker_utils import DockerUtils
from dcicutils.misc_utils import ignored, ignorable, filtered_warnings
from dcicutils.qa_utils import MockBoto3, MockBotoECR
from .helpers import using_fresh_cgap_state_for_testing

REPO_URL = '123456789.dkr.ecr.us-east-2.amazonaws.com/cgap-mastertest'  # dummy URL

def mocked_describe_respositories():
    """ Mocks the important info from the describe_repositories API call. """
    return {'repositories': [{
        'repositoryUri': REPO_URL,
       }]}

def mocked_ecr_login(*, username, password, registry):
    ignored(username, password, registry)
    return

@using_fresh_cgap_state_for_testing()
def test_ecr_utils_basic():
    """ Tests something simple for now, more tests to be added later. """
    # init args no longer default. -kmp 14-Jul-2022
    cli = ECRUtils(env_name='cgap-mastertest', local_repository='cgap-wsgi')
    with mock.patch.object(cli.ecr_client, 'describe_repositories', mocked_describe_respositories):
        url = cli.resolve_repository_uri()
        assert url == REPO_URL

@pytest.mark.skipif(not DockerUtils.docker_is_running(), reason="Docker is not running.")
@using_fresh_cgap_state_for_testing()
def test_ecr_utils_workflow():
    """ Tests URL + Login via Docker_cli"""
    # init args no longer default. -kmp 14-Jul-2022
    ecr_cli = ECRUtils(env_name='cgap-mastertest', local_repository='cgap-wsgi')
    docker_cli = DockerUtils()
    with mock.patch.object(ecr_cli.ecr_client, 'describe_repositories', mocked_describe_respositories):
        ecr_cli.resolve_repository_uri()
        ecr_user, ecr_pass = 'dummy', 'dummy'  # XXX: integrated test for this mechanism
        with mock.patch.object(docker_cli.client, 'login', mocked_ecr_login):
            ignorable(filtered_warnings)
            with filtered_warnings("ignore", category=DeprecationWarning):
                # e.g., as of docker 4.4.4, we see in site-packages/docker/utils/utils.py, lines 59-60:
                #  DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
                #    s1 = StrictVersion(v1)
                #  DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
                #    s2 = StrictVersion(v2)
                docker_cli.login(ecr_repo_uri=ecr_cli.url,
                                 ecr_user=ecr_user, ecr_pass=ecr_pass)
            # XXX: integrated test the remaining?

def test_ecr_utils_get_uri():
    print()  # start output on fresh line
    mock_boto3 = MockBoto3()
    with mock.patch.object(ecr_utils_module, "boto3", mock_boto3):
        ecr_client: MockBotoECR = mock_boto3.client('ecr')
        for repo in ['main', 'decoy-1', 'fourfront-foo', 'decoy-2']:
            print(f"Adding image repository"
                  f" {json.dumps(ecr_client.add_image_repository_for_testing(repo), indent=2, default=str)}")
        print("---- Setup complete. Beginning test. ----")
        ecr_utils = ECRUtils(env_name='fourfront-foo')
        assert ecr_utils.url is None
        with pytest.raises(Exception):
            ecr_utils.get_uri()  # This intentionally raises an error if there is no URL
        ecr_utils.resolve_repository_uri()
        print(f"ecr_utils.url = {ecr_utils.url}")
        assert ecr_utils.url is not None
        uri = ecr_utils.get_uri()
        print(f"ecr_utils.get_uri() = {uri}")
        assert isinstance(uri, str)
        assert uri.endswith('/fourfront-foo')

def test_ecr_utils_resolve_repository_uri():
    print()  # start output on fresh line
    mock_boto3 = MockBoto3()
    with mock.patch.object(ecr_utils_module, "boto3", mock_boto3):
        ecr_client: MockBotoECR = mock_boto3.client('ecr')
        for repo in ['main', 'decoy-1', 'fourfront-foo', 'decoy-2']:
            print(f"Adding image repository"
                  f" {json.dumps(ecr_client.add_image_repository_for_testing(repo), indent=2, default=str)}")
        print("---- Setup complete. Beginning test. ----")
        ecr_utils = ECRUtils(env_name='fourfront-foo')
        print(f"ecr_utils.url = {ecr_utils.url}")
        assert ecr_utils.url is None
        print("resolving repository URI")
        ecr_utils.resolve_repository_uri()
        print(f"ecr_utils.url = {ecr_utils.url}")
        assert ecr_utils.url is not None

def test_ecr_tag_watcher():
    print()  # start output on fresh line
    mock_boto3 = MockBoto3()
    with mock.patch.object(ecr_utils_module, "boto3", mock_boto3):
        ecr_client: MockBotoECR = mock_boto3.client('ecr')
        ecr_client.add_image_repository_for_testing('main')
        for i in range(5):
            last_from_loop = ecr_client.add_image_metadata_for_testing('main', tags=['latest'])
        watcher = ECRTagWatcher()
        first_found = watcher.get_current_image_digest()
        print(f"first_found = {first_found}")
        assert first_found == last_from_loop['imageDigest']
        to_deploy = watcher.check_for_new_image_to_deploy()
        assert not to_deploy
        print(f"to_deploy =   {to_deploy}")
        assert first_found == watcher.get_current_image_digest()
        print(f"Simulating deploy.")
        even_newer = ecr_client.add_image_metadata_for_testing('main', tags=['latest'])
        next_found = watcher.get_current_image_digest()
        print(f"next_found =  {next_found}")
        assert next_found != first_found
        assert next_found == even_newer['imageDigest']
        assert next_found == watcher.get_current_image_digest()  # It stays this until next deploy
        to_deploy = watcher.check_for_new_image_to_deploy()
        print(f"to_deploy =   {to_deploy}")
        assert to_deploy
        assert to_deploy == next_found
        to_deploy = watcher.check_for_new_image_to_deploy()
        print(f"to_deploy =   {to_deploy}")
        assert not to_deploy


import sys
import re
import math

def readText( file1 ):
    fo = open(file1, "r")
    text=fo.read()
    fo.close()
    return text

def writeOutput( str ):
    wo = open("nboutput.txt","w")
    wo.write(str)
    wo.close()
    return

def parsePrior( m ):
    m1 = m.split(" ")
    m11 = m1[0].split("/")
    m12 = math.log(float(m11[0])/float(m11[1]))
    m21 = m1[1].split("/")
    m22 = math.log(float(m21[0]) / float(m21[1]))
    m31 = m1[2].split("/")
    m32 = math.log(float(m31[0]) / float(m31[1]))
    m41 = m1[3].split("/")
    m42 = math.log(float(m41[0]) / float(m41[1]))
    prior = [ m12 , m22 , m32 , m42 ]
    return prior

def parseModel( model ):
    del model[0]
    del model[0]
    del model[0]
    del model[0]
    del model[0]     #removing forst 5 line of the model
    d = dict()
    #print model
    for x in model:
        t = x.split(":")
        p = t[1].strip('\t')
        p1 = p.split(" ")
        m11 = p1[0].split("/")
        m12 = math.log(float(m11[0]) / float(m11[1]))
        m21 = p1[1].split("/")
        m22 = math.log(float(m21[0]) / float(m21[1]))
        m31 = p1[2].split("/")
        m32 = math.log(float(m31[0]) / float(m31[1]))
        m41 = p1[3].split("/")
        m42 = math.log(float(m41[0]) / float(m41[1]))
        d[t[0]] = [ m12 , m22 , m32 , m42 ]

    return d

def classifyLine( line  , d , prior ):
    words = line.split(" ")
    op = words[0] + " "
    #print outputStr
    truth = prior[0]
    decep = prior[1]
    posi = prior[2]
    nega = prior[3]
    for l in words:
        cw0 = l.lower()
        cw = re.findall(r"[\w']+", cw0)
        for w1 in cw:
            if d.has_key(w1):
                pp = d.get(w1)      #pp contains all the partial probabilititis of that feature word
                truth += pp[0]
                decep += pp[1]
                posi += pp[2]
                nega += pp[3]

    if truth > decep:
        op += "truthful"+ " "
    else:
        op += "deceptive" + " "

    if posi > nega:
        op += "positive" + " "
    else:
        op += "negative" + " "
    #print outputStr
    return op

file1 = str(sys.argv[1]) #file path to test.txt
file2 = "nbmodel.txt"
text = readText(file1)
model = readText(file2)
t2 = text.split("\n")     #t2 contains all the test lines
m2 = model.split("\n")      #m2 contains all the lines on the model.txt

prior = parsePrior(m2[1])
#print prior     #dictionanry of priors

d = parseModel(m2)
#print d     #dictionary on partial probabilities

outputStr = ""
count = 0
while count<len(t2):
    if len(t2[count].strip()) !=0:
        o = classifyLine(t2[count] , d , prior)
        outputStr += o
        if count != (len(t2)-1):
            outputStr += "\n"
    count+=1

#print outputStr
writeOutput(outputStr)


import torch
import torch.nn as nn
import torch.nn.functional as F

__all__ = ['PreActResNet50', 'PreActResNet101', 'PreActResNet152']

class PreActBlock(nn.Module):
    '''Pre-activation version of the BasicBlock.'''
    expansion = 1

    def __init__(self, in_planes, planes, stride=1):
        super(PreActBlock, self).__init__()
        self.bn1 = nn.BatchNorm2d(in_planes)
        self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
        self.bn2 = nn.BatchNorm2d(planes)
        self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1, padding=1, bias=False)
        if stride != 1 or in_planes != self.expansion*planes:
            self.shortcut = nn.Sequential(
                nn.Conv2d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=False)
            )

    def forward(self, x):
        out = F.relu(self.bn1(x))

        if hasattr(self, 'shortcut'):
            shortcut = self.shortcut(x)
        else:
            shortcut = x

        out = self.conv1(out)
        out = self.bn2(out)
        out = F.relu(out)
        out = self.conv2(out)
        out = out + shortcut
        return out

class PreActBottleneck(nn.Module):
    '''Pre-activation version of the original Bottleneck module.'''
    expansion = 4

    def __init__(self, in_planes, planes, stride=1):
        super(PreActBottleneck, self).__init__()

        self.bn1 = nn.BatchNorm2d(in_planes)
        self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=1, bias=False)

        self.bn2 = nn.BatchNorm2d(planes)
        self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)

        self.bn3 = nn.BatchNorm2d(planes)
        self.conv3 = nn.Conv2d(planes, self.expansion*planes, kernel_size=1, bias=False)

        if stride != 1 or in_planes != self.expansion*planes:
            self.shortcut = nn.Sequential(
                nn.Conv2d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=False))

    def forward(self, x):
        out = F.relu(self.bn1(x))
        input_out = out

        out = self.conv1(out)
        out = self.bn2(out)
        out = F.relu(out)

        out = self.conv2(out)
        out = self.bn3(out)

        out = F.relu(out)
        out = self.conv3(out)

        if hasattr(self, 'shortcut'):
            shortcut = self.shortcut(x)
        else:
            shortcut = x

        out = out + shortcut
        return out

class PreActResNet(nn.Module):
    def __init__(self, block, num_blocks, num_classes=10, dataset="CIFAR10"):
        super(PreActResNet, self).__init__()

        self.in_planes = 32
        self.dataset = dataset

        if dataset == "CIFAR10":
            self.conv1 = nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1, bias=False)
            num_classes = 10
        elif dataset == "ImageNet":
            self.conv1 = nn.Conv2d(3, 32, kernel_size=7, stride=2, padding=3,
                                           bias=False)
            self.bn1 = nn.BatchNorm2d(32)
            self.relu = nn.ReLU(inplace=True)
            self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
            num_classes = 365

        self.layer1 = self._make_layer(block, 32, num_blocks[0], stride=1)
        self.layer2 = self._make_layer(block, 64, num_blocks[1], stride=2)
        self.layer3 = self._make_layer(block, 128, num_blocks[2], stride=2)
        self.layer4 = self._make_layer(block, 256, num_blocks[3], stride=2)

        if dataset == "CIFAR10":
            self.avgpool = nn.AvgPool2d(4, stride=1)
            self.linear = nn.Linear(256*block.expansion, num_classes)
        elif dataset == "ImageNet":
            self.avgpool = nn.AvgPool2d(7, stride=1)
            self.fc = nn.Linear(256 * block.expansion, num_classes)

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
            if isinstance(m, nn.BatchNorm2d):
                m.bias.data.zero_()

    def _make_layer(self, block, planes, num_blocks, stride):
        strides = [stride] + [1]*(num_blocks-1)
        layers = []
        for stride in strides:
            layers.append(block(self.in_planes, planes, stride))
            self.in_planes = planes * block.expansion
        return nn.Sequential(*layers)

    def forward(self, x):
        out = self.conv1(x)
        if self.dataset == "ImageNet":
            # out = self.bn1(out)
            # out = self.relu(out)
            out = self.maxpool(out)

        out = self.layer1(out)
        out = self.layer2(out)
        out = self.layer3(out)
        out = self.layer4(out)
        out = self.avgpool(out)

        out = out.view(out.size(0), -1)
        if self.dataset == "CIFAR10":
            out = self.linear(out)
        elif self.dataset == "ImageNet":
            out = self.fc(out)
        return out

def PreActResNet18(dataset="ImageNet"):
    return PreActResNet(PreActBlock, [2,2,2,2], dataset=dataset)

def PreActResNet34(dataset="ImageNet"):
    return PreActResNet(PreActBlock, [3,4,6,3], dataset=dataset)

def PreActResNet50(dataset = "ImageNet"):
    return PreActResNet(PreActBottleneck, [3,4,6,3], dataset = dataset)

def PreActResNet101(dataset = "ImageNet"):
    return PreActResNet(PreActBottleneck, [3,4,23,3], dataset = dataset)

def PreActResNet152(dataset = "ImageNet"):
    return PreActResNet(PreActBottleneck, [3,8,36,3], dataset = dataset)

def test():
    net = PreActResNet18()
    y = net((torch.randn(1,3,32,32)))
    print(y.size())

# test()