from flask import Flask, redirect, request, render_template, jsonify
from flask_cors import CORS
import sys
from dotenv import load_dotenv
import json
import os
from admin_classes import prodAdmin as prod, betaAdmin as beta, testAdmin as test

# sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../..")))
# from admin.admin_classes import prodAdmin as prod, betaAdmin as beta, testAdmin as test

load_dotenv()
# initialize the prod_admin class
water_prod=prod().water_db_live
food_prod=prod().food_db_live
bathroom_prod=prod().bathroom_db_live
forage_prod=prod().forage_db_live
# initialize the beta_admin class
water_beta=beta().water_db_live
food_beta=beta().food_db_live
bathroom_beta=beta().bathroom_db_live
forage_beta=beta().forage_db_live
# initialize the test_admin class
water_test=test().water_db_live
food_test=test().food_db_live
bathroom_test=test().bathroom_db_live
forage_test=test().forage_db_live

dashboard = Flask(__name__)

CORS(dashboard)

def connectDB():
    return water_prod

def time_it(func):
    def wrapper(*args, **kwargs):
        import time
        start = time.perf_counter()
        result = func(*args, **kwargs)
        end = time.perf_counter()
        run_time = end - start
        print(f"Finished {func.__name__!r} in {run_time:.4f} secs")
        return result
    return wrapper

@dashboard.route("/")
@time_it
def main():
    result = {}
    data = prod().getDb(water_prod)

    for tap in data:
        try:
            tapnum = tap["tapnum"]
            if tapnum not in result:
                result[tapnum] = {}
            result[tapnum]=tap
        except:
            pass
    return json.dumps(result)

# Initiating the count of taps in each database
water_prod_count = prod().getCount(water_prod)
food_prod_count = prod().getCount(food_prod)
bathroom_prod_count = prod().getCount(bathroom_prod)
forage_prod_count = prod().getCount(forage_prod)

# Create a route to send the formated chart data to the frontend
@dashboard.route('/chart-data')
def chart_data():
    data = {
        'labels': ['Water DB', 'Food DB', 'Bathroom DB', 'Forage DB'],
        'datasets': [
            {
                'label': 'Water Tap Count',
                'data': [water_prod_count, 0, 0, 0],
                'backgroundColor': 'rgba(75, 192, 192, 0.5)',
                'borderColor': 'rgb(75, 192, 192)',
                'borderWidth': 1,
                'hoverBackgroundColor': 'rgb(75, 192, 192)',
                'hoverBorderColor': 'rgba(75, 192, 192, 0.8)',
                'hoverBorderWidth': 2,
            },
            {
                'label': 'Food Tap Count',
                'data': [0, food_prod_count, 0, 0],
                'backgroundColor': 'rgba(255, 99, 132, 0.5)',
                'borderColor': 'rgb(255, 99, 132)',
                'borderWidth': 1,
                'hoverBackgroundColor': 'rgb(255, 99, 132)',
                'hoverBorderColor': 'rgba(255, 99, 132, 0.8)',
                'hoverBorderWidth': 2,
            },
            {
                'label': 'Bathroom Tap Count',
                'data': [0, 0, bathroom_prod_count, 0],
                'backgroundColor': 'rgba(54, 162, 235, 0.5)',
                'borderColor': 'rgb(54, 162, 235)',
                'borderWidth': 1,
                'hoverBackgroundColor': 'rgb(54, 162, 235)',
                'hoverBorderColor': 'rgba(54, 162, 235, 0.8)',
                'hoverBorderWidth': 2,
            },
            {
                'label': 'Forage Tap Count',
                'data': [0, 0, 0, forage_prod_count],
                'backgroundColor': 'rgba(255, 205, 86, 0.5)',
                'borderColor': 'rgb(255, 205, 86)',
                'borderWidth': 1,
                'hoverBackgroundColor': 'rgb(255, 205, 86)',
                'hoverBorderColor': 'rgba(255, 205, 86, 0.8)',
                'hoverBorderWidth': 2,
            }
        ]
    }
    return jsonify(data)

@dashboard.route('/updatetap/<int:tapnum>', methods = ['GET', 'PUT'])
def updatetap(tapnum):
    tp=[]
    db=prod().getDb(water_prod)
    if request.method == 'GET':
        try:
            tp = prod().getTap(water_prod, tapnum)
            return json.dumps(tp)
        except:
            pass

    elif request.method == 'PUT':
        try:
            data = request.get_json()
            water_prod.update({
                tapnum: {
                    "access": data.get("access"),
                    "address": data.get("address"),
                    "city": data.get("city"),
                    "description": data.get("description"),
                    "filteration": data.get("filtration"),
                    "gp_id": data.get("gp_id"),
                    "handicap": data.get("handicap"),
                    "latitude": data.get("lat"),
                    "longitude": data.get("lon"),
                    "norms": data.get("norms"),
                    "organization": data.get("organization"),
                    "permanently_closed": data.get("permanently_closed"),
                    "phone": data.get("phone"),
                    "quality": data.get("quality"),
                    "service": data.get("service"),
                    "statement": data.get("statement"),
                    "status": data.get("status"),
                    "tap_type": data.get("tap_type"),
                    "tapnum": data.get("tapnum"),
                    "vessel": data.get("vessel"),
                    "zip_code": data.get("zip_code")
                }
            })
            return f"Tap {tapnum} Updated Successfully"
        except:
            return f"Error updating tap {tapnum}"

@dashboard.route('/deletetap/<int:tapnum>')
def deletetap(tapnum):
    prod.deleteTap(water_prod, str(tapnum))
    return redirect('/')

# Uncomment this to run the app locally without docker! Dont forget to do similar configuration for the frontend!

if(__name__ == "__main__"):
    dbconn = connectDB()
    dashboard.run(host='0.0.0.0', port=int(os.environ.get('PORT',5000)), debug=True)


from __future__ import annotations

from functools import partial
import itertools
import threading
from typing import Optional, Dict
import sys

if sys.version_info[:2] < (3, 8):

    class Protocol(object):
        pass

else:
    from typing import Protocol

import typing
import weakref
from robotframework_debug_adapter.constants import DEBUG

from robocorp_ls_core.debug_adapter_core.dap.dap_base_schema import BaseSchema
from robocorp_ls_core.robotframework_log import get_logger
from robocorp_ls_core.debug_adapter_core.dap.dap_schema import (
    TerminatedEvent,
    StoppedEvent,
)

log = get_logger(__name__)

if typing.TYPE_CHECKING:
    from robotframework_debug_adapter.debug_adapter_comm import DebugAdapterComm

class IProtocolMessageCallable(Protocol):
    def __call__(self, message: BaseSchema) -> None:
        pass

class INextSeq(Protocol):
    def __call__(self) -> int:
        pass

class BaseLaunchProcessTargetComm(threading.Thread):
    def __init__(self, debug_adapter_comm: DebugAdapterComm) -> None:
        threading.Thread.__init__(self)
        self._weak_debug_adapter_comm = weakref.ref(debug_adapter_comm)

        self._connected_event = threading.Event()

        self._process_event_msg = None
        self._process_event = threading.Event()

        self._terminated_event_msg: Optional[TerminatedEvent] = None
        self._terminated_lock = threading.Lock()
        self._terminated_event = threading.Event()
        self._next_seq: INextSeq = partial(next, itertools.count(0))
        self._msg_id_to_on_response: Dict[int, Optional[IProtocolMessageCallable]] = {}

    def _handle_received_protocol_message_from_backend(
        self, protocol_message: BaseSchema, backend: str
    ) -> None:
        from robocorp_ls_core.debug_adapter_core.debug_adapter_threads import (
            READER_THREAD_STOPPED,
        )
        import json

        if protocol_message is READER_THREAD_STOPPED:
            if DEBUG:
                log.debug(
                    f"{self.__class__.__name__} when reading from {backend}: READER_THREAD_STOPPED."
                )
            return

        if DEBUG:
            log.debug(
                "Process json: %s\n"
                % (json.dumps(protocol_message.to_dict(), indent=4, sort_keys=True),)
            )

        try:
            on_response: Optional[IProtocolMessageCallable] = None
            if protocol_message.type == "request":
                from robocorp_ls_core.debug_adapter_core.dap.dap_schema import Request

                req = typing.cast(Request, protocol_message)
                method_name = f"on_{req.command}_request"

            elif protocol_message.type == "event":
                from robocorp_ls_core.debug_adapter_core.dap.dap_schema import Event

                ev = typing.cast(Event, protocol_message)
                method_name = f"on_{ev.event}_event"

            elif protocol_message.type == "response":
                from robocorp_ls_core.debug_adapter_core.dap.dap_schema import Response

                resp = typing.cast(Response, protocol_message)
                on_response = self._msg_id_to_on_response.pop(resp.request_seq, None)
                method_name = f"on_{resp.command}_response"

            else:
                if DEBUG:
                    log.debug(
                        "Unable to decide how to deal with protocol type: %s (read from %s - %s).\n"
                        % (protocol_message.type, backend, self.__class__.__name__)
                    )
                return

            if on_response is not None:
                on_response(protocol_message)

            on_request = getattr(self, method_name, None)

            if on_request is not None:
                on_request(protocol_message)
            elif on_response is not None:
                pass
            else:
                if DEBUG:
                    log.debug(
                        "Unhandled: %s not available when reading from %s - %s.\n"
                        % (method_name, backend, self.__class__.__name__)
                    )
        except:
            log.exception("Error")

    def on_stopped_event(self, event: StoppedEvent):
        debug_adapter_comm = self._weak_debug_adapter_comm()
        if debug_adapter_comm is not None:
            debug_adapter_comm.weak_stopped_target_comm = weakref.ref(self)
            debug_adapter_comm.write_to_client_message(event)
        else:
            log.debug("Command processor collected in event: %s" % (event,))


