import connexion
import json

from models.entities import TelemetryData
from services.services import TelemetryDataService
from schemas.schemas import TelemetryDataSchema
from swagger_server.models.error_response import ErrorResponse  # noqa: E501
from swagger_server.models.error_type_enum import ErrorTypeEnum  # noqa: E501
from custom_errors import EntityNotFound, InvalidPayload, BaseCustomError

from swagger_server.models.create_telemetry_data_request import CreateTelemetryDataRequest  # noqa: E501
from swagger_server.models.create_telemetry_data_response import CreateTelemetryDataResponse  # noqa: E501
from swagger_server.models.error_response import ErrorResponse  # noqa: E501
from swagger_server.models.get_telemetry_data_response import GetTelemetryDataResponse  # noqa: E501
from swagger_server.models.update_telemetry_data_request import UpdateTelemetryDataRequest  # noqa: E501
from swagger_server.models.list_telemetry_data_response import ListTelemetryDataResponse  # noqa: E501

telemetry_data_service = TelemetryDataService()
telemetry_data_schema = TelemetryDataSchema()

def create_telemetry_data(body):  # noqa: E501
    """Create Telemetry data tracking

    This operation is used to create new a telemetry data tracking. # noqa: E501

    :param body:
    :type body: dict | bytes

    :rtype: CreateTelemetryDataResponse
    """
    response = None
    response_code = None
    try:
        if not connexion.request.is_json:
            raise InvalidPayload(code="CST002", message="Invalid Request Payload",
                                 details=f"Request payload is not a JSON valid")
        body = CreateTelemetryDataRequest.from_dict(connexion.request.get_json())  # noqa: E501
        entity = TelemetryData(data_id=None, vehicle_id=body.vehicle_id, data_hora=body.data_hora,
                               latitude=body.latitude, longitude=body.longitude, altimeter=body.altimeter,
                               telemetry_value=body.telemetry_value, tipo_sensor=body.tipo_sensor)
        entity = telemetry_data_service.save(entity)
        response = CreateTelemetryDataResponse.from_dict(json.loads(telemetry_data_schema.dumps(entity)))
        response_code = 201

    except BaseCustomError as bce:
        response_code = bce.http_code
        response = bce.to_error_response()
    except Exception as e:
        response_code = 500
        response = ErrorResponse(code="CST0002", type=ErrorTypeEnum.PERSISTENCE,
                                 message="Error on create new TelemetryData", details=str(e))

    return response.to_dict(), response_code

def delete_telemetry_data(data_id):  # noqa: E501
    """Delete Telemetry data tracking

    This operation is used to delete a specific telemetry data. # noqa: E501

    :param data_id: Unique identifier of the Telemetry Data in the database
    :type data_id: dict | bytes

    :rtype: None
    """
    response = None
    response_code = None
    try:
        entity = telemetry_data_service.fetch_by_id(data_id)
        if entity is None:
            raise EntityNotFound(code="CST001", message="TelemetryData not found",
                                 details=f"Unable to find data ID {data_id}")
        telemetry_data_service.delete(data_id)
        response_code = 204
    except BaseCustomError as bce:
        response_code = bce.http_code
        response = bce.to_error_response()
    except Exception as e:
        response_code = 500
        response = ErrorResponse(code="CSM999", type=ErrorTypeEnum.UNKNOWN,
                                 message="Ops.. Unknown error..", details=str(e))
    if response is None:
        return None, response_code
    else:
        return response.to_dict(), response_code

def get_telemetry_data(data_id):  # noqa: E501
    """Get a single telemetry Data info

    This operation is used to retrieve the details of a specific telemetry data. # noqa: E501

    :param data_id: Unique identifier of the Telemetry Data in the database
    :type data_id: dict | bytes

    :rtype: GetTelemetryDataResponse
    """
    response = None
    response_code = None
    try:
        entity = telemetry_data_service.fetch_by_id(entity_id=data_id)
        if entity is None:
            raise EntityNotFound(code="CST001", message="TelemetryData not found",
                                 details=f"Unable to find data ID {data_id}")
        response = GetTelemetryDataResponse.from_dict(json.loads(telemetry_data_schema.dumps(entity)))
        response_code = 200
    except BaseCustomError as bce:
        response_code = bce.http_code
        response = bce.to_error_response()
    except Exception as e:
        response_code = 500
        response = ErrorResponse(code="CSM999", type=ErrorTypeEnum.UNKNOWN,
                                 message="Ops.. Unknown error..", details=str(e))
    return response.to_dict(), response_code

def list_telemetry_data():  # noqa: E501
    """Retrieve a list of telemetry data sended

    Retrieve a list of telemetry data # noqa: E501

    :rtype: List[GetTelemetryDataResponse]
    """
    entities = telemetry_data_service.fetch_all()

    telemetry_data_response_list = []
    for entity in entities:
        telemetry_data_response_list.append(
            GetTelemetryDataResponse.from_dict(json.loads(telemetry_data_schema.dumps(entity))))

    response = ListTelemetryDataResponse(content=telemetry_data_response_list,
                                         total_results=len(telemetry_data_response_list))
    return response.to_dict(), 200

def update_telemetry_data(body, data_id):  # noqa: E501
    """Update Telemetry data tracking

    This operation is used to update the details of a specific telemetry data. # noqa: E501

    :param body:
    :type body: dict | bytes
    :param data_id: Unique identifier of the Telemetry Data in the database
    :type data_id: dict | bytes

    :rtype: None
    """
    response = None
    response_code = None
    try:
        if not connexion.request.is_json:
            raise InvalidPayload(code="CST002", message="Invalid Request Payload",
                                 details=f"Request payload is not a JSON valid")
        body = UpdateTelemetryDataRequest.from_dict(connexion.request.get_json())  # noqa: E501
        entity = telemetry_data_service.fetch_by_id(data_id)
        if entity is None:
            raise EntityNotFound(code="CST001", message="Telemetry Data not found",
                                 details=f"Unable to find data ID {data_id}")

        entity.vehicle_id = body.vehicle_id
        entity.data_hora = body.data_hora
        entity.latitude = body.latitude
        entity.longitude = body.longitude
        entity.altimeter = body.altimeter
        entity.telemetry_value = body.telemetry_value
        entity.tipo_sensor = body.tipo_sensor

        telemetry_data_service.save(entity)
        response_code = 204
    except BaseCustomError as bce:
        response_code = bce.http_code
        response = bce.to_error_response()
    except Exception as e:
        response_code = 500
        response = ErrorResponse(code="CSM999", type=ErrorTypeEnum.UNKNOWN,
                                 message="Ops.. Unknown error..", details=str(e))

    if response is None:
        return None, response_code
    else:
        return response.to_dict(), response_code


from tkinter import *
from PIL import ImageTk, Image

'''alt+B to start program in Sublime
'''

'''This program assists me in understanding Game Theory Optimal solutions for Spin N Gos: 3-handed hyper turbos.
'''

'''Heads up nash equilibrium solutions 10- bbs'''
def display_nash():

	global nash
	nash = var6.get()

	if nash == "NASHJAM":
		imagenash = "C:/Python3.8/Spins/NASHJAM.png"
	elif nash == "NASHCALL":
		imagenash = "C:/Python3.8/Spins/NASHCALL.png"

	file = ImageTk.PhotoImage(Image.open(imagenash))
	label = Label(image=file)
	label.image = file
	label.grid(row=5, columnspan=6, pady=50)

'''show images from folder'''
def display_img():
	global actn
	actn = var4.get()

	if pos == "SB":
		image = '_'.join([ply,pos,bb])
	elif pos == "BBvsBTN":
		image = '_'.join([ply,pos,bb, "raise"])
	else:
		image = '_'.join([ply,pos,bb,actn])

	imagefile = "C:/Python3.8/Spins/" + image + ".png"

	file = ImageTk.PhotoImage(Image.open(imagefile))
	label = Label(image=file)
	label.image = file
	label.grid(row=5, columnspan=6, pady=50)

	var6.set(0)

'''response to villain: raise or limped pot?'''
def display_rspns():
	for i in r4:
		r4[i].grid_remove()
		var4.set(0)

	global bb
	bb = var3.get()

	if pos == "SB":
		values4 = {}
		display_img()
		return
	elif pos == "BBvsBTN":
		values4 = {}
		display_img()
		return
	else:
		values4 = {"raise" : "raise", "limp" : "limp"}

	count = 0

	label_4 = Label(root, text="Response?", width=10,font=("bold", 10))
	label_4.grid(row=4,column=count)
	for (t,v) in values4.items():
		count += 1
		r4[count] = Radiobutton(root, text=t, variable=var4, value=v, indicator=0, background="light blue", command=display_img)
		r4[count].grid(row=4, column=count, sticky=W)

'''Show number of effective big blinds'''
def display_bb():

	for i in r3:
		r3[i].grid_remove()
		var3.set(0)

	for i in r4:
		r4[i].grid_remove()
		var4.set(0)

	global pos
	pos = var2.get()

	if ply == "HU" and pos == "SB":
		values3 = {"9-11bb" : "9-11bb", "11-13bb" : "11-13bb", "13-17bb" : "13-17bb"}
	elif ply == "HU" and pos == "BB":
		values3 = {"11-13bb" : "11-13bb", "13-17bb" : "13-17bb"}
	elif ply == "3H" and pos == "SB":
		values3 = {"12-14bb" : "12-14bb", "14-17bb" : "14-17bb", "17-22bb" : "17-22bb", "22-25bb" : "22-25bb"}
	elif ply == "3H" and pos == "BB":
		values3 = {"10-12bb" : "10-12bb", "12-14bb" : "12-14bb", "14-17bb" : "14-17bb", "17-21bb" : "17-21bb", "21-25bb" : "21-25bb"}
	elif ply == "3H" and pos == "BBvsBTN":
		values3 = {"11-14bb" : "11-14bb", "14-17bb" : "14-17bb", "17-21bb" : "17-21bb", "21-25bb" : "21-25bb"}

	count = 0

	label_3 = Label(root, text="BBs?", width=10,font=("bold", 10))
	label_3.grid(row=3,column=count)
	for (t,v) in values3.items():
		count += 1
		r3[count] = Radiobutton(root, text=t, variable=var3, value=v, indicator=0, background="light blue", command=display_rspns)
		r3[count].grid(row=3, column=count, sticky=W)

'''Positions: Small Blind, Big Blind, Button'''
def display_pstn():

	for i in r2:
		r2[i].grid_remove()
		var2.set(0)

	for i in r3:
		r3[i].grid_remove()
		var3.set(0)

	for i in r4:
		r4[i].grid_remove()
		var4.set(0)

	global ply
	ply = var.get()

	if ply == "HU":
		values2 = {"SB" : "SB", "BB" : "BB"}
	else:
		values2 = {"SB" : "SB", "BB" : "BB", "BBvsBTN" : "BBvsBTN"}

	count = 0

	label_2 = Label(root, text="Position?",width=10,font=("bold", 10))
	label_2.grid(row=2, column=count)
	for (t, v) in values2.items():
		count += 1
		r2[count] = Radiobutton(root, text=t, variable=var2, value=v, indicator=0, background="light blue", command=display_bb)
		r2[count].grid(row=2, column=count, sticky=W)

'''initialize'''
root = Tk()
root.geometry('640x710')
root.title("Jackpot SitNGos")

r2 = dict()
r3 = dict()
r4 = dict()
var = StringVar()
var2 = StringVar()
var3 = StringVar()
var4 = StringVar()

