#!/usr/bin/env python3
# modifier: phien.ngo
import os
import re
from argparse import ArgumentParser
import json
import glob
import sys

AGNI_KEYWORD_TEMP_FILES = 'agni_keywords.json'
TESTCASE_TEMPLATE_FILE = 'tcs.yaml'
CONFIG_FILE = 'config.json'
with open(CONFIG_FILE) as json_file:
  config_data = json.load(json_file)

def detectCommentLine(yaml_content, format_structure):
  regex = r"(#.*)\n([\s]{0,})(.*)\n"
  matches = re.finditer(regex, yaml_content, re.MULTILINE)
  for matchNum, match in enumerate(matches, start=1):
    if len(match.groups()) >= 2:
      comment_line = match.group(1)
      space_of_next_line = match.end(2) - match.start(2)
      next_line = match.group(3)
      if next_line + '@None' in format_structure:
        format_structure[comment_line.strip() + '@None'] = format_structure[next_line + '@None']
      else:
        format_structure[comment_line.strip() + '@None'] = space_of_next_line

  return format_structure

def detect_sub_block_by_words(yaml_content, format_structure, begin_from_list_words, end_word,
                              detect_character_per_line, space):
  found = False
  i = 0
  for line in yaml_content.split('\n'):
    i = i + 1
    if line.strip() in begin_from_list_words:
      found = True
      continue
    if end_word is not None:
      if found and end_word not in line:
        format_structure[line.strip() + '@' + str(i)] = space
      if end_word in line:
        found = False
        continue
    if detect_character_per_line is not None:
      if found and line.__contains__(detect_character_per_line):
        format_structure[line.strip() + '@' + str(i)] = space
      else:
        found = False
        continue

  return format_structure

def detect_file(yaml_content, format_structure):
  # detect testcase name
  testcase_name_list = re.findall('\s*(.*:)\s*\n*common_variables:', yaml_content)
  for item in testcase_name_list:
    format_structure[item.strip() + '@None'] = config_data['indent']['level_2']
  # detect all common variables
  format_structure = detect_sub_block_by_words(yaml_content, format_structure,
                                               ['common_variables:'], 'steps:', None, config_data['indent']['level_4'])
  # detect index step
  index_step_list = re.findall('\n[\s]{0,}(\d+:)\n', yaml_content)
  for item in index_step_list:
    format_structure[item.strip() + '@None'] = config_data['indent']['level_4']
  # detect stepname
  index_stepname_list = re.findall('\n[\s]{0,}\d+:[\s]{0,}(.*)\n', yaml_content)
  for item in index_stepname_list:
    format_structure[item.strip() + '@None'] = config_data['indent']['level_5']
  # detect comment line
  format_structure = detectCommentLine(yaml_content, format_structure)
  # detect sub command of run_event, run_keyword, create_dictionary_and_get,..
  format_structure = detect_sub_block_by_words(yaml_content, format_structure,
                                               ['run_event:', 'run_keyword:', 'create_dictionary_and_get:',
                                                'create_dictionary_and_check:'], 'unique_id', None, config_data['indent']['level_7'])
  # detect sub command of checks options
  format_structure = detect_sub_block_by_words(yaml_content, format_structure,
                                               ['checks:'], None, '- ', config_data['indent']['level_9'])
  # detect sub command of loop_over_list options
  format_structure = detect_sub_block_by_words(yaml_content, format_structure,
                                               ['loop_over_list:'], None, '- ', config_data['indent']['level_8'])

  return format_structure

def addBlankLine(yaml_content):
  contentToWrite = ''
  i=0
  found_comment = False
  for line in yaml_content.split('\n'):
    i=i+1
    if i==2:
      contentToWrite = contentToWrite + '\n' + line + '\n'
    else:
      comment_pattern = r'^\s{4}[\#]'
      if re.match(comment_pattern, line): # detect comment line
        contentToWrite = contentToWrite + '\n\n' + line + '\n'
        found_comment = True
        continue
      else:
        # detect testcase name
        step_pattern = r'^\s{4}[\w|\d]'
        if found_comment==False and re.match(step_pattern, line):
          contentToWrite = contentToWrite + '\n\n' + line + '\n'
        else:
          contentToWrite = contentToWrite + line + '\n'
          found_comment = False

  return  contentToWrite

def update_unique_ids_and_format(yaml_file=None, uid=1):
  directory, filename = os.path.split(yaml_file)
  format_structure = {
    "Granular_tests:@None": config_data['indent']['level_1'],
    "common_variables:@None": config_data['indent']['level_3'],
    "steps:@None": config_data['indent']['level_3'],
    "devices: device@None": config_data['indent']['level_7'],
    "checks:@None": config_data['indent']['level_7'],
    "loop_over_list:@None": config_data['indent']['level_7']
  }

  unique_ids_mapping = {}
  file_name_obj_arr = config_data['testcase_filename'].split('|')
  for item in  file_name_obj_arr:
    item_arr = item.split('@')
    unique_ids_mapping[item_arr[0]]= item_arr[1]

  yaml_file_name = os.path.split(yaml_file)[-1]

  if yaml_file_name in unique_ids_mapping.keys():
    uid = unique_ids_mapping[yaml_file_name]
  with open(yaml_file, 'r') as fr:
    yaml_content = fr.read()
  format_structure = detect_file(yaml_content, format_structure);
  counter = int(uid)
  steps_counter = 1
  fw = open('output/'+filename, 'w')
  i = 0
  contentToWrite = ''
  for line in yaml_content.split('\n'):
    i = i + 1
    if 'steps:' in line:
      steps_counter = 1
    if 'run_keyword:' in line or 'run_event:' in line \
            or 'create_dictionary_and_get' in line or 'create_dictionary_and_check' in line:
      format_structure[line.strip() + '@' + str(i)] = config_data['indent']['level_6']
    if 'unique_id:' in line:
      line = line.split(':')[0] + ': ' + str(counter)
      counter += 1
      format_structure[line.strip() + '@' + str(i)] = config_data['indent']['level_6']
    if re.search('^\s*\d+\:$', line):
      spaces = config_data['indent']['level_4']
      line = ' ' * spaces + str(steps_counter) + ':'
      steps_counter += 1

    # format indent for the line
    line_content = line.strip();
    if line_content + '@None' in format_structure.keys():
      line = ' ' * format_structure[line_content + '@None'] + line_content
    else:
      if line_content + '@' + str(i) in format_structure.keys():
        line = ' ' * format_structure[line_content + '@' + str(i)] + line_content

    if line.strip()!='':
      contentToWrite = contentToWrite + line + '\n'

  contentToWrite = addBlankLine(contentToWrite)
  fw.write(contentToWrite.strip())
  fw.close()

def generateStep(arrStepObjs, file_name, testcase_name, username):
  stepsToWrite = getHeaderContent(testcase_name, username)
  for stepObj in arrStepObjs:
    if stepObj != '':
      arrItem = stepObj.split('#')
      data = {'step_name': arrItem[0], 'keyword': arrItem[1]}
      if len(arrItem) >= 3:
        data['sub-keyword'] = arrItem[2]
      stepsToWrite = getStepContent(data, stepsToWrite)
  pth_of_file = 'output/test_case_generate.yaml'
  if file_name is not None:
    pth_of_file = 'output/' + file_name
  fw = open(pth_of_file, 'w')
  fw.write(stepsToWrite.strip())
  fw.close()
  update_unique_ids_and_format(pth_of_file)

def getHeaderContent(testcase_name, username):
  result = ''
  with open(TESTCASE_TEMPLATE_FILE, 'r') as fr:
    yaml_content = fr.read()

  write_code_before_step = True
  for line in yaml_content.split('\n'):
    if line.strip() == 'steps:':
      write_code_before_step = False
      result = result + line + '\n'
    if write_code_before_step == True:
      if testcase_name is not None:
        line = line.replace('testcase_name', testcase_name)
      if username is not None:
        line = line.replace('owner_name', username)
      result = result + line + '\n'
  return result

def getStepContent(stepObj, result):
  agni_keyword_data = {}
  with open(AGNI_KEYWORD_TEMP_FILES) as json_file:
    agni_keyword_data = json.load(json_file)['content']

  with open(TESTCASE_TEMPLATE_FILE, 'r') as fr:
    yaml_content = fr.read()

  found = False
  for line in yaml_content.split('\n'):
    # detect step to write
    if re.sub(r"#|@", "", line).strip() == stepObj['keyword']:
      found = True
      continue
    if found and '#####' not in line:
      line = line.replace('step_name', stepObj['step_name'])
      if 'sub-keyword' in stepObj.keys():
        if stepObj['sub-keyword'] in agni_keyword_data.keys():
          line = line.replace('<<sub-keyword>>', agni_keyword_data[stepObj['sub-keyword']])
        else:
          line = line.replace('<<sub-keyword>>', stepObj['sub-keyword'])
      result = result + line + '\n'
    if "#####" in line:
      found = False
      continue
  return result

def readAgniKeyword():
  agni_directory_path = config_data['agni_path'] + '\\'
  list_file_paths = glob.glob(agni_directory_path + "*.yaml")

  yaml_contents = ''
  for file_path in list_file_paths:
    with open(file_path, 'r') as fr:
      yaml_content = fr.read()
      yaml_contents = yaml_contents + '\n' + yaml_content

  list_keywords = re.findall('keyword:(.*)\n', yaml_contents)
  keywords_without_run_event = {}
  for keyword in list_keywords:

    if not keyword.strip().lower() == 'on config' and not keyword.strip().lower() == 'on cli':
      keyword_name = ''
      keyword = keyword.strip()
      keyword_content = re.sub(r"\'", "", keyword)
      keyword_splited_arr = re.split(r'\s{2,}', keyword_content)
      if len(keyword_splited_arr) == 1:
        keyword_name = keyword_splited_arr[0].strip().lower()
      else:
        str = re.sub(r"\$|\'|\"", "", keyword_content)
        for item in re.split(r'\s{2,}', str):
          if not '{' in item and not '=' in item:
            keyword_name = item.strip().lower()
            break
      if not keyword_name == '':
        if not keywords_without_run_event:
          if keyword_name not in keywords_without_run_event.keys():
            keywords_without_run_event[keyword_name] = keyword_content
        else:
          keywords_without_run_event[keyword_name] = keyword_content
  data = {
    'keywords': [],
    'content': keywords_without_run_event
  }
  if sys.version_info[0] < 3:
    data['keywords'] = keywords_without_run_event.keys()
  else:
    for i in keywords_without_run_event.keys():
      data['keywords'].append(i)

  with open(AGNI_KEYWORD_TEMP_FILES, 'w') as outfile:
    json.dump(data, outfile)

