import numpy as np
import numpy.random as rnd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

np.random.seed(42)
m = 100
X = 6 * np.random.rand(m, 1) - 3
y = 0.2* X ** 4 + 0.5 * X**3 - 0.2 * X**2 + X + 2 + np.random.randn(m, 1)

from sklearn.preprocessing import PolynomialFeatures
poly_features = PolynomialFeatures(degree=4, include_bias=False)

X_poly = poly_features.fit_transform(X)
lin_reg = LinearRegression()
lin_reg.fit(X_poly, y)

X_new=np.linspace(-3, 3, 100).reshape(100, 1)
X_new_poly = poly_features.transform(X_new)
y_new = lin_reg.predict(X_new_poly)

print( lin_reg.intercept_, lin_reg.coef_)
plt.plot(X, y, "b.")
plt.plot(X_new, y_new, "r-", linewidth=2, label="Predictions")
plt.xlabel("$x_1$", fontsize=18)
plt.ylabel("$y$", rotation=0, fontsize=18)
plt.legend(loc="upper left", fontsize=14)
plt.axis([-3, 3, 0, 10])
plt.show()

from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split

def plot_learning_curves(model, X, y):
    X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=10)
    train_errors, val_errors = [], []
    for m in range(1, len(X_train)):
        model.fit(X_train[:m], y_train[:m])
        y_train_predict = model.predict(X_train[:m])
        y_val_predict = model.predict(X_val)
        train_errors.append(mean_squared_error(y_train[:m], y_train_predict))
        val_errors.append(mean_squared_error(y_val, y_val_predict))

    plt.plot(np.sqrt(train_errors), "r-+", linewidth=2, label="train")
    plt.plot(np.sqrt(val_errors), "b-", linewidth=3, label="val")
    plt.legend(loc="upper right", fontsize=14)   # not shown in the book
    plt.xlabel("Training set size", fontsize=14) # not shown
    plt.ylabel("RMSE", fontsize=14)              # not shown

lin_reg = LinearRegression()
plot_learning_curves(lin_reg, X, y)
plt.axis([0, 80, 0, 3])                         # not shown in the book
# plt.show()                                      # not shown

from sklearn.linear_model import Ridge

np.random.seed(42)
m = 20
X = 3 * np.random.rand(m, 1)
y = 1 + 0.5 * X + np.random.randn(m, 1) / 1.5
X_new = np.linspace(0, 3, 100).reshape(100, 1)
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler

def plot_model(model_class, polynomial, alphas, **model_kargs):
    for alpha, style in zip(alphas, ("b-", "g--", "r:")):
        model = model_class(alpha, **model_kargs) if alpha > 0 else LinearRegression()
        if polynomial:
            model = Pipeline([
                    ("poly_features", PolynomialFeatures(degree=10, include_bias=False)),
                    ("std_scaler", StandardScaler()),
                    ("regul_reg", model),
                ])
        model.fit(X, y)
        y_new_regul = model.predict(X_new)
        lw = 2 if alpha > 0 else 1
        plt.plot(X_new, y_new_regul, style, linewidth=lw, label=r"$\alpha = {}$".format(alpha))
    plt.plot(X, y, "b.", linewidth=3)
    plt.legend(loc="upper left", fontsize=15)
    plt.xlabel("$x_1$", fontsize=18)
    plt.axis([0, 3, 0, 4])

plt.figure(figsize=(8,4))
plt.subplot(121)
plot_model(Ridge, polynomial=False, alphas=(0, 10, 100), random_state=42)

plt.ylabel("$y$", rotation=0, fontsize=18)
plt.subplot(122)
plot_model(Ridge, polynomial=True, alphas=(0, 10**-5, 1), random_state=42)

from  sklearn.linear_model  import Ridge
ridge_reg = Ridge(alpha=1, solver="cholesky", random_state=42)
ridge_reg.fit(X, y)
print(  ridge_reg.predict( [[1.5]] ) )
from sklearn.linear_model import SGDRegressor

sgd_reg = SGDRegressor(max_iter=5, penalty="l2", random_state=42)
sgd_reg.fit(X, y.ravel())
print(sgd_reg.predict([[1.5]]))

ridge_reg = Ridge(alpha=1, solver="sag", random_state=42)
ridge_reg.fit(X, y)
print( ridge_reg.predict([[1.5]]) )

from  sklearn.linear_model  import Lasso

plt.figure(figsize=(8,4))
plt.subplot(121)
plot_model(Lasso, polynomial=False, alphas=(0, 0.1, 1), random_state=42)
plt.ylabel("$y$", rotation=0, fontsize=18)
plt.subplot(122)
plot_model(Lasso, polynomial=True, alphas=(0, 10**-7, 1), tol=1, random_state=42)

plt.show()


# -*- coding: utf-8 -*-

import matplotlib as mtl
import matplotlib.pyplot as plt
import openpyxl as px
import csv
import platform
pf = platform.system()

fname = '/Users/yokooannosuke/Cording/Pine64-python--pulse/Pulsesimulator/pulse_simdata.xlsm'

wb = px.load_workbook(fname, keep_vba=True, data_only=True)
ws = wb.get_sheet_by_name('Pulseパラメータ')

#################################################
PLsc1 = []
PLec1 = []
i = 1
while ws.cell(row=i, column=1).value != None:
    PLsc1.append(ws.cell(row=i, column=1).value)
    PLec1.append(ws.cell(row=i, column=3).value)
    i += 1
del PLsc1[0:3]
del PLec1[0:3]
# print(PLsc1)
# print(PLec1)

x_pri = PLsc1 + PLec1
x_list = sorted(x_pri)
x_value1 = sorted(x_list + x_list)

y_list = [0, 1, 1, 0]
y_value1 = []
for i in range(len(PLsc1)):
    y_value1 += y_list

if x_value1[0] != 0:
    x_value1.insert(0, 0)
    y_value1.insert(0, 0)
else:
    pass
# print(x_value1)
# print(y_value1)

##################################################

ADsc1 = []
ADec1 = []
i = 1
while ws.cell(row=i, column=13).value != None:
    ADsc1.append(ws.cell(row=i, column=13).value)
    ADec1.append(ws.cell(row=i, column=15).value)
    i += 1
del ADsc1[0:3]
del ADec1[0:3]
# print(ADsc1)
# print(ADec1)

x_pri = ADsc1 + ADec1
x_list = sorted(x_pri)
x_value4 = sorted(x_list + x_list)

y_value4 = []
for i in range(len(ADsc1)):
    y_value4 += y_list

if x_value4[0] != 0:
    x_value4.insert(0, 0)
    y_value4.insert(0, 0)
else:
    pass
# print(x_value4)
# print(y_value4)

####################################################

dds_csv = open(
    '/Users/yokooannosuke/Cording/Pine64-python--pulse/Pulsesimulator/pulse_simDDSdata.csv')

DDS_sc1 = []
DDSsc1_int = []
DDS_data = []
DDS_list = []

for row in csv.reader(dds_csv):
    DDS_sc1.append(row[0])
    DDS_data.append(row[4])
del DDS_sc1[0:3]
del DDS_data[0:3]

# 読み込んだデータをint型に変換
for i in range(len(DDS_sc1)):
    DDSsc1_int.append(int(DDS_sc1[i]))

    # 40bitdataを一文字ずつにして配列に格納
    chars = list(DDS_data[i].strip())
    DDS_list.append(chars)
# 読み込んだ配列をint型に変換
for j in range(len(DDS_list)):
    for i in range(len(DDS_list[0])):
        if str == type(DDS_list[j][i]):
            DDS_list[j][i] = (int(DDS_list[j][i]))
print(DDS_list)
print(DDSsc1_int)

####################################################

x1, y1 = x_value1, y_value1
x4, y4 = x_value4, y_value4
fig = plt.figure()

ax1 = fig.add_subplot(211, title='PL1', yticks=[0, 1], yticklabels=["0", "1"])
ax4 = fig.add_subplot(212, title='AD1',  xlabel='counter', yticks=[
                      0, 1], yticklabels=["0", "1"], sharex=ax1)

ax1.plot(x1, y1)
ax4.plot(x4, y4)

fig.savefig('simulation', facecolor=fig.get_facecolor())


#!/usr/bin/env python3

import psycopg2

DBNAME = "news"

def main():
    """Connect to the PostgreSQL database and return a connection object."""
    try:
        db = psycopg2.connect(database=DBNAME)
        c = db.cursor()
    except (UnboundLocalError):
        print("Unable to connect to database.")

    # 1. What are the most popular three articles of all time?
    sql_popular_articles = """
        SELECT articles.title, COUNT(*) AS num
        FROM articles
        JOIN log
        ON log.path LIKE concat('/article/%', articles.slug)
        GROUP BY articles.title
        ORDER BY num DESC
        LIMIT 3;
    """
    c.execute(sql_popular_articles)
    articles = c.fetchall()

    print("MOST POPULAR ARTICLES:\n")
    for article in articles:
        formatted_response = "{} - {} views".format(article[0], article[1])
        print(formatted_response)

    print("\n=================\n")

    # 2. Who are the most popular article authors of all time?
    sql_popular_authors = """
        SELECT authors.name, COUNT(articles.author) as views
        FROM articles, log, authors
        WHERE log.path = concat('/article/',articles.slug) and
        articles.author = authors.id
        GROUP by authors.name
        ORDER by views desc
    """
    c.execute(sql_popular_authors)
    authors = c.fetchall()

    print("MOST POPULAR AUTHORS:\n")
    for author in authors:
        clean_title = author[0]
        formatted_response = "{} - {} views".format(clean_title, author[1])
        print(formatted_response)

    print("\n=================\n")

    # 3. On which days did more than 1% of requests lead to errors?
    sql_request_errors = """
        SELECT Date, Total, Error, (Error::float*100)/Total::float
        as Percent
        FROM (select time::timestamp::date as Date, COUNT(status) as Total,
        SUM(case when status = '404 NOT FOUND' then 1 else 0 end) as Error
        FROM log
        GROUP by time::timestamp::date) as result
        WHERE (Error::float*100)/Total::float > 1.0 ORDER by Percent desc;
    """
    c.execute(sql_request_errors)
    error = c.fetchall()

    print("DAY WITH MOST ERRORS:\n")
    print("{} - {}%".format(error[0][0], round(error[0][3], 1)))

    db.close()

if __name__ == "__main__":
    main()


import numpy as np
import pandas as pd
import csv