var6 = StringVar()
global file

values = {"2" : "HU", "3" : "3H"}
count = 0

label_1 = Label(root, text="Players?",width=10,font=("bold", 10))
label_1.grid(row=1, column=count)

label_6 = Label(root, text="NASH?",width=10,font=("bold", 10))
label_6.grid(row=6, column=count)
Radiobutton(root, text="NASH JAM", variable=var6, value="NASHJAM", indicator=0, background="pink", command=display_nash).grid(row=6, column=1, sticky=W)
Radiobutton(root, text="NASH CALL", variable=var6, value="NASHCALL", indicator=0, background="pink", command=display_nash).grid(row=6, column=2, sticky=W)

for (t, v) in values.items():
	count += 1
	Radiobutton(root, text=t, variable=var, value=v, indicator=0, background="light blue", command=display_pstn).grid(row=1, column=count, sticky=W)

root.mainloop()


import re, pytest, os, uuid

from playwright.sync_api import expect
from src.pages.board import BoardPage
from src.pages.home import HomePage
from src.pages.login import LoginPage
from src.pages.components.header import HeaderComponent
from src.pages.components.workspace_nav import WorkspaceNavComponent

page = None
board = None
home = None
header = None
login = None
workspace = None
board_name = str(uuid.uuid4())
updated_board_name = str(uuid.uuid4())

@pytest.fixture(scope='function', autouse=True)
def before_each_after_each(playwright):
    global page
    global board
    global home
    global header
    global login
    global workspace
    # Before each
    firefox = playwright.firefox
    browser = firefox.launch()
    page = browser.new_page()
    board = BoardPage(page)
    home = HomePage(page)
    header = HeaderComponent(page)
    login = LoginPage(page)
    workspace = WorkspaceNavComponent(page)

    home.goto()
    header.login_btn.click()
    login.login(
        os.getenv('USER_NAME'),
        os.getenv('PASSWORD')
    )
    yield
    # here can come an After each part

def test_should_create_update_and_delete_board_through_ui():
    home.section_header.wait_for()
    expect(home.section_header).to_contain_text('YOUR WORKSPACES')

    # create board
    home.create_new_board(board_name, 'ðŸŒˆ')

    board.wait_for_page_loaded()
    workspace.nav.wait_for()

    expect(board.main_title).to_contain_text(board_name)

    #u pdate board
    board.update_board_name(updated_board_name)

    expect(board.main_title).to_contain_text(updated_board_name)

    # close board
    workspace.close_current_board()

    expect(board.close_board_message).to_contain_text(f'{updated_board_name} is closed.')

    # delete board
    board.delete_board()

    expect(home.section_header).to_contain_text('YOUR WORKSPACES')

    board_names = home.get_all_board_names()
    if (len(board_names) > 0):
        expect().not_to_contain_text(updated_board_name)


import boto3
import datetime
from datetime import timedelta
import io
import json
import logging
import os
import pytz
import subprocess
import sys
import tarfile
import tempfile
import traceback
import uuid
from urllib.parse import urlparse, urlunparse
from django.conf import settings
from django.template import loader, Context
from bs4 import BeautifulSoup
from core import models

logger = logging.getLogger('django')

def change_image_links(html, job_id):
    logger.info("Replacing relative image links with S3 signed urls")
    s3 = boto3.client("s3")
    bucket = "tlapp"

    soup = BeautifulSoup(html, "html.parser")
    img_tags = soup.find_all("img")

    for tag in img_tags:

        src = tag.get("src")

        # Some Rmd documents embed images directly in the html with
        # data attributes.
        if src.startswith("data:"):
            continue

        key = job_id + "/" + src
        url = s3.generate_presigned_url(
            "get_object",
            Params={"Bucket": bucket, "Key": key},
            ExpiresIn=60 * 60 * 24 * 30,
        )

        tag["src"] = url

    return soup.prettify()

def post_process_outputs(job):

    s3 = boto3.resource("s3")

    parts = urlparse(job.output_url)
    output_zip_key = parts.path[1:]  # Remove leading slash

    output_path = "/tmp/%s" % output_zip_key
    logger.info("Downloading job outputs to %s" % output_path)

    s3.Object("tlapp", output_zip_key).download_file(output_path)

    tar = tarfile.open(output_path)
    tar.extractall(path="/tmp")
    tar.close()

    # Upload all the files unzipped
    output_dirname = output_zip_key[:-7]
    f = []
    k = []
    output_path = os.path.join("/tmp", output_dirname)
    for (dirpath, dirnames, filenames) in os.walk(output_path):
        keypath = dirpath[len("/tmp/") :]
        keys = [os.path.join(keypath, name) for name in filenames]
        filenames = [os.path.join(dirpath, name) for name in filenames]
        f.extend(filenames)
        k.extend(keys)

    logger.info("Files to upload to s3:")
    logger.info(zip(f, k))

    for filename, key in zip(f, k):
        s3.Object("tlapp", key).upload_file(filename)

    report_path = os.path.join(output_path, "REPORT.html")
    if os.path.exists(report_path):
        logger.info("Found a report")
        with open(report_path) as f:
            job.report_html = f.read()
            job.report_html = change_image_links(job.report_html, output_dirname)
    else:
        logger.info("Didn't find a report")

def pick_stalled_job():
    five_minutes_ago = datetime.datetime.utcnow() - timedelta(minutes=5)
    job = (
        models.ModelRun.objects.filter(
            status=models.ModelRun.status_choices["postprocessing"],
            postprocessing_attempted_at__lte=five_minutes_ago
        )
        .order_by("?")
        .first()
    )
    return job

def pick_random_job():
    job = (
        models.ModelRun.objects.filter(
            status=models.ModelRun.status_choices["executed"]
        )
        .order_by("?")
        .first()
    )

    if job is None:
        job = pick_stalled_job()

    return job

def handle_a_job():
    job = pick_random_job()

    if job is None:
        return 0

    if job.postprocessing_attempts >= 2:
        job.status = models.ModelRun.status_choices["error"]
        job.postprocessing_traceback = "Maximum postprocessing attempts reached."
        job.save()
        return 1

    job.status = models.ModelRun.status_choices["postprocessing"]
    job.postprocessing_attempted_at = datetime.datetime.utcnow().replace(tzinfo=pytz.utc)
    job.postprocessing_attempts = job.postprocessing_attempts + 1
    job.save()

    try:
        post_process_outputs(job)
        job.status = models.ModelRun.status_choices["success"]
    except:
        traceback.print_exc()
        job.postprocessing_traceback = traceback.format_exc()
        job.status = models.ModelRun.status_choices["error"]
    finally:
        job.save()

    return 1


'''
Unit tests for API App

Author: Marina Dolokov
Date: February 2022

'''
from fastapi.testclient import TestClient

# Import our app from main.py.
from main import app

# Instantiate the testing client with our app.
client = TestClient(app)

# Write tests using the same syntax as with the requests module.
def test_get():
    r = client.get("/")
    assert r.status_code == 200
    assert r.json() == {"message": "Welcome to the census classification API"}

def test_post():
    r = client.post("/items/")
    assert r.status_code == 422

def test_post_prediction_more50():
    r = client.post("/items/",
                    json = {"age": 42,
                            "workclass": "Self-emp-not-inc",
                            "education": "HS-grad",
                            "marital_status": "Married-civ-spouse",
                            "occupation": "Farming-fishing",
                            "relationship": "Husband",
                            "race": "Asian-Pac-Islander",
                            "sex": "Male",
                            "capital_gain": 0,
                            "capital_loss": 0,
                            "hours_per_week": 40,
                            "native_country": "Cambodia"
                            }
                    )
    assert r.json() == {"Predicted salary": ">50k"}
    assert r.status_code == 200

def test_post_prediction_less50():
    r = client.post("/items/",
                    json = {"age": 50,
                            "workclass": "Self-emp-not-inc",
                            "education": "Bachelors",
                            "marital_status": "Married-civ-spouse",
                            "occupation": "Exec-managerial",
                            "relationship": "Husband",
                            "race": "White",
                            "sex": "Male",
                            "capital_gain": 0,
                            "capital_loss": 0,
                            "hours_per_week": 13,
                            "native_country": "United-States"
                            }
                    )
    assert r.json() == {"Predicted salary": "<=50k"}
    assert r.status_code == 200


from tkinter import filedialog
import datetime
from enum import Enum
import sys
import os
import traceback

class File_Type(Enum):
    PROJ_FILE, PARAM_FILE, DATA_FILE, ALG_FILE, CONFIG_FILE = range(5)

