from chalice import Chalice

app = Chalice(app_name='chalice-app')
app.debug = True
app.api.cors = True

from base import Session, engine, Base
from models import User, UserTeams, Team
import json
session = Session()

@app.route('/create_schema')
def create_schema():
    Base.metadata.create_all(engine)

@app.route('/teams/{name}', methods=['POST'])
def create_team(name):
    team = Team(name=name)
    session.add(team)
    session.commit()
    session.close()
    return {"team": name}

@app.route('/teams', methods=['GET'])
def list_all_teams():
    teams = session.query(Team).all()
    session.close()
    data = {}
    for team in teams:
        data[team.id] = team.name

    json_data = json.dumps(data)

    return json_data

@app.route('/users', methods=['GET'])
def list_all_users():
    users = session.query(User).all()
    session.close()
    data = {}
    for user in users:
        data[user.id] = user.email

    json_data = json.dumps(data)

    return json_data

@app.route('/users/{user_id}/listteams')
def list_user_teams(user_id):
    teams = session.query(UserTeams) \
    .filter(UserTeams.user_id == user_id) \
    .all()
    print(teams)
    data = {}
    for team in teams:
        row = {}
        row["member"] = team.users.email
        row["team"] = team.teams.name
        row["position"] = team.position
        data[team.team_id] = row

    json_data = json.dumps(data)
    session.close()

    return json_data

@app.route('/teams/{team_id}/members', methods=['GET'])
def list_team_members(team_id, sort=False):
    users = session.query(UserTeams) \
    .filter(UserTeams.team_id == team_id) \
    .all()

    data = {}
    for user in users:
        row = {}
        row["member"] = user.users.email
        row["position"] = user.position
        data[user.user_id] = row

    json_data = json.dumps(data)
    session.close()

    return json_data

@app.route('/users/{email}', methods=['POST'])
def create_user(email):
    user = User(email=email)
    session.add(user)
    session.flush()

    data = {}
    data["id"] = user.id
    data["email"] = user.email

    session.commit()
    session.close()
    return data

@app.route('/users/{email}', methods=['PUT'], content_types=['application/json'])
def update_user(email):
    body = app.current_request.json_body
    user = session.query(User) \
    .filter(User.email == email) \
    .one()

    user.email = body["new_email"]
    session.commit()
    session.close()

    return {'user': email}

@app.route('/teams/{team_id}/members', methods=['POST'], content_types=['application/json'])
def team_add_member(team_id):
    body = app.current_request.json_body
    print(body)
    userteam = UserTeams(team_id=team_id, user_id=body["user_id"], position=body["position"])
    session.add(userteam)
    session.commit()
    session.close()
    return {"team": body["team_id"], "user": body["user_id"], "position": body["position"]}
    # find team, user and create association

@app.route('/teams/{team_id}/members', methods=['PUT'], content_types=['application/json'])
def team_update_member(team_id):
    body = app.current_request.json_body
    user = session.query(UserTeams) \
    .filter(UserTeams.team_id == team_id) \
    .filter(UserTeams.user_id == body["user_id"]) \
    .all()

    user.position = body["position"]
    user.active = body["active"]

    session.commit()
    session.close()
    return True

@app.route('/teams/{team_id}/members', methods=['DELETE'], content_types=['application/json'])
def team_delete_member(team_id):
    body = app.current_request.json_body
    user = session.query(UserTeams) \
    .filter(UserTeams.team_id == team_id) \
    .filter(UserTeams.user_id == body["user_id"]) \
    .all()

    user.active = false

    session.commit()
    session.close()
    return True

@app.route('/invite', methods=['POST'], content_types=['application/json'])
def invite():
    body = app.current_request.json_body

    # Parse body, send invite link to user

    return True

@app.route('/invite/accept/{team_id}/{email}/{position}', methods=['POST'])
def accept_invite(team_id, email, position):
    user = create_user(email)

    userteam = UserTeams(team_id=team_id, user_id=user["id"], position=position)
    session.add(userteam)
    session.commit()
    session.close()

    return {"team": team_id, "user": email, "position": position}

@app.route('/')
def index():
    return {'hello': 'world'}


"""Module for kjoreplan service."""
import datetime
import logging
from typing import Any, List

class KjoreplanService:
    """Class representing kjoreplan service."""

    async def get_all_heat(self, db: Any) -> List:
        """Get all heat / kjøreplan function."""
        kjoreplan = []
        cursor = db.kjoreplan_collection.find()
        for document in await cursor.to_list(length=500):
            kjoreplan.append(document)
            logging.debug(document)
        return kjoreplan

    async def get_upcoming_heat(self, db: Any, count: int) -> List:
        """Get the next upcoming heat (count)."""
        kjoreplan = []
        i = 0

        timenow = datetime.datetime.now().strftime("%X")
        logging.debug(timenow)

        cursor = db.kjoreplan_collection.find()
        for document in await cursor.to_list(length=500):
            if (document["Start"] > timenow) and (i < count):
                i = i + 1
                kjoreplan.append(document)
                logging.debug(document)
        return kjoreplan

    async def get_heat_by_klasse(self, db: Any, lopsklasse: str) -> List:
        """Get all heat / kjøreplan for a given klasse."""
        kjoreplan = []
        cursor = db.kjoreplan_collection.find({"Løpsklasse": lopsklasse})
        for document in await cursor.to_list(length=500):
            kjoreplan.append(document)
            logging.debug(document)
        return kjoreplan

    async def get_heat_for_live_scroll(self, db: Any, lopsklasse: str) -> List:
        """Get heat / kjøreplan for live scroll."""
        # This method will reduce number of heat to dislpay in live view
        # SemiC does not exist -> show all heat
        # Elif semifinal result registered -> show semi and finals
        # Else -> show quarter and semi finals
        semi_c = False
        semi_result = False
        semi_not_started = True
        kjoreplan = []
        tmp_kjoreplan = []
        cursor = db.kjoreplan_collection.find({"Løpsklasse": lopsklasse})
        # loop throgh heat and determine size and status
        for document in await cursor.to_list(length=500):
            tmp_kjoreplan.append(document)
            logging.debug("Heat: " + document["Heat"])
            if document["Heat"][:2] == "SC":
                semi_c = True
                logging.debug("Found SemiC")
            if (document["Heat"][0] == "S") and (document["resultat_registrert"]):
                semi_result = True
                semi_not_started = False
                logging.debug("Found Semi result")

        # filter out non relevant heat for live view
        for heat in tmp_kjoreplan:
            if semi_c and semi_result and heat["Heat"][0] == "K":
                logging.debug("Ignored kvart - " + heat["Heat"])
            elif semi_c and semi_not_started and heat["Heat"][0] == "F":
                logging.debug("Ignored finale - " + heat["Heat"])
            else:
                kjoreplan.append(heat)
                logging.debug(heat["Heat"])

        return kjoreplan

    async def create_kjoreplan(self, db: Any, body: Any) -> int:
        """Create kjoreplan function. After deletion of existing instances, if any."""
        returncode = 201
        collist = await db.list_collection_names()
        logging.debug(collist)

        # format time
        for heat in body:
            # Format time from decimal to readable format hh:mm:ss:
            time = heat["Start"].replace(",", ".")
            heat["Start"] = _format_time(time)

        # if kjøreplan is updated, only change the heats without resultat_registrert
        if "kjoreplan_collection" in collist:
            returncode = 202
            for heat in body:
                result = await db.kjoreplan_collection.find_one(
                    {"Index": heat["Index"]}
                )
                logging.debug(heat["Index"])

                if result and "resultat_registrert" in result:
                    # resultat registrert - heat kan ikke endres
                    logging.debug("Ignorert: " + result["Index"])
                else:
                    result = await db.kjoreplan_collection.update_one(
                        {"Index": heat["Index"]}, {"$set": heat}
                    )
                    logging.debug(result)
        else:
            result = await db.kjoreplan_collection.insert_many(body)
            logging.debug("inserted %d docs" % (len(result.inserted_ids),))
            _newvalue = {"resultat_registrert": False}
            result = await db.kjoreplan_collection.update_many({}, {"$set": _newvalue})
            logging.debug(result)

        # update tidplan if kjøreplan exists
        if "klasser_collection" in collist:
            await KjoreplanService().update_tidplan(db)
            logging.debug("Updating tidplan")

        return returncode

    async def update_registrer_resultat(self, db: Any, heat: str) -> None:
        """Create kjoreplan function."""
        _myquery = {"Index": heat}
        _newvalue = {"resultat_registrert": True}
        result = await db.kjoreplan_collection.update_one(_myquery, {"$set": _newvalue})
        logging.debug(result)

    async def get_heat_by_index(self, db: Any, index: str) -> dict:
        """Get one klass by lopsklasse function."""
        heat = await db.kjoreplan_collection.find_one({"Index": index})
        logging.debug(heat)
        return heat

    # TODO: reduser kompleksistet i denne funksjonen
    async def update_tidplan(self, db: Any) -> int:  # noqa: C901
        """Update tidplan function. Will update klasser object, requires Kjøreplan created."""
        returncode = 201

        # get klasser
        klasser = []
        cursor = db.klasser_collection.find()
        for document in await cursor.to_list(length=100):
            klasser.append(document)

        # get heat
        kjoreplan = []
        cursor = db.kjoreplan_collection.find()
        for document in await cursor.to_list(length=500):
            kjoreplan.append(document)

        # loop through klasser and kjøreplan - update start time pr round
        for klasse in klasser:
            klasse["SemiC"] = False
            klasse["FinaleC"] = False
            klasse["FinaleB"] = False
            _myquery = {"Klasse": klasse["Klasse"]}
            for heat in kjoreplan:
                if klasse["Løpsklasse"] == heat["Løpsklasse"]:
                    if heat["Heat"] == "KA1":
                        _newvalue = {"TidKvart": heat["Start"]}
                        result = await db.klasser_collection.update_one(
                            _myquery, {"$set": _newvalue}
                        )
                        logging.debug(result)
                        returncode = 202
                    elif heat["Heat"] == "SC1":
                        _newvalue = {"TidSemi": heat["Start"]}
                        result = await db.klasser_collection.update_one(
                            _myquery, {"$set": _newvalue}
                        )
                        logging.debug(result)
                        klasse["SemiC"] = True
                    elif heat["Heat"] == "SA1":
                        if klasse["SemiC"] is False:
                            _newvalue = {"TidSemi": heat["Start"]}
                            result = await db.klasser_collection.update_one(
                                _myquery, {"$set": _newvalue}
                            )
                            logging.debug(result)
                    elif heat["Heat"] == "FC":
                        _newvalue = {"TidFinale": heat["Start"]}
                        result = await db.klasser_collection.update_one(
                            _myquery, {"$set": _newvalue}
                        )
                        klasse["FinaleC"] = True
                        logging.debug(result)
                    elif heat["Heat"] == "FB":
                        klasse["FinaleB"] = True
                        if klasse["FinaleC"] is False:
                            _newvalue = {"TidFinale": heat["Start"]}
                            result = await db.klasser_collection.update_one(
                                _myquery, {"$set": _newvalue}
                            )
                            logging.debug(result)
                    elif heat["Heat"] == "FA":
                        if (klasse["FinaleC"] or klasse["FinaleB"]) is False:
                            _newvalue = {"TidFinale": heat["Start"]}
                            result = await db.klasser_collection.update_one(
                                _myquery, {"$set": _newvalue}
                            )
                            logging.debug(result)
                    elif heat["Heat"] == "F1":
                        _newvalue = {"TidKvart": heat["Start"]}
                        result = await db.klasser_collection.update_one(
                            _myquery, {"$set": _newvalue}
                        )
                        logging.debug(result)
                    elif heat["Heat"] == "A1":
                        _newvalue = {"TidSemi": heat["Start"]}
                        result = await db.klasser_collection.update_one(
                            _myquery, {"$set": _newvalue}
                        )
                        logging.debug(result)

        return returncode