import sys
xTstPath = sys.argv[1]
outputPath = sys.argv[2]

tst_x = pd.read_csv(xTstPath)
tst_x = tst_x['feature']
pixels = tst_x.values
X = np.zeros((pixels.shape[0], 48*48))
for ix in range(X.shape[0]):
    p = pixels[ix].split(' ')
    for iy in range(X.shape[1]):
        X[ix, iy] = int(p[iy])

# X -= np.mean(X, axis=0)
# X /= np.std(X, axis=0)
X /= 255

X = X.reshape((X.shape[0], 48, 48, 1))

# print(X[0:10].shape)
from keras.models import load_model
model = load_model('./b_model.179-0.6742.h5')
pred = model.predict(X)

# print(type(pred))
# print(len(pred))
# print(pred[0])
# print(pred)

ans = []
for i in range(len(pred)):
    ans.append([str(i)])
    a = np.argmax(pred[i])
    ans[i].append(a)

# print(ans)

text = open(outputPath, "w+")
s = csv.writer(text,delimiter=',',lineterminator='\n')
s.writerow(["id","label"])
for i in range(len(ans)):
    s.writerow(ans[i])
text.close()


import sys
import os
import numpy as np
import cv2
import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot as plt
from keras import backend as K
from keras.preprocessing import image

import tensorflow as tf
from tensorflow.python.framework import ops
import datetime
from config import config_def
import config_env
from dataset_loader import DatasetLoader

config_dataset = config_env.config_dataset
config_model = config_env.config_model

preprocess_input = config_model.preprocess_input
decode_predictions = config_model.decode_predictions

fileSuffix = '_gradcam'
datetimePrefix = datetime.datetime.today().strftime("%Y%m%d_%H%M%S") + '_'
datasetPrefix = "%s_%s" % (config_model.modelName, config_dataset.datasetName)
filePrefix = datetimePrefix + datasetPrefix
weights_dir = '../weights/'
bestWeightsFile = os.path.join(weights_dir, datasetPrefix + "_best.hdf5")

testOutputDir = os.path.join('../testResults', filePrefix + fileSuffix)
os.makedirs(testOutputDir, exist_ok=True)
def formatPredictResultFilename(index, expected, actual, filename):
  if config_dataset.class_mode == 'categorical':
    classes = config_dataset.classes
    expected = classes[expected]
    actual = classes[actual] if actual != None else None
    fmt='{}'
  else:
    fmt='{:.2f}'
  if actual == None: fmt = '{}'

  return ('{:0>8}_expect_{}_pred_' + fmt + '_{}').format(index, expected, actual, os.path.basename(filename))

datasetLoader = DatasetLoader(config_env, config_dataset, config_model, augumentation=False)
weightsFile = bestWeightsFile
cachedModel = None
def build_model(resetCache = False):
    global cachedModel
    if not resetCache and cachedModel: return cachedModel
    model = config_model.create_model(config_dataset)
    print("load weights: {}".format(weightsFile))
    model.load_weights(weightsFile)
    #model.summary()
    cachedModel = model
    return model

# H, W = 224, 224 # Input shape, defined by the model (model.input_shape)
(W, H) = config_dataset.image_size
# ---------------------------------------------------------------------

def load_image(path, preprocess=True):
    """Load and preprocess image."""
    x = image.load_img(path, target_size=(H, W))
    if preprocess:
        x = image.img_to_array(x)
        x = np.expand_dims(x, axis=0)
        x = preprocess_input(x)
    return x

def grad_cam(input_model, image, cls, layer_name):
    """GradCAM method for visualizing input saliency."""
    y_c = input_model.output[0, cls]
    conv_output = input_model.get_layer(layer_name).output
    grads = K.gradients(y_c, conv_output)[0]
    gradient_function = K.function([input_model.input], [conv_output, grads])

    output, grads_val = gradient_function([image])
    output, grads_val = output[0, :], grads_val[0, :, :, :]

    weights = np.mean(grads_val, axis=(0, 1))
    cam = np.dot(output, weights)

    # Process CAM
    cam = cv2.resize(cam, (W, H), cv2.INTER_LINEAR)
    cam = np.maximum(cam, 0)
    cam_max = cam.max()
    if cam_max != 0:
        cam = cam / cam_max
    return cam

def compute_saliency(model, img_path, realValue, layer_name, index=None):
    preprocessed_input = load_image(img_path)

    ## Removed prediction to avoid memory leaks.
    # predictions = model.predict(preprocessed_input)
    predictions = None

    actual = predictions
    expected = realValue

    if config_dataset.class_mode == 'categorical':
      expected = realValue[0] # [class]. [0] [3] [5]
      actual = np.argmax(actual) if actual else None # one hot.
      cls = expected
    elif config_dataset.class_mode == 'binary':
      classes = config_dataset.classes
      expected = classes.index(expected) # '000_normal'
      actual = actual[0][0] if actual else None
      cls = 0
    else: # regression
      actual = actual[0][0] if actual else None
      cls = 0

    #class_name = decode_predictions(np.eye(1, 1000, cls))[0][0][1]
    #print("Explanation for '{}'".format(class_name))

    gradcam = grad_cam(model, preprocessed_input, cls, layer_name)

    jetcam = cv2.applyColorMap(np.uint8(255 * gradcam), cv2.COLORMAP_JET)
    jetcam = (np.float32(jetcam) + load_image(img_path, preprocess=False)) / 2
    prefix = formatPredictResultFilename(index, expected, actual, img_path)
    cv2.imwrite(os.path.join(testOutputDir, prefix + '_gradcam.jpg'), np.uint8(jetcam))

    return gradcam

def printInfo(s):
  print('{:%Y-%m-%d %H:%M:%S}: {}'.format(datetime.datetime.now(), s))

layer_name = config_model.gradcam_layer_name
if not layer_name:
  print("Missing variable gradcam_layer_name in config_model.")
  exit(1);
if __name__ == '__main__':
    printInfo("output dir={}".format(testOutputDir))
    modelReuseCount = 0
    for index, (filepath, value) in enumerate(datasetLoader.dictFiles('test').items()):
      printInfo(filepath)
      # Reset Tensorflow session periodically to avoid memory leaks.
      if modelReuseCount == 0:
        printInfo("Reset Tensorflow session.")
        K.clear_session()
        model = build_model(resetCache = True)
        printInfo("... done")
      modelReuseCount = (modelReuseCount + 1) % 10
      gradcam = compute_saliency(model, layer_name=layer_name,
                                 img_path=filepath, realValue=value, index=index)


import multiprocessing
import exifread
import rawpy
import os
import shutil
import PIL
from PIL import Image
from datetime import datetime
from timeit import default_timer
from time import sleep
from SUASSystem import GCSSettings
from SUASSystem.suas_logging import log
from ImgProcessingCLI.Runtime.RuntimeTarget import RuntimeTarget
from ImgProcessingCLI.DataMine.OrientationSolver import OrientationSolver
from ImgProcessingCLI.DataMine import KMeansCompare
from ImgProcessingCLI.Runtime import TargetCropper3
from EigenFit.DataMine import Categorizer
from ImgProcessingCLI.Runtime.GeoStamp import GeoStamp
from ImgProcessingCLI.Runtime.GeoStamps import GeoStamps
from EigenFit.Load import *
from ImgProcessingCLI.Runtime.TargetCrop import TargetCrop

def run_img_proc_process(logger_queue, img_proc_status, location_log, targets_to_submit):
    SUASSystem.logging.logger_worker_configurer(logger_queue)
    logger_name = multiprocessing.current_process().name

    log(logger_name, "Instantiating letter categorizer")
    eigenvectors = load_numpy_arr(GCSSettings.BASE_LETTER_CATEGORIZER_PCA_PATH + "/Data/Eigenvectors/eigenvectors 0.npy")
    projections_path = GCSSettings.BASE_LETTER_CATEGORIZER_PCA_PATH + "/Data/Projections"
    mean = load_numpy_arr(GCSSettings.BASE_LETTER_CATEGORIZER_PCA_PATH + "/Data/Mean/mean_img 0.npy")
    num_dim = 20
    letter_categorizer = Categorizer(eigenvectors, mean, projections_path, KMeansCompare, num_dim)
    log(logger_name, "Letter categorizer instantiated successfully")

    log(logger_name, "Instantiating orientation solver")
    orientation_eigenvectors = load_numpy_arr(GCSSettings.BASE_ORIENTATION_CLASSIFIER_PCA_PATH + "/Data/Eigenvectors/eigenvectors 0.npy")
    orientation_projections_path = GCSSettings.BASE_ORIENTATION_CLASSIFIER_PCA_PATH + "/Data/Projections"
    orientation_mean = load_numpy_arr(GCSSettings.BASE_ORIENTATION_CLASSIFIER_PCA_PATH + "/Data/Mean/mean_img 0.npy")
    orientation_num_dim = 50
    orientation_solver = OrientationSolver(orientation_eigenvectors, orientation_mean, GCSSettings.BASE_ORIENTATION_CLASSIFIER_PCA_PATH, orientation_num_dim)
    log(logger_name, "Orientation solver instantiated")

    if os.path.exists(GCSSettings.GENERATED_DATA_LOCATION):
        shutil.rmtree(GCSSettings.GENERATED_DATA_LOCATION)

    os.mkdir(GCSSettings.GENERATED_DATA_LOCATION)
    os.mkdir(os.path.join(GCSSettings.GENERATED_DATA_LOCATION, "object_file_format"))

    sd_path = os.path.join("/Volumes", GCSSettings.SD_CARD_NAME, "DCIM")
    gps_coords = []

    while True:
        if "enabled" in str(img_proc_status.value).lower():
                if len(location_log) != 0:
                    try:
                        gps_coords.append(location_log[0])
                        gps_coords = gps_coords[1:]
                    except:
                        pass

                log(logger_name, "Waiting for path to exist...")

                # Wait for SD card to be loaded
                if not os.path.exists(sd_path):
                    sleep(1)

                    continue

                # Once SD card is loaded, begin processing
                # For every image on the SD card
                # 1. Load the image
                # 2. Process the crops, determine the ones that are targets
                # 3. Save the targets to GENERATED_DATA_LOCATION
                # 4. Upload the targets to interop server
                log(logger_name, "Beginning image processing...")
                geo_stamps = GeoStamps([GeoStamp([38.38875, -77.27532], datetime.now())])#gps_coords)
                crop_index = 0

                all_target_crops = []
                for pic_folder in os.listdir(sd_path):
                    pictures_dir_path = os.path.join(sd_path, pic_folder)

                    for pic_name in os.listdir(pictures_dir_path):
                        if ".SRW" in pic_name:
                            start_time = default_timer()
                            log(logger_name, "Loading image " + pic_name)
                            pic_path = os.path.join(pictures_dir_path, pic_name)
                            img = rawpy.imread(pic_path).postprocess()
                            rgb_image = Image.fromarray(numpy.roll(img, 1, axis=0))
                            image_timestamp = get_image_timestamp(pic_path)

                            target_crops = TargetCropper3.get_target_crops_from_img(rgb_image, image_timestamp, geo_stamps, 6)
                            target_crops = TargetCrop.get_non_duplicate_crops(all_target_crops, target_crops, GCSSettings.MIN_DIST_BETWEEN_TARGETS_KM)
                            all_target_crops.extend(target_crops)
                            log(logger_name, "Finished processing " + pic_name + " in " + str(default_timer() - start_time) + " seconds")

                            for target_crop in target_crops:
                                try:
                                    log(logger_name, "Identifying target characteristics of target #" + str(crop_index))
                                    runtime_target = RuntimeTarget(target_crop, letter_categorizer, orientation_solver)
                                    target_json_output = runtime_target.get_competition_json_output()

                                    log(logger_name, "Saving target characteristics of target #" + str(crop_index))
                                    output_pic_name = os.path.join(GENERATED_DATA_LOCATION, "object_file_format", str(crop_index) + ".png")
                                    output_json_name = os.path.join(GENERATED_DATA_LOCATION, "object_file_format", str(crop_index) + ".txt")
                                    save_json_data(output_json_name, {"target_json_output" : "Testing"})#target_json_output)
                                    target_crop.get_crop_img().save(output_pic_name)

                                    crop_index += 1
                                except:
                                    log(logger_name, "ERROR: Could not process a target crop")

        sleep(0.5)