class file_module:
    def __init__(self, heide):
        self.heide = heide
        self.config_file = None
        self.proj_file = None
        self.param_file = None
        self.data_file = None
        self.alg_file = None

    def get_existing_loc(self, entry):
        entry.insert(0, filedialog.askopenfilename())

    def read_file(self, file_name, file_type):
        # 1. open file "file_name"
        # 2. read file
        # 3. set internal variable to keep track of open files
        try:
            file = open(file_name)
            contents = file.read()

            if file_type == File_Type.CONFIG_FILE:
                self.config_file = file_name
            elif file_type == File_Type.PROJ_FILE:
                self.proj_file = file_name
            elif file_type == File_Type.PARAM_FILE:
                self.param_file = file_name
            elif file_type == File_Type.DATA_FILE:
                self.data_file = file_name
            elif file_type == File_Type.ALG_FILE:
                self.alg_file = file_name
            else:
                error = ["Unexpected Error",
                         "Invalid File_Type",
                         ''.join(traceback.format_stack())]
                self.heide.gui_mod.display_error(error[0], error[1])
                self.write_to_error_log_internal(error)
                return ""

            return contents
        except:
            error = sys.exc_info()
            self.heide.gui_mod.display_error(error[0], error[1])
            self.write_to_error_log_internal(error)
            return ""

    def write_file(self, file_name, contents):
        try:
            file = open(file_name, "w")
            file.write(contents)
        except:
            error = sys.exc_info()
            self.heide.gui_mod.display_error(error[0], error[1])
            self.write_to_error_log_internal(error)
            return

    # used when writing errors that occured during a subprocess execution
    def write_to_error_log_subprocess(self, out, err):
        self.write_to_error_log("SUBPROCESS ERROR\n" +
                        "\nSTDOUT: \n" +
                        out.decode("utf-8") +
                        "\nSTDERR: \n" +
                        err.decode("utf-8"))

    # used for errors that occurred internally to heide
    def write_to_error_log_internal(self, error):
        self.write_to_error_log("INTERNAL ERROR\n" +
                                str(error[0]) + "\n" +
                                str(error[1]) + "\n" +
                                str(error[2]) + "\n")

    def write_to_error_log(self, message):
        now = datetime.datetime.now()

        try:
            error_log = open("./logs/error_log", "a")
            error_log.write("-" * 80 + "\n" +
                            str(now) + "\n" +
                            message)
            error_log.close()
        except:
            self.heide.gui_mod.display_error("File Write Error",
                                             "Error while writing to "
                                             "error log.\n" +
                                             str(sys.exc_info()[0]) + "\n")
            self.heide.gui_mod.root.quit()
            sys.exit()

    def open_file_proj(self):
        return self.open_read_file((("HEIDE proj files", "*.heide_proj"),
                               ("All Files", "*")), File_Type.PROJ_FILE)

    def open_file_param(self):
        return self.open_read_file((("HEIDE param files", "*.heide_params"),
                               ("All Files", "*")), File_Type.PARAM_FILE)

    def open_file_data(self):
        return self.open_read_file((("HEIDE data file", "*.heide_data"),
                               ("All Files", "*")), File_Type.ALG_FILE)

    def open_file_alg(self):
        return self.open_read_file((("HEIDE alg files", "*.heide_alg"),
                               ("All Files", "*")), File_Type.DATA_FILE)

    def open_read_file(self, file_types, file_type):
        file_name = filedialog.askopenfilename(filetypes=file_types)

        # For some reason if the user selects cancel on filedialog, the first
        # time it returns as () but then on subsequent calls it is empty.
        if str(file_name) == "" or str(file_name) == "()":
            return ""
        else:
            return self.read_file(file_name, file_type)

    def save_params(self, contents):
        self.save_to_file(File_Type.PARAM_FILE, self.param_file, contents)

    def save_data(self, contents):
        self.save_to_file(File_Type.DATA_FILE, self.data_file, contents)

    def save_alg(self, contents):
        self.save_to_file(File_Type.ALG_FILE, self.alg_file, contents)

    def save_as_params(self, contents):
        self.save_to_file(File_Type.PARAM_FILE, None, contents)

    def save_as_data(self, contents):
        self.save_to_file(File_Type.DATA_FILE, None, contents)

    def save_as_alg(self, contents):
        self.save_to_file(File_Type.ALG_FILE, None, contents)

    def save_to_file(self, file_type, file_name, contents):
        if file_name == None:
            file_name = filedialog.asksaveasfilename()

            # For some reason if the user selects cancel on filedialog, the first
            # time it returns as () but then on subsequent calls it is empty.
            # TODO look into this more?
            if str(file_name) == "" or str(file_name) == "()":
                return

            if file_type == File_Type.PARAM_FILE:
                self.param_file = file_name
                self.heide.gui_mod.param_custom_font.configure(weight="normal")
                self.heide.gui_mod.param_bold = False
            elif file_type == File_Type.DATA_FILE:
                self.data_file = file_name
                self.heide.gui_mod.data_custom_font.configure(weight="normal")
                self.heide.gui_mod.data_bold = False
            elif file_type == File_Type.ALG_FILE:
                self.alg_file = file_name
                self.heide.gui_mod.alg_custom_font.configure(weight="normal")
                self.heide.gui_mod.alg_bold = False
            else:
                error = ["Unexpected Error",
                         "Invalid File_Type.",
                         ''.join(traceback.format_stack())]
                self.heide.gui_mod.display_error(error[0], error[1])
                self.write_to_error_log_internal(error)
                return

        self.write_file(file_name, contents)

        if file_type == File_Type.PARAM_FILE:
            self.heide.gui_mod.param_custom_font.configure(weight="normal")
            self.heide.edit_mod.param_bold = False
        elif file_type == File_Type.DATA_FILE:
            self.heide.gui_mod.data_custom_font.configure(weight="normal")
            self.heide.edit_mod.data_bold = False
        elif file_type == File_Type.ALG_FILE:
            self.heide.gui_mod.alg_custom_font.configure(weight="normal")
            self.heide.edit_mod.alg_bold = False
        else:
            error = ["Unexpected Error",
                     "Invalid File_Type.",
                     ''.join(traceback.format_stack())]
            self.heide.gui_mod.display_error(error[0], error[1])
            self.write_to_error_log_internal(error)
            return

    def create_proj(self, he_src, proj_loc, proj_name,
                    existing_param, existing_data, existing_alg):

        # format file contents so that it is the string form of the
        # json object which holds the data.
        file_contents = "{\n\t" + self.format_settings_entry(he_src) + \
                        "\n\t\"param_file\": " + \
                        self.format_file_entry(proj_loc, proj_name,
                                          File_Type.PARAM_FILE) + \
                        ",\n\t\"data_file\": " + \
                        self.format_file_entry(proj_loc, proj_name,
                                          File_Type.DATA_FILE) + \
                        ",\n\t\"alg_file\": " + \
                        self.format_file_entry(proj_loc, proj_name,
                                          File_Type.ALG_FILE) + \
                        "\n}"

        try:
            os.mkdir(proj_loc + "/" + proj_name)
        except:
            error = sys.exc_info()
            self.heide.gui_mod.display_error(error[0], error[1])
            return

        base_file_name = proj_loc + "/" + proj_name + "/" + proj_name
        proj_file_name = base_file_name + ".heide_proj"
        params_file_name = base_file_name + ".heide_params"
        data_file_name = base_file_name + ".heide_data"
        alg_file_name = base_file_name + ".heide_alg"

        # write project file and set internal variable to keep track of open
        # file
        self.write_file(proj_file_name, file_contents)
        self.proj_file = proj_file_name

        # for the param, data, and alg file
        #   if importing from an existing file read existing file's contents
        #   and then write to project file
        # set internal variable to keep track of open file
        if existing_param != "":
            contents = self.read_file(existing_param, File_Type.PARAM_FILE)
        else:
            contents = "pd = {\t\"p\" : 65537,\n" \
                       "\t\"r\" : 1, \n" \
                       "\t\"L\" : 15, \n" \
                       "\t\"w\" : 64, \n" \
                       "\t\"d\" : 0, \n" \
                       "\t\"security\" : 128} \n" \
                       "\n%+pd\n"

        self.write_file(params_file_name, contents)
        self.param_file = params_file_name

        if existing_data != "":
            contents = self.read_file(existing_data, File_Type.DATA_FILE)
        else:
            contents = ""

        self.write_file(data_file_name, contents)
        self.data_file = data_file_name

        if existing_alg != "":
            contents = self.read_file(existing_alg, File_Type.ALG_FILE)
        else:
            contents = ""

        self.write_file(alg_file_name, contents)
        self.alg_file = alg_file_name

    def format_settings_entry(self, he_src):
        return "\"settings\": {\n\t\t\"he_src\": \"" + he_src + "\"\n\t},"

    def format_file_entry(self, proj_loc, proj_name, file_type):
        if file_type == File_Type.PARAM_FILE:
            return "\"" + proj_loc + "/" + proj_name + "/" + proj_name + \
                   ".heide_params\""
        elif file_type == File_Type.DATA_FILE:
            return "\"" + proj_loc + "/" + proj_name + "/" + proj_name + \
                   ".heide_data\""
        elif file_type == File_Type.ALG_FILE:
            return "\"" + proj_loc + "/" + proj_name + "/" + proj_name + \
                   ".heide_alg\""
        else:
            error = ["Unexpected Error",
                     "Invalid File_Type.",
                     ''.join(traceback.format_stack())]
            self.heide.gui_mod.display_error(error[0], error[1])
            self.write_to_error_log_internal(error)
            return

    def clear_files(self):
        self.proj_file = None
        self.param_file = None
        self.data_file = None
        self.param_file = None


# coding=UTF-8
import logging
from body.model import create_good_from_list, show_the_entire_list, max_min_goods_on_the_list
from body.model import delete_goods_from_list, db_in_json, clean_db
import body.view_file as view

logger = logging.getLogger(f'main.{__name__}')

class Controllers:
    """Single controller class."""

    @staticmethod
    def primary_data():
        """Entering initial data into the database."""
        for row in view.ViewForGood().data_from_file():
            create_good_from_list(row)

        logger.info('The data has been entered into the database.')

    @staticmethod
    def show_main_window():
        """The function of obtaining the initial data file for the database and launching the main user window."""
        logger.debug('Funktion show_main_window is worked')
        view.ViewForGood().data_from_list()
        view.ViewForGood().start_main_window()

    @staticmethod
    def create_user_good(data):
        """A function that creates products from a list."""
        logger.debug('Function create_user_good is worked')
        return create_good_from_list(data)

    @staticmethod
    def result_list():
        """The function of passing the database content sheet to the output in Tkinter"""
        return show_the_entire_list()

    @staticmethod
    def del_data(data):
        """Function to delete data by name."""
        deld = delete_goods_from_list(data)
        if deld:
            logger.debug(f'del_data is worked. Product "{data}" removed from the list')
            return deld

    @staticmethod
    def max_min_func():
        """Function of initial processing of max and min values."""
        data = max_min_goods_on_the_list()
        if data:
            return data
        return False

    @staticmethod
    def clear_db_func():
        """Function of completely deleting data from the database."""
        if show_the_entire_list():
            clean_db()
            logger.info('DB is cleaned')
            return True
        else:
            return False

    @staticmethod
    def json_output():
        """Function of outputting data from the database to a file in JSON format"""
        data = db_in_json()
        if data:
            logger.info(f'The database is written in JSON format to the file "{data}"')
            return data
        else:
            logger.info('DB is empty, unsuccessful attempt to create JSON file')
            return False


#!/usr/bin/python3

import smtplib
from email.mime.text import MIMEText

def send_email(subject, message, from_addr, *to_addrs, host="localhost", port=1025, **headers):
  email = MIMEText(message)
  email['Subject'] = subject
  email['From'] = from_addr
  for header, value in headers.items():
    email[header] = value

  sender = smtplib.SMTP(host, port)
  for addr in to_addrs:
    del email['To']
    email['To'] = addr
    sender.sendmail(from_addr, addr, email.as_string())
  sender.quit()

if __name__ == "__main__":
  # headers = {}
  send_email("A model subject", "The message contents", "from@example.com", "to1@example.com", "to2@example.com", "Reply-To", "me2@example.com")


from __future__ import print_function, absolute_import, division
from future.builtins import *
from future import standard_library
standard_library.install_aliases()

# Copyright 2017 Autodesk Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import json

import moldesign as mdt
from .. import units as u

PACKAGEPATH = os.path.abspath(os.path.dirname(mdt.__file__))

class CyclicList(list):
    def __getitem__(self, item):
        return super().__getitem__(item % len(self))

COLOR_LIST = ['lightgreen', 'lightblue', 'lightgrey',
              'yellow', 'orange', 'purple', 'IndianRed',
              'PaleTurquoise', 'OldLace', 'Thistle', 'pink']

DEFAULT_FORCE_TOLERANCE = (0.0001 * u.hartree / u.bohr).defunits()  # taken from GAMESS OPTTOL keyword

# Precompute odd factorial values (N!!)
ODD_FACTORIAL = {0: 1}  # by convention
_ofact = 1
for _i in range(1, 20, 2):
    _ofact *= _i
    ODD_FACTORIAL[_i] = float(_ofact)

def print_environment():
    """For reporting bugs - spits out the user's environment"""
    import sys
    version = {}
    for pkg in 'moldesign IPython ipywidgets jupyter matplotlib numpy docker pyccc distutils' \
               'nbmolviz jupyter_client jupyter_core pint Bio openbabel simtk pyscf pip setuptools'\
            .split():
        try:
            module = __import__(pkg)
        except ImportError as e:
            version[pkg] = str(e)
        else:
            try:
                version[pkg] = module.__version__
            except AttributeError as e:
                version[pkg] = str(e)
    env = {'platform': sys.platform,
           'version': sys.version,
           'prefix': sys.prefix}

    try:
        import platform
        env['machine'] = platform.machine()
        env['linux'] = platform.linux_distribution()
        env['mac'] = platform.mac_ver()
        env['windows'] = platform.win32_ver()
        env['impl'] = platform.python_implementation()
        env['arch'] = platform.architecture()
        env['system'] = platform.system()
        env['python_build'] = platform.python_build()
        env['platform_version'] = platform.version()

    except Exception as e:
        env['platform_exception'] = str(e)

    print(json.dumps({'env': env,
                      'versions': version}))


import sys
import json
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as md
import datetime