def _format_time(decimal_time: str) -> str:
    """Format time from decimal to readable format hh:mm:ss."""
    sekunder = int(round(float(decimal_time) * 24 * 60 * 60, 0))
    min = divmod(sekunder, 60)
    hour = divmod(min[0], 60)
    if hour[0] < 10:
        ut_hour = "0" + str(hour[0])
    else:
        ut_hour = str(hour[0])
    if hour[1] < 10:
        ut_min = "0" + str(hour[1])
    else:
        ut_min = str(hour[1])
    if min[1] < 10:
        ut_sek = "0" + str(min[1])
    else:
        ut_sek = str(min[1])
    logging.debug("Tid: " + ut_hour + ":" + ut_min + ":" + ut_sek)

    return ut_hour + ":" + ut_min + ":" + ut_sek


import logging
from config import BOT_TOKEN
from aiogram import Bot, Dispatcher, executor, types
from aiogram.utils.markdown import italic, link
from aiogram.utils.emoji import emojize
from aiogram.types import ParseMode
from aiogram.dispatcher.filters.state import State, StatesGroup
from aiogram.dispatcher.filters import Text
from aiogram.dispatcher import FSMContext
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from imdb_worker import (
    find_movies_by_query,
    get_movie_info,
    get_movie_link_hdrezka,
    get_youtube_trailer,
)
from afisha_worker import get_today_movies

# инициализация
logging.basicConfig(level=logging.INFO)
bot = Bot(token=BOT_TOKEN)
dp = Dispatcher(bot, storage=MemoryStorage())

# состояния
class FindMovie(StatesGroup):
    movie_name = State()

# приветствие
@dp.message_handler(commands=["start"])
async def send_welcome(message: types.Message):
    await message.answer(
        f'Привет, я бот-помощник в поиске информации о фильмах {emojize(":robot:")}\nДля помощи есть команда /help',
        parse_mode=ParseMode.HTML,
    )

# помощь
@dp.message_handler(commands=["help"])
async def send_help(message: types.Message):
    await message.answer(
        f'Для поиска информации о фильме или сериале набери команду /find\nЧтобы вывести список фильмов в прокате набери команду /cinema\n\n{emojize(":hourglass:")}запросы могут выполняться с задержкой для избежания капчи {emojize(":hourglass:")}',
        parse_mode=ParseMode.HTML,
    )

# что сейчас идет в кино
@dp.message_handler(commands=["cinema"])
async def handle_cinema(message: types.Message):
    movies = await get_today_movies()

    # генерируем кнопки для фильмов
    buttons = []
    for movie in movies:
        buttons.append(
            types.InlineKeyboardButton(
                text=f'{movie["title"]} [{movie["tags"][0]}]',
                callback_data=movie["link"],
            )
        )
    keyboard = types.InlineKeyboardMarkup(row_width=1, resize_keyboard=False)
    keyboard.add(*buttons)

    await message.answer(
        f'{emojize(":clapper:")}Сегодня в кино, подробная информация по нажатию:',
        reply_markup=keyboard,
        parse_mode=ParseMode.HTML,
    )

# форма по поиску фильма
def register_handlers_find_movie(dp_: Dispatcher):
    dp_.register_message_handler(handle_find, commands="find", state="*")
    dp_.register_message_handler(make_query, state=FindMovie.movie_name)

@dp.message_handler(commands=["find"])
async def handle_find(message: types.Message):
    await message.answer(
        f'{emojize(":movie_camera:")} Введите название интересующего вас фильма или сериала:',
        parse_mode=ParseMode.MARKDOWN,
    )
    await FindMovie.movie_name.set()

@dp.message_handler(state=FindMovie.movie_name, content_types=types.ContentTypes.TEXT)
async def make_query(message: types.Message, state: FSMContext):
    await message.answer(italic("Ожидайте") + "...", parse_mode=ParseMode.MARKDOWN)

    # пытаемся предложить варианты фильмов с похожим названием
    movies = await find_movies_by_query(message.text)
    if len(movies) == 0:
        await message.answer(
            f'{emojize(":zap:")} Я *не* нашел фильмы с таким названием, введите другой запрос:',
            parse_mode=ParseMode.MARKDOWN,
        )
        return

    # генерируем кнопки для фильмов
    buttons = []
    for movie in movies:
        buttons.append(
            types.InlineKeyboardButton(
                text=f'{movie["name"]} {movie["original_name"]} {movie["year"]} \n',
                callback_data=movie["link"],
            )
        )
    keyboard = types.InlineKeyboardMarkup(row_width=1, resize_keyboard=False)
    keyboard.add(*buttons)

    await message.answer("Выберите нужный вам фильм:", reply_markup=keyboard)
    await state.finish()

@dp.callback_query_handler(Text(startswith="https://www.kinopoisk.ru/"))
async def callback_imdb(call: types.CallbackQuery):
    await call.message.answer(italic("Ожидайте") + "...", parse_mode=ParseMode.MARKDOWN)

    # получаем информацию о фильме и выводим в красивом формате
    movie = await get_movie_info(call.data)
    msg = ""
    fields = [
        "Год производства",
        "Страна",
        "Жанр",
        "Слоган",
        "Бюджет",
        "Время",
        "Режиссер",
        "Композитор",
    ]

    if movie["original_title"]:
        msg += f'
{movie["title"]}
 [{movie["original_title"]}]'
    else:
        msg += f'
{movie["title"]}
'
    if "Возраст" in movie:
        msg += f'
{movie["Возраст"]}
'
    msg += "\n\n"

    was_rating = False
    if movie["imdb_rating"]:
        msg += f'Рейтинг IMDb:
{movie["imdb_rating"]}
\n'
        was_rating = True
    if movie["kp_rating"]:
        msg += f'Рейтинг КиноПоиска:
{movie["kp_rating"]}
\n'
        was_rating = True
    if was_rating:
        msg += "\n"

    for field in fields:
        if field in movie:
            msg += f"{field}: {movie[field]} \n"

    if movie["actors"]:
        msg += f'Основные актёры: {", ".join(movie["actors"])} \n'

    if movie["about"]:
        msg += f'\n    {movie["about"]}\n'

    # добавим кнопки к каждой выдаче
    buttons = []

    if movie["original_title"] is not None:
        watch_online_link = get_movie_link_hdrezka(
            movie["title"] + " " + movie["original_title"]
        )
    else:
        watch_online_link = get_movie_link_hdrezka(movie["title"])

    # кнопка для перехода на сайт для просмотра
    if watch_online_link:
        buttons.append(
            types.InlineKeyboardButton(
                text="Смотреть бесплатно на HDrezka", url=watch_online_link
            )
        )

    # кнопка для получения трейлера
    trailer_link = get_youtube_trailer(f'{movie["title"]} трейлер')
    if trailer_link:
        buttons.append(
            types.InlineKeyboardButton(
                text="Вывести трейлер", callback_data=trailer_link
            )
        )
    else:
        trailer_link = get_youtube_trailer(
            f'{movie["original_title"]} official trailer'
        )
        if trailer_link:
            buttons.append(
                types.InlineKeyboardButton(
                    text="Вывести трейлер", callback_data=trailer_link
                )
            )

    # строим клавиатуру
    keyboard = types.InlineKeyboardMarkup(row_width=1, resize_keyboard=False)
    keyboard.add(*buttons)

    # если доступен постер, отправляем его
    if movie["poster"] is not None:
        await bot.send_photo(chat_id=call.message.chat.id, photo=movie["poster"])

    # выводим информацию о фильме вместе с кнопками
    await call.message.answer(msg, parse_mode=ParseMode.HTML, reply_markup=keyboard)
    await call.answer()

@dp.callback_query_handler(Text(startswith="https://www.youtube.com/watch"))
async def callback_youtube(call: types.CallbackQuery):
    # показываем трейлер прямо в чате
    await call.message.answer(
        link("Сейчас появится трейлер", call.data), parse_mode=ParseMode.MARKDOWN
    )

if __name__ == "__main__":
    executor.start_polling(dp, skip_updates=True)


from node_huffman import node_huffman
# If you import an interface / class you have
# access to the class functions -_-
# input: information (words) to be encoded.
# output: a min heap as a priority queue!
import heapq
class minheap:
    # a function to remove nodes
    # a function to create a node
    # a function to check if there is only one node
    # a function to compare values to know where to insert or remove.
    def __init__():
        self.root = None

#this is cool. I didn't instantiate node_huffman yet.
node_huffman.dummy()
minheap = heapq.heapify([('k1',3),('k2',6)])
print(minheap)


print("*** Fun with countdown ***")
input_number = (input("Enter List : ")).split()

def trans_to_int(input_list):
    traned_list = []
    for i in input_list:
        traned_list.append(int(i))
    return traned_list

def countdown(input_number):
    countdown_list = []
    bus = []
    before_number = -10
    for i in input_number:
        if before_number - i == 1 and before_number not in bus:
            bus.append(before_number)
            bus.append(i)
            if before_number == 2 and i == 1:
                countdown_list.append(bus)

        elif before_number - i == 1 and before_number in bus and int(i) == 1:
            bus.append(i)
            countdown_list.append(bus)

        elif before_number - i == 1 and before_number in bus:
            bus.append(i)

        elif before_number - i != 1 and i != 1:
            bus = []

        elif i == 1:
            countdown_list.append([1])

        before_number = i

    return countdown_list