def get_image_timestamp(filename):
    """
    Returns an image's timestamp
    """
    opened_file = open(filename, 'rb')
    tags = exifread.process_file(opened_file)
    image_raw_time = tags['Image DateTime']
    converted_time = datetime.strptime(str(image_raw_time), "%Y:%m:%d %H:%M:%S")

    return converted_time


import sys

import pygame as pg
from pygame.locals import *

from display import window
from game import snake_sprite, apple_sprite
from constants import *

pg.init()

def run():
    screen= window.createWindow()
    clock = pg.time.Clock()

    snake = snake_sprite.Snake()
    apple = apple_sprite.Apple()

    pg.display.flip()

    while True:
        clock.tick(15)

        for event in pg.event.get():
            if event.type == pg.QUIT:
                sys.exit()

            elif event.type == pg.KEYDOWN:
                if event.key == pg.K_ESCAPE:
                    sys.exit()

                elif event.key >= pg.K_UP and event.key <= pg.K_LEFT:
                    snake.change_dir(event.key)

                break

        snake_updates = snake.update()
        ate = snake_updates.pop(0)

        apple_rect = []
        if ate:
            apple_rect = apple.place_apple()

        pg.display.update(snake_updates + apple_rect)


import pyspeckit
from spectral_cube import SpectralCube
from astropy import units as u

OneOneFile = '/home/monsch/Desktop/NH3/project/omc1_vla_11_JEP_clean_spw0_mscale_v1.fits'
cube11sc = SpectralCube.read(OneOneFile)
moment_0 = cube11sc.moment(order=0)
moment_1 = cube11sc.moment(order=1)

cube11 = pyspeckit.Cube(OneOneFile)

cube11.xarr.convert_to_unit(u.km/u.s, equivalencies=u.spectral())
cube11.xarr.refX = pyspeckit.spectrum.models.ammonia.freq_dict['oneone']

spf = pyspeckit.wrappers.fitnh3.fitnh3(cube11,vrange=[-100, 100], vrangeunit='km/s', quiet=False,Tex=20, Tkin=15, column=1000000000000000.0, fortho=1.0,tau=None)


import click
from acoustid.script import Script
from acoustid.uwsgi_utils import run_web_app, run_api_app
from acoustid.cron import run_cron
from acoustid.scripts.import_submissions import run_import

@click.group()
def cli():
    pass

@cli.group()
def run():
    pass

@run.command('web')
@click.option('-c', '--config', default='acoustid.conf', envvar='ACOUSTID_CONFIG')
@click.option('-w', '--workers', type=int, envvar='ACOUSTID_WEB_WORKERS')
def run_web_cmd(config, workers):
    """Run production uWSGI with the website."""
    script = Script(config)
    script.setup_console_logging()
    script.setup_sentry()
    run_web_app(script.config, workers=workers)

@run.command('api')
@click.option('-c', '--config', default='acoustid.conf', envvar='ACOUSTID_CONFIG')
@click.option('-w', '--workers', type=int, envvar='ACOUSTID_API_WORKERS')
def run_api_cmd(config, workers):
    """Run production uWSGI with the API."""
    script = Script(config)
    script.setup_console_logging()
    script.setup_sentry()
    run_api_app(script.config, workers=workers)

@run.command('cron')
@click.option('-c', '--config', default='acoustid.conf', envvar='ACOUSTID_CONFIG')
def run_cron_cmd(config):
    """Run cron."""
    script = Script(config)
    script.setup_console_logging()
    script.setup_sentry()
    run_cron(script)

@run.command('import')
@click.option('-c', '--config', default='acoustid.conf', envvar='ACOUSTID_CONFIG')
def run_import_cmd(config):
    """Run import."""
    script = Script(config)
    script.setup_console_logging()
    script.setup_sentry()
    run_import(script)

def main():
    cli()


#!/usr/bin/env python

import math
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("-n", type=int, help = "total number of items to choose from")
parser.add_argument("-k", type=int, help = "number of items to choose")
parser.add_argument("-l", "--log", action="store_true", help = "returns the log binomial coefficient")
parser.add_argument("-t", "--test", action="store_true", help="tests the module and quits")
args = parser.parse_args()

def logfactorial(n, k = 0):
    '''Takes an integer n and calculates log(n!) = log(1) + log(2) + ... + log(n)

    >>> logfactorial(1)
    0.0
    >>> round(logfactorial(10),5)
    15.10441
    '''

    assert int(n) == n, 'Error: input should be an integer -- it is not!'
    assert n > 0, 'Error: input should be positive!'

    if k != None:
        assert int(k) == k, 'Error: input k should be an integer'
        assert k >= 0, 'Error: input k should be positive'

    result=0

    for i in range(k+1, n+1):
        result += math.log(i)

    return(result)

def choose(n,k, log = False):
    '''Takes two positive integers, n and k, and calculates n choose k.

    >>> choose(5,1)
    5
    >>> choose(5,3)
    10
    >>> choose(5,10)
    0
    '''

    assert int(n) == n, 'Error: inputs should be an integer -- n it is not!'
    assert n > 0, 'Error: inputs should be positive -- n is not'
    assert int(k) == k, 'Error: inputs should be an integer -- k is not!'
    assert k > 0, 'Error: input should be positive -- k is not'

    if n < k:
        return(0)
    else:
        result = logfactorial(n,k) - logfactorial(n-k)

    if not log:
        ## Note: use int(round(...)) to force round to nearest integer, and return integer.
        ## Using only round will return float, using only integer, will return only integer part of number.
        ## For example, int(8.9) = 8
        result = int(round(math.exp(result)))

    return(result)

## If run as script (i.e. not imported)...
if __name__ == '__main__':
    ## ... and --test not specified:
    if not args.test:
        ## - check n
        if args.n<=0:
            raise Exception("argument -n must be positive")
        ## - check k
        elif args.k<0:
            raise Exception("argument -k must be 0 positive")
        ## if both n and k pass, run and print choose with user specified values
        else:
            print(choose(args.n, args.k, args.log))
    else:
        ## ... if --test is specified, run tests.
        print("Testing the module...")
        import doctest
        doctest.testmod()
        print("DONE!")


import feedparser

class PeopleOnTheISS:

    def __init__(self, agentConfig, checksLogger, rawConfig):
        self.agentConfig = agentConfig
        self.checksLogger = checksLogger
        self.rawConfig = rawConfig

    def run(self):
        url = "http://feeds.feedburner.com/SpaceRightNow"
        feed = feedparser.parse(url)
        current = feed[ "items" ][0]["title_detail"]["value"]
        return {"People on the ISS" : current}


from concurrent.futures import ThreadPoolExecutor
from work_with_db import get_data_from_table
from sql import SQL
import datetime
import logging

while True:
    db = input(
        '''
УКАЖИ СХЕМУ ДЛЯ ПОДКЛЮЧЕНИЯ НАПРИМЕР:

IBS/IBS_EXPOBANK1908@BARKDB.BARK_RPT
IBS/IBS@57_TEST_REP_OMSK
IBS/IBS@57_TEST_REP_TOMSK
IBS/IBS@57_TEST_REP_NSK1
IBS/IBS@57_TEST_REP_NSK2

СХЕМА:
''')
    logging.disable(logging.DEBUG)

    num = 0
    table_names = list(SQL.keys())
    for table_name in table_names: print(f'{num}.', table_name); num += 1

    tables = input('\nУКАЖИ НОМЕРА ВЫГРУЖАЕМЫХ ТАБЛИЦ В ФОРМАТЕ (12 или 0123):')
    id_realization_1 = str(input("ВВЕДИ id 1 РЕАЛИЗАЦИИ:"))
    id_realization_2 = str(input("ВВЕДИ id 2 РЕАЛИЗАЦИИ:"))

    startTime = datetime.datetime.today()
    print(startTime)
    Params = []
    for table in tables:
        table_name = table_names[int(table)]
        sql_query = SQL[table_name]
        if id_realization_2 != '':
            Params += [(table_name + '1', id_realization_1, id_realization_2, sql_query, db)]
            Params += [(table_name + '2', id_realization_2, id_realization_1, sql_query, db)]
        else:
            Params += [(table_name, id_realization_1, None, sql_query, db)]

    with ThreadPoolExecutor(max_workers=len(Params)) as executor:
        logging.debug('Запускаем потоки')
        list(map(lambda param: executor.submit(get_data_from_table, param), Params))

    endTime = datetime.datetime.today()
    print(f"ЗАВЕРШЕНО {endTime - startTime}")


# -*- coding: utf-8 -*-