# -*- coding: UTF-8 -*-
import shell

def run(client, module):
    args = module.get('args')
    if args:
        directory = args.get('dir')
        file_name = args.get('file')
        mod = {
            "shell": "openssl md5 {file} | cut -d' ' -f2".format(file=file_name),
            'args': {
                'chdir': directory
            }
        }
        return shell.run(client, mod)


import requests
import json
import argparse
import sys

def getPushshiftData(max):
    '''

    :param max: the max number of comments or posts to extract
    :return:
    '''
    # Postshift URLS
    url_askmen = "https://api.pushshift.io/reddit/search/comment/?size=" + str(max) + "&subreddit=askmen&score=>100"
    url_askwomen = "https://api.pushshift.io/reddit/search/comment/?size=" + str(max) + "&subreddit=askwomen&score=>100"
    url_relationship_advice = "https://api.pushshift.io/reddit/search/submission/?size=" + str(max * 2) +\
                              "&subreddit=relationship_advice&score=>100"
    url_men = "https://api.pushshift.io/reddit/search/comment/?size=" + str(max) + "&subreddit=men&score=>100"
    url_women = "https://api.pushshift.io/reddit/search/comment/?size=" + str(max) + "&subreddit=women&score=>100"

    # Make requests
    r = requests.get(url_askmen)
    askmen_data = json.loads(r.content)
    r = requests.get(url_askwomen)
    askwomen_data = json.loads(r.content)
    r = requests.get(url_relationship_advice)
    relationship_data = json.loads(r.content)
    r = requests.get(url_men)
    men_data = json.loads(r.content)
    r = requests.get(url_women)
    women_data = json.loads(r.content)

    return askmen_data, askwomen_data, relationship_data, men_data, women_data

def main(args):
    askmen_data, askwomen_data, relationship_data, men_data, women_data = getPushshiftData(args.max)

    # Write output to file
    with open("Data/AskMen", "w") as output:
        json.dump(askmen_data, output)

    with open("Data/AskWomen", "w") as output:
        json.dump(askwomen_data, output)

    with open("Data/RelationshipAdvice", "w") as output:
        json.dump(relationship_data, output)

    with open("Data/Men", "w") as output:
        json.dump(men_data, output)

    with open("Data/Women", "w") as output:
        json.dump(women_data, output)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Get data from pushshift')
    parser.add_argument("--max", type=int, help="The maximum number of comments to read from each file", default=10000)

    args = parser.parse_args()

    if (args.max > 200272):
        print("Error: If you want to read more than 200,272 comments per file, you have to read them all.")
        sys.exit(1)

    main(args)


import os
from PyPDF4 import PdfFileReader, PdfFileWriter
import argparse
from PIL import Image

def pdf_splitter(path):
    fname = os.path.splitext(os.path.basename(path))[0]

    pdf = PdfFileReader(path)
    for page in range(pdf.getNumPages()):
        pdf_writer = PdfFileWriter()
        pdf_writer.addPage(pdf.getPage(page))
        output_filename = '{}_page_{}.pdf'.format(
            fname, page + 1)

        with open(output_filename, 'wb') as out:
            pdf_writer.write(out)

        print('Created: {}'.format(output_filename))

def pdf_deletepages(inpath, delete_list, outpath):
    fname = os.path.splitext(os.path.basename(inpath))[0]

    if not outpath:
        outpath = '{}_output.pdf'.format(fname)

    pdf = PdfFileReader(inpath)
    pdf_writer = PdfFileWriter()

    with open(outpath, 'wb') as out:
        for page in range(pdf.getNumPages()):
            if page+1 not in delete_list:
                pdf_writer.addPage(pdf.getPage(page))

        pdf_writer.write(out)

    print('Created: {}'.format(outpath))

def pdf_generate(inpath, generate_list, outpath):
    fname = os.path.splitext(os.path.basename(inpath))[0]

    if not outpath:
        outpath = '{}_output.pdf'.format(fname)

    pdf_writer = PdfFileWriter()

    with open(outpath, 'wb') as out:
        for file in generate_list:
            pdf = PdfFileReader(file)
            for page in range(pdf.getNumPages()):
                pdf_writer.addPage(pdf.getPage(page))

        pdf_writer.write(out)

    print('Created: {}'.format(outpath))

def pdf_appendfile(inpath:str, appendpath:str, page_no: int, outpath:str):
    fname = os.path.splitext(os.path.basename(inpath))[0]
    if not outpath:
        outpath = '{}_output.pdf'.format(fname)

    pdf = PdfFileReader(inpath)
    pdf_a = PdfFileReader(appendpath)
    pdf_writer = PdfFileWriter()

    with open(outpath, 'wb') as out:
        for page in range(pdf.getNumPages()):
            pdf_writer.addPage(pdf.getPage(page))
            if page+1 == int(page_no):
                for page_a in range(pdf_a.getNumPages()):
                    pdf_writer.addPage(pdf_a.getPage(page_a))

        pdf_writer.write(out)

    print('Created: {}'.format(outpath))

def pdf_replacepage(inpath:str, replacepath:str, page_no: int, outpath:str):
    fname = os.path.splitext(os.path.basename(inpath))[0]
    if not outpath:
        outpath = '{}_output.pdf'.format(fname)

    pdf = PdfFileReader(inpath)
    pdf_d = PdfFileReader(replacepath)
    pdf_writer = PdfFileWriter()

    with open(outpath, 'wb') as out:
        for page in range(pdf.getNumPages()):
            if page+1 == int(page_no):
                for page_d in range(pdf_d.getNumPages()):
                    pdf_writer.addPage(pdf_d.getPage(page_d))
            else:
                pdf_writer.addPage(pdf.getPage(page))

        pdf_writer.write(out)

    print('Created: {}'.format(outpath))

def pdf_convertfile(inpath:str, convertpaths, outpath:str):
    fname = os.path.splitext(os.path.basename(inpath))[0]
    if not outpath:
        outpath = '{}_output.pdf'.format(fname)

    images = []
    for file in convertpaths:
        image = Image.open(file)
        image.convert('RGB')
        images.append(image)

    image_1 = images.pop(0)
    image_1.save(outpath, save_all=True, append_images=images)

    print('Created: {}'.format(outpath))

if __name__ == '__main__':
    # Initialize parser
    parser = argparse.ArgumentParser(description="Update PdfFiles")

    # Adding optional argument
    parser.add_argument(type=str, metavar='input_file', dest="infile",
                        help="PDF Input file")
    parser.add_argument("-s", dest="split", action='store_true', default=False,
                        help="Split input PDF into files containing individual pages")
    parser.add_argument("-d", dest="delete_pages", type=int, action='append', default=[],
                        help="delete page_number")
    parser.add_argument("-o", dest="output_file", type=str, action='store',
                        help="output filename")
    parser.add_argument("-a", nargs=2, metavar=('page_no', 'filename'), dest=("append"),
                        help="Append filename after page_no")
    parser.add_argument("-r", nargs=2, metavar=('page_no', 'filename'), dest=("replace"),
                        help="Replace page_no with filename ")
    parser.add_argument("-c", dest="images", type=str, action='append', default=[],
                        help="Convert images to one pdf file")
    parser.add_argument("-g", dest="generate", metavar="filename", type=str, action='append', default=[],
                        help="Combine all files in order to form a single PDF file ")

    # Read arguments from command line
    args = parser.parse_args()

    if args.images:
        print('Converting images : {}'.format(args.images))
        pdf_convertfile(args.infile, args.images, args.output_file)

    if args.split:
        print("Splitting %s to files " % args.infile)
        pdf_splitter(args.infile)

    if args.delete_pages:
        print('Deleting pages : {}'.format(args.delete_pages))
        pdf_deletepages(args.infile, args.delete_pages, args.output_file)

    if args.generate:
        print('Combining : {}'.format(args.generate))
        pdf_generate(args.infile, args.generate, args.output_file)

    if args.append:
        after_page_no, append_filename = args.append
        print('Append {} after page {}'.format(append_filename, after_page_no))
        pdf_appendfile(args.infile, append_filename, after_page_no, args.output_file)

    if args.replace:
        page_no, replace_filename = args.replace
        print('Replace page {} with {}'.format(page_no, replace_filename))
        pdf_replacepage(args.infile, replace_filename, page_no, args.output_file)