# ==========================
# *** Reading JSON files ***

stchoo = int(sys.argv[1])

#monthUb = ['aug', 'sep', 'oct', 'nov']
monthUub = ['aug', 'sep', 'oct', 'nov', 'dec', 'jan', 'feb', 'mar', 'abr']

basenameUub = "../../readMonit/hv"+str(stchoo)
outnameUub = "../../plots/uubHVSt"+str(stchoo)+".png"

# ===============================
# *** For Peak HBase Plotting ***

hvdataUub = [[], [], [], []]# time, pmt1, pmt2, pmt3
for mth in monthUub:
  tmpdata = np.loadtxt( basenameUub+str(mth)+".dat" )
  for info in tmpdata:
    hvdataUub[0].append( datetime.datetime.fromtimestamp( info[0] ) )
    hvdataUub[1].append( info[1] )
    hvdataUub[2].append( info[2] )
    hvdataUub[3].append( info[3] )

fig = plt.figure(figsize=(16,9))

markers = ['o', 's', '^']
ax=plt.gca()
xfmt = md.DateFormatter('%m/%d')
ax.xaxis.set_major_formatter(xfmt)
mkrSize = 6

ymin = 9e2
ymax = 2.75e3

plt.plot(hvdataUub[0], hvdataUub[1], markers[0], markersize=mkrSize, label="UUB HV PMT1") #, color='tab:blue'
plt.plot(hvdataUub[0], hvdataUub[2], markers[1], markersize=mkrSize, label="UUB HV PMT2") #, color='tab:blue'
plt.plot(hvdataUub[0], hvdataUub[3], markers[2], markersize=mkrSize, label="UUB HV PMT3") #, color='tab:blue'

plt.legend(fontsize=22)
#plt.ylim(ymin, ymax)
plt.ylabel("Station "+str(stchoo)+" HV [V]", fontsize=22)
plt.xlabel("Time since August 1st, 2020 [month/day]", fontsize=22)
plt.xticks(fontsize=22)
plt.yticks(fontsize=22)

plt.tight_layout()
#plt.show()
plt.savefig(outnameUub, dpi=100)
print("Done for HV over time, ST. "+str(stchoo))


import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt

#Angehangenes Gewicht
Masse = 1900

#Kraft auf den Stab
f=Masse*9.81

# Datenfile erzeugen
Abstand, mit_Gewicht, ohne_Gewicht = np.genfromtxt('Einseitig_Eckig.txt',unpack=True)

Abstand /=1000

#Durchbiegung des Stabes
durchbiegung = mit_Gewicht - ohne_Gewicht

durchbiegung /= 1000

print ('Ausschlag ohne Gewicht', ohne_Gewicht)

print('')
print('Ausschlag mit Gewicht', mit_Gewicht)

print('')
print ('Durchbiegung: ', durchbiegung)

print('')
print (Abstand)

def EModul(L, x):
	return (L*(x**2)-x**3/3)

#erstellt Ausgleichsgerade
def func(m, x, b):
	return  m * x + b

#dies dient zum testen der curve_fit Funktion
BspEM = EModul(0.51 ,Abstand)

parameters, pcov = curve_fit(func, durchbiegung, BspEM)

print('')
print ('L*x**2-x**3/3: ', BspEM)

d=0
for x in Abstand:
	print(d)
	print('Abstand: ',Abstand[d],' Auslenkung o Ge: ', ohne_Gewicht[d], ' Auslenkung m Ge: ',  mit_Gewicht[d], ' Differenz m-o: ', durchbiegung[d], ' f(x): ', BspEM[d] )
	d +=1

print('')
print (parameters)
#plt.errorbar(durchbiegung, BspEM, xerr=0.1, yerr=0.01, fmt='x')
plt.plot(durchbiegung, BspEM, 'x')
plt.plot(durchbiegung, func(durchbiegung, *parameters))
plt.xlabel(r'Durchbiegung in [1/m]')
plt.ylabel(r'Lx² - x³/3in [1/m]')
plt.show()


from typing import List
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        dictionary={}
        for i,a in enumerate(nums):
           if target-a in dictionary:
               return [dictionary[target-a],i]
           else:
               dictionary[a]=i

        return None

nums=[2, 7, 11, 15]
target= 9
print(Solution().twoSum(nums,target))

哈希表/1. 两数之和.py


import openai
import sys

# save your OpenAI API key to this file
openai.api_key_path ='.Renviron'

'''
Send in the chat history and user's new input, ChatGPT gives you a response
***Input***:
- chat_history, a list of dictionary. Check https://platform.openai.com/docs/guides/chat
- user_input, a string. User's new input
- print_conversation, bool default to false. If true, print conversation in terminal
***Return***
- chat_history, a list of dictionary. It is extended based on the inputted chat_history, but with user's new input and ChatGPT's new response appended
- gpt_msg, a string, ChatGPT's new response
- total token used: a measurement of cost
'''
def chat_with_gpt(chat_history, user_input, print_conversation=False):
    suffix_phrase = ' Answer me using one sentence.'

    chat_history.append({'role': 'user', 'content': user_input + suffix_phrase})

    response = openai.ChatCompletion.create(
        model = 'gpt-3.5-turbo',
        messages=chat_history,
        temperature = 0.2
    )
    chat_history[-1] = {'role': 'user', 'content': user_input}

    gpt_msg = response['choices'][0]['message']['content']
    chat_history.append({'role': 'system', 'content': gpt_msg})

    if print_conversation:
        print(f'User:\n{user_input}')
        print(f'ChatGPT:\n{gpt_msg}')

    return chat_history, gpt_msg, response['usage']['total_tokens']

'''
This function will setup the beginning of a chat scenario
Input:
- scenario_idx: int
Output:
- a list of dictionary (in this case, there is only one element in the list). The list contains the chat history. This chat history should be fed into the chat_with_gpt function
'''
def setup_chat(scenario_idx=0):
    scenarios = [
        'You will be my Economy professor. I am in your office hour.',
        'You are my roommate. We are doing grocery shopping in Trader Joe\'s.',
        'You are my boyfriend. We are having a date in a restaurant.'
        ]

    return [{'role': 'user', 'content':scenarios[scenario_idx]}]

'''
This function corrects the grammar mistake using GPT3
Input:
- content: the sentence that wants to be checked
Output:
- the sentence with correct grammar
'''
def grammar_check(content):
    prefix_phrase = 'Correct the following sentence\'s grammar: '

    response = openai.Completion.create(
        model = 'text-curie-001',
        prompt = prefix_phrase + content,
        temperature=0
    )

    return response['choices'][0]['text']

if __name__ == '__main__':
    chat_history = setup_chat(2)
    total_tokens = 0

    while True:
        user_input = input('Enter...')

        if user_input == 'exit()':
            break
        else:
            chat_history, gpt_msg, token_used = chat_with_gpt(chat_history, user_input, print_conversation=True)
            total_tokens += token_used

    print(f'Token used in this conversation = {token_used}')


#!/usr/bin/python3
""" a script to add Louisiana to the db
"""
from model_state import Base, State
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
import sys

if __name__ == "__main__":
    connection = create_engine('mysql+mysqldb://{}:{}@localhost:3306/{}'
                           .format(sys.argv[1], sys.argv[2], sys.argv[3]))
    Base.metadata.create_all(connection)
    Session = sessionmaker(bind=connection)
    session = Session()
    new_state = State(name='Louisiana')
    session.add(new_state)
    new_instance = session.query(State).filter_by(name='Louisiana').first()
    print(new_instance.id)
    session.commit()


# Face Detection
import cv2

# ອ່ານວິດີໂອ
cap = cv2.VideoCapture('images/Mark.mp4')

# ອ່ານ File xml ສຳຫຼັບ Classification
face_cascade = cv2.CascadeClassifier('detect/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('detect/haarcascade_eye_tree_eyeglasses.xml')

# ສະແດງຜົນວິດີໂອ
while (cap.isOpened()):
    check, frame = cap.read()
    if check == True:
        gray_vdo = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        # ຈຳແນກດວງຕາ
        scaleFactory = 1.2
        minNeighber = 5
        # ກວດຈັບໃບໜ້າ
        face_detect = face_cascade.detectMultiScale(gray_vdo, scaleFactory, minNeighber)
        # ກວດຈັບດວງຕາ
        eye_detect = eye_cascade.detectMultiScale(gray_vdo, scaleFactory, minNeighber)
        # ສະແດງຕຳແໜ່ງທີ່ພົບເຫັນດວງຕາ
        for (ex, ey, ew, eh) in face_detect:
            cv2.rectangle(frame, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), thickness=5)
            for (fx, fy, fw, fh) in eye_detect:
                cv2.rectangle(frame, (fx, fy), (fx+fw, fy+fh), (0, 0, 255), thickness=5)

        cv2.imshow('Output', frame)
        if cv2.waitKey(5) & 0xFF == ord('e'):
            break
    else:
        break

cap.release()
cv2.destroyAllWindows()


import urllib.request
import json
import logging as lg

"""
Used to make the (undocumented) api call to finance.yahoo.com
to retrieve the current option prices for the specified stock
trading symbol.

Returns the object containing the option prices for the day for
that security with the specified option expiration date and all strike prices.
"""

def retrieve_option_expiration_dates(symbol):
    try:
        urlData = "https://query2.finance.yahoo.com/v7/finance/options/" + symbol
        webURL = urllib.request.urlopen(urlData)
        data = webURL.read()
        encoding = webURL.info().get_content_charset('utf-8')
        jsonObject = json.loads(data.decode(encoding))
        return jsonObject
    except Exception as err:
        lg.error('Error retrieving expiration dates for symbol ' + symbol)

def retrieve_option_prices_by_expiration_date(symbol, expDate):
    try:
        urlData = "https://query2.finance.yahoo.com/v7/finance/options/" + symbol + "?date=" + str(expDate)
        webURL = urllib.request.urlopen(urlData)
        data = webURL.read()
        encoding = webURL.info().get_content_charset('utf-8')
        jsonObject = json.loads(data.decode(encoding))
        return jsonObject
    except Exception as err:
        lg.error('Error retrieving expiration dates for symbol ' + symbol)


# =================================================================
# =                  Author: Brad Heffernan                       =
# =================================================================

import os
import distro
import sys
import shutil
import psutil
import datetime
# import time
import subprocess
import threading  # noqa
import gi
# import configparser
gi.require_version('Gtk', '3.0')
from gi.repository import GLib, Gtk  # noqa

distr = distro.id()

sudo_username = os.getlogin()
home = "/home/" + str(sudo_username)

sddm_default = "/etc/sddm.conf"
sddm_default_original = "/usr/local/share/arcolinux/sddm/sddm.conf"

sddm_default_d1 = "/etc/sddm.conf"
sddm_default_d2 = "/etc/sddm.conf.d/kde_settings.conf"
sddm_default_d2_dir = "/etc/sddm.conf.d/"
sddm_default_d_sddm_original_1 = "/usr/local/share/arcolinux/sddm.conf.d/sddm.conf"
sddm_default_d_sddm_original_2 = "/usr/local/share/arcolinux/sddm.conf.d/kde_settings.conf"

if os.path.exists("/etc/sddm.conf.d/kde_settings.conf"):
    sddm_conf = "/etc/sddm.conf.d/kde_settings.conf"
else:
    sddm_conf = "/etc/sddm.conf"