import time
from pynput.keyboard import Controller
from deval.component.std.keyevent import KeyEventComponent

class MacKeyEventComponent(KeyEventComponent):

    def __init__(self, name):
        self._name = name
        self.keyboard = Controller()

    def keyevent(self, keyname):
        """
        Use pynput to simulate keyboard input

        Parameters:
            keyname - the keys
        """
        waittime = 0.05
        for c in keyname:
            self.keyboard.press(key=c)
            self.keyboard.release(key=c)
            time.sleep(waittime)

    def text(self, text, enter=False):
        """
        Use pynput to simulate keyboard input

        Parameters:
            keyname - the keys
        """
        waittime = 0.05
        for c in text:
            self.keyboard.press(key=c)
            self.keyboard.release(key=c)
            time.sleep(waittime)

    @property
    def name(self):
        return self._name

    @name.setter
    def name(self, value):
        self._name = value


import socket
import os
import time
import smtplib
import email.message

SLEEP_INT = int(os.getenv('SLEEP_INT'))
TIMEOUT = int(os.getenv('TIMEOUT'))

SMTP_SERVER = os.getenv('SMTP_SERVER')
EMAIL_RECIPIENT = os.getenv('EMAIL_RECIPIENT')
EMAIL_SENDER = os.getenv('EMAIL_SENDER')

EMAIL_SUBJECT = 'Warning from Rancher-SocketTester'

def try_send_email(subject, body):
    try:
        msg = email.message.Message()
        msg['Subject'] = subject
        msg['From'] = EMAIL_SENDER
        msg['To'] = EMAIL_RECIPIENT
        msg.add_header('Content-Type', 'text/html')
        msg.set_payload(body)
        s = smtplib.SMTP(SMTP_SERVER)
        s.sendmail(EMAIL_SENDER, EMAIL_RECIPIENT, msg.as_string())
    except Exception as e:
        print('Warning: could not send email: ' + e)

def test_socket_connection(host, port):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(TIMEOUT)
    try:
        s.connect((host, port))
        print('Connected to socket: %s:%d' % (host, port))
    except socket.error as e:
        msg = "Could not connect to %s:%d - %s" % (host, port, e)
        print(msg)
        try_send_email(EMAIL_SUBJECT, msg)
    s.close()

def main():
    while True:
        host = os.getenv('HOST')
        port = int(os.getenv('PORT'))
        test_socket_connection(host, port)
        time.sleep(SLEEP_INT)

if __name__ == '__main__':
    main()


import re
from typing import Optional

import yaml
from cfn_tools import load_json, load_yaml, ODict, CfnYamlLoader
from cfn_tools.yaml_dumper import TAG_MAP
from cloudrail.knowledge.context.cloud_provider import CloudProvider
from cloudrail.knowledge.context.iac_type import IacType
from cloudrail.knowledge.utils.file_utils import read_file
from cloudrail.knowledge.utils.string_utils import StringUtils

ELEMENT_POSITION_KEY: str = 'cfn_resource_block_position'
NODE_KEY_LINE_REGEX: str = '"{}"\\s*:'

# pylint: disable=R1710
class CustomCloudformationLoader(CfnYamlLoader):
    pass

def construct_mapping(self, node, deep=False):
    """
    Use ODict for maps
    """

    mapping = ODict()
    for key_node, value_node in node.value:
        key = self.construct_object(key_node, deep=deep)
        value = self.construct_object(value_node, deep=deep)
        mapping[key] = value

    if 'Type' in mapping and ELEMENT_POSITION_KEY not in mapping:
        mapping[ELEMENT_POSITION_KEY] = (node.start_mark.line, node.end_mark.line - 1)

    return mapping

CustomCloudformationLoader.add_constructor(TAG_MAP, construct_mapping)

class CloudformationUtils:

    EXTRA_PARAMETERS_KEY: str = 'cr_extra_params'

    @classmethod
    def validate_required_cfn_template_parameters(cls, cfn_template_file: str, cfn_template_parameters: dict):
        cfn_template_content: dict = cls.load_cfn_template(cfn_template_file)
        template_parameters: dict = cfn_template_content.get('Parameters', {})
        for param_name in template_parameters:
            if param_name not in cfn_template_parameters and 'Default' not in template_parameters[param_name]:
                raise Exception(f'Missing required template parameter "{param_name}"')

    @classmethod
    def create_cfn_template_extra_parameters(cls, cfn_stack_name: str, iac_type: IacType, cloud_provider: CloudProvider,
                                             cfn_stack_region: str, account_id: str, cfn_template_file_name: str,
                                             account_name: Optional[str] = None, cfn_parameters: dict = None) -> dict:
        extra_parameters = {'stack_name': cfn_stack_name,
                            'iac_type': iac_type,
                            'cloud_provider': cloud_provider,
                            'region': cfn_stack_region,
                            'account_id': account_id,
                            'cfn_template_file_name': cfn_template_file_name}

        if account_name:
            extra_parameters['account_name'] = account_name

        if cfn_parameters:
            extra_parameters.update(cfn_parameters)
        return extra_parameters

    @classmethod
    def load_cfn_template(cls, cfn_template_file: str) -> dict:
        if cfn_template_file.endswith('.json'):
            return cls.file_to_cfn_json(cfn_template_file)
        elif cfn_template_file.endswith('.yaml'):
            return cls.file_to_cfn_yaml(cfn_template_file)
        else:
            cls.raise_invalid_cfn_template_file()

    @classmethod
    def cfn_template_str_to_dict(cls, cfn_template_content: str) -> dict:
        if StringUtils.is_json(cfn_template_content):
            return load_json(cfn_template_content)
        elif StringUtils.is_yaml(cfn_template_content):
            return load_yaml(cfn_template_content)
        else:
            cls.raise_invalid_cfn_template_file()

    @classmethod
    def file_to_cfn_json(cls, file_path: str) -> dict:
        try:
            cfn_template_content: str = read_file(file_path)
            cfn_template_as_dict: dict = load_json(cfn_template_content)
            cls._set_cfn_resources_block_position(cfn_template_as_dict, file_path)
            return cfn_template_as_dict
        except Exception as ex:
            message = f'Error while reading CloudFormation template JSON file {file_path}. {ex}'
            raise Exception(message)

    @staticmethod
    def file_to_cfn_yaml(file_path: str) -> dict:
        try:
            return yaml.load(stream=read_file(file_path), Loader=CustomCloudformationLoader)
        except Exception as ex:
            message = f'error while reading CloudFormation template yaml file {file_path}. {ex}'
            raise Exception(message)

    @staticmethod
    def to_odict(ordinary_dict: dict) -> ODict:
        return ODict([[key, val] for key, val in ordinary_dict.items()])

    @staticmethod
    def raise_invalid_cfn_template_file():
        raise Exception('Invalid CloudFormation template file format')

    @classmethod
    def _set_cfn_resources_block_position(cls, cfn_template_as_dict: dict, file_path: str):
        json_doc: str = read_file(file_path)
        cfn_resources: dict = cfn_template_as_dict.get('Resources', {})
        for resource_name, cfn_resource in cfn_resources.items():
            if ELEMENT_POSITION_KEY not in cfn_resource:
                key_pattern: str = NODE_KEY_LINE_REGEX.format(resource_name)
                pattern_occurrences: list = re.split(key_pattern, json_doc)
                if len(pattern_occurrences) > 1:
                    start_line: int = pattern_occurrences[0].count('\n') + 1
                    end_line: int = start_line + cls._end_element_line_number(pattern_occurrences[-1])
                    cfn_resource[ELEMENT_POSITION_KEY] = (start_line, end_line)

    @staticmethod
    def _end_element_line_number(pattern_occurrence: str):
        open_braces_counter: int = 0
        already_opened: bool = False
        index: int = 0
        for character in pattern_occurrence:
            if character == '{':
                already_opened = True
                open_braces_counter += 1
            elif character == '}':
                open_braces_counter -= 1

            if already_opened and open_braces_counter == 0:
                break
            index += 1
        sub_str: str = pattern_occurrence[0: index]
        return sub_str.count('\n')


#!/usr/bin/python3
"""Some random docstring"""
from flask import Flask, render_template
from models import storage
from models.state import State
app = Flask(__name__)

@app.teardown_appcontext
def teardown(self):
    """SHUTS THE SESSION DOWN"""
    storage.close()

@app.route('/states', strict_slashes=False)
@app.route('/states/<id>', strict_slashes=False)
def hello_world_states(id=None):
    """
    ---------------------------
    METHOD: HELLO WORLD /states
    ---------------------------
    Description:
        Generates and returns an HTML with all the
        states if flask hits a ping on 0:5000/states
    Args:
        id: optional argument that generates an HTML
        with all the details of that specific state.
    """
    states = storage.all(State)
    if id is not None:
        id = 'State' + '.' + id
    return render_template('9-states.html',
                           objects=states,
                           id=id
                           )

if __name__ == '__main__':
    app.run(host="0.0.0.0", port="5000")


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
import jsonfield.fields

class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='OAuthToken',
            fields=[
                ('id', models.AutoField(serialize=False, primary_key=True, verbose_name='ID', auto_created=True)),
                ('token', models.TextField(verbose_name='token')),
                ('issued_at', models.DateTimeField(verbose_name='issued at')),
                ('expires_at', models.DateTimeField(null=True, verbose_name='expires at', blank=True)),
            ],
            options={
                'verbose_name': 'OAuth token',
                'verbose_name_plural': 'OAuth tokens',
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='User',
            fields=[
                ('id', models.AutoField(serialize=False, primary_key=True, verbose_name='ID', auto_created=True)),
                ('facebook_id', models.BigIntegerField(verbose_name='facebook id', unique=True)),
                ('facebook_username', models.CharField(null=True, max_length=255, verbose_name='facebook username', blank=True)),
                ('first_name', models.CharField(null=True, max_length=255, verbose_name='first name', blank=True)),
                ('middle_name', models.CharField(null=True, max_length=255, verbose_name='middle name', blank=True)),
                ('last_name', models.CharField(null=True, max_length=255, verbose_name='last name', blank=True)),
                ('birthday', models.DateField(null=True, verbose_name='birthday', blank=True)),
                ('email', models.CharField(null=True, max_length=255, verbose_name='email', blank=True)),
                ('locale', models.CharField(null=True, max_length=255, verbose_name='locale', blank=True)),
                ('gender', models.CharField(null=True, max_length=255, verbose_name='gender', blank=True)),
                ('authorized', models.BooleanField(verbose_name='authorized', default=True)),
                ('created_at', models.DateTimeField(verbose_name='created at', auto_now_add=True)),
                ('last_seen_at', models.DateTimeField(verbose_name='last seen at', auto_now_add=True)),
                ('extra_data', jsonfield.fields.JSONField()),
                ('oauth_token', models.OneToOneField(verbose_name='OAuth token', to='fandjango.OAuthToken')),
            ],
            options={
                'verbose_name': 'user',
                'verbose_name_plural': 'users',
            },
            bases=(models.Model,),
        ),
    ]