import pygame
from settings import *
from random import choice, randint

class BG(pygame.sprite.Sprite):
	"""
	Class for the background sprite
	"""
	def __init__(self, groups, scale_factor) -> None:
		"""
		Initialize images and display settings for the background
		"""
		super().__init__(groups)
		bg_image = pygame.image.load('../graphics/environment/background.png').convert()

		full_height = bg_image.get_height() * scale_factor
		full_width = bg_image.get_width() * scale_factor
		full_sized_image = pygame.transform.scale(bg_image, (full_width, full_height))

		self.image = pygame.Surface((full_width * 2, full_height))
		self.image.blit(full_sized_image, (0, 0))
		self.image.blit(full_sized_image, (full_width, 0))

		self.rect = self.image.get_rect(topleft=(0, 0))
		self.pos = pygame.math.Vector2(self.rect.topleft)

	def update(self, dt) -> None:
		"""
		Refreshes background on screen
		"""
		self.pos.x -= 300 * dt
		if self.rect.centerx <= 0:
			self.pos.x = 0
		self.rect.x = round(self.pos.x)

class Ground(pygame.sprite.Sprite):
	"""
	Class for the ground sprite
	"""
	def __init__(self, groups, scale_factor) -> None:
		"""
		Initialize images and display settings for the ground
		"""
		super().__init__(groups)
		self.sprite_type = 'ground'

		# image
		ground = pygame.image.load('../graphics/environment/ground.png').convert_alpha()
		self.image = pygame.transform.scale(ground, pygame.math.Vector2(ground.get_size()) * scale_factor)

		# position
		self.rect = self.image.get_rect(bottomleft=(0, WINDOW_HEIGHT))
		self.pos = pygame.math.Vector2(self.rect.topleft)
		self.pos = pygame.math.Vector2(self.rect.topleft)

		# mask
		self.mask = pygame.mask.from_surface(self.image)

	def update(self, dt) -> None:
		"""
		Refreshes ground on screen
		"""
		self.pos.x -= 360 * dt
		if self.rect.centerx <= 0:
			self.pos.x = 0

		self.rect.x = round(self.pos.x)

class Cookie(pygame.sprite.Sprite):
	"""
	Class for the cookie sprite
	"""
	def __init__(self, groups, scale_factor) -> None:
		"""
		Initialize images, display settings, movement, and sound for cookie
		"""
		super().__init__(groups)

		# image
		self.import_frames(scale_factor)
		self.frame_index = 0
		self.image = self.frames[self.frame_index]

		# rect
		self.rect = self.image.get_rect(midleft = (WINDOW_WIDTH / 20,WINDOW_HEIGHT / 2))
		self.pos = pygame.math.Vector2(self.rect.topleft)

		# movement
		self.gravity = 600
		self.direction = 0

		# mask
		self.mask = pygame.mask.from_surface(self.image)

		# sound
		self.jump_sound = pygame.mixer.Sound('../sounds/jump.wav')
		self.jump_sound.set_volume(0.3)

	def import_frames(self, scale_factor):
		"""
		Code for multiple cookie sprites
		"""
		self.frames = []
		for i in range(3):
			cookie = pygame.image.load(f'../graphics/cookie/cookie{i}.png').convert_alpha()
			scaled_cookie = pygame.transform.scale(cookie, pygame.math.Vector2(cookie.get_size()) * scale_factor)
			self.frames.append(scaled_cookie)

	def apply_gravity(self, dt) -> None:
		"""
		Creates gravity
		"""
		self.direction += self.gravity * dt
		self.pos.y += self.direction * dt
		self.rect.y = round(self.pos.y)

	def jump(self) -> None:
		"""
		Allows cookie to jump
		"""
		self.jump_sound.play()
		self.direction = -400

	def animate(self, dt) -> None:
		"""
		Animates sprite
		"""
		self.frame_index += 10 * dt
		if self.frame_index >= len(self.frames):
			self.frame_index = 0
		self.image = self.frames[int(self.frame_index)]

	def rotate(self) -> None:
		"""
		Method to make the cookie rotate
		"""
		rotate_cookie = pygame.transform.rotozoom(self.image, -self.direction * 0.03, 1)
		self.image = rotate_cookie
		self.mask = pygame.mask.from_surface(self.image)

	def update(self, dt) -> None:
		"""
		Method for refreshing the cookie on the screen
		"""
		self.apply_gravity(dt)
		self.animate(dt)
		self.rotate()

class Obstacle(pygame.sprite.Sprite):
	def __init__(self, groups, scale_factor) -> None:
		"""
		Initialize images and display settings for obstacle
		"""
		super().__init__(groups)
		self.sprite_type = 'obstacle'

		orientation = choice(('up', 'down'))
		obstacle = pygame.image.load(f'../graphics/obstacles/{choice((0,1))}.png').convert_alpha()
		self.image = pygame.transform.scale(obstacle, pygame.math.Vector2(obstacle.get_size()) * scale_factor)

		x = WINDOW_WIDTH + randint(40, 100)

		if orientation == 'up':
			y = WINDOW_HEIGHT + randint(10, 50)
			self.rect = self.image.get_rect(midbottom=(x, y))
		else:
			y = randint(-50, -10)
			self.image = pygame.transform.flip(self.image, False, True)
			self.rect = self.image.get_rect(midtop=(x, y))

		self.pos = pygame.math.Vector2(self.rect.topleft)

		# mask
		self.mask = pygame.mask.from_surface(self.image)

	def update(self, dt) -> None:
		"""
		Refreshes obstacle on the screen
		"""
		self.pos.x -= 400 * dt
		self.rect.x = round(self.pos.x)
		if self.rect.right <= -100:
			self.kill()


##Load the train and test data
##Train the model with the training data
##Save the parameters
import os
import json
import joblib
import argparse
import numpy as np
import pandas as pd
from get_data import read_params
from sklearn.model_selection import train_test_split
from sklearn.linear_model import ElasticNet
from sklearn.metrics import mean_squared_error,mean_absolute_error,r2_score

def train_evaluate(config_path):
    config = read_params(config_path)
    test_data_path = config['split_data']['test_data_path']
    train_data_path = config['split_data']['train_data_path']
    random_state = config['split_data']['random_state']
    model_store_dir = config['train_evaluate']['model_store_dir']
    alpha = config['train_evaluate']['alpha']
    l1_ratio = config['train_evaluate']['l1_ratio']
    target_col = [config['train_evaluate']['target_col']]
    scores_json = config['reports']['scores_json']
    params_file = config['reports']['params_json']

    train_data = pd.read_csv(train_data_path,sep=",")
    test_data = pd.read_csv(test_data_path,sep=",")

    train_x = train_data.drop(columns=target_col,axis=1)
    test_x = test_data.drop(columns=target_col,axis=1)

    train_y = train_data[target_col]
    test_y = test_data[target_col]

    lr = ElasticNet(alpha=alpha,l1_ratio=l1_ratio,random_state=random_state)
    lr.fit(train_x,train_y)

    predicted_qualities = lr.predict(test_x)
    rmse,mae,r2 = evaluate_metrics(test_y,predicted_qualities)
    print(rmse,mae,r2)

    with open(params_file,'w') as rb:
        params = {
            "alpha": alpha,
            "l1_ratio": l1_ratio,
        }
        json.dump(params,rb,indent=4)

    with open(scores_json,'w') as rb:
        scores = {
            "rmse": rmse,
            "mae": mae,
            "r2": r2
        }
        json.dump(scores,rb,indent=4)

    os.makedirs(model_store_dir,exist_ok=True)
    model_path = os.path.join(model_store_dir,"model.joblib")
    joblib.dump(lr,model_path)

def evaluate_metrics(actual,predicted):
    rmse = np.sqrt(mean_squared_error(actual, predicted))
    mae = mean_absolute_error(actual, predicted)
    r2 = r2_score(actual, predicted)
    return rmse,mae,r2

if __name__=='__main__':
    args = argparse.ArgumentParser()
    config_path = os.path.join("config","params.yml")
    args.add_argument("--config", default=config_path)
    parsed_args = args.parse_args()
    train_evaluate(config_path=parsed_args.config)


