#!/usr/bin/python

import os ; import numpy as np ; import datetime as dt ; from dateutil import tz, rrule ;
#import matplotlib.pyplot as plt; #import seaborn as sns ;

date_list=[x.strftime('%Y%m%d') for x in rrule.rrule(rrule.DAILY,dtstart=dt.datetime.strptime('20170601','%Y%m%d'),until=dt.datetime.strptime('20170630','%Y%m%d'))]
l_idx_s=6; l_idx_e=30 ;

for dte in date_list[:]:

    main='/home/vkvalappil/Data/' ; date=dte+'06' ;
    output=main+'modelWRF/ARW/output/gsi_plots/rad_sens/amsua_iasi_J/' +str(l_idx_s)+'_'+str(l_idx_e)+'hour/'+date+'/'
    if not os.path.exists(output)   :
        os.makedirs(output)

    gsiFile_1=main+'modelWRF/ARW/output/GFS_ENKF/gsi_opp/newBe/gsi_radiance_sensitivity/amsua_J/hourly'+date+'_d02_amsua_J.csv'
    gsiFile_2=main+'modelWRF/ARW/output/GFS_ENKF/gsi_opp/newBe/gsi_radiance_sensitivity/iasi_J/hourly'+date+'_d02_iasi_J.csv'
    gsiFile_3=main+'modelWRF/ARW/output/GFS_ENKF/gsi_opp/newBe/gsi_radiance_sensitivity/amsua+iasi_J/hourly'+date+'_d02_amsua+iasi_J.csv'

    #gsiFile_3=main+'modelWRF/ARW/output/GFS_ENKF/gsi_opp/newBe/gsi_radiance_sensitivity/'+date+'/hourly'+date+'_d02_iasi.csv'
    #gsiFile_5=main+'modelWRF/ARW/output/GFS_ENKF/gsi_opp/newBe/gsi_radiance_sensitivity/'+date+'/hourly'+date+'_d02_seviri.csv'
    #gsiFile_4=main+'modelWRF/ARW/output/GFS_ENKF/gsi_opp/newBe/gsi_radiance_sensitivity/'+date+'/hourly'+date+'_d02_amsua+iasi.csv'
    #gsiFile_7=main+'modelWRF/ARW/output/GFS_ENKF/gsi_opp/newBe/gsi_radiance_sensitivity/'+date+'/hourly'+date+'_d02_amsua+hirs.csv'
#   gsiFile_8=main+'modelWRF/ARW/output/GFS_ENKF/gsi_opp/newBe/gsi_radiance_sensitivity/'+date+'/hourly'+date+'_d02_amsua+mhs.csv'

    #gsiFile=main+'modelWRF/oppModel/ARW/output/hourly'+date+'_d02.csv'

    nogFile=main+'modelWRF_2/ARW/output/no_gsi_0.25/hourly'+date+'_d02.csv'

    lstFile=main+'modelWRF/scripts/master_uae.csv'

    lst_f=np.genfromtxt(lstFile,delimiter=',',dtype='S')
    g_data_1=np.genfromtxt(gsiFile_1,delimiter=',',dtype='S') ;
    g_data_2=np.genfromtxt(gsiFile_2,delimiter=',',dtype='S') ;
    g_data_3=np.genfromtxt(gsiFile_3,delimiter=',',dtype='S') ;

    #g_data_4=np.genfromtxt(gsiFile_4,delimiter=',',dtype='S') ;
    #g_data_5=np.genfromtxt(gsiFile_5,delimiter=',',dtype='S') ;
    #g_data_6=np.genfromtxt(gsiFile_6,delimiter=',',dtype='S') ;
    #g_data_7=np.genfromtxt(gsiFile_7,delimiter=',',dtype='S') ;
    #g_data_8=np.genfromtxt(gsiFile_8,delimiter=',',dtype='S') ;

    ng_data=np.genfromtxt(nogFile,delimiter=',',dtype='S')

    temp_cons_bias=np.empty((0,5)) ; rh_cons_bias=np.empty((0,5)) ;
    dt_cons_bias=np.empty((0,5)) ; mr_cons_bias=np.empty((0,5)) ;
    temp_cons_mae=np.empty((0,5)) ; temp_cons_rmse=np.empty((0,5)) ;
    rh_cons_mae=np.empty((0,5)) ; rh_cons_rmse=np.empty((0,5)) ;
    dt_cons_mae=np.empty((0,5)) ; dt_cons_rmse=np.empty((0,5)) ;
    mr_cons_mae=np.empty((0,5)) ; mr_cons_rmse=np.empty((0,5)) ;

    for ii in range(0,lst_f.shape[0]) :
        tid=lst_f[ii,0] ; locNme=lst_f[ii,-1]  ;
        g_req_data_1=g_data_1[np.where(g_data_1[:,0]==tid)[0],:]
        g_req_data_2=g_data_2[np.where(g_data_2[:,0]==tid)[0],:]
        g_req_data_3=g_data_3[np.where(g_data_3[:,0]==tid)[0],:]

    #    g_req_data_4=g_data_4[np.where(g_data_4[:,0]==tid)[0],:]
    #    g_req_data_5=g_data_5[np.where(g_data_5[:,0]==tid)[0],:]
    #    g_req_data_6=g_data_6[np.where(g_data_6[:,0]==tid)[0],:]
    #    g_req_data_7=g_data_7[np.where(g_data_7[:,0]==tid)[0],:]
    #    g_req_data_8=g_data_8[np.where(g_data_8[:,0]==tid)[0],:]

        ng_req_data=ng_data[np.where(ng_data[:,0]==tid)[0],:]

        f_date=g_req_data_1[:,3][::24] ; date_1=dt.datetime.strptime(f_date[0],'%Y-%m-%d %H:%M:%S').strftime('%Y%m%d')
        date_2=dt.datetime.strptime(f_date[1],'%Y-%m-%d %H:%M:%S').strftime('%Y%m%d') ;
        date_3=dt.datetime.strptime(f_date[2],'%Y-%m-%d %H:%M:%S').strftime('%Y%m%d');

        rs_file_1='/home/vkvalappil/Data/metarData/'+date_1[0:6]+'/'+locNme+'_'+date_1+'.csv'
        rs_file_2='/home/vkvalappil/Data/metarData/'+date_2[0:6]+'/'+locNme+'_'+date_2+'.csv'
        rs_file_3='/home/vkvalappil/Data/metarData/'+date_3[0:6]+'/'+locNme+'_'+date_3+'.csv'

        rs_data_1=np.genfromtxt(rs_file_1,delimiter=',',dtype='S')
        rs_data_2=np.genfromtxt(rs_file_2,delimiter=',',dtype='S')
        rs_data_3=np.genfromtxt(rs_file_3,delimiter=',',dtype='S')

        rs_data=np.concatenate([rs_data_1[1:,:],rs_data_2[1:,:],rs_data_3[1:,:]],axis=0)

        from_zone = tz.gettz('Asia/Dubai') ; to_zone = tz.gettz('UTC') ;
        date_list=np.array([(dt.datetime.strptime(x,'%Y-%m-%d %H:%M:%S')).replace(tzinfo=from_zone).astimezone(to_zone).strftime('%Y-%m-%d %H:%M') for x in g_req_data_1[:,3]]);
        rs_req_data=rs_data[np.where(np.in1d(rs_data[:,1],date_list))[0],:]

##################################################Temperature ##########################################################################################

        data_1=g_req_data_1[l_idx_s:l_idx_e,5].astype(float) ;
        data_2=g_req_data_2[l_idx_s:l_idx_e,5].astype(float) ;
        data_3=g_req_data_3[l_idx_s:l_idx_e,5].astype(float) ;

    #    data_4=g_req_data_4[l_idx_s:l_idx_e,5].astype(float) ;
    #    data_5=g_req_data_5[l_idx_s:l_idx_e,5].astype(float) ;
    #    data_6=g_req_data_6[l_idx_s:l_idx_e,5].astype(float) ;
    #    data_7=g_req_data_7[l_idx_s:l_idx_e,5].astype(float) ;
    #    data_8=g_req_data_8[l_idx_s:l_idx_e,5].astype(float) ;

        data_ng=ng_req_data[l_idx_s:l_idx_e,5].astype(float) ; obs=rs_req_data[l_idx_s:l_idx_e,4].astype(float)

        temp_bias_1=(data_1-obs).mean() ;
        temp_bias_2=(data_2-obs).mean() ;
        temp_bias_3=(data_3-obs).mean() ;
        temp_bias_ng=(data_ng-obs).mean() ;

        temp_mae_1=np.abs(data_1-obs).mean() ;
        temp_mae_2=np.abs(data_2-obs).mean() ;
        temp_mae_3=np.abs(data_3-obs).mean() ;

    #    temp_mae_4=np.abs(data_4-obs).mean() ;
        temp_mae_ng=np.abs(data_ng-obs).mean() ;

        temp_rmse_1=np.sqrt(np.square(data_1-obs).mean()) ;
        temp_rmse_2=np.sqrt(np.square(data_2-obs).mean()) ;
        temp_rmse_3=np.sqrt(np.square(data_3-obs).mean()) ;

    #    temp_rmse_4=np.sqrt(np.square(data_4-obs).mean()) ;
        temp_rmse_ng=np.sqrt(np.square(data_ng-obs).mean()) ;

        temp_bias=np.vstack([locNme,temp_bias_1,temp_bias_2,temp_bias_3,temp_bias_ng]).T
        temp_mae=np.vstack([locNme,temp_mae_1,temp_mae_2,temp_mae_3,temp_mae_ng]).T
        temp_rmse=np.vstack([locNme,temp_rmse_1,temp_rmse_2,temp_rmse_3,temp_rmse_ng]).T

        temp_cons_bias=np.concatenate([temp_cons_bias,temp_bias],axis=0)
        temp_cons_mae=np.concatenate([temp_cons_mae,temp_mae],axis=0)
        temp_cons_rmse=np.concatenate([temp_cons_rmse,temp_rmse],axis=0)

##########################################Relative Hum #############################################################

        data_1=g_req_data_1[l_idx_s:l_idx_e,7].astype(float) ;
        data_2=g_req_data_2[l_idx_s:l_idx_e,7].astype(float) ;
        data_3=g_req_data_3[l_idx_s:l_idx_e,7].astype(float) ;

    #    data_4=g_req_data_4[l_idx_s:l_idx_e,7].astype(float) ;
    #    data_5=g_req_data_5[l_idx_s:l_idx_e,7].astype(float) ;
    #    data_6=g_req_data_6[l_idx_s:l_idx_e,7].astype(float) ;
    #    data_7=g_req_data_7[l_idx_s:l_idx_e,7].astype(float) ;
    #    data_8=g_req_data_8[l_idx_s:l_idx_e,7].astype(float) ;

        data_ng=ng_req_data[l_idx_s:l_idx_e,7].astype(float) ; obs=np.round(rs_req_data[l_idx_s:l_idx_e,6].astype(float))

        rh_bias_1=(data_1-obs).mean() ;
        rh_bias_2=(data_2-obs).mean() ;
        rh_bias_3=(data_3-obs).mean() ;
        rh_bias_ng=(data_ng-obs).mean()

        rh_mae_1=np.abs(data_1-obs).mean() ;
        rh_mae_2=np.abs(data_2-obs).mean() ;
        rh_mae_3=np.abs(data_3-obs).mean() ;

    #    rh_mae_4=np.abs(data_4-obs).mean() ;
        rh_mae_ng=np.abs(data_ng-obs).mean() ;

        rh_rmse_1=np.sqrt(np.square(data_1-obs).mean()) ;
        rh_rmse_2=np.sqrt(np.square(data_2-obs).mean()) ;
        rh_rmse_3=np.sqrt(np.square(data_3-obs).mean()) ;

    #    rh_rmse_4=np.sqrt(np.square(data_4-obs).mean()) ;
        rh_rmse_ng=np.sqrt(np.square(data_ng-obs).mean()) ;

        rh_bias=np.vstack([locNme,rh_bias_1,rh_bias_2,rh_bias_3,rh_bias_ng]).T
        rh_mae=np.vstack([locNme,rh_mae_1,rh_mae_2,rh_mae_3,rh_mae_ng]).T
        rh_rmse=np.vstack([locNme,rh_rmse_1,rh_rmse_2,rh_rmse_3,rh_rmse_ng]).T

        rh_cons_mae=np.concatenate([rh_cons_mae,rh_mae],axis=0)
        rh_cons_rmse=np.concatenate([rh_cons_rmse,rh_rmse],axis=0)
        rh_cons_bias=np.concatenate([rh_cons_bias,rh_bias],axis=0)