def count_list(countdown_list:list):
    counted_list = []
    count = 0
    for i in countdown_list:
        count += 1
    counted_list.append(count)
    counted_list.append(countdown_list)
    return counted_list

print(count_list(countdown(trans_to_int(input_number))))


class Solution:
    def longestArithSeqLength(self, nums: List[int]) -> int:
        dp = {}
        max_len = 0
        dp[0] = {}
        for i in range(1, len(nums)):
            dp[i] = {}
            for j in range(0, i):
                diff = nums[i]-nums[j]
                if diff in dp[j]:
                    dp[i][diff] = dp[j][diff]+1
                else:
                    dp[i][diff] = 2
                max_len = max(max_len, dp[i][diff])
        return max_len


def rational_reconstruct_poly(p, m, n_deg = 0, d_deg = 0):
    """
    Construct a rational function n/d such that p*d is equivalent to n modulo m.

    returns (n, d)

    Examples:
        sage: P.<z> = QQ[]
        sage: p = -z**16 - z**15 - z**14 + z**13 + z**12 + z**11 - z**5 - z**4 - z**3 + z**2 + z + 1
        sage: m = z**21
        sage: n, d = rational_reconstruct_poly(p, m);
        sage: print n, d
        z**4 + 2*z**3 + 3*z**2 + 2*z + 1 z**10 + z**9 + z**8 + z**7 + z**6 + z**5 + z**4 +
        z**3 + z**2 + z + 1
        sage: print(p*d) % m == n
        True
    """
    # n and d are unique if m.degree() > (n.degree() + d.degree())
    if n_deg < 0 or d_deg < 0:
        raise ValueError("The degree bounds n_deg and d_deg should be positive.")

    if n_deg == 0:
        n_deg = (m.degree() - 1)//2
    if d_deg == 0:
        d_deg = (m.degree() - 1)//2

    P = p.parent()
    #XGCD until degree the degree of t1 surpasses the degree of n
    s0 = P(0);
    t0 = P(1);
    s1 = P(m);
    t1 = p % P(m);

    while n_deg < t1.degree():
        #not optimal, we should use divrem
        q = s1 // t1;
        r1 = s1 % t1;
        r0 = s0 - q*t0;
        s0 = t0
        s1 = t1
        t0 = r0
        t1 = r1

    assert(t0 != 0)
    if d_deg < t0.degree():
        raise ValueError("could not complete rational reconstruction")
    # make the denominator monic
    c = t0.leading_coefficient()
    t0 = t0.monic()
    t1 = t1/c
    return t1, t0


import jieba    # 分词
from wordcloud import WordCloud  # 制作词云图
import matplotlib.pyplot as plt  # 展示图像
import re   # 使用正则表达式
from PIL import Image   # 使用图片
import numpy as np      # 处理图片

with open("../douBan/douban_top250.txt", "r", encoding="utf8") as o:
    text = o.read()     # 导入文本
    text = re.sub('[/：:\n.|-]', '', text)  # 去除不必要的字符

# 分词
words_list = jieba.lcut(text)
words_str = ' '.join(words_list)
# 添加停词列表（除去不必要的词汇）
stop_words = open("./cn_stopwords.txt", "r", encoding="utf8").read().split("\n")
# 扩展停词列表
stop_words.extend(('No', '国家', '年份', '演员', '片名', '评分', '导演', '类型', '评价', '人数'))

# 导入背景图
background = Image.open('./1344340629730.jpg')
graph = np.array(background)

# 使用WordCloud生成词云
word_cloud = WordCloud(
    font_path='simsun.ttc',  # 设置字体
    max_font_size=80,
    min_font_size=3,
    background_color='white',
    stopwords=stop_words,
    mask=graph,     # 设置背景图
)
word_cloud.generate(words_str)

# 使用matplotlib展现结果
plt.subplots(figsize=(12, 8))
plt.imshow(word_cloud)
plt.axis("off")
plt.show()


import cv2

# Define mouse callback function
def mouse_callback(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
        print("Clicked at ({}, {})".format(x, y))

# Load image
image = cv2.imread("../data/roi_test1.jpg")

# Create window and set mouse callback function
cv2.namedWindow("Image")
cv2.setMouseCallback("Image", mouse_callback)

# Display image and wait for key press
cv2.imshow("Image", image)
cv2.waitKey(0)

# Destroy window
cv2.destroyAllWindows()


import math
# base Pr is 1st
# leavingLinks is number of links from each site
# iterationNum is number of iterations

def calcSum(sites, leavingLinks, incomingLinks, whoIAM):
    result = float(0)
    for x in range( 0, incomingLinks[whoIAM] ):
        if ( True == isinstance(leavingLinks[x], list) ):
            result = result + ( sites[x] / len( leavingLinks[x] ) )
    return result

# sites is a list, leavingLinks is a list of lists
def pR(sites, leavingLinks, incomingLinks, iterationNum=15):

    for x in range(0,iterationNum):

        if ( len(sites) == len(leavingLinks) ) :
            # A = sites[0]
            calculated = sites

            for x in range(0, len(sites) ) :
                calculated[x] = 0.85 * ( calcSum(sites, leavingLinks, incomingLinks, x) ) + (1-0.85)

        else :
            print("You did something wrong!")

        sites = calculated

    print(sites)

#Testen
#print(type([ [1,2],[0,2], 0 ]))
#print(calcSum([1, 1, 1], [ [1,2],[0,2], 0 ], [1,1,2], 2 )  )

#Erklärung
#[1,1,1] -> A(p)=1 , B(p)=1, C(p)=1,
#0 -> A, 1 -> B, 2 -> C

pR([1, 6, 13], [ [1,2] , [0,2] , [0] ], [1,1,2], 15)


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

import argparse
import itertools
import os

PREFIXES = {
    "dp4a": [
        ("batch_conv_bias_int8_implicit_gemm_precomp_ncdiv4hw4", True),
        ("batch_conv_bias_int8_gemm_ncdiv4hw4", False),
        ("batch_conv_bias_int8_gemm_ncdiv4hw4_ldg_128", False),
    ]
}

ACTIVATIONS = {1: ("IDENTITY", "_id"), 2: ("RELU", "_relu"), 3: ("H_SWISH", "_hswish")}

BIASES = {
    1: ("PerElementBiasVisitor", "_per_elem"),
    2: ("PerChannelBiasVisitor", "_per_chan"),
}

SUFFIXES = {"dp4a": [""], "imma": [""]}

def main():
    parser = argparse.ArgumentParser(
        description="generate cuda batch conv bias (dp4a/imma) kern impl files",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
    )
    parser.add_argument(
        "--type",
        type=str,
        choices=["dp4a", "imma"],
        default="dp4a",
        help="generate cuda conv bias kernel file",
    )
    parser.add_argument("output", help="output directory")
    args = parser.parse_args()

    if not os.path.isdir(args.output):
        os.makedirs(args.output)

    inst = """
template void megdnn::cuda::batch_conv_bias::do_PREFIXSUFFIX<BIAS,
        IConvEpilogue<Activation<megdnn::param_enumv::BatchConvBias::NonlineMode::ACTIVATION>>>(
        const int8_t* d_src,
        const int8_t* d_filter, WORKSPACE
        BIAS bias,
        IConvEpilogue<Activation<megdnn::param_enumv::BatchConvBias::NonlineMode::ACTIVATION>> epilogue,
        const ConvParam& param,
        float alpha,
        float beta,
        cudaStream_t stream);"""

    for prefix in PREFIXES[args.type]:
        for suffix in SUFFIXES[args.type]:
            for _, act in ACTIVATIONS.items():
                has_workspace = prefix[1]
                bias = BIASES[2]
                fname = "{}{}{}{}.cu".format(prefix[0], suffix, bias[1], act[1])
                fname = os.path.join(args.output, fname)
                with open(fname, "w") as fout:
                    w = lambda s: print(s, file=fout)
                    w("// generated by gen_batch_cuda_conv_bias_kern_impls.py")
                    cur_inst = (
                        inst.replace("PREFIX", prefix[0])
                        .replace("SUFFIX", suffix)
                        .replace("BIAS", bias[0])
                        .replace("ACTIVATION", act[0])
                    )
                    if has_workspace:
                        cur_inst = cur_inst.replace("WORKSPACE", "\nint* d_workspace, ")
                    else:
                        cur_inst = cur_inst.replace("WORKSPACE", "")
                    w('#include "../{}{}.cuinl"'.format(prefix[0], suffix))
                    w(cur_inst)

                    print("generated {}".format(fname))
    os.utime(args.output)

if __name__ == "__main__":
    main()


from PyQt5 import QtWidgets, QtGui, QtCore

class xPosition(QtWidgets.QLineEdit):
    def __init__(self, mainwindow):
        QtWidgets.QLineEdit.__init__(self)
        self.mainwindow = mainwindow
        self.setText("0.00")
        self.setParent(self.mainwindow.drawingArea)
        self.setValidator(QtGui.QDoubleValidator())
        self.returnPressed.connect(self.changeFocusToY)

    def changeFocusToY(self):
        self.mainwindow.yPos.setFocus()
        self.mainwindow.yPos.selectAll()

class yPosition(QtWidgets.QLineEdit):
    def __init__(self, mainwindow):
        QtWidgets.QLineEdit.__init__(self)
        self.mainwindow = mainwindow
        self.setText("0.00")
        self.setParent(self.mainwindow.drawingArea)
        self.setValidator(QtGui.QDoubleValidator())
        self.returnPressed.connect(self.addPointToScene)

        validator = QtGui.QDoubleValidator()
        validator.setDecimals(2)
        validator.setLocale(QtCore.QLocale())
        self.setValidator(validator)

    def addPointToScene(self):
        x = float(self.mainwindow.xPos.text()) * 1000
        y = float(self.text()) * 1000
        self.mainwindow.scene.addPoint(QtCore.QPointF(x, -y))
        self.mainwindow.xPos.setFocus()
        self.mainwindow.xPos.selectAll()

class status_bar(QtWidgets.QLabel):
    def __init__(self, mainwindow):
        QtWidgets.QTextEdit.__init__(self)
        self.mainwindow = mainwindow
        self.setParent(self.mainwindow.drawingArea)
        self.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignBottom)
        self.timer = QtCore.QTimer()
        self.timer.setInterval(2000)
        self.timer.timeout.connect(self.unshow)
        #self.setStyleSheet("background-color: yellow;")

    def show_message(self, msg):
        self.setText(msg)
        left = self.parent().mapToParent(self.parent().rect().bottomLeft()).x()
        right = self.parent().mapToParent(self.parent().rect().bottomRight()).x()
        top = self.parent().mapToParent(self.parent().rect().bottomRight()).y() - 45
        width = right - left
        self.setGeometry(left, top, width - 200, 30)
        self.setVisible(True)
        self.timer.start()

    def unshow(self):
        self.setVisible(False)

    def update_pos(self):
        left = self.parent().mapToParent(self.parent().rect().bottomRight()).x() - 300
        top = self.parent().mapToParent(self.parent().rect().bottomRight()).y() - 45
        self.setGeometry(left, top, 100, 30)


