""" S3 Watcher Lambda

Reacts to Events from a Kinesis stream, and parses s3 file uploads and triggers
pipelines
"""
import base64
import json
import logging

import boto3

LOGGER = logging.getLogger()
LOGGER.setLevel(logging.DEBUG)
DYNAMO = boto3.client('dynamodb')
LAMBDA = boto3.client('lambda')

def get_key_name(event):
    return event['Records'][0]['s3']['object']['key']

def get_key_details(key_name):
    key_parts = key_name.split('/')
    return (key_parts[1], key_parts[2], key_parts[-1])

def handle(event, context):
    """ Receives the event trigger, typically a Kinesis stream event and
    determines if a pipeline is finished. Invokes a telemetry lambda
    if a pipeline is complete.

    :param event: The data from the triggering event
    :type event: dict
    :param context: Any additional context from the event
    :type context: dict

    :return: None
    :rtype: None
    """
    for record in event.get('Records', []):
        payload = json.loads(base64.b64decode(record["kinesis"]["data"]))
        LOGGER.info('Payload: %s', payload)
        key_name = get_key_name(payload)
        app_slug, uuid_folder, file_name = get_key_details(key_name)
        if not app_slug or not file_name:
            return event

        response = DYNAMO.get_item(
                TableName='FilesToPipelines',
                Key={
                    'App': {'S': app_slug},
                    'File': {'S': file_name}
                },
                ProjectionExpression='Pipeline')
        if 'Item' not in response:
            LOGGER.info('Could not file pipeline for %s', key_name)
            return
        if not response['Item'].get('Pipeline'):
            LOGGER.info('Could not file pipeline for %s: Got %s',
                        key_name, response)
            return

        pipeline = response['Item']['Pipeline']["S"]
        response = LAMBDA.invoke_async(
            FunctionName=pipeline,
            InvokeArgs=json.dumps({
                'trigger': 'evented',
                'uuid': uuid_folder
            })
        )
        LOGGER.debug(response)
        LOGGER.info('Executed %s on %s', pipeline, uuid_folder)


import os
import os.path as osp
from glob import glob
import numpy as np
import torch
import torch.utils.data as data
from skimage.color import rgb2gray

from datasets.utils_data import read_gen, readFlowKITTI

class FlowDataset(data.Dataset):
    def __init__(self, args, augmentor=None, sparse=False, is_training=True):
        self.args = args
        if augmentor is not None:
            self.augmentor = augmentor(args)
        else:
            self.augmentor = None
        self.sparse = sparse
        self.is_training = is_training

        self.flow_list = []
        self.image_list = []
        self.extra_info = []
        self.pseudo_gt_list = []

    def __getitem__(self, index):

        index = index % len(self.image_list)

        im1 = read_gen(self.image_list[index][0])
        im2 = read_gen(self.image_list[index][1])
        im1 = np.array(im1)
        im2 = np.array(im2)

        # grayscale images
        if len(im1.shape) == 2:
            im1 = np.tile(im1.astype(np.uint8)[...,None], (1, 1, 3))
            im2 = np.tile(im2.astype(np.uint8)[...,None], (1, 1, 3))

        elif self.args.data_in_grayscale:
            im1 = np.tile(rgb2gray(im1)[...,None] * 255, (1, 1, 3)).astype(np.uint8)
            im2 = np.tile(rgb2gray(im2)[...,None] * 255, (1, 1, 3)).astype(np.uint8)

        else:
            im1 = im1[..., :3].astype(np.uint8)
            im2 = im2[..., :3].astype(np.uint8)
        # else:
        if self.augmentor is not None:
            example = self.augmentor(im1, im2)

        else:
            example = {
                'ims': np.stack([im1, im2])
                }

        for key, value in example.items():
            if key in ['pad_params', 'orig_dims', 'offsets', 'valid', 'pseudo_gt']:
                example[key] = torch.from_numpy(value)
            else:
                example[key] = torch.from_numpy(value).permute(0, 3, 1, 2).float()

        example['index'] = [index, self.extra_info[index]]

        if not self.is_training:

            if self.sparse:
                flow_occ, valid_occ = readFlowKITTI(self.flow_occ_list[index])
                flow_noc, valid_noc = readFlowKITTI(self.flow_noc_list[index])

                flow_occ = np.array(flow_occ).astype(np.float32)
                flow_noc = np.array(flow_noc).astype(np.float32)
                valid_occ = np.expand_dims(valid_occ, axis=0)
                valid_noc = np.expand_dims(valid_noc, axis=0)

                example['flow_occ'] = torch.from_numpy(flow_occ).permute(2, 0, 1).float()
                example['valid_occ'] = torch.from_numpy(valid_occ)
                example['flow_noc'] = torch.from_numpy(flow_noc).permute(2, 0, 1).float()
                example['valid_noc'] = torch.from_numpy(valid_noc)

            else:

                flow = read_gen(self.flow_list[index])
                flow = np.array(flow).astype(np.float32)
                flow = torch.from_numpy(flow).permute(2, 0, 1).float()
                valid = (flow[0].abs() < 1000) & (flow[1].abs() < 1000).unsqueeze(0)

                example['flow'] = flow
                example['valid'] = valid

        return example

    def __rmul__(self, v):
        self.flow_list = v * self.flow_list
        self.image_list = v * self.image_list
        return self

    def __len__(self):
        return len(self.image_list)

class MpiSintel(FlowDataset):
    def __init__(self, args, augmentor, is_training=True, split='training', root='/path/to/sintel/', dstype='final'):
        super(MpiSintel, self).__init__(args, augmentor=augmentor, sparse=False, is_training=is_training)

        if split == 'training':
            image_root = osp.join(root, 'test', dstype)

            for scene in os.listdir(image_root):
                image_list = sorted(glob(osp.join(image_root, scene, '*.png')))
                for i in range(len(image_list)-1):
                    self.image_list += [ [image_list[i], image_list[i+1]] ]
                    frame1_id = image_list[i].split('/')[-1].split('.')[0]
                    frame2_id = image_list[i+1].split('/')[-1].split('.')[0]
                    self.extra_info += [[f'{scene}_{frame1_id}', f'{scene}_{frame2_id}']]

        elif split == 'validation':
            image_root = osp.join(root, 'training', dstype)
            flow_root = osp.join(root, 'training', 'flow')

            for scene in os.listdir(image_root):
                image_list = sorted(glob(osp.join(image_root, scene, '*.png')))
                for i in range(len(image_list)-1):
                    self.image_list += [ [image_list[i], image_list[i+1]] ]

                    self.extra_info += [ (scene, i) ] # scene and frame_id

                self.flow_list += sorted(glob(osp.join(flow_root, scene, '*.flo')))

class KITTI(FlowDataset):
    def __init__(self, args, augmentor, is_training=True, split='training', root='/path/to/kitti_data/data_scene_flow/'):
        super(KITTI, self).__init__(args, augmentor=augmentor, sparse=True, is_training=is_training)

        images1 = []
        images2 = []
        self.flow_occ_list = []
        self.flow_noc_list = []

        if split == 'training':
            root = osp.join(root, 'testing')

            for dir in ['image_2', 'image_3']:
                for i in range(200):
                    images1 += sorted(glob(osp.join(root, f'{dir}/{str(i).zfill(6)}_*.png')))[:-1]
                    images2 += sorted(glob(osp.join(root, f'{dir}/{str(i).zfill(6)}_*.png')))[1:]

        elif split == 'validation':
            root = osp.join(root, 'training')

            for i in range(200):
                images1 += [osp.join(root, f'image_2/{str(i).zfill(6)}_10.png')]
                images2 += [osp.join(root, f'image_2/{str(i).zfill(6)}_11.png')]
            for i in range(200):

                self.flow_occ_list += [osp.join(root, f'flow_occ/{str(i).zfill(6)}_10.png')]
                self.flow_noc_list += [osp.join(root, f'flow_noc/{str(i).zfill(6)}_10.png')]

        for im1, im2 in zip(images1, images2):
            dir = im1.split('/')[-2]
            frame1_id = im1.split('/')[-1].split('.')[0]
            frame2_id = im2.split('/')[-1].split('.')[0]
            self.extra_info += [ [f'{dir}_{frame1_id}', f'{dir}_{frame2_id}'] ]
            self.image_list += [ [im1, im2] ]

class HD1K(FlowDataset):
    def __init__(self, args, augmentor, is_training=True, split='training', root='/path/to/HD1K'):
        super(HD1K, self).__init__(args, augmentor=augmentor, sparse=True, is_training=is_training)

        self.flow_occ_list = []
        self.flow_noc_list = []

        if split == 'validation':

            seq_ix = 0
            while True:
                flows = sorted(glob(os.path.join(root, 'hd1k_flow_gt', 'flow_occ/%06d_*.png' % seq_ix)))
                images = sorted(glob(os.path.join(root, 'hd1k_input', 'image_2/%06d_*.png' % seq_ix)))

                if len(flows) == 0:
                    break

                for i in range(len(flows)-1):
                    self.flow_occ_list += [flows[i]]
                    self.flow_noc_list += [flows[i]]
                    self.image_list += [ [images[i], images[i+1]] ]
                    frame1_id = images[i].split('/')[-1].split('.')[0]
                    frame2_id = images[i+1].split('/')[-1].split('.')[0]
                    self.extra_info += [ [frame1_id, frame2_id] ]

                seq_ix += 1

        elif split == 'training':

            seq_ix = 0
            while True:
                images = sorted(glob(os.path.join(root, 'hd1k_challenge', 'image_2/%06d_*.png' % seq_ix)))

                if len(flows) == 0:
                    break

                for i in range(len(flows)-1):
                    self.image_list += [ [images[i], images[i+1]] ]
                    frame1_id = images[i].split('/')[-1].split('.')[0]
                    frame2_id = images[i+1].split('/')[-1].split('.')[0]
                    self.extra_info += [ [frame1_id, frame2_id] ]

                seq_ix += 1