###################################################Dew temp ######################################################

        data_1=g_req_data_1[l_idx_s:l_idx_e,6].astype(float) ;
        data_2=g_req_data_2[l_idx_s:l_idx_e,6].astype(float) ;
        data_3=g_req_data_3[l_idx_s:l_idx_e,6].astype(float) ;

    #    data_4=g_req_data_4[l_idx_s:l_idx_e,6].astype(float) ;
    #    data_5=g_req_data_5[l_idx_s:l_idx_e,6].astype(float) ;
    #    data_6=g_req_data_6[l_idx_s:l_idx_e,6].astype(float) ;
    #    data_7=g_req_data_7[l_idx_s:l_idx_e,6].astype(float) ;
    #    data_8=g_req_data_8[l_idx_s:l_idx_e,6].astype(float) ;

        data_ng=ng_req_data[l_idx_s:l_idx_e,6].astype(float) ; obs=np.round(rs_req_data[l_idx_s:l_idx_e,5].astype(float))

        dt_bias_1=(data_1-obs).mean() ;
        dt_bias_2=(data_2-obs).mean() ;
        dt_bias_3=(data_3-obs).mean() ;
        dt_bias_ng=(data_ng-obs).mean() ;

        dt_mae_1=np.abs(data_1-obs).mean() ;
        dt_mae_2=np.abs(data_2-obs).mean() ;
        dt_mae_3=np.abs(data_3-obs).mean() ;

#       dt_mae_4=np.abs(data_4-obs).mean() ;
        dt_mae_ng=np.abs(data_ng-obs).mean() ;

        dt_rmse_1=np.sqrt(np.square(data_1-obs).mean()) ;
        dt_rmse_2=np.sqrt(np.square(data_2-obs).mean()) ;
        dt_rmse_3=np.sqrt(np.square(data_3-obs).mean()) ;

#    dt_rmse_4=np.sqrt(np.square(data_4-obs).mean()) ;
        dt_rmse_ng=np.sqrt(np.square(data_ng-obs).mean()) ;

        dt_bias=np.vstack([locNme,dt_bias_1,dt_bias_2,dt_bias_3,dt_bias_ng]).T
        dt_mae=np.vstack([locNme,dt_mae_1,dt_mae_2,dt_mae_3,dt_mae_ng]).T
        dt_rmse=np.vstack([locNme,dt_rmse_1,dt_rmse_2,dt_rmse_3,dt_rmse_ng]).T

        dt_cons_mae=np.concatenate([dt_cons_mae,dt_mae],axis=0)
        dt_cons_rmse=np.concatenate([dt_cons_rmse,dt_rmse],axis=0)
        dt_cons_bias=np.concatenate([dt_cons_bias,dt_bias],axis=0)

############################################### Mix ratio #######################################################

        data_1=g_req_data_1[l_idx_s:l_idx_e,10].astype(float) ;
        data_2=g_req_data_2[l_idx_s:l_idx_e,10].astype(float) ;
        data_3=g_req_data_3[l_idx_s:l_idx_e,10].astype(float) ;

    #    data_4=g_req_data_4[l_idx_s:l_idx_e,10].astype(float) ;
    #    data_5=g_req_data_5[l_idx_s:l_idx_e,10].astype(float) ;
    #    data_6=g_req_data_6[l_idx_s:l_idx_e,10].astype(float)  ;
    #    data_7=g_req_data_7[l_idx_s:l_idx_e,10].astype(float)  ;
    #    data_8=g_req_data_8[l_idx_s:l_idx_e,10].astype(float)  ;
    #

        data_ng=ng_req_data[l_idx_s:l_idx_e,10].astype(float) ; obs=rs_req_data[l_idx_s:l_idx_e,11].astype(float)

        mr_bias_1=np.abs(data_1-obs).mean() ;
        mr_bias_2=np.abs(data_2-obs).mean() ;
        mr_bias_3=np.abs(data_3-obs).mean() ;
        mr_bias_ng=np.abs(data_ng-obs).mean() ;

        mr_mae_1=np.abs(data_1-obs).mean() ;
        mr_mae_2=np.abs(data_2-obs).mean() ;
        mr_mae_3=np.abs(data_3-obs).mean() ;

    #    mr_mae_4=np.abs(data_4-obs).mean() ;
        mr_mae_ng=np.abs(data_ng-obs).mean() ;

        mr_rmse_1=np.sqrt(np.square(data_1-obs).mean()) ;
        mr_rmse_2=np.sqrt(np.square(data_2-obs).mean()) ;
        mr_rmse_3=np.sqrt(np.square(data_3-obs).mean()) ;

    #    mr_rmse_4=np.sqrt(np.square(data_4-obs).mean()) ;
        mr_rmse_ng=np.sqrt(np.square(data_ng-obs).mean()) ;

        mr_bias=np.vstack([locNme,mr_bias_1,mr_bias_2,mr_bias_3,mr_bias_ng]).T
        mr_mae=np.vstack([locNme,mr_mae_1,mr_mae_2,mr_mae_3,mr_mae_ng]).T
        mr_rmse=np.vstack([locNme,mr_rmse_1,mr_rmse_2,mr_rmse_3,mr_rmse_ng]).T

        mr_cons_mae=np.concatenate([mr_cons_mae,mr_mae],axis=0)
        mr_cons_rmse=np.concatenate([mr_cons_rmse,mr_rmse],axis=0)
        mr_cons_bias=np.concatenate([mr_cons_bias,mr_bias],axis=0)

    header='locname,amsua,iasi,iasi+amsua,noDa' ;
    header=np.vstack(np.array(header.split(","))).T

    temp_cons_bias=np.concatenate([header,temp_cons_bias],axis=0)
    rh_cons_bias=np.concatenate([header,rh_cons_bias],axis=0)
    dt_cons_bias=np.concatenate([header,dt_cons_bias],axis=0)
    mr_cons_bias=np.concatenate([header,mr_cons_bias],axis=0)

    temp_cons_mae=np.concatenate([header,temp_cons_mae],axis=0)
    rh_cons_mae=np.concatenate([header,rh_cons_mae],axis=0)
    dt_cons_mae=np.concatenate([header,dt_cons_mae],axis=0)
    mr_cons_mae=np.concatenate([header,mr_cons_mae],axis=0)

    temp_cons_rmse=np.concatenate([header,temp_cons_rmse],axis=0)
    rh_cons_rmse=np.concatenate([header,rh_cons_rmse],axis=0)
    dt_cons_rmse=np.concatenate([header,dt_cons_rmse],axis=0)
    mr_cons_rmse=np.concatenate([header,mr_cons_rmse],axis=0)

    outFile_mr_bias=output+'/'+str(l_idx_s)+'_'+str(l_idx_e)+'bias_mixr_2m_'+date+'.csv'
    outFile_dt_bias=output+'/'+str(l_idx_s)+'_'+str(l_idx_e)+'bias_dewtemp_2m_'+date+'.csv'
    outFile_rh_bias=output+'/'+str(l_idx_s)+'_'+str(l_idx_e)+'bias_rh_2m_'+date+'.csv'
    outFile_temp_bias=output+'/'+str(l_idx_s)+'_'+str(l_idx_e)+'bias_temperature_2m_'+date+'.csv'

    outFile_mr_mae=output+'/'+str(l_idx_s)+'_'+str(l_idx_e)+'mae_mixr_2m_'+date+'.csv'
    outFile_dt_mae=output+'/'+str(l_idx_s)+'_'+str(l_idx_e)+'mae_dewtemp_2m_'+date+'.csv'
    outFile_rh_mae=output+'/'+str(l_idx_s)+'_'+str(l_idx_e)+'mae_rh_2m_'+date+'.csv'
    outFile_temp_mae=output+'/'+str(l_idx_s)+'_'+str(l_idx_e)+'mae_temperature_2m_'+date+'.csv'

    outFile_mr_rmse=output+'/'+str(l_idx_s)+'_'+str(l_idx_e)+'rmse_mixr_2m_'+date+'.csv'
    outFile_dt_rmse=output+'/'+str(l_idx_s)+'_'+str(l_idx_e)+'rmse_dewtemp_2m_'+date+'.csv'
    outFile_rh_rmse=output+'/'+str(l_idx_s)+'_'+str(l_idx_e)+'rmse_rh_2m_'+date+'.csv'
    outFile_temp_rmse=output+'/'+str(l_idx_s)+'_'+str(l_idx_e)+'rmse_temperature_2m_'+date+'.csv'

    np.savetxt(outFile_temp_bias,temp_cons_bias,delimiter=',',fmt='%s')
    np.savetxt(outFile_rh_bias,rh_cons_bias,delimiter=',',fmt='%s')
    np.savetxt(outFile_dt_bias,dt_cons_bias,delimiter=',',fmt='%s')
    np.savetxt(outFile_mr_bias,mr_cons_bias,delimiter=',',fmt='%s')

    np.savetxt(outFile_temp_mae,temp_cons_mae,delimiter=',',fmt='%s')
    np.savetxt(outFile_rh_mae,rh_cons_mae,delimiter=',',fmt='%s')
    np.savetxt(outFile_dt_mae,dt_cons_mae,delimiter=',',fmt='%s')
    np.savetxt(outFile_mr_mae,mr_cons_mae,delimiter=',',fmt='%s')

    np.savetxt(outFile_temp_rmse,temp_cons_rmse,delimiter=',',fmt='%s')
    np.savetxt(outFile_rh_rmse,rh_cons_rmse,delimiter=',',fmt='%s')
    np.savetxt(outFile_dt_rmse,dt_cons_rmse,delimiter=',',fmt='%s')
    np.savetxt(outFile_mr_rmse,mr_cons_rmse,delimiter=',',fmt='%s')

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


import cv2
import numpy as np
import os
from random import shuffle
import tensorflow as tf
import matplotlib.pyplot as plt
import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
from tqdm import tqdm
from pathlib import Path
import models

TRAINDIR='train/train'
TESTDIR='test1/test1'
PRACTDIR='pract'
IMG_SIZE= 50
LR=1e-3

def predict_clf(target_path,model):
    target_img=cv2.imread(target_path)
    target= process_img(target_path)
    np_test=np.array(target).reshape(IMG_SIZE, IMG_SIZE, 1)
    ans=model.predict([np_test])
    sc=ans
    ans=np.round(ans)
    print(sc[0])
    if(np.all(ans==[1,0])):
        return 'cat',str(sc[0]*100)[1:6]
    else:
        return 'dog',str(sc[0]*100)[1:6]

def process_img(path):
    img_data=cv2.imread(path,cv2.IMREAD_GRAYSCALE)
    img_data=cv2.resize(img_data,(IMG_SIZE,IMG_SIZE))
    return np.array(img_data)

def create_label(img_name):
    #creating one-hot encoded vector from imgname
    word_label=img_name.split('.')[0]
    if word_label == 'cat':
        return np.array([1,0])
    elif word_label == 'dog':
        return np.array([0,1])

def model_evaluate(model,test_it_x,test_it_y):
    test_it_y=np.array(test_it_y)
    score = model.evaluate(test_it_x, test_it_y)
    return score

def run(targname=''):

    MODEL_NAME='default.tfl'
    MODEL_DIR='models'

    #tf-model
    tf.reset_default_graph()
    model=models.catDefaultModel(IMG_SIZE,LR)

    if(os.path.exists( os.path.join( MODEL_DIR,'{}.meta'.format(MODEL_NAME)))):
        model.load(os.path.join( MODEL_DIR,MODEL_NAME))

    if not targname=='':
        target_path=targname
        return predict_clf(target_path,model)
        #print(model_evaluate(model,X_test,y_test))

run()


from pip._vendor import colorama
from sklearn import preprocessing,svm
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression

import matplotlib.pyplot as plt
from matplotlib import style
import pandas as pd

import numpy as np
import csv

import pickle

x_dates = []
y_prices = []

def load_model(model_name):
    pickle_in = open(model_name,'rb')
    clf = pickle.load(pickle_in )
    return clf

def load_file_graph(file_name):
    with open(file_name, 'r') as csv_file:
        csv_file_reader = csv.reader(csv_file)
        next(csv_file_reader)
        for row in csv_file_reader:
            x_dates.append(int(row[0].split('-')[0]))
            y_prices.append(float(row[1]))

def estimate_coefficients(x,y):

    x_size = np.size(x)
    mean_x, mean_y = np.mean(x), np.mean(y)

    SS_xy = np.sum((y*x) - (x_size*mean_y*mean_x))
    SS_xx = np.sum((x*x) - (x_size*mean_x*mean_x))

    b_1 = SS_xy / SS_xx
    b_0 = mean_y - (b_1*mean_x)
    return (b_0,b_1)

def plot_regression_line(x,y,coefficient_b):

    #plot the pois ate the graph
    plt.scatter(x,y,marker="o",s=30,color="m")

    #predict de responce vector
    y_pred = coefficient_b[0]+ (coefficient_b[1]*x)

    #plotting the regretion line
    plt.plot(x,y_pred,color="g")

    #labels
    plt.xlabel("year")
    plt.ylabel("price")

    plt.show()

load_file_graph('gold_price.csv')

clf = load_model('linearRegre.pickle')

prediction = clf.predict([[2021],[2022],[2023],[2044]] )
print("predict values_________________________")
print(prediction)

x_dates = np.array(x_dates)
y_prices = np.array(y_prices)

b =  estimate_coefficients(x_dates,y_prices)
plot_regression_line(x_dates,y_prices,b)


import os
import cv2
import numpy as np
from sklearn.model_selection import train_test_split
from tensorflow import keras
from tensorflow.keras import layers
import pandas as pd
from sklearn.linear_model import LogisticRegression