import abc
import sqlite3
from contextlib import contextmanager
from os import path
from typing import Any, Dict, Generator, List, Sequence, Sized

from movielog import humanize
from movielog.logger import logger

DB_FILE_NAME = "movie_db.sqlite3"
DB_DIR = "db"

Connection = sqlite3.Connection
Cursor = sqlite3.Cursor
Row = sqlite3.Row

DB_PATH = path.join(DB_DIR, DB_FILE_NAME)
DbConnectionOpts: Dict[str, Any] = {"isolation_level": None}

@contextmanager
def connect() -> Generator[Connection, None, None]:
    connection = sqlite3.connect(DB_PATH, **DbConnectionOpts)
    connection.row_factory = sqlite3.Row
    yield connection
    connection.close()

@contextmanager
def transaction(connection: Connection) -> Generator[None, None, None]:
    connection.execute("PRAGMA journal_mode = WAL;")
    connection.execute("BEGIN TRANSACTION;")
    yield
    connection.commit()
    connection.execute("PRAGMA journal_mode = DELETE")

def exec_query(query: str) -> Sequence[sqlite3.Row]:
    with connect() as connection:
        return connection.execute(query).fetchall()

class Table(abc.ABC):
    recreate_ddl: str
    table_name: str

    @classmethod
    def add_index(cls, column: str) -> None:
        script = """
            DROP INDEX IF EXISTS "index_{0}_on_{1}";
            CREATE INDEX "index_{0}_on_{1}" ON "{0}" ("{1}");
        """
        with connect() as connection:
            connection.executescript(script.format(cls.table_name, column))

    @classmethod
    def validate(cls, collection: Sized) -> None:
        with connect() as connection:
            inserted = connection.execute(
                "select count(*) from {0}".format(cls.table_name),  # noqa: S608
            ).fetchone()[0]

            assert collection  # noqa: S101

            expected = len(collection)
            assert expected == inserted  # noqa: S101

            logger.log("Inserted {} {}.", humanize.intcomma(inserted), cls.table_name)

    @classmethod
    def recreate(cls) -> None:
        formatted_ddl = cls.recreate_ddl.format(cls.table_name)
        with connect() as connection:
            logger.log("Recreating {} table...", cls.table_name)
            connection.executescript(formatted_ddl)

    @classmethod
    def insert(cls, ddl: str, parameter_seq: List[Dict[str, Any]]) -> None:
        logger.log("Inserting {}...", cls.table_name)

        with connect() as connection:
            with transaction(connection):
                connection.executemany(ddl, parameter_seq)

    @classmethod
    def delete(cls, key: str, ids: Sequence[str]) -> None:
        ddl = """
            DELETE FROM {0} WHERE {1} IN ({2});
        """

        with connect() as connection:
            with transaction(connection):
                connection.execute(ddl.format(cls.table_name, key, cls.format_ids(ids)))
                logger.log(
                    "Deleted {} rows from {}...",
                    humanize.intcomma(len(ids)),
                    cls.table_name,
                )

    @classmethod
    def format_ids(cls, ids: Sequence[str]) -> str:
        return ",".join('"{0}"'.format(db_id) for db_id in ids)


import os
import time
import typing
import uuid
from collections import Counter
from datetime import datetime, timedelta

import dateutil.parser
import pytest
from django.utils import timezone

import gamechannels.gameservice as gameservice
import gamechannels.score as score_service
from gamechannels.game import GameState, GameStatus
import gamechannels.gamealias as gamealias
from gamechannels.gameexceptions import *

game_service = gameservice.RedisGameService()

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "eggwords.settings.local_settings")

TEST_WORDS = ['spiders', 'diss', 'redips', 'eds', 'per', 'pes', 'speirs', 'sped', 'pride', 'psi', 'pied', 'rise',
              'reps', 'spires', 'prides', 'press', 'spired', 'pier', 'pies', 'resid', 'reis', 'die', 'ers', 'pries', 'pried',
              'dip', 'side', 'dis', 'sipe', 'dries', 'ser', 'peds', 'prises', 'spider', 'sips', 'sei', 'ides', 'ess', 'res',
              'rep', 'spire', 'siped', 'redip', 'rei', 'psis', 'ped', 'sipes', 'ire', 'red', 'piss', 'pisser', 'spier',
              'sris', 'dire', 'spied', 'rises', 'pissed', 'rids', 'sri', 'ids', 'reds', 'sire', 'sirs', 'resids',
              'peri', 'speir', 'dips', 'drips', 'spiers', 'seis', 'sers', 'riped', 'ride', 'ripes', 'peris', 'piers',
              'ripe', 'spies', 'pie', 'rip', 'rips', 'rid', 'pis', 'dress', 'sis', 'sir', 'sip', 'sired', 'priss',
              'prise', 'prised', 'rides', 'sires', 'dies', 'ires', 'ired', 'sides', 'drip']

TEST_WORD_COUNTER = Counter((len(w) for w in TEST_WORDS))
TEST_WORD_COUNT = [0] * (max((len(w) for w in TEST_WORDS)) + 1)
for (w_len, w_count) in TEST_WORD_COUNTER.items():
    TEST_WORD_COUNT[w_len] = w_count

@pytest.fixture(autouse=True, scope='function')
def game_id():
    game_id = uuid.uuid4()
    yield game_id
    # import pdb; pdb.set_trace()
    game_service.clean_up_game(game_id)

def test_simple_game(game_id):
    print("Test game_id = " + str(game_id))

    p1_id = uuid.uuid4()
    p1_token = uuid.uuid4()

    p2_id = uuid.uuid4()
    p2_token = uuid.uuid4()

    game_service.init_game(game_id, p1_id, p1_token)

    game_state = game_service.get_game_state(game_id)
    assert GameStatus.WAITING == game_service.get_game_status(game_id)
    assert 1 == len(game_state.player_ids)
    assert p1_id in game_state.player_ids
    assert game_state.created_time is not None
    assert dateutil.parser.parse(game_state.created_time) < datetime.utcnow()

    game_service.add_player(game_id, p2_id, p2_token)

    game_state = game_service.get_game_state(game_id)
    assert GameStatus.WAITING == game_service.get_game_status(game_id)
    assert 2 == len(game_state.player_ids)
    assert p1_id in game_state.player_ids
    assert p2_id in game_state.player_ids

    result = game_service.use_word(game_id, p1_id, 'spiders')
    assert not result

    did_start = game_service.start_game(game_id, TEST_WORDS, countdown=1)
    assert did_start

    words = game_service.get_game_words(game_id)
    assert words is not None
    for test_word in TEST_WORDS:
        assert test_word in words

    game_state = game_service.get_game_state(game_id)
    assert GameStatus.SCHEDULED == game_service.get_game_status(game_id)
    assert len('spiders') == len(game_state.letters)
    for l in 'spiders':
        assert l in game_state.letters

    time.sleep(1)
    assert GameStatus.PLAYING == game_service.get_game_status(game_id)

    result = game_service.use_word(game_id, p1_id, 'spiders')
    assert result == True

    result2 = game_service.use_word(game_id, p2_id, 'spiders')
    assert result2 == False

    game_state = game_service.get_game_state(game_id)

    p1_words = game_state.words_for(p1_id)
    assert len(p1_words) == 1
    assert 'spiders' in p1_words

    p2_words = game_state.words_for(p2_id)
    assert len(p2_words) == 0

    game_service.end_game(game_id)
    game_state = game_service.get_game_state(game_id)
    assert GameStatus.COMPLETED == game_service.get_game_status(game_id)

def test_score() -> None:
    p1_id = uuid.uuid4()
    p2_id = uuid.uuid4()

    game_state0 = GameState(
        uuid.uuid4(),
        {p1_id: {}, p2_id: {}},
        {},
        (timezone.now() - timedelta(seconds=45)).isoformat(),
        (timezone.now() + timedelta(seconds=45)).isoformat(),
        (timezone.now() - timedelta(seconds=70)).isoformat(),
        'spiders',
        TEST_WORD_COUNT,
        (timezone.now() + timedelta(minutes=10)).isoformat()
    )

    score0 = score_service.score_game(game_state0)
    assert score0[p1_id] == score0[p2_id] == 0

    game_state1 = GameState(
        uuid.uuid4(),
        {p1_id: {}, p2_id: {}},
        {  'spiders': p1_id,
           'diss': p1_id,
           'redips': p1_id,
           'eds': p1_id,
           'per': p1_id,
           'pes': p1_id,
           'speirs': p1_id,
           'sped': p1_id,
           'pride': p1_id,
           'psi': p2_id,
           'pied': p2_id,
           'rise': p2_id,
           'reps': p2_id,
           'spires': p2_id,
           'prides': p2_id,
           'press': p2_id
        },
        (timezone.now() - timedelta(seconds=45)).isoformat(),
        (timezone.now() + timedelta(seconds=45)).isoformat(),
        (timezone.now() - timedelta(seconds=70)).isoformat(),
        'spiders',
        TEST_WORD_COUNT,
        (timezone.now() + timedelta(minutes=10)).isoformat()
    )

    score1 = score_service.score_game(game_state1)
    assert score1[p1_id] > score1[p2_id]

def test_expire(game_id):
    game_service.set_expire(game_id, 180)

def test_get_missing_game():
    game_state = game_service.get_game_state(uuid.uuid4())
    assert not game_state.exists()

def test_reinit_game(game_id):
    p1_id = uuid.uuid4()
    p1_token = uuid.uuid4()

    p2_id = uuid.uuid4()
    p2_token = uuid.uuid4()

    game_service.init_game(game_id, p1_id, p1_token)
    game_service.add_player(game_id, p2_id, p2_token)
    game_service.start_game(game_id, TEST_WORDS)
    game_service.end_game(game_id)
    assert GameStatus.COMPLETED == game_service.get_game_status(game_id)

    game_service.reinit_game(game_id)
    gstate = game_service.get_game_state(game_id)
    assert GameStatus.WAITING == gstate.status()
    assert gstate.start_time is None
    assert gstate.end_time is None
    assert gstate.letters is None
    assert 0 == len(gstate.word_count)
    assert all(0 == v for k, v in gstate.score().items())
    assert 0 == len(gstate.used_words)