arcolinux_mirrorlist = "/etc/pacman.d/arcolinux-mirrorlist"
arcolinux_mirrorlist_original = "/usr/local/share/arcolinux/arcolinux-mirrorlist"
pacman = "/etc/pacman.conf"
pacman_arco ="/usr/share/arcolinux-tweak-tool/data/arco/pacman.conf"
pacman_eos ="/usr/share/arcolinux-tweak-tool/data/eos/pacman.conf"
pacman_garuda ="/usr/share/arcolinux-tweak-tool/data/garuda/pacman.conf"
blank_pacman_arco ="/usr/share/arcolinux-tweak-tool/data/arco/blank/pacman.conf"
blank_pacman_eos ="/usr/share/arcolinux-tweak-tool/data/eos/blank/pacman.conf"
blank_pacman_garuda ="/usr/share/arcolinux-tweak-tool/data/garuda/blank/pacman.conf"
oblogout_conf = "/etc/oblogout.conf"
# oblogout_conf = home + "/oblogout.conf"
gtk3_settings = home + "/.config/gtk-3.0/settings.ini"
gtk2_settings = home + "/.gtkrc-2.0"
grub_theme_conf = "/boot/grub/themes/Vimix/theme.txt"
xfce_config = home + "/.config/xfce4/xfconf/xfce-perchannel-xml/xsettings.xml"
slimlock_conf = "/etc/slim.conf"
termite_config = home + "/.config/termite/config"
neofetch_config = home + "/.config/neofetch/config.conf"
lightdm_conf = "/etc/lightdm/lightdm.conf"
bd = ".att_backups"
config = home + "/.config/arcolinux-tweak-tool/settings.ini"
config_dir = home + "/.config/arcolinux-tweak-tool/"
polybar = home + "/.config/polybar/"
desktop = ""
autostart = home + "/.config/autostart/"
zsh_config = ""
fish_config = ""
if os.path.isfile(home + "/.config/fish/config.fish"):
    fish_config = home + "/.config/fish/config.fish"
if os.path.isfile(home + "/.zshrc"):
    zsh_config = home + "/.zshrc"
bash_config = ""
if os.path.isfile(home + "/.bashrc"):
    bash_config = home + "/.bashrc"
account_list = ["Standard","Administrator"]
i3wm_config = home + "/.config/i3/config"
awesome_config = home + "/.config/awesome/rc.lua"
qtile_config = home + "/.config/qtile/config.py"

seedhostmirror = "Server = https://ant.seedhost.eu/arcolinux/$repo/$arch"
aarnetmirror = "Server = https://mirror.aarnet.edu.au/pub/arcolinux/$repo/$arch"

atestrepo = "[arcolinux_repo_testing]\n\
SigLevel = Required DatabaseOptional\n\
Include = /etc/pacman.d/arcolinux-mirrorlist"

arepo = "[arcolinux_repo]\n\
SigLevel = Required DatabaseOptional\n\
Include = /etc/pacman.d/arcolinux-mirrorlist"

a3drepo = "[arcolinux_repo_3party]\n\
SigLevel = Required DatabaseOptional\n\
Include = /etc/pacman.d/arcolinux-mirrorlist"

axlrepo = "[arcolinux_repo_xlarge]\n\
SigLevel = Required DatabaseOptional\n\
Include = /etc/pacman.d/arcolinux-mirrorlist"

chaotics_repo = "[chaotic-aur]\n\
SigLevel = Required DatabaseOptional\n\
Include = /etc/pacman.d/chaotic-mirrorlist"

endeavouros_repo = "[endeavouros]\n\
SigLevel = PackageRequired\n\
Include = /etc/pacman.d/endeavouros-mirrorlist"

nemesis_repo = "[nemesis_repo]\n\
SigLevel = Optional TrustedOnly\n\
Server = https://erikdubois.github.io/$repo/$arch"

arch_testing_repo = "[testing]\n\
Include = /etc/pacman.d/mirrorlist"

arch_core_repo = "[core]\n\
Include = /etc/pacman.d/mirrorlist"

arch_extra_repo = "[extra]\n\
Include = /etc/pacman.d/mirrorlist"

arch_community_testing_repo = "[community-testing]\n\
Include = /etc/pacman.d/mirrorlist"

arch_community_repo = "[community]\n\
Include = /etc/pacman.d/mirrorlist"

arch_multilib_testing_repo = "[multilib-testing]\n\
Include = /etc/pacman.d/mirrorlist"

arch_multilib_repo = "[multilib]\n\
Include = /etc/pacman.d/mirrorlist"

# =====================================================
#               Create log file
# =====================================================

log_dir="/var/log/arcolinux/"
att_log_dir="/var/log/arcolinux/att/"

def create_log(self):
    print('Making log in /var/log/arcolinux')
    now = datetime.datetime.now()
    time = now.strftime("%Y-%m-%d-%H-%M-%S" )
    destination = att_log_dir + 'att-log-' + time
    command = 'sudo pacman -Q > ' + destination
    subprocess.call(command,
                    shell=True,
                    stdout=subprocess.PIPE,
                    stderr=subprocess.STDOUT)
    #GLib.idle_add(show_in_app_notification, self, "Log file created")

# =====================================================
#               NOTIFICATIONS
# =====================================================

def show_in_app_notification(self, message):
    if self.timeout_id is not None:
        GLib.source_remove(self.timeout_id)
        self.timeout_id = None

    self.notification_label.set_markup("" +
                                       message + "")
    self.notification_revealer.set_reveal_child(True)
    self.timeout_id = GLib.timeout_add(3000, timeOut, self)

def timeOut(self):
    close_in_app_notification(self)

def close_in_app_notification(self):
    self.notification_revealer.set_reveal_child(False)
    GLib.source_remove(self.timeout_id)
    self.timeout_id = None

# =====================================================
#               PERMISSIONS
# =====================================================

def test(dst):
    for root, dirs, filesr in os.walk(dst):
        # print(root)
        for folder in dirs:
            pass
            # print(dst + "/" + folder)
            for file in filesr:
                pass
                # print(dst + "/" + folder + "/" + file)
        for file in filesr:
            pass
            # print(dst + "/" + file)

def permissions(dst):
    try:
        # original_umask = os.umask(0)
        # calls = subprocess.run(["sh", "-c", "cat /etc/passwd | grep " +
        #                         sudo_username],
        #                        shell=False,
        #                        stdout=subprocess.PIPE,
        #                        stderr=subprocess.STDOUT)
        groups = subprocess.run(["sh", "-c", "id " +
                                 sudo_username],
                                shell=False,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.STDOUT)
        for x in groups.stdout.decode().split(" "):
            if "gid" in x:
                g = x.split("(")[1]
                group = g.replace(")", "").strip()
        # print(group)
        # name = calls.stdout.decode().split(":")[0].strip()
        # group = calls.stdout.decode().split(":")[4].strip()

        subprocess.call(["chown", "-R",
                         sudo_username + ":" + group, dst], shell=False)

    except Exception as e:
        print(e)
# =====================================================
#               COPY FUNCTION
# =====================================================

def copy_func(src, dst, isdir=False):
    if isdir:
        subprocess.run(["cp", "-Rp", src, dst], shell=False)
    else:
        subprocess.run(["cp", "-p", src, dst], shell=False)
    # permissions(dst)

# =====================================================
#               SOURCE
# =====================================================

def source_shell(self):
    process = subprocess.run(["sh", "-c", "echo \"$SHELL\""],
                             stdout=subprocess.PIPE)

    output = process.stdout.decode().strip()
    print(output)
    if output == "/bin/bash":
        subprocess.run(["bash", "-c", "su - " + sudo_username +
                        " -c \"source " + home + "/.bashrc\""],
                       stdout=subprocess.PIPE)
    elif output == "/bin/zsh":
        subprocess.run(["zsh", "-c", "su - " + sudo_username +
                        " -c \"source " + home + "/.zshrc\""],
                       stdout=subprocess.PIPE)
    elif output == "/usr/bin/fish":
        subprocess.run(["fish", "-c", "su - " + sudo_username +
                        " -c \"source " + home + "/.config/fish/config.fish\""],
                       stdout=subprocess.PIPE)