# Function to load the FER2013 dataset
def load_fer2013_dataset(dataset_dir):
    X = []  # List to store image data
    y = []  # List to store corresponding labels

    # Define emotion labels based on FER2013 dataset
    emotion_labels = ["angry", "disgust", "fear", "happy", "sad", "surprise", "neutral"]

    for emotion_label in emotion_labels:
        # Construct the path to the folder containing images of the current emotion
        emotion_dir = os.path.join(dataset_dir, emotion_label)

        # Loop through images in the folder
        for image_filename in os.listdir(emotion_dir):
            # Load and preprocess the image
            image_path = os.path.join(emotion_dir, image_filename)
            image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)  # Read image in grayscale
            image = cv2.resize(image, (48, 48))  # Resize to a common size (adjust as needed)

            # Append the image data and corresponding label
            X.append(image)
            y.append(emotion_labels.index(emotion_label))

    # Convert the lists to NumPy arrays
    X = np.array(X)
    y = np.array(y)

    return X, y

# Define the path to your FER2013 dataset folder
dataset_dir = "C:/Users/91950/Downloads/app (1)/train"

# Load the dataset
X, y = load_fer2013_dataset(dataset_dir)

# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Normalize pixel values (typically scaled between 0 and 1)
X_train = X_train / 255.0
X_test = X_test / 255.0

# Define your CNN model
model = keras.Sequential([
    layers.Conv2D(32, (3, 3), activation='relu', input_shape=(48, 48, 1)),
    layers.MaxPooling2D((2, 2)),
    layers.Conv2D(64, (3, 3), activation='relu'),
    layers.MaxPooling2D((2, 2)),
    layers.Conv2D(128, (3, 3), activation='relu'),
    layers.MaxPooling2D((2, 2)),
    layers.Flatten(),
    layers.Dense(256, activation='relu'),
    layers.Dropout(0.5),
    layers.Dense(7, activation='softmax')  # 7 emotions
])

# Compile the model
model.compile(optimizer=keras.optimizers.Adam(learning_rate=0.0001),
              loss='sparse_categorical_crossentropy',  # Since y is not one-hot encoded
              metrics=['accuracy'])

# Train the model
model.fit(X_train, y_train, epochs=5, batch_size=32, validation_data=(X_test, y_test))

# Initialize the camera
cap = cv2.VideoCapture(0)  # Use the default camera (you can specify a different camera if needed)

# Define emotion labels for displaying predictions
emotion_labels = ["Angry", "Disgust", "Fear", "Happy", "Sad", "Surprise", "Neutral"]

# Read the music recommendations from the CSV file
music_recommendations_csv = "C:/Users/91950/Downloads/app (1)/mu.xlsx"  # Adjust the filename and path as needed
music_recommendations_df = pd.read_excel(music_recommendations_csv)

# Initialize a logistic regression model
logistic_regression_model = LogisticRegression()
'''def apply_cartoon_filter(frame):
    # Convert the frame to grayscale
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Apply Gaussian blur to the grayscale image
    gray = cv2.medianBlur(gray, 5)

    # Detect edges using adaptive thresholding
    edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 9)

    # Apply a bilateral filter to smooth the image while preserving edges
    color = cv2.bilateralFilter(frame, 9, 300, 300)

    # Combine the color image and edges using bitwise and
    cartoon = cv2.bitwise_and(color, color, mask=edges)

    return cartoon'''