def test_player_info(game_id):
    p1_id = uuid.uuid4()
    p1_token = uuid.uuid4()

    game_service.init_game(game_id, p1_id, p1_token)
    game_service.update_player_name(game_id, p1_id, 'TestName', p1_token)
    gstate = game_service.get_game_state(game_id)
    assert 'TestName' == gstate.player_info[p1_id]['name']
    assert 'color' in gstate.player_info[p1_id]

def test_full_game(game_id):
    game_service.init_game(game_id, uuid.uuid4(), uuid.uuid4())
    for _ in range(gameservice.MAX_PLAYERS - 1):
        game_service.add_player(game_id, uuid.uuid4(), uuid.uuid4())
    with pytest.raises(GameFull):
        game_service.add_player(game_id, uuid.uuid4(), uuid.uuid4())
        game_service.add_player(game_id, uuid.uuid4(), uuid.uuid4())

def test_player_colors(game_id):
    p1_id = uuid.uuid4()
    p1_token = uuid.uuid4()

    game_service.init_game(game_id, p1_id, p1_token)
    game_service.update_player_name(game_id, p1_id, 'TestName', p1_token)

    p2_id, p2_token = uuid.uuid4(), uuid.uuid4()
    p3_id, p3_token = uuid.uuid4(), uuid.uuid4()
    p4_id, p4_token = uuid.uuid4(), uuid.uuid4()
    p5_id, p5_token = uuid.uuid4(), uuid.uuid4()

    game_service.add_player(game_id, p2_id, p2_token)
    game_service.add_player(game_id, p3_id, p3_token)
    game_service.add_player(game_id, p4_id, p4_token)
    game_service.add_player(game_id, p5_id, p5_token)

    gstate = game_service.get_game_state(game_id)

    # Assert all colors are unique
    used_colors = [pi['color'] for pi in gstate.player_info.values()]
    assert len(set(used_colors)) == len(used_colors)

    # Remove / Add player, then test again
    game_service.remove_player(game_id, p3_id)
    new_id, new_token = uuid.uuid4(), uuid.uuid4()
    game_service.add_player(game_id, new_id, new_token)

    gstate = game_service.get_game_state(game_id)
    used_colors = [pi['color'] for pi in gstate.player_info.values()]
    assert len(set(used_colors)) == len(used_colors)

def test_player_info_unauthorized(game_id):
    p1_id = uuid.uuid4()
    p1_token = uuid.uuid4()

    game_service.init_game(game_id, p1_id, p1_token)
    with pytest.raises(UnauthorizedAction):
        game_service.update_player_name(game_id, p1_id, 'TestName', None)
    with pytest.raises(UnauthorizedAction):
        game_service.update_player_name(game_id, p1_id, 'TestName', uuid.uuid4())

def test_game_alias(game_id) -> None:
    p1_id = uuid.uuid4()
    p1_token = uuid.uuid4()

    game_service.init_game(game_id, p1_id, p1_token)
    aliased = gamealias.create_game_alias(game_id)
    dealiased = gamealias.dealias_game(aliased)

    assert str(dealiased) == str(game_id)

def test_game_alias_missing(game_id) -> None:
    p1_id = uuid.uuid4()
    p1_token = uuid.uuid4()

    game_service.init_game(game_id, p1_id, p1_token)
    with pytest.raises(GameAliasNotFound):
        gamealias.dealias_game('abcdefg')

def test_start_missing_game(game_id) -> None:
    with pytest.raises(GameDoesNotExist):
        game_service.start_game(game_id, TEST_WORDS)

def test_double_start_game(game_id) -> None:
    p1_id = uuid.uuid4()
    p1_token = uuid.uuid4()

    game_service.init_game(game_id, p1_id, p1_token)
    game_service.start_game(game_id, TEST_WORDS)

    with pytest.raises(GameStatusException):
        game_service.start_game(game_id, TEST_WORDS)


"""
A working demo using KMeans

"""

import numpy as np
import scipy.io as sio
import matplotlib.pyplot as plt

data = sio.loadmat('data.mat')
X = np.array(data['X'])

from KMeans import KMeans

k = 3
est = KMeans(k)

c = est.train(X)

colors=np.array(['green', 'red', 'blue'])
# lets plot on matplotlib
for i in range(k):
    x = X[np.where(c == i)[0]]
    plt.scatter(x[:, 0], x[:, 1], color=colors[i])

# plt.savefig('clustering_example.png')
plt.show()


from django.contrib.postgres.search import (SearchQuery, SearchRank,
                                            SearchVector)
from django.shortcuts import render

from analyzer.utils.stemmer import stemm
from cards.models import Meal

from .forms import SearchForm

def search(request, ingredients_correction=None, meal_url_correction=None):
    meals = Meal.objects.all().order_by('-created')[0:3]
    form = SearchForm()
    query = None
    results = []
    if request.method == 'POST':
        if 'query' in request.POST:
            form = SearchForm(request.POST)
            if form.is_valid():
                query = form.cleaned_data['query']
                token = stemm(query)
                results = Meal.objects.annotate(search=SearchVector('meal_name', 'ingredients', 'tokens'),).filter(search=token[0])
                for word in token:
                    results = results | Meal.objects.annotate(search=SearchVector('meal_name', 'ingredients', 'tokens'),).filter(search=word)
    else:
        form = SearchForm()
    return render(request, 'search/search.html', {'form': form, 'query': query, 'results': results, 'meals': meals})


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Feb  7 21:32:00 2021