def main():
  argparser = ArgumentParser('python main.py -yaml=<yaml_file>')
  argparser.add_argument('-f', '--yaml_file', default=None, help='yaml file to update and format indent')
  argparser.add_argument('-u', '--unique_id', default=1, help='unique id to start for file')
  argparser.add_argument('-s', '--listStep', nargs='+', default=None,
                         help='String of Step in format step#keyword step1#keyword1')
  argparser.add_argument('-tn', '--testcase_name', default=None, help='Name of testcase')
  argparser.add_argument('-fn', '--file_name', default=None, help='File name of testcase')
  argparser.add_argument('-usr', '--username', default=None, help='User name')
  argparser.add_argument('-r', '--ragni', default=None, help='any value to read agni keyword from directory')
  args = argparser.parse_args()
  yaml_file = args.yaml_file
  start_unique_id = args.unique_id
  listStep = args.listStep
  testcase_name = args.testcase_name
  file_name = args.file_name
  username = args.username
  ragni = args.ragni
  if not os.path.isdir('output'):
    os.mkdir('output')
  if yaml_file is not None and start_unique_id is not None:
    update_unique_ids_and_format(yaml_file=yaml_file, uid=start_unique_id)
  if listStep is not None:
    generateStep(listStep, file_name, testcase_name, username)
  if ragni is not None:
    readAgniKeyword()

if __name__ == '__main__':
  main()


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

import tempfile

import ddt
import mock

from rally.plugins.openstack.scenarios.glance import utils
from tests.unit import test

GLANCE_UTILS = "rally.plugins.openstack.scenarios.glance.utils"

@ddt.ddt
class GlanceScenarioTestCase(test.ScenarioTestCase):

    def setUp(self):
        super(GlanceScenarioTestCase, self).setUp()
        self.image = mock.Mock()
        self.image1 = mock.Mock()
        self.scenario_clients = mock.Mock()
        self.scenario_clients.glance.choose_version.return_value = 1

    def test_list_images(self):
        scenario = utils.GlanceScenario(context=self.context)
        return_images_list = scenario._list_images()
        self.clients("glance").images.list.assert_called_once_with()
        self.assertEqual(list(self.clients("glance").images.list.return_value),
                         return_images_list)
        self._test_atomic_action_timer(scenario.atomic_actions(),
                                       "glance.list_images")

    @ddt.data({},
              {"name": "foo"},
              {"name": None},
              {"name": ""},
              {"name": "bar", "fakearg": "fakearg"},
              {"fakearg": "fakearg"})
    @mock.patch("rally.plugins.openstack.wrappers.glance.wrap")
    def test_create_image(self, create_args, mock_wrap):
        image_location = tempfile.NamedTemporaryFile()
        mock_wrap.return_value.create_image.return_value = self.image
        scenario = utils.GlanceScenario(context=self.context,
                                        clients=self.scenario_clients)
        scenario.generate_random_name = mock.Mock()

        return_image = scenario._create_image("container_format",
                                              image_location.name,
                                              "disk_format",
                                              **create_args)

        expected_args = dict(create_args)
        if not expected_args.get("name"):
            expected_args["name"] = scenario.generate_random_name.return_value

        self.assertEqual(self.image, return_image)
        mock_wrap.assert_called_once_with(scenario._clients.glance, scenario)
        mock_wrap.return_value.create_image.assert_called_once_with(
            "container_format", image_location.name, "disk_format",
            **expected_args)
        self._test_atomic_action_timer(scenario.atomic_actions(),
                                       "glance.create_image")

    @mock.patch("rally.plugins.openstack.wrappers.glance.wrap")
    def test_delete_image(self, mock_wrap):
        scenario = utils.GlanceScenario(context=self.context,
                                        clients=self.scenario_clients)
        scenario._delete_image(self.image)
        mock_wrap.assert_called_once_with(scenario._clients.glance, scenario)
        mock_wrap.return_value.delete_image.assert_called_once_with(self.image)
        self._test_atomic_action_timer(scenario.atomic_actions(),
                                       "glance.delete_image")


import os
import numpy as np
import csv

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense
from sklearn.metrics import classification_report

# split a univariate sequence

def split_sequence(sequence, n_steps):
    X, y = list(), list()
    for i in range(len(sequence)):
        # find the end of this pattern
        end_ix = i + n_steps
        # check if we are beyond the sequence
        if end_ix > len(sequence)-1:
            break
        # gather input and output parts of the pattern
        seq_x, seq_y = sequence[i:end_ix], sequence[end_ix]
        X.append(seq_x)
        y.append(seq_y)
    return np.array(X), np.array(y)

# configuration
n_steps = 6
train_split = 0.25
epochs = 100

# read data
data = []
for file in os.listdir('./data'):
    filename = os.fsdecode(file)
    arr = np.genfromtxt('./data/' + filename, delimiter=',')
    if len(arr) > n_steps:
        data.append(arr)

# construct network
model = Sequential()
model.add(LSTM(50, activation='relu',
               return_sequences=True, input_shape=(n_steps, 1)))