def get_shell():
    process = subprocess.run(["su", "-", sudo_username,"-c","echo \"$SHELL\""],
                             #shell=False,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.STDOUT)

    output = process.stdout.decode().strip().strip('\n')
    if output == "/bin/bash" or output == "/usr/bin/bash":
        return "bash"
    elif output == "/bin/zsh" or output == "/usr/bin/zsh":
        return "zsh"
    elif output == "/bin/fish" or output == "/usr/bin/fish":
        return "fish"

def run_as_user(script):
    subprocess.call(["su - " + sudo_username + " -c " + script], shell=False)

# =====================================================
#               MESSAGEBOX
# =====================================================

def MessageBox(self, title, message):
    md2 = Gtk.MessageDialog(parent=self,
                            flags=0,
                            message_type=Gtk.MessageType.INFO,
                            buttons=Gtk.ButtonsType.OK,
                            text=title)
    md2.format_secondary_markup(message)
    md2.run()
    md2.destroy()

# =====================================================
#               CONVERT COLOR
# =====================================================

def rgb_to_hex(rgb):
    if "rgb" in rgb:
        rgb = rgb.replace("rgb(", "").replace(")", "")
        vals = rgb.split(",")
        return "#{0:02x}{1:02x}{2:02x}".format(clamp(int(vals[0])),
                                               clamp(int(vals[1])),
                                               clamp(int(vals[2])))
    return rgb

def clamp(x):
    return max(0, min(x, 255))

# =====================================================
#               GLOBAL FUNCTIONS
# =====================================================

def _get_position(lists, value):
    data = [string for string in lists if value in string]
    position = lists.index(data[0])
    return position

def _get_positions(lists, value):
    data = [string for string in lists if value in string]
    position = []
    for d in data:
        position.append(lists.index(d))
    return position

def _get_variable(lists, value):
    data = [string for string in lists if value in string]

    if len(data) >= 1:

        data1 = [string for string in data if "#" in string]

        for i in data1:
            if i[:4].find('#') != -1:
                data.remove(i)
    if data:
        data_clean = [data[0].strip('\n').replace(" ", "")][0].split("=")
    return data_clean

# Check  value exists

def check_value(list, value):
    data = [string for string in list if value in string]
    if len(data) >= 1:
        data1 = [string for string in data if "#" in string]
        for i in data1:
            if i[:4].find('#') != -1:
                data.remove(i)
    return data

def check_backups(now):
    if not os.path.exists(home + "/" + bd + "/Backup-" +
                          now.strftime("%Y-%m-%d %H")):
        os.makedirs(home + "/" + bd + "/Backup-" +
                    now.strftime("%Y-%m-%d %H"), 0o777)
        permissions(home + "/" + bd + "/Backup-" + now.strftime("%Y-%m-%d %H"))

# =====================================================
#               Check if File Exists
# =====================================================

def file_check(file):
    if os.path.isfile(file):
        return True

    return False

def path_check(path):
    if os.path.isdir(path):
        return True

    return False

# =====================================================
#               GTK3 CONF
# =====================================================

def gtk_check_value(my_list, value):
    data = [string for string in my_list if value in string]
    if len(data) >= 1:
        data1 = [string for string in data if "#" in string]
        for i in data1:
            if i[:4].find('#') != -1:
                data.remove(i)
    return data

def gtk_get_position(my_list, value):
    data = [string for string in my_list if value in string]
    position = my_list.index(data[0])
    return position

# =====================================================
#               OBLOGOUT CONF
# =====================================================
# Get shortcuts index

def get_shortcuts(conflist):
    sortcuts = _get_variable(conflist, "shortcuts")
    shortcuts_index = _get_position(conflist, sortcuts[0])
    return int(shortcuts_index)

# Get commands index

def get_commands(conflist):
    commands = _get_variable(conflist, "commands")
    commands_index = _get_position(conflist, commands[0])
    return int(commands_index)

# =====================================================
#               LIGHTDM CONF
# =====================================================

def check_lightdm_value(list, value):
    data = [string for string in list if value in string]
    # if len(data) >= 1:
    #     data1 = [string for string in data if "#" in string]

    return data

# =====================================================
#               SDDM CONF
# =====================================================

def check_sddm_value(list, value):
    data = [string for string in list if value in string]
    return data

# =====================================================
#               HBLOCK CONF
# =====================================================

def hblock_get_state(self):
    lines = int(subprocess.check_output('wc -l /etc/hosts',
                                        shell=True).strip().split()[0])
    if os.path.exists("/usr/local/bin/hblock") and lines > 100:
        return True

    self.firstrun = False
    return False

def do_pulse(data, prog):
    prog.pulse()
    return True

def set_hblock(self, toggle, state):
    GLib.idle_add(toggle.set_sensitive, False)
    GLib.idle_add(self.label7.set_text, "Run..")
    GLib.idle_add(self.progress.set_fraction, 0.2)

    timeout_id = None
    timeout_id = GLib.timeout_add(100, do_pulse, None, self.progress)

    if not os.path.isfile("/etc/hosts.bak.att"):
            shutil.copy("/etc/hosts", "/etc/hosts.bak.att")

    try:

        install = 'pacman -S arcolinux-hblock-git --needed --noconfirm'
        enable = "/usr/local/bin/hblock"

        if state:
            if os.path.exists("/usr/local/bin/hblock"):
                GLib.idle_add(self.label7.set_text, "Database update...")
                subprocess.call([enable],
                                shell=False,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.STDOUT)
            else:
                GLib.idle_add(self.label7.set_text, "Install Hblock......")
                subprocess.call(install.split(" "),
                                shell=False,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.STDOUT)
                GLib.idle_add(self.label7.set_text, "Database update...")
                subprocess.call([enable],
                                shell=False,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.STDOUT)

        else:
            GLib.idle_add(self.label7.set_text, "Remove update...")
            subprocess.run(["sh", "-c",
                            "HBLOCK_SOURCES=\'\' /usr/local/bin/hblock"],
                           shell=False,
                           stdout=subprocess.PIPE,
                           stderr=subprocess.STDOUT)

        GLib.idle_add(self.label7.set_text, "Complete")
        GLib.source_remove(timeout_id)
        timeout_id = None
        GLib.idle_add(self.progress.set_fraction, 0)

        GLib.idle_add(toggle.set_sensitive, True)
        if state:
            GLib.idle_add(self.label7.set_text, "HBlock Active")
        else:
            GLib.idle_add(self.label7.set_text, "HBlock Inactive")

    except Exception as e:
        MessageBox(self, "ERROR!!", str(e))
        print(e)

# =====================================================
#               UBLOCK ORIGIN
# =====================================================

def ublock_get_state(self):
    if os.path.exists("/usr/lib/firefox/browser/extensions/uBlock0@raymondhill.net.xpi"):
        return True
    return False

def set_firefox_ublock(self, toggle, state):
    GLib.idle_add(toggle.set_sensitive, False)
    GLib.idle_add(self.label7.set_text, "Run..")
    GLib.idle_add(self.progress.set_fraction, 0.2)

    timeout_id = None
    timeout_id = GLib.timeout_add(100, do_pulse, None, self.progress)

    try:

        install_ublock = "pacman -S firefox-ublock-origin --needed --noconfirm"
        uninstall_ublock = 'pacman -Rs firefox-ublock-origin --noconfirm'

        if state:
            GLib.idle_add(self.label7.set_text, "Installing ublock Origin...")
            subprocess.call(install_ublock.split(" "),
                            shell=False,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.STDOUT)
        else:
            GLib.idle_add(self.label7.set_text, "Removing ublock Origin...")
            subprocess.call(uninstall_ublock.split(" "),
                            shell=False,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.STDOUT)

        GLib.idle_add(self.label7.set_text, "Complete")
        GLib.source_remove(timeout_id)
        timeout_id = None
        GLib.idle_add(self.progress.set_fraction, 0)

        GLib.idle_add(toggle.set_sensitive, True)
        if state:
            GLib.idle_add(self.label7.set_text, "uBlock Origin installed")
        else:
            GLib.idle_add(self.label7.set_text, "uBlock Origin removed")

    except Exception as e:
        MessageBox(self, "ERROR!!", str(e))
        print(e)

# =====================================================
#               ALACRITTY
# =====================================================

def install_alacritty(self):
    install = 'pacman -S alacritty --needed --noconfirm'

    if os.path.exists("/usr/bin/alacritty"):
        pass
    else:
        subprocess.call(install.split(" "),
                        shell=False,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.STDOUT)

# =====================================================
#               ZSH + PACKAGES (ARCOLINUXD)
# =====================================================

def install_zsh(self):
    install = 'pacman -S zsh zsh-completions zsh-syntax-highlighting arcolinux-zsh-git oh-my-zsh-git --needed --noconfirm'

    if os.path.exists("/usr/bin/zsh") and os.path.exists("/etc/skel/.zshrc") :
        pass
    else:
        subprocess.call(install.split(" "),
                        shell=False,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.STDOUT)

# =====================================================
#               FISH + PACKAGES (ARCOLINUXD)
# =====================================================

def install_fish(self):
    install = 'pacman -S fish arcolinux-fish-git --needed --noconfirm'

    if os.path.exists("/usr/bin/fish") and os.path.exists("/etc/skel/.config/fish/config.fish") :
        pass
    else:
        subprocess.call(install.split(" "),
                        shell=False,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.STDOUT)

def remove_fish(self):
    install = 'pacman -Rs fish --noconfirm'

    if not os.path.exists("/usr/bin/fish") :
        pass
    else:
        subprocess.call(install.split(" "),
                        shell=False,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.STDOUT)

# =====================================================
#               ARCOLINUX-DESKTOP-TRASHER
# =====================================================

def install_adt(self):
    install = 'pacman -S arcolinux-desktop-trasher-git --noconfirm'

    if os.path.exists("/usr/local/bin/arcolinux-desktop-trasher"):
        pass
    else:
        subprocess.call(install.split(" "),
                        shell=False,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.STDOUT)

# =====================================================
#               GRUB CONF
# =====================================================

def get_grub_wallpapers():
    if os.path.isdir("/boot/grub/themes/Vimix"):
        lists = os.listdir("/boot/grub/themes/Vimix")

        rems = ['select_e.png', 'terminal_box_se.png', 'select_c.png',
                'terminal_box_c.png', 'terminal_box_s.png',
                'select_w.png', 'terminal_box_nw.png',
                'terminal_box_w.png', 'terminal_box_ne.png',
                'terminal_box_sw.png', 'terminal_box_n.png',
                'terminal_box_e.png']

        ext = ['.png', '.jpeg', '.jpg']

        new_list = [x for x in lists if x not in rems for y in ext if y in x]

        new_list.sort()
        return new_list

def set_grub_wallpaper(self, image):
    if os.path.isfile(grub_theme_conf):
        if not os.path.isfile(grub_theme_conf + ".bak"):
            shutil.copy(grub_theme_conf, grub_theme_conf + ".bak")
        try:
            with open(grub_theme_conf, "r") as f:
                lists = f.readlines()
                f.close()

            val = _get_position(lists, "desktop-image: ")
            lists[val] = "desktop-image: \"" + os.path.basename(image) + "\"" + "\n"

            with open(grub_theme_conf, "w") as f:
                f.writelines(lists)
                f.close()

            show_in_app_notification(self, "Settings Saved Successfully")
            # MessageBox(self, "Success!!", "Settings Saved Successfully")
        except:  # noqa
            pass

# =====================================================
#               NEOFETCH CONF
# =====================================================

def neofetch_set_value(lists, pos, text, state):
    if state:
        if text in lists[pos]:
            if "#" in lists[pos]:
                lists[pos] = lists[pos].replace("#", "")
    else:
        if text in lists[pos]:
            if "#" not in lists[pos]:
                lists[pos] = "#" + lists[pos]

    return lists

def neofetch_set_backend_value(lists, pos, text, value):
    if text in lists[pos] and "#" not in lists[pos]:
        lists[pos] = text + value + "\"\n"

# ====================================================================
#                       CUSTOM FUNCTION
# ====================================================================

def get_desktop(self):
    base_dir = os.path.dirname(os.path.realpath(__file__))

    desktop = subprocess.run(["sh", base_dir + "/get_desktop.sh", "-n"],
                             shell=False,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.STDOUT)
    dsk = desktop.stdout.decode().strip().split("\n")
    self.desktop = dsk[-1].strip()

def copytree(self, src, dst, symlinks=False, ignore=None):  # noqa

    if not os.path.exists(dst):
        os.makedirs(dst)
    for item in os.listdir(src):
        s = os.path.join(src, item)
        d = os.path.join(dst, item)
        if os.path.exists(d):
            try:
                shutil.rmtree(d)
            except Exception as e:
                print(e)
                os.unlink(d)
        if os.path.isdir(s):
            try:
                shutil.copytree(s, d, symlinks, ignore)
            except Exception as e:
                print(e)
                print("ERROR2")
                self.ecode = 1
        else:
            try:
                shutil.copy2(s, d)
            except:  # noqa
                print("ERROR3")
                self.ecode = 1

# =====================================================
#               CHECK RUNNING PROCESS
# =====================================================

def checkIfProcessRunning(processName):
    for proc in psutil.process_iter():
        try:
            pinfo = proc.as_dict(attrs=['pid', 'name', 'create_time'])
            if processName == pinfo['pid']:
                return True
        except (psutil.NoSuchProcess,
                psutil.AccessDenied,
                psutil.ZombieProcess):
            pass
    return False

def restart_program():
    python = sys.executable
    os.execl(python, python, *sys.argv)


# Imports
import os
import numpy as np
import pandas as pd

import pickle, json
import tweepy

from datetime import datetime,timezone
import re
import time

from utils import get_logger
from auth import api
from dotenv import load_dotenv
import dicttoobject

if not load_dotenv():
    print('cant load .env file. Exiting!')
    exit()

users_folder = os.getenv('USERS_CACHE_FOLDER')

logger = get_logger('logs', 'flask.log')

#twitter_keys = {
#    'consumer_key': os.environ.get('consumer_key', None),
#    'consumer_secret': os.environ.get('consumer_secret', None),
#    'access_token_key': os.environ.get('access_token_key', None),
#    'access_token_secret': os.environ.get('access_token_secret', None)
#}

# Get fully-trained XGBoostClassifier model
with open('model_37k.pickle', 'rb') as read_file:
    xgb_model = pickle.load(read_file)

# Set up connection to Twitter API
#auth = tweepy.OAuthHandler(
#    twitter_keys['consumer_key'], twitter_keys['consumer_secret'])
#auth.set_access_token(
#    twitter_keys['access_token_key'], twitter_keys['access_token_secret'])

#api = tweepy.API(auth)

def is_cached(screen_name):
    filepath = os.path.join(users_folder, f'{screen_name}.json')
    if os.path.isfile(filepath):
        logger.info(('reading cached data', filepath))
        data = json.loads(open(filepath, 'r').read())
        data['created_at'] = datetime.strptime(data['created_at'], '%a %b %d %H:%M:%S %z %Y')
        return dicttoobject.dict_to_readonly_object(data)

def save_user(screen_name, data):
    file_path = os.path.join(users_folder, f'{screen_name}.json')
    os.makedirs(users_folder, exist_ok=True)
    if os.path.isfile(file_path):
        logger.info(('Duplicate file found - skipping', file_path))
        return None
    with open(file_path,'w') as f:
        json.dump(data,f,indent=4) # write to file
    logger.info(('File saved', file_path))
    return file_path

def get_user_features(screen_name):
    '''
    Input: a Twitter handle (screen_name)
    Returns: a list of account-level information used to make a prediction
            whether the user is a bot or not
    '''

    try:

        user = is_cached(screen_name)
        if not user:
            # Get user information from screen name
            user = api.get_user(screen_name=screen_name)
            save_user(screen_name, user._json)

        # account features to return for predicton
        account_age_days = (datetime.now(timezone.utc) - user.created_at).days
        verified = user.verified
        geo_enabled = user.geo_enabled
        default_profile = user.default_profile
        default_profile_image = user.default_profile_image
        favourites_count = user.favourites_count
        followers_count = user.followers_count
        friends_count = user.friends_count
        statuses_count = user.statuses_count
        average_tweets_per_day = np.round(statuses_count / account_age_days, 3)

        # manufactured features
        hour_created = int(user.created_at.strftime('%H'))
        network = np.round(np.log(1 + friends_count)
                           * np.log(1 + followers_count), 3)
        tweet_to_followers = np.round(
            np.log(1 + statuses_count) * np.log(1 + followers_count), 3)
        follower_acq_rate = np.round(
            np.log(1 + (followers_count / account_age_days)), 3)
        friends_acq_rate = np.round(
            np.log(1 + (friends_count / account_age_days)), 3)

        # organizing list to be returned
        account_features = [verified, hour_created, geo_enabled, default_profile, default_profile_image, favourites_count, followers_count, friends_count, statuses_count, average_tweets_per_day, network, tweet_to_followers, follower_acq_rate,friends_acq_rate]
        logger.info((f'features for {screen_name} verified:{verified}, hour_created:{hour_created}, geo_enabled:{geo_enabled}, default_profile:{default_profile}, default_profile_image:{default_profile_image}, favourites_count:{favourites_count}, followers_count:{followers_count}, friends_count:{friends_count}, statuses_count:{statuses_count}, average_tweets_per_day:{average_tweets_per_day}, network:{network}, tweet_to_followers:{tweet_to_followers}, follower_acq_rate:{follower_acq_rate}, friends_acq_rate:{friends_acq_rate}'))

    except Exception as e:
        logger.exception(e)
        return 'User not found'

    return account_features if len(account_features) == 14 else f'User not found'

def bot_or_not(twitter_handle):
    '''
    Takes in a twitter handle and predicts whether or not the user is a bot
    Required: trained classification model (XGBoost) and user account-level info as features
    '''

    user_features = get_user_features(twitter_handle)

    if user_features == 'User not found':
        return 'User not found'

    else:
        # features for model
        features = ['verified', 'hour_created', 'geo_enabled', 'default_profile', 'default_profile_image',
                    'favourites_count', 'followers_count', 'friends_count', 'statuses_count', 'average_tweets_per_day',
                    'network', 'tweet_to_followers', 'follower_acq_rate', 'friends_acq_rate']

        # creates df for model.predict() format
        user_df = pd.DataFrame(np.matrix(user_features), columns=features)

        prediction = xgb_model.predict(user_df)[0]

        return "Bot" if prediction == 1 else "Not a bot"

def bot_proba(twitter_handle):
    '''
    Takes in a twitter handle and provides probabily of whether or not the user is a bot
    Required: trained classification model (XGBoost) and user account-level info from get_user_features
    '''
    user_features = get_user_features(twitter_handle)

    if user_features == 'User not found':
        return 'User not found'
    else:
        user = np.matrix(user_features)
        proba = np.round(xgb_model.predict_proba(user)[:, 1][0]*100, 2)
        return proba

if __name__=="__main__":
    username = input('Enter a username:')
    u = get_user_features(username)
    print(u)


import re
import csv
import glob
import json
import argparse
import pandas as pd

from parse_gpx import GPXObj as gpxobj

# 1. Paths to:
#   a. gpx file                 -- duration,lat,lon
#   b. allData_clusters.csv     -- cluster, xy_umap, filepath
#   c. phases_dates.csv         -- day/moth/date/phase

# 2. Loop through allData_clusters.csv file

# X. activedays.json needs to be updated after geojson is generated

parser = argparse.ArgumentParser()
parser.add_argument('--gpx_folder_path')
parser.add_argument('--data_clusters_csv_path')
parser.add_argument('--phases_dates_csv_path')
parser.add_argument('--phase_files_path')

args = parser.parse_args()

# {
#    'type': 'Feature',
#    'properties': {
#        # from gpx file
#        'duration': '3',
#        'phase': '2.1', # generate from matching gpx filename to phases_dates.csv
#        'chunks': [
#            # from --> allData_clusters.csv
#            {
#                'chunkFilepath': '/Users/elinaoik/Documents/MIT/THESIS-MIT/python/CLEAN/CHUNKS3/200731_001_Stereo/ChannelsTest/200731_001_Stereo_681_mono.wav',
#                'cluster': '0',
#                'xy_umap' : [1,2]
#            },
#            {
#                'chunkFilepath': '/Users/elinaoik/Documents/MIT/THESIS-MIT/python/CLEAN/CHUNKS3/200731_001_Stereo/ChannelsTest/200731_001_Stereo_681_mono.wav',
#                'cluster': '0',
#                'xy_umap' : [1,2]
#            },
#            {
#                'chunkFilepath': '/Users/elinaoik/Documents/MIT/THESIS-MIT/python/CLEAN/CHUNKS3/200731_001_Stereo/ChannelsTest/200731_001_Stereo_681_mono.wav',
#                'cluster': '0',
#                'xy_umap' : [1,2]
#            }
#        ],
#    },
#    'geometry': {
#        'type': 'LineString',
#        'coordinates': [
#            # from gpx file
#            [-122.483696, 37.833818],
#            [-122.483482, 37.833174],
#        ]
#    }
# }

def all_files_in_folder(gpx_folder_path: str, file_suffix: str) -> list:
    """ list of gpx filepaths """
    return glob.glob(f"{gpx_folder_path}/*.{file_suffix}")

def phase_url_from_phase_nr(datestr: str, phase_nr: str) -> str:
    """ 200606_PM & 3.1 --> PM_3_1 """
    am_pm = datestr.split('_')[-1]
    phase, subphase = str(phase_nr).split('.')
    return f"{am_pm}_{phase}_{subphase}"

def add_zero_to_date(date):
    """ 6 --> 06 """
    if len(str(date)) == 1:
        return f"0{date}"
    else:
        return date

if __name__ == '__main__':

    # all of the data clusters for all the days
    data_clusters = pd.read_csv(args.data_clusters_csv_path)
    # phases dates file
    phases_dates = pd.read_csv(args.phases_dates_csv_path)
    # list of files in the phase path
    phase_file_list = all_files_in_folder(args.phase_files_path, 'csv')
    # gpx fiile list
    gpx_paths = all_files_in_folder(args.gpx_folder_path, 'gpx')

    # updated filenames to match with gpx files
    # audio names set
    audio_names_updated = set()
    # get YYMMDD_AM unique filenames for matching with GPX FILES
    for idx, cluster_row in data_clusters.iterrows():
        audio_name = (cluster_row['AudioNames'])
        updated_name = '_'.join(audio_name.split('/')[-1].split('_')[:2])
        audio_names_updated.add(updated_name)

    # match phases with dates
    dates_phases = {}

    # match the audio files from the csv file
    for dates in list(audio_names_updated):
        # whith the file from the phases
        for idx, phases_dt in phases_dates.iterrows():
            date = add_zero_to_date(phases_dt['Date'])
            month = add_zero_to_date(phases_dt['Month'])
            datestr = f"20{month}{date}_{phases_dt['Part_of_the_Day']}"

            # get the right phase for the the right date
            if dates == datestr:
                phase_file = f"{phase_url_from_phase_nr(datestr, phases_dt['Phase'])}.csv"
                for phase_url in phase_file_list:
                    phase_url_short = phase_url.split('/')[-1]
                    if phase_url_short == phase_file:
                        dates_phases[dates] = {
                            'phase': phases_dt['Phase'], 'url': f"{phase_url.split('.')[0]}.json"}

    # create Objects containing all of the GPX data
    gpx_objects = []
    # create gpx file for each
    for gpx_file_name in gpx_paths:
        gpx_date = gpx_file_name.split('/')[-1].split('.')[0]
        try:
            curr_phase_f = dates_phases[gpx_date]['url']
        except KeyError as ke:
            continue
        with open(curr_phase_f) as f:
            phase_File = json.load(f)
        gpx_f = open(gpx_file_name, mode='r', encoding='utf-8-sig')
        curr_gpx_o = gpxobj(gpx_date, gpx_f, phase_File)
        curr_gpx_o.phase = curr_phase_f
        gpx_objects.append(curr_gpx_o)

    # match gpx files to wav dates
    for gpx_obj in gpx_objects:
        for idx, cluster_row in data_clusters.iterrows():
            match = re.search(r"[0-9]+_(?:AM|PM)", cluster_row['AudioNames'])
            if match:
                if match.group() == gpx_obj.gpx_date:
                    gpx_obj.add_chunk_path(cluster_row)

    for gpx_obj in gpx_objects:
        # sorting
        gpx_obj.chunk_paths = sorted(gpx_obj.chunk_paths, key=lambda x: int(
            x['AudioNames'].split('/')[-1].split('_')[-2]))
        # duration to millisec
        gpx_obj.duration_multi = [i*1000 for i in gpx_obj.duration]
        chunks_select = []

        # duration needs more than 100 points
        if len(gpx_obj.duration_multi) > 100:
            durations = iter(gpx_obj.duration_multi)
            curr_dur = next(durations)
            for i, chunk in enumerate(gpx_obj.chunk_paths):
                audio_chunk = int(chunk['AudioNames'].split(
                    '/')[-1].split('_')[-2]) * 960
                if audio_chunk + 960 <= curr_dur:
                    chunks_select.append(chunk)
                if audio_chunk > curr_dur:
                    gpx_obj.chunks_by_gps.append(chunks_select)
                    chunks_select = []
                    chunks_select.append(chunk)
                    try:
                        curr_dur = next(durations) + curr_dur
                    except StopIteration:
                        break

    for gpx_obj in gpx_objects:
        res = gpx_obj.to_geojson()

        with open(f"{gpx_obj.gpx_date}.json", "w") as outfile:
            json.dump(res, outfile)


# -*- coding: utf-8 -*-
"""
Created on Thu May  2 18:16:06 2019

@author: preet
"""

import numpy as np
import matplotlib.pyplot as plt

class Polynomial:

    def __init__(self, *coefficients):
        self.coefficients = coefficients[::-1]

    def __repr__(self):
        return "Polynomial" + str(self.coefficients[::-1])

    def __call__(self, x):
        res = 0
        for index, coeff in enumerate(self.coefficients):
            res += coeff * x ** index
        return res

p = Polynomial(4, 0, -4, 3, 0)
for x in range(-3, 3):
    print(x, p(x))

X = np.linspace(-3, 3, 50, endpoint=True)
F = p(X)
plt.plot(X, F)
plt.show()


import numpy as np
import cv2

img = cv2.imread("jp.png")

print("{}\n{}\n{}".format(img.shape, img.size, img.dtype))

b, g, r = cv2.split(img)
img = cv2.merge((b, g, r))

# something = img[180:240, 230:290]
# img[262:322, 100:160] = something

# in order to add images they need to have the same side
dst = cv2.resize(img, (512, 512))

cv2.imshow('test', img)

cv2.waitKey()


__author__ = 'Pavel Gladkov'
# -*- coding: utf-8 -*-

import logging
from utils.age_gender import *
from utils.utils import get_domains_count
import csv

if __name__ == "__main__":

    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
        datefmt='%m-%d-%y %H:%M:%S')

    logger = logging.getLogger(__name__)

    train_file = open('age_gender/data/train.csv', 'w')
    test_file = open('age_gender/data/test.csv', 'w')
    data_file = open('age_gender/data/gender_age_dataset.txt', 'r')

    logger.info("get all domains")
    # домены-фичи, только хорошие
    all_domains = get_good_domains(domains_filename='/home/pgladkov/P/BigData/lab_4/age_gender/data/good_domains.txt',
                                   dataset_file=data_file)

    ages = ['18-24', '25-34', '35-44', '45-54', '>=55']
    genders = ['M', 'F']
    classes = []
    for a in ages:
        for g in genders:
            classes.append(a+g)

    train_writer = csv.DictWriter(train_file, ['cl'] + all_domains)
    train_writer.writeheader()

    test_writer = csv.DictWriter(test_file, ['uid'] + all_domains)
    test_writer.writeheader()

    logger.info("start split")

    def get_result_dict(_data):
        _d_count = get_domains_count(_data.get('visits', []))
        _total_visits = 0
        for _k, _v in _d_count.iteritems():
            _total_visits += _v
        _result_dict = {}
        for _domain in all_domains:
            _v = _d_count.get(_domain, 0)
            _result_dict[_domain] = _v

        return _result_dict

    # курсор в начало файла
    data_file = open('age_gender/data/gender_age_dataset.txt', 'r')
    for line in data_file:
        line = line.strip()
        fields = line.split('\t')
        try:
            gender = fields[0]
            age = fields[1]
            uid = fields[2]
            data = json.loads(fields[3])
        except (IndexError, ValueError):
            continue

        if gender == '-':
            result_dict = get_result_dict(data)
            result_dict['uid'] = uid
            test_writer.writerow(result_dict)
        else:
            result_dict = get_result_dict(data)
            result_dict['cl'] = age + gender
            train_writer.writerow(result_dict)

    test_file.close()
    train_file.close()


