#!/usr/bin/env python
# coding=utf-8
import logging
import math
import os
from pathlib import Path
import sys
from itertools import chain

import datasets
from datasets import load_dataset, load_metric

from huggingface_hub import Repository
import transformers
from transformers import (
    CONFIG_MAPPING,
    MODEL_FOR_CAUSAL_LM_MAPPING,
    AutoConfig,
    AutoModelForCausalLM,
    AutoTokenizer,
    HfArgumentParser,
    Trainer,
    TrainingArguments,
    default_data_collator,
    is_torch_tpu_available,
    set_seed,
)
from transformers.testing_utils import CaptureLogger
from transformers.trainer_utils import get_last_checkpoint
from transformers.utils import check_min_version
from transformers.utils import get_full_repo_name
from transformers.utils.versions import require_version

logger = logging.getLogger(__name__)
# Will error if the minimal version of Transformers is not installed. Remove at your own risks.
check_min_version("4.20.0.dev0")
require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/language-modeling/requirements.txt")

MODEL_CONFIG_CLASSES = list(MODEL_FOR_CAUSAL_LM_MAPPING.keys())
MODEL_TYPES = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES)

def main(ModelArguments, DataTrainingArguments, TrainingArguments):
    parser = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments))
    if len(sys.argv) == 2 and sys.argv[1].endswith(".json"):
        model_args, data_args, training_args = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1]))
    else:
        model_args, data_args, training_args = parser.parse_args_into_dataclasses()

    # Setup logging
    logging.basicConfig(
        format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
        datefmt="%m/%d/%Y %H:%M:%S",
        handlers=[logging.StreamHandler(sys.stdout)],
    )

    training_args.log_level = logging.DEBUG
    log_level = training_args.log_level
    # log_level = training_args.get_process_log_level()
    logger.setLevel(log_level)
    datasets.utils.logging.set_verbosity(log_level)
    transformers.utils.logging.set_verbosity(log_level)
    transformers.utils.logging.enable_default_handler()
    transformers.utils.logging.enable_explicit_format()

    '''
    # HF Hub
    if training_args.push_to_hub:
        if training_args.hub_model_id is None:
            repo_name = get_full_repo_name(
                Path(training_args.output_dir).absolute().name, token=training_args.hub_token)
        else:
            repo_name = training_args.hub_model_id
        repo = Repository(training_args.output_dir, clone_from=repo_name)
        repo.git_pull()
    '''

    # Log on each process the small summary:
    logger.warning(
        f"Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}"
        + f"distributed training: {bool(training_args.local_rank != -1)}, 16-bits training: {training_args.fp16}"
    )
    logger.info(f"Training/evaluation parameters {training_args}")

    # Detecting last checkpoint.
    last_checkpoint = None
    if os.path.isdir(training_args.output_dir) and training_args.do_train and not training_args.overwrite_output_dir:
        last_checkpoint = get_last_checkpoint(training_args.output_dir)
        if last_checkpoint is None and len(os.listdir(training_args.output_dir)) > 0:
            raise ValueError(
                f"Output directory ({training_args.output_dir}) already exists and is not empty. "
                "Use --overwrite_output_dir to overcome."
            )
        elif last_checkpoint is not None and training_args.resume_from_checkpoint is None:
            logger.info(
                f"Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change "
                "the `--output_dir` or add `--overwrite_output_dir` to train from scratch."
            )

    set_seed(training_args.seed)

    # datasets
    if data_args.dataset_name is not None:
        # Downloading and loading a dataset from the hub.
        raw_datasets = load_dataset(
            data_args.dataset_name,
            data_args.dataset_config_name,
            cache_dir=model_args.cache_dir,
            use_auth_token=True if model_args.use_auth_token else None,
        )
        if "validation" not in raw_datasets.keys():
            raw_datasets["validation"] = load_dataset(
                data_args.dataset_name,
                data_args.dataset_config_name,
                split=f"train[:{data_args.validation_split_percentage}%]",
                cache_dir=model_args.cache_dir,
                use_auth_token=True if model_args.use_auth_token else None,
            )
            raw_datasets["train"] = load_dataset(
                data_args.dataset_name,
                data_args.dataset_config_name,
                split=f"train[{data_args.validation_split_percentage}%:]",
                cache_dir=model_args.cache_dir,
                use_auth_token=True if model_args.use_auth_token else None,
            )
    else:
        data_files = {}
        dataset_args = {}
        if data_args.train_file is not None:
            data_files["train"] = data_args.train_file
        if data_args.validation_file is not None:
            data_files["validation"] = data_args.validation_file
        extension = (
            data_args.train_file.split(".")[-1]
            if data_args.train_file is not None
            else data_args.validation_file.split(".")[-1]
        )
        if extension == "txt":
            extension = "text"
            dataset_args["keep_linebreaks"] = data_args.keep_linebreaks
        raw_datasets = load_dataset(
            extension,
            data_files=data_files,
            cache_dir=model_args.cache_dir,
            use_auth_token=True if model_args.use_auth_token else None,
            **dataset_args,
        )
        # If no validation data is there, validation_split_percentage will be used to divide the dataset.
        if "validation" not in raw_datasets.keys():
            raw_datasets["validation"] = load_dataset(
                extension,
                data_files=data_files,
                split=f"train[:{data_args.validation_split_percentage}%]",
                cache_dir=model_args.cache_dir,
                use_auth_token=True if model_args.use_auth_token else None,
                **dataset_args,
            )
            raw_datasets["train"] = load_dataset(
                extension,
                data_files=data_files,
                split=f"train[{data_args.validation_split_percentage}%:]",
                cache_dir=model_args.cache_dir,
                use_auth_token=True if model_args.use_auth_token else None,
                **dataset_args,
            )

    # shuffle the datasets for sure
    # raw_datasets['train'] = raw_datasets['train'].shuffle(seed=training_args.seed)

    # configs
    config_kwargs = {
        "cache_dir": model_args.cache_dir,
        "revision": model_args.model_revision,
        "use_auth_token": True if model_args.use_auth_token else None,
    }
    if model_args.config_name:
        config = AutoConfig.from_pretrained(model_args.config_name, **config_kwargs)
    elif model_args.model_name_or_path:
        config = AutoConfig.from_pretrained(model_args.model_name_or_path, **config_kwargs)
    else:
        config = CONFIG_MAPPING[model_args.model_type]()
        logger.warning("You are instantiating a new config instance from scratch.")
        if model_args.config_overrides is not None:
            logger.info(f"Overriding config: {model_args.config_overrides}")
            config.update_from_string(model_args.config_overrides)
            logger.info(f"New config: {config}")

    tokenizer_kwargs = {
        "cache_dir": model_args.cache_dir,
        "use_fast": model_args.use_fast_tokenizer,
        "revision": model_args.model_revision,
        "use_auth_token": True if model_args.use_auth_token else None,
    }
    if model_args.tokenizer_name:
        tokenizer = AutoTokenizer.from_pretrained(model_args.tokenizer_name, **tokenizer_kwargs)
    elif model_args.model_name_or_path:
        tokenizer = AutoTokenizer.from_pretrained(model_args.model_name_or_path, **tokenizer_kwargs)
    else:
        raise ValueError(
            "You are instantiating a new tokenizer from scratch. This is not supported by this script."
            "You can do it from another script, save it, and load it from here, using --tokenizer_name."
        )

    if model_args.model_name_or_path:
        model = AutoModelForCausalLM.from_pretrained(
            model_args.model_name_or_path,
            from_tf=bool(".ckpt" in model_args.model_name_or_path),
            config=config,
            cache_dir=model_args.cache_dir,
            revision=model_args.model_revision,
            use_auth_token=True if model_args.use_auth_token else None,
        )
        n_params = sum(dict((p.data_ptr(), p.numel()) for p in model.parameters()).values())
        logger.info(
            f"Training from pretrained `{model_args.model_name_or_path}` - Total size={n_params/2**20:.2f}M params")
    else:
        model = AutoModelForCausalLM.from_config(config)
        n_params = sum(dict((p.data_ptr(), p.numel()) for p in model.parameters()).values())
        logger.info(f"Training new model from scratch - Total size={n_params/2**20:.2f}M params")

    model.resize_token_embeddings(len(tokenizer))

    # Preprocessing the datasets.
    # First we tokenize all the texts.
    if training_args.do_train:
        column_names = raw_datasets["train"].column_names
    else:
        if data_args.dataset_name == 'oscar':
            column_names = raw_datasets["train"].column_names
        else:
            column_names = raw_datasets["validation"].column_names
    text_column_name = "text" if "text" in column_names else column_names[0]

    # since this will be pickled to avoid _LazyModule error in Hasher force logger loading before tokenize_function
    tok_logger = transformers.utils.logging.get_logger("transformers.tokenization_utils_base")

    def tokenize_function(examples):
        with CaptureLogger(tok_logger) as cl:
            output = tokenizer(examples[text_column_name])
        # clm input could be much much longer than block_size
        if "Token indices sequence length is longer than the" in cl.out:
            tok_logger.warning(
                "^^^^^^^^^^^^^^^^ Please ignore the warning above - this long input will be chunked into smaller bits before being passed to the model."  # noqa
            )
        return output

    with training_args.main_process_first(desc="dataset map tokenization"):
        tokenized_datasets = raw_datasets.map(
            tokenize_function,
            batched=True,
            num_proc=data_args.preprocessing_num_workers,
            remove_columns=column_names,
            load_from_cache_file=not data_args.overwrite_cache,
            desc="Running tokenizer on dataset",
        )

    if data_args.block_size is None:
        block_size = tokenizer.model_max_length
        if block_size > 1024:
            logger.warning(
                f"The tokenizer picked seems to have a very large `model_max_length` ({tokenizer.model_max_length}). "
                "Picking 1024 instead. You can change that default value by passing --block_size xxx."
            )
            block_size = 1024
    else:
        if data_args.block_size > tokenizer.model_max_length:
            logger.warning(
                f"The block_size passed ({data_args.block_size}) is larger than the maximum length for the model"
                f"({tokenizer.model_max_length}). Using block_size={tokenizer.model_max_length}."
            )
        block_size = min(data_args.block_size, tokenizer.model_max_length)
    logger.info(f"block_size = {block_size}")

    # Main data processing function that will concatenate all texts from our dataset and generate chunks of block_size.
    def group_texts(examples):
        # Concatenate all texts.
        concatenated_examples = {k: list(chain(*examples[k])) for k in examples.keys()}
        total_length = len(concatenated_examples[list(examples.keys())[0]])
        # We drop the small remainder, we could add padding if the model supported it instead of this drop, you can
        # customize this part to your needs.
        if total_length >= block_size:
            total_length = (total_length // block_size) * block_size
        # Split by chunks of max_len.
        result = {
            k: [t[i:i + block_size] for i in range(0, total_length, block_size)]
            for k, t in concatenated_examples.items()
        }
        result["labels"] = result["input_ids"].copy()
        return result

    with training_args.main_process_first(desc="grouping texts together"):
        lm_datasets = tokenized_datasets.map(
            group_texts,
            batched=True,
            num_proc=data_args.preprocessing_num_workers,
            load_from_cache_file=not data_args.overwrite_cache,
            desc=f"Grouping texts in chunks of {block_size}",
        )

    if training_args.do_train:
        if "train" not in tokenized_datasets:
            raise ValueError("--do_train requires a train dataset")
        train_dataset = lm_datasets["train"]
        if data_args.max_train_samples is not None:
            train_dataset = train_dataset.select(range(data_args.max_train_samples))

    if training_args.do_eval:
        if "validation" not in tokenized_datasets:
            raise ValueError("--do_eval requires a validation dataset")
        eval_dataset = lm_datasets["validation"]
        if data_args.max_eval_samples is not None:
            eval_dataset = eval_dataset.select(range(data_args.max_eval_samples))

        def preprocess_logits_for_metrics(logits, labels):
            if isinstance(logits, tuple):
                logits = logits[0]
            return logits.argmax(dim=-1)

        metric = load_metric("accuracy")

        def compute_metrics(eval_preds):
            preds, labels = eval_preds
            labels = labels[:, 1:].reshape(-1)
            preds = preds[:, :-1].reshape(-1)
            return metric.compute(predictions=preds, references=labels)

    # Initialize our Trainer
    trainer = Trainer(
        model=model,
        args=training_args,
        train_dataset=train_dataset if training_args.do_train else None,
        eval_dataset=eval_dataset if training_args.do_eval else None,
        tokenizer=tokenizer,
        # Data collator will default to DataCollatorWithPadding, so we change it.
        data_collator=default_data_collator,
        compute_metrics=compute_metrics if training_args.do_eval and not is_torch_tpu_available() else None,
        preprocess_logits_for_metrics=preprocess_logits_for_metrics
        if training_args.do_eval and not is_torch_tpu_available()
        else None,
    )

    # Training
    if training_args.do_train:
        checkpoint = None
        if training_args.resume_from_checkpoint is not None:
            checkpoint = training_args.resume_from_checkpoint
        elif last_checkpoint is not None:
            checkpoint = last_checkpoint
        train_result = trainer.train(resume_from_checkpoint=checkpoint)
        trainer.save_model()  # Saves the tokenizer too for easy upload

        metrics = train_result.metrics

        max_train_samples = (
            data_args.max_train_samples if data_args.max_train_samples is not None else len(train_dataset)
        )
        metrics["train_samples"] = min(max_train_samples, len(train_dataset))

        trainer.log_metrics("train", metrics)
        trainer.save_metrics("train", metrics)
        trainer.save_state()

    # Evaluation
    if training_args.do_eval:
        logger.info("*** Evaluate ***")

        metrics = trainer.evaluate()

        max_eval_samples = data_args.max_eval_samples if data_args.max_eval_samples is not None else len(eval_dataset)
        metrics["eval_samples"] = min(max_eval_samples, len(eval_dataset))
        try:
            perplexity = math.exp(metrics["eval_loss"])
        except OverflowError:
            perplexity = float("inf")
        metrics["perplexity"] = perplexity

        trainer.log_metrics("eval", metrics)
        trainer.save_metrics("eval", metrics)

    kwargs = {"finetuned_from": model_args.model_name_or_path, "tasks": "text-generation"}
    if data_args.dataset_name is not None:
        kwargs["dataset_tags"] = data_args.dataset_name
        if data_args.dataset_config_name is not None:
            kwargs["dataset_args"] = data_args.dataset_config_name
            kwargs["dataset"] = f"{data_args.dataset_name} {data_args.dataset_config_name}"
        else:
            kwargs["dataset"] = data_args.dataset_name

    if training_args.push_to_hub:
        trainer.push_to_hub(**kwargs)
    else:
        trainer.create_model_card(**kwargs)

def _mp_fn(index):
    # For xla_spawn (TPUs)
    main()

if __name__ == "__main__":
    import importlib
    from utils import Timer

    with Timer(prefix="main"):
        # import argparse
        # parser = argparse.ArgumentParser()
        # parser.add_argument("--cfg", type=str, default="cfg0")
        # args, _ = parser.parse_known_args()
        # config = importlib.import_module(args.cfg)
        config = importlib.import_module(os.getenv("CONF"))
        # import cfg as config
        main(config.ModelArguments, config.DataArguments, TrainingArguments)


"""
    This module is responsible to be the endpoints of bob
"""

from flask import jsonify
from app.controller.client import ClientController
from app.controller.bob_log import BobLogController
from app.constants import constants

def login(client_json):
    """
        This method is the endpoint to login
    :param client_json: user_id, device_id
    :return: response
    """
    client_controller = ClientController()
    client_returned = client_controller.login_client(client_json)

    if client_returned == constants.NOT_FOUND:
        response = {'status': constants.NOT_FOUND, 'response': 'Not Found.'}
        return jsonify(response)
    response = {'status': constants.OK, 'response': client_returned}
    return jsonify(response)

def balance_safra(client_json):
    """
        This method is the endpoint to return the balance of a client
    :param client_json: user_id, device_id, token
    :return: balance response
    """
    client_controller = ClientController()
    balance = client_controller.client_balance(client_json)
    if balance == constants.UNAUTHORIZED:
        response = {'status': constants.UNAUTHORIZED, 'response': 'Expired session'}
        return jsonify(response)
    response = {'status': constants.OK, 'response': balance}
    return jsonify(response)

def transactions_safra(client_json):
    """
    This method is the endpoint to return the transactions of a client
    :param client_json: user_id, device_id, token
    :return: transactions response
    """
    client_controller = ClientController()
    transactions = client_controller.client_transactions(client_json)
    if transactions == constants.UNAUTHORIZED:
        response = {'status': constants.UNAUTHORIZED, 'response': 'Expired session'}
        return jsonify(response)
    response = {'status': constants.OK, 'response': transactions}
    return jsonify(response)

def morning_call_safra(client_json):
    """
    This method is the endpoint to return the morning call of a client
    :param client_json: user_id, device_id, token
    :return: morning call response
    """
    client_controller = ClientController()
    morning_calls = client_controller.client_morning_call(client_json)
    if morning_calls == constants.UNAUTHORIZED:
        response = {'status': constants.UNAUTHORIZED, 'response': 'Expired session'}
        return jsonify(response)
    response = {'status': constants.OK, 'audioURL': morning_calls['data'][1]['links'][1]['href']}
    return jsonify(response)

def bob_logs(adm_token):
    """
    This method is the endpoint to return the logs
    :return: JSON with all logs
    """
    bob_controller = BobLogController()
    bob_logs_json_array = bob_controller.find_all_bob_log(adm_token)
    if bob_logs_json_array == constants.NOT_FOUND:
        response = {'status': constants.NOT_FOUND, 'response': 'Not Found'}
        return jsonify(response)
    if bob_logs_json_array == constants.UNAUTHORIZED:
        response = {'status': constants.UNAUTHORIZED, 'response': 'Unauthorized'}
        return jsonify(response)
    response = {'status': constants.OK, 'response': bob_logs_json_array}
    return jsonify(response)


import pytest
import json
import os

from colors_api.color_extractor import ColorExtractor

def test_color_extractor_should_load_the_input_image():
    test_image_path = os.path.join('tests', 'utils', 'sky-7622960_1280.jpg')
    colorExtractor = ColorExtractor()
    colorExtractor.load(test_image_path)
    assert colorExtractor.image is not None

def test_color_extractor_should_should_extract_colors():
    test_image_path = os.path.join('tests', 'utils', 'sky-7622960_1280.jpg')
    colorExtractor = ColorExtractor()

    result_json = colorExtractor.load(test_image_path).extract(n_colors=3)
    result_dict = json.loads(result_json)
    assert len(result_dict['palette']) == 3


from datetime import date, timedelta
import sys
import numpy as np
import pandas as pd
import plotly.express as px
import yfinance as yf
from dash import Dash, html, dcc, Input, Output, State
from math import factorial
from time import sleep

app = Dash(__name__)
stocks_options = ['AAPL', 'MSFT', 'ADBE']
#maxsize = sum of permutations of stocks_options when s=3 + s=2 + s=1
#permutation = n!/(n-s)! -> n = len(stocks_options), s = number of stocks choosen
def get_data(symbols: list, start: str|None, end: str|None):
    print('Dados coletados')
    today = date.today()
    # start and end must be in "YYYY-MM-DD" format
    end = f"{today.year}-{f'0{today.month}' if len(str(today.month))==1 else today.month}-{today.day}" if end==None else end
    start = f"{today.year}-01-01" if start==None else start
    print(f"{start} <-> {end} {type(end)}")
    if len(symbols)==0:
        return None
    elif len(symbols)==1:
        data = yf.Ticker(symbols[0]).history(start=start, end=end).Close
    else:
        data = yf.Tickers(symbols).history(start=start, end=end).Close
    return data

def return_of(data):
    percentage_return_of_data = data/data.iloc[0]-1
    return percentage_return_of_data

def plot_(data, _format=None):
    fig = px.line(data)
    if _format=='%':
        fig.layout.yaxis = dict(tickformat=".2%")
    return fig

@app.callback(
    Output("graph", "figure"),
    inputs=Input("refresh-graph", "n_clicks"),
    state=[
        State("dropdown-symbols", "value"),
        State("interval-date", "start_date"),
        State("interval-date", "end_date"),
    ]
)
def update_graph(n_clicks, symbols, start_date, end_date):
    print(n_clicks)
    data = get_data(symbols, start=start_date, end=end_date)
    percentage_return = return_of(data)
    fig = plot_(percentage_return, _format='%')
    return fig

app.layout = html.Div([
    html.H4('Analysis of stocks collected from yahoo finance'),
    html.P('Select the stock'),
    dcc.Dropdown(
        id="dropdown-symbols",
        options=stocks_options,
        value=stocks_options[0],
        multi=True
    ),
    html.P('Select the interval'),
    dcc.DatePickerRange(
        id="interval-date",
        month_format='MMMM Y',
        display_format='DD/MM/YYYY',
        start_date_placeholder_text='Start date',
        end_date_placeholder_text='End date',
        max_date_allowed=date.today(),
    ),
    dcc.Graph(id="graph", figure=update_graph(None, ['MSFT'], None, None)),
    html.Button("Refresh", id='refresh-graph')
])
if __name__ == "__main__":
    app.run_server(debug=True)


import http

import requests
from requests import adapters
from urllib3.util import retry

def get_http_client(total_retries: int = 5, backoff_factor: float = 0.1) -> requests.Session:
    """Construct retryable http client.

    Args:
        total_retries: Total number of retries to allow.
        backoff_factor: A backoff factor to apply between attempts after the second try. Time to sleep is calculated by
            {backoff factor} * (2 ** ({number of previous retries})). For example, with default retries of 5 and backoff
            factor set to 0.1, each subsequent retry will sleep [0.2s, 0.4s, 0.8s, 1.6s, 3.2s] respectively.

    Returns:
        requests.Session object.

    """

    retry_strategy = retry.Retry(
        total=total_retries,
        backoff_factor=backoff_factor,
        status_forcelist=[
            http.HTTPStatus.TOO_MANY_REQUESTS,
            http.HTTPStatus.INTERNAL_SERVER_ERROR,
            http.HTTPStatus.BAD_GATEWAY,
            http.HTTPStatus.SERVICE_UNAVAILABLE,
            http.HTTPStatus.GATEWAY_TIMEOUT,
        ],  # retry on these status codes
    )

    adapter = adapters.HTTPAdapter(max_retries=retry_strategy)
    req_session = requests.Session()
    req_session.mount("https://", adapter)
    req_session.mount("http://", adapter)

    return req_session


#!/usr/bin/python3
# -*- coding: utf-8 -*-

import gmpy2

import sys
sys.path.append("..")
from cryptolib import *

"""
In number theory, the Chinese remainder theorem states that
if one knows the remainders of the Euclidean division of an integer n
by several integers, then one can determine uniquely the remainder of the division of n
by the product of these integers, under the condition that the divisors are pairwise coprime.

# 2 = 23 % 3
# 3 = 23 % 5
# 2 = 23 % 7
n = [3, 5, 7]
a = [2, 3, 2]
chinese_remainder(n, a) == 23
"""

def main():
	secret_plaintext = random_string(100).encode('utf-8')
	print('plaintext is:{}'.format(secret_plaintext.decode('utf-8')))

	rsa = RSA()
	rsa.gen_keypair()
	e1, n1 = rsa.get_public_key()
	rsa.gen_keypair()
	e2, n2 = rsa.get_public_key()
	rsa.gen_keypair()
	e3, n3 = rsa.get_public_key()

	ct1 = rsa.encrypt(secret_plaintext, 3, n1)
	ct1 = int.from_bytes(ct1, 'big')
	ct2 = rsa.encrypt(secret_plaintext, 3, n2)
	ct2 = int.from_bytes(ct2, 'big')
	ct3 = rsa.encrypt(secret_plaintext, 3, n3)
	ct3 = int.from_bytes(ct3, 'big')

	n = [n1, n2, n3]
	a = [ct1, ct2, ct3]
	result_cubed = chinese_remainder(n, a)

	result = int(gmpy2.iroot_rem(result_cubed, 3)[0])

	plaintext = "%x" % result
	plaintext = bytes.fromhex(plaintext)
	plaintext = plaintext.decode('utf-8')
	print('recovered   :{}'.format(plaintext))

if __name__ == '__main__':
	main()


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

from django import forms
from django.forms import ModelForm
from django.forms.widgets import ClearableFileInput, HiddenInput, Textarea, TextInput, URLInput
from django.utils.translation import ugettext_lazy as _
from pysnippets.strsnippets import strip

from resources.models import ApiInfo, UserApiInfo, WebSiteInfo, WebSiteSubmit
from utils.tt4it_utils import *

class WebSiteDiyModelForm(ModelForm):
    class Meta:
        model = WebSiteInfo
        exclude = ('qiniu_logo', 'srcode', 'visit', 'evaluate', 'like', 'unlike', 'fav', 'display', 'create_time', 'modify_time')
        widgets = {
            'url': TextInput(
                attrs={'autocomplete': 'off', 'autofocus': 'autofocus', 'placeholder': _('Url')}
            ),
            'name': TextInput(
                attrs={'autocomplete': 'off', 'placeholder': _('Name')}
            ),
            'logo': ClearableFileInput(
                attrs={'autocomplete': 'off', 'placeholder': _('Logo')}
            ),
            'descr': Textarea(
                attrs={'autocomplete': 'off', 'placeholder': _('Description')}
            ),
            'tag': TextInput(
                attrs={'autocomplete': 'off', 'placeholder': _('Tag')}
            ),
        }

    def clean_url(self):
        url = self.cleaned_data['url']
        if not url:
            raise forms.ValidationError(_('The url is needed'))
        if 'http' not in url:
            raise forms.ValidationError(_('The url should starts with http(s)'))
        return url

    def clean_logo(self):
        logo = self.cleaned_data['logo']
        if not logo:
            raise forms.ValidationError(_('The logo is needed'))
        if logo.size > 50000:
            raise forms.ValidationError(_('The logo is too big'))
        return logo

class WebSiteSubmitModelForm(ModelForm):
    class Meta:
        model = WebSiteSubmit
        fields = '__all__'
        widgets = {
            'url': TextInput(
                attrs={'autocomplete': 'off', 'autofocus': 'autofocus', 'placeholder': _('Url')}
            ),
            'name': TextInput(
                attrs={'autocomplete': 'off', 'placeholder': _('Tag')}
            ),
            'deal': HiddenInput(),
        }

class ApiModelForm(ModelForm):
    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop('request', None)
        super(ApiModelForm, self).__init__(*args, **kwargs)

    class Meta:
        model = ApiInfo
        fields = ('api', 'func', 'tag', 'url')
        widgets = {
            'api': TextInput(
                attrs={'autocomplete': 'off', 'autofocus': 'autofocus', 'placeholder': _('Api')}
            ),
            'func': Textarea(
                attrs={'autocomplete': 'off', 'onscroll': 'this.rows++;', 'placeholder': _('Func')}
            ),
            'tag': TextInput(
                attrs={'autocomplete': 'off', 'placeholder': _('Tags')}
            ),
            'url': URLInput(attrs={'autocomplete': 'off', 'placeholder': _('Url')}),
        }

    def clean_api(self):
        """ Clean for api """

        api = strip(self.cleaned_data['api'])
        if not api:
            raise forms.ValidationError(_('Api is needed'))
        a = ApiInfo.objects.filter(api=api)
        if a.exists():
            ua = UserApiInfo.objects.filter(api=a, user=getUI(getUsr(self.request)))
            if ua.exists():
                raise forms.ValidationError(_('This api has already record by you'))
            return api
        return api

    def clean_tag(self):
        """ Clean for tag """

        tag = strip(self.cleaned_data['tag']) or ''
        return ' '.join([strip(t) for t in tag.split(' ')])


# 爬取中国大学排名
import bs4
import requests
from bs4 import BeautifulSoup

def getHTML(url):
    try:
        r=requests.get(url)
        r.raise_for_status()
        r.enconing=r.apparent_enconing
        return r.text
    except:
        return""

def getInfo(list,html):
    soup=BeautifulSoup(html,"html.parser")
    for tr in soup.find('tbody').children:
        tds=soup.find_all('td')
        list.append(u[0],u[1],u[8])

def printinfo(list,num):
    print("{:^10}\t{:^6}\t{:^10}".format("排名","学校名称","总分"))
    for i in range(num):
        u=list[i]
        print("{:^10}\t{:^6}\t{:^10}".format(u[0],u[1],u[2]))

def main():
    uinfo=[]
    url="http://www.gaokaopai.com/paihang-otype-2.html?f=1&ly=bd&city=&cate=&batch_type="
    html=getHTML(url)
    getInfo(uinfo,html)
    printinfo(uinfo,20)


import zmq
import multiprocessing as mp
import cv2
import time
import imutils

from imutils.video import VideoStream
from foosball.perception_utils import *
from foosball.angle_finder import find_angle

def camera_ps(shutdown, outfd, hwm, config, on_jetson):
    context = zmq.Context()

    socket = context.socket(zmq.PUB)
    socket.setsockopt(zmq.SNDHWM, hwm)
    socket.bind(outfd)

    if on_jetson:
        camera = VideoStream(src="nvarguscamerasrc wbmode=4 aelock=true gainrange=\"8 8\" ispdigitalgainrange=\"1 1\" exposuretimerange=\"5000000 5000000\" ! video/x-raw(memory:NVMM), "\
                            "width=(int)1280, height=(int)720, format=(string)NV12, " \
                            "framerate=(fraction)40/1 ! nvvidconv flip-method=2 ! video/x-raw, " \
                            "format=(string)BGRx ! videoconvert ! video/x-raw, " \
                            "format=(string)BGR ! appsink").start()
    else:
        camera = VideoStream('unity_5.mkv')
    time.sleep(2.0)

    frame = 0
    mask = get_mask(config['ROI'])
    filt = get_filt()
    vm = VelocityMonitor()
    roi = config['ROI']
    bt = config['BALL_THRESH']
    rt = config['ROBOT_THRESH']
    ht = config['HUMAN_THRESH']

    while not shutdown.is_set():
        # Fetch and crop frame
        image = camera.read()
        image = crop_image(image, mask)
        image = imutils.resize(image, width=500)

        # Preprocess frame for perception
        hsv = cv2.GaussianBlur(image, (11, 11), 0)
        hsv = cv2.cvtColor(hsv, cv2.COLOR_BGR2HSV)

        # Threshold frame
        ball = cv2.inRange(hsv, *bt)
        robot = cv2.inRange(hsv, *rt)
        human = cv2.inRange(hsv, *ht)

        # Postprocess thresholds
        human = cv2.erode(human, None, iterations=2)
        human = human / 255
        robot = robot / 255

        # Track ball
        try:
            ballx, bally = track_ball(ball)
            vm.pos(ballx, bally)
        except ValueError:
            ballx, bally = -1., -1.
            vm.clear()

        ballxv, ballyv = vm.velocity()
        ballxv /= config['PIXELS_PER_CM']
        ballyv /= config['PIXELS_PER_CM']

        # Track human player
        hg_roi = human[:, 0:125]
        hg_loc = get_player_pos(hg_roi, filt)
        hg_l, hg_r = get_player_bounds(hg_roi)
        hg_ang = -find_angle(*mirror_player(hg_l, hg_r, *config['GOALIE_PARAMS']), True)

        ho_roi = human[:, 250:375]
        ho_loc = get_player_pos(ho_roi, filt)
        ho_l, ho_r = get_player_bounds(ho_roi)
        ho_ang = -find_angle(*mirror_player(ho_l, ho_r, *config['OFFENSE_PARAMS']), False)

        # Track robot player
        rg_roi = robot[:, 375:500]
        rg_loc = get_player_pos(rg_roi, filt)
        rg_l, rg_r = get_player_bounds(rg_roi)
        rg_ang = find_angle(rg_l, rg_r, True)

        ro_roi = robot[:, 125:250]
        ro_loc = get_player_pos(ro_roi, filt)
        ro_l, ro_r = get_player_bounds(ro_roi)
        ro_ang = find_angle(ro_l, ro_r, False)

        # Compile track object
        tracks = {
            'frame': frame,
            'ball': {
                'x': ballx,
                'y': bally,
                'xv': ballxv,
                'yv': ballyv
            },
            'human': {
                'goalie': {
                    'loc': hg_loc,
                    'ang': hg_ang
                },
                'offense': {
                    'loc': ho_loc,
                    'ang': ho_ang
                }
            },
            'robot': {
                'goalie': {
                    'loc': rg_loc,
                    'ang': rg_ang
                },
                'offense': {
                    'loc': ro_loc,
                    'ang': ro_ang
                }
            }
        }

        print(f"Sending frame {frame}")
        try:
            socket.send_pyobj(tracks, flags=zmq.NOBLOCK)
        except zmq.error.Again:
            print("hwm hit!")

        frame += 1

class Camera:
    hwm = 10

    def __init__(self, endpoint, config, is_jetson=True):
        self.outfd = endpoint
        self.shutdown = mp.Event()
        self.psargs = (self.shutdown, self.outfd, self.hwm, config, is_jetson)
        self.ps = None

    def start(self):
        if self.ps is not None:
            raise RuntimeError("Camera already running!")

        self.ps = mp.Process(target=camera_ps, args=self.psargs)
        self.ps.daemon = True
        self.ps.start()
        print(self)

    def stop(self):
        if self.ps is None:
            raise RuntimeError("Camera not running")

        self.shutdown.set()
        self.ps.join()
        self.ps = None
        self.shutdown.clear()

    def __repr__(self):
        rpr = "-------Camera-------\n"
        rpr += f"OUT: {self.outfd}\n"
        rpr += f"HWM: {self.hwm}\n"

        return rpr


#!/usr/bin/python
# -*- coding: utf-8 -*-
"""Surface tension from capillarity shape and scale parameters and expansion data."""
from misura.canon.logger import get_module_logging
logging = get_module_logging(__name__)
import veusz.plugins as plugins
import numpy as np
from scipy import interpolate

class DensityFunction(object):

    """Callable returning density at any temperature, given a reference density and temperature and linear/volumetric expansion curves"""

    def __init__(self, rho0, T0, dil=False, T=False, ex_start=0, ex_end=0, dim='Linear'):
        logging.debug('DensityFunction',
                      rho0, T0, dil, T, ex_start, ex_end, dim)
        self.rho0 = rho0
        self.T0 = T0
        self.dil = dil
        self.T = T
        self.dim = dim
        if dil is False or T is False:
            self.f = False
            return
        # Set up expansion curve
        self.f = interpolate.interp1d(T, dil)

        # Extrapolation towards lower T
        minT = min(T)
        self.low_start = minT + min(3, minT * 3 // 100)
        self.low_end = minT + min(10, minT // 10)
        self.low_dil = self.f(self.low_start)
        low_e2 = self.f(self.low_end)
        self.low_coef = (low_e2 - self.low_dil) / \
            (self.low_end - self.low_start)
        # Reference expansion
        if T0 < self.low_start:
            # extrapolate towards lower start temperature
            dT = (self.low_start - T0)
            self.e0 = self.low_dil - dT * self.low_coef
        else:
            self.e0 = self.f(T0)
        # Extrapolation
        self.ex_end = ex_end
        self.ex_start = ex_start
        if ex_end <= 0:
            return
        # Real expansion at end of extrapolation interval
        self.ex_dil = self.f(ex_end)
        # Fixed coefficient for any dilatation above ex_end
        self.ex_coef = (self.ex_dil - self.f(ex_start)) / (ex_end - ex_start)

    def __call__(self, T):
        """Adapt density to volumetric expansion"""
        if self.f is False:
            # If T was an array, return an array of rho0:
            return (T * 0) + self.rho0

        def func(T):
            # Use extrapolation
            if self.ex_end > 0 and T > self.ex_end:
                e = self.ex_dil - self.e0 + (T - self.ex_end) * self.ex_dil
            elif T < self.low_start:
                e = self.low_dil - (self.low_start - T) * self.low_coef
            else:
                # Use interpolated function
                e = self.f(T) - self.e0
            # Adapt linear to volumetric
            if self.dim == 'Linear':
                e = (1 + e / 100) ** 3
                return self.rho0 / e
            # Already volumetric
            rho = self.rho0 * 100 / (100 + e)
        vfunc = np.vectorize(func)
        rho = vfunc(T)
        return rho

def air_density(T):
    # Source:
    # http://bouteloup.pierre.free.fr/lica/phythe/don/air/air_density_plot.pdf
    return 360.77819 * ((T + 273.15) ** -1.00336)

g = 9.80665

class SurfaceTensionPlugin(plugins.DatasetPlugin):

    """Dataset plugin to perform operations between couples of x,y datasets."""
    # tuple of strings to build position on menu
    menu = ('Misura', 'Surface Tension')
    # internal name for reusing plugin later
    name = 'SurfaceTension'
    # string which appears in status bar
    description_short = 'Calculate surface tension from runtime capillary parameters.'

    # string goes in dialog box
    description_full = ('Calculate surface tension from capillary shape parameters,'
                        'scaling factor, temperature,'
                        'sample expansion curve, media expansion curve.')

    MIN_TEMPERATURE_VARIATION = 100

    def __init__(self, beta='', R0='', T='', rho0=1000, T0=25, dil='', dilT='', ex_start=0, ex_end=0, dim='Linear',
                            grho0=air_density(25), gT0=25, gdil='', gdilT='', gdim='Volumetric', ds_out='', temperature_dataset=[0, MIN_TEMPERATURE_VARIATION]):
        """Define input fields for plugin."""

        default_sample_expansion = dil
        default_sample_expansion_temperature = dilT

        if max(temperature_dataset) - min(temperature_dataset) < self.MIN_TEMPERATURE_VARIATION:
            default_sample_expansion = ''
            default_sample_expansion_temperature = ''

        self.fields = [
            plugins.FieldDataset(
                'beta', 'Capillarity factor dataset', default=beta),
            plugins.FieldDataset('R0', 'Scaling factor dataset', default=R0),
            plugins.FieldDataset(
                'T', u'Sample temperature (\u00B0C)', default=T),

            plugins.FieldFloat(
                "rho0", descr=u"Reference sample density (kg/m\u00B3)", default=rho0),
            plugins.FieldFloat(
                "T0", descr=u"Reference sample temperature (\u00B0C)", default=T0),
            plugins.FieldDataset(
                'dil', 'Sample expansion [blank=fixed] (%)', default=default_sample_expansion),
            plugins.FieldDataset(
                'dilT', u'Sample expansion temperature [blank=as beta] (\u00B0C)', default=default_sample_expansion_temperature),
            plugins.FieldFloat(
                'ex_start', u'Extrapolate below temperature [0=no] (\u00B0C)', default=ex_start),
            plugins.FieldFloat(
                'ex_end', u'Extrapolate above temperature [0=no] (\u00B0C)', default=ex_end),

            plugins.FieldCombo("dim", descr="Sample expansion dimension", items=[
                               'Linear', 'Volumetric'], default=dim),

            plugins.FieldFloat(
                "grho0", descr=u"Reference gas density (kg/m\u00B3)", default=grho0),
            plugins.FieldFloat(
                "gT0", descr=u"Reference gas temperature (\u00B0C)", default=gT0),

            plugins.FieldDataset(
                'gdil', 'Gas expansion [blank=air] (%)', default=gdil),
            plugins.FieldDataset(
                'gdilT', 'Gas expansion temperature [blank=air] (%)', default=gdilT),

            plugins.FieldCombo("gdim", descr="Gas expansion dimension", items=[
                               'Linear', 'Volumetric'], default=gdim),

            plugins.FieldDataset(
                'ds_out', 'Output dataset name', default=ds_out)
        ]
        self.error = 0

    def getDatasets(self, fields):
        """Returns single output dataset (self.ds_out).
        This method should return a list of Dataset objects, which can include
        Dataset1D, Dataset2D and DatasetText
        """
        # raise DatasetPluginException if there are errors
        if len(set([fields['beta'], fields['R0'], fields['T']])) < 3:
            raise plugins.DatasetPluginException(
                'Capillarity, scaling and temperature datasets must differ.')
        if fields['ds_out'] in set([fields['beta'], fields['R0'], fields['T'], '', fields['dil'], fields['dilT'], fields['gdil'], fields['gdilT']]):
            raise plugins.DatasetPluginException(
                'Input and output datasets cannot be the same.')
        # make a new dataset with name in fields['ds_out']
        self.ds_out = plugins.Dataset1D(fields['ds_out'])
        self.error = 0
        # return list of datasets
        return [self.ds_out]

    def updateDatasets(self, fields, helper):
        # Capillarity data
        beta = np.array(helper.getDataset(fields['beta']).data)
        R0 = np.array(helper.getDataset(fields['R0']).data)
        # Sample expansion data
        rho0 = fields['rho0']
        T0 = fields['T0']
        dsample = None
        if fields['T'] == '':
            T = np.ones(len(beta)) * T0
            dsample = np.ones(len(beta)) * rho0
        else:
            T = np.array(helper.getDataset(fields['T']).data)
        if len(set([len(beta), len(R0), len(T)])) > 1:
            raise plugins.DatasetPluginException(
                'Sample datasets must have same length')

        # Sample expansion data
        dim = fields['dim']
        ex_start = fields['ex_start']
        ex_end = fields['ex_end']
        # Disable if one is 0 or negative
        if ex_start * ex_end <= 0:
            ex_start, ex_end = 0, 0
        if fields['dil'] == '':
            dil = False
            dilT = False
        else:
            dil = np.array(helper.getDataset(fields['dil']).data)
            # Use the same T array
            if fields['dilT'] == '':
                dilT = T
            else:
                dilT = np.array(helper.getDataset(fields['dilT']).data)
            if len(dil) != len(dilT):
                raise plugins.DatasetPluginException(
                    'Sample expansion/T datasets must have same length')

        # Gas expansion data
        grho0 = fields['grho0']
        gT0 = fields['gT0']
        gdim = fields['gdim']
        if fields['gdil'] == '':
            gdil, gdilT = False, False
        else:
            gdil = np.array(helper.getDataset(fields['gdil']).data)
            gdilT = np.array(helper.getDataset(fields['gdilT']).data)
            if len(dil) != len(dilT):
                raise plugins.DatasetPluginException(
                    'Gas expansion/T datasets must have same length')

        # Correct dil curves starting at 100%
        if dil is not False:
            if dil[0] > 50:
                dil -= 100

        # Sample density vector
        if dsample is None:
            df = DensityFunction(rho0, T0, dil, dilT, ex_start, ex_end, dim)
            dsample = df(T)
        # Gas density vector
        if not gdil:
            gdf = air_density
        else:
            gdf = DensityFunction(grho0, gT0, gdil, gdilT, dim=gdim)
        dgas = gdf(T)
        # Surface tension formula
        gamma = g * (dsample - dgas) * ((R0 / 1e6) ** 2) / (beta / 1000)
        # Remove nan/inf due to missing betas (beta=0)
        gamma[np.isnan(gamma) + np.isinf(gamma)] = 0
        self.ds_out.update(data=gamma)
        return gamma

# add plugin classes to this list to get used
plugins.datasetpluginregistry.append(SurfaceTensionPlugin)


# pylint: disable=no-member,line-too-long

from __future__ import print_function

from builtins import str # # pylint: disable=redefined-builtin

import base64
import gzip
import json

import io

import six

from nacl.public import PublicKey, PrivateKey, Box

from django.conf import settings
from django.core.management.base import BaseCommand

from ...decorators import handle_lock
from ...models import DataBundle, install_supports_jsonfield

class Command(BaseCommand):
    help = 'Prints the content of a bundle to the terminal, uncompressing and decrypting if necessary.'

    def add_arguments(self, parser):
        parser.add_argument('bundle_pk',
                            nargs=1,
                            type=int,
                            help='PK of bundle to dump')

    @handle_lock
    def handle(self, *args, **options): # pylint: disable=too-many-locals, too-many-branches, too-many-statements
        supports_json = install_supports_jsonfield()

        bundle = DataBundle.objects.get(pk=options['bundle_pk'][0])

        if supports_json is False:
            bundle.properties = json.loads(bundle.properties)

        if bundle.encrypted:
            if 'nonce' in bundle.properties and 'encrypted' in bundle.properties:
                payload = base64.b64decode(bundle.properties['encrypted'])
                nonce = base64.b64decode(bundle.properties['nonce'])

                private_key = PrivateKey(base64.b64decode(settings.PDK_SERVER_KEY).strip()) # pylint: disable=line-too-long
                public_key = PublicKey(base64.b64decode(settings.PDK_CLIENT_KEY).strip()) # pylint: disable=line-too-long

                box = Box(private_key, public_key)

                decrypted_message = box.decrypt(payload, nonce)

                decrypted = six.text_type(decrypted_message, encoding='utf8')

                if bundle.compression != 'none':
                    compressed = base64.b64decode(decrypted)

                    if bundle.compression == 'gzip':
                        fio = io.StringIO(compressed)  # io.BytesIO for Python 3
                        gzip_file_obj = gzip.GzipFile(fileobj=fio)
                        payload = gzip_file_obj.read()
                        gzip_file_obj.close()

                        decrypted = payload

                bundle.properties = json.loads(decrypted)
            elif 'encrypted' in bundle.properties:
                print('Missing "nonce" in encrypted bundle. Cannot decrypt bundle ' + str(bundle.pk) + '. Skipping...')
            elif 'nonce' in bundle.properties:
                print('Missing "encrypted" in encrypted bundle. Cannot decrypt bundle ' + str(bundle.pk) + '. Skipping...')
        elif bundle.compression != 'none':
            compressed = base64.b64decode(bundle.properties['payload'])

            if bundle.compression == 'gzip':
                fio = io.StringIO(compressed)  # io.BytesIO for Python 3
                gzip_file_obj = gzip.GzipFile(fileobj=fio)
                payload = gzip_file_obj.read()
                gzip_file_obj.close()

                bundle.properties = json.loads(payload)

        print(json.dumps(bundle.properties, indent=2))


import os

from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ObjectProperty, StringProperty
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen

from libs.navigationdrawer import NavigationDrawer
from kivymd.theming import ThemeManager

from features import BookList, MainScreenManagement, BookScreenManagement
from config import KV_DIR
from ui import StartScreen, SearchBookScreen, AddBookScreen, SearchAuthorScreen, SearchTopicScreen, AboutScreen

class Navigator(NavigationDrawer):
    title = StringProperty('Navigation')

class BookStoreApp(App, MainScreenManagement, BookList, BookScreenManagement):
    theme_cls = ThemeManager()
    nav_drawer = ObjectProperty()

    def build(self):
        self.load_all_view_files(KV_DIR)

        self.main_screen = StartScreen()
        self.nav_drawer = Navigator()

        self.main_screen_manager = self.main_screen.ids.main_manager

        # setting up menu links

        self.screens = [SearchBookScreen(name="search_book"),
                        AddBookScreen(name="add_book"),
                        SearchAuthorScreen(name="search_author"),
                        SearchTopicScreen(name="search_topic"),
                        AboutScreen(name="about")]

        btn = Button(text="suka")
        # self.screens[0].add_widget(btn)
        self.main_screen_manager.add_widget(self.screens[0])
        self.main_screen_manager.add_widget(self.screens[1])
        self.main_screen_manager.add_widget(self.screens[2])
        self.main_screen_manager.add_widget(self.screens[3])
        self.main_screen_manager.add_widget(self.screens[4])
        # self.book_screen_manager = self.root.ids.book_manager
        # self.book_screen_manager = self.main_screen.ids.book_manager

        return self.main_screen

    def load_all_view_files(self, directory_kv_files):
        for kv_file in os.listdir(directory_kv_files):
            kv_file = os.path.join(directory_kv_files, kv_file)
            if os.path.isfile(kv_file):
                with open(kv_file) as kv:
                    Builder.load_string(kv.read())

    def add_screens(self, name_screen, screen_manager, new_screen):
        screen = Screen(name=name_screen)
        screen.add_widget(new_screen)
        screen_manager.add_widget(screen)
        screen_manager.current = name_screen

    # Navigation menu links
    def show_about(self, *args):
        self.root.ids.main_manager.current = "about"
        return True

    def show_search_book(self, *args):
        self.root.ids.main_manager.current = "search_book"
        return True

    def show_add_book(self, *args):
        self.root.ids.main_manager.current = "add_book"
        return True

    def show_search_author(self, *args):
        self.root.ids.main_manager.current = "search_author"
        return True

    def show_search_topic(self, *args):
        self.root.ids.main_manager.current = "search_topic"
        return True

BookStoreApp().run()