#!/usr/bin/env python3
#
# etl_migrate.py:
# Migrate an input database file to an output file
# Based on etl_ipums.py
# Note coding around Pandas memory leak...

import gc
import gzip
import json
import logging
import logging.handlers
import os
import os.path
import psutil
import re
import sqlite3
import subprocess
import math
import sys
import time
import gzip
import sqlite3

from schema import Schema,Table,Variable,Recode,TYPE_VARCHAR,TYPE_INTEGER
from configparser import ConfigParser
from census_spec_scanner import CensusSpec

from migrate import Migrate

import ctools.cspark  as cspark
import ctools.dconfig as dconfig
import ctools.clogging as clogging

PROCESS = psutil.Process(os.getpid())

SETUP_SECTION = "setup"
CONFIGFILE_TYPE = "configfile_type"
CONFIGFILE_TYPE_WANTED = "etl_migrate"
OUTDIR = 'outdir'
RANDOM_RECORDS = 'random_records'
LIMIT = 'limit'
DEBUG_OPTION = 'debug'
SPARK_SECTION= 'spark'
SPARK_OPTION = 'spark'
NUM_EXECUTORS = 'num_executors'
NUM_EXECUTORS_DEFAULT = '2'
WRITE_DELIMITER = "write_delimiter"
SETUP_OPTIONS = set([CONFIGFILE_TYPE,CONFIGFILE_TYPE_WANTED,OUTDIR,RANDOM_RECORDS,LIMIT,WRITE_DELIMITER,DEBUG_OPTION])

RECODES_SECTION = "recodes"
RUN_SECTION = "run"
GLOBALS_SECTION = "globals"
GLOBALS = "globals"
COMPLETED_FLAG = '.completed'
BULK_LOAD_REPORTING_INTERVAL=100000

def fatal(msg):
    """Send msg to the error log and then raise a runtime error"""
    logging.error(msg)
    raise RuntimeError(msg)

def squash_floats(d):
    """Transform floats into ints; used in the output routines"""
    for k in d.keys():
        try:
            if math.isnan(d[k]):
                d[k] = ''
                continue
        except TypeError:
            continue

        try:
            if (type(d[k])==float and float(d[k])==int(d[k])):
                d[k]=int(d[k])
        except ValueError:
            pass

def configFileHasDuplicateKeys(f):
    # return true if any section in the config file has multiple keys
    import configparser
    config = ConfigParser()
    try:
        config.readfp(f)
        return False
    except configparser.DuplicateOptionError as e:
        return True

def configFileSectionOutOfOrder(f,section):
    # return true if any of the labels in the section of the config file are out of order
    section_re = re.compile(r"^\[(\S+)\]")
    label_re   = re.compile(r"^([^# \t]\S*):")
    in_section = ''
    last_label = ''
    this_label = ''
    for line in f:
        m = section_re.search(line)
        if m:
            in_section = m.group(1)
            continue
        m = label_re.search(line)
        if m:
            last_label = this_label
            this_label = m.group(1)
            if last_label and this_label < last_label:
                return (last_label,this_label)
    return None

def infodb(path):
    conn = sqlite3.connect(path)
    c = conn.cursor()
    print("Database: {} Metadata".format(path))
    print("Key: value")
    print("==========")
    for (key,value) in c.execute("select key,value from metadata"):
        print("{}: {}".format(key,value))
    print("Database: {} Data (limit 10)".format(path))
    print("Key: value")
    print("==========")
    for (key,value) in c.execute("select key,value from data limit 10"):
        print("{}: {}".format(key,value))
    exit(0)

def infosas(path):
    s = Schema()
    print("Loading schema from {}".format(path))
    s.load_schema_from_file( dconfig.dopen(path))
    s.dump(path)
    exit(0)

RUN_COMMAND_RE = re.compile(r"\s*((\w+)\s*=)?\s*(\w+)\((.*)\)")
def parse_run_step_command(line):
    m = RUN_COMMAND_RE.search(line.strip())
    if not m:
        fatal("Cannot parse: {}".format(line))
    return m.group(2,3,4)

def process_setup_section(config, sc=None):
    """Process the setup section of the config file. Return the mig variable"""
    setup = config[SETUP_SECTION]

    # Check for configuration file variables that set options

    mig = Migrate( outdir=os.path.expandvars(config[SETUP_SECTION][OUTDIR]),
                   dry_run = args.dry_run ,
                   limit   = args.limit,
                   dumpdb  = args.dumpdb,
                   spark_context = sc)

    # Get the input and output schemas from the input and output options
    db = CensusSpec(name='etl_db')
    for (key,val) in config[SETUP_SECTION].items():
        # inputNN: specify the schema of an input table.
        # For some tables, also specifies the filenames containing the data
        if key.startswith("input"):
            val = os.path.expandvars(val)
            logging.info("{}: {}".format(key,val))
            db.load_schema_from_file( val )

        # outputNN: or formatNN: specify the schema of an output table
        elif key.startswith("output"):
            val = os.path.expandvars(val)
            logging.info("{}: {}".format(key,val))
            db.load_schema_from_file( val )

        # sqlNN: Specify the schema of a table with SQL
        elif key.startswith("sql"):
            db.add_sql_table(val)

        elif key == RANDOM_RECORDS:
            args.random = int(val)
        elif key == LIMIT:
            args.limit = int(val)
        elif key == WRITE_DELIMITER:
            args.write_delimiter = val

        elif key not in SETUP_OPTIONS: # ignore these
            fatal("Unknown option in secton {}: '{}'".format(SETUP_SECTION,key))

    return (mig,db)

def process_recodes_section(config,mig,db):
    if RECODES_SECTION not in config.sections():
        return
    logging.info("Adding recodes")
    recode_re = re.compile("([^(]+)[(](.+)[)]")
    for option in sorted(config.options(RECODES_SECTION)):
        recode_desc = config[RECODES_SECTION][option]
        m = recode_re.search(option)
        if m:
            (option_without_vtype,vtype) = m.group(1,2)
            vtype = vtype.upper()
            logging.info("{}:  {} ({})".format(option_without_vtype,recode_desc,vtype))
            db.add_recode(option_without_vtype,vtype,recode_desc)
        else:
            fatal("Cannot code recode {}:{}".format(name,recode_desc))
    db.compile_recodes()

def process_run_section(config,mig,db):
    # Process the steps
    for option in sorted(config.options(RUN_SECTION)):
        if option==GLOBALS:     # ignore the globals
            continue
        logging.info("processing [{}] {}".format(RUN_SECTION,option))
        if not option.startswith("step"):
            logging.info("Ignoring option '{}'".format(option))
            continue
        lines = config[RUN_SECTION][option].split("\n")

        (dest,func_name,func_args) = parse_run_step_command(lines[0])
        logging.info("{} dest:{} func_name:{}  func_args:{}".format(option,dest,func_name,func_args))
        if func_args:
            try:
                kvargs = dict([kv.strip().split("=") for kv in func_args.split(",")])
            except ValueError as e:
                raise RuntimeError("Parsing error: option:{} argument:{} missing '=' for key=value assignment".format(
                    option,func_args))
        else:
            kvargs = {}
        # If any of the kvargs are quoted strings, unquote them
        for (key,value) in kvargs.items():
            while (value[0]==value[-1]) and (value[0] in "\"'"):
                value = value[1:-1]
                kvargs[key] = value

        if mig.command(db=db, cmd=func_name, dest=dest, kvargs=kvargs, lines=lines):
            continue
        if func_name=='DUMP':
            db.dump(logging.info)
        elif func_name=="COLLECT_ACCUM":
            mig.compile_and_exec("euid_counts = euid_counts.value")
        else:
            fatal("Unknown command in config file option '{}': func_name={} func_args={} dest={} ".
                               format(option,func_name,func_args,dest))