import requests
import falcon
import json

from .json_validate import DefaultValidatingDraft4Validator

class UtilBase():

    _error_msg = {
        'authentication_info_required': 'AUTHENTICATION_INFO_REQUIRED',
        'authentication_info_illegal': 'AUTHENTICATION_INFO_ILLEGAL',
        'attempt_too_many_times': 'ATTEMPT_TOO_MANY_TIMES',
        'balance_insufficient': 'BALANCE_INSUFFICIENT',
        'conflict_info_exist': 'CONFLICT_INFO_EXIST',
        'conflict_mobile_exist': 'CONFLICT_MOBILE_EXIST',
        'forbidden': 'FORBIDDEN',
        'img_code_timeout': 'IMG_CODE_TIMEOUT',
        'img_code_verification_failed': 'IMG_CODE_VERIFICATION_FAILED',
        'invalid_body_content': 'INVALID_BODY_CONTENT',
        'invalid_mobile_number': 'INVALID_MOBILE_NUMBER',
        'invalid_query_params': 'INVALID_QUERY_PARAMS',
        'need_credential': 'NEED_CREDENTIAL',
        'not_found': 'NOT_FOUND',
        'no_mobile_info': 'NO_MOBILE_INFO',
        'password_verification_failed': 'PASSWORD_VERIFICATION_FAILED',
        'sms_can_not_send_in_60': 'SMS_CAN_NOT_SEND_IN_60',
        'sms_max_limit_per_day': 'SMS_MAX_LIMIT_PER_DAY',
        'sms_timeout': 'SMS_TIMEOUT',
        'sms_verification_failed': 'SMS_VERIFICATION_FAILED',
        'token_timeout': 'TOKEN_TIMEOUT',
        'token_verification_failed': 'TOKEN_VERIFICATION_FAILED',
        'user_not_exist': 'USER_NOT_EXIST',
        'user_exist_more_information_needed': 'USER_EXIST_MORE_INFORMATION_NEEDED',
        'unsupported_operation': 'UNSUPPORTED_OPERATION',
        'unsupported_transaction_category': 'UNSUPPORTED_TRANSACTION_CATEGORY',
    }

    _headers = {
        "Accept": "application/json",
        "Content-Type": "application/json"
    }

    _methods = {
        "GET": requests.get,
        "POST": requests.post,
        "PUT": requests.put,
        "DELETE": requests.delete
    }

    @property
    def ERROR_MSG(self):
        return UtilBase._error_msg

    @property
    def HEADERS(self):
        return UtilBase._headers

    @property
    def METHODS(self):
        return UtilBase._methods

    @staticmethod
    def result_error(msg, detail=None):
        msg = {'error': msg}
        if detail:
            msg['detail'] = detail

        return falcon.HTTP_400, json.dumps(msg)

    @staticmethod
    def transform_status_code(status_code):

        if status_code == 200:
            return falcon.HTTP_200

        if status_code == 201:
            return falcon.HTTP_201

        if status_code == 202:
            return falcon.HTTP_202

        if status_code == 403:
            return falcon.HTTP_403

        if status_code == 404:
            return falcon.HTTP_404

        if status_code == 409:
            return falcon.HTTP_409

        if status_code == 415:
            return falcon.HTTP_415

        if status_code == 500:
            return falcon.HTTP_500

    @staticmethod
    def transform_to_int(param_dict, if_raise_value_error=False):
        if not isinstance(param_dict, dict):
            return False, "<%s> is not <dict> type" % type(param_dict)

        for key, value in param_dict.items():
            try:
                param_dict[key] = int(value)
            except ValueError:
                if if_raise_value_error:
                    return False, "'%s' type <%s> can not be converted to <int>" % (value, type(value))
                else:
                    pass
        return True, None

    def request(self, method, url, **params):
        response = self.METHODS[method](url, headers=self.HEADERS, **params)
        return response

    @staticmethod
    def parse_pagination_params(params_dict):
        condition = dict(params_dict)
        pageNum = condition.get('pageNum')
        pageSize = condition.get('pageSize')

        if pageNum:
            del condition['pageNum']
        else:
            pageNum = 0

        if pageSize:
            del condition['pageSize']
        else:
            pageSize = 10

        return condition, pageNum, pageSize

    @staticmethod
    def validate_dict_with_schema(data, schema):
        try:
            DefaultValidatingDraft4Validator(schema).validate(data)
            return 200, None
        except Exception as ex:
            return 415, "parse body exception: %s" % ex

    @classmethod
    def get_params_from_body(cls, body_stream, schema=None, return_dict=False):
        data = {}
        body = body_stream.read()

        try:
            data = json.loads(body)
        except:
            return 415, "parse json failed"

        if schema:
            code, tag = cls.validate_dict_with_schema(data, schema)
            if code != 200:
                return code, tag

        return 200, data if return_dict else json.dumps(data)