from django import forms
from django.core.validators import email_re

class MultipleEmailsField(forms.Field):
    def clean(self, value):
        """
        Check that the field contains one or more comma-separated emails
        and normalizes the data to a list of the email strings.
        """
        if not value:
            raise forms.ValidationError('Enter at least one e-mail address.')
        emails = value.split(',')
        for email in emails:
            if not email_re.match(email):
                raise forms.ValidationError('%s is not a valid e-mail address.' % email)

        # Always return the cleaned data.
        return emails


"""
@file
@brief Add references to DLL.
"""
from clr import AddReference as ClrAddReference

def AddReference(name, version=None):
    """
    Imports a :epkg:`C#` dll.

    @param      name        name of the DLL.
    @param      version     `'Release'` or `'Debug'`
    @return                 imported DLL

    If the version is None, it first tries `'Release'`
    then `'Debug'`.
    """
    try:
        return ClrAddReference(name)
    except Exception as e:
        if "Unable to find assembly" in str(e):
            import sys
            import os
            this = os.path.abspath(os.path.dirname(__file__))
            if version is None:
                vers = 'Release'
            else:
                vers = version
            rel = os.path.join(this, vers)
            if os.path.exists(os.path.join(rel, '__init__.py')):
                this = rel
            elif version is None:
                rel = os.path.join(this, "Debug")
                if not os.path.exists(os.path.join(rel, '__init__.py')):
                    raise FileNotFoundError(
                        "Unable to find folders 'Release' or 'Debug' in '{0}'".format(this))
                this = rel
            if this and os.path.exists(this):
                sys.path.append(this)
                try:
                    res = ClrAddReference(name)
                except Exception:
                    del sys.path[-1]
                    raise
                del sys.path[-1]
                return res
            else:
                raise
        else:
            raise

def add_csharpml_extension():
    """
    Imports *CSharpExtension* into global context.

    This binary has a version. On :epkg:`Windows`,
    the system might decide to skip the replacement
    of an assembly because it is in use. You can
    check the version of this by using the following code.

    .. exref::
        :title: Imports the C# extension into Python

        .. runpython::
            :showcode:

            from csharpyml.binaries import add_csharpml_extension
            from csharpyml import __version__

            add_csharpml_extension()

            # This line needs to be after the previous one.
            from CSharPyMLExtension import Constants

            vers = Constants.Version()
            print(__version__, vers)
    """
    AddReference("CSharPyMLExtension")


import os
import requests
from typing import Dict
from pprint import pprint

from flask.views import MethodView
from flask_smorest import Blueprint, abort
from marshmallow import ValidationError

from utils import get_logger
from models import Place, Review
from schemas import GoooglePlaceDataSchema, GoooglePlaceIdSchema, ApiResponseSchema
from db import db

blp = Blueprint("Google", "google", description="Google reviews for places",
                url_prefix='/api/flask/')

logger = get_logger('SMAR')

HEADERS = {
    'Content-Type': 'application/json',
    'X-Goog-Api-Key': os.environ.get("GOOGLE_API_KEY"),
    'X-Goog-FieldMask': 'places.id,places.displayName,places.formattedAddress,places.types'
}
GOOGLE_PLACE_ID_URL = 'https://places.googleapis.com/v1/places:searchText'
GOOGLE_PLACE_DATA_URL = 'https://maps.googleapis.com/maps/api/place/details/json'

def save_places(
    places_data: Dict = None,
    place_id: str = None,
    category: str = None) -> Place:
    # print(places_data.keys())
    place_data = places_data['result']
    reviews_data = place_data.get('reviews')
    category = category if category else place_data.get('types',[None])[0]
    plus_code = place_data.get('plus_code')
    if plus_code:
        city = " ".join(place_data['plus_code']['compound_code'].split(' ')[1:])
    else:
        city = 'Unknown'

    new_place = Place(
        place_id=place_id,
        formatted_phone_number=place_data.get('formatted_phone_number'),
        name=place_data.get('name'),
        rating=place_data.get('rating'),
        lat=place_data['geometry']['location']['lat'],
        lng=place_data['geometry']['location']['lng'],
        category=category.capitalize(),
        city=city
    )
    new_place.save_to_db()
    if reviews_data:
        for review_data in reviews_data:
            new_review = Review(
                author_name=review_data.get('author_name'),
                rating=review_data.get('rating'),
                text=review_data.get('text'),
                time=review_data.get('time'),
                translated=review_data.get('translated'),
                profile_photo_url=review_data.get('profile_photo_url'),
                relative_time_description=review_data.get('relative_time_description'),
                author_url=review_data.get('author_url'),
                language=review_data.get('language'),
                original_language=review_data.get('original_language'),
                place=new_place
            )
            new_review.save_to_db()

    return new_place

@blp.route("/google/place-id")
class GooglePlaces(MethodView):
    # @jwt_required(fresh=True)
    @blp.arguments(GoooglePlaceDataSchema)
    @blp.response(200, None)
    def post(self, term_data):
        text = term_data.pop('text')
        category = term_data.get('includedType')
        if category:
            category = category.lower()
            post_data = {
                "textQuery": text + f' {category}',
                "includedType": category
            }
        else:
           post_data = {
                "textQuery": text
            }

        try:
            res = requests.post(
                url=GOOGLE_PLACE_ID_URL,
                headers=HEADERS,
                json=post_data
            )
            # print(res.json())
            if res.status_code != 200:
                raise ValueError
        except ValueError as e:
            logger.error(e)
            abort(500, message="An error occurred on Backend.")

        return res.json()

@blp.route("/google/place-data")
class GooglePlaces(MethodView):
    # @jwt_required(fresh=True)
    @blp.arguments(GoooglePlaceIdSchema,location='query')
    @blp.response(200, None)
    def get(self, term_data):
        _id = term_data['id']
        includedType = term_data.get('includedType')
        params = {
            'place_id': _id,
            'fields': 'name,rating,formatted_phone_number,reviews,geometry,types,plus_code',
            'key': os.environ.get("GOOGLE_API_KEY")
        }

        try:
            place = Place.find_by_id(place_id=_id)
            if place:
                place.delete_from_db()

            res = requests.get(
                url=GOOGLE_PLACE_DATA_URL,
                params=params
            )

            if res.status_code != 200:
                raise ValueError
            data = res.json()
            # pprint(data)
            place = save_places(data, place_id=_id, category=includedType)

        except ValueError as e:
            print(e)
            logger.error(e)
            abort(500, message="An error occurred on Backend.", error=str(e))

        return ApiResponseSchema().dump({'result': place, 'status': data['status']}), 200


import pathlib
import os
import sys
import logging
from setuptools import setup
from setuptools.dist import Distribution
from pybind11.setup_helpers import Pybind11Extension

logging.basicConfig(level=logging.INFO)

# torch{audio, vision} are added here to prevent the torch upgrade when other
# packages depend on torch{audio, vision}.
REQUIRES = [
    'tqdm', '@TORCH_DEPENDENCY@', '@TORCHAUDIO_DEPENDENCY@',
    '@TORCHVISION_DEPENDENCY@'
]
VERSION = "@VERSION@"

LONG_DESCRIPTION = (
    "PopTorch is a set of extensions for PyTorch enabling "
    "models to be trained, evaluated and used on the Graphcore IPU.")

LIBS = ["*.so", "lib/*", "lib/poplar_rt/*", "lib/graphcore/lib/*.a"]

class BinaryDistribution(Distribution):
    """Distribution which always forces a binary package with platform name"""

    def has_ext_modules(self):
        return True

def get_torch_paths():
    # setup.py is executed several times, so it's ok if torch is not always
    # available.
    try:
        import torch  # pylint: disable=import-outside-toplevel
    except ModuleNotFoundError:
        return [], []
    torch_root = str(pathlib.Path(torch.__file__).parent)
    return [
        os.path.join(torch_root, "include"),
        os.path.join(torch_root, "include", "torch", "csrc", "api", "include")
    ], [os.path.join(torch_root, "lib")]

torch_include_dirs, torch_lib_dirs = get_torch_paths()
package_data = {'poptorch': LIBS}

# Copy custom codelets into the package so that we can pre-compile them later.
package_data["poptorch"].append("*.inc.cpp")

core_mod = Pybind11Extension(
    "poptorch.poptorch_core", ["src/poptorch.cpp"],
    define_macros=[("_GLIBCXX_USE_CXX11_ABI", 0)],
    include_dirs=["include"] + torch_include_dirs,
    library_dirs=["poptorch/lib"] + torch_lib_dirs,
    extra_link_args=["-Wl,--rpath=$ORIGIN/lib:$ORIGIN"],
    libraries=[
        "poptorch", "popart_compiler", "poptorch_err", "poptorch_logging",
        "torch_python", "torch"
    ],
    language="c++",
    cxx_std="17")

# Same as pybind11_add_module but without stripping the symbols and setting the visibility to hidden.
# Source: https://pybind11.readthedocs.io/en/stable/compiling.html#advanced-interface-library-targets
#
# If the symbols are stripped then error messages will only contain symbol
# addresses instead of human readable names.
core_mod.extra_compile_args = [
    f for f in core_mod.extra_compile_args
    if not "visibility=hidden" in f and not "-g0" in f
]

setup(
    name='poptorch',
    version=VERSION,
    description=LONG_DESCRIPTION,
    long_description=LONG_DESCRIPTION,
    long_description_content_type="text/markdown",
    url='http://graphcore.ai',
    author='Graphcore',
    author_email='contact@graphcore.ai',
    ext_modules=[core_mod],
    has_ext_modules=lambda: True,
    license='MIT License',
    license_files=('License.txt', 'poptorch_third_party_licenses.txt'),
    packages=['poptorch'],
    package_data=package_data,
    include_package_data=True,
    python_requires=f"=={sys.version_info.major}.{sys.version_info.minor}.*",
    platforms="@PLATFORM@",
    install_requires=REQUIRES,
    zip_safe=False,
    distclass=BinaryDistribution,
    classifiers=[
        'Intended Audience :: Developers',
        'Intended Audience :: Education',
        'Intended Audience :: Science/Research',
        'License :: OSI Approved :: MIT License',
        'Programming Language :: Python :: 3',
        'Topic :: Scientific/Engineering',
        'Topic :: Scientific/Engineering :: Mathematics',
        'Topic :: Scientific/Engineering :: Artificial Intelligence',
        'Topic :: Software Development',
        'Topic :: Software Development :: Libraries',
        'Topic :: Software Development :: Libraries :: Python Modules',
    ],
)