@author: vriesdejelmer
"""

import csv
import torch

import numpy as np
import matplotlib.collections as mcoll
import matplotlib.path as mpath
import matplotlib.pyplot as plt

def writeProperties(new_vars, old_vars, data_dir):

    with open(data_dir + 'properties.csv', 'w') as csv_file:
        writer = csv.writer(csv_file)
        for key in set(new_vars) - set(old_vars):
            if key != 'old_vars':
                writer.writerow([key, new_vars[key]])

def getDevice():
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    print(device)
    if not (str(device) == "cuda"):
        print("WE'RE NOT RUNNING ON CUDA")

    return device

def colorline(x, y, z=None, cmap=plt.get_cmap('copper'), norm=plt.Normalize(0.0, 1.0), linewidth=5, alpha=1.0):
    """
    http://nbviewer.ipython.org/github/dpsanders/matplotlib-examples/blob/master/colorline.ipynb
    http://matplotlib.org/examples/pylab_examples/multicolored_line.html
    Plot a colored line with coordinates x and y
    Optionally specify colors in the array z
    Optionally specify a colormap, a norm function and a line width
    """

    # Default colors equally spaced on [0,1]:
    if z is None:
        z = np.linspace(0.0, 1.0, len(x))

    # Special case if a single number:
    if not hasattr(z, "__iter__"):  # to check for numerical input -- this is a hack
        z = np.array([z])

    z = np.asarray(z)

    segments = make_segments(x, y)
    lc = mcoll.LineCollection(segments, array=z, cmap=cmap, norm=norm,
                              linewidth=linewidth, alpha=alpha)

    ax = plt.gca()
    ax.add_collection(lc)

    return lc

def make_segments(x, y):
    """
    Create list of line segments from x and y coordinates, in the correct format
    for LineCollection: an array of the form numlines x (points per line) x 2 (x
    and y) array
    """
    points = np.array([x, y]).T.reshape(-1, 1, 2)
    segments = np.concatenate([points[:-1], points[1:]], axis=1)
    return segments

    #convert cartesian coordinates to polar
def cart2pol(x, y):
    rho = np.sqrt(x**2 + y**2)
    phi = np.arctan2(y, x)
    return(rho, phi)

    #convert proportions to cartesian coords on the unit circle
def prop2unit(prop):
    x_coord = np.cos(prop*2*np.pi)
    y_coord = np.sin(prop*2*np.pi)
    return(x_coord, y_coord)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib import messages
from django.shortcuts import render, redirect
from .models import User, Message, Comment
import re
EMAILREG = re.compile(r'^[a-zA-Z0-9\.\+_-]+@[a-zA-Z0-9\._-]+\.[a-zA-Z]*$')

# Create your views here.
def index(request):
    return render(request, "wall/index.html")

def wall(request):
    context = {
        "user_messages": Message.objects.all().order_by('id').reverse(),
        "user_comments": Comment.objects.all().order_by('id'),
    }
    return render(request, "wall/wall.html", context)

def post_message(request):
    if request.method == 'POST':
        if request.POST["wall_posts"] == "message_submit":
            response = Message.objects.new_message_input(request.POST, request.session['user']['id'])
        if request.POST["wall_posts"] == "comment_submit":
            response = Comment.objects.new_comment_input(request.POST, request.session['user']['id'])
    return redirect('wall:wall')

def user(request, id):
    user_check = User.objects.filter(id=id)
    if user_check:
        context = {
            "user": User.objects.get(id=id),
        }
        return render(request, "wall/user.html", context)
    else:
        return redirect('wall:all_users')

def all_users(request):
    context = {
        "all": User.objects.all(),
    }
    return render(request, "wall/allusers.html", context)

def entrance(request):
    if request.method == "POST":
        if request.POST["gate"] == "register":
            response = User.objects.check_create(request.POST)
        elif request.POST["gate"] == "login":
            response = User.objects.check_login(request.POST)
        if not response[0]:
            for error in response[1]:
                messages.error(request, error[1])
            return redirect('wall:index')
        else:
            request.session['user'] = {
            "id": response[1].id,
            "first_name": response[1].first_name,
            "last_name": response[1].last_name,
            }
            return redirect('wall:wall')
    return redirect('wall:index')

def user_update(request):
    user_id = str(request.session['user']['id'])
    if request.method == "POST":
        if request.POST["update_option"] == "profile":
            response = User.objects.check_update(request.POST, request.session['user']['id'])
        if request.POST["update_option"] == "password":
            response = User.objects.check_password(request.POST, request.session['user']['id'])
        if not response[0]:
            for error in response[1]:
                messages.error(request, error[1])
            return redirect('/'+user_id)
    return redirect('/'+user_id)

def user_delete(request):
    user_id = request.session['user']['id']
    User.objects.filter(id=user_id).delete()
    return redirect('wall:logout')

def logout(request):
    request.session.clear()
    return redirect('wall:index')


import simpy
import datetime
import time
import random
import os
import pandas
import openpyxl as opxl
import win32com.client as win32
import numpy as np
from openpyxl.utils.dataframe import dataframe_to_rows

class Simulation:

    def __init__(self,serveTime,nServers,nEmployees,lunchTime):
        self.startTime = time.time()
        self.serveTime = int(serveTime)
        self.nServers = int(nServers)
        self.nEmployees = int(nEmployees)
        self.lunchTime = lunchTime
        self.data = {"Arrival Time":[],"Queue Time":[],"Inter-Arrival":[]}

    def customer(self,env, server, TBA, serveTime):

        # Simulate a period of time before the arrival of the next customer
        yield env.timeout(TBA)

        # Record time of arrival
        arriveTime = env.now
        self.data["Arrival Time"].append(arriveTime)

        # Join the queue
        with server.request() as req:
            # Wait if resource is being used
            yield req

            # Get served
            queueTime = (env.now - arriveTime)
            self.data["Queue Time"].append(queueTime)

            yield env.timeout(serveTime)

    def distribute(self,mInterval,length):
        intervals = list(random.expovariate(1.0/mInterval) for i in range(length))
        self.data["Inter-Arrival"] = intervals
        return list(np.cumsum(intervals))

    def runSimulation(self):
        # Initialise environment and resource
        env = simpy.Environment()
        cafe = simpy.Resource(env, capacity=self.nServers)

        # Generate customers
        meanTBA = int(self.lunchTime)/(int(self.nEmployees)-1)
        arrivalTimes = self.distribute(meanTBA,self.nEmployees)
        for i in range(self.nEmployees):
            env.process(self.customer(env, cafe, arrivalTimes[i], self.serveTime))

        # Run simulation
        env.run()

    def exportData(self):
        simDay = datetime.datetime.now()
        sheetName = simDay.strftime("%H %M %S %f")
        direcName = os.path.dirname(os.path.abspath(__file__)) + "\\" + fileName

        # Export data collected to Excel
        try:
            wb = opxl.load_workbook(filename=fileName)
        except FileNotFoundError:
            new_wb = opxl.Workbook()
            new_wb.save("%s.xlsx" % self.dist)
            wb = new_wb

        ws = wb.create_sheet(title="%s" % sheetName)
        df = pandas.DataFrame.from_dict(self.data)

        for row in dataframe_to_rows(df, index=True, header=True):
            ws.append(row)

        # Format table
        ws['A1'] = 'ID #'

        #Summarise data
        ws['H2'] = 'Summary'
        ws['H3'],ws['I3'] = 'Avg. Queue Time',np.mean(self.data["Queue Time"])
        ws['H4'],ws['I4'] = '# of Servers',self.nServers
        ws['H5'],ws['I5'] = 'Serve Time (s)',self.serveTime
        ws['H6'],ws['I6'] = 'Lunch Time (hrs)',round(self.lunchTime/3600,1)
        ws['H7'],ws['I7'] = 'Mean TBA (s)',np.mean(self.data["Inter-Arrival"])
        ws['H8'],ws['I8'] = 'Spread',np.std(self.data["Queue Time"])

        # Save workbook
        wb.save(fileName)

        # Autofit columns
        excel = win32.gencache.EnsureDispatch('Excel.Application')
        wb = excel.Workbooks.Open(direcName)
        ws = wb.Worksheets(sheetName)
        ws.Columns.AutoFit()
        wb.Save()
        excel.Application.Quit()

    def printRuntime(self):
        print('RUNTIME: --- %.2f seconds ---' % (time.time()-self.startTime))

    def run(self):
        self.runSimulation()
        self.exportData()
        self.printRuntime()

if __name__ == '__main__':

    startTime = time.time()

	#Input Data
    serveTimes = [5,10,15,30,45]
    numServers = [1,2,3,5]
    nEmployees = 1250
    lunchTimes = [1800,3600,7200]

    fileName = "FCFS.xlsx"
    for sTime in serveTimes:
        for num in numServers:
            for lunchTime in lunchTimes:
                s = Simulation(sTime,num,nEmployees,lunchTime)
                s.run()

    print("TOTAL RUNTIME: %.2f seconds" % (time.time() - startTime))


import xml.etree.ElementTree as ET, urllib, gzip, io
from system import System

url = "https://github.com/OpenExoplanetCatalogue/oec_gzip/raw/master/systems.xml.gz"
oec = ET.parse(gzip.GzipFile(fileobj=io.BytesIO(urllib.urlopen(url).read())))

system_objs = []

for system_node in oec.findall('.//system'):
    system_objs.append(System().build(system_node))

print (system_objs)


from pprint import pprint
import json

import pytest

from models.contact_model import Contact
from models.experience_model import Experience, Month
from models.resume_model import Resume
from models.resume_section_model import ResumeSection
from models.tag_model import Tag
from models.tag_item_model import TagItem
from models.program_contact_model import ProgramContact
from models.session_model import UserSession

from .test_api import post_request, POSTS

# This only works because the script is at the top level
from resources.trello_utils import (
    query_board_data,
    Board,
    BoardList,
    Card,
)

TALENT_INTAKE_BOARDS = {
    'local': '5e37744114d9d01a03ddbcfe',
    'dev': '5e25dae2e640be7e5248a3e6',
    'production': '5ddd741f5cc43e2b21346dbb'
}

TALENT_INTAKE_LABELS = [
    'New', 'Fellowship', 'Mayoral Fellowship', 'PFP', 'PA', 'JHU - Carey'
]

TALENT_INTAKE_ATTACHMENTS = ['Profile', 'Full Response']

def check_labels(board_id, expected_labels):
    board = Board(query_board_data(board_id))
    labels = board.labels['name'].keys()
    print(labels)
    for label in expected_labels:
        assert label in labels

def test_talent_intake_board_labels(app):
    local_board_id = TALENT_INTAKE_BOARDS['local']
    check_labels(local_board_id, TALENT_INTAKE_LABELS)
    prod_board_id = TALENT_INTAKE_BOARDS['production']
    check_labels(prod_board_id, TALENT_INTAKE_LABELS)
    dev_board_id = TALENT_INTAKE_BOARDS['dev']
    check_labels(dev_board_id, TALENT_INTAKE_LABELS)

def test_talent_intake(app):
    mimetype = 'application/x-www-form-urlencoded'
    headers = {
        'Content-Type': mimetype,
        'Accept': mimetype,
    }

    card_id = '5e4af2d6fc3c0954ff187ddc'

    contact = Contact.query.get(123)
    board_id = TALENT_INTAKE_BOARDS['local']
    board = Board(query_board_data(board_id))
    card = board.cards.get(card_id)
    started_list = board.lists['stage'][1]

    for attachment in TALENT_INTAKE_ATTACHMENTS:
        response = card.remove_attachment(attachment)
    card.set_labels(label_names=['New', 'Fellowship'])
    card.move_card(started_list)

    board = Board(query_board_data(board_id))
    card = board.cards.get(card_id)

    assert card.attachments == {}
    assert card.label_names == ['New', 'Fellowship']
    assert card.stage == 1
    assert contact.stage == 1

    url = '/api/form-assembly/talent-app/'
    data = (
        'contact_id=123&program_id=1&first_name=Billy&last_name=Daly&email=billy%40example.com&phone=9085784622&street1=2401+Liberty+Heights+Ave&street2=Suite+2730&city=Baltimore&state=MD&postal_code=21215&equity=Race&effectiveness=Effectiveness&programs%5B0%5D=Fellowship&programs%5B1%5D=JHU+-+Carey&programs%5B2%5D=Mayoral+Fellowship&programs%5B3%5D=PFP&programs%5B4%5D=PA&programs%5B5%5D=I%27d+like+some+help+figuring+this+out&mayoral_eligible=Yes&experience=0-2+years&capabilities%5B0%5D=Advocacy+and+Public+Policy&capabilities%5B1%5D=Community+Engagement+and+Outreach&capabilities%5B2%5D=Data+Analysis&capabilities%5B3%5D=Fundraising+and+Development&capabilities%5B4%5D=Marketing+and+Public+Relations&alum_radio=No&race_all=American+Indian+or+Alaskan+Native&gender=Male&pronouns=He%2FHim&response_id=160140910'
    )
    with app.test_client() as client:
        response = client.post(url, data=data, headers=headers)
        pprint(response.json)
        assert response.status_code == 201
        board = Board(query_board_data(board_id))
        card = board.cards.get(card_id)
        contact = Contact.query.get(123)
        assert card.stage == 2
        assert contact.stage == 2
        assert 'New' in card.label_names
        for label in TALENT_INTAKE_LABELS[1:]:
            assert label in card.label_names
        assert 'Race' in card.desc
        assert '- Data Analysis\n' in card.desc
        assert '0-2 years' in card.desc
        assert '**Mayoral Fellowship:** Yes' in card.desc
        assert '**JHU - Carey:** N/A' in card.desc
        pprint(card.attachments)
        assert card.attachments['Profile']['url'] == (
            'https://app.baltimorecorps.org/profile/123'
        )
        assert card.attachments['Full Response']['url'] == (
            'https://app.formassembly.com/responses/view/160140910'
        )
        data = json.loads(response.data)['data']

def test_resubmit_approved_app(app):
    mimetype = 'application/x-www-form-urlencoded'
    headers = {
        'Content-Type': mimetype,
        'Accept': mimetype,
    }

    card_id = '5e4af2d6fc3c0954ff187ddc'

    board_id = TALENT_INTAKE_BOARDS['local']
    board = Board(query_board_data(board_id))
    card = board.cards.get(card_id)
    approved_list = board.lists['stage'][3]
    card.move_card(approved_list)

    board = Board(query_board_data(board_id))
    card = board.cards.get(card_id)
    assert card.stage == 3

    url = '/api/form-assembly/talent-app/'
    data = (
        'contact_id=123&program_id=1&first_name=Billy&last_name=Daly&email=billy%40example.com&phone=9085784622&street1=2401+Liberty+Heights+Ave&street2=Suite+2730&city=Baltimore&state=MD&postal_code=21215&equity=Race&effectiveness=Effectiveness&programs%5B0%5D=Fellowship&programs%5B1%5D=JHU+-+Carey&programs%5B2%5D=Mayoral+Fellowship&programs%5B3%5D=PFP&programs%5B4%5D=PA&programs%5B5%5D=I%27d+like+some+help+figuring+this+out&mayoral_eligible=Yes&experience=0-2+years&capabilities%5B0%5D=Advocacy+and+Public+Policy&capabilities%5B1%5D=Community+Engagement+and+Outreach&capabilities%5B2%5D=Data+Analysis&capabilities%5B3%5D=Fundraising+and+Development&capabilities%5B4%5D=Marketing+and+Public+Relations&alum_radio=No&race_all=American+Indian+or+Alaskan+Native&gender=Male&pronouns=He%2FHim&response_id=160140910'
    )

    with app.test_client() as client:
        response = client.post(url, data=data, headers=headers)
        pprint(response.json)
        assert response.status_code == 400
        message = json.loads(response.data)['message']
        assert message == 'Application has already been submitted'

# TODO: Add trello specific checks
def test_create_program_contact_with_contact(app):
    mimetype = 'application/json'
    headers = {
        'Content-Type': mimetype,
        'Accept': mimetype,
        'Authorization': 'Bearer test-valid|0123456789',
    }
    with app.test_client() as client:
        response = client.post('/api/contacts/',
                               data=json.dumps(POSTS['contact']),
                               headers=headers)

        assert response.status_code == 201
        data = json.loads(response.data)['data']
        assert len(data) > 0
        assert data['id'] is not None
        id_ = data['id']

        program_contacts = Contact.query.get(id_).programs
        assert len(program_contacts) == 1
        assert program_contacts[0].program_id == 1
        assert program_contacts[0].stage == 1
        assert program_contacts[0].program.name == 'Place for Purpose'
        assert program_contacts[0].is_active == True
        assert program_contacts[0].is_approved == False

# TODO: Add trello specific checks
def test_post_contact(app):
    mimetype = 'application/json'
    headers = {
        'Content-Type': mimetype,
        'Accept': mimetype,
        'Authorization': 'Bearer test-valid|0123456789',
    }
    with app.test_client() as client:
        response = client.post('/api/contacts/',
                               data=json.dumps(POSTS['contact']),
                               headers=headers)
        assert response.status_code == 201
        set_cookie = response.headers.get('set-cookie')
        assert set_cookie is not None
        assert set_cookie.find('HttpOnly;') is not -1
        # Note: Can't test "secure" due to non-https connection
        contact = Contact.query.filter_by(account_id='test-valid|0123456789').first()
        assert contact.first_name == 'Tester'
        assert contact.email == 'testerb@example.com'
        assert contact.profile.years_exp is None

        assert UserSession.query.filter_by(contact_id=contact.id).first()

# TODO: Add trello specific checks
def test_post_duplicate_contact(app):
    mimetype = 'application/json'
    headers = {
        'Content-Type': mimetype,
        'Accept': mimetype,
        'Authorization': 'Bearer test-valid|0123456789abcdefabcdefff',
    }

    contact_data = POSTS['contact'].copy()
    contact_data['account_id'] = 'test-valid|0123456789abcdefabcdefff'

    with app.test_client() as client:
        response = client.post('/api/contacts/',
                               data=json.dumps(contact_data),
                               headers=headers)
        assert response.status_code == 400
        message = json.loads(response.data)['message']
        assert message == 'A contact with this account already exists'


#!/usr/bin/python -W ignore
########################################################## {COPYRIGHT-TOP} ###
# Licensed Materials - Property of IBM
# 5737-I32
#
# (C) Copyright IBM Corp. 2019
#
# US Government Users Restricted Rights - Use, duplication, or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
########################################################## {COPYRIGHT-END} ###

import logging
import os
import sys
import re

ENCODING = 'utf-8'

class DocumentRetrievalFactory:
    """Factory class to create the right sort of retrieval object."""

    @staticmethod
    def create(application, key):
        """Lookup connection and create required type."""
        platform, client = application.connections.get((key.datasource, key.cluster), (None, None))

        if platform and client:
            if platform == 'COS':
                return DocumentRetrievalCOS(client)
            elif platform == 'NFS':
                return DocumentRetrievalNFS(client)
            elif platform == 'Spectrum Scale':
                return DocumentRetrievalScale(client)
            elif platform == 'Spectrum Scale Local':
                return DocumentRetrievalLocalScale(client)
        return None

class DocumentRetrievalBase():
    """A class to retrieve a document via a Spectrum Discover Connection."""

    def __init__(self, client):
        """Constructor."""
        # Instantiate logger
        loglevels = {'INFO': logging.INFO, 'DEBUG': logging.DEBUG,
                     'ERROR': logging.ERROR, 'WARNING': logging.WARNING}
        log_level = os.environ.get('LOG_LEVEL', 'DEBUG')
        log_format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
        logging.basicConfig(stream=sys.stdout,
                            format=log_format,
                            level=loglevels[log_level])
        self.logger = logging.getLogger(__name__)

        self.client = client

    def get_document(self, key):
        """
        To be implemented by the child class.

        Receives a document key that uniquely identifies a particular
        object, and is responsible for retrieving that object and
        returning the filepath as a string.

        Returns None if object was not able to be retrieved.
        """
        self.logger.warning("get_document has not been implemented for this class")
        return None

    def get_headers(self, key):
        """
        To be implemented by the child class.

        Receives a document bucket and key that uniquely identifies a particular
        cos object, and is responsible for retrieving that objects headers.

        Returns None if object was not able to be retrieved.
        """
        self.logger.warning("get_headers has not been implemented for this class")
        return None

    def cleanup_document(self):
        """
        To be implemented by the child class.

        Perform cleanup and/or remove any tmp files as needed.

        Returns None.
        """
        self.logger.warning("cleanup_document has not been implemented for this class")

    def create_file_path(self, prefix, path):
        """Return the tmpfile_path with filetype as string."""
        self.logger.debug('filepath prefix: %s, path: %s', prefix, path.decode(ENCODING))
        if '.' in path.decode(ENCODING):
            return prefix + '.' + path.decode(ENCODING).split('.')[-1]
        return prefix

class DocumentRetrievalCOS(DocumentRetrievalBase):
    """Create a COS document class.

    This will act appropriately upon COS documents that have been downloaded as temp files.
    """

    filepath = None

    def get_document(self, key):
        """Return document filepath."""
        content = None
        self.filepath = None

        if self.client:
            obj = self.client.get_object(Bucket=key.datasource, Key=key.path.decode(ENCODING).split('/', 1)[1])
            content = obj['Body'].read()
            self.filepath = self.create_file_path('/tmp/cosfile_' + str(os.getpid()), key.path)
            with open(self.filepath, 'w', encoding=ENCODING) as file:
                file.write(content.decode(ENCODING, 'replace'))
        else:
            self.logger.error('Could not access file %s', key.path.decode(ENCODING))

        return self.filepath

    def get_headers(self, key):
        """Return the COS HTTPHeaders for a key, bucket."""
        http_headers = None

        if self.client:
            vault, doc_name = key.path.decode(ENCODING).split('/', 1)
            headers = self.client.head_object(Bucket=vault, Key=doc_name)
            try:
                http_headers = headers['ResponseMetadata']['HTTPHeaders']
            except KeyError:
                self.logger.error('Could not access header metadata information for file %s: %s', key.path.decode(ENCODING), headers)
        else:
            self.logger.error('Could not access client')
        return http_headers

    def cleanup_document(self):
        """Cleanup files as needed."""
        self.logger.debug("COS: Attempting to delete file: %s", self.filepath)

        try:
            os.remove(self.filepath)
        except FileNotFoundError:
            self.logger.debug('No file: %s to delete.', self.filepath)

class DocumentRetrievalNFS(DocumentRetrievalBase):
    """Create a NFS document class.

    This will act appropriately upon nfs documents.
    No need to download or cleanup since accessed direclty upon the mount point.
    """

    filepath = None

    def get_document(self, key):
        """Return document filepath."""
        self.filepath = None

        if self.client:
            mount_path_prefix = self.client['additional_info']['local_mount']
            source_path_prefix = self.client['mount_point']
            self.filepath = re.sub('^' + source_path_prefix, mount_path_prefix, key.path.decode(ENCODING))
        else:
            print('No document match')

        return self.filepath

    def cleanup_document(self):
        """Cleanup files as needed."""
        self.logger.debug("NFS: Not doing any cleanup.")

class DocumentRetrievalScale(DocumentRetrievalBase):
    """Create a Spectrum Scale document class.

    This will act appropriately upon Scale documents that have been downloaded as temp files.
    """

    filepath = None

    def get_document(self, key):
        """Return document filepath."""
        self.filepath = None

        if self.client:
            try:
                self.filepath = self.create_file_path('/tmp/scalefile_' + str(os.getpid()), key.path)
                self.logger.debug(self.filepath)
                self.client.get(key.path, self.filepath)
            except UnicodeDecodeError:
                self.logger.error('Could not decode file %s', key.path.decode(ENCODING))
            except FileNotFoundError:
                self.logger.error('Could not find file %s', key.path.decode(ENCODING))
            except OSError: # Seen when scale nsd disk is down and file is not accessible
                self.logger.error('Could not transfer file %s', key.path.decode(ENCODING))

        else:
            print('No document match')

        return self.filepath

    def cleanup_document(self):
        """Cleanup files as needed."""
        self.logger.debug("SCALE: Attempting to delete file: %s", self.filepath)

        try:
            os.remove(self.filepath)
        except FileNotFoundError:
            self.logger.debug('No file: %s to delete.', self.filepath)

class DocumentRetrievalLocalScale(DocumentRetrievalBase):
    """Create a Local Spectrum Scale document class.

    This will act appropriately upon nfs documents.
    No need to download or cleanup since accessed direclty upon the mount point.
    """

    def get_document(self, key):
        """Return Document Key."""
        return key.path.decode(ENCODING)

    def cleanup_document(self):
        """Cleanup files as needed."""
        self.logger.debug("Scale Local: Not doing any cleanup.")

class DocumentKey(object):
    """A class to identify a unique document on a Spectrum Discover Connection."""

    def __init__(self, doc):
        """Init."""
        self.fkey = doc['fkey']
        self.datasource = doc['datasource']
        self.cluster = doc['cluster']
        self.path = doc['path'].encode(ENCODING)
        # a unique identifier for the connection this document belongs to.
        self.id = self.datasource + ':' + self.cluster

    def __str__(self):
        """Return string formatted doc info."""
        return "{}/{} -> {} (fkey: {})".format(self.datasource, self.cluster, self.path, self.fkey)