'''
Created on 29 Aug 2017

@author: Michael
'''
from datetime import datetime
import time
import SQLiteAdapter as db
import Topics

class Message(object):
    '''
    classdocs
    '''

    def __init__(self,id=None,username=None,message=None,reply_to=None,topic=None,
                 modified=0,likes=0,dislikes=0,timestamp=datetime.now()):
        '''
        Constructor
        '''
        self.id = id
        self.username = username
        self.message = message
        self.reply_to = reply_to
        self.topic = topic
        self.modified = modified
        self.likes = likes
        self.dislikes = dislikes
        self.timestamp = timestamp

    def save(self):
        '''
            Adds this object to the database if no id exists, otherwise it updates.
            This must be called after any direct modification to this object in order to
            make it permanent.
        '''
        if self.id==None:
            #Add
            res = db.QUERY("INSERT INTO messages (username,message,reply_to,topic,modified,likes,dislikes) VALUES (?,?,?,?,?,?,?)",
                           (self.username,self.message,self.reply_to,self.topic,self.modified,self.likes,self.dislikes))
            if res:
                self.id=res
                return True
        else:
            #Update
            res = db.QUERY("UPDATE messages SET username=?,message=?,reply_to=?,topic=?,modified=?,likes=?,dislikes=? WHERE id=?",
                           (self.username,self.message,self.reply_to,self.topic,self.modified,self.likes,self.dislikes,self.id))

        if res:
            return True
        else:
            return False

    def getMessages(self):
        return getFiltered(filters={'reply_to':self.id})

    def getTopic(self):
        if self.topic:
            return Topics.get(self.topic)
        return None

    def getParentMessage(self):
        if self.reply_to:
            return get(self.reply_to)
        return None

def getCount(filters={}):
    ''' Number of messages in the database given a number of filters'''
    keys = filters.keys()
    values = filters.values()
    query = 'SELECT COUNT(*) AS count FROM messages '
    if len(keys)>0:
        query = query + 'WHERE '
        for key in keys:
            query = query + key + '=?, '
        query = query[:-2]
    res = db.QUERY(query,tuple(values))
    if res:
        return res[0]['count']
    return 0

def get(id=None):
    ''' Gets a Message given it's id
    '''
    if id==None:
        return None

    res = db.QUERY('SELECT * FROM messages WHERE id=?',(id,))
    if res and len(res)>0:
        return Message(**res[0])
    else:
        return None

def getAll(limit=-1):
    ''' Get a list of all Messages '''
    if limit>0:
        res = db.QUERY('SELECT * FROM messages LIMIT ?',(limit,))
    else:
        res = db.QUERY('SELECT * FROM messages')
    rlist = []
    if res:
        for values in res:
            rlist.append(Message(**values))
        return rlist
    return None

def getFiltered(filters={}, limit=-1,order_by=None,ascending=True):
    ''' Get a list of filtered Messages '''

    keys = filters.keys()
    query = 'SELECT * FROM messages '
    if len(keys)>0:
        query = query + 'WHERE '
        for key in keys:
            if filters[key] is None:
                #Need to do something slightly special as SQLite is weird will NULLs
                query = query + key + ' IS NULL and '
                del filters[key]
            else:
                query = query + key + '=? and '
        query = query[:-4]
    values = filters.values()
    if order_by:
        order_by = order_by if order_by.lower() in ['id','username','message',
                                                    'reply_to','topic','modified',
                                                    'likes','dislikes',
                                                    'timestamp'] else 'id'
        query = query + 'ORDER BY ' + order_by
        if ascending:
            query = query + " ASC "
        else:
            query = query + " DESC "
        values.append(order_by)
    if limit>0:
        values.append(limit)
        res = db.QUERY(query + ' LIMIT ?',tuple(values))
    else:
        res = db.QUERY(query,tuple(values))
    if res:
        rlist = []
        for values in res:
            rlist.append(Message(**values))
        return rlist
    return None

def delete(id=None):
    ''' Deletes a Message given it's id
    '''
    if id==None:
        return None
    res = db.QUERY('DELETE FROM messages WHERE id=?',(id,))
    if res>0:
        return True
    else:
        return False