model.add(LSTM(50, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
              optimizer='adam', metrics=['accuracy'])

# train
sep = int(len(data) * train_split)

for i in range(epochs):
    for instance in data[:sep]:
        X, y = split_sequence(instance, n_steps)
        X = X.reshape((X.shape[0], X.shape[1], 1))
        model.fit(X, y, epochs=1, verbose=1)

# test
pred = []
truth = []
for instance in data[sep:]:
    X, y = split_sequence(instance, n_steps)
    X = X.reshape((X.shape[0], X.shape[1], 1))
    pred.append(model.predict(X, verbose=0))
    truth.append(y)

pred = np.round(np.vstack(pred))
truth = np.concatenate(truth).reshape(-1, 1)

print(classification_report(truth, pred))


import re
from nltk.stem import WordNetLemmatizer
from nltk.stem import PorterStemmer
from functools import reduce
import database as db
import file_manager as fm

stop_words = ["i", "me", "my", "myself", "we", "our", "ours", "ourselves", "you", "your", "yours", "yourself",
              "yourselves", "he", "him", "his", "himself", "she", "her", "hers", "herself", "it", "its", "itself",
              "they", "them", "their", "theirs", "themselves", "what", "which", "who", "whom", "this", "that", "these",
              "those", "am", "is", "are", "was", "were", "be", "been", "being", "have", "has", "had", "having", "do",
              "does", "did", "doing", "a", "an", "the", "and", "but", "if", "or", "because", "as", "until", "while",
              "of", "at", "by", "for", "with", "about", "against", "between", "into", "through", "during", "before",
              "after", "above", "below", "to", "from", "up", "down", "in", "out", "on", "off", "over", "under", "again",
              "further", "then", "once", "here", "there", "when", "where", "why", "how", "all", "any", "both", "each",
              "few", "more", "most", "other", "some", "such", "no", "nor", "not", "only", "own", "same", "so", "than",
              "too", "very", "s", "t", "can", "will", "just", "don", "should", "now"]

def get_document(path, filename, doc_id, t):
    db.connect_to_database("IR_database")
    db.save_filename(doc_id, filename+".txt")
    global stop_words
    file = open(path+filename+".txt", t)
    counter = 0
    document = []
    for line in file:
        field = re.findall(r"\w+", line)
        for word in field:
            counter += 1
            word = word.lower()
            if word not in stop_words:
                key_word = (word, doc_id, counter)
                document.append(key_word)
    return document

def normalization(word):
    lemmatizer = WordNetLemmatizer()
    ps = PorterStemmer()

    term = lemmatizer.lemmatize(word)
    term = ps.stem(term)
    return term

def tokenizier():
    path = "Collection/"
    # collection = ["A Dark Brown Dog" , "A Pair of Silk Stockings" , "About Love","An Angel in Disguise",
    # "An Occurrence at Owl Creek Bridge","Araby","Army",
    # "Cousin Tribulation's Story","Desiree's Baby","Ex Oblivione","Hearts And Hands","How the Camel Got His Hump",
    # "Lost Hearts","On The Day of the Crucifixion",
    # "Regret","The Brave Tin Soldier","The Cactus","The Cat","The Gift of the Magi","The Haunted Mind",
    # "The Luck of Roaring Camp","The Monkey's Paw","The Skylight Room","The Story of An Hour",
    # "The Tale of Peter Rabbit"]
    collection = fm.get_collection_names(path)
    collection_key_words = []
    for index, document in enumerate(collection):
        key_words_in_one_document = get_document(path, document, index, "r")
        #  normalized_key_words = []
        for word_info in key_words_in_one_document:
            word = normalization(word_info[0])
            collection_key_words.append((word, word_info[1], word_info[2]))
    return collection_key_words

def pre_processed_query(query):
    post_query = []
    counter = 0
    field = re.findall(r"\w+", query)
    for word in field:
        counter += 1
        word = word.lower()
        global stop_words
        if word not in stop_words:
            word = normalization(word)
            post_query.append((word, counter))
    return post_query

def find_term(word, dictionary):
    for item in dictionary:
        if item[0] == word:
            return item[1]

def get_id(post_query, dictionary):
    term_position_id = []
    for item in post_query:
        _id = find_term(item[0], dictionary)
        if _id is None:
            return -1
        position = item[1]
        term_position_id.append((position, _id))
    return term_position_id

def find_common(all_doc):
    res = list(reduce(lambda i, j: i & j, (set(x) for x in all_doc)))
    return res

def positional_intersect(pl1, pl2, d):
    answer = []
    for x, y in zip(range(len(pl1)), range(len(pl2))):
        #  tuple of first->last doc
        tuple1 = pl1[x]
        tuple2 = pl2[y]
        if tuple1[0] == tuple2[0]:
            intersection_list = []
            positions1 = tuple1[1]
            positions2 = tuple2[1]
            i = 0
            j = 0
            while i < len(positions1) and j < len(positions2):
                if positions2[j] - positions1[i] == d:
                    intersection_list.append(positions2[j])
                    i += 1
                    j += 1
                elif positions1[i] > positions2[j]:
                    j += 1
                else:
                    i += 1
            answer.append((tuple1[0], intersection_list))
    return answer

def merge(query, dictionary):
    query_terms = pre_processed_query(query)
    term_position_id = get_id(query_terms, dictionary)
    if term_position_id == -1:
        return None
    query_postings = []
    for word in term_position_id:
        posting_list = db.load_posting_list(word[1])
        query_postings.append((word[0], posting_list))
    differences = []
    for i in range(len(query_postings) - 1):
        diff = query_postings[i + 1][0] - query_postings[i][0]
        differences.append(diff)
    all_doc = []
    for item in query_postings:
        term_doc = []
        for tup in item[1]:
            term_doc.append(tup[0])
        all_doc.append(term_doc)
    common_docs = find_common(all_doc)
    common_postings = []
    for item in query_postings:
        term = []
        for tup in item[1]:
            if tup[0] in common_docs:
                term.append((tup[0], tup[1]))
        common_postings.append(term)
    result_docs = []
    if len(common_postings) > 1:
        result = positional_intersect(common_postings[0], common_postings[1], differences[0])
        for i, d in zip(range(2, len(common_postings)), range(1, len(differences))):
            result = positional_intersect(result, common_postings[i], differences[d])
        for item in result:
            if len(item[1]) != 0:
                result_docs.append(item[0])
    else:
        for term in common_postings:
            for item in term:
                if len(item[1]) != 0:
                    result_docs.append(item[0])
    return result_docs


#coding=utf-8

import sys
import click
import config
import logging
import trade_date
import numpy as np
import pandas as pd
import statsmodels.api as sm

from ipdb import set_trace

from sqlalchemy import *
from sqlalchemy.orm import sessionmaker
from sqlalchemy import MetaData,Table,select

from db.asset_fundamental import *
from db import database,asset_trade_dates,base_ra_index_nav
from calendar import monthrange
from datetime import datetime,timedelta

from statsmodels.stats.outliers_influence import summary_table

#from sklearn import datasets,linear_model
# from sklearn.cross_validation import cross_val_score, ShuffleSplit
from sklearn.preprocessing import StandardScaler,Normalizer,Binarizer,PolynomialFeatures
from sklearn.feature_selection import VarianceThreshold,SelectKBest,chi2,RFE,SelectFromModel#,MINE
from sklearn.ensemble import GradientBoostingClassifier,RandomForestRegressor,ExtraTreesClassifier
from sklearn.decomposition import PCA
from sklearn.linear_model import LinearRegression,RandomizedLasso,LogisticRegression,Ridge
#from sklearn.svm import LinearSVC

sys.path.append('shell/')
logger = logging.getLogger(__name__)

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

@click.group(invoke_without_command=True)
@click.pass_context

def ot(ctx):
    if ctx.invoked_subcommand is None:
        ctx.invoke(oil_view_update)
    else:
        pass

################   数据读入    ##############################################

def load_oil_indicator():
    feature_names = {
        'MC.GD0018': 'USnrty',
        'MC.GD0032': 'brent_f_price',
        'MC.GD0033': 'wti_f_price',
        'MC.GD0034': 'oman_f_price',
        'MC.GD0035': 'wti_FOB',
        'MC.GD0036': 'brent_FOB',
        'MC.GD0037': 'dibai_FOB',
        'MC.GD0038': 'opec_price',
        'MC.GD0039': 'nymex_long_Fund',
        'MC.GD0040': 'nymex_short_Fund',
        'MC.GD0041': 'nymex_Arbitrage',
        'MC.GD0042': 'nymex_long_Commerce',
        'MC.GD0043': 'nymex_short_Commerce',
        'MC.GD0070': 'oilproduct_worldtotal',
        'MC.GD0071': 'oilopec_total',
        'MC.GD0095': 'oildemand_worldtotal',
        'MC.GD0201': 'oil_OPEC_Pro',
        'MC.GD0202': 'oil_OECD_consum',
        'MC.GD0203': 'oil_OECD_stocks',
        'MC.GD0204': 'oil_US_stocks',
        'MC.GD0205': 'oil_US_utiliza',
        'MC.GD0206': 'oil_US_Pro',
        'MC.GD0207': 'money_US_Index',
        'MC.GD0208': 'eco_US_GDP',
        'MC.GD0209': 'oil_US_export',
        'MC.GD0210': 'oil_world_demand',
        'MC.GD0211': 'oil_US_stocks2',
        'MC.GD0212': 'pos_ICE_longC',
        'MC.GD0213': 'pos_ICE_shortC',
        'MC.GD0214': 'pos_ICE_longF',
        'MC.GD0215': 'pos_ICE_shortF',
        'MC.GD0216': 'pos_Nymex_longC',
        'MC.GD0217': 'pos_Nymex_shortC',
        'MC.GD0218': 'pos_Nymex_longF',
        'MC.GD0219': 'pos_Nymex_shortF',
        'MC.GD0220': 'price_f_brent',
        'MC.GD0221': 'price_f_wti',
        'MC.GD0222': 'price_f_oman',
        'MC.GD0223': 'price_s_brent',
        'MC.GD0224': 'price_s_wti',
        'MC.GD0225': 'price_s_dibai',
        'MC.GD0050': 'EuroDollar',
        'MC.GD0051': 'GDP-A',
        'MC.GD0052': 'CPIr-A',
        'MC.GD0053': 'M2-A',
        'MC.GD0055': 'GDP-J',
        'MC.GD0056': 'M2-J',
        'MC.GD0058': 'CPIr-J',
        'MC.GD0059': 'J-ex',
        'MC.GD0060': 'GDP-G',
        'MC.GD0061': 'M2-G',
        'MC.GD0063': 'CPIr-G',
        'MC.GD0064': 'Euro-ex',
        'MC.GD0065': 'GDP-B',
        'MC.GD0066': 'M2-B',
        'MC.GD0068': 'CPIr-B',
        'MC.GD0069': 'B-ex',
        'MC.GD0072': 'GDP-I',
        'MC.GD0073': 'M2-I',
        'MC.GD0075': 'CPIr-I',
        'MC.GD0076': 'GDP-C',
        'MC.GD0077': 'M2-C',
        'MC.GD0079': 'CPIr-C',
        'MC.GD0080': 'C-ex',
        'MC.GD0081': 'GDP-R',
        'MC.GD0082': 'M2-R',
        'MC.GD0084': 'CPIr-R',
        'MC.GD0085': 'R-ex',
        'MC.GD0086': 'GDP-Ch',
        'MC.GD0087': 'M2-Ch',
        'MC.GD0088': 'CPIr-Ch',
        'MC.GD0089': 'Ch-ex',
        'MC.GD0090': 'Traded_DI',
    }

    engine = database.connection('wind')
    Session = sessionmaker(bind=engine)
    session = Session()
    sql = session.query(
        mc_gold_indicator.globalid,
        mc_gold_indicator.mc_gold_date,
        mc_gold_indicator.mc_gold_value,
        ).filter(mc_gold_indicator.globalid.in_(feature_names.keys())).statement

    gdi = pd.read_sql(
        sql,
        session.bind,
        index_col = ['mc_gold_date','globalid'],
        parse_dates = ['mc_gold_date'],
    )

    session.commit()
    session.close()

    gdi = gdi.unstack()
    gdi.columns = gdi.columns.levels[1]
    gdi = gdi.rename(columns = feature_names)

    return gdi

#更新数据库函数
def oildata_save(gid,df):
    #读入旧数据
    db = database.connection('wind')
    t2 = Table('mc_gold_indicator',MetaData(bind=db),autoload=True)
    columns = [literal_column(c) for c in (df.index.names + list(df.columns))]
    s = select(columns).where(t2.c.globalid == gid)
    df_old = pd.read_sql(s,db,index_col=['globalid','mc_gold_date'],parse_dates=['mc_gold_date'])
    print ('##############################',df_old.tail())
    #更新数据库
    database.batch(db,t2,df,df_old,timestamp=False)

#更新数据库
def initial_update():
    feature_names = {
        'MC.GD0201': 'oil_OPEC_Pro',
        'MC.GD0202': 'oil_OECD_consum',
        'MC.GD0203': 'oil_OECD_stocks',
        'MC.GD0204': 'oil_US_stocks',
        'MC.GD0205': 'oil_US_utiliza',
        'MC.GD0206': 'oil_US_Pro',
        'MC.GD0207': 'money_US_Index',
        'MC.GD0208': 'eco_US_GDP',
        'MC.GD0209': 'oil_US_export',
        'MC.GD0210': 'oil_world_demand',
        'MC.GD0211': 'oil_US_stocks2',
        'MC.GD0212': 'pos_ICE_longC',
        'MC.GD0213': 'pos_ICE_shortC',
        'MC.GD0214': 'pos_ICE_longF',
        'MC.GD0215': 'pos_ICE_shortF',
        'MC.GD0216': 'pos_Nymex_longC',
        'MC.GD0217': 'pos_Nymex_shortC',
        'MC.GD0218': 'pos_Nymex_longF',
        'MC.GD0219': 'pos_Nymex_shortF',
        'MC.GD0220': 'price_f_brent',
        'MC.GD0221': 'price_f_wti',
        'MC.GD0222': 'price_f_oman',
        'MC.GD0223': 'price_s_brent',
        'MC.GD0224': 'price_s_wti',
        'MC.GD0225': 'price_s_dibai',
        'MC.GD0050': 'EuroDollar',
        'MC.GD0051': 'GDP-A',
        'MC.GD0052': 'CPIr-A',
        'MC.GD0053': 'M2-A',
        'MC.GD0055': 'GDP-J',
        'MC.GD0056': 'M2-J',
        'MC.GD0058': 'CPIr-J',
        'MC.GD0059': 'J-ex',
        'MC.GD0060': 'GDP-G',
        'MC.GD0061': 'M2-G',
        'MC.GD0063': 'CPIr-G',
        'MC.GD0064': 'Euro-ex',
        'MC.GD0065': 'GDP-B',
        'MC.GD0066': 'M2-B',
        'MC.GD0068': 'CPIr-B',
        'MC.GD0069': 'B-ex',
        'MC.GD0072': 'GDP-I',
        'MC.GD0073': 'M2-I',
        'MC.GD0075': 'CPIr-I',
        'MC.GD0076': 'GDP-C',
        'MC.GD0077': 'M2-C',
        'MC.GD0079': 'CPIr-C',
        'MC.GD0080': 'C-ex',
        'MC.GD0081': 'GDP-R',
        'MC.GD0082': 'M2-R',
        'MC.GD0084': 'CPIr-R',
        'MC.GD0085': 'R-ex',
        'MC.GD0086': 'GDP-Ch',
        'MC.GD0087': 'M2-Ch',
        'MC.GD0088': 'CPIr-Ch',
        'MC.GD0089': 'Ch-ex',
        'MC.GD0090': 'Traded_DI',
   }

    today = datetime.now().strftime('%Y-%m-%d')
    df = pd.read_csv('oildata_update1.csv')
    df = df.set_index(['date'])
    sorted_keys = sorted(feature_names.keys())
    print (sorted_keys)

    for i in range(0,len(sorted_keys)):
        key_i = sorted_keys[i]
        columns_name = feature_names[key_i]
        print (columns_name)
        df_i = df.loc[:,[columns_name]]
        df_i.index.name = 'mc_gold_date'
        df_i.columns = ['mc_gold_value']
        df_i.loc[:,'globalid'] = key_i
        df_i.loc[:,'created_at'] = today
        df_i.loc[:,'updated_at'] = today
        df_i = df_i.reset_index().set_index(['globalid','mc_gold_date'])
        print (key_i,df_i.tail(10))

        oildata_save(key_i,df_i)

###################   数据读入 -- END   #####################################

###################   辅助函数    ###########################################
def next_month(now_time):
    '''
    # 输入：字符串如'2018-06'
    # 输出：‘2018-7’
    '''
    next_year = int(now_time[0:4])
    next_month = int(now_time[5:7]) + 1
    if next_month == 13:
        next_month = 1
        next_year += 1
    month_time = str(next_year) + '-' + str(next_month)

    return month_time

def ema(s,n):
    #def/fucntion:"指数移动平均"
    ema = []
    j = 1
    sma = sum(s[:n]) / n
    multiplier = 2 / float(1+n)
    ema.append(sma)
    ema.append(((s[n]-sma)*multiplier)+sma)

    for i in s[n+1:]:
        tmp = ((i - ema[j])*multiplier)+ema[j]
        j = j=1
        ema.append(tmp)
    ema1 = [s[:x].mean() for x in range(1,n)]
    ema = ema1+ema

    return np.array(ema)

def cal_cot(df,cot_rolling_win):
    #def/function:"计算cot值"
    if len(df) < cot_rolling_win:
        print ('Error')
    cal_df  =pd.DataFrame()
    cal_df['max'] = df.rolling(min_periods = 1,window=cot_rolling_win).max()
    cal_df['min'] = df.rolling(min_periods = 1,window=cot_rolling_win).min()
    cal_df['cot'] = (df - cal_df['min']) / (cal_df['max'] - cal_df['min'])

    return cal_df['cot'].fillna(0)

def cal_netcot(df,cot_win,ema_win):
    #def/function:"计算两cot值差"
    df_netcot = cal_cot(df.iloc[:,0],cot_win) - cal_cot(df.iloc[:,1],cot_win)
    cot = pd.DataFrame(ema(df_netcot.values, ema_win), index=df_netcot.index,columns=['netcot'])
    return cot

def filter_fuc(values,para):
    #def/function:简单过滤
    filter_values = list(values)
    for i in range(len(values)):
        if i> 2 and (values[i-1]*values[i-2] >= 0 and values[i-1]*values[i] <= 0 and abs(values[i]) < para):
            filter_values[i] = -1.0*values[i]
    return filter_values

def filter_fuc(values,para):
    #function:'简单过滤'
    filter_values = values
    for i in range(len(values)):
        if i > 2 and (values[i-1]*values[i-2] >= 0 and values[i-1]*values[i] <= 0 and abs(values[i]) < para):
            filter_values[i] = -1.0*values[i]
    return filter_values

def cal_view(df,para=0.34):
    #function:'计算观点值'
    df_data = df.iloc[:,0]
    strength = filter_fuc(pd.DataFrame(ema(df_data.values,12)).pct_change().fillna(0).values/para*100,para*5)
    return strength
##################    辅助函数  -- END   #####################################

##################    参数测试  --START  #####################################

#################     参数测试  --END   #####################################

#################     主体函数     ###########################################

def cal_oil_view(choice=3):
    #def/funciton:"计算原油的观点：基本面观点和资金面观点"
    #initial_update()
    data_origin = load_oil_indicator()

    ##################计算macro指标################
    data_names = ['GDP-A','M2-A','GDP-J','M2-J','GDP-G','M2-G','GDP-B','M2-B','GDP-I','M2-I','GDP-C','M2-C',\
        'GDP-R','M2-R','GDP-Ch','M2-Ch','J-ex','Euro-ex','B-ex','C-ex','R-ex','Ch-ex',\
        'CPIr-A','CPIr-J','CPIr-G','CPIr-B','CPIr-I','CPIr-C','CPIr-Ch']
    data = data_origin.loc[:,data_names]
    data = data.dropna().replace({0:np.nan}).fillna(method='ffill').resample('M').last()
    data = pd.concat([data,data_origin.loc[:,['CPIr-R']].replace({0:np.nan}).fillna(method='ffill').resample('M').last().pct_change(12)],axis=1).dropna()
    #美国数据：以万亿美元为单位，衡量GDP十亿和M2十亿
    data[['GDP-A','M2-A']] = data[['GDP-A','M2-A']]/1000.0
    #日本数据：日本GDP-十亿和M2亿
    data['GDP-J'] = data['GDP-J']/1000.0/data['J-ex']
    data['M2-J'] = data['M2-J']/1000.0/data['J-ex']
    #德国数据:GDP百万欧元，M2十亿欧元
    data['GDP-G'] = data['GDP-G']/1000000.0/data['Euro-ex']
    data['M2-G'] = data['M2-G']/1000.0/data['Euro-ex']
    #英国数据：GDP百万英镑，M2百万英镑
    data['GDP-B'] = data['GDP-B']/1000000.0/data['B-ex']
    data['M2-B'] = data['M2-B']/1000000.0/data['B-ex']
    #意大利数据：GDP百万英镑，M2百万英镑
    data['GDP-I'] = data['GDP-I']/1000000.0/data['Euro-ex']
    data['M2-I'] = data['M2-I']/1000000.0/data['Euro-ex']
    #加拿大数据：GDP百万加元，M2百万加元
    data['GDP-C'] = data['GDP-C']/1000000.0/data['C-ex']
    data['M2-C'] = data['M2-C']*data['GDP-C']#M2/GDP比例转化为M2
    ##俄罗斯数据：GDP十亿卢布，M2十亿卢布
    data['GDP-R'] = data['GDP-R']*4/1000.0/data['R-ex']
    data['M2-R'] = data['M2-R']/1000.0/data['R-ex']
    #中国数据：亿元
    data['GDP-Ch'] = data['GDP-Ch']*4/10000.0/data['Ch-ex']
    data['M2-Ch'] = data['M2-Ch']/10000.0/data['Ch-ex']
    #计算
    data['GDP'] = data[['GDP-A','GDP-J','GDP-G','GDP-B','GDP-I','GDP-C','GDP-R','GDP-Ch']].apply(lambda x:x.sum(),axis=1).values
    data['M2'] = data[['M2-A','M2-J','M2-G','M2-B','M2-I','M2-C','M2-R','M2-Ch']].apply(lambda x:x.sum(),axis=1).values
    data['M2_GDP'] = (data['M2']/data['GDP']).values - 1.0
    GDP_ratio = data[['GDP-A','GDP-J','GDP-G','GDP-B','GDP-I','GDP-C','GDP-R','GDP-Ch']].div(data['GDP'],axis='index')
    data['CPI'] = (data[['CPIr-A','CPIr-J','CPIr-G','CPIr-B','CPIr-I','CPIr-C','CPIr-R','CPIr-Ch']].values*GDP_ratio.values).sum(axis=1)
    data['unCPI'] = (data[['CPI']] - data[['CPI']].rolling(window=18,min_periods=1).mean()).values
    data_macro = data[['M2_GDP','CPI','unCPI']].shift(3).dropna()
    #print (data_macro.head(15))

    #################计算资金多空强度######################

    Pos_data = data_origin[['price_f_brent','price_f_wti','pos_ICE_longC','pos_ICE_shortC','pos_ICE_longF','pos_ICE_shortF','pos_Nymex_longC','pos_Nymex_shortC','pos_Nymex_longF','pos_Nymex_shortF']].truncate(before = '1995-03-21')
    Pos_data.iloc[0,2] = Pos_data['pos_ICE_longC'].loc[Pos_data['pos_ICE_longC'] > 0].head(1).values
    Pos_data.iloc[0,3] = Pos_data['pos_ICE_shortC'].loc[Pos_data['pos_ICE_shortC'] > 0].head(1).values
    Pos_data.iloc[0,4] = Pos_data['pos_ICE_longF'].loc[Pos_data['pos_ICE_longF'] > 0].head(1).values
    Pos_data.iloc[0,5] = Pos_data['pos_ICE_shortF'].loc[Pos_data['pos_ICE_shortF'] > 0].head(1).values
    Pos_data = Pos_data.replace(0,np.nan).fillna(method='ffill').resample('M').last().dropna()
    Pos_data['long'] = Pos_data[['pos_ICE_longC','pos_ICE_longF','pos_Nymex_longC','pos_Nymex_longF']].sum(axis=1).values
    Pos_data['short'] = Pos_data[['pos_ICE_shortC','pos_ICE_shortF','pos_Nymex_shortC','pos_Nymex_shortF']].sum(axis=1).values
    oil_netcot = cal_netcot(Pos_data[['long','short']],30,12).rename(columns={'netcot':'cot_T'})
    oil_netcot['price_f_wti'] = Pos_data['price_f_wti'].values
    #oil_netcot['price_forward'] = Pos_data['wti_f_price'].shift(1).values

#######################其他回归因素

    market_data = data_origin[['oil_OPEC_Pro','oil_OECD_consum','oil_OECD_stocks','oil_US_stocks','oil_US_Pro','money_US_Index','eco_US_GDP','USnrty']].truncate(before = '2000-12-31')
    market_data = market_data.dropna().replace(0,np.nan).fillna(method='ffill').resample('M').last().dropna()
    market_data[['oil_OPEC_Pro','oil_OECD_consum','oil_OECD_stocks','oil_US_stocks','oil_US_Pro']] = market_data[['oil_OPEC_Pro','oil_OECD_consum','oil_OECD_stocks','oil_US_stocks','oil_US_Pro']].pct_change(12).shift(1)
    market_data[['money_US_Index']] = market_data[['money_US_Index']].pct_change(1)
    market_data[['eco_US_GDP']] = market_data[['eco_US_GDP']].pct_change(12).shift(3)
    market_data = market_data.dropna()

######################回归
    oil_data = pd.concat([market_data,data_macro,oil_netcot],axis=1).dropna()
    name_par = ['oil_OPEC_Pro','oil_OECD_consum','oil_OECD_stocks','oil_US_stocks','oil_US_Pro','M2_GDP','CPI','unCPI','money_US_Index','USnrty','cot_T']
    x_par = oil_data.loc[:,name_par]
    y_par = oil_data.loc[:,['price_f_wti']]
    #x_par = pd.DataFrame(StandardScaler().fit_transform(x_par),columns=name_par,index=y_par.index)

    win_reg = 120
    rlasso_para = 0.94
    lr_para = 5
    y_pre = []
    for i in range(0,len(y_par)-1-win_reg):
        X = x_par.iloc[i:i+win_reg,:]
        Y = y_par.iloc[i:i+win_reg,:]
        names = name_par

        if choice==1:
            #特征选择算法1：稳定性选择<产生随机数，对于相同的输入可能会得到不同的结果>
            rlasso = RandomizedLasso(alpha=0.025)
            rlasso.fit(X,Y)
            rl_names = pd.DataFrame(sorted(zip(map(lambda x: round(x,4),rlasso.scores_),names),reverse=True),columns=['score','name'])
            rl_names = rl_names.loc[rl_names['score'] > rlasso_para]
            #rl_names = rl_names.iloc[0:4,:]
            select_names = rl_names['name'].values
            X = X[select_names]
            print ('Features sorted by their score:')
            print (select_names,rl_names['score'])
        if choice==2:
            #lsvc = LinearSVC(C=0.01,penalty='l1',dual=False).fit(X,Y)
            #X = SelectFromModel(lsvc,prefit=True).transform(X)
            clf = ExtraTreesClassifier().fit(X,Y)
            X = SelectFromModel(lcf,prefit=True).transform(X)
        if choice==3:
            #特征选择算法2：递归特征消除
            lr = Ridge(alpha=1.0)
            #lr = LinearRegression()
            rfe = RFE(lr, n_features_to_select=5)
            rfe.fit(X,Y)
            lr_names = pd.DataFrame(sorted(zip(map(lambda x: round(x, 4), rfe.ranking_), names)),columns=['score','name'])
            lr_names = lr_names.loc[lr_names['score'] == 1]
            select_names = lr_names['name'].values
            X = X[select_names]
            print ("Features sorted by their rank:")
            print (select_names,lr_names['score'])

            #set_trace()
        #回归预测
        mod = sm.OLS(Y,X)
        res = mod.fit()
        y_pre.append(res.predict(x_par[select_names].iloc[i+win_reg,:].values)/res.predict(x_par[select_names].iloc[i+win_reg-1,:].values))

    df_y_pre = pd.DataFrame(y_pre,index=y_par.index[win_reg+1:],columns=['y_pre'])
    new_date = next_month(df_y_pre.index[-1].strftime('%Y-%m'))
    df_y_pre.loc[new_date] = None
    df_y_pre = pd.concat([df_y_pre.shift(1),y_par],axis=1)
    df_y_pre['y_pre'][0] = df_y_pre['price_f_wti'][0]
    df_y_pre = df_y_pre.loc[df_y_pre.y_pre > 0,:]
    df_y_pre[['y_pre']] = df_y_pre[['y_pre']].cumprod()
    df_y_pre['view_oil'] = cal_view(df_y_pre[['y_pre']])
    price_ema = ema(df_y_pre['price_f_wti'].values,12) - ema(df_y_pre['price_f_wti'].values,20)
    df_y_pre['view_technical']  = np.where(price_ema >= 0 ,np.where(price_ema > 0,1,0),-1)
    df_y_pre.to_csv('df_y_pre.csv')
    print (df_y_pre)

    return df_y_pre

if __name__ == '__main__':
    initial_update()
    cal_oil_view()

###################     主体函数 END     ######################################

@ot.command()
@click.option('--start-date','startdate',default='2003-01-01',help=u'start date to calc')
@click.option('--end-date','enddate',default=datetime.today().strftime('%Y-%m-%d'),help=u'start date to calc')
@click.option('--viewid','viewid',default='BL.000010',help=u'macro timing view id')
@click.pass_context

def oil_view_update(ctx,startdate,enddate,viewid):
    '''
    id=BL.000010,保存到表格：asset/ra_bl_view
    '''
    mv = cal_oil_view()
    today = datetime.now().strftime('%Y-%m-%d')
    union_mv = {}
    union_mv['globalid'] = np.repeat(viewid,len(mv))
    union_mv['bl_date'] = mv.index
    union_mv['bl_view'] = np.where(mv.view_oil >= 0,1,-1)
    union_mv['bl_index_id'] = np.repeat('120000014',len(mv))
    union_mv['created_at'] = np.repeat(today,len(mv))
    union_mv['updated_at'] = np.repeat(today,len(mv))
    union_mv_df = pd.DataFrame(union_mv,columns=['globalid','bl_date','bl_view','bl_index_id','created_at','updated_at'])
    df_new = union_mv_df.set_index(['globalid','bl_date'])

    db = database.connection('asset')
    metadata = MetaData(bind=db)
    t = Table('ra_bl_view',metadata,autoload = True)
    columns = [
        t.c.globalid,
        t.c.bl_date,
        t.c.bl_view,
        t.c.bl_index_id,
        t.c.created_at,
        t.c.updated_at,
    ]

    s = select(columns,(t.c.globalid == viewid))
    df_old = pd.read_sql(s,db,index_col=['globalid','bl_date'],parse_date=['bl_date'])
    database.batch(db,t,df_new,df_old,timestamp=False)
    print ('######  id=BL.000010,保存到表格：asset/ra_bl_view ########')


# coding: utf-8

"""
    ORY Hydra

    Welcome to the ORY Hydra HTTP API documentation. You will find documentation for all HTTP APIs here.  # noqa: E501

    The version of the OpenAPI document: v1.7.0
    Generated by: https://openapi-generator.tech
"""

import pprint
import re  # noqa: F401

import six

from ory_hydra_client.configuration import Configuration

class UserinfoResponse(object):
    """NOTE: This class is auto generated by OpenAPI Generator.
    Ref: https://openapi-generator.tech

    Do not edit the class manually.
    """

    """
    Attributes:
      openapi_types (dict): The key is attribute name
                            and the value is attribute type.
      attribute_map (dict): The key is attribute name
                            and the value is json key in definition.
    """
    openapi_types = {
        'birthdate': 'str',
        'email': 'str',
        'email_verified': 'bool',
        'family_name': 'str',
        'gender': 'str',
        'given_name': 'str',
        'locale': 'str',
        'middle_name': 'str',
        'name': 'str',
        'nickname': 'str',
        'phone_number': 'str',
        'phone_number_verified': 'bool',
        'picture': 'str',
        'preferred_username': 'str',
        'profile': 'str',
        'sub': 'str',
        'updated_at': 'int',
        'website': 'str',
        'zoneinfo': 'str'
    }

    attribute_map = {
        'birthdate': 'birthdate',
        'email': 'email',
        'email_verified': 'email_verified',
        'family_name': 'family_name',
        'gender': 'gender',
        'given_name': 'given_name',
        'locale': 'locale',
        'middle_name': 'middle_name',
        'name': 'name',
        'nickname': 'nickname',
        'phone_number': 'phone_number',
        'phone_number_verified': 'phone_number_verified',
        'picture': 'picture',
        'preferred_username': 'preferred_username',
        'profile': 'profile',
        'sub': 'sub',
        'updated_at': 'updated_at',
        'website': 'website',
        'zoneinfo': 'zoneinfo'
    }

    def __init__(self, birthdate=None, email=None, email_verified=None, family_name=None, gender=None, given_name=None, locale=None, middle_name=None, name=None, nickname=None, phone_number=None, phone_number_verified=None, picture=None, preferred_username=None, profile=None, sub=None, updated_at=None, website=None, zoneinfo=None, local_vars_configuration=None):  # noqa: E501
        """UserinfoResponse - a model defined in OpenAPI"""  # noqa: E501
        if local_vars_configuration is None:
            local_vars_configuration = Configuration()
        self.local_vars_configuration = local_vars_configuration

        self._birthdate = None
        self._email = None
        self._email_verified = None
        self._family_name = None
        self._gender = None
        self._given_name = None
        self._locale = None
        self._middle_name = None
        self._name = None
        self._nickname = None
        self._phone_number = None
        self._phone_number_verified = None
        self._picture = None
        self._preferred_username = None
        self._profile = None
        self._sub = None
        self._updated_at = None
        self._website = None
        self._zoneinfo = None
        self.discriminator = None

        if birthdate is not None:
            self.birthdate = birthdate
        if email is not None:
            self.email = email
        if email_verified is not None:
            self.email_verified = email_verified
        if family_name is not None:
            self.family_name = family_name
        if gender is not None:
            self.gender = gender
        if given_name is not None:
            self.given_name = given_name
        if locale is not None:
            self.locale = locale
        if middle_name is not None:
            self.middle_name = middle_name
        if name is not None:
            self.name = name
        if nickname is not None:
            self.nickname = nickname
        if phone_number is not None:
            self.phone_number = phone_number
        if phone_number_verified is not None:
            self.phone_number_verified = phone_number_verified
        if picture is not None:
            self.picture = picture
        if preferred_username is not None:
            self.preferred_username = preferred_username
        if profile is not None:
            self.profile = profile
        if sub is not None:
            self.sub = sub
        if updated_at is not None:
            self.updated_at = updated_at
        if website is not None:
            self.website = website
        if zoneinfo is not None:
            self.zoneinfo = zoneinfo

    @property
    def birthdate(self):
        """Gets the birthdate of this UserinfoResponse.  # noqa: E501

        End-User's birthday, represented as an ISO 8601:2004 [ISO8601‑2004] YYYY-MM-DD format. The year MAY be 0000, indicating that it is omitted. To represent only the year, YYYY format is allowed. Note that depending on the underlying platform's date related function, providing just year can result in varying month and day, so the implementers need to take this factor into account to correctly process the dates.  # noqa: E501

        :return: The birthdate of this UserinfoResponse.  # noqa: E501
        :rtype: str
        """
        return self._birthdate

    @birthdate.setter
    def birthdate(self, birthdate):
        """Sets the birthdate of this UserinfoResponse.

        End-User's birthday, represented as an ISO 8601:2004 [ISO8601‑2004] YYYY-MM-DD format. The year MAY be 0000, indicating that it is omitted. To represent only the year, YYYY format is allowed. Note that depending on the underlying platform's date related function, providing just year can result in varying month and day, so the implementers need to take this factor into account to correctly process the dates.  # noqa: E501

        :param birthdate: The birthdate of this UserinfoResponse.  # noqa: E501
        :type: str
        """

        self._birthdate = birthdate

    @property
    def email(self):
        """Gets the email of this UserinfoResponse.  # noqa: E501

        End-User's preferred e-mail address. Its value MUST conform to the RFC 5322 [RFC5322] addr-spec syntax. The RP MUST NOT rely upon this value being unique, as discussed in Section 5.7.  # noqa: E501

        :return: The email of this UserinfoResponse.  # noqa: E501
        :rtype: str
        """
        return self._email

    @email.setter
    def email(self, email):
        """Sets the email of this UserinfoResponse.

        End-User's preferred e-mail address. Its value MUST conform to the RFC 5322 [RFC5322] addr-spec syntax. The RP MUST NOT rely upon this value being unique, as discussed in Section 5.7.  # noqa: E501

        :param email: The email of this UserinfoResponse.  # noqa: E501
        :type: str
        """

        self._email = email

    @property
    def email_verified(self):
        """Gets the email_verified of this UserinfoResponse.  # noqa: E501

        True if the End-User's e-mail address has been verified; otherwise false. When this Claim Value is true, this means that the OP took affirmative steps to ensure that this e-mail address was controlled by the End-User at the time the verification was performed. The means by which an e-mail address is verified is context-specific, and dependent upon the trust framework or contractual agreements within which the parties are operating.  # noqa: E501

        :return: The email_verified of this UserinfoResponse.  # noqa: E501
        :rtype: bool
        """
        return self._email_verified

    @email_verified.setter
    def email_verified(self, email_verified):
        """Sets the email_verified of this UserinfoResponse.

        True if the End-User's e-mail address has been verified; otherwise false. When this Claim Value is true, this means that the OP took affirmative steps to ensure that this e-mail address was controlled by the End-User at the time the verification was performed. The means by which an e-mail address is verified is context-specific, and dependent upon the trust framework or contractual agreements within which the parties are operating.  # noqa: E501

        :param email_verified: The email_verified of this UserinfoResponse.  # noqa: E501
        :type: bool
        """

        self._email_verified = email_verified

    @property
    def family_name(self):
        """Gets the family_name of this UserinfoResponse.  # noqa: E501

        Surname(s) or last name(s) of the End-User. Note that in some cultures, people can have multiple family names or no family name; all can be present, with the names being separated by space characters.  # noqa: E501

        :return: The family_name of this UserinfoResponse.  # noqa: E501
        :rtype: str
        """
        return self._family_name

    @family_name.setter
    def family_name(self, family_name):
        """Sets the family_name of this UserinfoResponse.

        Surname(s) or last name(s) of the End-User. Note that in some cultures, people can have multiple family names or no family name; all can be present, with the names being separated by space characters.  # noqa: E501

        :param family_name: The family_name of this UserinfoResponse.  # noqa: E501
        :type: str
        """

        self._family_name = family_name

    @property
    def gender(self):
        """Gets the gender of this UserinfoResponse.  # noqa: E501

        End-User's gender. Values defined by this specification are female and male. Other values MAY be used when neither of the defined values are applicable.  # noqa: E501

        :return: The gender of this UserinfoResponse.  # noqa: E501
        :rtype: str
        """
        return self._gender

    @gender.setter
    def gender(self, gender):
        """Sets the gender of this UserinfoResponse.

        End-User's gender. Values defined by this specification are female and male. Other values MAY be used when neither of the defined values are applicable.  # noqa: E501

        :param gender: The gender of this UserinfoResponse.  # noqa: E501
        :type: str
        """

        self._gender = gender

    @property
    def given_name(self):
        """Gets the given_name of this UserinfoResponse.  # noqa: E501

        Given name(s) or first name(s) of the End-User. Note that in some cultures, people can have multiple given names; all can be present, with the names being separated by space characters.  # noqa: E501

        :return: The given_name of this UserinfoResponse.  # noqa: E501
        :rtype: str
        """
        return self._given_name

    @given_name.setter
    def given_name(self, given_name):
        """Sets the given_name of this UserinfoResponse.

        Given name(s) or first name(s) of the End-User. Note that in some cultures, people can have multiple given names; all can be present, with the names being separated by space characters.  # noqa: E501

        :param given_name: The given_name of this UserinfoResponse.  # noqa: E501
        :type: str
        """

        self._given_name = given_name

    @property
    def locale(self):
        """Gets the locale of this UserinfoResponse.  # noqa: E501

        End-User's locale, represented as a BCP47 [RFC5646] language tag. This is typically an ISO 639-1 Alpha-2 [ISO639‑1] language code in lowercase and an ISO 3166-1 Alpha-2 [ISO3166‑1] country code in uppercase, separated by a dash. For example, en-US or fr-CA. As a compatibility note, some implementations have used an underscore as the separator rather than a dash, for example, en_US; Relying Parties MAY choose to accept this locale syntax as well.  # noqa: E501

        :return: The locale of this UserinfoResponse.  # noqa: E501
        :rtype: str
        """
        return self._locale

    @locale.setter
    def locale(self, locale):
        """Sets the locale of this UserinfoResponse.

        End-User's locale, represented as a BCP47 [RFC5646] language tag. This is typically an ISO 639-1 Alpha-2 [ISO639‑1] language code in lowercase and an ISO 3166-1 Alpha-2 [ISO3166‑1] country code in uppercase, separated by a dash. For example, en-US or fr-CA. As a compatibility note, some implementations have used an underscore as the separator rather than a dash, for example, en_US; Relying Parties MAY choose to accept this locale syntax as well.  # noqa: E501

        :param locale: The locale of this UserinfoResponse.  # noqa: E501
        :type: str
        """

        self._locale = locale

    @property
    def middle_name(self):
        """Gets the middle_name of this UserinfoResponse.  # noqa: E501

        Middle name(s) of the End-User. Note that in some cultures, people can have multiple middle names; all can be present, with the names being separated by space characters. Also note that in some cultures, middle names are not used.  # noqa: E501

        :return: The middle_name of this UserinfoResponse.  # noqa: E501
        :rtype: str
        """
        return self._middle_name

    @middle_name.setter
    def middle_name(self, middle_name):
        """Sets the middle_name of this UserinfoResponse.

        Middle name(s) of the End-User. Note that in some cultures, people can have multiple middle names; all can be present, with the names being separated by space characters. Also note that in some cultures, middle names are not used.  # noqa: E501

        :param middle_name: The middle_name of this UserinfoResponse.  # noqa: E501
        :type: str
        """

        self._middle_name = middle_name

    @property
    def name(self):
        """Gets the name of this UserinfoResponse.  # noqa: E501

        End-User's full name in displayable form including all name parts, possibly including titles and suffixes, ordered according to the End-User's locale and preferences.  # noqa: E501

        :return: The name of this UserinfoResponse.  # noqa: E501
        :rtype: str
        """
        return self._name

    @name.setter
    def name(self, name):
        """Sets the name of this UserinfoResponse.

        End-User's full name in displayable form including all name parts, possibly including titles and suffixes, ordered according to the End-User's locale and preferences.  # noqa: E501

        :param name: The name of this UserinfoResponse.  # noqa: E501
        :type: str
        """

        self._name = name

    @property
    def nickname(self):
        """Gets the nickname of this UserinfoResponse.  # noqa: E501

        Casual name of the End-User that may or may not be the same as the given_name. For instance, a nickname value of Mike might be returned alongside a given_name value of Michael.  # noqa: E501

        :return: The nickname of this UserinfoResponse.  # noqa: E501
        :rtype: str
        """
        return self._nickname

    @nickname.setter
    def nickname(self, nickname):
        """Sets the nickname of this UserinfoResponse.

        Casual name of the End-User that may or may not be the same as the given_name. For instance, a nickname value of Mike might be returned alongside a given_name value of Michael.  # noqa: E501

        :param nickname: The nickname of this UserinfoResponse.  # noqa: E501
        :type: str
        """

        self._nickname = nickname

    @property
    def phone_number(self):
        """Gets the phone_number of this UserinfoResponse.  # noqa: E501

        End-User's preferred telephone number. E.164 [E.164] is RECOMMENDED as the format of this Claim, for example, +1 (425) 555-1212 or +56 (2) 687 2400. If the phone number contains an extension, it is RECOMMENDED that the extension be represented using the RFC 3966 [RFC3966] extension syntax, for example, +1 (604) 555-1234;ext=5678.  # noqa: E501

        :return: The phone_number of this UserinfoResponse.  # noqa: E501
        :rtype: str
        """
        return self._phone_number

    @phone_number.setter
    def phone_number(self, phone_number):
        """Sets the phone_number of this UserinfoResponse.

        End-User's preferred telephone number. E.164 [E.164] is RECOMMENDED as the format of this Claim, for example, +1 (425) 555-1212 or +56 (2) 687 2400. If the phone number contains an extension, it is RECOMMENDED that the extension be represented using the RFC 3966 [RFC3966] extension syntax, for example, +1 (604) 555-1234;ext=5678.  # noqa: E501

        :param phone_number: The phone_number of this UserinfoResponse.  # noqa: E501
        :type: str
        """

        self._phone_number = phone_number

    @property
    def phone_number_verified(self):
        """Gets the phone_number_verified of this UserinfoResponse.  # noqa: E501

        True if the End-User's phone number has been verified; otherwise false. When this Claim Value is true, this means that the OP took affirmative steps to ensure that this phone number was controlled by the End-User at the time the verification was performed. The means by which a phone number is verified is context-specific, and dependent upon the trust framework or contractual agreements within which the parties are operating. When true, the phone_number Claim MUST be in E.164 format and any extensions MUST be represented in RFC 3966 format.  # noqa: E501

        :return: The phone_number_verified of this UserinfoResponse.  # noqa: E501
        :rtype: bool
        """
        return self._phone_number_verified

    @phone_number_verified.setter
    def phone_number_verified(self, phone_number_verified):
        """Sets the phone_number_verified of this UserinfoResponse.

        True if the End-User's phone number has been verified; otherwise false. When this Claim Value is true, this means that the OP took affirmative steps to ensure that this phone number was controlled by the End-User at the time the verification was performed. The means by which a phone number is verified is context-specific, and dependent upon the trust framework or contractual agreements within which the parties are operating. When true, the phone_number Claim MUST be in E.164 format and any extensions MUST be represented in RFC 3966 format.  # noqa: E501

        :param phone_number_verified: The phone_number_verified of this UserinfoResponse.  # noqa: E501
        :type: bool
        """

        self._phone_number_verified = phone_number_verified

    @property
    def picture(self):
        """Gets the picture of this UserinfoResponse.  # noqa: E501

        URL of the End-User's profile picture. This URL MUST refer to an image file (for example, a PNG, JPEG, or GIF image file), rather than to a Web page containing an image. Note that this URL SHOULD specifically reference a profile photo of the End-User suitable for displaying when describing the End-User, rather than an arbitrary photo taken by the End-User.  # noqa: E501

        :return: The picture of this UserinfoResponse.  # noqa: E501
        :rtype: str
        """
        return self._picture

    @picture.setter
    def picture(self, picture):
        """Sets the picture of this UserinfoResponse.

        URL of the End-User's profile picture. This URL MUST refer to an image file (for example, a PNG, JPEG, or GIF image file), rather than to a Web page containing an image. Note that this URL SHOULD specifically reference a profile photo of the End-User suitable for displaying when describing the End-User, rather than an arbitrary photo taken by the End-User.  # noqa: E501

        :param picture: The picture of this UserinfoResponse.  # noqa: E501
        :type: str
        """

        self._picture = picture

    @property
    def preferred_username(self):
        """Gets the preferred_username of this UserinfoResponse.  # noqa: E501

        Non-unique shorthand name by which the End-User wishes to be referred to at the RP, such as janedoe or j.doe. This value MAY be any valid JSON string including special characters such as @, /, or whitespace.  # noqa: E501

        :return: The preferred_username of this UserinfoResponse.  # noqa: E501
        :rtype: str
        """
        return self._preferred_username

    @preferred_username.setter
    def preferred_username(self, preferred_username):
        """Sets the preferred_username of this UserinfoResponse.

        Non-unique shorthand name by which the End-User wishes to be referred to at the RP, such as janedoe or j.doe. This value MAY be any valid JSON string including special characters such as @, /, or whitespace.  # noqa: E501

        :param preferred_username: The preferred_username of this UserinfoResponse.  # noqa: E501
        :type: str
        """

        self._preferred_username = preferred_username

    @property
    def profile(self):
        """Gets the profile of this UserinfoResponse.  # noqa: E501

        URL of the End-User's profile page. The contents of this Web page SHOULD be about the End-User.  # noqa: E501

        :return: The profile of this UserinfoResponse.  # noqa: E501
        :rtype: str
        """
        return self._profile

    @profile.setter
    def profile(self, profile):
        """Sets the profile of this UserinfoResponse.

        URL of the End-User's profile page. The contents of this Web page SHOULD be about the End-User.  # noqa: E501

        :param profile: The profile of this UserinfoResponse.  # noqa: E501
        :type: str
        """

        self._profile = profile

    @property
    def sub(self):
        """Gets the sub of this UserinfoResponse.  # noqa: E501

        Subject - Identifier for the End-User at the IssuerURL.  # noqa: E501

        :return: The sub of this UserinfoResponse.  # noqa: E501
        :rtype: str
        """
        return self._sub

    @sub.setter
    def sub(self, sub):
        """Sets the sub of this UserinfoResponse.

        Subject - Identifier for the End-User at the IssuerURL.  # noqa: E501

        :param sub: The sub of this UserinfoResponse.  # noqa: E501
        :type: str
        """

        self._sub = sub

    @property
    def updated_at(self):
        """Gets the updated_at of this UserinfoResponse.  # noqa: E501

        Time the End-User's information was last updated. Its value is a JSON number representing the number of seconds from 1970-01-01T0:0:0Z as measured in UTC until the date/time.  # noqa: E501

        :return: The updated_at of this UserinfoResponse.  # noqa: E501
        :rtype: int
        """
        return self._updated_at

    @updated_at.setter
    def updated_at(self, updated_at):
        """Sets the updated_at of this UserinfoResponse.

        Time the End-User's information was last updated. Its value is a JSON number representing the number of seconds from 1970-01-01T0:0:0Z as measured in UTC until the date/time.  # noqa: E501

        :param updated_at: The updated_at of this UserinfoResponse.  # noqa: E501
        :type: int
        """

        self._updated_at = updated_at

    @property
    def website(self):
        """Gets the website of this UserinfoResponse.  # noqa: E501

        URL of the End-User's Web page or blog. This Web page SHOULD contain information published by the End-User or an organization that the End-User is affiliated with.  # noqa: E501

        :return: The website of this UserinfoResponse.  # noqa: E501
        :rtype: str
        """
        return self._website

    @website.setter
    def website(self, website):
        """Sets the website of this UserinfoResponse.

        URL of the End-User's Web page or blog. This Web page SHOULD contain information published by the End-User or an organization that the End-User is affiliated with.  # noqa: E501

        :param website: The website of this UserinfoResponse.  # noqa: E501
        :type: str
        """

        self._website = website

    @property
    def zoneinfo(self):
        """Gets the zoneinfo of this UserinfoResponse.  # noqa: E501

        String from zoneinfo [zoneinfo] time zone database representing the End-User's time zone. For example, Europe/Paris or America/Los_Angeles.  # noqa: E501

        :return: The zoneinfo of this UserinfoResponse.  # noqa: E501
        :rtype: str
        """
        return self._zoneinfo

    @zoneinfo.setter
    def zoneinfo(self, zoneinfo):
        """Sets the zoneinfo of this UserinfoResponse.

        String from zoneinfo [zoneinfo] time zone database representing the End-User's time zone. For example, Europe/Paris or America/Los_Angeles.  # noqa: E501

        :param zoneinfo: The zoneinfo of this UserinfoResponse.  # noqa: E501
        :type: str
        """

        self._zoneinfo = zoneinfo

    def to_dict(self):
        """Returns the model properties as a dict"""
        result = {}

        for attr, _ in six.iteritems(self.openapi_types):
            value = getattr(self, attr)
            if isinstance(value, list):
                result[attr] = list(map(
                    lambda x: x.to_dict() if hasattr(x, "to_dict") else x,
                    value
                ))
            elif hasattr(value, "to_dict"):
                result[attr] = value.to_dict()
            elif isinstance(value, dict):
                result[attr] = dict(map(
                    lambda item: (item[0], item[1].to_dict())
                    if hasattr(item[1], "to_dict") else item,
                    value.items()
                ))
            else:
                result[attr] = value

        return result

    def to_str(self):
        """Returns the string representation of the model"""
        return pprint.pformat(self.to_dict())

    def __repr__(self):
        """For `print` and `pprint`"""
        return self.to_str()

    def __eq__(self, other):
        """Returns true if both objects are equal"""
        if not isinstance(other, UserinfoResponse):
            return False

        return self.to_dict() == other.to_dict()

    def __ne__(self, other):
        """Returns true if both objects are not equal"""
        if not isinstance(other, UserinfoResponse):
            return True

        return self.to_dict() != other.to_dict()


from django.shortcuts import render
from .summarization import summarize
from .forms import TextSummarization

# Create your views here.
def nlp_text_summarization(response):
    # Check if there is any users loggin in:
    if(response.method == 'POST'):
        form = TextSummarization(response.POST)
        if(form.is_valid()):
            text = form.cleaned_data['text']
            original_charcount = len(text)
            original_wordcount = len(text.split(' '))

            summary = summarize(text)
            summary_wordcount = len(summary.split(' '))
            summary_charcount = len(summary)
            form = TextSummarization() # new form
            return render(response, 'nlp/summarization.html',{'original':text,
             'summary':summary,'original_wordcount':original_wordcount,
             'original_charcount':original_charcount,
             'summary_charcount':summary_charcount,
             'summary_wordcount':summary_wordcount,'form' : form})
    else:
        if(response.session['username'] == None or response.session['fullname'] == None):
            return render(response, 'nlp/error.html',{})
        else:
            form = TextSummarization()
            return render(response, 'nlp/summarization.html', {'form':form})


"""Main."""
import pygame
import sys

import KeyListener
import game_graphics.Display
import game_graphics.UI.UI
import Menu
import World
import objects.mobs.Player
import objects.mobs.Worker_man
import objects.mobs.Goblin
import game_state.GameState
import game_graphics.UI
import sounds.Sound_control
import Menu_world

pygame.init()
pygame.mixer.pre_init(44100, 16, 2, 4096)  # preset the mixer init arguments

window_title = "Driven Into the Last Corner"  # Title on top of the frame.
window_width = 1200  # Width of the canvas.
window_height = 700  # Height of the canvas.
fps_fix = 160

running = True  # while True game will run.

sc = sounds.Sound_control.SoundControl()
player = objects.mobs.Player.Player(30 << 5, 70 << 5)
menu_ghost = objects.mobs.Player.Player(30 << 5, 70 << 5)
world = World.World()
menu_world = Menu_world.MenuWorld()
gamestate = game_state.GameState.GameState()

worker_man = objects.mobs.Worker_man.Worker(37 << 5, 70 << 5, 0, [1456, 2162])   # first wood
worker_man2 = objects.mobs.Worker_man.Worker(37 << 5, 70 << 5, 0, [1456, 2162])   # second wood
worker_man3 = objects.mobs.Worker_man.Worker(27 << 5, 70 << 5, 1, [496, 2194])   # first stone

user_interface = None

def update():
    """Update all."""
    if Menu.inMenu:
        menu_world.update()
    else:
        KeyListener.update()
        world.update()
        gamestate.update()
        user_interface.update()
    cameraPos()  # update camera position.

    if KeyListener.exit_game is True:
        sys.exit()

def render(display_obj):
    """Render all visible stuff."""
    if Menu.inMenu:
        menu_world.render(display_obj)
    else:
        display_obj.canvas.fill(int(0x609CB0))
        world.render(display_obj)
        user_interface.render(display_obj)
        #  print(KeyListener.mouseX + World.World.camera_x, World.World.camera_y + KeyListener.mouseY)

cameraPosDoubleX = (menu_ghost.x - (window_width / 2))
cameraPosDoubleY = (menu_ghost.y - (window_height / 2))

def cameraPos():
    """Update camera position."""
    global cameraPosDoubleX
    global cameraPosDoubleY

    if Menu.inMenu:
        cameraPosDoubleY -= 1  # speed of menu camera up movement
        Menu_world.MenuWorld.camera_x = int(cameraPosDoubleX)
        Menu_world.MenuWorld.camera_y = int(cameraPosDoubleY)
    else:
        cameraPosDoubleX += ((player.x + player.camera_shift_x) - (cameraPosDoubleX + window_width / 2)) / 20
        cameraPosDoubleY += ((player.y + player.camera_shift_y) - (cameraPosDoubleY + window_height / 2)) / 20
        World.World.camera_x = int(cameraPosDoubleX)
        World.World.camera_y = int(cameraPosDoubleY)

def run():
    """Run the game."""
    sounds.Sound_control.SoundControl.play_menu_music(sc)

    game_window = game_graphics.Display.Display(window_width, window_height, fps_fix)  # surface
    mr = Menu.MenuRun(game_window.canvas)
    mr.run(game_window.canvas, surf=game_window)

    world.mobs.append(worker_man)
    world.mobs.append(worker_man3)
    world.mobs.append(player)
    world.mobs.append(objects.mobs.Goblin.Goblin(30 << 5, 40 << 5))
    """
    world.mobs.append(objects.mobs.Goblin.Goblin(31 << 5, 40 << 5))
    world.mobs.append(objects.mobs.Goblin.Goblin(32 << 5, 40 << 5))
    world.mobs.append(objects.mobs.Goblin.Goblin(33 << 5, 40 << 5))
    world.mobs.append(objects.mobs.Goblin.Goblin(34 << 5, 40 << 5))
    world.mobs.append(objects.mobs.Goblin.Goblin(35 << 5, 40 << 5))
    world.mobs.append(objects.mobs.Goblin.Goblin(36 << 5, 40 << 5))
    world.mobs.append(objects.mobs.Goblin.Goblin(29 << 5, 40 << 5))
    world.mobs.append(objects.mobs.Goblin.Goblin(28 << 5, 40 << 5))
    world.mobs.append(objects.mobs.Goblin.Goblin(27 << 5, 40 << 5))
    world.mobs.append(objects.mobs.Goblin.Goblin(26 << 5, 40 << 5))
    world.mobs.append(objects.mobs.Goblin.Goblin(30 << 5, 40 << 5))
    world.mobs.append(objects.mobs.Goblin.Goblin(31 << 5, 41 << 5))
    world.mobs.append(objects.mobs.Goblin.Goblin(32 << 5, 42 << 5))
    world.mobs.append(objects.mobs.Goblin.Goblin(33 << 5, 43 << 5))
    world.mobs.append(objects.mobs.Goblin.Goblin(34 << 5, 44 << 5))
    world.mobs.append(objects.mobs.Goblin.Goblin(35 << 5, 45 << 5))
    world.mobs.append(objects.mobs.Goblin.Goblin(36 << 5, 39 << 5))
    world.mobs.append(objects.mobs.Goblin.Goblin(29 << 5, 38 << 5))
    world.mobs.append(objects.mobs.Goblin.Goblin(28 << 5, 37 << 5))
    world.mobs.append(objects.mobs.Goblin.Goblin(27 << 5, 36 << 5))
    world.mobs.append(objects.mobs.Goblin.Goblin(26 << 5, 35 << 5))
    """

    menu_world.mobs.append(menu_ghost)
    menu_world.player = menu_ghost
    World.player = player
    global user_interface
    user_interface = game_graphics.UI.UI.UI()
    sounds.Sound_control.SoundControl.fadeout_menu_music_to_game_music(sc, 3000)  # fadeout time is in ms

    while running:
        game_window.clock.tick(game_window.fps)
        pygame.display.set_caption(window_title + " | " + "FPS: %i" % game_window.clock.get_fps())
        update()
        render(game_window)
        pygame.display.flip()

if __name__ == "__main__":
    run()


from jedi.parser_utils import get_flow_branch_keyword, is_scope, get_parent_scope
from jedi.evaluate.recursion import execution_allowed

class Status(object):
    lookup_table = {}

    def __init__(self, value, name):
        self._value = value
        self._name = name
        Status.lookup_table[value] = self

    def invert(self):
        if self is REACHABLE:
            return UNREACHABLE
        elif self is UNREACHABLE:
            return REACHABLE
        else:
            return UNSURE

    def __and__(self, other):
        if UNSURE in (self, other):
            return UNSURE
        else:
            return REACHABLE if self._value and other._value else UNREACHABLE

    def __repr__(self):
        return '<%s: %s>' % (type(self).__name__, self._name)

REACHABLE = Status(True, 'reachable')
UNREACHABLE = Status(False, 'unreachable')
UNSURE = Status(None, 'unsure')

def _get_flow_scopes(node):
    while True:
        node = get_parent_scope(node, include_flows=True)
        if node is None or is_scope(node):
            return
        yield node

def reachability_check(context, context_scope, node, origin_scope=None):
    first_flow_scope = get_parent_scope(node, include_flows=True)
    if origin_scope is not None:
        origin_flow_scopes = list(_get_flow_scopes(origin_scope))
        node_flow_scopes = list(_get_flow_scopes(node))

        branch_matches = True
        for flow_scope in origin_flow_scopes:
            if flow_scope in node_flow_scopes:
                node_keyword = get_flow_branch_keyword(flow_scope, node)
                origin_keyword = get_flow_branch_keyword(flow_scope, origin_scope)
                branch_matches = node_keyword == origin_keyword
                if flow_scope.type == 'if_stmt':
                    if not branch_matches:
                        return UNREACHABLE
                elif flow_scope.type == 'try_stmt':
                    if not branch_matches and origin_keyword == 'else' \
                            and node_keyword == 'except':
                        return UNREACHABLE
                if branch_matches:
                    break

        # Direct parents get resolved, we filter scopes that are separate
        # branches.  This makes sense for autocompletion and static analysis.
        # For actual Python it doesn't matter, because we're talking about
        # potentially unreachable code.
        # e.g. `if 0:` would cause all name lookup within the flow make
        # unaccessible. This is not a "problem" in Python, because the code is
        # never called. In Jedi though, we still want to infer types.
        while origin_scope is not None:
            if first_flow_scope == origin_scope and branch_matches:
                return REACHABLE
            origin_scope = origin_scope.parent

    return _break_check(context, context_scope, first_flow_scope, node)

def _break_check(context, context_scope, flow_scope, node):
    reachable = REACHABLE
    if flow_scope.type == 'if_stmt':
        if flow_scope.is_node_after_else(node):
            for check_node in flow_scope.get_test_nodes():
                reachable = _check_if(context, check_node)
                if reachable in (REACHABLE, UNSURE):
                    break
            reachable = reachable.invert()
        else:
            flow_node = flow_scope.get_corresponding_test_node(node)
            if flow_node is not None:
                reachable = _check_if(context, flow_node)
    elif flow_scope.type in ('try_stmt', 'while_stmt'):
        return UNSURE

    # Only reachable branches need to be examined further.
    if reachable in (UNREACHABLE, UNSURE):
        return reachable

    if context_scope != flow_scope and context_scope != flow_scope.parent:
        flow_scope = get_parent_scope(flow_scope, include_flows=True)
        return reachable & _break_check(context, context_scope, flow_scope, node)
    else:
        return reachable

def _check_if(context, node):
    with execution_allowed(context.evaluator, node) as allowed:
        if not allowed:
            return UNSURE

        types = context.eval_node(node)
        values = set(x.py__bool__() for x in types)
        if len(values) == 1:
            return Status.lookup_table[values.pop()]
        else:
            return UNSURE


import os
import re

from setuptools import find_packages, setup

from discover_tests import DiscoverTest

def str_to_bool(x):
    x = str(x)

    return x.lower() in ('true', 't', 'yes', 'y', '1')

with open(
    os.path.join(os.path.dirname(__file__), 'metREx', '__init__.py')
) as i_file:
    info = i_file.read()

    TITLE = (
        re.compile(r""".*__title__ = ["']([^\n]*)['"]""", re.S).match(info).group(1)
    )

    VERSION = (
        re.compile(r""".*__version__ = ["']([^\n]*)['"]""", re.S).match(info).group(1)
    )

    DESCRIPTION = (
        re.compile(r""".*__description__ = ["']([^\n]*)['"]""", re.S).match(info).group(1)
    )

with open(os.path.join(os.path.dirname(__file__), 'README.md')) as r_file:
    README = r_file.read()

INSTALL_REQUIRES = [
    'cfenv',
    'cryptofy',
    'Flask-APIAlchemy',
    'Flask-APScheduler',
    'flask-restx',
    'Flask-Script',
    'Flask-SQLAlchemy',
    'numpy',
    'prometheus-flask-exporter',
    'python-dotenv',
    'PyYAML',
    'SQLAlchemy'
]

EXTRAS_REQUIRE = {
    'db2': [
        'ibm_db',
        'ibm-db-sa'
    ],
    'mssql': [
        'pymssql'
    ],
    'mysql': [
        'pymysql'
    ],
    'oracle': [
        'cx-Oracle'
    ],
    'postgresql': [
        'pg8000'
    ]
}

TESTS_REQUIRE = [
    'Flask-Testing',
    'pytest',
    'teamcity-messages'
]

install_all_extras = str_to_bool(os.getenv('INSTALL_ALL_EXTRAS', False))

if install_all_extras:
    for packages in EXTRAS_REQUIRE.values():
        for package in packages:
            if package not in INSTALL_REQUIRES:
                INSTALL_REQUIRES.append(package)

setup(name=TITLE,
      version=VERSION,
      description=DESCRIPTION,
      long_description=README,
      long_description_content_type='text/markdown',
      license='Apache 2.0',
      keywords='prometheus exporter prometheus-exporter metrics metrics-exporter query-exporter sql sql-exporter appd appd-exporter appdynamics appdynamics-exporter extrahop extrahop-exporter',
      url='https://github.com/homedepot/metREx',
      author='Mike Phillipson',
      author_email='MICHAEL_PHILLIPSON1@homedepot.com',
      packages=find_packages('metREx'),
      package_dir={
          '': 'metREx'
      },
      data_files=[
          ('static', [
              'favicon.ico'
          ])
      ],
      entry_points={
          'console_scripts': [
              'metrex=manage:run'
          ]
      },
      python_requires='>=3.6',
      install_requires=INSTALL_REQUIRES,
      extras_require=EXTRAS_REQUIRE,
      tests_require=TESTS_REQUIRE,
      cmdclass={
          'test': DiscoverTest
      },
      zip_safe=False)


from PySide2.QtWidgets import QLabel, QWidget, QPushButton, QApplication, QHBoxLayout,QVBoxLayout, QGridLayout, QTextEdit, QProgressBar, QSlider

class IHM(QWidget):

    def __init__(self):

        QWidget.__init__(self)

        self.layout = QHBoxLayout()
        self.setWindowTitle("IHM")

        self.bar = QProgressBar(self)
        self.slider = QSlider(self)
        self.slider.setMinimum(0)
        self.slider.setMaximum(100)
        self.slider.setValue(20)
        self.slider.valueChanged.connect(self.valuechange)

        self.layout.addWidget(self.bar)
        self.layout.addWidget(self.slider)
        self.setLayout(self.layout)

    def valuechange(self):
        size = self.slider.value()
        print(size)

if __name__ == "__main__":
    app = QApplication([])
    win = IHM()
    win.show()
    app.exec_()


import httplib
import json
import os

import requests
from nose import tools
from nose.plugins import attrib

from tests.integration import base, testing_utilities

PATH_PARAMS = {
    'base_url': 'http://0.0.0.0:5000',
    'service': 'offers',
    'list_id': 'edaa3541-7376-4eb3-8047-aaf78af900da'
}

@attrib.attr('local_integration')
class AppendListEndpointTest(base.BaseIntegrationLiveStubServerTestCase):

    @classmethod
    def setUpClass(cls, **kwargs):
        super(AppendListEndpointTest, cls).setUpClass(**kwargs)
        base.BaseIntegrationTestCase.setUpClass()

    def setUp(self):
        self.test_file = testing_utilities.copy_test_file(file_name='normal_large.csv')
        self.test_path = os.path.join('tests/samples/{}'.format(self.test_file))
        self.headers = testing_utilities.generate_headers(base_url='http://live.lcpenv')
        self.data = {'filePath': self.test_path}

    def tearDown(self):
        testing_utilities.delete_test_files(self.test_file)

    def test_append_list_missing_file(self):
        data = {}
        response = requests.put(base.ListPaths.append(**PATH_PARAMS), json.dumps(data), headers=self.headers)
        response_content = json.loads(response.content)

        tools.assert_equal(httplib.BAD_REQUEST, response.status_code)
        tools.assert_equal(1, len(response_content['errors']))
        tools.assert_equal('MISSING_FIELD', response_content['errors'][0]['code'])
        tools.assert_equal("'filePath' is required.", response_content['errors'][0]['description'])

    def test_append_list_big_file(self):
        response = requests.put(base.ListPaths.append(**PATH_PARAMS), json.dumps(self.data), headers=self.headers)
        response_content = json.loads(response.content)

        tools.assert_equal(httplib.BAD_REQUEST, response.status_code)
        tools.assert_equal(1, len(response_content['errors']))
        tools.assert_equal('BAD_REQUEST', response_content['errors'][0]['code'])
        tools.assert_equal("There are too many accounts specified.", response_content['errors'][0]['description'])


import numpy as np
import cv2

def cvt_object_label(img, ori_label_color, dst_label_color=None):

    if dst_label_color is None:
        dst_label_color = ori_label_color

    height, width, channels = img.shape
    blue_mask = np.ones([height, width, 1], dtype=np.uint8) * ori_label_color[0]
    green_mask = np.ones([height, width, 1], dtype=np.uint8) * ori_label_color[1]
    red_mask = np.ones([height, width, 1], dtype=np.uint8) * ori_label_color[2]
    color_mask = cv2.merge((blue_mask, green_mask, red_mask))

    # Set label_color to 0
    img = cv2.bitwise_xor(img, color_mask)

    # Set label_color to 255
    img = cv2.bitwise_not(img)

    # Set other pixel to 0
    mask = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    ret, mask = cv2.threshold(mask, 254, 255, cv2.THRESH_BINARY)

    mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
    img = cv2.bitwise_and(mask, np.array(dst_label_color))

    return img