import json
from pathlib import Path

annotationsDir = Path('../annotations')
aggregatedAnnotations = dict()

try:
    for x in annotationsDir.iterdir():
        if x.suffix.lower() == ".json":
            with open(x, 'r') as f:
                annotationDict = json.load(f)
                annList = list(map(dict, annotationDict.values()))
                image_name_to_class_map = { it['filename'] : it['regions'][0]['region_attributes']['class'].lower() for it in annList }
                aggregatedAnnotations.update(image_name_to_class_map)

    image_name_to_class_file = '../annotations/aggragatedAnnotations.txt'
    with open(image_name_to_class_file, 'w') as json_file:
        json.dump(aggregatedAnnotations, json_file, indent = 4)
except:
    raise Exception("Failed to aggregate annotated json files")
finally:
    print("Annotation files aggragated successfully! Results were written to {}".format(image_name_to_class_file))


# Simulador de dado
# Simular o uso de um dado, gerando um valor de 1 ate 6
import random

class SimuladorDeDado:
    def __init__(self):
        self.valor_minimo = 1
        self.valor_maximo = 6
        self.mensagem = "Você gostaria de gerar um novo valor para o dado? "

    def Iniciar(self):
        resposta = input(self.mensagem)
        try:
            if resposta == "sim" or resposta == "s":
                self.GerarValorDoDado()
            elif resposta == "nao" or resposta == "n":
                print("Agradecemos sua participação!")
            else:
                print("Favor digitar sim ou nao")
        except:
            print("Ocorreu um erro ao receber sua resposta")

    def GerarValorDoDado(self):
        print(random.randint(self.valor_minimo,self.valor_maximo))

dado = SimuladorDeDado()
dado.Iniciar()


# -----------Welcome Intro----------

import os
from turtle import clear

def restart_program():
    restart = input("Would you like to restart this program?(y/n)")
    if restart == "yes" or restart == "y":
        console_app()
    if restart == "n" or restart == "no":
        print( "Program terminating...")

def console_app():

    print(""" 'User Management System' \n
    Tasks to Do: \n
    1.User Info Creation
    2.User Listing
    3.User Info Editing
    4.User Info Deleting""")

    #-------------TASKS--------------#

    try:
        global userInput
        userInput = int(input('\n Choose a number from above list:'))

    except:
        ('\n Invalid Option! Enter a valid number!!')

    finally:
        ('\n')

    # -----------FILE HANDLING LOOP-----------#
        if(userInput == 1):
            print("\nUSER INFO CREATION!!!\n")
            # -----------USER INFO CREATION-----------#

            global fileName
            fileName = input("Enter a name for new file: ")
            global f
            with open(fileName,'x') as f:
                    f.write('\nUser Information\n')
                    f.write('\nFullName\t\tBirthDate\t\tAddress\n')
                    n= int(input('Enter the number of user data you want to enter:'))
                    for i in range(n) :
                        name = (input('Enter FullName:'))
                        birthDate = (input('Enter birthdate (yyyy/mm/dd): '))
                        address= (input('Enter Address: '))
                        f.write(name )
                        f.write('\t\t')
                        f.write(birthDate)
                        f.write('\t\t')
                        f.write(address)
                        f.write('\n')
            f.close()

            console_app()
        # -----------USER LISTING-----------#

        elif(userInput == 2):
            print('\nUSER LISTING\n')
            f = open(fileName, "r")
            for name in f:
                print("{}".format(name))

            f.close()
            console_app()

        # -----------USER INFO EDITING-----------#

        elif(userInput == 3):
            print('\nUSER INFO EDITING\n')
            print("""1. EDIT FILE
                    2. ADD TO FILE""")
            choose= int(input('Choose 1 or 2:'))
            if(choose == 1):
                try:
                    f = open(fileName,'w')
                    name = (input('Enter FullName:'))
                    birthDate = (input('Enter birthdate (yyyy/mm/dd): '))
                    address= (input('Enter Address: '))
                    f.write(name )
                    f.write('\t\t')
                    f.write(birthDate)
                    f.write('\t\t')
                    f.write(address)
                    f.write('\n')

                except:
                    print("Oops! something error")
                    restart_program()

            elif(choose==2):
                            f=open(fileName,'a')
                            name = (input('Enter FullName:'))
                            birthDate = (input('Enter birthdate (yyyy/mm/dd): '))
                            address= (input('Enter Address: '))
                            f.write(name )
                            f.write('\t\t')
                            f.write(birthDate)
                            f.write('\t\t')
                            f.write(address)
                            f.write('\n')

            else:
                    print('Error! Choose a valid option.')
                    restart_program()

            f.close()
            console_app()

        # -----------USER INFO DELETING-----------#

        elif(userInput == 4):
            print('USER INFO DELETING')
            print("""1. DELETE DATA
                    2. DELETE FILE""")
            choose= int(input('Choose 1 or 2:'))
            if(choose == 1):
                try:
                    with open(fileName, 'r') as f:
                        line = f.readlines()
                    global fileName_2
                    fileName_2=input('enter a new file name:')

                    with open(fileName_2, 'w') as fw:
                        data=input('Enter the text to be deleted:')
                        for line in data:
                            if line.strip('\n') != data:
                                fw.write(line)
                        print("Deleted")
                        fw.close()

                    with open(fileName_2, 'r') as fw:
                        for name in fw:
                            print("{}".format(name))

                except:
                    print("Oops! something error")
                    restart_program()

            elif(choose==2):
                    os.remove(fileName)

            else:
                    print('Error! Choose a valid option.')
                    restart_program()

            f.close()
            console_app()

# -------------PROGRAM INVALID----------

        elif(userInput < 0 or userInput > 4):
            print(' Invalid Option! Enter a valid number!')
            restart_program()

        f.close()

console_app()


def uneFonction(texte, *args, **kwargs):
    print(texte) #Argument obligatoire
    if args: #Arguments optionnels
        print(args)
    if kwargs: #Arguments optionnels
        print(kwargs)

#Avec Args
#uneFonction() #Résultat: plantage
uneFonction("Allo")#Résultat: Allo
uneFonction("Allo", "1", 2, 3)
#Résultat: Allo
#          ('1',2,3)

#Avec Kwargs
uneFonction("Allo", valeur1 = 1, valeur2 = 2)
# Résultats: Allo
#            {'valeur1': 1, 'valeur2': 2}


import shutil
import tempfile

from django.conf import settings
from django.test import Client, TestCase, override_settings
from django.urls import reverse
from django.contrib.auth import get_user_model
from django.core.cache import cache
from .fixtures.factories import post_create, group_create, url_rev

from posts.models import Post, Comment

User = get_user_model()

TEMP_MEDIA_ROOT = tempfile.mkdtemp(dir=settings.BASE_DIR)