if __name__=="__main__":
    from argparse import ArgumentParser,ArgumentDefaultsHelpFormatter
    parser = ArgumentParser( formatter_class = ArgumentDefaultsHelpFormatter,
                             description="Migrate an input dataset to an output dataset" )
    parser.add_argument('--debug', action='store_true')
    parser.add_argument('--dump',  action='store_true', help='Dump schema and internal functions; do not run the transfer')
    parser.add_argument('--dry-run', action='store_true', help='Print what would be done only')
    parser.add_argument('--schema', action='store_true', help='display the SQL schema')
    parser.add_argument('--limit', help='Only copy this many records from each file (for testing)', type=int)
    parser.add_argument('--random', action='store_true', help='generate random data')
    parser.add_argument('--loglevel', help='Set logging level',
                        choices=['CRITICAL','ERROR','WARNING','INFO','DEBUG'],
                        default='INFO')
    parser.add_argument('--write_delimiter', help='delimiter for output csv files',default='|')
    parser.add_argument("--justglobals", help="Just run the globals setup, dump what's in the environment, and then stop",
                        action='store_true')
    parser.add_argument('--infosas',help='Print information about SAS7BDAT file and exit; config is ignored')
    parser.add_argument('--loaddb', action='store_true', help='Only load join databases')
    parser.add_argument('--infodb', help='Print information about the SQlite3 database file and exit; config is ignored')
    parser.add_argument('--dumpdb', help='If specified, dump this many records of the SQLite3 database',type=int)
    parser.add_argument("--detach", action="store_true", help="disconnection from console and write output to logfiles")
    parser.add_argument("--spark",  action="store_true", help="Run under spark")
    parser.add_argument('config', help='config file')

    args   = parser.parse_args()

    # Check the args
    if args.random and not args.limit:
        parser.error("--limit must be specified with --random")

    # See if simple infos are specified
    if args.infodb:  infodb(args.infodb); exit(0)
    if args.infosas: infosas(args.infosas); exit(0)

    ###
    ### VALIDATE AND THEN READ CONFIG FILE
    ###

    if configFileHasDuplicateKeys(open(args.config)):
        fatal("{} has duplicate keys".format(args.config))
    os.environ["CONFIGDIR"] = os.path.split(os.path.abspath(args.config))[0]
    config = dconfig.get_config(pathname=args.config)
    if CONFIGFILE_TYPE not in config[SETUP_SECTION]:
        fatal("{} not in config file {}".format(CONFIGFILE_TYPE,args.config))

    if config[SETUP_SECTION][CONFIGFILE_TYPE] != CONFIGFILE_TYPE_WANTED:
        fatal("{} is not {} in config file {}".
                           format(CONFIGFILE_TYPE,CONFIGFILE_TYPE_WANTED,args.config))
    order = configFileSectionOutOfOrder(open(args.config),"run")
    if order:
        fatal("[run] section has steps that are out of order: {} comes before {}".format(order[0],order[1]))

    if config[SETUP_SECTION].get("DEBUG","").lower()[0:1] in ["1","t","y"]:
        args.debug = True

    if SPARK_SECTION in config and config[SPARK_SECTION].get("SPARK","").lower()[0:1] in ["1","t","y"]:
        args.spark = True

    # Detach if requested
    if args.detach:
        print("Detaching...")
        cspark.detach()

    ###
    ### ENABLE SPARK IF REQUESTED
    ###

    sc = None
    if args.spark:
        num_executors = config[SPARK_SECTION].get(NUM_EXECUTORS, NUM_EXECUTORS_DEFAULT)
        sc = cspark.spark_context(num_executors=int(num_executors),
                                  pydirs=[os.path.dirname(__file__),
                                          os.path.join(os.path.dirname(__file__),'ctools'),
                                          os.path.join(os.path.dirname(__file__),'dfxml/python')])

    ###
    ### ENABLE LOGGING.
    ### Logging must be set up before any logging is done
    ### Note that we log to both a file and to syslog facility 1
    ###

    clogging.setup(args.loglevel)
    logging.info("START {} ".format(os.path.abspath(__file__)))

    (mig,db) = process_setup_section(config, sc)
    process_recodes_section(config,mig,db)

    # Dump or print schema, as necessary
    if args.dump:       db.dump()
    if args.schema:     print(db.sql_schema())
    #if args.spark:      mig.add_spark_to_environment()

    # run steps are implemented as compiled code.
    mig.add_tables_to_environment(db)

    # Add globals to environment if we have any
    if GLOBALS in config[RUN_SECTION]:
        fatal("globals must now be  [globals] section")

    if GLOBALS_SECTION in config:
        logging.info("Adding GLOBALs")
        for option in config[GLOBALS_SECTION]:
            logging.info("Adding [{}]{}".format(GLOBALS_SECTION,option))
            lines = config[GLOBALS_SECTION][option]
            mig.compile_and_exec(lines)
    else:
        logging.info("No [{}] section".format(GLOBALS_SECTION))

    #mig.undo_spark()

    if args.justglobals:
        mig.dump_context()
    else:
        process_run_section(config, mig, db)

    logging.info("DONE")
    exit(0)


from flask import Flask, render_template, request
import json
import cv2
import numpy as np
import urllib.request as url_req
import ssl

app = Flask(__name__)

# METHOD #1: OpenCV, NumPy, and urllib
def readImageFromUrl(imgUrl):
	# download the image, convert it to a NumPy array, and then read
	# it into OpenCV format
	context = ssl._create_unverified_context()
	with url_req.urlopen(imgUrl, context=context) as url:
   		 s = url.read()
	image = np.asarray(bytearray(s), dtype="uint8")
	image = cv2.imdecode(image, cv2.IMREAD_COLOR)
	#return the image
	return image

# METHOD #2: Checks Whether the input is just string or Url
def isUrl(string):
	if "http://" in string:
		return True
	elif "https://" in string:
		return True
	else :
		return False

# METHOD #3: Check for the Faces Presence and
def getFaceCoordinates(faceImage):
	#load harcascade Classifier
	face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
	greay_image = cv2.cvtColor(faceImage, cv2.COLOR_BGR2GRAY)
	faceDetected = False
	faces = face_cascade.detectMultiScale(greay_image,
	scaleFactor=1.05,
	minNeighbors = 5)
	coordinates=list()
	for x, y, w, h in faces:
		faceDetected = True
		face_coordinates = {"Start_X":x, "Start_Y": y, "End_X":(x+w), "End_Y": (y+h), "height": h, "width": w}
		coordinates.append(face_coordinates)

	result = {"FacePresence": faceDetected,
		 	"FacesCount": len(faces),
		 	"Coordinates": coordinates}
	return result

# WEB_METHOD #1: for Default url
@app.route('/')
def home():
	return "Get request called"

# WEB_METHOD #2: Detect Faces And return face coodrinates of the face in the image..
@app.route('/faceDetect', methods=['GET','POST'])
def faceDetect():
	if request.method == 'GET':
	   return "{\"method\": \"GET\", \"response\": \"GET method called Successfully\", \"status\":\"SUCCESS\"}"
	elif request.method == 'POST':
		data = json.loads(request.data)
		uof = isUrl(data["url"])
		if uof == True:
			img = readImageFromUrl(data["url"])
			response = getFaceCoordinates(img)
		#stringResponse = "Url: {} ImageName: {}".format(data["url"], data["image_name"])
		return str(response)

if __name__ == '__main__':
	app.run(debug = True)


import weakref
import numpy as np
import pygame as p

class Object:
    objects = weakref.WeakSet()
    def __init__(self, typeX, vel, xsize, ysize):

        self.type = typeX
        self.damage = 10

        if self.type == 0:
            self.damage = 10
            self.sprite = p.image.load("vfx/sprites/misil1.png")

        self.vel = vel
        self.size = (40, 70)
        self.pos = (np.random.randint(20, xsize-20), ysize-40)

        self.cann = p.transform.scale(self.sprite, self.size)
        self.hitbox = (self.pos[0]-self.size[0]/2, self.pos[1]-self.size[1]/2, \
            self.size[0]-15, self.size[1]-2)
        self.sprite_rect = None

        self.__class__.objects.add(self)

    def UpdatePos(self):
        self.hitbox = (self.pos[0]-self.size[0]/2, self.pos[1]-self.size[1]/2, \
            self.size[0]-10, self.size[1]-10)

        self.pos = (self.pos[0], self.pos[1]-self.vel)

    def Draw(self, win, hitbox=False):
        self.sprite = p.transform.rotate(self.cann, 0)
        self.sprite_rect = self.sprite.get_rect()

        self.sprite_rect.center = self.pos
        win.blit(self.sprite, self.sprite_rect)
        if hitbox:
            p.draw.rect(win, (255, 0, 0), (self.pos[0]+self.hitbox[2]/2, \
                self.pos[1]-self.hitbox[3]/2, 3, self.hitbox[3]))

            p.draw.rect(win, (255, 255, 0), (self.pos[0]-self.hitbox[2]/2, \
                self.pos[1]-self.hitbox[3]/2, 3, self.hitbox[3]))

            p.draw.rect(win, (255, 255, 255), (self.pos[0]-self.hitbox[2]/2, \
                self.pos[1]+self.hitbox[3]/2, self.hitbox[2], 3))

            p.draw.rect(win, (255, 0, 255), (self.pos[0]-self.hitbox[2]/2, \
                self.pos[1]-self.hitbox[3]/2, self.hitbox[2], 3))

    @classmethod
    def getObjects(cls):
        return cls.objects


import subprocess
import json
import requests
import os
import shutil

def get_functions():
    result = json.loads(subprocess.check_output(['aws', '--profile', 'thankshell', 'lambda', 'list-functions']))

    info = []
    for f in result['Functions']:
        if f['FunctionName'].startswith('thankshell'):
            info.append({key: value for key, value in f.items() if key in ['FunctionName', 'Layers']})

    return info