while True:
    # Capture a frame from the camera
    ret, frame = cap.read()
    '''if not ret:
        print("Error: Could not read frame")
        continue'''
    #cartoon_frame = apply_cartoon_filter(frame)
    # Preprocess the frame (resize and convert to grayscale)
    frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    frame_gray = cv2.resize(frame_gray, (48, 48))

    # Normalize pixel values (same as training data)
    frame_gray = frame_gray / 255.0

    # Make a prediction using the CNN model
    emotion_id = np.argmax(model.predict(np.expand_dims(frame_gray, axis=0)))
    predicted_emotion = emotion_labels[emotion_id]

    # Perform binary classification using logistic regression
    # Define a binary outcome based on your criteria (e.g., "Positive" or "Negative")
    # Modify this logic based on your criteria
    binary_outcome = 0  # Default to "Negative" (e.g., ["Fear", "Angry", "Sad"])
    if predicted_emotion in ["Happy", "Surprise", "Disgust", "Neutral"]:
        binary_outcome = 1  # Set to "Positive"

    # Display both the emotion and binary outcome on the frame
    cv2.putText(frame, f"Emotion: {predicted_emotion}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
    cv2.putText(frame, f"Binary Outcome: {binary_outcome}", (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)

    # Find music recommendations based on the detected emotion
    recommendations_for_emotion = music_recommendations_df[predicted_emotion].dropna().tolist()

    print(f"Top 3 Music Recommendations for {predicted_emotion}:")
    for i, recommendation in enumerate(recommendations_for_emotion[:3]):
        print(f"{i+1}. {recommendation}")

    # Display the frame
    cv2.imshow('Emotion Recognition', frame)

    # Exit the loop when 'q' is pressed
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release the camera and close the OpenCV windows
cap.release()
cv2.destroyAllWindows()


#!/usr/bin/env python

import logging
import os
import pygit2
import sys

AUDIT_OK = 0
AUDIT_FAILED = 1
ERROR = 2

issues = {}

def record_issue(issue, more_data):
    if issue in issues:
        if more_data not in issues[issue]:
            issues[issue].append(more_data)
    else:
        issues[issue] = [more_data]

def check_can_merge_to_this_branch(email):
    pass

def track_back_to_parents(this_commit, excluded_author,
                          all_baselines, depth=1):
    logging.debug("Checking feature branch %s by %s" % (
        this_commit.id,
        this_commit.author.email))
    if depth > 20:
        logging.info("Max depth exceeded while looking for the parents of %r" %
                     this_commit)
        sys.exit(ERROR)
    logging.debug("Looking for parents of %r" % this_commit.message)
    if this_commit.id in all_baselines:
        logging.debug("Tracked back to base; nothing futher to do")
        return None
    if this_commit.author.email == excluded_author:
        logging.info(("This feature branch contains commit %s, authored by " +
                      " %s, who performed the merge.") %
                     (this_commit.id, this_commit.author.email))
        record_issue("Feature branch merged by one of its contributors",
                     this_commit.id)
        return None
    for p in this_commit.parents:
        return track_back_to_parents(p, excluded_author, all_baselines,
                                     depth + 1)

def print_commit(commit):
    return "%s %s" % (commit.id, commit.message)

def find_baselines(commit):
    """ Finds all the previous revisions of the branch this
    commit is now the head of. """
    baselines = []
    while True:
        if len(commit.parents) > 1:
            baselines.append(commit.id)
            commit = commit.parents[0]
        elif len(commit.parents) == 0:
            logging.debug("Reached the end of the branch")
            return baselines
        else:
            record_issue("Non-merge commit on trunk", commit.id)
            baselines.append(commit.id)
            commit = commit.parents[0]

def check_merges_by_branch_authors(repo):
    head = repo.head

    # Is it a merge commit? Can we get the parents?
    commit = head.peel(pygit2.Commit)

    all_baselines = find_baselines(commit)

    while True:
        merge_author = commit.author.email
        parents = commit.parents

        if len(parents) == 0:
            # We've reached the end of the repository
            break

        if len(parents) < 2:
            record_issue("Non-merge commit on trunk", commit.id)
            commit = parents[0]
            continue

        check_can_merge_to_this_branch(merge_author)

        # ASSUME (!) the first parent was the previous
        # position of the branch...
        feature_branches = parents[1:]
        logging.debug("Base of this merge is %s" % (parents[0].id))
        for p in feature_branches:
            ident = track_back_to_parents(p, merge_author, all_baselines)
        commit = parents[0]

def get_mergers_for_commit(repo, commit_id):
    """ This reads the ROLES file at the time of a given commit and returns a
        list of the people allowed to merge each branch. The return value is a
        dictionary whose keys are branch names and values are lists of email
        addresses. """
    commit = repo[commit_id]
    try:
        permissions_file_id = commit.tree['ROLES'].id
    except KeyError:
        record_issue("Repository has no ROLES file", None)
        return {}
    permissions_file = repo[permissions_file_id]
    perms = permissions_file.data.split("\n")
    mergers = {}
    for l in perms:
        if l == "":
            continue
        fields = l.strip().split(":")
        branch = fields[0]
        users = fields[1].split(",")
        if users is not None:
            mergers[branch] = users
        logging.info("Users %s are allowed to merge to %s" % (users, branch))
    return mergers

def check_merge_permissions(repo):
    """ Checks the ROLES file for each baseline - at the time of the previous
        baseline - allows that merge. """

    head_commit = repo.head.peel(pygit2.Commit)
    all_baselines = find_baselines(head_commit)
    checked_branch = "master"

    # This requires that baselines are in order (most recent to oldest)
    for baseline_no in range(0, len(all_baselines)-1):
        b = all_baselines[baseline_no]
        rules_baseline = all_baselines[baseline_no+1]
        mergers = get_mergers_for_commit(repo, rules_baseline)
        if checked_branch not in mergers:
            # If there are no rules for a branch, that's considered
            # OK, so there's nothing to check at this point (although
            # this might warrant a low-level warning)
            continue
        commit = repo.get(b)
        mergers_for_branch = mergers[checked_branch]
        if commit.author.email not in mergers_for_branch:
            record_issue("Unauthorised user committed to branch",
                         "%s committed to %s at %s" %
                         (commit.author.email, checked_branch, b))
        else:
            logging.info("Commit by %s to %s" %
                         (checked_branch, commit.author.email))

def plural(n):
    return 's' if len(n) != 1 else ''

def main():
    if len(sys.argv) < 2:
        print("Usage: check.py <git directory>")
        sys.exit(ERROR)
    gitdir = sys.argv[1]
    if not os.path.isdir(gitdir):
        logging.error("%s is not a directory" % gitdir)
        sys.exit(ERROR)

    try:
        repo = pygit2.Repository(gitdir)
    except Exception as e:
        logging.error("Failed to open git repository %s: %r" % (gitdir, e))
        sys.exit(ERROR)

    check_merges_by_branch_authors(repo)
    check_merge_permissions(repo)

    print("Analysis complete.")
    if len(issues.items()) == 0:
        print ("No issues found in repository.")
        sys.exit(AUDIT_OK)
    else:
        print ("Issues found in git repository: ")
        for (k, v) in issues.items():
            if len(v) <= 5:
                print("  %s (%d count%s)" % (k, len(v), plural(v)))
                for note in v:
                    if note is not None:
                        print("    %s" % note)
            else:
                print("  %s (%d counts, not listed)" % (k, len(v)))
        sys.exit(AUDIT_FAILED)

if __name__ == "__main__":
    main()


from flask import Flask
from flask_jsonpify import jsonify
import json
from flask_cors import CORS

i = ''
i += 'Franziska '
i += 'Romani '
i += 'Furtado'

app = Flask(__name__)
#app.run(port = 5000)
CORS(app)
#obj = json.loads(i)
with open('teste.json', 'w') as myfile:
    json.dump(i , myfile)

@app.route("/")
def hello():
    return jsonify(i)

if __name__ == "__main__":
    app.run(port = 5001)


import collections
import sys

inputs = sys.stdin.readline
N = int(inputs())
counseling_info = [list(map(int, inputs().split())) for _ in range(N)]

def solution(n, info):
    adj_list = collections.defaultdict(tuple)
    for d, (t, p) in enumerate(info):
        adj_list[d + 1] = (t, p)

    ret = 0

    def dfs(curr_day, curr_sum):
        nonlocal adj_list, n, ret

        if curr_day == n:
            ret = max(curr_sum, ret)
            return

        dfs(curr_day + 1, curr_sum)

        if curr_day + adj_list[curr_day + 1][0] <= n:
            dfs(curr_day + adj_list[curr_day + 1][0], curr_sum + adj_list[curr_day + 1][1])

    dfs(curr_day=0, curr_sum=0)

    return ret

ret = solution(N, counseling_info)
print(ret)


import collections
import operator
import typing as T

from .base import Vocabulary

class CountVocabulary(Vocabulary[int]):
    """Vocabulary from token counts"""

    def __init__(
        self,
        sentences: T.Optional[T.Iterable[T.Iterable[str]]] = None,
        min_count: int = 1,
        max_vocab_size: T.Optional[int] = None,
        padding: bool = True,
        padding_token: T.Optional[str] = None,
        oov_token: T.Optional[str] = None,
    ) -> None:
        super().__init__(padding=padding, padding_token=padding_token, oov_token=oov_token)
        self._min_count = min_count
        self._max_vocab_size = max_vocab_size
        self._counter: T.Optional[collections.Counter] = None
        self._index_to_token: T.List[str] = []

        if sentences is not None:
            self.update(sentences)

    @property
    def index_to_token_dict(self) -> T.List[str]:
        return self._index_to_token

    @property
    def counter_dict(self):
        return self._counter

    @property
    def min_count(self) -> int:
        return self._min_count

    @min_count.setter
    def min_count(self, v: int):
        if v != self._min_count:
            self._min_count = v
            self.build()

    @property
    def max_vocab_size(self):
        return self._max_vocab_size

    @max_vocab_size.setter
    def max_vocab_size(self, v: T.Optional[int]):
        if v != self._max_vocab_size:
            self._max_vocab_size = v
            self.build()

    def to_tokens(self, id_sentences: T.Iterable[T.Iterable[int]], remove_paddings: bool = True) -> T.List[T.List[str]]:
        if not self._index_to_token:
            self._build_index_to_token()
        retlist = []
        padding_idx = self._token_to_index.get(self._padding_token)
        for ids in id_sentences:
            tokens = []
            for idx in ids:
                if remove_paddings and padding_idx is not None and idx == padding_idx:
                    continue
                tokens.append(self._index_to_token[idx])
            retlist.append(tokens)
        return retlist

    def merge_by_count(self, vocab: "CountVocabulary") -> None:
        if vocab.counter_dict:
            if self._counter is None:
                self._counter = collections.Counter()
            self._counter.update(vocab.counter_dict)
            self.build()

    def update(self, sentences: T.Iterable[T.Iterable[str]]) -> None:
        if self._counter is None:
            self._counter = collections.Counter()
        for tokens in sentences:
            self._counter.update(tokens)
        # Need to recreate token_to_index dictionary from counts
        self.build()

    def build(self):
        self.init()
        counts = sorted(self._counter.items(), key=operator.itemgetter(1), reverse=True)
        actual_max_vocab_size = self._max_vocab_size
        if actual_max_vocab_size is not None:
            actual_max_vocab_size += len(self._token_to_index)
        for key, count in counts:
            if actual_max_vocab_size is not None and len(self._token_to_index) >= actual_max_vocab_size:
                break
            if self._min_count and count < self._min_count:
                continue
            self.add_token(key)

    def __getstate__(self) -> T.Dict[str, T.Any]:
        """Robust pickling"""
        state = super().__getstate__()
        state["min_count"] = self._min_count
        state["max_vocab_size"] = self._max_vocab_size
        state["counter"] = self._counter
        return state

    def __setstate__(self, state) -> None:
        """Robust pickling"""
        super().__setstate__(state)
        self._min_count = state["min_count"]
        self._max_vocab_size = state["max_vocab_size"]
        self._counter = state["counter"]
        self._build_index_to_token()

    # overrides

    def add_token(self, token: str):
        self._token_to_index[token] = len(self._token_to_index)

    def init(self):
        super().init()
        self._index_to_token = []

    def init_padding(self):
        self.add_token(self._padding_token)

    def init_oov(self):
        self.add_token(self._oov_token)

    def serialize_entry(self, token: str, _: T.Any) -> str:
        return token

    def deserialize_entry(self, entry: str) -> None:
        self.add_token(entry)

    # private

    def _build_index_to_token(self):
        self._index_to_token = [None] * len(self._token_to_index)
        for token, token_id in self._token_to_index.items():
            self._index_to_token[token_id] = token


'''Configuration script for Target environment.'''
from collections import OrderedDict
from typing import Dict
import numpy as np

from psgi.envs.base_predicate_config import BasePredicateConfig
from psgi.envs.predicate_graph import PredicateLogicGraph

# Define parameters (no intersection allowed)
FOOD_FRUIT = ['apple', 'pear']    # pick X
FOOD_VEGI = ['cabbage', 'carrot'] # pick X / slice X
FOOD_MEAT = ['beef', 'pork']      # pick X / slice X / cook X
FOOD_PARAMS = FOOD_FRUIT + FOOD_VEGI + FOOD_MEAT

OBJECT= ['dish']
PLACE = ['table']
###
PARAMS = FOOD_PARAMS + OBJECT + PLACE
assert len(set(PARAMS)) == len(PARAMS), "Error! duplicated params!"

TRAIN_ONLY = ['apple', 'beef']
EVAL_ONLY = ['pear', 'pork']
assert all([par in PARAMS for par in TRAIN_ONLY+EVAL_ONLY]), "Unknown parameter in TRAIN_ONLY and/or EVAL_ONLY"
TRAIN_PARAMS = [par for par in PARAMS if par not in EVAL_ONLY]
EVAL_PARAMS = [par for par in PARAMS if par not in TRAIN_ONLY]

# Manually set the feature
FEATURE_POSITIVE_NAME_PARAMS = {
  'f_pickupable': FOOD_PARAMS+OBJECT,
  'f_sliceable': FOOD_PARAMS,
  'f_needslice': FOOD_VEGI + FOOD_MEAT,
  'f_cookable': FOOD_MEAT,
}
assert all([set(param_set).issubset(set(PARAMS)) for param_set in FEATURE_POSITIVE_NAME_PARAMS.values()] ), "Error! 'FEATURE_POSITIVE_NAME_PARAMS' contains unknown parameter"

class CookingClassic(BasePredicateConfig):
  environment_id = 'cooking_classic'

  def __init__(self, seed: int, graph_param: str, keep_pristine: bool = False, feature_mode: str = 'cluster-s', visualize: bool = False):
    self.num_graphs = 1
    self.max_step = 8
    super().__init__(
      seed=seed,
      graph_param=graph_param,
      keep_pristine=keep_pristine,
      feature_mode=feature_mode,
      visualize=visualize,
    )

  @property
  def parameters(self):
    return set(PARAMS)

  # Manually set
  def _construct_predicate_graph(self):
    """Implement predicate precondition&effect
    """
    g = PredicateLogicGraph('CookingClassic')
    # Add all features
    PICKUPABLE_X = '(f_pickupable, A)' # pick
    SLICEABLE_X = '(f_sliceable, A)' # slice
    NEEDSLICE_X = '(f_needslice, A)'
    COOKABLE_X = '(f_cookable, A)' # slice

    g.add_features([PICKUPABLE_X, SLICEABLE_X, NEEDSLICE_X, COOKABLE_X])

    # Add all subtask nodes (just names)
    PICKUP_X = '(pickup, A)' # pick
    SLICE_X = '(slice, A)' # slice
    COOK_X = '(cook, A)'
    g.add_subtasks([PICKUP_X, SLICE_X, COOK_X])

    # Add each option one-by-one
    OP_PICKUP_X = '(op_pickup, A)'
    g.add_option(
      name=OP_PICKUP_X,
      precondition=g[PICKUPABLE_X],
      effect=g[PICKUP_X]
    )
    # Slice option
    OP_SLICE_X = '(op_slice, A)'
    g.add_option(
      name=OP_SLICE_X,
      precondition=g[SLICEABLE_X] & g[PICKUP_X],
      effect=g[SLICE_X]
    )
    # Cook option
    OP_COOK_X = '(op_cook, A)'
    AND1 = g[COOKABLE_X] & g[PICKUP_X] & (~g[NEEDSLICE_X])
    AND2 = g[COOKABLE_X] & g[PICKUP_X] & g[SLICE_X]
    g.add_option(
      name=OP_COOK_X,
      precondition=AND1|AND2,
      effect=g[COOK_X]
    )
    return g

  def _sample_param_set(self, seed, graph_param):
    if graph_param == 'train':
      params = TRAIN_PARAMS
    elif graph_param == 'eval':
      params = EVAL_PARAMS
    else:
      raise ValueError(f"Error! Got unknown graph_param: {graph_param}")
    return params

  # Manually set
  def _set_reward_and_termination(self):
    if self.graph_param == 'train':
      self._add_reward(name='(cook, beef)', reward=1.0, terminal=True, success=True)
    elif self.graph_param == 'eval':
      self._add_reward(name='(cook, pork)', reward=1.0, terminal=True, success=True)

  # Manually set
  def _construct_initial_completion(self, unrolled_graph):
    # Initialize
    completion = OrderedDict()
    for node in unrolled_graph.subtask_nodes:
      completion[node.name] = False

    # Nothing is completed in the beginning
    return completion

  def _construct_feature_positives(self):
    return FEATURE_POSITIVE_NAME_PARAMS

  def _perturb_subtasks(self, rng):
    return


import os, sys, requests
import time, json
from bs4 import BeautifulSoup

URL_CONST = 7

def clear():
    if os.name == 'nt':
        _ = os.system('cls')

    else:
        _ = os.system('clear')

def yes_no_query():
  yes = {'y', 'yes', 'ye', ''}
  no = {'no','n'}

  while(True):
    choice = input().lower()
    if choice in yes:
      return True
    elif choice in no:
      return False
    else:
      sys.stdout.write("Please respond with 'yes' or 'no'\t")

def read_monitor_query():
  read = {'read', 'r', 'rea'}
  monitor = {'monitor','monit','m'}

  while(True):
    choice = input().lower()
    if choice in read:
      return True
    elif choice in monitor:
      return False
    else:
      sys.stdout.write("Please respond with 'read' or 'monitor'\t")

def enter_query():
  enter = {''}

  while(True):
    choice = input().lower()
    if choice in enter:
      return True
    else:
      return False

#main

#clear the console window
clear()

#load the saved url, ask if it's okay, do changes and start the monitor
print("Default file: stored_data.txt\nPress Enter to continue or input anything to select another file:\n")
if enter_query() == False:
    print("\nPlease specify the name of file you want to use:\n")
    filename = input()
    if not filename.endswith(".txt"):
        filename = filename + ".txt"
else:
    filename = "stored_data.txt"

#load the json file
data = None

while(True):
  try:
      with open(filename, 'r') as textfile:
          data = json.load(textfile)
          if "url" not in data or not data["url"]:
              raise FileNotFoundError
          else:
              break

  except (FileNotFoundError, json.decoder.JSONDecodeError):   #if there is no file, input new url
      print(f"File {filename} does not exist or is corrupted, do you want to create a new one? Y/n")
      if yes_no_query() == False:
          exit("Program has ended.")

      print("Please input the url of the Minecraft server you want to monitor:\n")
      url = input()
      data = {
        "url": str(url),
        "name": "",
        "address": "",
        "max players": 0,
        "hours": {}
        }
      with open(filename, 'w') as outfile:
          json.dump(data, outfile, indent=4)

#request the webpage data
clear()
print(f"Currently used url: {data['url']}\n")

print("Do you want to launch the monitor or get a single data read?\tmonitor/read")
if read_monitor_query() == True:    #single read
    res = requests.get(data['url'])
    soup = BeautifulSoup(res.text, 'html.parser')

    server_name = (soup.find("h1").string)
    server_ip = (soup.find(string = "Address").parent.parent.parent.contents[3].string)

    player_string = (soup.find(string = "Players").parent.parent.parent.contents[3].string)
    players = int(player_string.split("/")[0])
    max_players = int(player_string.split("/")[1])

    clear()
    print(f"Server name: {server_name}\nAddress: {server_ip}\n")
    print(f"Players: {players}\nMax players: {max_players}\n")

else:                               #monitor
    while(True):
        t = time.localtime()
        current_time = time.strftime("%H:%M", t)
        print(f"Running: {current_time}", end="\r")

        if t.tm_min % 15 == 0:   #get data

            res = requests.get(data['url'])
            soup = BeautifulSoup(res.text, 'html.parser')

            server_name = (soup.find("h1").string)
            server_address = (soup.find(string = "Address").parent.parent.parent.contents[3].string)

            player_string = (soup.find(string = "Players").parent.parent.parent.contents[3].string)

            players = int(player_string.split("/")[0])
            max_players = int(player_string.split("/")[1])

            data["name"] = server_name
            data["address"] = server_address
            data["max players"] = max_players

            time_formatted = str( float(t.tm_hour) + round(float(t.tm_min/60), 2) )

            if time_formatted not in data["hours"]:
                data["hours"][time_formatted] = []
            data["hours"][time_formatted].append(players)

            with open(filename, 'w') as outfile:
                json.dump(data, outfile, indent=4)
            print("\nCheck successful!")

        time.sleep(60)


from django import forms
from .models import *
from django.utils.timezone import now

category_3 = {1: 'Удовлетворительное', 2: 'Хорошее', 0: 'Неудовлетворительное'}
category_3 = tuple(map(tuple, category_3.items()))

mark_rep = {0: 'Репер', 1: 'Марка', 2: 'Инклинометр'}
mark_rep = tuple(map(tuple, mark_rep.items()))

cond = {1: 'Действующая', 2: 'Резервная', 3: 'Недействующая', 4: 'Законсервированная', 5: 'Неисправная',
        6: 'Заброшенная', 7: 'Ликвидированная', 8: 'Проектная', 9: 'Пробурена'}
cond = tuple(map(tuple, cond.items()))

class FileFieldForm(forms.Form):
    file_field = forms.FileField(label='Фотодокументация', widget=forms.ClearableFileInput(attrs={'multiple': True}))

class EgpObservesInspectionForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(EgpObservesInspectionForm, self).__init__(*args, **kwargs)
        self.fields['date'].initial = now
        self.fields['damage_description'].label = False
        self.fields['survey2'].label = False
        self.fields['logs_results'].label = False

    class Meta:
        model = EgpObservesInspection
        fields = (
            'egp_obs', 'date', 'survey1', 'survey2', 'painting_condition', 'label_condition', 'damage_description',
            'logs_status', 'logs_results', 'comments', 'recommendations')

        widgets = {
            'egp_obs': forms.TextInput(attrs={'class': 'form-control', 'type': 'integer'}),
            'date': forms.DateInput(attrs={'class': 'form-control', 'type': 'date'}),
            'painting_condition': forms.Select(choices=category_3, attrs={'class ': 'form-control'}),
            'label_condition': forms.Select(choices=category_3, attrs={'class': 'form-control'}),
            'damage_description': forms.Textarea(
                attrs={'class': 'form-control', 'type': 'text', 'rows': 2, 'placeholder': 'Описание повреждений'}),
            'logs_status': forms.CheckboxInput(),
            'logs_results': forms.Textarea(
                attrs={'class': 'form-control', 'type': 'text', 'rows': 2, 'placeholder': 'Результаты видеокаротажа'}),
            'comments': forms.Textarea(
                attrs={'class': 'form-control', 'type': 'text', 'rows': 2}),
            'recommendations': forms.Textarea(
                attrs={'class': 'form-control', 'type': 'text', 'rows': 2}),
            'survey1': forms.Select(attrs={'class': 'form-control'}),
            'survey2': forms.Select(attrs={'class': 'form-control'})
        }
        labels = {
            'egp_obs': 'Номер пункта наблюдений',
            'date': 'Дата инспекции',
            'painting_condition': 'Состояние покраски',
            'label_condition': 'Состояние маркировки',
            'logs_status': 'Проведение видеокаротажа',
            'comments': 'Примечания',
            'recommendations': 'Рекомендации',
            'survey1': 'Обследование провели',
        }

class EgpObservesForm(forms.ModelForm):
    class Meta:
        model = EgpObserves
        fields = ('egp_obs_type',)

        widgets = {
            'egp_obs_type': forms.Select(choices=mark_rep, attrs={'class': 'form-control'}),
        }
        labels = {
            'egp_obs_type': 'Тип пункта наблюдений',
        }

class EgpObservesConditionForm(forms.ModelForm):
    class Meta:
        model = EgpObservesCondition
        fields = ('condition',)

        widgets = {
            'condition': forms.Select(choices=cond, attrs={'class': 'form-control'}),
        }
        labels = {
            'condition': 'Техническое состояние пункта наблюдений',
        }

class DocumentsForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(DocumentsForm, self).__init__(*args, **kwargs)
        self.fields['creation_date'].initial = now
    class Meta:
        model = Documents
        fields = ('creation_date',)

        widgets = {
            'creation_date': forms.DateInput(attrs={'class': 'form-control', 'type': 'date'}),
        }
        labels = {
            'creation_date': 'Дата создания акта',
        }


from docx.shared import Mm
from docx.shared import Pt
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.shared import RGBColor, Inches
from docx.oxml.ns import nsdecls
from docx.oxml import parse_xml

##
#cloudos2.0 api端口为9000
#cloudos3.0 api端口为8000

# 创建表格,默认行距为1cm
def createTable(document, row, col):
    # table = document.add_table(row, col, style='Medium Grid 1 Accent 1')
    table = document.add_table(row, col, style='Table Grid')
    table.style.font.name = u'宋体'
    table.style.font.size = Pt(11)
    for i in table.rows[0].cells:
        shading_elm_2 = parse_xml(r'<w:shd {} w:fill="B0C4DE"/>'.format(nsdecls('w')))
        i._tc.get_or_add_tcPr().append(shading_elm_2)
        del shading_elm_2
    # table = document.add_table(row, col, style='Medium Shading 2 Accent 1')
    for i in table.rows:
        i.height = Mm(10)
    return table

# 合并单元格,返回单元格地址
def mergeCell(table, beginRow, beginCol, endRow, endCol):
    c1 = table.cell(beginRow, beginCol)
    c2 = table.cell(endRow, endCol)
    return c1.merge(c2)

# document = openDocument(r'cas.docx')
# serverList为参数列表，包含6个参数，从参数0-6分别为：服务器型号、服务器规格、CAS版本、CVM部署方式、
# S1020V版本、是否使用临时license
def casBasicDocument(document, list1):
    h1 = document.add_heading('CAS平台巡检结果')
    h1.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
    h2 = document.add_heading('1.CAS平台基本信息')
    t1 = createTable(document, 7, 2)

    # 初始化表格
    t1.cell(0, 0).text = "巡检项"
    t1.cell(0, 1).text = "参数"
    t1.cell(1, 0).text = "服务器型号"
    t1.cell(2, 0).text = "服务格器规"
    t1.cell(3, 0).text = "CAS版本号"
    t1.cell(4, 0).text = "CVM部署方式"
    t1.cell(5, 0).text = "S1020V版本号"
    t1.cell(6, 0).text = "是否有使用临时license"
    # 参数赋值
    for i in range(6):
        run = t1.cell(i + 1, 1).paragraphs[0].add_run(list1[i])
        # run.font.name = '宋体'
        # run.font.size = Pt(11)
    return

# li1,li2为参数列表，list1为巡检结果，list2为巡检结果说明
# list1
def clusterDocument(document, list1, list2):
    h1 = document.add_heading('2.集群巡检')
    count = 0
    text = ''
    for i in list2:
      if i:
          count += 1
    p1 = document.add_paragraph()
    run1 = p1.add_run("巡检小结：")
    run1.font.name = u'宋体'
    run1.font.size = Pt(11)
    text = "对集群虚拟化进行巡检，巡检异常项数：" + (str)(count) + "；" + "正常项数：" + (str)(len(list2) - count)
    p2 = document.add_paragraph()
    p2.paragraph_format.first_line_indent = Inches(0.3)
    run2 = p2.add_run(text)
    run2.font.name = u'宋体'
    run2.font.size = Pt(11)

    t1 = createTable(document, 7, 4)
    # 初始化表格
    t1.cell(0, 0).text = "检查内容"
    t1.cell(0, 1).text = "检查方法"
    t1.cell(0, 2).text = "检查结果"
    t1.cell(0, 3).text = "说明"
    t1.cell(1, 0).text = "集群高可靠性（HA）功能：查看集群的高可靠性（HA）功能是否正常开启"
    t1.cell(1, 1).text = "在<云资源>/<主机池>/<集群>的“高可靠性”页面检查是否选择了“启用HA”"
    t1.cell(2, 0).text = "集群动态资源调度（DRS）功能：查看集群的动态" \
                         "资源调度（DRS）功能是否正常开启"
    t1.cell(2, 1).text = "在<云资源>/<主机池>/<集群>的“动态资源调度”" \
                         "页面检查是否选择了“开启动态资源调度”"
    t1.cell(3, 0).text = "集群下虚拟交换机分配：查" \
                         "看集群下虚拟交换机的分配情况。"
    t1.cell(3, 1).text = "在<云资源>/<主机池>/<主机>的“虚拟交" \
                         "换机”页面检查集群下的所有主机是否都" \
                         "有相同名称的虚拟交换机"
    t1.cell(4, 0).text = "集群下共享存储分配：" \
                         "查看集群下共享存储的分配情况"
    t1.cell(4, 1).text = "在<云资源>/<主机池>/<集群>的“存储”" \
                         "页面检查集群下的主机是否都分配了相同的共享存储"
    t1.cell(5, 0).text = "集群下共享存储使用率：查看集群下共享存" \
                         "储的实际使用情况，实际使用率超过70%标记为不" \
                         "正常。实际使用率超过90%，标记为平台重大风险项。"
    t1.cell(5, 1).text = "在<云资源>/<主机池>/<集群>的“存储”页面检" \
                         "查集群下的共享存储可用容量"
    t1.cell(6, 0).text = "集群高可靠性生效最小节点数：查看集群中正常运行的主机数量不少于“HA生效最小节点数”"
    t1.cell(6, 1).text = "在<云资源>/<主机池>/<集群>的“高可靠性”页面检查“HA生效最小节点数”和集群内正常运行的主机数量"

    t1.columns[2].width = Mm(20)
    # 参数赋值
    for i in range(6):
        if not list2[i]:
            t1.cell(i + 1, 2).paragraphs[0].add_run(list1[i])
        else:
            run = t1.cell(i + 1, 2).paragraphs[0].add_run(list1[i])
            run.font.color.rgb = RGBColor(255, 0, 0)
            t1.cell(i + 1, 3).paragraphs[0].add_run(list2[i])
    return

def hostDocument(document, list1, list2):
    h1 = document.add_heading('3.主机巡检')
    count = 0
    text = ''
    for i in list2:
      if i:
          count += 1
    p1 = document.add_paragraph()
    run1 = p1.add_run("巡检小结：")
    run1.font.name = u'宋体'
    run1.font.size = Pt(11)
    text = "对主机CVK进行巡检，巡检异常项数：" + (str)(count) + "；" + "正常项数：" + (str)(len(list2) - count)
    p2 = document.add_paragraph()
    p2.paragraph_format.first_line_indent = Inches(0.3)
    run2 = p2.add_run(text)
    run2.font.name = u'宋体'
    run2.font.size = Pt(11)
    t1 = createTable(document, 8, 4)
    # 初始化表格
    t1.cell(0, 0).text = "检查内容"
    t1.cell(0, 1).text = "检查方法"
    t1.cell(0, 2).text = "检查结果"
    t1.cell(0, 3).text = "说明"
    t1.cell(1, 0).text = "*主机状态：\n*查看所有主机的运行状态。"
    t1.cell(1, 1).text = "在<云资源>的“主机”页面检查所有主" \
                         "机的运行状态是否显示“正常”"
    t1.cell(2, 0).text = "主机CPU占用率：查看所有主机CPU占用率，不超过80%"
    t1.cell(2, 1).text = "在<云资源>的“主机”页面检查所有主机的CPU占用率是否正常。"
    t1.cell(3, 0).text = "主机内存占用率：查看所有主机内存占用率，不超过80%。"
    t1.cell(3, 1).text = "在<云资源>的“主机”页面检查所有主机的内存占用率是否正常。"
    t1.cell(4, 0).text = "主机的磁盘和分区占用率：查看主机的磁盘和分区占用率，各个分区的占用率不超过80%。"
    t1.cell(4, 1).text = "在<云资源>/<主机池>/<集群>/<主机>的“性能监控”页面，查看“磁盘利用率”和“分区利用率”"
    t1.cell(5, 0).text = "主机的存储池：查看主机的存储池资源是否正常。\n*状态：活动"
    t1.cell(5, 1).text = "在<云资源>/<主机池>/<集群>/<主机>的“存储”页面，查看状态是否为“活动”，是否有足够的存储资源"
    t1.cell(6, 0).text = "主机的虚拟交换机：查看主机的虚拟交换机池资源是否正常。\n*状态：活动"
    t1.cell(6, 1).text = "在<云资源>/<主机池>/<集群>/<主机>的“虚拟交换机”页面，查看状态是否为“活动”，并且仅配置一个网关"
    t1.cell(7, 0).text = "主机的物理网卡：查看主机的物理网卡是否正常。" \
                         "\n*状态：活动\n*速率：与物理网卡实际速率保持一致" \
                         "\n*工作模式：full"
    t1.cell(7, 1).text = "在<云资源>/<主机池>/<集群>/<主机>的“物理网卡”" \
                         "页面，查看“状态”、“速率”以及“工作模式”是否正常。"

    # t1.cell(8, 0).text = "主机的FC HBA卡状态（可选）：查看主机的FC HBA卡是否" \
    #                      "正常。\n*状态：活动\n*速率：与物理FC HBA卡实际速率保持一致"
    # t1.cell(8, 1).text = "在<云资源>/<主机池>/<集群>/<主机>的“FC HBA”" \
    #                      "页面，查看“状态”和“速率”是否正常。"

    # 参数赋值
    shading_elm_1 = parse_xml(r'<w:shd {} w:fill="FF0000"/>'.format(nsdecls('w')))
    for i in range(7):
        if not list2[i]:
            t1.cell(i + 1, 2).paragraphs[0].add_run(list1[i])
        else:
            run = t1.cell(i + 1, 2).paragraphs[0].add_run(list1[i])
            run.font.color.rgb = RGBColor(255, 0, 0)
            t1.cell(i + 1, 3).paragraphs[0].add_run(list2[i])
    return

def vmDocument(document, list1, list2):
    h1 = document.add_heading('4.虚拟机巡检')
    count = 0
    text = ''
    for i in list2:
        if i:
            count += 1
    p1 = document.add_paragraph()
    run1 = p1.add_run("巡检小结：")
    run1.font.name = u'宋体'
    run1.font.size = Pt(11)
    text = "对主机虚拟机进行巡检，巡检异常项数：" + (str)(count) + "；" + "正常项数：" + (str)(len(list2) - count)
    p2 = document.add_paragraph()
    p2.paragraph_format.first_line_indent = Inches(0.3)
    run2 = p2.add_run(text)
    run2.font.name = u'宋体'
    run2.font.size = Pt(11)
    t1 = createTable(document, 8, 4)
    # 初始化表格
    t1.cell(0, 0).text = "检查内容"
    t1.cell(0, 1).text = "检查方法"
    t1.cell(0, 2).text = "检查结果"
    t1.cell(0, 3).text = "说明"
    t1.cell(1, 0).text = "*虚拟机状态：\n*查看所有虚拟机的运行状态"
    t1.cell(1, 1).text = "在<云资源>的“虚拟机”页面检查所有虚拟机的运行状态。"
    t1.cell(2, 0).text = "虚拟机CPU占用率：查看所有虚拟机CPU占用率，不超过80%"
    t1.cell(2, 1).text = "在<云资源>的“虚拟机”页面检查所有主机的CPU占用率是否正常。"
    t1.cell(3, 0).text = "虚拟机内存占用率：查看所有虚拟机内存占用率，不超过80%。"
    t1.cell(3, 1).text = "在<云资源>的“虚拟机”页面检查所有虚拟机的内存占用率是否正常。"
    t1.cell(4, 0).text = "虚拟机的CAS Tools：查看虚拟机的CAS Tools工具是否正常运行。"
    t1.cell(4, 1).text = "在<云资源>/<主机池>/<集群>/<主机>/<虚拟机>的“概要”页面，查看“CAS Tools”是否为运行状态"
    t1.cell(5, 0).text = "虚拟机的磁盘和分区占用率：查看虚拟机的磁盘和分区占用率，各个分区的占用率不超过80%。"
    t1.cell(5, 1).text = "在<云资源>/<主机池>/<集群>/<主机>/<虚拟机>的“性能监控”页面，查看“磁盘利用率”和“分区利用率”"
    t1.cell(6, 0).text = "虚拟机的磁盘类型（大云可选）：查看虚拟机的磁盘信息。\n" \
                         "*设备对象：virtio磁盘 XXX\n" \
                         "*源路径：共享存储路径\n" \
                         "*缓存方式：建议使用“directsync”\n" \
                         "*存储格式：建议使用“智能”"
    t1.cell(6, 1).text = "在<云资源>/<主机池>/<集群>/<主机>/<虚拟机>的“修改虚拟机”对话框，查看“总线类型”和“存储卷路径”等"
    t1.cell(7, 0).text = "拟机的网卡（大云可选）：" \
                         "查看虚拟机的网卡信息。\n" \
                         "*设备型号：virtio网卡\n" \
                         "*内核加速：勾选"
    t1.cell(7, 1).text = "在<云资源>/<主机池>/<集群>/<主机>/<虚拟机>的“修改虚拟机”对话框，查看网卡类型。"
    # 参数赋值
    for i in range(7):
        if not list2[i]:
            t1.cell(i + 1, 2).paragraphs[0].add_run(list1[i])
        else:
            run = t1.cell(i + 1, 2).paragraphs[0].add_run(list1[i])
            run.font.color.rgb = RGBColor(255, 0, 0)
            t1.cell(i + 1, 3).paragraphs[0].add_run(list2[i])
    return

def systemHaDocument(document, list1, list2):
    h1 = document.add_heading('5.系统可靠性巡检')
    count = 0
    text = ''
    for i in list2:
        if i:
            count += 1
    p1 = document.add_paragraph()
    run1 = p1.add_run("巡检小结：")
    run1.font.name = u'宋体'
    run1.font.size = Pt(11)
    text = "对系统可靠性进行巡检，巡检异常项数：" + (str)(count) + "；" + "正常项数：" + (str)(len(list2) - count)
    p2 = document.add_paragraph()
    p2.paragraph_format.first_line_indent = Inches(0.3)
    run2 = p2.add_run(text)
    run2.font.name = u'宋体'
    run2.font.size = Pt(11)
    t1 = createTable(document, 5, 4)
    t1.style.font.name = '微软雅黑'
    t1.style.font.size = Pt(9)
    # 初始化表格
    t1.cell(0, 0).text = "检查内容"
    t1.cell(0, 1).text = "检查方法"
    t1.cell(0, 2).text = "检查结果"
    t1.cell(0, 3).text = "说明"
    t1.cell(1, 0).text = "链路冗余：查看系统的链路冗余情况。"
    t1.cell(1, 1).text = "在<云资源>/<主机池>/<集群>/<主机>/<虚拟交换机>页面，检查各个虚拟交换机是否进行了链路冗余（动态或者静态聚合）"
    t1.cell(2, 0).text = "CVM配置备份：查看CVM配置的备份情况。用户CVM主机故障时的系统配置恢复。"
    t1.cell(2, 1).text = "在<系统管理>/<安全管理>的“CVM备份配置”页面，确认已启用了定时备份功能，推荐备份到远端目录。"
    t1.cell(3, 0).text = "CVM双机热备状态检查：检查CAS的CVM双机热备状态是否正常。"
    t1.cell(3, 1).text = "在CVM双机热备环境中，随意登录CVM主机后台执行“crm status”检查双机热备状态。"
    t1.cell(4, 0).text = "虚拟机的备份：检查重要虚拟机是否已经开启备份功能。"
    t1.cell(4, 1).text = "检查运行客户重要业务的虚拟机是否开启了定时备份功能。"
    # 参数赋值
    for i in range(4):
        if not list2[i]:
            t1.cell(i + 1, 2).paragraphs[0].add_run(list1[i])
        else:
            run = t1.cell(i + 1, 2).paragraphs[0].add_run(list1[i])
            run.font.color.rgb = RGBColor(255, 0, 0)
            t1.cell(i + 1, 3).paragraphs[0].add_run(list2[i])
    return

# cvm平台信息巡检
def cvmCheck(document, casInfo):
    list1 = []
    list1.append(casInfo['productVersion'])
    list1.append(casInfo['deviceDmide'])
    list1.append(casInfo['casVersion'])
    list1.append(casInfo['installType'])
    list1.append(casInfo['ovsVersion'])
    list1.append(casInfo['licenseInfo'])
    casBasicDocument(document, list1)
    del list1
    return

##################
# 集群巡检        #
##################
def clusterCheck(document, casInfo):
    list1 = []
    list2 = ['' for n in range(7)]

    # 集群是否开启HA和DRS
    tempHa = ''
    tempLB = ''
    for i in casInfo['clusterInfo']:
        if i['enableHA'] == '0':
            list2[0] += "集群" + i['name'] + " HA未开启\n"
        if i['enableLB'] == '0':
            list2[1] = "集群" + i['name'] + " DRS未开启\n"

    # 集群下主机虚拟交换机部署是否合规
    dict1 = dict()
    for i in casInfo['clusterInfo']:
        dict1[i['name']] = list()
        for j in i['cvkInfo']:
            if j['status'] == '1':
                for k in j['vswitch']:
                    if not k['name'] in dict1[i['name']]:
                        dict1[i['name']].append(k['name'])
    for i in casInfo['clusterInfo']:
        if len(dict1[i['name']]) != 3:
            list2[2] += "集群" + i['name'] + "下交换机的部署不合规\n"

    # cvk共享存储池部署是否一致
    dict1 = {}  # 存储集群下的所有共享存储池
    dict2 = {}  # 存储主机下的共享存储池
    for i in casInfo['clusterInfo']:
        dict1[i['name']] = set()
        for j in i['cvkInfo']:
            dict2[j['name']] = set()
            if j['status'] == '1':
                for k in j['sharePool'] :
                    dict1[i['name']].add(k['name'])
                    dict2[j['name']].add(k['name'])

        for m in i['cvkInfo']:
            if m['status'] == '1':
                if dict1[i['name']] != dict2[m['name']]:
                    list2[3] += "集群" + i['name'] + "下主机" + m['name'] + "共享存储池与集群不一致"
    del dict1, dict2

    # 共享存储利用率：
    for i in casInfo['clusterInfo']:
        li1 = list()
        for j in i['cvkInfo']:
            if j['status'] == '1':
                for k in j['sharePool']:
                    if not k in li1:
                        li1.append(k)
        for h in li1:
            if h['rate'] > 0.8:
                list2[4] = "集群" + i['name'] + "下共享存储池" + h['name'] + "利用率超过80%达到" + str(h['rate'])
        del li1

    # 集群最小主机节点
    for i in casInfo['clusterInfo']:
        if i['enableHA'] == '0':
            list2[5] = "集群未开启高可靠"
        else:
            if (int)(i['HaMinHost']) > len(i['cvkInfo']):
                list2[5] = "Ha最小节点数小正常运行主机数"

    for i in list2:
        if not i:
            list1.append("正常")
        else:
            list1.append("异常")

    clusterDocument(document, list1, list2)
    del list1, list2
    return

#######################################
# 主机巡检                            #
#                                     #
########################################
def cvkCheck(document, casInfo):
    list1 = []
    list2 = ['' for n in range(7)]
    dict1 = {}
    dict2 = {}
    dict3 = {}
    dict4 = {}
    for i in casInfo['clusterInfo']:
        for j in i['cvkInfo']:
            dict1[j['name']] = ''
            dict2[j['name']] = ''
            dict3[j['name']] = ''
            dict4[j['name']] = ''
            # 主机状态检测
            if j['status'] != '1':
                if not list2[0]:
                    list2[0] += "状态异常主机如下" + j['name'] + '\n'
                else:
                    list2[0] += j['name'] + '\n'
            else:
                # 主机cpu利用率
                if j['cpuRate'] > 80:
                    if not list2[1]:
                        list2[1] += "cpu利用率超过80%主机如下：" + j['name'] + '\n'
                    else:
                        list2[1] += j['name'] + '\n'

                # 主机内存利用率
                if j['memRate'] > 80:
                    if not list2[2]:
                        list2[2] += "内存利用率超过80%主机如下：" + j['name'] + '\n'
                    else:
                        list2[2] += j['name'] + '\n'

                # 主机磁盘利用率
                for k in j['diskRate']:
                    if (float)(k['usage']) > 80:
                        if not dict1[j['name']]:
                            dict1[j['name']] += "\n主机" + j["name"] + "磁盘利用率查过80%的磁盘如下：" + k["name"]
                        else:
                            dict1[j['name']] += ("、" + k["name"])

                # 主机存储池状态：
                for m in j['storagePool']:
                    if m['status'] != '1':
                        if not dict2[j['name']]:
                            dict2[j['name']] = "\n主机" + j['name'] + "状态异常磁盘如下：" + m['name']
                        else:
                            dict2[j['name']] += ("、" + m['name'])

                # 虚拟交换机状态
                for k in j['vswitch']:
                    if k['status'] != '1':
                        if not dict3[j['name']]:
                            dict3[j['name']] = "\n主机" + j['name'] + "状态异常虚拟交换机如下：" + k['name']
                        else:
                            dict3[j['name']] += ("、" + k['name'])

                #网卡状态
                for k in j['network']:
                    if k['status'] != '1':
                        if not dict4[j['name']]:
                            dict4[j['name']] = "\n主机" + j['name'] + "状态异常网卡如下：" + k['name']
                        else:
                            dict4[j['name']] += ("、" + k['name'])

    for h in dict1.values():
        list2[3] += h

    for h in dict2.values():
        list2[4] += h

    for h in dict3.values():
        list2[5] += h

    for h in dict4.values():
        list2[6] += h
    del dict1, dict2, dict3, dict4
    # 主机巡检结果写入docx
    for i in list2:
        if not i:
            list1.append("正常")
        else:
            list1.append("异常")
    hostDocument(document, list1, list2)
    del list1, list2
    return

#######################################
# 虚拟机巡检                           #
########################################

def vmCheck(document, casInfo):
    list1 = []
    list2 = ['' for n in range(7)]
    for i in casInfo['clusterInfo']:
        for j in i['cvkInfo']:
            dict1 = {}
            dict2 = {}
            if j['status'] == '1':
                for k in j['vmInfo']:
                    # print(k)
                    # 虚拟机状态
                    if k['status'] != 'running' and k['status'] != 'shutOff':
                        if not list2[0]:
                            list2[0] = "状态异常虚拟机如下：" + k['name']
                        else:
                            list2[0] += '、' + k['name']
                    else:
                        if k['status'] == 'running':
                        # 虚拟机cpu利用率是否超过80%
                            if k['cpuReate'] > 80:
                                if not list2[1]:
                                    list2[1] = "cpu利用率超过80%虚拟机如下：" + k['name']
                                else:
                                    list2[1] += '、' + k['name']

                            # 虚拟机内存利用率是否超过80%
                            if k['memRate'] > 80:
                                if not list2[2]:
                                    list2[2] = "内存利用率超过80%虚拟机如下：" + k['name']
                                else:
                                    list2[2] += '、' + k['name']

                            # 虚拟机castool状态异常
                            if k['castoolsStatus'] != '1':
                                if not list2[3]:
                                    list2[3] = "castool状态虚拟机如下：" + k['name']
                                else:
                                    list2[3] += '、' + k['name']

                            # 虚拟机磁盘分区巡检
                            tmp = ''
                            for m in k['diskRate']:
                                if m['usage'] > 80:
                                    if not tmp:
                                        tmp = "\n虚拟机" + k['name'] + '磁盘利用率超过80%的磁盘如下：' + m['name']
                                    else:
                                        tmp += '、' + m['name']
                            list2[4] += tmp
                            del tmp

                            # 虚拟机磁盘巡检
                            dict1[k['name']] = ''
                            for n in k['vmdisk']:
                                tmp = n['path'].split('/')
                                path = '/' + tmp[1] + '/' + tmp[2]
                                bool1 = False
                                for m in j['sharePool']:
                                    if path == m['path']:
                                        bool1 = True
                                if not bool1:
                                    if not dict1[k['name']]:
                                        dict1[k['name']] = "\n虚拟机" + k['name'] + "磁盘" + n['name'] + '使用了非共享存储池'
                                    else:
                                        dict1[k['name']] += "磁盘" + n['name'] + '使用了非共享存储池'
                                if n['format'] != 'qcow2':
                                    if not dict1[k['name']]:
                                        dict1[k['name']] = "\n虚拟机" + k['name'] + "磁盘" + n['name'] + '格式错误'
                                    else:
                                        dict1[k['name']] += "磁盘" + n['name'] + '格式错误'
                                if n['cacheType'] != 'directsync':
                                    if not dict1[k['name']]:
                                        dict1[k['name']] = "\n虚拟机" + k['name'] + "磁盘" + n['name'] + '缓存方式错误'
                                    else:
                                        dict1[k['name']] += "磁盘" + n['name'] + '缓存方式错误'
                            list2[5] += dict1[k['name']]

                            # 虚拟机网卡巡检
                            dict2[k['name']] = ''
                            for m in k['vmNetwork']:
                                if m['mode'] != 'virtio':
                                    if not dict2[k['name']]:
                                        dict2[k['name']] = '\n虚拟机' + k['name'] + '网卡' + m['name'] + '模式错误'
                                    else:
                                        dict2[k['name']] = '网卡' + m['name'] + '模式错误'
                                if m['KernelAccelerated'] != '1':
                                    if not dict2[k['name']]:
                                        dict2[k['name']] = '\n虚拟机' + k['name'] + '网卡' + m['name'] + '未开启内核加速'
                                    else:
                                        dict2[k['name']] = '网卡' + m['name'] + '未开启内核加速'
                            list2[6] += dict2[k['name']]
            del dict1, dict2

    for i in list2:
        if not i:
            list1.append("正常")
        else:
            list1.append("异常")
    vmDocument(document, list1, list2)
    del list1, list2
    return

####################
# cvm可靠性巡检
####################
def cvmHaCheck(document, casInfo):
    list1 = []
    list2 = ['' for n in range(4)]

    # 虚拟交换机的是否配置冗余链路

    # cvm是否开启备份策略
    if casInfo['BackupEnable'] != '1':
        list2[1] = 'cvm未开启备份策略'
    # cvm是否开启HA高可靠
    if not casInfo['HA']:
        list2[2] = '未开启HA高可靠'
    # 检查虚拟机是否配置高可靠
    if casInfo['vmBackPolicy'] == 'NONE':
        list2[3] = '未配置虚拟机备份'
    else:
        for i in casInfo['vmBackPolicy']:
            if i['state'] != '1':
                if not list2[3]:
                    list2[3] = '状态异常备份策略如下：' + i['name']
                else:
                    list2[3] += '、' + i['name']
    for i in list2:
        if not i:
            list1.append("正常")
        else:
            list1.append("异常")
    systemHaDocument(document, list1, list2)
    return


import hashlib
import matplotlib.pyplot as plt

#from my_laba import MyHashFunction,MyHashNumberFunction,HashMD5
#Хеш функции
#Хэш-функция берем сумму первой и последней букв слова
def MyHashFunction(word):
    if word != '':
        return (ord(word[0])+ord(word[-1]))//10
    return None
#Хэш-функция берем все букву и умножаем на их порядковый номер
def MyHashNumberFunction(word):
    if word != '':
        sum=0
        for i in range(len(word)):
            sum+=i*ord(word[i])
        return sum
    return None
#Хэш-функция MD5
def HashMD5(word):
    if word != '':
        return hashlib.md5(word.encode()).hexdigest()
    return None

class guards_date:   #является ячейкой помяти
    def __init__(self,text=None):
        if text:
            self.id=text[1]     #код хранимой информации
            self.text=text[0]  #хранит информацию
        else:
            self.id=text    #код хранимой информации
            self.text=text  #хранит информацию
    def delet(self):    #функция удаления
        self.id=None
        self.text=None  #функция удаления

class key_date:     #хранит информацию о ключе
    def __init__(self, name,pair=None):
        self.key_name=str(name)      #ключ
        self.pair=guards_date(pair)        #ссылка на элемент памяти :guards_date

    def replacement_pair(self,pair):    #замена ссылки на ячейку памяти
        self.pair=guards_date(pair)            #замена ссылки на ячейку памяти
    def del_key(self):          #удаление ключа
        self.key_name=None
        self.pair.delet()
        self.pair=None                      #удаление ключа

class hash:
    def __init__(self, name,key=None,pair=None):
        self.hash_name=str(name)      #хеш
        self.keys=[]        #ссылка на ключи :key_date
        if key!=None:
            self.keys.append(key_date(key))
            if pair!=None:
                self.keys[len(self.keys)-1].replacement_pair(pair)    #хранит ключи
    def add_key(self,key,pair):    #добавление ссылки на ячейку памяти
        for i in self.keys:
            if i.key_name==key:
                i.replacement_pair(pair)
                return True
        self.keys.append(key_date(key, pair))              #добавляет или заменяет ключ и данные его
        return False
    def del_hash(self):          #удаление хеша
        self.hash_name=None
        for i in self.keys:
            i.del_key()
        self.keys=None                      #удаление хеша
    def del_key_number(self,number): #удаление ключа по номерному порядку
        if number>len(self.keys)-1:
            return False
        self.keys[number].del_key()
        self.keys.pop(number)
        return True         #удаление ключа по номерному порядку
    def del_key_text(self,text): #удаление ключа по содержанию
        x=self.search_key(text)
        if x>=0:
            self.keys[x].del_key()
            self.keys.pop(x)
            return True
        else:
            return False             #удаление ключа по содержанию
    def search_key(self,text):                  #поиск ключа по содержанию
        for i in range(len(self.keys)):
            if self.keys[i].key_name==str(text):
                return i
        return -1               #поиск ключа по содержанию
    def return_value_key(self,text):
        x=self.search_key(text)
        if x>=0:
            return self.keys[x].pair
        return None         #выводит значение данного ключа

class hash_table:
    def __init__(self,name='Name_table'):
        self.hash_table_name=str(name)      #название хеш-таблицы
        self.hash_table=[]        #хранит список кеша  :hash
    def search_hash(self,text):                  #поиск hash
        for i in range(len(self.hash_table)):
            if self.hash_table[i].hash_name==str(text):
                return i
        return -1                   #поиск hash
    def add_hash(self,name_hash,name_key,pair):    #добавляет пару в хеш-таблицу
        x=self.search_hash(name_hash)
        if x>=0:
            self.hash_table[x].add_key(name_key,pair)
        else:
            self.hash_table.append(hash(name_hash,name_key,pair))   #добавляет пару в хеш-таблицу
    def del_hash_table(self):          #удаление хеш-таблицы
        self.hash_table_name=None
        for i in self.hash_table:
            i.del_hash()
        self.hash_table=None                     #удаление хеш-таблицы
    def return_key(self,name_hash,name_key):
        x=search_hash(name_hash)
        if x>=0:
            y=self.hash_table[x].keys.search_key(name_key)
            if y>=0:
                return self.hash_table[x].keys[y].pair
        return -1      #получение доступа к элементу памяти
    def hist(self):
        index = [i for i in range(len(self.hash_table))]
        values = []
        for key in self.hash_table:
            if len(key.keys):
                values.append(len(key.keys))
            else:
                values.append(0)
        plt.bar(index, values)
        plt.show()
    def view(self):
        print(self.hash_table_name)
        t1='-'
        t2='--'
        t3='---'
        for i in self.hash_table:
            print(t1+str(i.hash_name))#хеш - значение
            for j in i.keys:
                print(t2+str(j.key_name))#ключ - значение
                print(t3,j.pair.id,j.pair.text)

if __name__ == '__main__':
    pair=[None]
    if pair[0]:
        print(pair)
    hash_tab=hash_table()
    #id_text=0
    with open('text6.txt', 'r') as file:
        for index, word in enumerate(file.read().split(' ')):
            word = ''.join(letter for letter in word if letter.isalpha())
            #print(index, word)
     #       id_text+=1
            hash_tab.add_hash(MyHashFunction(word),word,[word,index])
    hash_tab.view()
    hash_tab.hist()
    print('')
    print('------------------------------')
    print('')
    key2=hash_table()
    with open('text6.txt', 'r') as file:
        for index, word in enumerate(file.read().split(' ')):
            word = ''.join(letter for letter in word if letter.isalpha())
            #print(index, word)
            key2.add_hash(MyHashNumberFunction(word),word,[word,index])
    key2.view()
    key2.hist()
    print('')
    print('------------------------------')
    print('')
    key3=hash_table()
    with open('text6.txt', 'r') as file:
        for index, word in enumerate(file.read().split(' ')):
            word = ''.join(letter for letter in word if letter.isalpha())
            #print(index, word)
            key3.add_hash(HashMD5(word),word,[word,index])
    key3.view()
    key3.hist()


import theano.tensor as tt
import lasagne as lnn

from . import dnn

def compute_loss(prediction, target):
    # need to clip predictions for numerical stability
    eps = 1e-7
    pred_clip = tt.clip(prediction, eps, 1.-eps)
    return lnn.objectives.binary_crossentropy(pred_clip, target).mean()

def build_net(in_shape, out_size_chroma, out_size, model):
    # first, stack the dnn chroma extractor
    chroma_network, input_var, crm_target_var = dnn.build_net(
        in_shape, out_size_chroma, model
    )

    # then, add the logistic regression chord classifier
    crd_target_var = tt.matrix('target_output', dtype='float32')

    chord_network = lnn.layers.DenseLayer(
        chroma_network, name='chords', num_units=out_size,
        nonlinearity=lnn.nonlinearities.softmax)

    # tag chord classification parameters so we can distinguish them later
    for p in chord_network.get_params():
        chord_network.params[p].add('chord')

    return (chroma_network, chord_network,
            input_var, crm_target_var, crd_target_var)

def build_model(in_shape, out_size_chroma, out_size, model):
    (crm, crd, inv, crmv, crdv) = build_net(in_shape, out_size_chroma,
                                            out_size, model)
    return dict(chroma_network=crm, chord_network=crd,
                input_var=inv, chroma_target_var=crmv, chord_target_var=crdv,
                chroma_loss_fn=compute_loss,
                chord_loss_fn=dnn.categorical_crossentropy)

create_iterators = dnn.create_iterators

def add_sacred_config(ex):

    # =============================================================== dense net

    ex.add_named_config(
        name='dense_net',
        datasource=dict(
            context_size=7,
        ),
        chroma_network=dict(
            model=dict(
                type='chroma_dnn',
                dense=dict(
                    num_layers=3,
                    num_units=512,
                    dropout=0.5,
                    nonlinearity='rectify',
                    batch_norm=False,
                ),
                out_nonlinearity='sigmoid'
            ),
            optimiser=dict(
                name='adam',
                params=dict(
                    learning_rate=0.0001
                ),
                schedule=None
            ),
            training=dict(
                iterator='BatchIterator',
                batch_size=512,
                num_epochs=500,
                early_stop=20,
                early_stop_acc=False,
            ),
            regularisation=dict(
                l2=1e-4,
                l1=0.0,
            ),
        ),
        optimiser=dict(
            name='adam',
            params=dict(
                learning_rate=0.001
            ),
            schedule=None
        ),
        training=dict(
            iterator='BatchIterator',
            batch_size=512,
            num_epochs=500,
            early_stop=20,
            early_stop_acc=True
        ),
        regularisation=dict(
            l2=1e-4,
            l1=0.0,
        ),
        testing=dict(
            test_on_val=False,
            batch_size=None
        )
    )


import matplotlib.pyplot as plt

from sdaudio.callables import Circular
from sdaudio.callables import Constant
from sdaudio import draw
from sdaudio import wavio
from sdaudio.wt_oscillators import Choruses

def main():

    #-------------------------------------------------------------------------
    # sawtooth demo

    print("Generating 60 Hz sawtooth, no chorus")

    sr = 8000
    dur = 7.0

    freqs = draw.line(sr, dur, 60, 60)

    x = draw.sawtooth(sr, dur, Circular(freqs), n = 5)

    plt.figure()
    plt.plot(x)
    plt.xlim([0, 3000])
    plt.grid(True)
    plt.title('Sawtooth, n = 5, no chorus')

    fout = 'saw-no-chorus.wav'

    print("Writing: %s" % fout)

    wavio.write(fout, 0.666 * x, sr)

    #-------------------------------------------------------------------------
    # sawtooth oscillator with chorus

    print("Generating 60 Hz sawtooth, with chorus")

    table = draw.sawtooth(sr, 1.0, Constant(1.0))

    chorus = [0.99, 1.0, 1.01]

    chorus = [0.97, 1.0, 1.03]

    chorus = [0.991234134, 1.012983475290375]

    gen = Choruses(sr, table, chorus)

    x = gen.generate(dur, Circular(freqs))

    plt.figure()
    plt.plot(x)
    plt.xlim([0, 3000])
    plt.grid(True)
    plt.title('Sawtooth, n = 5, with chorus')

    fout = 'saw-with-chorus.wav'

    print("Writing: %s" % fout)

    wavio.write(fout, 0.666 * x, sr)

    #-------------------------------------------------------------------------
    # freq ramp

    print("Generating sawtooth ramp, no chorus")

    freqs = draw.line(sr, dur, 40, 200)

    x = draw.sawtooth(sr, dur, Circular(freqs))

    fout = 'saw-ramp-no-chorus.wav'

    print("Writing: %s" % fout)

    wavio.write(fout, 0.666 * x, sr)

    print("Generating sawtooth ramp, with chorus")

    x = gen.generate(dur, Circular(freqs))

    fout = 'saw-ramp-with-chorus.wav'

    print("Writing: %s" % fout)

    wavio.write(fout, 0.666 * x, sr)

    plt.show()

if __name__ == "__main__":
    main()


import requests
import bs4

response = requests.get("https://yandex.ru/pogoda/")
print(response.status_code)

tree = bs4.BeautifulSoup(response.text, 'html.parser')

for item in tree.select(".card"):
    obj = item.select_one('.title-icon__text')
    if (obj is not None):
        print(obj.text)

count = 0
for item in tree.select(".forecast-briefly__day"):
    res = item.find('a').attrs['aria-label']
    if (count > 2) & (count < 9):
        print(res)
    count+=1


import os
import time
import pytest
from models import Event
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from app import create_app

SECRET = 'Isecret'
DB_NAME = 'test.db'

@pytest.fixture
def client():
    db_url = "sqlite:///" + DB_NAME
    app = create_app(SECRET, db_url)
    app.testing = True
    yield app.test_client()
    os.unlink(DB_NAME)

@pytest.fixture
def sess():
    db_url = "sqlite:///" + DB_NAME
    engine = create_engine(db_url)
    sess = sessionmaker(bind=engine)
    yield sess()

def _test_status(code, resp):
    assert resp.status_code == code

ROUTES = [
    ('/query_name', 'post'),
    ('/add_event', 'put')
]

def test_no_auth(client):
    for route, method in ROUTES:
        _test_status(401, getattr(client, method)(route))

def test_bad_auth(client):
    for route, method in ROUTES:
        _test_status(401, getattr(client, method)(route, data={'SECRET_KEY':'sup'}))

def test_good_auth(client):
    for route, method in ROUTES:
        _test_status(200, getattr(client, method)(route,
                                                  data={'SECRET_KEY':SECRET,
                                                        'name':"haha"}))
def test_count(client):
    data = {'SECRET_KEY':SECRET,'name':"haha"}
    for i in range(3):
        client.put('/add_event', data=data)
    assert client.post('/query_name', data=data).json == 1
    time.sleep(1)
    data['window'] = 1e-7
    assert client.post('/query_name', data=data).json == 0

def test_multiple_keys(client):
    data = {'SECRET_KEY':SECRET}
    names = ['haha', 'sup', 'todo']
    for name in names:
        data['name'] = name
        client.put('/add_event', data=data)
    for name in names:
        data['name'] = name
        assert client.post('/query_name', data=data).json == 1

def test_unique(client, sess):
    """test failure"""
    data = {'SECRET_KEY':SECRET,'name':"haha"}
    events = [Event(name=data['name'], time=Event.gen_dt(i % 3))
              for i in range(6)]
    sess.add_all(events)
    sess.commit()
    assert client.post('/query_name', data=data).json == 3


import os
import cv2 as cv
import numpy as np
import labeling as lbl

for _i in range(1,4):
    prefix = 'frame_1_{:02d}'.format(_i)
    img = cv.imread('frames/{:s}.png'.format(prefix),0)
    tile_size = 16
    tile_num_x = 77
    tile_num_y = 77
    _path = 'data/'+prefix+'_data_'+str(tile_size)
    if os.path.isdir(_path) is False:
        os.makedirs(_path)
    # cv.imshow('original',img)
    img = cv.medianBlur(img,21)

    print(_i)
    # get border from user and save it
    selected_border = lbl.input(img).getPoints()
    cv.imwrite(_path+'/selected_border.png',np.array(selected_border*255,dtype=np.uint8))

    # get border from saved file and tile and class the data
    selected_border = cv.imread(_path+'/selected_border.png',0)/255
    lbl.tile_and_save(img,tile_size,selected_border, _path)


#!/bin/bash
from plan2scene.config_manager import ConfigManager
import argparse
import os
import os.path as osp
import glob
import subprocess
import logging

if __name__ == "__main__":
    """
    Preview GNN preditions for every nth epoch.
    All surfaces (including observed surfaces) are assigned a propagated texture computed using texture embeddings of neighboring surfaces.
    """
    conf = ConfigManager()
    parser = argparse.ArgumentParser(
        description="Preview GNN preditions for every nth epoch. All surfaces are assigned the GNN prediction (including observed surfaces).")
    conf.add_args(parser)
    parser.add_argument("output_path", type=str, help="Output directory which will contain the preview.html file.")
    parser.add_argument("interval", type=int, help="Interval between previewed epochs.")
    parser.add_argument("gnn_train_path", type=str,
                        help="Path to GNN train directory. This directory has the conf directory and checkpoints directory.")
    args = parser.parse_args()
    conf.process_args(args, output_is_dir=True)

    output_path = args.output_path
    gnn_train_path = args.gnn_train_path
    interval = args.interval
    conf_path = osp.join(gnn_train_path, "conf", "texture_prop.json")
    checkpoints_path = osp.join(gnn_train_path, "checkpoints")

    assert osp.exists(conf_path)
    assert osp.exists(checkpoints_path)

    current_checkpoint_id = interval
    preview_images = None

    with open(osp.join(output_path, "preview.html"), "w") as f:
        f.write("
\n")
        while True:
            candidates = glob.glob(osp.join(checkpoints_path, f"loss-*-epoch-{current_checkpoint_id}.ckpt"))
            if len(candidates) == 0:
                break
            candidate = candidates[0]
            logging.info(f"Processing {candidate}")

            command = "/bin/bash ./code/scripts/plan2scene/texture_prop/preview_all_prop.sh {output_path} {conf_path} {checkpoint_path}".format(
                output_path=osp.join(output_path, f"epoch_{current_checkpoint_id}"),
                conf_path=conf_path,
                checkpoint_path=candidate
            )
            my_env = os.environ.copy()
            assert subprocess.call(command, shell=True, env=my_env) == 0

            # Preview
            if preview_images is None:
                preview_images = os.listdir(osp.join(output_path, f"epoch_{current_checkpoint_id}", "archs"))
                preview_images = [a for a in preview_images if a.endswith(".png")]
                f.write("
\n")
                f.write("Epoch	\n")
                for preview_image in preview_images:
                    f.write(f"{preview_image}	\n")

                f.write("
\n")

            f.write("
\n")
            f.write(f"{current_checkpoint_id}	\n")

            for preview_image in preview_images:
                f.write("\n")
                f.write("".format(img_path=osp.join(f"epoch_{current_checkpoint_id}", "archs", preview_image)))
                f.write("	\n")

            f.write("
\n")
            f.flush()

            current_checkpoint_id += interval
        f.write("
\n")


from globals import MAP_SIZE, MAP_WINDOW_SIZE, WORLD_INFO, SETTINGS, LIFE

import graphics as gfx

import bad_numbers
import maps

import threading
import logging
import time

class ChunkHandler(threading.Thread):
	def __init__(self, check_every=5):
		threading.Thread.__init__(self)

		self.last_checked = -check_every
		self.check_every = check_every
		self.load_clusters = []

	def check_chunks(self, force=False):
		if not force and WORLD_INFO['ticks']-self.last_checked<self.check_every:
			return False

		self.last_checked = WORLD_INFO['ticks']

		for life in [l for l in LIFE.values() if l['online']]:
			_x_min = bad_numbers.clip(life['pos'][0]-MAP_WINDOW_SIZE[0], 0, MAP_SIZE[0]-1-MAP_WINDOW_SIZE[0])
			_y_min = bad_numbers.clip(life['pos'][1]-MAP_WINDOW_SIZE[1], 0, MAP_SIZE[1]-1-MAP_WINDOW_SIZE[1])
			_x_max = bad_numbers.clip(life['pos'][0]+MAP_WINDOW_SIZE[0], 0, MAP_SIZE[0]-1)
			_y_max = bad_numbers.clip(life['pos'][1]+MAP_WINDOW_SIZE[1], 0, MAP_SIZE[1]-1)
			_refresh = False

			for y in range(_y_min, _y_max, WORLD_INFO['chunk_size']):
				for x in range(_x_min, _x_max, WORLD_INFO['chunk_size']):
					maps.load_cluster_at_position_if_needed((x, y))

					SETTINGS['loading'] = True

					if 'player' in life:
						_refresh = True

			if _refresh:
				gfx.refresh_view('map')

		SETTINGS['loading'] = False

	def load_cluster(self, cluster_key):
		self.load_clusters.append(cluster_key)

	def run(self):
		while SETTINGS['running'] and not SETTINGS['kill threads']:
			self.check_chunks()
			time.sleep(1)

		logging.debug('ChunkHandler thread shutting down.')

def init():
	SETTINGS['chunk_handler'] = ChunkHandler()
	SETTINGS['chunk_handler'].start()


from abc import ABCMeta, abstractmethod
from functools import wraps
from typing import Callable, Iterable, List, Set, Union, Any

from ...errors import SlackObjectFormationError

class BaseObject:
    def __str__(self):
        return f"<slack.{self.__class__.__name__}>"

class JsonObject(BaseObject, metaclass=ABCMeta):
    @property
    @abstractmethod
    def attributes(self) -> Set[str]:
        """
        Provide a set of attributes of this object that will make up its JSON structure
        """
        return set()

    def validate_json(self) -> None:
        """
        Raises:
          SlackObjectFormationError if the object was not valid
        """
        for attribute in (func for func in dir(self) if not func.startswith("__")):
            method = getattr(self, attribute)
            if callable(method) and hasattr(method, "validator"):
                method()

    def get_non_null_attributes(self) -> dict:
        """
        Construct a dictionary out of non-null keys (from attributes property)
        present on this object
        """
        return {
            key: getattr(self, key, None)
            for key in sorted(self.attributes)
            if getattr(self, key, None) is not None
        }

    def to_dict(self, *args: Any) -> dict:
        """
        Extract this object as a JSON-compatible, Slack-API-valid dictionary

        Args:
          *args: Any specific formatting args (rare; generally not required)

        Raises:
          SlackObjectFormationError if the object was not valid
        """
        self.validate_json()
        return self.get_non_null_attributes()

    def __repr__(self):
        json = self.to_dict()
        if json:
            return f"<slack.{self.__class__.__name__}: {json}>"
        else:
            return self.__str__()

class JsonValidator:
    def __init__(self, message: str):
        """
        Decorate a method on a class to mark it as a JSON validator. Validation
            functions should return true if valid, false if not.

        Args:
            message: Message to be attached to the thrown SlackObjectFormationError
        """
        self.message = message

    def __call__(self, func: Callable) -> Callable[..., None]:
        @wraps(func)
        def wrapped_f(*args, **kwargs):
            if not func(*args, **kwargs):
                raise SlackObjectFormationError(self.message)

        wrapped_f.validator = True
        return wrapped_f

class EnumValidator(JsonValidator):
    def __init__(self, attribute: str, enum: Iterable[str]):
        super().__init__(
            f"{attribute} attribute must be one of the following values: "
            f"{', '.join(enum)}"
        )

def extract_json(
    item_or_items: Union[JsonObject, List[JsonObject], str], *format_args
) -> Union[dict, List[dict], str]:
    """
    Given a sequence (or single item), attempt to call the to_dict() method on each
    item and return a plain list. If item is not the expected type, return it
    unmodified, in case it's already a plain dict or some other user created class.

    Args:
      item_or_items: item(s) to go through
      format_args: Any formatting specifiers to pass into the object's to_dict
            method
    """
    try:
        return [
            elem.to_dict(*format_args) if isinstance(elem, JsonObject) else elem
            for elem in item_or_items
        ]
    except TypeError:  # not iterable, so try returning it as a single item
        return (
            item_or_items.to_dict(*format_args)
            if isinstance(item_or_items, JsonObject)
            else item_or_items
        )


import jinja2
#
# Create a Jinja2 environment, using the file system loader
# to be able to load templates from the local file system
#
env = jinja2.Environment(
    loader=jinja2.FileSystemLoader('.')
)
#
# Load our template
#
template = env.get_template('index.html.j2')
#
# Prepare the input variables, as Ansible would to it (use ansible -m setup to see
# how this structure looks like, you can even copy the JSON output)
#
groups = {'all': ['127.0.0.1', '192.168.33.44']}
ansible_facts={
        "all_ipv4_addresses": [
            "10.0.2.15",
            "192.168.33.10"
        ],
        "env": {
            "HOME": "/home/vagrant",
        },
        "interfaces": [
            "enp0s8"
        ],
        "enp0s8": {
            "ipv4": {
                "address": "192.168.33.11",
                "broadcast": "192.168.33.255",
                "netmask": "255.255.255.0",
                "network": "192.168.33.0"
            },
            "macaddress": "08:00:27:77:1a:9c",
            "type": "ether"
        },
}
#
# Render the template and print the output
#
print(template.render(groups=groups, ansible_facts=ansible_facts))