@override_settings(MEDIA_ROOT=TEMP_MEDIA_ROOT)
class PostCreateFormTests(TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        super().setUpClass()
        # Создаём изображение
        cls.image = tempfile.NamedTemporaryFile(suffix=".jpg").name
        # Создаём пользователя
        cls.user = User.objects.create_user('User')
        # Создаём автора поста
        cls.author = User.objects.create_user('Author')
        # Создаём группу
        cls.group = group_create()
        # Создаём пост
        cls.post = post_create(cls.author, cls.group, cls.image)

    @classmethod
    def tearDownClass(cls):
        super().tearDownClass()
        shutil.rmtree(TEMP_MEDIA_ROOT, ignore_errors=True)

    # Создаём пользователя гостя и афторизируем пользователя.
    def setUp(self) -> None:
        # Создаем гостя
        self.guest_client = Client()
        # Автор(Зарегестрированный пользователь)
        self.author_client = Client()
        self.author_client.force_login(self.author)
        cache.clear()

    def test_form_create_new_Post(self):
        """Валидная форма создаёт запись в Post"""
        group = PostCreateFormTests.group
        user = PostCreateFormTests.author
        image = PostCreateFormTests.image

        posts_count = Post.objects.count()

        form_data = {
            'text': 'Текст поста',
            'group': group.id,
            'image': image
        }

        response = self.author_client.post(
            reverse('posts:post_create'),
            data=form_data,
            follow=True,
        )
        self.assertRedirects(
            response,
            url_rev('posts:profile', username=user)
        )
        self.assertEqual(Post.objects.count(), posts_count + 1)
        self.assertTrue(
            Post.objects.filter(
                text='Текст поста',
                group=group
            ).exists()
        )

    def test_form_create_change_Post(self):
        """"Изменение существующего поста в базе данных."""
        group = PostCreateFormTests.group
        post = PostCreateFormTests.post

        posts_count = Post.objects.count()

        form_data = {
            'text': 'Изменённый текст поста',
            'group': group.id
        }
        response = self.author_client.post(
            url_rev('posts:post_edit', post_id=post.id),
            data=form_data,
            follow=True,
        )

        self.assertRedirects(
            response,
            url_rev('posts:post_detail', post_id=post.id)
        )

        self.assertEqual(Post.objects.count(), posts_count)

        first_post = Post.objects.first()

        self.assertEqual(
            first_post.text,
            'Изменённый текст поста'
        )

    def test_author_comment(self):
        """комментировать посты может только авторизованный пользователь;"""
        post = PostCreateFormTests.post

        form_data = {
            'text': 'Коментарий авторизованного пользователя',
        }

        response = self.author_client.post(
            url_rev('posts:add_comment', post_id=post.id),
            data=form_data,
            follow=True,
        )
        self.assertRedirects(
            response,
            url_rev('posts:post_detail', post_id=post.id)
        )
        self.assertTrue(
            Comment.objects.filter(
                text='Коментарий авторизованного пользователя'
            ).exists()
        )

    def test_no_author_comment(self):
        """не авторизованный пользователь не может комментировать;"""
        post = PostCreateFormTests.post

        form_data = {
            'text': 'Коментарий не авторизованного пользователя',
        }

        self.guest_client.post(
            url_rev('posts:add_comment', post_id=post.id),
            data=form_data,
            follow=True,
        )

        self.assertFalse(
            Comment.objects.filter(
                text='Коментарий не авторизованного пользователя'
            ).exists()
        )


import os
import pathlib
import logging

from osgeo import gdal, osr
from s100py import s100
from s100py.s102 import v2_0
from s100py.s102 import v2_1
from s100py.s102 import v2_2

def remove_file(pth, quiet=True):
    """Remove a file if it exists"""
    try:
        os.remove(pth)
    except (FileNotFoundError, PermissionError):  #
        if not quiet:
            logging.warning(f"{pth} not found")

# test_rat(str(local_path.joinpath("F00788_SR_8m.tif")), output_path)
# test_rat(tiffname, output_path)
if 0:
    metadata = {"horizontalDatumReference": "EPSG", "horizontalDatumValue": 32610}
    out_path = r"C:\Data\BlueTopo\RATs\BlueTopo_BC25M26L_20221102b.2_2.h5"
    remove_file(out_path)
    new_s102_20 = v2_2.utils.from_gdal(r"C:\Data\BlueTopo\RATs\BlueTopo_BC25M26L_20221102b.tiff", out_path, metadata=metadata)
if 1:
    root_path = pathlib.Path("C:\\Data\\S102\\S102_v2.2\\")
    bags = [r"NBS_US4NY1CQ_20230221.bag",
            r"NBS_US5NYCII_20230602.bag",
            r"NBS_US5NYCJH_20230110.bag",
            r"NBS_US5NYCJI_20230221.bag",
            r"NBS_US5NYCKG_20230110.bag",
            r"NBS_US5NYCKH_20230110.bag",
            r"NBS_US5NYCLG_20230110.bag",
            r"NBS_US5NYCLH_20230221.bag",
            r"NBS_US5NYCMG_20230110.bag",
            r"NBS_US5NYCMH_20230110.bag",
            r"NBS_US5NYCNG_20230110.bag",
            r"NBS_US5NYCNH_20230221.bag",
            r"NBS_US5NYCOG_20230110.bag",
            r"NBS_US5NYCGH_20230602.bag",
            r"NBS_US5NYCHH_20230602.bag",
            r"NBS_US5NYCIH_20230602.bag",]

    from urllib.request import urlretrieve

    # download the savannah files, if needed
    # for line in savanah_files.split("\n"):
    #     try:
    #         tif_url, aux_url = line.split(", ")
    #     except ValueError:
    #         continue
    #     else:
    #         for url in (tif_url, aux_url):
    #             filename = os.path.split(url)[-1]
    #             target = root_path.joinpath(filename)
    #             tiffs.append(target)
    #             if not target.exists():
    #                 urlretrieve(url, target)
    # Used the bluetopo downloader since the dates had changed invalidating the links for the savannah files
    for tif in root_path.glob("*.tif?"):
        print('processing', tif)
        # Bluetopop TIFs were using NAD83 (epsg~26900) instead of WGS84 (epsg~32600)
        dataset = gdal.Open(str(tif))
        sr = osr.SpatialReference(dataset.GetProjection())
        epsg = sr.GetAuthorityCode(None)
        # It also was using compound CRS with custom vertical
        if epsg is None and sr.IsProjected():
            crs_2d = osr.SpatialReference(dataset.GetProjection())
            # in GDAL 3.2 this was added which may make getting the horizontal CRS more obvious
            crs_2d.DemoteTo2D()
            epsg = crs_2d.GetAuthorityCode(None)
        if 26900 <= int(epsg) < 26999:
            epsg = 32600 + int(epsg) - 26900
        metadata = {'geographicIdentifier': "Sample Data", "horizontalDatumReference": "EPSG", "horizontalDatumValue": epsg}
        fname_2_2 = tif.with_suffix(".2_2.h5")
        remove_file(fname_2_2)
        s102_22 = v2_2.utils.from_gdal(tif, fname_2_2, metadata=metadata, flip_z=True)  # bluetopo tiff is in elevation instead of depth
        fname_2_1 = tif.with_suffix(".2_1.h5")
        remove_file(fname_2_1)
        s102_21 = v2_1.utils.from_gdal(tif, fname_2_1, metadata=metadata, flip_z=True)  # bluetopo tiff is in elevation instead of depth

if 0:
    metadata = {'geographicIdentifier': "Sample Data"}
    for bag in root_path.glob("*.bag"):
        fname_2_2 = bag.with_suffix(".bag.2_2.h5")
        remove_file(fname_2_2)
        s102_22 = v2_2.utils.from_bag(bag, fname_2_2, metadata=metadata)
        fname_2_1 = bag.with_suffix(".bag.2_1.h5")
        remove_file(fname_2_1)
        s102_21 = v2_1.utils.from_bag(bag, fname_2_1, metadata=metadata)

if 0:
    # test upgrade of v2.1 to v2.2
    remove_file(r"C:\data\S102\S102_v2.2\NBS_US4NY1CQ_20230221_162221_base.tiff.2_1_to_2_2.h5")
    sf22 = v2_2.api.S102File.upgrade(r"C:\data\S102\S102_v2.2\NBS_US4NY1CQ_20230221_162221_base.tiff.2_1.h5", r"C:\data\S102\S102_v2.2\NBS_US4NY1CQ_20230221_162221_base.tiff.2_1_to_2_2.h5")


#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
Functions for Preprocessing
EXtract the features of Refined Composite Multi-Scale Entropy, Multi-Scale Entropy
'''

from argparse import ArgumentParser
import os
import warnings
import numpy as np
from biosppy.signals import ecg
from scipy.stats import skew, kurtosis

from utils import butter_highpass_filter, butter_lowpass_filter
from utils import getfreqs_power, getBand_Power, getFiveBands_Power
from utils import detrend
from utils import multiscale_entropy, permutation_entropy,RC_composite_multiscale_entropy,RC_sample_entropy
from config import SUBJECT_NUM, VIDEO_NUM, SAMPLE_RATE, MISSING_DATA_SUBJECT

warnings.filterwarnings(action="ignore", module="scipy", message="^internal gelsd")

def eeg_preprocessing(signals):
    ''' Preprocessing for EEG signals '''
    # trans_signals = np.transpose(signals)
    column_0=signals[:,0]
    max_scale = 3
    max_m = 3
    feature_rcmse = np.zeros(( max_m,max_scale))
    feature_mse = np.zeros(( max_m,max_scale))
    for i in range(max_m):
        for j in range(max_scale):
            feature_rcmse[i,j]=RC_composite_multiscale_entropy(column_0,i+1,j+1,i+1, None)
            feature_mse[i,j]=multiscale_entropy(column_0,j+1,i+1, None)[-1]

    feature_rcmse = feature_rcmse.flatten()
    feature_mse = feature_mse.flatten()

    #ibi_mse = list(multiscale_entropy(ibi,3,3,3, None))
    #ibi_pe = []
    #for m in range(3, 4):
    #    ibi_pe.append(permutation_entropy(ibi, m, 1))

    features=np.array([])
    features=np.append(features,feature_mse)
    features=np.append(features,feature_rcmse)

    return features

def ecg_preprocessing(signals):
    ''' Preprocessing for ECG signals '''
    signals = butter_highpass_filter(signals, 1.0, 256.0)
    ecg_all = ecg.ecg(signal=signals, sampling_rate=256., show=False)
    rpeaks = ecg_all['rpeaks']  # R-peak location indices.

    ibi = np.array([])
    for i in range(len(rpeaks) - 1):
        ibi = np.append(ibi, (rpeaks[i + 1] - rpeaks[i]) / 128.0)
    #multiscale_entropy(time_series, scaling_factor , m , tolerance=None)
    #composite_multiscale_entropy(time_series, m, scale, tolerance=None)
    #
    #RC_composite_multiscale_entropy(time_series, sample_length, scale,m, tolerance=None):
    #  SAMPLE LENGTH need to equal m = =''
    #m is actually m+1 of the last ratio in numerator
    #rcmse_SCALE_M ######
    tol=0.2*np.std(ibi)
    '''
    max_scale = 3
    max_m = 3
    feature_rcmse = np.zeros(( max_m,max_scale))
    feature_mse = np.zeros(( max_m,max_scale))
    for i in range(max_m):
        for j in range(max_scale):
            feature_rcmse[i,j]=RC_composite_multiscale_entropy(ibi,i+1,j+1,i+1, tol)
            feature_mse[i,j]=multiscale_entropy(ibi,j+1,i+1,tol )[-1]

    feature_rcmse = feature_rcmse.flatten()
    feature_mse = feature_mse.flatten()
    '''
    #ibi_mse = list(multiscale_entropy(ibi,3,3,3, None))
    '''
    pe = []
    for m in range(2,8):
        pe.append(permutation_entropy(ibi, m, 1))
    '''
    #mse_2_3_0.1
    a= multiscale_entropy(ibi,2,3,0.1*np.std(ibi) )[-1]
    #rcmse_3_2_3_0.1
    b=RC_composite_multiscale_entropy(ibi,3,2,3, 0.1*np.std(ibi))
    #rcmse_2_2_2_0.2
    c=RC_composite_multiscale_entropy(ibi,2,2,2, 0.2*np.std(ibi))
    #rcmse_2_3_2_0.2
    d=RC_composite_multiscale_entropy(ibi,2,3,2, 0.2*np.std(ibi))
    #rcmse_1_2_1_0.1
    e=RC_composite_multiscale_entropy(ibi,1,2,1, 0.1*np.std(ibi))
    #mse_2_1_0.1
    f=multiscale_entropy(ibi,2,1,0.1*np.std(ibi) )[-1]
    features=np.array([])
    features=np.append(features,a)
    features=np.append(features,b)
    features=np.append(features,c)
    features=np.append(features,d)
    features=np.append(features,e)
    features=np.append(features,f)
    print(features.shape)

    return features

def gsr_preprocessing(signals):
    ''' Preprocessing for GSR signals '''
    nor_signals = (signals - np.mean(signals)) / np.std(signals)
    con_signals = 1.0 / signals
    nor_con_signals = (con_signals - np.mean(con_signals)) / np.std(con_signals)
    '''
    max_scale = 7
    max_m = 7
    feature_rcmse = np.zeros(( max_m,max_scale))
    feature_mse = np.zeros(( max_m,max_scale))
    for i in range(max_m):
        for j in range(max_scale):
            feature_rcmse[i,j]=RC_composite_multiscale_entropy(nor_con_signals,i+1,j+1,i+1, None)
            feature_mse[i,j]=multiscale_entropy(nor_con_signals,j+1,i+1, None)[-1]

    feature_rcmse = feature_rcmse.flatten()
    feature_mse = feature_mse.flatten()
    '''
    pe = []
    for m in range(3,6):
        pe.append(permutation_entropy(nor_con_signals, m, 1))

    features=np.array([])
    features=np.append(features,pe)
    #features=np.append(features,feature_rcmse)

    return features

def read_dataset(path):
    ''' Read AMIGOS dataset '''
    amigos_data = np.array([])

    for sid in range(SUBJECT_NUM):
        for vid in range(VIDEO_NUM):
            if sid + 1 in MISSING_DATA_SUBJECT:
                print("Skipping {}_{}.csv".format(sid + 1, vid + 1))
                continue
            print('Reading {}_{}.csv'.format(sid + 1, vid + 1))
            signals = np.genfromtxt(os.path.join(path, "{}_{}.csv".format(sid + 1, vid + 1)),
                                    delimiter=',')
            eeg_signals = signals[:, :14]
            ecg_signals = signals[:, 14]  # Column 14 or 15
            gsr_signals = signals[20:, -1]  # ignore the first 20 data, since there is noise in it

            #eeg_features = eeg_preprocessing(eeg_signals)
            ecg_features = ecg_preprocessing(ecg_signals)
            #gsr_features = gsr_preprocessing(gsr_signals)

            features = np.array( ecg_features )

            amigos_data = np.vstack((amigos_data, features)) if amigos_data.size else features

    return amigos_data

def main():
    ''' Main function '''
    parser = ArgumentParser(
        description='Affective Computing with AMIGOS Dataset -- Feature Extraction')
    parser.add_argument('--data', type=str, default='./data')
    args = parser.parse_args()

    amigos_data = read_dataset(args.data)
    np.savetxt(os.path.join(args.data, 'bestmse_features.csv'), amigos_data, delimiter=',')

if __name__ == '__main__':

    main()


import requests
import chardet

class HtmlDownloader:
    def download(self, url):
        if url is None:
            return None

        user_agent = "Mozilla/4.0 (compatible;MSIE 5.5; Windows NT)"
        # 伪装成浏览器
        headers = {"User-Agent" : user_agent}
        # 获取页面
        r = requests.get(url, headers = headers)
        # 如果返回的响应码为200,即可成功连接服务器
        if r.status_code == 200:
            # 自动辨别网页的编码格式
            r.encoding = chardet.detect(r.content)["encoding"]
            return r.text

        return None

if __name__ == "__main__":
    url = "http://www.woidm.com/"

    htmld = HtmlDownloader()
    htmlText = htmld.download(url)

    print(type(htmlText))


# Do pandas inner join
import pandas as pd
import numpy as np

from engine.config import config as cf

from engine.pipes.diag.c_string_matching import get_source
from engine.pipes.diag.c_string_matching import get_fields

def group_by_func(table_source,fixed_fields_source,comparable_fields_source):

    nb_unique_values_source = pd.DataFrame()

    for column in comparable_fields_source:
        table_count = pd.DataFrame(table_source.groupby(fixed_fields_source.tolist())[column].nunique())

        nb_unique_values_source = pd.concat([nb_unique_values_source, table_count], axis=1)

    return (nb_unique_values_source)

def create_report_source(table_source,table_ID,fixed_fields_source,comparable_fields_source,nb_unique_values_source):

    nb_unique_values_source['SOURCE'] = table_ID.iloc[0]
    nb_unique_values_source['FIXED_FIELDS'] = str(fixed_fields_source.tolist())
    nb_unique_values_source['COMPARISON_FIELDS'] = str(comparable_fields_source.tolist())
    nb_unique_values_source['FIXED_VALUES'] = nb_unique_values_source.index.tolist()

    df_comparison = pd.DataFrame()

    df_comparison['FIXED_VALUES']= table_source[fixed_fields_source.tolist()]
    df_comparison['COMPARISON_VALUES'] = table_source[comparable_fields_source].apply(lambda x: '-'.join(map(str, x)), axis=1).values.tolist()

    df_result = pd.merge(nb_unique_values_source,df_comparison,on="FIXED_VALUES")
    return(df_result)

def row_matching(self,list_inputs_dict, df_files_mapping_dict, df_fields_mapping, df_row_matching):

    df_result = pd.DataFrame()
    my_ids_set = set(df_row_matching["ROW_MATCHING_ID"].tolist())

    for matching_id in my_ids_set:

        df_tmp = df_row_matching[df_row_matching.ROW_MATCHING_ID == matching_id]

        table_1_ID = df_tmp["SOURCE1_ID"]
        table_2_ID = df_tmp["SOURCE2_ID"]

        if (len(set(table_1_ID)) == len(set(table_2_ID)) == 1):

            table_source1, name_source1 = get_source(table_1_ID.iloc[0], df_files_mapping_dict, list_inputs_dict)
            table_source2, name_source2 = get_source(table_2_ID.iloc[0], df_files_mapping_dict, list_inputs_dict)

            df_fields = df_tmp[["TYPE", "FIELD_MAPPING_ID"]]

            list_fixed = df_fields[df_fields.TYPE == 'FIXED']["FIELD_MAPPING_ID"].tolist()
            list_to_compare = df_fields[df_fields.TYPE == 'TO COMPARE']["FIELD_MAPPING_ID"].tolist()
            fields_mapping = {'FIXED': list_fixed, 'TO COMPARE': list_to_compare}

            fixed_fields_source1, fixed_fields_source2 = get_fields(fields_mapping['FIXED'], df_fields_mapping,
                                                                    table_1_ID.iloc[0],
                                                                    table_2_ID.iloc[0])

            comparable_fields_source1, comparable_fields_source2 = get_fields(fields_mapping['TO COMPARE'],
                                                                              df_fields_mapping, table_1_ID.iloc[0],
                                                                              table_2_ID.iloc[0])

            nb_unique_values_source1 = group_by_func(table_source1,fixed_fields_source1,comparable_fields_source1)
            nb_unique_values_source2 = group_by_func(table_source2,fixed_fields_source2, comparable_fields_source2)

            if (len(nb_unique_values_source1) == len(nb_unique_values_source2) == 0):

                table_source1_tmp = table_source1
                table_source1_tmp['SOURCE'] = "SRC1"

                table_source2_tmp = table_source2
                table_source1_tmp['SOURCE'] = "SRC1"

                df_compare = pd.merge(table_source1[fixed_fields_source1.tolist() + comparable_fields_source1.tolist()],
                                      table_source2[fixed_fields_source2.tolist() + comparable_fields_source2.tolist()],
                                      how='inner', left_on = fixed_fields_source1.tolist(), right_on= fixed_fields_source2.tolist())

                df_result = df_compare[df_compare[comparable_fields_source1].values != df_compare[comparable_fields_source2].values]

                #result_merged_sources = create_report_source(df_result, fixed_fields_source2, comparable_fields_source2, nb_unique_values_source2)

            else :
                nb_unique_values_source1.columns = [list_to_compare]
                nb_unique_values_source2.columns = [list_to_compare]

                result_source1_tmp = create_report_source(table_source1,table_1_ID, fixed_fields_source1, comparable_fields_source1, nb_unique_values_source1)
                result_source2_tmp = create_report_source(table_source2,table_2_ID, fixed_fields_source2, comparable_fields_source2, nb_unique_values_source2)

                result_tmp = pd.concat([result_source1_tmp, result_source2_tmp])

                result_tmp['TMP']=result_tmp[list_to_compare].sum(axis=1)

                df_result = result_tmp[result_tmp['TMP'] >= 2]

    return ( df_result.drop('TMP', axis=1) )


def get_ctx_result(result):

    ctx_result = {}
    param = result.get_param()

    ctx_result['task_id'] = param.get('id')
    ctx_result['vault_id'] = param.get('vault_id')
    ctx_result['vault_file_name'] = param.get('file_name')

    data = result.get_data()

    if (not data):
        return ctx_result

    data = data[0]

    if (not data):
        return ctx_result

    ctx_result['analysis_details'] = data.get('analysis_details', {}).get('analysis_details')
    ctx_result['analysis_array'] = data.get('analysis_details', {}).get('analysis_array')
    ctx_result['behavior_details'] = data.get('behavior_details')

    message = result.get_message()

    if (message) and ('max polling attempts' in message):
        ctx_result['message'] = message

    return ctx_result

def display_report(provides, all_app_runs, context):

    context['results'] = results = []
    for summary, action_results in all_app_runs:
        for result in action_results:

            ctx_result = get_ctx_result(result)
            if (not ctx_result):
                continue
            results.append(ctx_result)
    # print context
    return 'cyphort_display_report.html'


def solution(n, t, m, p):
    answer = ''
    num = ['0', '1', '2', '3', '4', '5', '6', '7',
           '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
    narr, size, cur, ck = [], p+m*(t-1), [0], False
    while len(narr) < size:
        if cur[-1] >= n:
            for i in range(1, len(cur)+1):
                cur[-i] += 1
                if cur[-i] < n:
                    ck = True
                    break
                cur[-i] = 0
            if ck == False:
                cur = [1] + cur
        for v in cur:
            narr.append(num[v])
        cur[-1] += 1
        ck = False
    for i in range(t):
        answer += narr[p+m*i-1]
    return answer


# -*- coding: utf-8 -*-
#  This file is part of AC3ES Tools.
#
#  AC3ES Tools is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  AC3ES Tools is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with AC3ES Tools.  If not, see <http://www.gnu.org/licenses/>.

import collections
import os
from ac3es.exceptions import NotTimException

class TimReader:
    signature = b'\x10\x00\x00\x00'

    BPP_TYPES = {
        b'\x08\x00\x00\x00': 4,
        b'\x09\x00\x00\x00': 8,
        b'\x02\x00\x00\x00': 16,
        b'\x03\x00\x00\x00': 24,
    }

    def __init__(self, tim_stream, original_path=''):
        self.vram_x = None
        self.vram_y = None
        self.img_height = None
        self.img_width = None
        self.n_clut = None
        self.n_colors = None
        self.clut_data = None
        self.palette_x = None
        self.palette_y = None
        self.stream = tim_stream
        self.offsets = {}
        self.clut_size = None
        self.clut_colors = None

        self.original_path = original_path

        self.read_tim()

    def info(self, file_location=''):
        self.read_tim()

        bpb_location = ''
        if file_location:
            file_location = os.path.abspath(file_location)
            if file_location.find('bpb') > -1:
                ext_pos = file_location.rfind('.')
                if ext_pos == -1:
                    ext_pos = len(file_location)
                bpb_location = file_location[file_location.find('bpb') + 3:ext_pos]

        return collections.OrderedDict({
            'file_location': file_location.lower(),
            'bpb_location': bpb_location,
            'filetype': 'tim',
            'size': os.path.getsize(file_location),
            'bpp': self.bpp,
            # 'md5': utils.md5_for_file(path),
            'vram_x': self.vram_x,
            'vram_y': self.vram_y,
            'img_height': self.img_height,
            'img_width': self.img_width,
            'n_clut': self.n_clut,
            'n_colors': self.n_colors,
            'palette_x': self.palette_x,
            'palette_y': self.palette_y,
        })

    def read_tim(self):
        self.stream.seek(0)
        header = self.stream.read(4)
        if header != self.signature:
            raise NotTimException(
                '{} Signature does not match {}'.format(
                    self.original_path,
                    str(header)
                )
            )

        self.bpp = self.BPP_TYPES.get(
            self.stream.read(4),
            None
        )

        if self.bpp is None:
            raise NotTimException('{} Invalid BPP {}'.format(self.original_path, str(self.bpp)))

        self.clut_size = int.from_bytes(self.stream.read(4), byteorder='little') - 4

        if self.bpp == 4 or self.bpp == 8:
            self.offsets['clut_header_start'] = self.stream.tell()
            self.offsets['palette_x'] = self.stream.tell()
            self.palette_x = int.from_bytes(self.stream.read(2), byteorder='little')
            self.offsets['palette_y'] = self.stream.tell()
            self.palette_y = int.from_bytes(self.stream.read(2), byteorder='little')

            self.offsets['clut_colors'] = self.stream.tell()
            self.clut_colors = int.from_bytes(self.stream.read(2), byteorder='little')

            self.offsets['n_clut'] = self.stream.tell()
            self.n_clut = int.from_bytes(self.stream.read(2), byteorder='little')

            self.offsets['clut_data'] = self.stream.tell()

            if self.bpp == 4:
                self.n_colors = self.n_clut * 16
                self.clut_data = self.stream.read(self.clut_size)
            else:
                self.n_colors = self.n_clut * 256
                self.clut_data = self.stream.read(self.clut_size)

            self.offsets['clut_header_end'] = self.stream.tell()

            self.offsets['vram_x'] = self.stream.tell()
            self.vram_x = int.from_bytes(self.stream.read(2), byteorder='little')
            self.offsets['vram_y'] = self.stream.tell()
            self.vram_y = int.from_bytes(self.stream.read(2), byteorder='little')

            self.offsets['img_width'] = self.stream.tell()
            self.img_width = int.from_bytes(self.stream.read(2), byteorder='little')
            self.offsets['img_height'] = self.stream.tell()
            self.img_height = int.from_bytes(self.stream.read(2), byteorder='little')

            if self.bpp == 4:
                self.img_width *= 4
            else:
                self.img_width *= 2

        elif self.bpp == 16 or self.bpp == 24:

            self.offsets['vram_x'] = self.stream.tell()
            self.vram_x = int.from_bytes(self.stream.read(2), byteorder='little')
            self.offsets['vram_y'] = self.stream.tell()
            self.vram_y = int.from_bytes(self.stream.read(2), byteorder='little')

            self.offsets['img_width'] = self.stream.tell()
            self.img_width = int.from_bytes(self.stream.read(2), byteorder='little')
            self.offsets['img_height'] = self.stream.tell()
            self.img_height = int.from_bytes(self.stream.read(2), byteorder='little')

            if self.bpp == 24:
                self.img_width *= 1.5

        self.offsets['header_end'] = self.stream.tell()

    @staticmethod
    def get_tim_info(tim_path):
        stats = {}
        with open(tim_path, 'rb') as tim:
            tim_file = TimReader(tim)
            stats = tim_file.info(tim_path)
        return stats


# Author: Austin Shih
# Date: 20 Feb 2022

""" Downloads csv file from the web and saves contents to a local directory.
Usage: download.py --out_file=<out_file>
Options:
--out_file=<out_file>     Path to write the contents to
"""

# python src/download.py --out_file=data/raw/nbi_raw.csv

import requests
from docopt import docopt
import os
import pandas as pd

opt = docopt(__doc__)

def main(out_file):

    # file updated January 13, 2023
    url = 'https://opendata.arcgis.com/api/v3/datasets/2467101f84e447aebf164a6680d2f59d_0/downloads/data?format=csv&spatialRefId=3857&where=1%3D1'
    #check if URL is valid
    try:
        print('checking URL...')
        request = requests.get(url, stream=True)
        request.status_code == 200
        print("URL valid")
    except Exception as ex:
        print("the URL provided is invalid")
        print(ex)

    print('downloading csv file...')
    data = pd.read_csv(url, header=0)
    try:
      data.to_csv(out_file, index = False)
    except:
      os.makedirs(os.path.dirname(out_file))
      data.to_csv(out_file, index = False)

if __name__ == "__main__":
    main(opt['--out_file'])


from server import server
from sqlalchemy.orm import sessionmaker
from model import user

engine = user.dbCreate()
session = sessionmaker(bind=engine)()

while True:
    my_dict = server.run_server()
    name = my_dict.get("name")
    lastname = my_dict.get("lastname")
    user_obj = user.User(name,lastname)

    session.add(user_obj)
    session.commit()


#steganography -
# Lets hide our  data by embedding it into an image.
# ensure to install  opencv-python

import cv2
import os

HEADER_FILENAME_LENGTH= 30
HEADER_FILESIZE_LENGTH= 20
HEADER_LENGTH = HEADER_FILENAME_LENGTH +  HEADER_FILESIZE_LENGTH

getbits = lambda n : [n>>5,(n&0x1C)>>2,n&0x3]

getbyte =lambda bits : (((bits[0]<<3)|bits[1])<<2)|bits[2]

def get_file_size(filename):
	try:
		return os.stat(filename).st_size
	except:
		return 0

	#fileName: 'd:/images/work.jpg
def generate_header(filename):

	file_size=get_file_size(filename)
	if file_size==0:
		return None
	name=filename.split('/')[-1]
	#splitted: [d:, images, work.jpg]
	name_extension=name.split('.')
	extension_name=name_extension[1]
	extension_size=len(extension_name)+1

	name_len=HEADER_FILENAME_LENGTH - extension_size

	name=name_extension[0][:name_len]+ '.' + extension_name

	name=name.ljust(HEADER_FILENAME_LENGTH,'*')
	qty=str(file_size).ljust(HEADER_FILESIZE_LENGTH,'*')

	return name+qty

def embed(result_img,src_img,file_to_embed):
	image=cv2.imread(src_img,cv2.IMREAD_COLOR)
	if image is None:
		print(src_img,"not found")
		return False

	file_size=get_file_size(file_to_embed)
	if file_size==0:
		print(file_to_embed,"not found")
		return False

	#capacity check

	h,w,band=image.shape  #band (BGR)
	capacity=h*w
	if capacity<file_size+HEADER_LENGTH:
		print('embedding is not possible Insufficient Image Capacity')
		return False

        #get header
	header=generate_header(file_to_embed)
	# print("header= ",header)
	cnt=0
	i=0
	data=0
	keep_going=True
	fh=open(file_to_embed,'rb')
	b=0
	while i<h and keep_going:
		j=0
		while j<w:
			if cnt<HEADER_LENGTH:
				data=ord(header[cnt])
				# print(header[cnt])

				#cnt+=1
			else:
				data=fh.read(1)
				    # as the file is opened in binary mode
					# so we get byte objects on read
					# the byte object dont support bitwise operations
					# hence they are to be converted to int
				if data:
					data=int.from_bytes(data,byteorder='big')
				else:
					keep_going=False
					break

			bits=getbits(data)

			image[i,j,2]=(image[i,j,2]& (~0x7))|bits[0]
			image[i,j,1]=(image[i,j,1]& (~0x7))|bits[1]
			image[i,j,0]=(image[i,j,0]& (~0x3))|bits[2]

			# if b<10:
			# 	print(image[i,j,2]," ",image[i,j,1]," ",image[i,j,0])
			# 	b+=1

			cnt+=1
			j+=1

		i+=1

	fh.close()

	#save back the image
	cv2.imwrite(result_img, image)
	print('Embedding Done')

def extract(src_img,result_folder):
	#load the image as a numpy.ndarray
	image=cv2.imread(src_img,cv2.IMREAD_COLOR)
	if image is None:
		print(src_img,"not found")
		return False
	header=''
	fh=None
	i=0
	cnt=0
	keep_going=True
	h,w,band=image.shape
	b=0
	while i<h and keep_going:
		j=0
		while j<w:
			bit1=image[i,j,2]& 0x7
			bit2=image[i,j,1]& 0x7
			bit3=image[i,j,0]& 0x3

			data=getbyte([bit1,bit2,bit3])

			# if b<50:
			# 	print(image[i,j,2]," ",image[i,j,1]," ",image[i,j,0])
			# 	b+=1

			if cnt<HEADER_LENGTH:
				header=header+chr(data)
				# print(chr(data))
			else :

				if cnt==HEADER_LENGTH:
					# print(header)
					filename=result_folder+ '/' + header[:HEADER_FILENAME_LENGTH].strip('*')
					# print(filename)
					filesize=int(header[HEADER_FILENAME_LENGTH:].strip('*')) + cnt
					# print(filesize,cnt)
					fh=open(filename,'wb')

				if cnt<filesize:
					data=int.to_bytes(int(data),1,byteorder='big')
					fh.write(data)

				else:
					fh.close()
					keep_going=False
					break

			cnt+=1
			j+=1
		i+=1

	print("extraction done")

def main():

	while True:

		print('1. Embed ')
		print('2. Extract')
		print('3. Exit')
		ch = input()
		if ch=='1':
		    result_img=input('enter the path with the resultant image_name eg:-"/home/nikhil/Downloads/result.png" or "d:/images/result.png" ')
		    file_to_embed=input('enter the path for file to embed')
		    src_img=input('enter the path of image in which the file needs to be embedded')

		    result=embed(result_img,src_img,file_to_embed)
		    if result == False:
		        print('EMBEDDED FAILED ')

		elif ch=='2':
		    signedImg = input('Enter  Stego Image ')
		    result_folder=input('Enter the folder name where you want the extracted file eg:- "/home/nikhil/Pictures" or "f:/images"')
		    x=extract(signedImg,result_folder)
		    if x==False:
		        print("extraction failed")

		elif ch == '3':
		    break

		else:
		    print('Wrong Choice')

main()


#!/usr/bin/env python3

import requests
import os
url = 'http://localhost/upload/'
file_dir = 'supplier-data/images/'
files = os.listdir(file_dir)
for file in files:
    if not file.endswith('.jpeg'):
        continue
    with open(file_dir+file, 'rb') as opened:
        r = requests.post(url, files={'file':opened})