if __name__ == '__main__':
    lambda_dir = os.path.dirname(os.path.abspath(__file__))

    functions = get_functions()
    json.dump(functions, open('functions.json', 'w'), indent=2)

    for f_info in functions:
        print("downloading: " + f_info['FunctionName'])
        fname = f_info['FunctionName']
        result = json.loads(subprocess.check_output(['aws', '--profile', 'thankshell', 'lambda', 'get-function', '--function-name', fname]))
        response = requests.get(result['Code']['Location'])
        with open('tmp.zip', 'wb') as fp:
            fp.write(response.content)

        shutil.unpack_archive('tmp.zip', fname)

        os.remove('tmp.zip')


import ast
import boto3
import botocore
import datetime
import time

from botocore.exceptions import ClientError

codecommit_client = boto3.client('codecommit')
codepipeline_client = boto3.client('codepipeline')
codebuild_client = boto3.client('codebuild')

# Repository name from codecommit
repository_name = 'Android'

# AWS account details
account_number = ''
role = 'codepipeline-android-service-role'
region = 'ap-south-1'

# S3 bucket to store Source and Build Artifacts
s3_android_bucket_development = 'android-source'
s3_android_bucket_builds = 'android-builds'
s3_android_bucket_release = 'android-release'

# DeviceFarm Project and DevicePool arn to test the application
device_farm_project_id = ''
device_farm_device_pool_arn = ''

pipeline_configuration = {
    "pipeline": {
        "stages": [
            {
                "actions": [
                    {
                        "runOrder": 1,
                        "actionTypeId": {
                            "category": "Source",
                            "provider": "CodeCommit",
                            "version": "1",
                            "owner": "AWS"
                        },
                        "name": "Source",
                        "outputArtifacts": [
                            {
                                "name": "SourceArtifact"
                            }
                        ],
                        "configuration": {
                            "BranchName": "",
                            "OutputArtifactFormat": "CODE_ZIP",
                            "PollForSourceChanges": "false",
                            "RepositoryName": repository_name
                        },
                        "inputArtifacts": []
                    }
                ],
                "name": "Source"
            },
            {
                "actions": [
                    {
                        "runOrder": 1,
                        "actionTypeId": {
                            "category": "Build",
                            "provider": "CodeBuild",
                            "version": "1",
                            "owner": "AWS"
                        },
                        "name": "CodeBuild",
                        "outputArtifacts": [
                            {
                                "name": "BuildArtifact"
                            }
                        ],
                        "configuration": {
                            "BatchEnabled": "false",
                            "ProjectName": ""
                        },
                        "inputArtifacts": [
                            {
                                "name": "SourceArtifact"
                            }
                        ]
                    }
                ],
                "name": "Build"
            },
            {
                "actions": [
                    {
                        "runOrder": 1,
                        "actionTypeId": {
                            "category": "Deploy",
                            "provider": "S3",
                            "version": "1",
                            "owner": "AWS"
                        },
                        "name": "Deploy",
                        "configuration": {
                            "BucketName": s3_android_bucket_builds,
                            "ObjectKey": "MyWebsite",
                            "Extract": "true"
                            },
                        "outputArtifacts": [],
                        "region": region,
                        "inputArtifacts": [
                            {
                                "name": "BuildArtifact"
                            }
                        ]
                    }
                ],
                "name": "Deploy"
            },
            {
                "name": "Test",
                "actions": [
                    {
                        "name": "TestDeviceFarm",
                        "actionTypeId": {
                            "category": "Test",
                            "owner": "AWS",
                            "provider": "DeviceFarm",
                            "version": "1"
                        },
                        "runOrder": 1,
                        "configuration": {
                            "App": "app-debug.apk",
                            "AppType": "Android",
                            "DevicePoolArn": device_farm_device_pool_arn,
                            "FuzzEventCount": "6000",
                            "FuzzEventThrottle": "50",
                            "ProjectId": device_farm_project_id,
                            "TestType": "BUILTIN_FUZZ"
                        },
                        "outputArtifacts": [],
                        "inputArtifacts": [
                            {
                                "name": "BuildArtifact"
                            }
                        ],
                        "region": region
                    }
                ]
            },
        ],
        "artifactStore": {
            "type": "S3",
            "location": s3_android_bucket_development
        },
        "name": "ci-pipeline",
        "version": 1,
        "roleArn": 'arn:aws:iam::'+account_number+':role/service-role/'+role
    }
}

def get_status(pipeline_name):
    pipeline_status = codepipeline_client.get_pipeline_state(name=pipeline_name)
    return pipeline_status

def post_comment(pr_id, repository_name, source_commit,
                 destination_commit, content):
    codecommit_client.post_comment_for_pull_request(
            pullRequestId=pr_id,
            repositoryName=repository_name,
            beforeCommitId=source_commit,
            afterCommitId=destination_commit,
            content=content
        )

def delete_pipeline(pipeline_name):
    try:
        print("Cleaning up, deleting pipeline %s" % pipeline_name)
        codepipeline_client.delete_pipeline(name=pipeline_name)
    except ClientError as e:
        if e.response['Error']['Code'] == 'PipelineNotFoundException':
            print("Pipeline %s does not exist, nothing to do." % pipeline_name)
        else:
            print("Unexpected error: %s" % e)

def update_pipeline(code_pipeline_configuration, branch_ref, destination_branch=None):
    for stage in code_pipeline_configuration['pipeline']['stages']:
        if stage['name'] == 'Source':
            stage['actions'][0]['configuration']['BranchName'] = branch_ref
        elif stage['name'] == 'Build':
            if destination_branch == 'master':
                stage['actions'][0]['configuration']['ProjectName'] = 'android-master-apk-build'
            else:
                stage['actions'][0]['configuration']['ProjectName'] = 'android-develop-apk-build'

    return code_pipeline_configuration['pipeline']

def create_pipeline(modified_pipeline_json):
    new_pipeline_response = codepipeline_client.create_pipeline(pipeline=modified_pipeline_json)
    return new_pipeline_response

def get_pipeline_artifact(build_id):
    response = codebuild_client.batch_get_builds(ids=[build_id])
    pipeline_artifact = '/'.join(response['builds'][0]['artifacts']['location'].split('/')[1:])
    return pipeline_artifact

# Beanch Events Ex: referenceCreated, referenceDeleted, referenceUpdated
def branch_events(message, event_ytpe):
    print(message)
    #reference_type = message['detail']['referenceType'] == 'branch' or 'tag'
    commit_id = message['detail']['commitId']
    print("Commit ID :: %s" % commit_id)
    #branch_ref = '/'.join(message['detail']['referenceFullName'].split('/')[2:])
    branch_ref = message['detail']['referenceName']
    branch_name = '-'.join(message['detail']['referenceName'].split('/'))
    print("Branch Name :: %s" % branch_ref)

    code_pipeline_configuration = pipeline_configuration
    pipeline_name = branch_name + '-' + commit_id + '-pipeline'
    code_pipeline_configuration['pipeline']['name'] = pipeline_name
    code_pipeline_configuration['pipeline']['stages'][2]['actions'][0]['configuration']['ObjectKey'] = commit_id

    if message['detail']['referenceType'] == 'tag':
        #code_pipeline_configuration['pipeline']['artifactStore']['location'] = s3_android_bucket_release
        code_pipeline_configuration['pipeline']['stages'][2]['actions'][0]['configuration']['BucketName'] = s3_android_bucket_release
        # Use master branch for tags
        branch_ref = 'master'
        s3_android_bucket_builds = s3_android_bucket_release

    modified_pipeline_json = update_pipeline(code_pipeline_configuration, branch_ref)
    print(modified_pipeline_json)
    delete_pipeline(pipeline_name)

    time.sleep(5)

    new_pipeline_response = create_pipeline(modified_pipeline_json)

    time.sleep(10)

    pipeline_name = new_pipeline_response['pipeline']['name']
    pipeline_stages = get_status(pipeline_name)

    for stage in pipeline_stages['stageStates']:

        if stage['stageName'] == 'Source':
            source_execution_status = stage['actionStates'][0]['latestExecution']['status']

            if source_execution_status == 'Succeeded':
                print('Stage Source Succeeded.')
            else:
                print('Stage Source Failed.')
                break

        elif stage['stageName'] == 'Build':
            build_url = stage['actionStates'][0]['latestExecution']['externalExecutionUrl']
            count = 0
            while count < 2000:
                pipeline_build_status = get_status(pipeline_name)
                build_execution_status = pipeline_build_status['stageStates'][1]['latestExecution']['status']
                #print('Build status :: ', build_execution_status)

                if build_execution_status == 'Succeeded':
                    print('Stage Build Succeeded.')
                    print(build_url)
                    #delete_pipeline(pipeline_name)
                    break

                elif build_execution_status == 'Failed':
                    print(pipeline_build_status['stageStates'][1]['actionStates'][0]['latestExecution']['errorDetails']['message'])
                    print(build_url)
                    #delete_pipeline(pipeline_name)
                    break

                else:
                    count += 1
                    time.sleep(2)

        elif stage['stageName'] == 'Deploy':
            count = 0
            while count < 500:
                #try:
                pipeline_deploy_status = get_status(pipeline_name)
                deploy_execution_status = pipeline_deploy_status['stageStates'][2]['latestExecution']['status']
                if deploy_execution_status == 'Succeeded':
                    print('Stage '+pipeline_deploy_status['stageStates'][2]['actionStates'][0]['latestExecution']['summary'])
                    build_path = pipeline_deploy_status['stageStates'][2]['actionStates'][0]['latestExecution']['externalExecutionId']
                    print(build_path)
                    apk_s3_location = 'https://'+region+'.console.aws.amazon.com/s3/buckets/android-builds?region='+region+'&prefix='+commit_id+'/&showversions=false'
                    print('Download APK file from :: {}'.format(apk_s3_location))
                    break
                elif deploy_execution_status == 'Failed':
                    print('Stage Deploy failed')
                    break
                else:
                    count += 1
                    time.sleep(2)

        elif stage['stageName'] == 'Test':
            count = 0
            while count < 500:
                pipeline_test_status = get_status(pipeline_name)
                test_execution_status = pipeline_test_status['stageStates'][3]['latestExecution']['status']
                if test_execution_status == 'Succeeded':
                    print('Stage '+pipeline_test_status['stageStates'][3]['actionStates'][0]['latestExecution']['summary'])
                    devicefarm_url = pipeline_test_status['stageStates'][3]['actionStates'][0]['latestExecution']['externalExecutionUrl']
                    print(devicefarm_url)
                    break
                elif test_execution_status == 'Failed':
                    print('Stage Test failed')
                    break
                else:
                    count += 1
                    time.sleep(2)