class Chairs(FlowDataset):
    def __init__(self, args, augmentor, is_training=True, split='training', root='/path/to/FlyingChairs_release/data'):
        super(Chairs, self).__init__(args, augmentor=augmentor, sparse=False, is_training=is_training)

        self.args = args

        flows = sorted(glob(osp.join(root, '*.flo')))
        split_list = np.loadtxt('/path/to/FlyingChairs_release/FlyingChairs_train_val.txt', dtype=np.int32)

        images = sorted(glob(osp.join(root, '*.ppm')))
        assert (len(images)//2 == len(flows))

        for i in range(len(flows)):
            xid = split_list[i]
            if (split=='training' and xid==1) or (split=='validation' and xid==2):
                self.flow_list += [ flows[i] ]
                self.image_list += [ [images[2*i], images[2*i+1]] ]
                frame1_id = images[2*i].split('/')[-1].split('.')[0]
                frame2_id = images[2*i+1].split('/')[-1].split('.')[0]
                self.extra_info += [ [frame1_id, frame2_id] ]


from collections import Counter
from . import listings

def test_lists() -> None:
    includes = {"Connect4", "TicTacToe", "Othello8", "Othello16", "GameOfTheAmazons",
                "Hex5", "Hex11", "Hex13", "Connect6",
                "Havannah5", "Havannah8", "Breakthrough", "Tristannogo",
                "Minishogi", "Surakarta", "DiceShogi"}
    listed_items = listings.games()
    duplicated = {x: y for x, y in Counter(listed_items).items() if y > 1}
    assert duplicated == {}
    missing = includes - set(listed_items)
    assert not missing, f"Could not find {missing} (screening through core/game.h or model_zoo/init or main), was it renamed?"


import numpy as np
from PIL import Image
import matplotlib.pyplot as plt

import histogram_module
import dist_module

def rgb2gray(rgb):

    r, g, b = rgb[:, :, 0], rgb[:, :, 1], rgb[:, :, 2]
    gray = 0.2989 * r + 0.5870 * g + 0.1140 * b

    return gray

#
# model_images - list of file names of model images
# query_images - list of file names of query images
#
# dist_type - string which specifies distance type:  'chi2', 'l2', 'intersect'
# hist_type - string which specifies histogram type:  'grayvalue', 'dxdy', 'rgb', 'rg'
#
# note: use functions 'get_dist_by_name', 'get_hist_by_name' and 'is_grayvalue_hist' to obtain
#       handles to distance and histogram functions, and to find out whether histogram function
#       expects grayvalue or color image
#

def find_best_match(model_images, query_images, dist_type, hist_type, num_bins):

    hist_isgray = histogram_module.is_grayvalue_hist(hist_type)

    model_hists = compute_histograms(
        model_images, hist_type, hist_isgray, num_bins)
    query_hists = compute_histograms(
        query_images, hist_type, hist_isgray, num_bins)

    D = np.zeros((len(model_images), len(query_images)))

    # your code here
    for model_image_hist_index in range(D.shape[0]):
        for query_image_hist_index in range(D.shape[1]):
            model_hist = model_hists[model_image_hist_index]
            query_hist = query_hists[query_image_hist_index]
            D[model_image_hist_index][query_image_hist_index] = dist_module.get_dist_by_name(
                model_hist, query_hist, dist_type)

    best_match = np.argmin(D, 0)
    return best_match, D

def compute_histograms(image_list, hist_type, hist_isgray, num_bins):

    image_hist = []

    # compute histogram for each image and add it at the bottom of image_hist
    # your code here
    for image_name in image_list:
        image = np.array(Image.open(image_name)).astype('double')
        if hist_isgray:
            image = rgb2gray(image)

        image_hist.append(histogram_module.get_hist_by_name(
            image, num_bins, hist_type))

    return image_hist

#
# for each image file from 'query_images' find and visualize the 5 nearest images from 'model_image'.
#
# note: use the previously implemented function 'find_best_match'
# note: use subplot command to show all the images in the same Python figure, one row per query image
#

def show_neighbors(model_images, query_images, dist_type, hist_type, num_bins):

    num_nearest = 5  # show the top-5 neighbors

    [best_matches, D] = find_best_match(
        model_images, query_images, dist_type, hist_type, num_bins)

    best_matches = np.argsort(D, 0)[:num_nearest, :]

    # your code here
    f, axarr = plt.subplots(len(query_images), num_nearest + 1)

    for i, image in enumerate(query_images):

        neighbors = best_matches[:, i]
        axarr[i, 0].imshow(Image.open(query_images[i]))
        for j in range(len(neighbors)):
            axarr[i, 1 + j].imshow(Image.open(model_images[neighbors[j]]))

    plt.show()


import gym
import gym_digger
import time
from IPython.display import clear_output

env = gym.make('Digger-v0')

for episode in range(5):
    env.reset()
    done = False
    step = 0
    total_reward = 0
    while not done:
        step += 1
        action = env.action_space.sample()
        observation, reward, done, info = env.step(action)
        battery = info['battery']
        total_reward += reward

        # print stats
        print(f'episode: {episode}, step: {step}, action: {action}, reward: {reward}, tot_reward: {total_reward}, done: {done}, battery: {battery}')

        # render it
        env.render()

        # delay
        time.sleep(0.05)
        clear_output(wait=True)


from sanbproject.config.configuration import ConfigurationManager
import joblib
from sanbproject.components.data_transformation import DataTransformation
from sanbproject.utils.common import load_bin
from pathlib import Path
import os
import pandas as pd
from sanbproject.logger import logging

class PredictionPipeline:
    def __init__(self):
        # Get model path
        # Load configuration object
        self.config = ConfigurationManager()

    def return_data_transformation_config(self):
        data_transformation_config = self.config.get_data_transformation_config()
        return data_transformation_config

    def perform_text_preprocessing(self, input):
        # Fet data transformation config
        data_transformation_config = self.return_data_transformation_config()
        # Get data transformation object
        data_transformation_obj = DataTransformation(config=data_transformation_config)
        # Prepare the objects required for preprocessing the "review" text
        lemmatizer, punc_to_remove, stop_words, speller = data_transformation_obj.get_required_preprocessing_objects()
        # Perform text preprocessing
        processed_input = data_transformation_obj.perform_text_preprocessing(input, lemmatizer, punc_to_remove,
                                                                             stop_words, speller)

        logging.info(f"The review was processed into : {processed_input}")

        # Get tf-idf vectorizor object
        tf_idf_vectorizer = load_bin(Path(os.path.join(data_transformation_config.tf_idf_vectorizer_path,
                                                       "tf_idf_vectorizer_obj")))

        processed_input = [str(processed_input)]

        logging.info(f"The input being sent to tf-idf vectorizer {processed_input}")

        # Perform tf-idf vectorization
        processed_input = tf_idf_vectorizer.transform(processed_input)

        processed_input = pd.DataFrame(processed_input.toarray())

        processed_input = processed_input.reset_index(drop=True)

        processed_input.columns = processed_input.columns.astype(str)

        logging.info(f"The review after tf-idf vectorization : {processed_input}")
        return processed_input

    def return_model_eval_config(self):
        model_eval_config = self.config.get_model_eval_config()
        return model_eval_config

    def perform_prediction(self, input: str):
        logging.info(f"Prediction is starting for review : {input}")
        # Preprocess the input
        processed_input = self.perform_text_preprocessing(input)

        # Load the model
        model_eval_config = self.return_model_eval_config()
        model = joblib.load(model_eval_config.model_path)

        # Perform predictions
        prediction = model.predict(processed_input)[0]

        logging.info(f"Prediction was done successfully and the prediction is {prediction}")
        return prediction


#predict eddies on testdataset

from matplotlib.animation import ArtistAnimation
from model_components import *
from training_and_plot_utils  import *
from device_config_and_data_loader import *
import os
from fetch_data_utils import *

prev_date, prev_month, prev_year = get_dates_with_delta(331)

data_root = os.path.join(os.path.expanduser("~"), "ML_test")

val_folder = os.path.join(data_root, "cds_ssh_test_everyday_interval")

prev_date = int(prev_date)
prev_month = int(prev_month)
prev_year = int(prev_year)

val_file = os.path.join(val_folder, f"subset_pet_masks_with_adt_{prev_year:04d}{prev_month:02d}{prev_date:02d}_lat14N-46N_lon166W-134W.npz")

binary = False
num_classes = 2 if binary else 3

val_loader, _ = get_eddy_dataloader(
    val_file, binary=binary, batch_size=batch_size, shuffle=False
)

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.load_state_dict(torch.load("/home/chetana/tensorboard/2023-03-15_03-26/model_ckpt_68.pt"))
model.eval()

with torch.no_grad():
    fig, ax = plt.subplots(1, 3, figsize=(25, 10))
    artists = []
    # loop through all SSH maps and eddy masks in 2019
    # and run the model to generate predicted eddy masks
    for n, (ssh_vars, seg_masks, date_indices) in enumerate(val_loader):
        ssh_vars = ssh_vars.to(device)
        seg_masks = seg_masks.to(device)
        # Run the model to generate predictions
        preds = model(ssh_vars)

        # For each pixel, EddyNet outputs predictions in probabilities,
        # so choose the channels (0, 1, or 2) with the highest prob.
        preds = preds.argmax(dim=1)

        # Loop through all SSH maps, eddy masks, and predicted masks
        # in this minibatch and generate a video
        preds = preds.cpu().numpy()
        seg_masks = seg_masks.cpu().numpy()
        ssh_vars = ssh_vars.cpu().numpy()
        date_indices = date_indices.cpu().numpy()
        for i in range(len(ssh_vars)):
            date, img, mask, pred = date_indices[i], ssh_vars[i], seg_masks[i], preds[i]
            img1, title1, img2, title2, img3, title3 = plot_eddies_on_axes(
                date, img, mask, pred, ax[0], ax[1], ax[2]
            )
            artists.append([img1, title1, img2, title2, img3, title3])
            fig.canvas.draw()
            fig.canvas.flush_events()
    animation = ArtistAnimation(fig, artists, interval=200, blit=True)
    plt.close()

print(os.path.join(tensorboard_dir, "test_predictions.gif"))
animation.save(os.path.join(tensorboard_dir, "test_predictions.gif"), writer="pillow")

# HTML(animation.to_jshtml())

#plot contour

p = preds[0].astype(np.uint8)

print(f"Number of anticyclonic eddies: {count_eddies(p, eddy_type='anticyclonic')}")
print(f"Number of cyclonic eddies: {count_eddies(p, eddy_type='cyclonic')}")
print(f"Number of both eddies: {count_eddies(p, eddy_type='both')}")

# draw contours on the image
thr = cv2.threshold(p, 0, 1, cv2.THRESH_BINARY)[1].astype(np.uint8)
contours, hierarchy = cv2.findContours(thr, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
img = np.zeros(p.shape, np.uint8)
cv2.drawContours(img, contours, -1, (255, 255, 255), 1)
fileName = os.path.join("/home/chetana/plots/test/",f"{prev_year:04d}{prev_month:02d}{prev_date:02d}_contours.png")
cv2.imwrite(fileName, img)
plt.imshow(img, cmap="gray")
plt.axis("off")

# get average contour area
area = 0

for cnt in contours:
    area += cv2.contourArea(cnt)
area /= len(contours)

print(f"Average contour area: {area:.2f} sq. pixels")


from django.http import HttpResponse

def index(request):
    return HttpResponse("" +
                        "
" +
                        "
Main Page
" +
                        "
" +
                        "to start page
" +
                        "to start2 page
" +
                        # "to start3 page
" +
                        "to admin page
" +
                        "to app5 page
" +
                        "")


from django.urls import path
from home.views import HomeTemplateView, DetailView, ListTemplateView, Obrigado

app_name = 'home'

urlpatterns = [
  path('', HomeTemplateView.as_view()),
  path('detalhes/', DetailView.as_view(), name="detalhe"),
  path('lista/', ListTemplateView.as_view(), name="list"),
  path('obrigado/', Obrigado.as_view(), name="home")
]


import speech_recognition as shanu
from translate import Translator

test = shanu.Recognizer()
translator= Translator(to_lang="te")
with shanu.Microphone() as source:
    print("Now Start Speaking")
    audio = test.listen(source)
try:
    print("You said " + test.recognize_google(audio))
    answer=test.recognize_google(audio)
except LookupError:
    print("Could not understand audio")

translation = translator.translate(answer)

print(translation)


## EKF_visual ##
import torch
import torch.nn as nn
#from filing_paths import path_model
from config import get_h_derivative, eps, H,F, encoded_dimention, y_size
import sys
import numpy as np
import time
import matplotlib.pyplot as plt
#sys.path.insert(1, path_model)
from main_AE import test_epoch, create_dataset_loader
import math

if torch.cuda.is_available():
    dev = torch.device("cuda:0")  # you can continue going on here, like cuda:1 cuda:2....etc.
    torch.set_default_tensor_type('torch.cuda.FloatTensor')
else:
    dev = torch.device("cpu")
    #print("Running on the CPU")

class ExtendedKalmanFilter:

    def __init__(self, SystemModel,Lorenz_data_flag ,EKF_with_encoder_flag, model_encoder_trained, mode='full'):
        ########## training encoder with EKF ###########
        self.encoder = model_encoder_trained.double()
        self.EKF_with_encoder_flag=EKF_with_encoder_flag
        self.Lorenz_data_flag = Lorenz_data_flag
        #self.loss_EKF_with_encoser_for_training =  nn.MSELoss(reduction='mean')
        #self.optimizer=torch.optim.Adam(self.encoder.parameters(), lr=1e-3, weight_decay=1e-2)
        ##################################################
        #self.f = SystemModel.f
        self.F_func = SystemModel.F
        self.F = SystemModel.F
        self.m = SystemModel.m

        # Has to be transformed because of EKF non-linearity
        self.givenQ = SystemModel.givenQ

        self.h = SystemModel.h
        self.n = SystemModel.n
        self.H = SystemModel.H

        # Has to be transofrmed because of EKF non-linearity
        #self.R = SystemModel.realR
        self.givenR = SystemModel.givenR

        self.T = SystemModel.T
        self.T_test = SystemModel.T_test

        # Pre allocate KG array
        if EKF_with_encoder_flag:
            self.KG_array = torch.zeros((self.T_test, self.m, encoded_dimention))
        else:
            self.KG_array = torch.zeros((self.T_test, self.m, self.n))

        # Full knowledge about the model or partial? (Should be made more elegant)
        if (mode == 'full'):
            self.fString = 'ModAcc'
            self.hString = 'ObsAcc'
        elif (mode == 'partial'):
            self.fString = 'ModInacc'
            self.hString = 'ObsInacc'

    # Predict
    def Predict(self):
        ####### Predict the 1-st moment of x [F*x]
        self.x_t_given_prev = torch.matmul(self.F_func(self.x_t_est) ,self.x_t_est.double())
        #torch.squeeze(self.f(self.x_t_est.float()))
        #.type(torch.FloatTensor)
        # Predict the 1-st moment of y  [H*F*x]
        # Compute the Jacobians
        if self.EKF_with_encoder_flag: #H=I
            self.y_t_given_prev =  torch.matmul(self.H.double(), self.x_t_given_prev.double())
            self.UpdateJacobians(self.F_func(self.x_t_given_prev), self.H)
        else: #using real h function and its derivative for update stage
            self.y_t_given_prev = torch.squeeze(self.h(self.x_t_given_prev))
            self.UpdateJacobians(self.F_func(self.x_t_est),get_h_derivative(self.x_t_given_prev).view(-1, self.m))

        ####### Predict the 2-nd moment of x  cov(x)=[F*Cov(x)*F_T+Q]
        self.m2x_prior = torch.matmul(self.F, self.m2x_posterior.double())
        self.m2x_prior = torch.matmul(self.m2x_prior, self.F_T) + self.givenQ
        self.m2y = torch.matmul(self.H, self.m2x_prior)
        self.m2y = torch.matmul(self.m2y, self.H_T) + self.givenR

        # plt.imshow(self.y_t_given_prev)
        # plt.show()
        # plt.imshow(tmp.reshape(28,28))
        # plt.show()
        # plt.imshow(self.H.reshape(28,28))
        # plt.show()
        # Predict the 2-nd moment of y  cov(x)=[H*Cov(x)*H_T+R]

    # Compute the Kalman Gain
    def KGain(self):
        self.KG = torch.matmul(self.m2x_prior, self.H_T)
        if self.EKF_with_encoder_flag:
            self.KG = torch.matmul(self.KG.double().cpu(), torch.inverse(self.m2y.cpu().detach()+ eps * np.eye(encoded_dimention)))
        else:
            self.KG = torch.matmul(self.KG.double().cpu(), torch.inverse(self.m2y.cpu()+eps*np.eye(self.n)))
        # Save KalmanGain
        self.KG_array[self.i] = self.KG
        self.i += 1

    # Innovation
    def Innovation(self, y):
        if self.EKF_with_encoder_flag:
            self.dy = y - self.y_t_given_prev
        else:
            self.dy = y - self.y_t_given_prev.reshape(y_size*y_size) #after h function need to be flatten

    # Compute Posterior
    def Correct(self):
        # Compute the 1-st posterior moment
        self.x_t_est = self.x_t_given_prev + torch.matmul(self.KG.to(dev),self.dy.double().to(dev))
        # Compute the 2-nd posterior moment
        self.m2x_posterior = torch.matmul(self.m2y.double().to(dev), torch.transpose(self.KG, 0, 1).to(dev))
        self.m2x_posterior = self.m2x_prior - torch.matmul(self.KG.to(dev), self.m2x_posterior.to(dev))

    def Update(self, y):
        self.Predict()
        #t_start = time.time()
        self.KGain()
        #print('calculate KGain took {}'.format(time.time()-t_start))
        self.Innovation(y)
        self.Correct()
        return self.x_t_est, self.m2x_posterior

    def InitSequence_EKF(self, m1x_0, m2x_0):
        self.m1x_0 = m1x_0
        self.m2x_0 = m2x_0

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

    def UpdateJacobians(self, F, H):
        self.F = F.double()
        self.F_T = torch.transpose(F, 0, 1).double()
        self.H = H.double()
        self.H_T = torch.transpose(H, 0, 1).double()
        # print(self.H,self.F,'\n')

    ### Generate Sequence ###
    #########################
    def GenerateSequence(self, video, target_of_video, T):
        loss_fn = nn.MSELoss(reduction='mean')
        # test_loader = create_dataset_loader(video.cpu().detach().numpy(), target_of_video.cpu().detach().numpy(), True,32)
        # test_loss = test_epoch(self.encoder, self.encoder, test_loader, torch.nn.MSELoss(), 32, True, not True)
        # print(10 * math.log10(test_loss))

        # Pre allocate an array for predicted state and variance
        self.x = torch.empty(size=[self.m, T]) #space for estimation of state sequence
        self.encoder_output = torch.empty(size=[self.m, T])
        self.sigma = torch.empty(size=[self.m, self.m, T]) #covariance state noise
        # Pre allocate KG array
        if self.EKF_with_encoder_flag:
            self.KG_array = torch.zeros((T, self.m, encoded_dimention))   # space for KG of each time step
        else:
            self.KG_array = torch.zeros((T, self.m, self.n))
        self.i = 0  # Index for KG_array alocation
        self.x_t_est = torch.squeeze(self.m1x_0)
        self.m2x_posterior = torch.squeeze(self.m2x_0)

        for t in range(0, T):
            obsevation_t = video[:, t]  # taking the t'th image
            if self.EKF_with_encoder_flag:  # working on images so need to go throgh an encoder
                # imgplot = plt.imshow(yt.reshape(y_size, y_size).cpu().detach().numpy())
                # plt.title("obsevation")
                # plt.colorbar()
                # plt.show()
                obsevation_t = obsevation_t.reshape(1, 1, y_size, y_size)
                self.encoder.eval()
                encoder_output_t=torch.squeeze(self.encoder(obsevation_t.double()))
                self.encoder_output[:, t] = encoder_output_t
                #target_t = target_of_video[:,t]
                xt, sigmat = self.Update(encoder_output_t)
            else:
                xt, sigmat = self.Update(obsevation_t)
            self.sigma[:, :, t] = torch.squeeze(sigmat)
            self.x[:, t] = torch.squeeze(xt)


import tkinter as tk
from PIL import ImageTk, Image
import os

from Handlers.UIComponents.Usefuls import Usefuls

class TransitionalButton(tk.Canvas):
    def __init__(self, Root, OnClickFuncRef = None, bg=Usefuls.LightGrey, Mode="Side", OverlayImage = None, TwoTapToReverse=False, AutoPlay=True):

        self.Width = 125 if (Mode == "Side") else 30
        self.Height = 37 if (Mode == "Side") else 30

        super().__init__(Root, width=self.Width, height=self.Height, bg=bg, borderwidth=0, highlightthickness=0)

        self.AutoPlay = AutoPlay

        self.Mode = Mode
        self.TwoTapToReverse = TwoTapToReverse

        self.SideBarImage = ImageTk.PhotoImage(Image.open(Usefuls.DirectoryAbove + "/Image/TransitionalButton/SideBar.png").resize((125, 37), Image.ANTIALIAS))
        self.BottomBarImageR = ImageTk.PhotoImage(Image.open(Usefuls.DirectoryAbove + "/Image/TransitionalButton/BottomBarR.png").resize((30, 30), Image.ANTIALIAS))
        self.BottomBarImageL = ImageTk.PhotoImage(Image.open(Usefuls.DirectoryAbove + "/Image/TransitionalButton/BottomBarL.png").resize((30, 30), Image.ANTIALIAS))

        self.OverlayImage = OverlayImage
        self.OnClickFuncRef = OnClickFuncRef

        self.IsHighlighted = False
        self.Cooldown = False

        self.SetUpUI()

    def OnClick(self):

        if (self.Cooldown):
            return

        if (self.OnClickFuncRef):
            self.OnClickFuncRef()

        if self.AutoPlay:
            self.Cooldown = True
            self.PlayAnimation(not self.IsHighlighted)

    def SetUpUI(self):
        self.SlideImage = self.create_image(self.Width, 0, image=self.SideBarImage if self.Mode == "Side" else self.BottomBarImageL if self.Mode == "BL" else self.BottomBarImageR, anchor="nw")
        self.ButtonID = self.create_image(0, 0, image=self.OverlayImage, anchor="nw")
        self.tag_bind(self.ButtonID, "<1>", lambda x :[self.OnClick()])

    def PlayAnimation(self, Reversed = False, Lerp = 0, CallbackFuncRef=None):

        if (Reversed == self.IsHighlighted): return

        if Lerp > 1:
            self.IsHighlighted = Reversed

            if self.IsHighlighted:
                self.Cooldown = not self.TwoTapToReverse
            else:
                self.Cooldown = False

            if (CallbackFuncRef):
                CallbackFuncRef()
            return

        LerpFlipped = Lerp * (-1 if Reversed else 1)

        self.moveto(self.SlideImage, ((LerpFlipped * self.Width) + (self.Width if Reversed else 0)) if self.Mode == "Side" else 0, ((LerpFlipped * self.Height) + (self.Height if Reversed else 0)) if self.Mode != "Side" else 0)

        self.after(10, lambda: [self.PlayAnimation(Reversed, Lerp = Lerp + 0.1 * (1 - Lerp + 0.01), CallbackFuncRef=CallbackFuncRef)])

if __name__ == "__main__":
    root = tk.Tk()
    root.geometry("300x150")
    root["bg"] = bg=Usefuls.LightGrey

    # create canvas
    myCanvas = TransitionalButton(root, Mode="Bottom", OverlayImage=ImageTk.PhotoImage(Image.open(Usefuls.DirectoryAbove + "/Image/TransitionalButton/Info.png").resize((50, 50), Image.ANTIALIAS)))
    myCanvas.pack()

    myCanvas = TransitionalButton(root, Mode="Side", OverlayImage=ImageTk.PhotoImage(Image.open(Usefuls.DirectoryAbove + "/Image/TransitionalButton/CPP.png").resize((125, 37), Image.ANTIALIAS)))
    myCanvas.pack()
    # add to window and show
    root.mainloop()


from __future__ import print_function

__author__ = "rahulpaul@google.com (Rahul Paul)"

import pprint
import re
import sys

from oauth2client import client
from googleapiclient import sample_tools

def main(argv):
    # Authenticate and construct service.
    service, flags = sample_tools.init(
        argv,
        "audit",
        "v1",
        __doc__,
        __file__,
        scope="https://www.googleapis.com/auth/apps/reporting/audit.readonly",
    )

    try:
        activities = service.activities()

        # Retrieve the first two activities
        print("Retrieving the first 2 activities...")
        activity_list = activities.list(
            applicationId="207535951991",
            customerId="C01rv1wm7",
            maxResults="2",
            actorEmail="admin@enterprise-audit-clientlib.com",
        ).execute()
        pprint.pprint(activity_list)

        # Now retrieve the next 2 events
        match = re.search("(?<=continuationToken=).+$", activity_list["next"])
        if match is not None:
            next_token = match.group(0)

            print("\nRetrieving the next 2 activities...")
            activity_list = activities.list(
                applicationId="207535951991",
                customerId="C01rv1wm7",
                maxResults="2",
                actorEmail="admin@enterprise-audit-clientlib.com",
                continuationToken=next_token,
            ).execute()
            pprint.pprint(activity_list)

    except client.AccessTokenRefreshError:
        print(
            "The credentials have been revoked or expired, please re-run"
            "the application to re-authorize"
        )

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


import os
import glob
import struct
from PIL import Image, ImageEnhance
import matplotlib.pyplot as plt

import numpy as np
import cv2 as cv2
print('opencv version:', cv2.__version__)

# 半角カタカナとひらがな辞書
han_to_zen_dict = {
    'ｱ':'あ',
    'ｲ':'い',
    'ｳ':'う',
    'ｴ':'え',
    'ｵ':'お',
    'ｶ':'か',
    'ｷ':'き',
    'ｸ':'く',
    'ｹ':'け',
    'ｺ':'こ',
    'ｻ':'さ',
    'ｼ':'し',
    'ｽ':'す',
    'ｾ':'せ',
    'ｿ':'そ',
    'ﾀ':'た',
    'ﾁ':'ち',
    'ﾂ':'つ',
    'ﾃ':'て',
    'ﾄ':'と',
    'ﾅ':'な',
    'ﾆ':'に',
    'ﾇ':'ぬ',
    'ﾈ':'ね',
    'ﾉ':'の',
    'ﾊ':'は',
    'ﾋ':'ひ',
    'ﾌ':'ふ',
    'ﾍ':'へ',
    'ﾎ':'ほ',
    'ﾏ':'ま',
    'ﾐ':'み',
    'ﾑ':'む',
    'ﾒ':'め',
    'ﾓ':'も',
    'ﾔ':'や',
    'ﾕ':'ゆ',
    'ﾖ':'よ',
    'ﾗ':'ら',
    'ﾘ':'り',
    'ﾙ':'る',
    'ﾚ':'れ',
    'ﾛ':'ろ',
    'ﾜ':'わ',
    'ｦ':'を',
    'ﾝ':'ん',
    }

def jis0201_to_char(JIS0201_PATH):
    """
    jis0201とcharの辞書生成

    Parameters
    ----------
    JIS0201_PATH : string
        下記ファイルのパス
        http://ash.jp/ash/src/codetbl/jis0201.txt

    Returns
    -------
    JIS0201_char_dict : dict
        JIS0201とCharのdict E.g. '0xb1': 'あ'
    """
    JIS0201_char_dict = {}
    with open(JIS0201_PATH, 'r') as f:
        for row in f:
            if row[0] != '#':
                rows = row.split("\t")
                unicode_char = chr(int(rows[1], 16))

                if unicode_char in han_to_zen_dict:
                    hiragana = han_to_zen_dict[chr(int(rows[1], 16))]
                    JIS0201_char_dict[rows[0].lower()] = hiragana
                else:
                    JIS0201_char_dict[rows[0].lower()] = unicode_char

    return JIS0201_char_dict

def jis0208_to_char(JIS0208_PATH):
    """
    jis0208とcharの辞書生成

    Parameters
    ----------
    JIS0208_PATH : string
        下記ファイルのパス
        http://ash.jp/ash/src/codetbl/jis0208.txt

    Returns
    -------
    JIS0208_char_dict : dict
        JIS0208とCharのdict E.g. '0xb1': 'あ'
    """
    JIS0208_char_dict = {}
    with open(JIS0208_PATH, 'r') as f:
        for row in f:
            if row[0] != '#':
                rows = row.split("\t")
                unicode_char = chr(int(rows[2], 16))
                JIS0208_char_dict[rows[1].lower()] = unicode_char
    return JIS0208_char_dict

def convert_m_type(target_dir, JIS0201_char_dict):
    """
    ETL1, 6, 7のバイナリデータ(M-Type)をPNGに変換します
    M-Type : http://etlcdb.db.aist.go.jp/etlcdb/etln/form_m.htm

    Parameters
    ----------
    target_dir : string
        バイナリデータがある親ディレクトリ. (E.g. ~/ETL7/)
    JIS0201_char_dict: dict
        JIS0201とCharのdict E.g. '0xb1': 'あ'

    Returns
    -------
    img_path_list : list
        saveしたimgのフルパスのリスト. E.g. [[img_full_path, unicode char e.g.'あ']...]

    """

    allFiles = glob.glob(target_dir + '*')
    targetFiles = [path for path in allFiles if os.path.isfile(path)]

    img_path_list = []

    # 変換. binary -> png.
    for etlfile in targetFiles:
        if 'INFO' in etlfile: # infoにはpngが含まれていないため.
            continue

        # 出力先ディレクトリ作成.
        dir_name = os.path.basename(etlfile)
        img_dir = target_dir + dir_name + '_IMG'
        os.makedirs(img_dir, exist_ok=True)
        print('create output image folder:', img_dir)

        # convert m-type data.
        RECORD_SIZE = 2052
        X_SIZE = 64
        Y_SIZE = 63
        DATA_NUM_POSITION = 0
        SERIAL_SHEETS_NUM_POSTION = 2
        JIS_CODE_POSITION = 3 # JIS Code (JIS X 0201)
        IMAGE_POSITION = 18
        Y_POSITION = 14
        X_POSITION = 15

        f = open(etlfile, 'rb')
        f.seek(0)

        while True:
            s = f.read(RECORD_SIZE)

            # stream完了でbreak.
            if not s:
                break

            # byte unpack
            r = struct.unpack('>H2sH6BI4H4B4x2016s4x', s)
            # read code jis.
            code_jis = r[JIS_CODE_POSITION]

            # JIS0201からunicode文字を取得.
            unicode_char = JIS0201_char_dict[ hex(code_jis) ]

            # JIS_CODE毎の出力ディレクトリ作成.
            jis_path = img_dir + '/' + str(code_jis)
            os.makedirs(jis_path, exist_ok=True)
            # imgのファイル名作成.
            fn = "{0:02x}_{1:02x}_{2:04x}.png".format(code_jis, r[DATA_NUM_POSITION], r[SERIAL_SHEETS_NUM_POSTION])
            image_full_path = jis_path + "/" + fn

            # when already exist image, skip save.
            if os.path.exists(image_full_path) == False:
                # read image.
                iF = Image.frombytes('F', (X_SIZE, Y_SIZE), r[IMAGE_POSITION], 'bit', 4)
                iP = iF.convert('L')

                # save image.
                enhancer = ImageEnhance.Brightness(iP)
                iE = enhancer.enhance(16)
                iE.save(image_full_path)

            img_path_list.append([image_full_path, unicode_char])

    return img_path_list

def convert_b_type(target_dir, JIS0208_char_dict):
    """
    ETL8B, 9のバイナリデータ(B-Type)をPNGに変換します
    B-Type : http://etlcdb.db.aist.go.jp/specification-of-etl-8

    Parameters
    ----------
    target_dir : string
        バイナリデータがある親ディレクトリ. (E.g. ~/ETL8B/)
    JIS0208_char_dict: dict
        JIS0208とCharのdict E.g. '0xb1': 'あ'

    Returns
    -------
    img_path_list : list
        saveしたimgのフルパスのリスト. E.g. [[img_full_path, unicode char e.g.'あ']...]

    """
    allFiles = glob.glob(target_dir + '*')
    targetFiles = [path for path in allFiles if os.path.isfile(path)]
    #print(targetFiles)

    img_path_list = []

    # 変換. binary -> png.
    for etlfile in targetFiles:
        if 'INFO' in etlfile: # infoにはpngが含まれていないため.
            continue

        # 出力先ディレクトリ作成.
        dir_name = os.path.basename(etlfile)
        img_dir = target_dir + dir_name + '_IMG'
        os.makedirs(img_dir, exist_ok=True)
        print('create output image folder:', img_dir)

        # convert m-type data.
        RECORD_SIZE = 512
        X_SIZE = 64
        Y_SIZE = 63
        SERIAL_SHEETS_NUM_POSTION = 0
        JIS_CODE_POSITION = 1 # JIS Code (JIS X 0208)
        IMAGE_POSITION = 3

        f = open(etlfile, 'rb')
        f.seek(0)

        while True:
            s = f.read(RECORD_SIZE)

            # stream完了でbreak.
            if not s:
                break

            # byte unpack
            r = struct.unpack('>2H4s504s', s)

            # read code jis.
            code_jis = r[JIS_CODE_POSITION]
            if code_jis == 8224: # this is dummy file.
                continue

            # JIS0208からunicode文字を取得.
            unicode_char = JIS0208_char_dict[ hex(code_jis) ]

            # JIS_CODE毎の出力ディレクトリ作成.
            jis_path = img_dir + '/' + str(code_jis)
            os.makedirs(jis_path, exist_ok=True)
            # imgのファイル名作成.
            fn = "{0:02x}_{1:04x}.png".format(code_jis, r[SERIAL_SHEETS_NUM_POSTION])
            image_full_path = jis_path + "/" + fn

            # when already exist image, skip save.
            if os.path.exists(image_full_path) == False:
                # read image.
                iF = Image.frombytes('1', (X_SIZE, Y_SIZE), r[IMAGE_POSITION], 'raw')
                iP = iF.convert('L')

                # save image.
                enhancer = ImageEnhance.Brightness(iP)
                iE = enhancer.enhance(16)
                iE.save(image_full_path)

            img_path_list.append([image_full_path, unicode_char])

    return img_path_list

def convertCVtoPIL(cv_image):
    '''
    OpenCV型 -> PIL型
    '''
    new_image = cv_image.copy()
    if new_image.ndim == 2:  # モノクロ
        pass
    elif new_image.shape[2] == 3:  # カラー
        new_image = cv2.cvtColor(new_image, cv2.COLOR_BGR2RGB)
    elif new_image.shape[2] == 4:  # 透過
        new_image = cv2.cvtColor(new_image, cv2.COLOR_BGRA2RGBA)
    new_image = Image.fromarray(new_image)
    return new_image

# convert PIL to OpenCV
def convertPILtoCV(pil_image):
    '''
    PIL型 -> OpenCV型

    Parameters
    ----------
    pil_image : obj

    Returns
    -------
    cv image : np.array
    '''
    new_image = np.array(pil_image, dtype=np.uint8)
    if new_image.ndim == 2:  # モノクロ
        pass
    elif new_image.shape[2] == 3:  # カラー
        new_image = cv2.cvtColor(new_image, cv2.COLOR_RGB2BGR)
    elif new_image.shape[2] == 4:  # 透過
        new_image = cv2.cvtColor(new_image, cv2.COLOR_RGBA2BGRA)
    #return np.asarray(pil_image)
    return new_image

def create_contour(img_path):
    """
    opencvを利用し、外接矩形を抽出します.
    この関数は、ETL文字データベースの黒画像に白文字で1文字のみ文字が存在することを前提としています.

    Parameters
    ----------
    img_path : string
        画像データのフルパス E.g.[[img_full_path, unicode char e.g.'あ']...]

    Returns
    -------
    output : list
        img_pathに外接矩形の座標を付与したリスト E.g. [[img_path, [leftTop_x, leftTop_y, width, height], unicode char e.g. 'あ']...]

    """
    # load image.
    img_cv = cv2.imread(img_path[0], cv2.IMREAD_COLOR)
    # get image size.
    height, width, channels = img_cv.shape
    image_size = height * width

    # 入力画像の中には、画像の端まで文字がかかれているものがある
    # 余白がないとうまく文字の外接矩形を切り出せないため黒画素の画像を追加し余白を生成する.
    BLANK_SIZE = 1
    height_blank_array = np.zeros((height, BLANK_SIZE, 3), np.uint8)
    width_blank_array = np.zeros((1, width+BLANK_SIZE*2, 3), np.uint8) # width+BLANK_SIZE*2は先に幅1の画像を両端に連結するため.
    width_blank_pil = Image.fromarray(np.uint8(width_blank_array))
    height_blank_pil = Image.fromarray(np.uint8(height_blank_array))
    # 左右に連結.
    concat_img_cv = cv2.hconcat([convertPILtoCV(height_blank_pil), img_cv])
    concat_img_cv = cv2.hconcat([concat_img_cv, convertPILtoCV(height_blank_pil)])
    # 上下に連結.
    concat_img_cv = cv2.vconcat([convertPILtoCV(width_blank_pil), concat_img_cv])
    concat_img_cv = cv2.vconcat([concat_img_cv, convertPILtoCV(width_blank_pil)])

    # to gray scale.
    img_gray = cv2.cvtColor(concat_img_cv, cv2.COLOR_RGB2GRAY)

    # 2値化.
    # ref http://labs.eecs.tottori-u.ac.jp/sd/Member/oyamada/OpenCV/html/py_tutorials/py_imgproc/py_thresholding/py_thresholding.html
    ret,img_thresh = cv2.threshold(img_gray,155,255,cv2.THRESH_BINARY_INV)

    # 輪郭の取得.
    contours, hierarchy = cv2.findContours(img_thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

    for index, contour in enumerate(contours):
        area = cv2.contourArea(contour)
        # 画像全体を占めるareaを除去.
        if image_size * 0.99 < area:
            contours.pop(index)
            continue
        # 画像サイズの0.3%以下はノイズとして除去.
        # 64*63の画像中に12以下areaでノイズとして判定
        if area < image_size * 0.003:
            contours.pop(index)

    # 文字の外接矩形を取得.
    # inputの画像に文字は1文字のみという前提.
    # そのため、最小のx,y座標, 最大のx,y座標を抽出し、矩形を作り文字の外接矩形とする.
    x_list = []
    y_list = []

    for index, contour in enumerate(contours):
        for coordinate in contour:
            x_list.append(coordinate[0][0])
            y_list.append(coordinate[0][1])

    # 追加したBLANK_SIZE分座標を調整.
    if len(x_list) != 0 and len(y_list):
        min_x = min(x_list) - BLANK_SIZE
        if min_x < 0:
            min_x = 0
        min_y = min(y_list) - BLANK_SIZE
        if min_y < 0:
            min_y = 0
        max_x = max(x_list) - BLANK_SIZE
        max_y = max(y_list) - BLANK_SIZE
        rec_width = max_x - min_x
        if width < rec_width:
            rec_width = width
        rec_height = max_y - min_y
        if height < rec_height:
            rec_height = height
    else:
        print('this file was not get contours:', img_path[0])
        min_x, min_y, rec_width, rec_height = 0, 0, 0, 0

    return [img_path[0], [min_x,min_y,rec_width,rec_height], img_path[1]]

def showCVimage(cv_image, description=''):
    """
    show openCV image as PIL image.
    """
    if len(description) != 0:
        print(description)
    array = np.asarray(convertCVtoPIL(cv_image))

    _, axes = plt.subplots(1, 1,figsize=(10,10))
    #_, axes = plt.subplots(dpi=300)

    axes.imshow(array)
    axes.set_facecolor('m')
    plt.show()

    del array

def showRectangleImage(img_path, xywh, name):
    """
    画像に矩形をplotして出力.
    """
    cv_image = cv2.imread(img_path, cv2.IMREAD_COLOR)

    rec_cv_image = cv2.rectangle(
        cv_image,
        (xywh[0][0], xywh[0][1]),
        (xywh[0][0]+xywh[0][2], xywh[0][1]+xywh[0][3]), (255, 0, 0), 1
    )
    showCVimage(rec_cv_image, name)


import os
import re
import sublime
import sublime_plugin
import subprocess
import zipfile
try: # ST2
    from version_file_checker import check_file_match
except ImportError: # ST3
    from .version_file_checker import check_file_match

RESULT_VIEW_NAME = 'csslint_result_view'
RESULT_REGION_NAME = 'csslint_highlighted_region'
SETTINGS_FILE = "CSSLint.sublime-settings"
PLUGIN_PATH = os.path.abspath(os.path.dirname(__file__))
PACKAGE_PATH = os.path.abspath(os.path.join(sublime.packages_path(), 'CSSLint'))
SCRIPT_PATH = os.path.join(PACKAGE_PATH, 'scripts')
MANIFEST = ''

def plugin_loaded():
    """
    ST3: If run from a zip file, this method verifies the checksums
    of the CSSLint and Rhino scripts and extracts them to the packages
    folder if necessary.
    """
    global PLUGIN_PATH
    global PACKAGE_PATH
    global SCRIPT_PATH

    PLUGIN_PATH = os.path.abspath(os.path.dirname(__file__))
    PACKAGE_PATH = os.path.abspath(os.path.join(sublime.packages_path(), 'CSSLint'))
    SCRIPT_PATH = os.path.join(PACKAGE_PATH, 'scripts')

    manifest = [
        {
            'file_path': 'scripts/csslint/csslint-rhino.js',
            'checksum': '71d63e12a904771978ddf06f22933e1f3e3812155545b844769703926f9dc027'
        },
        {
            'file_path': 'scripts/rhino/js.jar',
            'checksum': '7ade44513268f7cfe08579f8ef69e4ea663b8613d3923491000784c650c67c8b'
        }
    ]

    file_matches = check_file_match(manifest, path_prefix=PACKAGE_PATH)

    if '.sublime-package' in PLUGIN_PATH:
        print('CSSLint is a .sublime-package; Verifying required files.')
        package = zipfile.ZipFile(PLUGIN_PATH, 'r')

        for match in file_matches:
            if match['isMatch'] is False:
                print('CSSLint: extracting {0} into {1}'.format(match['file_path'], PACKAGE_PATH))
                package.extract(match['file_path'], PACKAGE_PATH)

class CsslintCommand(sublime_plugin.TextCommand):

    def run(self, edit, paths=False, quiet=False):
        settings         = sublime.load_settings(SETTINGS_FILE)
        self.edit        = edit
        self.file_path   = None
        file_paths       = None
        self.file_paths  = None
        cssFiles         = []
        self.use_console = True

        def add_css_to_list(path):
            if path.endswith('.css'):
                cssFiles.append('"' + path + '"')

        # Make new document for lint results - we're linting multiple files.
        if paths is not False:
            self.use_console = False

            # Walk through any directories and make a list of css files
            for path in paths:
                if os.path.isdir(path) is True:
                    for path, subdirs, files in os.walk(path):
                        for name in files:
                            add_css_to_list(os.path.join(path, name))

                else:
                    add_css_to_list(path)

            # Generate the command line paths argument
            if len(cssFiles) < 1:
                sublime.error_message("No CSS files selected.")
                return

            else:
                self.file_paths = cssFiles
                file_paths      = ' '.join(cssFiles)

                # set up new file for lint results
                self.current_document = sublime.active_window().new_file()
                self.current_document.insert(self.edit,
                                             self.current_document.size(),
                                             'CSSLint Results\n\n')

        # Invoke console - we're linting a single file.
        else:
            if self.view.window().active_view().file_name() is None:
                if quiet is False:
                    sublime.error_message("CSSLint: Please save your file before linting.")
                return

            if self.view.window().active_view().file_name().endswith('css') is not True:
                if quiet is False:
                    sublime.error_message("CSSLint: This is not a css file.")
                return

            self.file_path = '"' + self.view.window().active_view().file_name() + '"'
            # init_tests_panel(self)
            show_tests_panel(self)

        # Begin linting.
        file_name          = os.path.basename(self.file_path) if self.file_path else ', '.join(self.file_paths)
        self.buffered_data = ''
        self.file_name     = file_name
        path_argument      = file_paths if file_paths else self.file_path
        self.is_running    = True
        rhino_path         = settings.get('rhino_path') if settings.has('rhino_path') and settings.get('rhino_path') != False else '"{0}"'.format(os.path.join(SCRIPT_PATH, 'rhino', 'js.jar'))
        csslint_rhino_js   = settings.get('csslint_rhino_js') if settings.has('csslint_rhino_js') and settings.get('csslint_rhino_js') != False else '"{0}"'.format(os.path.join(SCRIPT_PATH, 'csslint', 'csslint-rhino.js'))
        errors             = ' --errors=' + ','.join(settings.get('errors')) if isinstance(settings.get('errors'), list) and len(settings.get('errors')) > 0 else ''
        warnings           = ' --warnings=' + ','.join(settings.get('warnings')) if isinstance(settings.get('warnings'), list) and len(settings.get('warnings')) > 0 else ''
        ignores            = ' --ignore=' + ','.join(settings.get('ignore')) if isinstance(settings.get('ignore'), list) and len(settings.get('ignore')) > 0 else ''
        options            = '--format=compact' + errors + warnings + ignores
        cmd                = 'java -jar ' + rhino_path + ' ' + csslint_rhino_js + ' ' + options + ' ' + path_argument

        self.run_linter(cmd)

    def update_status(self, msg, progress):
        sublime.status_message(msg + " " + progress)

    def process_data(self, data, end=False):

        # truncate file paths but save them in an array.
        # add error number to each line - needed for finding full path.
        def munge_errors(data):
            data_all_lines      = data.split('\n')
            data_nonempty_lines = []
            self.errors         = []

            # remove empty lines
            for line in data_all_lines:
                if len(line) > 0:
                    data_nonempty_lines.append(line)

            # truncate path for display, save full path in array.
            for line in data_nonempty_lines:
                full_path_string   = line[0:line.find('css:') + 3]
                path_to_remove     = full_path_string + ': '
                cleaned_error_item = line.replace(path_to_remove, '')
                found_error        = False

                def add_new_error():
                    new_error_stylesheet = {
                        'full_path': full_path_string,
                        'items': [cleaned_error_item]
                    }
                    self.errors.append(new_error_stylesheet)

                for error in self.errors:
                    if error['full_path'] == full_path_string:
                        found_error = True
                        error['items'].append(cleaned_error_item)
                        break

                if found_error is False:
                    add_new_error()

        # Concatenate buffered data but prevent duplicates.
        self.buffered_data = self.buffered_data + data.decode("utf-8")
        data = self.buffered_data.replace('\r\n', '\n').replace('\r', '\n')

        if end is False:
            rsep_pos = data.rfind('\n')
            if rsep_pos == -1:
                # not found full line.
                return
            self.buffered_data = data[rsep_pos+1:]
            data = data[:rsep_pos+1]

        munge_errors(data)

        # Push to display.
        if self.use_console is True:
            self.output_to_console()
        else:
            self.output_to_document()

    def output_to_console(self):
        self.output_view.set_read_only(False)

        for error_section in self.errors:
            self.output_view.insert(self.edit, self.output_view.size(), '\n'.join(error_section['items']))

        self.output_view.set_read_only(True)
        CsslintEventListener.disabled = False

    def output_to_document(self):
        for error_section in self.errors:
            error_output = error_section['full_path'] + '\n\t' + '\n\t'.join(error_section['items']) + '\n\n'
            self.current_document.insert(self.edit, self.current_document.size(), error_output)

    def run_linter(self, cmd):
        self.proc = subprocess.Popen(cmd,
                                     env={"PATH": os.environ['PATH']},
                                     shell=True,
                                     stdout=subprocess.PIPE,
                                     stderr=subprocess.STDOUT)

        result = self.proc.communicate()[0]

        if result is not None:
            sublime.set_timeout(self.process_data(result), 0)

class CsslintSelectionCommand(sublime_plugin.WindowCommand):
    def run(self, paths=[]):
        self.view.window().run_command('csslint', {"paths": paths})

class CsslintEventListener(sublime_plugin.EventListener):
    disabled = False

    def __init__(self):
        self.previous_region = None
        self.file_view = None

    def on_post_save(self, view):
        settings = sublime.load_settings(SETTINGS_FILE)

        if settings.get('run_on_save') is True:
            view.window().run_command("csslint", {'quiet': True})

    # for some reason on_selection_modified_async does not fire any events,
    # but this one does.
    def on_selection_modified(self, view):
        if CsslintEventListener.disabled:
            return
        if view.name() != RESULT_VIEW_NAME:
            return
        region = view.line(view.sel()[0])

        # make sure call once.
        if self.previous_region == region:
            return
        self.previous_region = region

        # extract line from csslint result.
        text = view.substr(region)

        if len(text) < 1:
            return

        line = re.search('(?<=line\s)[0-9]+', text).group(0)

        # hightlight view line.
        view.add_regions(RESULT_VIEW_NAME, [region], "comment")

        # highlight the selected line in the active view.
        file_view = sublime.active_window().active_view() # if self.file_view is None else self.file_view
        file_view.run_command("goto_line", {"line": line})
        file_region = file_view.line(file_view.sel()[0])

        # highlight file_view line
        region_settings = sublime.DRAW_NO_FILL if hasattr(sublime, 'DRAW_NOFILL') else sublime.DRAW_OUTLINED
        file_view.add_regions(RESULT_REGION_NAME, [file_region], "string", "", region_settings)

        if hasattr(self, 'file_view') is True:
            self.file_view = file_view

    def on_deactivated(self, view):
        if view.name() == RESULT_VIEW_NAME:
            if hasattr(self, 'file_view'):
                self.file_view.erase_regions(RESULT_REGION_NAME)

def show_tests_panel(self):
    """Initializes (if not already initialized) and shows the results output panel."""

    if not hasattr(self, 'output_view'):
        try:  # ST3
            self.output_view = self.view.window().create_output_panel(RESULT_VIEW_NAME)
        except AttributeError: # ST2
            self.output_view = self.view.window().get_output_panel(RESULT_VIEW_NAME)

        self.output_view.set_name(RESULT_VIEW_NAME)

        # self.output_view.settings().set("file_path", self.file_path)

    clear_test_view(self)

    self.view.window().run_command("show_panel", {"panel": "output." + RESULT_VIEW_NAME})

def clear_test_view(self):
    self.output_view.set_read_only(False)
    self.output_view.erase(self.edit, sublime.Region(0, self.output_view.size()))
    self.output_view.set_read_only(True)


#! /usr/bin/env nix-shell
#! nix-shell -i python3 -p python3Packages.beautifulsoup4 python3Packages.requests
# SPDX-License-Identifier: MPL-2.0

"""
Simple update script for managing Visual Studio Code extensions with Nix.

Given a file named `sources.json`, in the same directory as this script, with
a list of objects containing keys that correspond to the extension's publisher
and name:

.. highlight:: json
.. code-block:: json

    [{
        "publisher": "bbenoist",
        "name": "nix"
    }]

...this script will update that file in-place with version, download URL, and
hash information.

If the extension has architecture-specific versions available, the download
URLs for any matching platforms in the global `PLATFORMS` dict.
"""

import json
from concurrent.futures import ThreadPoolExecutor
import os
import subprocess
from tempfile import NamedTemporaryFile

from bs4 import BeautifulSoup
import requests

ROOT_PATH = os.path.dirname(os.path.realpath(__file__))

# Mapping between VS Code platforms (keys) & Nix platforms (values).
PLATFORMS = {
    "darwin-x64": "x86_64-darwin",
    "darwin-arm64": "aarch64-darwin",
    "linux-x64": "x86_64-linux",
    "linux-arm64": "aarch64-linux",
}

def get_extension_sha(url):
    """Get the base32 SRI hash for the VS Code extension at some url."""
    resp = requests.get(url)
    sha = None
    with NamedTemporaryFile() as extension:
        extension.write(resp.content)
        sha = calculate_nix_sha(extension.name)
    return sha

def calculate_nix_sha(fpath):
    """Calculate the base32 SRI hash for a given package."""
    sha = subprocess.check_output(
        ["nix", "hash", "file", "--base32", "--sri", "--type", "sha256", fpath]
    )
    return sha.decode().strip()

def get_multiarch_ext_url(version_blob):
    """Get the extension download URL for a given platform."""
    platform = version_blob["targetPlatform"]
    url = get_ext_url(version_blob)
    sha = get_extension_sha(url)
    return {PLATFORMS[platform]: {"url": url, "sha256": sha}}

def get_multiarch_ext_urls(target_version, version_blobs):
    """Get the extension download URLs for any matching platforms."""
    # pylint: disable=redefined-outer-name
    futures = []
    with ThreadPoolExecutor(max_workers=4) as executer:
        for blob in version_blobs:
            blob_platform = blob.get("targetPlatform")
            blob_version = blob.get("version")
            if (
                blob_platform is not None
                and blob_platform in PLATFORMS
                and blob_version == target_version
            ):
                future = executer.submit(get_multiarch_ext_url, blob)
                futures.append(future)

    results = {}
    for future in futures:
        results.update(future.result())
    return results

def get_ext_url(version_blob):
    """Get the extension download URL for a VS Code extension."""
    # Presumably there's a more idiomatic way to do this...
    return next(
        (
            file["source"]
            for file in version_blob["files"]
            if file["assetType"] == "Microsoft.VisualStudio.Services.VSIXPackage"
        )
    )

def get_ext_blobs(publisher, ext_name):
    """Get the metadata associated with some VS Code extension."""
    name = f"{publisher}.{ext_name}"
    params = {"itemName": name, "ssr": "false"}
    url = "https://marketplace.visualstudio.com/items#version-history"

    page = requests.get(url, params=params)
    soup = BeautifulSoup(page.content, "html.parser")

    ext_txt = soup.find("script", {"class": "vss-extension"}).get_text()
    ext_json = json.loads(ext_txt)
    return ext_json["versions"]

def get_ext_info(source):
    """Get the download information for a VS Code extension."""
    publisher = source["publisher"]
    ext_name = source["name"]
    version_blobs = get_ext_blobs(publisher, ext_name)
    latest = version_blobs[0]
    version = latest["version"]

    # If the version hasn't changed, don't do any more work.
    current_version = source.get("version")
    if (current_version is not None) and (version == current_version):
        return source

    is_multiarch = "targetPlatform" in latest

    src = {"name": f"{publisher}-{ext_name}-{version}.zip"}
    if is_multiarch:
        platforms = get_multiarch_ext_urls(version, version_blobs)
        src["platforms"] = platforms
    else:
        url = get_ext_url(latest)
        src["url"] = url
        src["sha256"] = get_extension_sha(url)
    return {"publisher": publisher, "name": ext_name, "version": version, "src": src}

sources = []
with open(f"{ROOT_PATH}/sources.json", "r", encoding="utf-8") as f:
    sources = json.load(f)

# pylint: disable=invalid-name
futures = None
with ThreadPoolExecutor(max_workers=4) as executor:
    futures = executor.map(get_ext_info, sources)

updated_sources = list(futures)

with open(f"{ROOT_PATH}/sources.tmp.json", "w", encoding="utf-8") as f:
    json.dump(updated_sources, f, ensure_ascii=False, indent=4)

os.replace(f"{ROOT_PATH}/sources.tmp.json", f"{ROOT_PATH}/sources.json")


from __future__ import absolute_import, division, print_function

import pytest ; pytest

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------

# Standard library imports
import mock
from mock import patch

# External imports

# Bokeh imports
from bokeh.core.validation import check_integrity
from bokeh.plotting import figure

from bokeh.models import GlyphRenderer, Label, Plot, LinearAxis, CustomJS
from bokeh.models.ranges import FactorRange, DataRange1d, Range1d
from bokeh.models.scales import CategoricalScale, LinearScale, LogScale
from bokeh.models.tools import PanTool

# Module under test
import bokeh.models.plots as bmp

#-----------------------------------------------------------------------------
# Setup
#-----------------------------------------------------------------------------

_LEGEND_EMPTY_WARNING = """
You are attempting to set `plot.legend.location` on a plot that has zero legends added, this will have no effect.

Before legend properties can be set, you must add a Legend explicitly, or call a glyph method with the 'legend' parameter set.
"""

#-----------------------------------------------------------------------------
# General API
#-----------------------------------------------------------------------------

class TestPlotLegendProperty(object):

    def test_basic(self):
        plot = figure(tools='')
        x = plot.legend
        assert isinstance(x, bmp._list_attr_splat)
        assert len(x) == 0
        plot.circle([1,2], [3,4], legend="foo")
        x = plot.legend
        assert isinstance(x, bmp._list_attr_splat)
        assert len(x) == 1

    def test_warnign(self):
        plot = figure(tools='')
        with pytest.warns(UserWarning) as warns:
            plot.legend.location = "above"
            assert len(warns) == 1
            assert warns[0].message.args[0] == _LEGEND_EMPTY_WARNING

class TestPlotSelect(object):

    def setup_method(self):
        self._plot = figure(tools='pan')
        self._plot.circle([1,2,3], [3,2,1], name='foo')

    @patch('bokeh.models.plots.find')
    def test_string_arg(self, mock_find):
        self._plot.select('foo')
        assert mock_find.called
        assert mock_find.call_args[0][1] == dict(name='foo')

    @patch('bokeh.models.plots.find')
    def test_type_arg(self, mock_find):
        self._plot.select(PanTool)
        assert mock_find.called
        assert mock_find.call_args[0][1] == dict(type=PanTool)

    @patch('bokeh.models.plots.find')
    def test_kwargs(self, mock_find):
        kw = dict(name='foo', type=GlyphRenderer)
        self._plot.select(**kw)
        assert mock_find.called
        assert mock_find.call_args[0][1] == kw

    @patch('bokeh.models.plots.find')
    def test_single_selector_kwarg(self, mock_find):
        kw = dict(name='foo', type=GlyphRenderer)
        self._plot.select(selector=kw)
        assert mock_find.called
        assert mock_find.call_args[0][1] == kw

    def test_selector_kwarg_and_extra_kwargs(self):
        with pytest.raises(TypeError) as exc:
            self._plot.select(selector=dict(foo='foo'), bar='bar')
        assert "when passing 'selector' keyword arg, not other keyword args may be present" == str(exc.value)

    def test_bad_arg_type(self):
        with pytest.raises(TypeError) as exc:
            self._plot.select(10)
        assert "selector must be a dictionary, string or plot object." == str(exc.value)

    def test_too_many_args(self):
        with pytest.raises(TypeError) as exc:
            self._plot.select('foo', 'bar')
        assert 'select accepts at most ONE positional argument.' == str(exc.value)

    def test_no_input(self):
        with pytest.raises(TypeError) as exc:
            self._plot.select()
        assert 'select requires EITHER a positional argument, OR keyword arguments.' == str(exc.value)

    def test_arg_and_kwarg(self):
        with pytest.raises(TypeError) as exc:
            self._plot.select('foo', type=PanTool)
        assert 'select accepts EITHER a positional argument, OR keyword arguments (not both).' == str(exc.value)

class TestPlotValidation(object):

    def test_missing_renderers(self):
        p = figure()
        p.renderers = []
        with mock.patch('bokeh.core.validation.check.log') as mock_logger:
            check_integrity([p])
        assert mock_logger.warning.call_count == 1
        assert mock_logger.warning.call_args[0][0].startswith("W-1000 (MISSING_RENDERERS): Plot has no renderers")

    def test_missing_scale(self):
        p = figure()
        p.x_scale = None
        with mock.patch('bokeh.core.validation.check.log') as mock_logger:
            check_integrity([p])
        assert mock_logger.error.call_count == 1
        assert mock_logger.error.call_args[0][0].startswith("E-1008 (REQUIRED_SCALE): A required Scale object is missing: x_scale")

        p.y_scale = None
        with mock.patch('bokeh.core.validation.check.log') as mock_logger:
            check_integrity([p])
        assert mock_logger.error.call_count == 1
        assert mock_logger.error.call_args[0][0].startswith("E-1008 (REQUIRED_SCALE): A required Scale object is missing: x_scale, y_scale")

    def test_missing_range(self):
        p = figure()
        p.x_range = None
        with mock.patch('bokeh.core.validation.check.log') as mock_logger:
            check_integrity([p])
        assert mock_logger.error.call_count == 1
        assert mock_logger.error.call_args[0][0].startswith("E-1004 (REQUIRED_RANGE): A required Range object is missing: x_range")

        p.y_range = None
        with mock.patch('bokeh.core.validation.check.log') as mock_logger:
            check_integrity([p])
        assert mock_logger.error.call_count == 1
        assert mock_logger.error.call_args[0][0].startswith("E-1004 (REQUIRED_RANGE): A required Range object is missing: x_range, y_range")

    def test_bad_extra_range_name(self):
        p = figure()
        p.xaxis.x_range_name="junk"
        with mock.patch('bokeh.core.validation.check.log') as mock_logger:
            check_integrity([p])
        assert mock_logger.error.call_count == 1
        assert mock_logger.error.call_args[0][0].startswith(
            "E-1020 (BAD_EXTRA_RANGE_NAME): An extra range name is configued with a name that does not correspond to any range: x_range_name='junk' [LinearAxis"
        )

        p = figure()
        p.extra_x_ranges['foo'] = Range1d()
        p.grid.x_range_name="junk"
        with mock.patch('bokeh.core.validation.check.log') as mock_logger:
            check_integrity([p])
        assert mock_logger.error.call_count == 1
        assert mock_logger.error.call_args[0][0].startswith(
            "E-1020 (BAD_EXTRA_RANGE_NAME): An extra range name is configued with a name that does not correspond to any range: x_range_name='junk' [Grid"
        )
        assert mock_logger.error.call_args[0][0].count("Grid") == 2

        # test whether adding a figure (*and* it's extra ranges)
        # to another's references doesn't create a false positive
        p, dep = figure(), figure()
        dep.extra_x_ranges['foo'] = Range1d()
        dep.grid.x_range_name="foo"
        p.x_range.callback = CustomJS(code = "", args = {"dep": dep})
        assert dep in p.references()
        with mock.patch('bokeh.core.validation.check.log') as mock_logger:
            check_integrity([p])
        assert mock_logger.error.call_count == 0

def test_plot_add_layout_raises_error_if_not_render():
    plot = figure()
    with pytest.raises(ValueError):
        plot.add_layout(Range1d())

def test_plot_add_layout_adds_label_to_plot_renderers():
    plot = figure()
    label = Label()
    plot.add_layout(label)
    assert label in plot.center

def test_plot_add_layout_adds_axis_to_renderers_and_side_renderers():
    plot = figure()
    axis = LinearAxis()
    plot.add_layout(axis, 'left')
    assert axis in plot.left

def test_sizing_mode_property_is_fixed_by_default():
    plot = figure()
    assert plot.sizing_mode is None

class BaseTwinAxis(object):
    """Base class for testing extra ranges"""

    def verify_axis(self, axis_name):
        plot = Plot()
        range_obj = getattr(plot, 'extra_{}_ranges'.format(axis_name))
        range_obj['foo_range'] = self.get_range_instance()
        assert range_obj['foo_range']

    def test_x_range(self):
        self.verify_axis('x')

    def test_y_range(self):
        self.verify_axis('y')

    @staticmethod
    def get_range_instance():
        raise NotImplementedError

class TestCategoricalTwinAxis(BaseTwinAxis, object):
    """Test whether extra x and y ranges can be categorical"""

    @staticmethod
    def get_range_instance():
        return FactorRange('foo', 'bar')

class TestLinearTwinAxis(BaseTwinAxis, object):
    """Test whether extra x and y ranges can be Range1d"""

    @staticmethod
    def get_range_instance():
        return Range1d(0, 42)

def test_plot_with_no_title_specified_creates_an_empty_title():
    plot = Plot()
    assert plot.title.text == ""

def test_plot__scale_classmethod():
    assert isinstance(Plot._scale("auto"), LinearScale)
    assert isinstance(Plot._scale("linear"), LinearScale)
    assert isinstance(Plot._scale("log"), LogScale)
    assert isinstance(Plot._scale("categorical"), CategoricalScale)
    with pytest.raises(ValueError):
        Plot._scale("malformed_type")

def test__check_required_scale_has_scales():
    plot = Plot()
    check = plot._check_required_scale()
    assert check == []

def test__check_required_scale_missing_scales():
    plot = Plot(x_scale=None, y_scale=None)
    check = plot._check_required_scale()
    assert check != []

def test__check_compatible_scale_and_ranges_compat_numeric():
    plot = Plot(x_scale=LinearScale(), x_range=Range1d())
    check = plot._check_compatible_scale_and_ranges()
    assert check == []

    plot = Plot(y_scale=LogScale(), y_range=DataRange1d())
    check = plot._check_compatible_scale_and_ranges()
    assert check == []

def test__check_compatible_scale_and_ranges_compat_factor():
    plot = Plot(x_scale=CategoricalScale(), x_range=FactorRange())
    check = plot._check_compatible_scale_and_ranges()
    assert check == []

def test__check_compatible_scale_and_ranges_incompat_numeric_scale_and_factor_range():
    plot = Plot(x_scale=LinearScale(), x_range=FactorRange())
    check = plot._check_compatible_scale_and_ranges()
    assert check != []

def test__check_compatible_scale_and_ranges_incompat_factor_scale_and_numeric_range():
    plot = Plot(x_scale=CategoricalScale(), x_range=DataRange1d())
    check = plot._check_compatible_scale_and_ranges()
    assert check != []

#-----------------------------------------------------------------------------
# Dev API
#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# Private API
#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------


from lxml import etree
import requests
import json
import sys
from exjs import ctx

def translate(a):
    tk = ctx.call("TL",a)
    url='https://translate.google.cn/translate_a/single?client=webapp&sl=en&tl=zh-CN&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&otf=1&ssel=0&tsel=0&kc=1&tk={}&q={}'
    url=url.format(tk,a)
    con=requests.get(url)
    t=json.loads(con.text)
    i = 0
    a = ''
    while(t[0][i][0]):
        a += t[0][i][0]
        i += 1
    return a

if __name__=="__main__":

    a = 'loser'
    b = sys.argv[1] if len(sys.argv)>1 else a
    file1 = 'test1.txt'
    file2 = sys.argv[2] if len(sys.argv)>2 else file1
    tra = translate(b)
    with open(file2,'a') as f:
        f.write(tra)
    print(tra)


from flask import Flask
from flask import request
from flask import make_response
import json
import actions

app = Flask(__name__)

def processRequest(req):
    #replace with the entity you have trained for
    action = req.get("result").get("action")
    response_message = ""

    if  action == "get_info":#<YOUR ACTION NAME>:
        entity = req.get("result").get("parameters")#.get("your entity name")
        response_message = actions.get_info(entity)

    return {"speech": response_message,
            "displayText": response_message,
            # "data": data,
            # "contextOut":  [{"name":"company name", "lifespan":2, "parameters":{"city":"Rome"}}],
            "source": "webbot-api"
            }

@app.route('/',methods = ["POST"])
def sitebot_webook_handler():
    req = request.get_json(silent=True, force=True)

    # print("Request:")
    # print(json.dumps(req, indent=4))
    res = processRequest(req)
    res = json.dumps(res, indent=4)
    # print res
    r = make_response(res)
    r.headers['Content-Type'] = 'application/json'
    return r


import functools as ft, itertools as it, operator as op, collections as co, cmath, math, re, traceback, copy

fin = './Short Programming\FranksFlapjacksIn.txt'
fout = './Short Programming\FranksFlapjacksOut.txt'
open(fout, 'w').close()

def splitcases(x):
    return x.strip('\n').split('\n')

# create a dataclass (like a struct in c/c++) to keep track of info about pancakes
from dataclasses import dataclass
@dataclass
class pancake:
    butter: bool
    almond: bool
    choc: bool
    blue: bool
    bacon: bool

# checks if a pancake is consistent with the rules about mixing ingredients
def consistent(p: pancake):
    return (
        all([
            # requirements
            sum([p.almond, p.butter]) == 1,
            sum([p.choc, p.blue, p.bacon]) >= 1,
        ])
        and not any([
            # failure conditions
            p.almond and p.bacon,
            p.blue and p.bacon,
        ])
    )

# generate a list of all possible pancake mixes, then filter out the ones that break the rules
# this is global because it's the same for each case
pancakes = [*filter(consistent,
    [pancake(*args) for args in it.product((0, 1), repeat=5)]
)]

def solve(x, n):
    flour, butter, almond, choc, blue, bacon = map(int, x.split(' '))
    results = []

    # given a list of pancakes that were already made, return a list of pancakes that can still be made
    def options(c):
        nonlocal flour, butter, almond, choc, blue, bacon

        # get remaining amounts of each ingredient
        # if theres no flour, no pancakes can be made
        rflour = flour - len(c)
        if rflour == 0:
            return []

        rbutter = butter - sum([p.butter for p in c])
        ralmond = almond - sum([p.almond for p in c])
        rchoc = choc - sum([p.choc for p in c])
        rblue = blue - sum([p.blue for p in c])
        rbacon = bacon - sum([p.bacon for p in c])

        # filter out any pancakes that can't be made with the remaining ingredients
        options = [*filter(
            lambda p:
                rbutter >= p.butter
                and ralmond >= p.almond
                and rchoc >= p.choc
                and rblue >= p.blue
                and rbacon >= p.bacon,
            pancakes
        )]

        return options

    def brute(c):
        # see what pancakes can be made
        ops = options(c)
        # we are done with the ingredients when we can't make any more pancakes
        if len(ops) == 0:
            nonlocal results
            results.append(c)
        for op in ops:
            brute(c + [op])

    # run the brute force method, initialized with an empty list
    brute([])

    def price(p):
        return (
            p.butter * 2
            + p.almond * 3
            + p.choc
            + p.blue * 2
            + p.bacon * 3
        )

    # for each result of the brute force method, add up the price of all the pancakes. take the max price of any result and return it
    return max([
        sum(map(price, result))
        for result in results
    ])

for num, case in enumerate(splitcases(open(fin).read())):
    print(f'Case {num + 1}:')
    try:
        sol = str(solve(case, num))
        print(sol, '\n')
    except Exception:
        print(f'Input: "{case[:99]}"')
        print(traceback.format_exc())
        sol = ''
    open(fout, 'a').write(sol + '\n')


from django.urls import path

from .views import SelectielijstProcestypenListView, SelectielijstResultatenListView

app_name = "admin-api"

urlpatterns = [
    path(
        "selectielijst/resultaten",
        SelectielijstResultatenListView.as_view(),
        name="selectielijst-resultaten",
    ),
    path(
        "selectielijst/procestypen",
        SelectielijstProcestypenListView.as_view(),
        name="selectielijst-procestypen",
    ),
]


from __future__ import unicode_literals
# vim: tabstop=4 shiftwidth=4 softtabstop=4

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

from builtins import object
from builtins import range

from future import standard_library
standard_library.install_aliases()  # noqa
import requests
from six.moves.urllib import parse as urlparse

class OpenContrailAPIFailed(Exception):
    pass

class Client(object):
    """Opencontrail Base Statistics REST API Client."""

    def __init__(self, analytics_api_ip, analytics_api_port,
                 data={}, analytics_api_ssl_params=None):
        self.data = data
        # As per requirements, IPs can be space or comma seperated
        self.analytics_api_servers = analytics_api_ip.replace(',', ' ').split()
        if len(self.analytics_api_servers) == 0:
            raise IndexError("No analytics API IP provided")
        self.analytics_api_port = analytics_api_port
        self._analytics_ssl_params = analytics_api_ssl_params
        self.index = -1

    def set_analytics_api_ssl_params(self, analytics_api_ssl_params=None):
        self._analytics_ssl_params = analytics_api_ssl_params

    def round_robin(self):
        self.index += 1
        if self.index >= len(self.analytics_api_servers):
            self.index = 0
        return self.index

    def request(self, path, fqdn_uuid, user_token=None,
                data=None):
        req_data = dict(self.data)
        if data:
            req_data.update(data)

        req_params = self._get_req_params(user_token, data=req_data)

        # get the starting index
        analytics_server_index = self.round_robin()

        client_count = len(self.analytics_api_servers)
        # once you have a starting index, we need to traverse through
        # entire list in case of failure.
        last_exc = None
        for index in range(client_count):
            try:
                analytics_ip = self.analytics_api_servers[
                    analytics_server_index]

                resp = None
                if (self._analytics_ssl_params and
                        self._analytics_ssl_params['ssl_enable']):
                    endpoint = "https://%s:%s" % (analytics_ip,
                                                  self.analytics_api_port)
                    certfile = self._analytics_ssl_params['certfile']
                    keyfile = self._analytics_ssl_params['keyfile']
                    certificate = (certfile, keyfile)
                    ca_cert = self._analytics_ssl_params['ca_cert']
                    url = urlparse.urljoin(endpoint, path + fqdn_uuid)
                    if (self._analytics_ssl_params['insecure_enable']):
                        resp = requests.get(url, cert=certificate,
                                            verify=False, **req_params)
                    else:
                        resp = requests.get(url, cert=certificate,
                                            verify=ca_cert,
                                            **req_params)
                else:
                    endpoint = "http://%s:%s" % (analytics_ip,
                                                 self.analytics_api_port)
                    url = urlparse.urljoin(endpoint, path + fqdn_uuid)
                    resp = requests.get(url, **req_params)
                if resp.status_code != 200:
                    raise OpenContrailAPIFailed(
                        ('Opencontrail API returned %(status)s %(reason)s') %
                        {'status': resp.status_code, 'reason': resp.reason})
                return resp.json()

            except Exception as e:
                # In case of failure, continue till we check all other
                # available servers
                last_exc = e
                analytics_server_index = self.round_robin()
                continue

        # If we reach here, raise the last encountered exception
        if last_exc:
            raise last_exc
        else:
            raise Exception("Analytics servers are not accessible.")

    def _get_req_params(self, user_token, data=None):
        req_params = {
            'headers': {
                'Accept': 'application/json'
            },
            'data': data,
            'allow_redirects': False,
        }
        if user_token:
            req_params['headers']['X-AUTH-TOKEN'] = user_token

        return req_params


from flask import render_template, url_for, flash, redirect, request, abort, jsonify
from files import app, db
from files.models.mission import Mission
import datetime as dt

@app.route("/missions", methods=["GET"])
def get_missions():
    missions = Mission.query.all()

    return jsonify(f"{missions}"), 200

@app.route("/missions/<string:id>/crew", methods=["GET"])
def get_mission_id_crew(id):
    missions = Mission.query.get(id)

    return jsonify(f"{missions.return_crew()}"), 200

@app.route("/missions/add", methods=['POST'])
def missions_add():
    missions = Mission.query.order_by(Mission.id.desc()).first()

    m = Mission()
    m.start_date = missions.end_date

    timedelta = dt.timedelta(weeks=8)
    m.end_date = m.start_date + timedelta

    db.session.add(m)
    db.session.commit()

    return jsonify(m.output()), 201