# Pull Request Events Ex: pullRequestCreated, pullRequestSourceBranchUpdated
def pr_events(message, event_ytpe):
    print(message)
    pr_id = message['detail']['pullRequestId']
    print("Pull Request ID :: %s" % pr_id)
    source_commit = message['detail']['sourceCommit']
    print("Source Commit ID :: %s" % source_commit)
    destination_commit = message['detail']['destinationCommit']
    print("Destination Commit ID :: %s" % destination_commit)
    pipeline_name = str(repository_name+'-'+'PullRequest'+'-'+pr_id+'-'+source_commit)
    print("Pipeline Name :: %s" % pipeline_name)
    destination_branch = message['detail']['destinationReference'].split('/')[-1]
    print("Destination Branch Reference :: %s" % destination_branch)

    try:
        pipeline_response = codepipeline_client.get_pipeline(name=pipeline_name)
    except ClientError as e:
        if e.response['Error']['Code'] == 'PipelineNotFoundException':
            print("Pipeline %s does not exist, creating a new one." % pipeline_name)
            branch_ref = '/'.join(message['detail']['sourceReference'].split('/')[2:])
            print("Branch Name :: %s" % branch_ref)

            code_pipeline_configuration = pipeline_configuration
            code_pipeline_configuration['pipeline']['name'] = pipeline_name
            code_pipeline_configuration['pipeline']['stages'][2]['actions'][0]['configuration']['ObjectKey'] = source_commit
            code_pipeline_configuration['pipeline']['stages'][3]['actions'][0]['configuration']['App'] = source_commit+'/app-debug.apk'

            modified_pipeline_json = update_pipeline(code_pipeline_configuration, branch_ref, destination_branch)
            new_pipeline_response = create_pipeline(modified_pipeline_json)

            pipeline_url = 'https://'+region+'.console.aws.amazon.com/codesuite/codepipeline/pipelines/'+pipeline_name+'/view?region='+region
            content = u'\u23F3'+' Pipeline started at {}'.format(datetime.datetime.utcnow().time())+' '+'- See the [Pipeline]({0})'.format(pipeline_url)
            post_comment(pr_id, repository_name, source_commit, destination_commit, content)

            # TO-DO get the comment ID and use it to post all other status updates
            time.sleep(10)

            pipeline_name = new_pipeline_response['pipeline']['name']
            pipeline_stages = get_status(pipeline_name)

            for stage in pipeline_stages['stageStates']:

                if stage['stageName'] == 'Source':
                    source_execution_status = stage['actionStates'][0]['latestExecution']['status']
                    if source_execution_status == 'Succeeded':
                        print('Stage Source Passed.')
                    else:
                        print('Stage Source Failed.')
                        break

                elif stage['stageName'] == 'Build':
                    build_url = stage['actionStates'][0]['latestExecution']['externalExecutionUrl']
                    count = 0
                    while count < 500:
                        pipeline_build_status = get_status(pipeline_name)
                        build_execution_status = pipeline_build_status['stageStates'][1]['latestExecution']['status']
                        print('Build status :: ', build_execution_status)
                        if build_execution_status == 'Succeeded':
                            print('Stage Build Passed.')
                            content = u'\u2705'+' Stage Build Passed - See the [Logs]({0})'.format(build_url)
                            post_comment(pr_id, repository_name,
                                         source_commit, destination_commit,
                                         content)

                            #if destination_branch == 'master':
                            #    print('Keeping the Master Branch Pipelines for reference.')
                            #else:
                            #    delete_pipeline(pipeline_name)
                            break

                        elif build_execution_status == 'Failed':
                            content = u'\u274C'+' Build Failed - See the [Logs]({0})'.format(build_url)
                            post_comment(pr_id, repository_name,
                                         source_commit, destination_commit,
                                         content)
                            #if destination_branch == 'master':
                            #    print('Keeping the Master Branch Pipelines for reference.')
                            #else:
                            #    delete_pipeline(pipeline_name)
                            break

                        else:
                            count += 1
                            time.sleep(10)

                elif stage['stageName'] == 'Deploy':
                    count = 0
                    while count < 500:
                        pipeline_deploy_status = get_status(pipeline_name)
                        deploy_execution_status = pipeline_deploy_status['stageStates'][2]['latestExecution']['status']
                        if deploy_execution_status == 'Succeeded':
                            print('Stage '+pipeline_deploy_status['stageStates'][2]['actionStates'][0]['latestExecution']['summary'])
                            build_path = pipeline_deploy_status['stageStates'][2]['actionStates'][0]['latestExecution']['externalExecutionId']
                            print(build_path)
                            apk_s3_location = 'https://'+region+'.console.aws.amazon.com/s3/buckets/android-builds?region='+region+'&prefix='+commit_id+'/&showversions=false'
                            print('Download APK file from :: {}'.format(apk_s3_location))
                            content = u'\u2705'+' Stage Deploy Passed - See the [Artifactory]({0})'.format(apk_s3_location)
                            post_comment(pr_id, repository_name,
                                         source_commit, destination_commit,
                                         content)
                            break
                        elif deploy_execution_status == 'Failed':
                            print('Stage Deploy failed')
                            break
                        else:
                            count += 1
                            time.sleep(2)

                elif stage['stageName'] == 'Test':
                    count = 0
                    while count < 500:
                        pipeline_test_status = get_status(pipeline_name)
                        test_execution_status = pipeline_test_status['stageStates'][3]['latestExecution']['status']
                        if test_execution_status == 'Succeeded':
                            summary = pipeline_test_status['stageStates'][3]['actionStates'][0]['latestExecution']['summary']
                            print('Stage '+summary)
                            devicefarm_url = pipeline_test_status['stageStates'][3]['actionStates'][0]['latestExecution']['externalExecutionUrl']
                            print(devicefarm_url)
                            content = u'\u2705'+' '+summary+' - See the logs [DeviceFarm]({0})'.format(devicefarm_url)
                            post_comment(pr_id, repository_name,
                                         source_commit, destination_commit,
                                         content)
                            break
                        elif test_execution_status == 'Failed':
                            print('Stage Deploy failed')
                            break
                        else:
                            count += 1
                            time.sleep(2)

def lambda_handler(event, context):

    message = ast.literal_eval(event['Records'][0]['Sns']['Message'])
    event_type = message['detail']['event']

    if event_type == 'referenceCreated' or event_type == 'referenceUpdated':
        branch_events(message, event_type)
    elif event_type == 'pullRequestCreated' or event_type == 'pullRequestSourceBranchUpdated':
        pr_events(message, event_type)


import spotipy
from spotipy.oauth2 import SpotifyOAuth,SpotifyClientCredentials

import os
import numpy as np
import pandas as pd
from tqdm import tqdm
from time import sleep

# setup spotify with app credentials, use environment variables so GitHub scrapers don't get access to my spotify developer keys
cid = os.getenv('SPOTIPY_CLIENT_ID')
secret = os.getenv('SPOTIPY_CLIENT_SECRET')

#https://medium.com/@maxtingle/getting-started-with-spotifys-api-spotipy-197c3dc6353b

client_credentials_manager = SpotifyClientCredentials(client_id = cid,
                                                      client_secret = secret)
sp = spotipy.Spotify(client_credentials_manager = client_credentials_manager)

# load in new tracks csv
new_tracks = pd.read_csv('../data/tracks_with_popularity.csv')

# drop songs by artists with more than 70 popularity
new_tracks = new_tracks[new_tracks['popularity'] <= 70]

# only need to get user features after getting new_track features once
new_features_list = []
for i in tqdm(range(len(new_tracks))):
    new_features_list.append(sp.audio_features(new_tracks.iloc[i]['track_uri'])[0])
    sleep(0.067)

def is_empty(any_structure):
    if any_structure:
        return False
    else:
        print('Structure is empty.')
        return True

new_features_list = [i for i in new_features_list if is_empty(i) == False]
new_features_df = pd.DataFrame(new_features_list)
new_features_df = pd.merge(new_features_df, new_tracks, left_on = 'uri', right_on = 'track_uri')
new_features_df = new_features_df.drop(['type', 'id', 'uri', 'track_href', 'analysis_url', 'duration_ms',
                      'time_signature', 'track_url', 'popularity'],
                    axis = 1)
new_features_df.to_csv('../data/new_track_features.csv', index = False)


from django.shortcuts import render, HttpResponse, redirect
from app02.models import Department, UserInfo, PrettyNum
from app02.utils.pagination import Pagination
from app02.utils.form import UserModelForm, PrettyModelForm, PrettyEditModelForm

def pretty_list(request):
    """靓号列表"""

    # 搜索框
    data_dict = {}
    search_data = request.GET.get('q', "")
    if search_data:
        data_dict["mobile__contains"] = search_data

    queryset = PrettyNum.objects.filter(**data_dict).order_by("-level")

    page_object = Pagination(request, queryset)
    page_queryset = page_object.page_queryset
    page_string = page_object.html()

    context = {
        "queryset": page_queryset,  # 分完页的数据
        "search_data": search_data,
        "page_string": page_string  # 页码
    }
    return render(request, "pretty_list.html", context)

def pretty_add(request):
    """添加靓号"""
    if request.method == "GET":
        form = PrettyModelForm()
        return render(request, "pretty_add.html", {"form": form})
    form = PrettyModelForm(data=request.POST)
    if form.is_valid():
        form.save()
        return redirect("/pretty/list")
    return render(request, "pretty_add.html", {"form": form})

def pretty_edit(request, nid):
    """编辑靓号"""
    row_object = PrettyNum.objects.filter(id=nid).first()
    # print(row_object)
    if request.method == "GET":
        form = PrettyEditModelForm(instance=row_object)
        return render(request, "pretty_edit.html", {"form": form})
    form = PrettyEditModelForm(data=request.POST, instance=row_object)
    if form.is_valid():
        form.save()
        return redirect("/pretty/list")
    return render(request, "pretty_edit.html", {"form": form})

def pretty_delete(request, nid):
    PrettyNum.objects.filter(id=nid).delete()
    return redirect("/pretty/list")


import requests
import json

class StatioN_Status:

    bikes = []
    docks = []

    def getData(self):
        try:
            print("Divvya Bikes Stations ...")
            url = "https://gbfs.divvybikes.com/gbfs/en/station_status.json"
            headers = { 'Accept':'application/json','Content-Type': 'application/x-www-form-urlencoded' }
            response = requests.get(url, headers=headers, timeout=15)

            serialization = response.json()
            print('Response_Code :',response.status_code)

            jsondata = json.dumps(serialization)

            dictdata = json.loads(jsondata)
        except Exception as e:
            self.Log.error("Error get payload")

        return dictdata

    def stationsData(self):
        jsondata = self.getData()
        mainstation = jsondata.get("data")

        stations = mainstation.get("stations")

        print('Stations :',len(stations))

        return stations
    def activeStationsData(self):
        satations = self.stationsData()
        activestationcount = 0
        for i in satations:
            if i.get('station_status') == 'active':
                activestationcount += 1
            self.docks.append(i.get('num_docks_available'))

            self.bikes.append(i.get('num_bikes_available'))
        return activestationcount

    def docksQty(self):
        #print(self.docks)
        return self.docks

    def bikesQty(self):
        #print(self.bikes)
        return self.bikes

s = StatioN_Status()
a = s.activeStationsData()
d = s.docksQty()
print(sum(d))
print(sum(s.bikesQty()))


from Python.Design.KuponBruteForceProcessFormUI import Ui_KuponBruteForceProcessForm
from PyQt5 import QtWidgets

#Model
from Python.Model.Product import Product

from PyQt5.QtCore import QTimer #timer
from PyQt5.QtCore import QTime
from time import gmtime, strftime , time
import Python.MessageBox as MessageBox

from PyQt5.QtTest import QTest

class KuponBruteForceProcessForm(QtWidgets.QMainWindow, Ui_KuponBruteForceProcessForm):
    ISLEM_BASLADI_MI = False
    BROWSER_GELDI = False
    time_counter_sec = 0
    len_couponList = 0
    active_coupon_index = 0
    basarili_sayisi = 0
    basarisiz_sayisi = 0
    basarili_kupon_list = []
    def __init__(self, parent=None):
        super(KuponBruteForceProcessForm, self).__init__(parent)
        self.setupUi(self)
        self.timer = QTimer()
        self.timer.timeout.connect(self.time_counter)
        self.durdur_button.clicked.connect(self.durdur)

    def setProduct(self,product:Product):
        self.product = product

    def loadProduct(self):
        self.urun_domain_edit.setText(self.product.get_domain())
        self.urun_ismi_edit.setText(self.product.get_isim())
        self.urun_url_edit.setText(self.product.get_link())
        self.urun_fiyat_edit.setText(str(self.product.get_fiyat()))

        if self.product.get_stok():
            self.urun_stok_edit.setText("VAR")
        else:
            self.urun_stok_edit.setText("YOK")

    def time_counter(self):
        self.time_counter_sec += 1
        myTime = strftime('%H:%M:%S', gmtime(self.time_counter_sec)) # second to hour:minute:second
        myTime = QTime.fromString(str(myTime), 'hh:mm:ss') # string to QTime
        self.gecen_sure_time_edit.setTime(myTime) # set QTime to QTimeEdit

        self.progress_label.setText(f"{self.active_coupon_index} of {self.len_couponList} (Başarılı: {self.basarili_sayisi}, Başarısız: {self.basarisiz_sayisi})")

        #progress bar
        self.progressBar.setValue(self.active_coupon_index)

        if self.active_coupon_index == self.len_couponList:
            self.timer.stop()
            self.ISLEM_BASLADI_MI = False
            if self.basarili_sayisi > 0:
                MessageBox.getBasicMB(self,"Bitti",f"İşlem başarıyla tamamlandı. Başarılı kupon sayısı: {self.basarili_sayisi}")
            else:
                MessageBox.getBasicMB(self,"Bitti","İşlem başarısız.")
            self.close()

    def setCouponList(self,couponList):
        self.couponList = couponList
        self.len_couponList = len(couponList)
        self.progressBar.setMaximum(self.len_couponList)
        self.progress_label.setText(f"{self.active_coupon_index} of {self.len_couponList} (Başarılı: {self.basarili_sayisi}, Başarısız: {self.basarisiz_sayisi})")

        #progress bar
        self.progressBar.setValue(self.active_coupon_index)

    def durdur(self):
        self.ISLEM_BASLADI_MI = False
        self.close()
        MessageBox.getBasicMB(self,"Durdur","İşlem durduruldu.")

    def getBrowser(self,browser):
        self.browser = browser
        self.BROWSER_GELDI = True

    def start_process(self):
        if self.BROWSER_GELDI:
            self.ISLEM_BASLADI_MI = True
            self.timer.start(1000)
            if self.browser.add_product_to_basket(self.product):

                self.browser.get_basket(self.product)
                for coupon in self.couponList:
                    self.aktif_kupon_edit.setText(coupon)
                    result = self.browser.try_coupon(self.product,coupon)
                    if result:
                        self.basarili_sayisi += 1
                        self.basarili_kupon_list.append(coupon)

                    else:
                        self.basarisiz_sayisi += 1
                    self.active_coupon_index += 1
                    QTest.qWait(200)

            else:
                print("ürün eklenmedi.")