from __future__ import division, print_function

from collections import namedtuple
from glob import iglob
from os.path import (abspath, basename,
    join, split, splitext)
from re import (compile as re_compile,
    search as re_search, I as re_I)

from BioExt.references._lazyseq import _lazyseq

__all__ = []

_installrefdirs = []

_refdir = join(
    split(
        split(
            abspath(__file__)
        )[0] # _references/
    )[0], # _references/../
    'data',
    'references'
)

def _reffactory(seqdir, seqfmt, name=None, genes=None):
    if name is None:
        name = re_compile(r'[^0-9A-Z]', re_I).sub('_', basename(seqdir))
    datadict = {}
    if genes is not None:
        for gene in genes:
            datadict[gene] = _lazyseq(seqdir, seqfmt % gene)
    else:
        genes = []
        for seqpath in iglob(join(seqdir, seqfmt % '*')):
            m = re_search(seqfmt % '(.+)', seqpath)
            if m:
                gene = m.group(1)
                genes.append(gene)
                datadict[gene] = _lazyseq(seqdir, basename(seqpath))
    # if the seqdir has the refdir in it, then add it to the list
    # of default-installed reference sequence directories
    if _refdir in seqdir:
        globber = '*' + splitext(seqfmt)[1]
        _installrefdirs.append(
            join(
                'data',
                'references',
                basename(seqdir),
                globber
            )
        )
    return namedtuple(name, genes)(**datadict)


from wic import RESTRICT
from wic import etl as ETL
from wic.etl import column as Column
from wic.etl import common as Common
from wic.etl import db as ETLDB
from wic.etl.key import system_key
from wic.util import folder as Folder
from wic.util.logging import logger
import datetime, csv, re

_cat = RESTRICT.CM
_cat1 = RESTRICT.DC
_re_csv1 = re.compile('^(.+)_(LRC\d+)_(\d\d\d\d-\d\d-\d\d)[.]txt$')
_re_csv2 = re.compile('^(.+)_(NHRC\d+)_(\d\d\d\d-\d\d-\d\d)[.]txt$')

def _csv_filename_to_tblname(csv_filename):
    m = _re_csv1.match(csv_filename)
    if m is None:
        m = _re_csv2.match(csv_filename)
    if m is not None:
        return m.group(1), m.group(1).lower().endswith('_list')

"""
# Commnad: etl check cm date date_to
# date:    <yyyymmdd>
# date_to: <yyyymmdd>
"""
def check(cusid, tech, date, date_to):
    if type(date) is datetime.date and type(date_to) is datetime.date:
        logger(__name__).info('checking CM delta {} from {}'.format(date, date_to))

        conf = ETL.get_computed_config(cusid, tech, __name__)
        dpbase = conf[RESTRICT.DATA_PATH].joinpath('{}/{}/{:%Y%m%d}'.format(cusid, tech, date))
        tfdrpath = dpbase.joinpath('tmp')

        cc = Column.get_check_columns(None, cusid, tech, RESTRICT.CORE, __name__)
        df = '{:%Y-%m-%d}'
        preficies = system_key[RESTRICT.CATEGORY]
        excluded_columns = set(['OBJ_GID', 'NBR', 'LAST_MODIFIED', 'LAST_MODIFIER'])
        """
        #c = 0
        #ig = 0
        """
        ## using key (line['CO_GID'], line['CO_PARENT_GID'], line['CO_DN'])
        for LRC, z1, f1, z2, f2 in Common.extract_info_pair(date, date_to, cusid, tech, _cat, __name__):

            tblname, is_list_tblname = _csv_filename_to_tblname(f1)
            if tblname not in cc:
                continue
            outpath = Common.get_output_path('C_LTE_CMDLTE_DELTA_CHECK_{date:%Y%m}'.format(date= date), f1, cusid, tech, date, _cat1, __name__)
            """
            #logger(__name__).debug(tblname)
            #logger(__name__).debug(outpath)
            #logger(__name__).debug((LRC, z1, f1, z2, f2))
            #continue
            """
            Common.extract_file(z1, f1, tfdrpath, __name__)
            Common.extract_file(z2, f2, tfdrpath, __name__)

            curdict = dict()
            tfpath = tfdrpath.joinpath(f2)
            with open(str(tfpath), 'r') as fo:
                reader = csv.DictReader(fo, delimiter= RESTRICT.DELIMITER)
                for _, line in enumerate(reader):
                    if is_list_tblname:
                        curdict[line['OBJ_GID'], line['NBR']] = line
                    else:
                        curdict[line['OBJ_GID']] = line
                fo.close()
            tfpath.unlink()

            tfpath = tfdrpath.joinpath(f1)
            lines = list()
            with open(str(tfpath), 'r') as fo:
                reader = csv.DictReader(fo, delimiter= RESTRICT.DELIMITER)
                if is_list_tblname:
                    e = enumerate([ line for _, line in enumerate(reader)
                                    if (line['OBJ_GID'], line['NBR']) in curdict ])
                else:
                    e = enumerate([ line for _, line in enumerate(reader)
                                    if line['OBJ_GID'] in curdict ])
                """
                #c1 = 0
                #ig1 = 0
                """
                for _, line in e:
                    t = line
                    if is_list_tblname:
                        y = curdict[line['OBJ_GID'], line['NBR']]
                    else:
                        y = curdict[line['OBJ_GID']]
                    if t != y:
                        """
                        #c1 = c1 + 1
                        """
                        for _, col in enumerate([ x for x in line.keys() if x not in excluded_columns ]):
                            if col in t and col in y and t[col] != y[col]:
                                strtdate = df.format(date)
                                strydate = df.format(date_to)
                                new_line = RESTRICT.DELIMITER.join([
                                    strtdate,
                                    strydate,
                                    strtdate,
                                    str(preficies[LRC, 'CTP']) + line['OBJ_GID'],
                                    line['NBR'] if 'NBR' in line else '0',
                                    col,
                                    y[col],
                                    t[col]
                                ])
                                #logger(__name__).debug(new_line)
                                lines.append(new_line)
                    """
                    #else:
                    #    ig1 = ig1 + 1
                    """
                fo.close()
            tfpath.unlink()
            """
            #logger(__name__).debug(c1)
            #logger(__name__).debug(ig1)
            #c = c + c1
            #ig = ig + ig1
            """
            if lines != list():
                Folder.create(outpath.parent, __name__)
                with open(str(outpath), 'w') as fo:
                    fo.write('TIME;DateY;DateT;_id;NBR;Field;ParamY;ParamT\n')
                    i = -1
                    for i, ln in enumerate(lines):
                        fo.write(ln)
                        fo.write('\n')
                    fo.close()
                    logger(__name__).info('written: "{}"'.format(outpath))
                    logger(__name__).info('{} line{}'.format(i + 1, '' if i < 1 else 's'))
            del curdict

        """
        #logger(__name__).debug(c)
        #logger(__name__).debug(ig)
        """

def _SQL_gen_proc(sqlformat, lines, owner, tblname):
    """
    ## sqlformat: 'insert into some_table ({columns}) values {values};'
    ## lines: a list of { column: value }
    """
    cs = "TIME,DateY,DateT,_id,NBR,Field,ParamY,ParamT"
    vss = set()
    for i, ln in enumerate(lines):
        vs = "('{TIME}','{DateY}','{DateT}',{_id},{NBR},'{Field}','{ParamY}','{ParamT}')".format(
            TIME= ln['TIME'],
            DateY= ln['DateY'],
            DateT= ln['DateT'],
            _id= ln['_id'],
            NBR= ln['NBR'],
            Field= ln['Field'],
            ParamY= ln['ParamY'],
            ParamT= ln['ParamT']
        )
        vss.add(vs)
    vss = ','.join(vss)
    #logger(__name__).debug('{} record{} prepared'.format(i + 1, '' if i < 1 else 's'))
    return sqlformat.format(columns= cs, values= vss)

def load(cusid, tech, date):
    Common.load(_DDL_proc, _SQL_gen_proc, date, cusid, tech, _cat1, __name__)

def _DDL_proc(owner, tblname):
    return 'CREATE TABLE if not exists `{tblname}` ('.format(tblname= tblname) +\
        """
  `TIME` datetime DEFAULT NULL,
  `DateY` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `DateT` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `_id` bigint NOT NULL,
  `NBR` int NOT NULL,
  `Field` varchar(50) NOT NULL DEFAULT '',
  `ParamY` varchar(50) DEFAULT NULL,
  `ParamT` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`DateY`,`DateT`,`_id`,`NBR`,`Field`),
  UNIQUE KEY `unique_key` (`DateY`,`DateT`,`_id`,`NBR`,`Field`),
  KEY `TIME` (`TIME`),
  KEY `_id` (`_id`),
  KEY `Field` (`Field`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
        """


from flask import Flask, send_file, render_template, request, redirect
from utils.file_manager import FileManager
from utils.converter import Converter
from utils.downloader import Downloader
import io
import os

app = Flask(__name__)

@app.route('/', methods=['GET'])
def index():
    video_id = request.args.get('v')
    if video_id:
        link = f'https://www.youtube.com/watch?v={video_id}'
        return render_template('index.html', link=link)
    return render_template('index.html')

@app.route('/watch', methods=['GET'])
def watch():
    video_id = request.args.get('v')
    if video_id:
        return redirect(f'/?v={video_id}')
    return redirect('/')

# Route to download as mp3
@app.route('/download', methods=['GET'])
def download():
    video_id = request.args.get('v')
    if not video_id:
        return "Error: No URL provided"

    link = f'https://www.youtube.com/watch?v={video_id}'

    # Download video
    file_path = Downloader.download_by_link(link)
    if not file_path:
        return "Error: Video download failed"

    # Convert to mp3
    converted_file_path = Converter.convert_to_mp3(file_path)
    if not converted_file_path:
        FileManager.delete_file(file_path)
        return "Error: MP3 conversion failed"

    # Save converted file in memory to delete it later
    download_name = os.path.basename(converted_file_path)
    file = io.BytesIO()
    with open(file_path, 'rb') as fo:
        file.write(fo.read())
    file.seek(0)

    # Delete downloaded and converted files
    FileManager.delete_file(file_path)
    FileManager.delete_file(converted_file_path)

    # Send file for download
    return send_file(file, as_attachment=True, mimetype='audio/mp3', download_name=download_name)

if __name__ == '__main__':
    app.run()


import json

from social.tests.backends.oauth import OAuth2Test

class RedditOAuth2Test(OAuth2Test):
    backend_path = 'social.backends.reddit.RedditOAuth2'
    user_data_url = 'https://oauth.reddit.com/api/v1/me.json'
    expected_username = 'foobar'
    access_token_body = json.dumps({
        'name': 'foobar',
        'created': 1203420772.0,
        'access_token': 'foobar-token',
        'created_utc': 1203420772.0,
        'expires_in': 3600.0,
        'link_karma': 34,
        'token_type': 'bearer',
        'comment_karma': 167,
        'over_18': True,
        'is_gold': False,
        'is_mod': True,
        'scope': 'identity',
        'has_verified_email': False,
        'id': '33bma',
        'refresh_token': 'foobar-refresh-token'
    })
    user_data_body = json.dumps({
        'name': 'foobar',
        'created': 1203420772.0,
        'created_utc': 1203420772.0,
        'link_karma': 34,
        'comment_karma': 167,
        'over_18': True,
        'is_gold': False,
        'is_mod': True,
        'has_verified_email': False,
        'id': '33bma'
    })
    refresh_token_body = json.dumps({
        'access_token': 'foobar-new-token',
        'token_type': 'bearer',
        'expires_in': 3600.0,
        'refresh_token': 'foobar-new-refresh-token',
        'scope': 'identity'
    })

    def test_login(self):
        self.do_login()

    def test_partial_pipeline(self):
        self.do_partial_pipeline()

    def refresh_token_arguments(self):
        uri = self.strategy.build_absolute_uri('/complete/reddit/')
        return {'redirect_uri': uri}

    def test_refresh_token(self):
        user, social = self.do_refresh_token()
        self.assertEqual(social.extra_data['access_token'], 'foobar-new-token')


# IMPORTACION DE NUESTRAS LIBrerias
import face_recognition as fr
import numpy as np
import cv2

camara = cv2.VideoCapture(0)

#cargara la imagen
nnielpro_imagen = fr.load_image_file("niel.jpg")
niel_codigo_rostro = fr.face_encodings(nnielpro_imagen)[0]

#decodificaccion del rostro
codcarared = [
    niel_codigo_rostro
]

#colocacion del nombre
nombre = [
    "nnielpro"
]

while True:
    #toma de datos
    ret, frame = camara.read()
    #convercion de colores BGR A RGB
    rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

    #ECONTRAR DETALLES DEL ROSTRO
    face_locations = fr.face_locations(rgb_frame)
    face_encodings = fr.face_encodings(rgb_frame, face_locations)

    for (arriba, derecha, abajo, izquierda), face_encodings in zip(face_locations, face_encodings):
        noresult = fr.compare_faces(codcarared, face_encodings)
        #si no encuntra el rostro
        dat = "ROSTRO NO DETECTADO"

        #cuando existe un rostro
        distancia = fr.face_distance(codcarared, face_encodings)
        coincidencia = np.argmin(distancia)

        if noresult[coincidencia]:
            nombre2 = nombre[coincidencia]

        #dibujo del cuadro
        cv2.rectangle(frame, (izquierda, arriba), (derecha, abajo), (0,255,0), 2)
        #creacion de la etiqueta
        cv2.rectangle(frame, (izquierda, abajo -35), (derecha, abajo), (0, 255,0), cv2.FILLED)
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(frame, nombre2, (izquierda +6, abajo -6), font, 1.0, (255,255,255), 1)

    #nombre a la cqamra wed
    cv2.imshow('DETECTOR DE PERSONAS POR ROSTRO', frame)

    #apagar la camara
    if cv2.waitKey(1) & 0xFF == ord('a'):
        break

#liberacion de la camara
camara.release()
cv2.destroyAllWindows()


from selenium import webdriver
from selenium.webdriver.chrome.service import Service

chrome_driver_path = "/Users/Flip/Development/chromedriver"

ser = Service(chrome_driver_path)
driver = webdriver.Chrome(service=ser)

driver.get(
    "https://www.amazon.com/all-new-fire-tv-stick-4k-with-alexa-voice-remote/dp/B08XVYZ1Y5/ref=sr_1_1?keywords=fire+stick&qid=1663695856&sr=8-1"
)

price = driver.find_element(by="class name", value="a-offscreen+span")
print(price.text)

driver.close()  ## Closes active tab
# driver.quit()  ## Closes whole browser


import json

class Analysis_Core:
    def __init__(self):
        self.Parsed = {}
        self.rootpoint = None
        self.depth = 0
        self.iterlist = []
        self.value = None

#setiter function must be be called to setup iter function so that depth rootpoint is set
    def setiter(self, rootpoint, depth):
        self.rootpoint = rootpoint
        self.depth = depth - 1
        self.iterlist = list(range(depth))
        self.value = None

#recursive function to go through the nested dict structure and yield when reaching the depth
    def print_nested_dict(self, d, levl):
        if levl == self.depth:
            for key, value in d.items():
                self.iterlist[levl] = key
                yield self.iterlist, value
        else:
            for key, value in d.items():
                self.iterlist[levl] = key
                if isinstance(value, dict) and self.depth > levl:
                    for retvalues in self.print_nested_dict(value, levl + 1):
                        yield retvalues

#iter function , needs setting of rootpoint and depth so the class can be iterated over
    def __iter__(self):
        for keylist, value in self.print_nested_dict(self.get(self.rootpoint), 0):
            yield keylist, value
        return

    def nesting(self, tdt, clist):
        # print(f"{tdt} {type(clist)}\n")
        if type(clist[0]) is dict:
            tdt = clist
            return
        if clist[0] in tdt.keys():
            pass
        else:
            if type(clist[1]) is dict:
                tdt[clist[0]] = clist[1]
                return
            else:
                tdt[clist[0]] = {clist[1]: {}}
        if len(clist) > 2:
            self.nesting(tdt[clist[0]], clist[1:])
        else:
            tdt[clist[0]] = clist[1]
        return

    def oneup(self, tdict, uplevel):
        ttdict = {}
        ttdict = {uplevel: tdict}
        return ttdict

    def buildstruct(self, clist):
        tdict = clist[-1]
        for i in range(len(clist) - 2, 0, -1):
            tdict = self.oneup(tdict, clist[i])
        return tdict

        '''Add function , interface to be called to safely add a leaf to the parsed structure'''

    def add(self, clist):
        # print(f"test....{self.Parsed}")
        t = self.get(clist[:-1])
        # print(f"command {t} {clist[:-1]}")
        if t is not None:
            return
        tlist = []
        tdict = {}
        for i, commands in enumerate(clist):
            tlist.append(commands)
            if self.get(tlist) is None:
                # print(f"{tlist} {clist[:-1]}  is empty could append dict here")
                tdict = self.buildstruct(clist[i:])
                ttdict = self.get(tlist[:-1])
                ttdict.update({commands: tdict})
                # print(f"************** {ttdict} {commands} return")
                return
            else:
                # print(f"{tlist}  {clist[:-1]} is not empty ")
                tdict = self.get(tlist[:-1])
                if commands in tdict.keys():
                    # print(f"{commands} found in keys")
                    pass
                else:
                    # print(f"{commands} not found in keys {clist[:-1]}, it new!")
                    tdict = self.buildstruct(clist[i:])
                    ttdict = self.get(tlist[:-1])
                    ttdict[commands] = tdict

    def modify(self, clist, label, value):
        oldval = self.get(clist)
        oldval[label] = value
        passalong = [*clist, oldval]
        self.add(passalong)
        return

    # Function to print out data structure as formatted json
    def printit(self):
        print("\n" + "*" * 80)
        print("          Raw Data:")
        print(json.dumps(self.Parsed, indent=4))
        return

    def get(self, clist):
        tdt = self.Parsed
        for clis in clist:
            if clis in tdt.keys():
                tdt = tdt[clis]
            else:
                return None
        return tdt


# coding: utf-8
import logging

from rrd_xml_parser.exceptions import NotImplementedTypeError, NotImplementedValueError
from rrd_xml_parser.parser import ParserXML
from rrd_xml_parser.parsers.names import ATTRIBUTES

logger = logging.getLogger(__name__)

def createParser():
    """
    Объявление параметров командной строки

    :return: объект с определенными параметрами
    :rtype: argparse.ArgumentParser
    """
    import argparse
    parser = argparse.ArgumentParser()

    parser.add_argument('-i', '--input',
                        help='Шаблон пути для поиска файлов',
                        type=str,
                        required=True
                        )
    parser.add_argument('-o', '--output',
                        help='Файл для вывода данных',
                        type=argparse.FileType(mode='w', encoding='utf-8'),
                        default='-'
                        )
    parser.add_argument('-f', '--format',
                        help='Формат выгружаемых данных',
                        type=str,
                        default='csv',
                        choices=['csv', 'json']
                        )
    parser.add_argument('-s', '--split',
                        help='Разделять результат по отедельным файлам',
                        action='store_true'
                        )
    parser.add_argument('--replace',
                        help='Имена директорий которые нужно заменить при выгрузке',
                        type=str,
                        nargs='*')
    parser.add_argument('--includes',
                        help='Включаемые типы объектов',
                        type=str,
                        nargs='*')
    parser.add_argument('--excludes',
                        help='Включаемые типы объектов',
                        type=str,
                        nargs='*')
    return parser

def parse(xml, **kwargs):
    parser = ParserXML()
    try:
        yield from parser.parse(xml, **kwargs)
    except (NotImplementedTypeError, NotImplementedValueError) as e:
        logger.error(str(e))
    finally:
        del parser

def main():
    import csv
    import json
    import os
    from rrd_utils.classes import ITT
    from rrd_utils.utils import rrd_file_iterator_with_origin_name

    csv.register_dialect('itt', ITT)

    logging.basicConfig(level=logging.DEBUG)
    parser = createParser()
    args = parser.parse_args()

    logger.info('Начало обработки документов')
    for filename, filename_origin in rrd_file_iterator_with_origin_name(args.input):
        logger.debug(filename_origin)
        if args.split:
            filename_output = os.path.extsep.join([os.path.splitext(filename_origin)[0], args.format])
            if args.replace and len(args.replace) == 2:
                filename_output = filename_output.replace(args.replace[0], args.replace[1])
            os.makedirs(os.path.dirname(filename_output), exist_ok=True)
            fileobj_output = open(filename_output, mode='w', encoding='utf-8')
        else:
            fileobj_output = args.output

        if args.format == 'csv':
            writer = csv.DictWriter(fileobj_output, dialect='itt', fieldnames=ATTRIBUTES)
            writer.writeheader()

            for feature in parse(filename, includes=args.includes, excludes=args.excludes):
                try:
                    writer.writerow({key: getattr(feature, key) for key in ATTRIBUTES if getattr(feature, key, None)})
                except Exception as e:
                    logger.exception(e)

        else:
            for feature in parse(filename, includes=args.includes, excludes=args.excludes):
                try:
                    json.dump(feature, fileobj_output, ensure_ascii=False,
                              default=lambda x: {key: getattr(x, key, None)
                                                 for key in dir(x)
                                                 if not key.startswith('_') and not callable(getattr(x, key))})
                    fileobj_output.write('\n')
                except Exception as e:
                    logger.exception(e)

    logger.info('Завершение обработки документов')

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    main()


#!/usr/bin/env python
# -*- coding: utf8 -*-
#----------------------------------------------------------------------------
# Created By  : vloegler
# Created Date: 2022/09/15
# version ='2.0'
# ---------------------------------------------------------------------------
'''
This script check if all chromosomes of a reference are covered by
a draft assembly.
It uses the nucmer (--maxmatch) and show-coords MUMmer4's functions

It takes as input :
	-r --ref: the reference genome assembly (multi fasta)
	-d --draft: draft genome assemblies (multi fasta, several allowed)
	-o --output: name of output tsv file
	-p --percentCovered: Percentage of each chromosome that have to be covered
	-m --mummerPath: Path to mummer function, if not in path
	-t --threads: Number of threads for nucmer
	-i --detailledInfo: Display the coverage of each chromosome of the reference

Output will be:
Assembly	NbChrCovered
Draft1		16
Draft2		15
Draft3		16
'''
# ---------------------------------------------------------------------------
import os
import sys
import argparse
import time
from datetime import datetime
from random import randint
from Tools import *
# ---------------------------------------------------------------------------
# Definitions

def rank_simple(vector):
	return sorted(range(len(vector)), key=vector.__getitem__)

def decreasing_rank_simple(vector):
	return sorted(range(len(vector)), key=vector.__getitem__)[::-1]

def getShowCoords(refPath, draftPath, prefix, mummerPath, threads):
	# Run nucmer
	nucmerShellCommand=mummerPath+"nucmer -t "+str(threads)+" --maxmatch --prefix "+prefix+" "+refPath+" "+draftPath
	os.system(nucmerShellCommand)
	# Run show coords
	showcoordsShellCommand=mummerPath+"show-coords -TH "+prefix+".delta > "+prefix+".coords"
	os.system(showcoordsShellCommand)
	# Remove nucmer delta file
	os.remove(prefix+".delta")

# ---------------------------------------------------------------------------

# =============
# Get arguments
# =============

# Initiate the parser
parser = argparse.ArgumentParser(
	description = """This script check if all chromosomes of a reference are covered by a draft assembly.
	It uses the nucmer (--maxmatch) and show-coords MUMmer4's functions.
	The percentage of each chromosome to be covered can be specified for all chromosomes,
	or each one specifically. """)
parser.add_argument("-r", "--ref", help="reference genome assembly (multi fasta)", required=True)
parser.add_argument("-d", "--draft", help="draft genome assemblies (multi fasta)", nargs='+', required=True)
parser.add_argument("-o", "--output", help="Output file", default = "")
parser.add_argument("-p", "--percentCovered", help="Percentage of each chromosome that have to be covered", type=int, default=80)
parser.add_argument("-pid", "--percentCoveredForID", help="Percentage that have to be covered for a specific chromosome. Syntax is chromosome1=75 for min coverage of 75p on chromosome1. Several allowed after -pid. ", nargs='+', default = "", type=str)
parser.add_argument("-i", "--detailledInfo", help="Display the coverage of each chromosome of the reference", action='store_true')
parser.add_argument("-m", "--mummerPath", help="Path to mummer function, if not in path", type=str, default="")
parser.add_argument("-t", "--threads", help="Number of threads for nucmer", type=int, default=20)

# Read arguments from the command line
args = parser.parse_args()

refPath=args.ref
draftPaths=args.draft
nbDraft=len(draftPaths)
outputPath=args.output
percent=int(args.percentCovered)
percentID=args.percentCoveredForID
nbIDspecific = len(percentID)
mummer=args.mummerPath
if mummer != "" and not mummer.endswith("/") :
	mummer += "/"
threads=args.threads

# Write header in output file
if args.detailledInfo:
	if outputPath != "":
		out = open(outputPath, 'w')
		out.write("Assembly\tChromosome\tCoverage\n")
	else:
		sys.stdout.write("Assembly\tChromosome\tCoverage\n")
else:
	if outputPath != "":
		out = open(outputPath, 'w')
		out.write("Assembly\tNbChrPresent\n")
	else:
		sys.stdout.write("Assembly\tNbChrPresent\n")

# ========================================
# Get reference chromosome name and length
# Read reference fasta
refFasta = Fasta(refPath)

# make list of percentage to cover for each chromosome
percentList = [percent] * len(refFasta) # Default percentage for every Chr
if nbIDspecific != 0: # If specific percentage for a chromosome
	for i in range(nbIDspecific):
		specChr = percentID[i].split("=")[0]
		specPerc = int(percentID[i].split("=")[1])
		if specChr not in refFasta.getID():
			raise ValueError("Chromosome ID specified for specific coverage is not found in the reference genome. ")
		else:
			percentList[refFasta.getIndexFromID(specChr)] = specPerc

refName=refPath.split("/")[-1]

# ==================================================
# Iterate over each draft assembly to get the number
# of chromosomes covered by the draft assembly
# ==================================================
for d in range(nbDraft):

	draftName=draftPaths[d].split("/")[-1]
	#print("\n\t--- Running for draft assembly "+draftName+" ---\n")
	start = time.time()

	# Align draft to ref
	prefix = "Alignment_" + refName + "_" + draftName + "_" + datetime.now().strftime("%Y_%m_%d_%H_%M_%S_%m_%f") + "_" + str(randint(0, 10000))
	getShowCoords(refPath, draftPaths[d], prefix, mummer, threads)
	end = time.time()
	#print("1/2 Alignment done: ran in "+str(round(end-start))+"s")

	start = time.time()
	# Get BED list of reference genome
	refBED = getBED(refPath)

	# ======================================
	# Get BED of alignments for each chromosome of the ref
	alignmentBEDs = [[] for i in refFasta]
	# Alignments BED will contain per Chromosome the BED of alignments of this chromosome on the draft genome
	with open(prefix+".coords", 'r') as coords:
		for line in coords:
			refStart = int(line.split("\t")[0])
			refEnd = int(line.split("\t")[1]) + 1
			Chr = line.split("\t")[7]
			ChrIndex = refFasta.getIndexFromID(Chr)

			alignmentBEDs[ChrIndex] += [BEDcoordinates(id = Chr, start = refStart, end = refEnd)]

	os.remove(prefix+".coords")

	# Convert all to BED
	alignmentBEDs = [BED(x) for x in alignmentBEDs]

	# Get coverage per chromosome
	refCoverage = []
	for i in range(len(refFasta)):
		refCoverage += [refBED.getID(refFasta.getID()[i]).overlapLen(alignmentBEDs[i], percent = True)]

	# Output detailled for each chromosome
	if args.detailledInfo:
		for i in range(len(refFasta)):
			if outputPath != "":
				out.write(draftName + "\t" + refFasta.getID()[i] + "\t" + str(refCoverage[i]) + "\n")
			else:
				sys.stdout.write(draftName + "\t" + refFasta.getID()[i] + "\t" + str(refCoverage[i]) + "\n")

	# Output total number of chromosome presents
	else:
		# Get number of chromosome covered at X%
		nbChrPresent = 0
		for i in range(len(refFasta)):
			if refCoverage[i] >= percentList[i]:
				nbChrPresent += 1

		if outputPath != "":
			out.write(draftName + "\t" + str(nbChrPresent) + "\n")
		else:
			sys.stdout.write(draftName + "\t" + str(nbChrPresent) + "\n")

if outputPath != "":
	out.close()


import numpy as np
from sklearn.mixture import GaussianMixture
from scipy.stats import multivariate_normal
from matplotlib import pyplot as plt

class ExplorationModule:
    def __init__(self, initial_samples, n_components=1, max_samples=None, **kwargs):
        """
        Initialize the exploration module with a Gaussian Mixture Model.

        Parameters:
        - initial_samples: An array of initial sample points.
        - n_components: Number of components (Gaussians) in the initial GMM.
        - max_samples: Maximum number of samples to use for updating the GMM.
        """
        self.n_components = n_components
        self.max_samples = max_samples
        self.samples = initial_samples
        self.gmm = GaussianMixture(n_components=self.n_components)
        self.gmm.fit(self.samples)
        self.upper_bound = kwargs.get("upper_bound", 1)
        self.lower_bound = kwargs.get("lower_bound", -1)

    def update_distribution(self, new_samples):
        """
        Update the GMM with new samples.

        Parameters:
        - new_samples: An array of new sample points.
        """
        # Optionally limit the number of samples to prevent excessive growth
        if self.max_samples and len(self.samples) >= self.max_samples:
            self.samples = self.samples[-self.max_samples:]

        self.samples = np.vstack([self.samples, new_samples])
        self.gmm = GaussianMixture(n_components=self.n_components)
        self.gmm.fit(self.samples)

    def sample_candidate_points(self, n_samples, exploration_rate=1, random=False):
        """
        Generate new candidate points based on the current GMM.

        Parameters:
        - n_samples: Number of candidate points to generate.
        """
        sample =  self.gmm.sample(n_samples)[0]
        sample += np.random.normal(0, exploration_rate, sample.shape)
        # regenerate samples if they are out of bounds
        while np.any(sample > self.upper_bound) or np.any(sample < self.lower_bound):
            sample =  self.gmm.sample(n_samples)[0]
        # if random:
        if random:
            sample = np.random.uniform(self.lower_bound, self.upper_bound, sample.shape)
        return sample

    def assess_novelty(self, points):
        """
        Assess the novelty of given points based on the current GMM.

        Parameters:
        - points: An array of points to assess.
        """
        # Evaluate the probability density of each point under each GMM component
        densities = np.array([multivariate_normal(mean=mean, cov=cov, allow_singular=True).pdf(points)
                              for mean, cov in zip(self.gmm.means_, self.gmm.covariances_)])

        # Novelty score could be the inverse of density or a more complex function
        novelty_scores = 1 / np.max(densities, axis=0)
        return novelty_scores

    def get_variance(self, point):
        """
        Estimate the variance of a given point based on the GMM.

        Parameters:
        - point: The point to estimate variance for.
        """
        # Find the nearest GMM component to the point
        nearest_component = np.argmin(np.linalg.norm(self.gmm.means_ - point, axis=1))
        # Return the variance (diagonal of the covariance matrix) of the nearest component
        return np.diag(self.gmm.covariances_[nearest_component])

    def plot_distribution(self):
        """
        Plot the current GMM.
        """
        # Create a mesh grid on which to evaluate the GMM
        x = np.linspace(self.lower_bound, self.upper_bound, 100)
        y = np.linspace(self.lower_bound, self.upper_bound, 100)
        X, Y = np.meshgrid(x, y)
        XY = np.array([X.ravel(), Y.ravel()]).T

        # Evaluate the GMM's probability density function (PDF) on the grid
        Z = np.exp(self.gmm.score_samples(XY))
        Z = Z.reshape(X.shape)
        # Plot the contour
        plt.contourf(X, Y, Z, levels=50, cmap='viridis')
        plt.colorbar()
        plt.title('GMM Contour Plot')
        plt.xlabel('X-axis')
        plt.ylabel('Y-axis')
        plt.show()

    def plot_2d_distribution(self, candidate_points, novelty_scores, env, particles=None):
        fig, ax = plt.subplots()
        x = np.linspace(env.bounds[0], env.bounds[1], 1000)
        y = np.linspace(env.bounds[0], env.bounds[1], 1000)
        X, Y = np.meshgrid(x, y)
        Z = env.objective_function.evaluate(np.array([X.flatten(), Y.flatten()]).T).reshape(X.shape)

        # Evaluate the GMM PDF on the grid
        XY = np.vstack([X.ravel(), Y.ravel()]).T
        Z_gmm = np.exp(self.gmm.score_samples(XY)).reshape(X.shape)

        # Plot the objective function contour
        ax.contour(X, Y, Z, 50)

        # Plot the GMM contour with some transparency
        ax.contourf(X, Y, Z_gmm, 50, cmap='viridis', alpha=0.5)  # Set alpha for transparency

        ax.set_xlim(env.bounds[0][0], env.bounds[1][0])
        ax.set_ylim(env.bounds[0][1], env.bounds[1][1])

        # Plot particles and candidate points with scaled novelty scores
        if particles is not None:
            ax.scatter(particles[:, 0], particles[:, 1], c='blue', label='Particles')
        novelty_scores = (novelty_scores - np.min(novelty_scores)) / (np.max(novelty_scores) - np.min(novelty_scores)) * 9 + 1
        ax.scatter(candidate_points[:, 0], candidate_points[:, 1], c='red', label='Candidate points', s=novelty_scores*100)

        ax.legend()
        plt.show()


import torch
import torchsparse.nn.functional as F
from torchsparse import PointTensor, SparseTensor
from torchsparse.nn.utils import get_kernel_offsets

__all__ = ['initial_voxelize', 'point_to_voxel', 'voxel_to_point']

# z: PointTensor
# return: SparseTensor
def initial_voxelize(z, init_res, after_res):
    new_float_coord = torch.cat(
        [(z.C[:, :3] * init_res) / after_res, z.C[:, -1].view(-1, 1)], 1)

    pc_hash = F.sphash(torch.floor(new_float_coord).int())
    sparse_hash = torch.unique(pc_hash)
    idx_query = F.sphashquery(pc_hash, sparse_hash)
    counts = F.spcount(idx_query.int(), len(sparse_hash))

    inserted_coords = F.spvoxelize(torch.floor(new_float_coord), idx_query,
                                   counts)
    inserted_coords = torch.round(inserted_coords).int()
    inserted_feat = F.spvoxelize(z.F, idx_query, counts)

    new_tensor = SparseTensor(inserted_feat, inserted_coords, 1)
    new_tensor.cmaps.setdefault(new_tensor.stride, new_tensor.coords)
    z.additional_features['idx_query'][1] = idx_query
    z.additional_features['counts'][1] = counts
    z.C = new_float_coord

    return new_tensor

# x: SparseTensor, z: PointTensor
# return: SparseTensor
def point_to_voxel(x, z):
    if z.additional_features is None or z.additional_features.get('idx_query') is None\
       or z.additional_features['idx_query'].get(x.s) is None:
        #pc_hash = hash_gpu(torch.floor(z.C).int())
        pc_hash = F.sphash(
            torch.cat([
                torch.floor(z.C[:, :3] / x.s[0]).int() * x.s[0],
                z.C[:, -1].int().view(-1, 1)
            ], 1))
        sparse_hash = F.sphash(x.C)
        idx_query = F.sphashquery(pc_hash, sparse_hash)
        counts = F.spcount(idx_query.int(), x.C.shape[0])
        z.additional_features['idx_query'][x.s] = idx_query
        z.additional_features['counts'][x.s] = counts
    else:
        idx_query = z.additional_features['idx_query'][x.s]
        counts = z.additional_features['counts'][x.s]

    inserted_feat = F.spvoxelize(z.F, idx_query, counts)
    new_tensor = SparseTensor(inserted_feat, x.C, x.s)
    new_tensor.cmaps = x.cmaps
    new_tensor.kmaps = x.kmaps

    return new_tensor

# x: SparseTensor, z: PointTensor
# return: PointTensor
def voxel_to_point(x, z, nearest=False):
    if z.idx_query is None or z.weights is None or z.idx_query.get(
            x.s) is None or z.weights.get(x.s) is None:
        off = get_kernel_offsets(2, x.s, 1, device=z.F.device)
        #old_hash = kernel_hash_gpu(torch.floor(z.C).int(), off)
        old_hash = F.sphash(
            torch.cat([
                torch.floor(z.C[:, :3] / x.s[0]).int() * x.s[0],
                z.C[:, -1].int().view(-1, 1)
            ], 1), off)
        pc_hash = F.sphash(x.C.to(z.F.device))
        idx_query = F.sphashquery(old_hash, pc_hash)
        weights = F.calc_ti_weights(z.C, idx_query,
                                    scale=x.s[0]).transpose(0, 1).contiguous()
        idx_query = idx_query.transpose(0, 1).contiguous()
        if nearest:
            weights[:, 1:] = 0.
            idx_query[:, 1:] = -1
        new_feat = F.spdevoxelize(x.F, idx_query, weights)
        new_tensor = PointTensor(new_feat,
                                 z.C,
                                 idx_query=z.idx_query,
                                 weights=z.weights)
        new_tensor.additional_features = z.additional_features
        new_tensor.idx_query[x.s] = idx_query
        new_tensor.weights[x.s] = weights
        z.idx_query[x.s] = idx_query
        z.weights[x.s] = weights

    else:
        new_feat = F.spdevoxelize(x.F, z.idx_query.get(x.s),
                                  z.weights.get(x.s))
        new_tensor = PointTensor(new_feat,
                                 z.C,
                                 idx_query=z.idx_query,
                                 weights=z.weights)
        new_tensor.additional_features = z.additional_features

    return new_tensor


from __future__ import print_function
import cv2 as cv
import argparse

profileface_path= "haarcascades/haarcascade_frontalcatface.xml"
profileface_cascade = cv.CascadeClassifier()
#-- 1. Load the cascades
if not profileface_cascade.load(cv.samples.findFile(profileface_path)):
    print('--(!)Error loading face cascade')
    exit(0)

cap = cv.VideoCapture(2)
if not cap.isOpened:
    print('--(!)Error opening video capture')
    exit(0)
while True:
    ret, frame = cap.read()
    frame_gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    frame_gray = cv.equalizeHist(frame_gray)
    #-- Detect faces
    faces = profileface_cascade.detectMultiScale(frame_gray)
    for (x,y,w,h) in faces:
        center = (x + w//2, y + h//2)
        frame = cv.rectangle(frame,(x,w),(y,h),(0,255,0),2)
        # faceROI = frame_gray[y:y+h,x:x+w]
        #-- In each face, detect eyes
        # v2.rectangle(img,(x1,y1),(x2,y2),(0,255,0),2)

    cv.imshow('Capture - Face detection', frame)
    if frame is None:
        print('--(!) No captured frame -- Break!')
        break
    if cv.waitKey(10) == 27:
        break

cap.release()
cv2.destroyAllWindows()


import asyncio
import os
import time

from aiogram.fsm.context import FSMContext
from aiogram.types import Message, CallbackQuery, FSInputFile
from aiogram.filters import Command
from aiogram import F, Router
from aiogram.utils.markdown import hcode

from create_bot import bot, config
from .filters import AdminFilter
from .inline import AdminInlineKeyboard
from tgbot.misc.states import AdminFSM
from ...services.excel import xlsx_parser
from ...services.orecht import get_card_info
from ...services.ozon_api import OzonAPI

router = Router()
router.message.filter(AdminFilter())

inline = AdminInlineKeyboard()

ozon_api = OzonAPI()

admin_group = config.tg_bot.admin_group

async def main_screen_render(start: bool, user_id: int | str):
    if start:
        text = "Это главный экран бота. Чтобы скопировать карточки товаров, нажмите на клавишу нижу 👇"
    else:
        text = "ГЛАВНОЕ МЕНЮ"
    kb = inline.main_menu_kb()
    await bot.send_message(chat_id=admin_group, text=text, reply_markup=kb)

@router.message(Command("start"))
async def main_block(message: Message, state: FSMContext):
    await state.set_state(AdminFSM.home)
    await main_screen_render(start=True, user_id=message.from_user.id)

@router.callback_query(F.data == "home")
async def main_block(callback: CallbackQuery, state: FSMContext):
    await state.set_state(AdminFSM.home)
    await main_screen_render(start=False, user_id=callback.from_user.id)
    await bot.answer_callback_query(callback.id)

@router.callback_query(F.data == "clone")
async def main_block(callback: CallbackQuery, state: FSMContext):
    text = "Введите ClientID аккаунта"
    kb = inline.home_kb()
    await state.set_state(AdminFSM.client_id)
    await callback.message.answer(text, reply_markup=kb)
    await bot.answer_callback_query(callback.id)

@router.message(F.text, AdminFSM.client_id)
async def main_block(message: Message, state: FSMContext):
    text = "Введите токен"
    kb = inline.home_kb()
    await state.update_data(client_id=message.text.strip())
    await state.set_state(AdminFSM.api_token)
    await message.answer(text, reply_markup=kb)

@router.message(F.text, AdminFSM.api_token)
async def main_block(message: Message, state: FSMContext):
    file_name = f'{os.getcwd()}/template.xlsx'
    file = FSInputFile(path=file_name, filename=file_name)
    text = "Заполните шаблон ссылками и загрузите в бот"
    kb = inline.home_kb()
    await state.update_data(ozon_token=message.text.strip())
    await state.set_state(AdminFSM.get_data)
    await message.answer_document(document=file, caption=text, reply_markup=kb)

@router.message(F.document, AdminFSM.get_data)
async def main_block(message: Message, state: FSMContext):
    file_name = f"{os.getcwd()}/data.xlsx"
    await bot.download(file=message.document, destination=file_name)
    file_data = await xlsx_parser(file=file_name)
    state_data = await state.get_data()
    ozon_token = state_data["ozon_token"]
    client_id = state_data["client_id"]
    if len(file_data) == 0:
        await message.answer("Лист не должен быть пустым")
        return
    await message.answer("Ожидайте... ⏳")
    item_list = []
    for row in file_data:
        if row:
            item = dict(sku=int(row["ozon_id"]), art=row["outer_id"], outer_source=row["outer_source"])
            item_list.append(item)
    task_id = await ozon_api.clone_card(item_list=item_list, ozon_token=ozon_token, client_id=client_id)
    await message.answer(f"ID задачи {hcode(task_id)}\nПроверяем результаты клонирования ⏳")
    await asyncio.sleep(30)
    clone_result = await ozon_api.clone_status(task_id=task_id, ozon_token=ozon_token, client_id=client_id)
    kb = inline.home_kb()
    await asyncio.sleep(1)
    if len(clone_result) > 0:
        text = f"{len(clone_result)} / {len(file_data)} товаров скопированы с ошибками. Запускается " \
               f"парсер\n<u>Внимание! процесс может занять длительное время. Пожалуйста, не прерывайте работу бота</u>"
        await message.answer(text)
    else:
        await message.answer("✅ Все товары скопированы", reply_markup=kb)
        return
    error_items = [dict(offer_id=i["offer_id"], product_id=i["product_id"]) for i in clone_result]
    count_msg = await message.answer(f"Принудительно скопировано 0 / {len(error_items)} товаров")
    counter = 0
    archived_articles = []
    for item in error_items:
        offer_id = item["offer_id"]
        product_id = item["product_id"]
        try:
            card_attrs = await ozon_api.get_card_attrs(offer_id=offer_id, ozon_token=ozon_token, client_id=client_id)
            time.sleep(9)
            is_archived = await ozon_api.delete_cards(ozon_token=ozon_token,
                                                      client_id=client_id,
                                                      archive_item=[product_id],
                                                      delete_item=[{"offer_id": offer_id}])
            if is_archived:
                archived_articles.append(offer_id)
            if offer_id.split("-")[0] == "РСВ":
                oreht_data = await get_card_info(item_art=offer_id.split("-")[-1])
                if not oreht_data:
                    await message.answer("Неправильная ссылка в Oreht")
                    continue
                images = [oreht_data["image"]]
            else:
                images = [i["file_name"] for i in card_attrs["result"][0]["images"]]
            result = await ozon_api.create_card(income_data=card_attrs,
                                                images=images,
                                                price=str(2000),
                                                ozon_token=ozon_token,
                                                client_id=client_id)
            if result:
                counter += 1
                await count_msg.edit_text(f"Принудительно скопировано {counter} / {len(error_items)} товаров")
            else:
                await message.answer(f"{offer_id} не найден")
        except Exception as ex:
            await message.answer(f"{offer_id} error: {ex}")
    archived_items_chunks = ozon_api.paginator(item_list=archived_articles, size=20)
    for chunk in archived_items_chunks:
        text = ["Созданные ранее товары, сейчас в архиве:", "-" * 5]
        for item in chunk:
            text.append(hcode(item))
        await message.answer("\n".join(text))
    os.remove(file_name)
    await state.set_state(AdminFSM.home)
    text = "✅ Цикл завершён"
    await message.answer(text, reply_markup=kb)


#!/usr/bin/python
import curses
import os
import re
import pexpect
import sys
import textwrap
import pickle
from threading import Thread

import start_app
from actions import edit, suspend_curses, list_dir, cat_file, open_shell_with
from color import get_color, get_rgb
from ansi import ansi_to_attr_list

ORIG_CWD = os.getcwd()
TMP_FILE = '/tmp/gitco-gh-issues'
os.chdir('/home/david/dev/inboundsms')

from plumbum.cmd import hub, highlight, firefox, git
from plumbum import local, FG
from contextlib import contextmanager

@contextmanager
def in_orig_dir():
    os.chdir(ORIG_CWD)
    yield
    os.chdir('/home/david/dev/inboundsms')

def checkout_branch_starting_with(prefix):
    with in_orig_dir():
        branch = git('branch', '--list', '%s*' % prefix).split('\n')[0][2:].strip()
        if branch:
            git('checkout', branch)

def run_in_tty(cmd, *args):
    child = pexpect.spawn(cmd, list(args))
    try:
        child.expect(None)
    except pexpect.exceptions.EOF:
        return child.before.decode('utf-8')

class Issues():
    def __init__(self):
        self.issues = []
        self.load_issues_cache()
        t = Thread(target=self.load_issues)
        t.start()

    def load_issues_cache(self):
        if os.path.exists(TMP_FILE):
            with open(TMP_FILE, 'r') as f:
                self.issues = pickle.load(f)

    def load_issues(self):
        fields = {
            'number': 'i',
            'id': 'I',
            'url': 'U',
            'title': 't',
            'body': 'b',
            'labels': 'l',
            'author': 'au',
            'assignes': 'as',
            'milestone': 'Mt',
            'comment_count': 'NC',
            'created_at': 'ct',
            'updated_at': 'ut',
        }

        field_pairs = fields.items()
        field_separator = '---djoji2e101u182sd----'
        issue_separator = '---1oij231j231kasds----'
        format = field_separator.join('%' + pair[1] for pair in field_pairs) + issue_separator

        resp = (run_in_tty('hub', 'issue', '-a', 'none', '-a', 'none', '-f', format) +
                run_in_tty('hub', 'issue', '-a', 'dvdbng', '-f', format))

        if issue_separator not in resp:
            print(resp)
            sys.exit(1)

        res = [
            dict(zip((pair[0] for pair in field_pairs), issue_raw.split(field_separator)))
            for issue_raw in resp.split(issue_separator)
            if field_separator in issue_raw
        ]
        res.sort(key=lambda i: (len(i.get('milestone', '')), i.get('milestone')))
        res.reverse()
        self.issues = res
        with open(TMP_FILE, 'w') as f:
            f.write(pickle.dumps(res))

    def find_by_id(self, id):
        for issue in self.issues:
            if issue['id'] == id:
                return issue

    def issue_title(self, issue):
        status = re.search(' ([0-5]) -', issue['labels'])
        status = status.group(1) if status else ' '
        status_color = get_color(curses.COLOR_WHITE, get_rgb(0, 0, 255)) if status != ' ' else 0

        prioritym = re.search('(\d+;\d+;\d+)m (low|medium|high|critical) ', issue['labels'], re.I)
        priority = prioritym.group(2).lower() if prioritym else ' '
        priority_color = get_color(curses.COLOR_WHITE, get_rgb(*map(int, prioritym.group(1).split(';')))) if prioritym else 0

        assigned = 'A' if 'dvdbng' in issue['assignes'].lower() else ' '
        assigned_color = get_color(curses.COLOR_WHITE, curses.COLOR_GREEN) if assigned == 'A' else 0

        return [
            (issue['number'].rjust(8), get_color(curses.COLOR_GREEN)),
            (u' ', 0),
            (status, status_color),
            (priority[0].upper(), priority_color),
            (assigned, assigned_color),
            (u' ' + issue['title'], 0),
        ]

    def show_issue(self, issue):
        if issue is None:
            return "No issue"
        text = issue['body'].replace('\r', '')
        text = "\n".join(textwrap.wrap(text, width=120, replace_whitespace=False))

        formatted = (highlight['-O', 'ansi', '--syntax', 'markdown'] << text)()
        issue['formatted_body'] = formatted
        return textwrap.dedent(u"""
        Title: %(title)s
        Author: %(author)s
        Labels: %(labels)s
        Url: %(url)s
        Assigned: %(assignes)s
        Milestone: %(milestone)s
        Comments: %(comment_count)s

        %(formatted_body)s""") % issue

    def update(self, diff, menu):
        options = []

        i = 0
        last_milestone = None
        for issue in self.issues:
            if last_milestone == None or last_milestone != issue['milestone']:
                options.append([('In milestone %s' % issue['milestone'], get_color(curses.COLOR_BLUE) | curses.A_BOLD)])
                last_milestone = issue['milestone']
            options.append(self.issue_title(issue))

        if len(options) == 0:
            options.append([('no issues', 0)])
        menu.set_options(options)

        issue = self.selected_issue(menu.selected_line())
        if issue:
            diff.set_text(self.show_issue(issue))
        else:
            diff.set_text(menu.selected_line())

    def selected_issue(self, selected_line):
        if selected_line.strip().startswith('#'):
            return self.find_by_id(selected_line.strip().split(' ')[0][1:])
        else:
            return None

    def handle(self, key, menu, win):
        if key == ord('q'):
            sys.exit(0)
            return True
        elif key == curses.KEY_F5:
            start_app.App.app.set_status('Refreshing...')
            self.load_issues()
            return True

        issue = self.selected_issue(menu.selected_line())
        if issue is None:
            return

        if key == ord('g'):
            firefox(issue['url'])
        elif key == ord('b'):
            name = '%s_%s' % (issue['id'], re.sub('[^a-z0-9_]', '', re.sub('\s+', '_', issue['title'].lower())))
            with in_orig_dir():
                open_shell_with('git mkbranch %s ; and exit' % name)
        elif key == ord('c'):
            checkout_branch_starting_with(issue['id'])
        else:
            return False
        return True

app = Issues()
start_app.main(app.handle, app.update)


import os
from unittest import result
import openai
import json
import argparse

openai.api_key = os.getenv("OPENAI_API_KEY")
example_data = {
	"puzzle":"A man walks into a bar and asks the bartender for a drink of water. The bartender pulls out a qun, points it at the man and cocks it. The man pauses before saying \"Thank you\"and leaving. What happened?",
	"solution":"The man had the hiccups. The bartender realized this and chose instead to cure the hiccups by frightening the man with the gun.",
	"question_list":[
		"Could the bartender hear him?",
		"Did the man ask for water in an offensive way?"
	],
	"answer_list": [
		"Yes",
		"No"
	]
}

def load_data(data_path):
	with open(data_path, 'r') as f:
		data = json.load(f)
	return data

def convert_to_question_generation_prefix(item_dict):
	task_description = "I am an intelligent bot that can play situation puzzles with user. A puzzle is given first, and the user begin to ask a \"yes/no\" question to ensure details."
	result_prefix = task_description+'\n'+"Puzzle: "+example_data['puzzle']+'\n'
	result_prefix += "Question: "+example_data["question_list"][0]+'\n'
	result_prefix += "Answer: "+example_data["answer_list"][0]+'\n'
	result_prefix += "Question: "+example_data["question_list"][1]+'\n'
	result_prefix += '\n'
	result_prefix += "Puzzle:"+item_dict['senario'][0]+' '+item_dict['question']+'\n'
	if "question_list" in item_dict:
		for question, answer in zip(item['question_list'], item['answer_list']):
			result_prefix += "Question: "+question+'\n'
			result_prefix += "Answer: "+answer+'\n'
	result_prefix += "Question: "
	return result_prefix

def convert_to_answer_generation_prefix(item_dict):
	task_description = "I am an intelligent bot that can play as judge in situation puzzles with user. A puzzle is given first, and the user begin to ask a \"yes/no\" question to ensure details, and I will give \"Yes/No/Irrelevent\" as answer to questions."
	result_prefix = task_description+'\n'+"Puzzle: "+example_data['puzzle']+'\n'
	result_prefix += "Solution: "+example_data["solution"]
	result_prefix += "Question: "+example_data["question_list"][0]+'\n'
	result_prefix += "Answer: "+example_data["answer_list"][0]+'\n'
	result_prefix += "Question: "+example_data["question_list"][1]+'\n'
	result_prefix += "Answer: "+example_data["answer_list"][1]+'\n'
	result_prefix += '\n'
	result_prefix += "Puzzle:"+item_dict['senario'][0]+' '+item_dict['question']+'\n'
	if "question_list" in item_dict and "answer_list" in item_dict:
		for question, answer in zip(item['question_list'], item['answer_list']):
			result_prefix += "Question: "+question+'\n'
			result_prefix += "Answer: "+answer+'\n'
	result_prefix += "Question: "+item['question_list'][-1]+'\n'
	result_prefix += "Answer: "
	return result_prefix

def convert_to_hint_generation_prefix(item_dict):
	pass

def convert_to_solution_generation_prefix(item_dict):
	task_description = "I am an intelligent bot that can play situation puzzles with user. A puzzle is given first, and the user begin to ask \"yes/no\" question to ensure details, then I will give the question a\"yes/no/irrelevent\" answer. Finally user try to give solution for the puzzle."
	result_prefix = task_description+'\n'+"Puzzle: "+example_data['puzzle']+'\n'
	result_prefix += "Question: "+example_data["question_list"][0]+'\n'
	result_prefix += "Answer: "+example_data["answer_list"][0]+'\n'
	result_prefix += "Question: "+example_data["question_list"][1]+'\n'
	result_prefix += "Answer: "+example_data["answer_list"][1]+'\n'
	result_prefix += '\n'
	result_prefix += "Puzzle:"+item_dict['senario'][0]+' '+item_dict['question']+'\n'
	if "question_list" in item_dict and "answer_list" in item_dict:
		for question, answer in zip(item['question_list'], item['answer_list']):
			result_prefix += "Question: "+question+'\n'
			result_prefix += "Answer: "+answer+'\n'
	result_prefix += "Solution: "
	return result_prefix

def Jaccard_similarity(infer_sent, ref):
	infer_sent_list = infer_sent.split(' ')
	ref_list = ref.split(' ')
	infer_sent_set = set(infer_sent_list)
	ref_set = set(ref_list)
	union_set = infer_sent_set.union(ref_set)
	intersetion_set = infer_sent_set.intersection(ref_set)
	jaccard_score = float(len(intersetion_set)/len(union_set))
	return jaccard_score

def get_response(prompt):
	response = openai.Completion.create(
		model="text-davinci-002",
		prompt=prompt,
		temperature=0.7,
		max_tokens=100,
		top_p=1,
		frequency_penalty=0.0,
		presence_penalty=0.0,
		stop=["\n\n"]
		)
	return response.get('choices')[0]['text'].split("\n")[0]

if __name__=="__main__":
	parse = argparse.ArgumentParser()
	parse.add_argument('--sample_num', type=int, default=1, help="the sample number to be infer")
	parse.add_argument('--max_turn', type=int, default=5)
	parse.add_argument('--with_hint', action='store_true')
	args = parse.parse_args()

	dataset = load_data("situation-data/puzzles.json")
	dataset = list(dataset.values())

	for item in dataset[:args.sample_num]:
		turn_count = 0
		while turn_count < args.max_turn:
			question_generation_prompt = convert_to_question_generation_prefix(item)
			# import ipdb;ipdb.set_trace()
			generated_question = get_response(question_generation_prompt)

			if 'question_list' not in item:
				item["question_list"] = [generated_question]
			else:
				item['question_list'].append(generated_question)
			answer_generation_prompt = convert_to_answer_generation_prefix(item)
			generated_answer = get_response(answer_generation_prompt)
			if 'answer_list' not in item:
				item["answer_list"] = [generated_answer]
			else:
				item["answer_list"].append(generated_answer)
			solution_generation_prompt = convert_to_solution_generation_prefix(item)
			generated_solution = get_response(solution_generation_prompt)
			print(f"turn_{turn_count+1}: ", generated_solution)
			if 'solution_history' not in item:
				item["solution_history"] = [generated_solution]
			else:
				item["solution_history"].append(generated_solution)
			jaccard_score = Jaccard_similarity(generated_solution, item['answer'][0])
			if jaccard_score>0.5:
				break
			turn_count += 1
	with open('output/infer_output.json', 'w') as f:
		json.dump(dataset[:args.sample_num], f)


from django.urls import path
# from django.conf.urls import url
from . import views

urlpatterns = [
    path('', views.index, name='main'),
    path('about_me', views.about, name='about'),
    path("python", views.button, name='python'),
    path("output", views.output, name='script'),
]


from __future__ import annotations

import albumentations as A
from albumentations.pytorch import ToTensorV2
from lightning import Trainer
from lightning.pytorch import loggers as pl_loggers
from model.architectures.unet import UNet
from model.model import SegmentationModel

from data.datamodules import VOCDatamodule

def main():
    # mean of the imagenet dataset for normalizing
    train_transform = A.Compose(
        [
            A.Resize(height=160, width=160),
            A.Rotate(limit=35, p=1.0),
            A.HorizontalFlip(p=0.5),
            A.VerticalFlip(p=0.1),
            A.Normalize(
                mean=[0.0, 0.0, 0.0],
                std=[1.0, 1.0, 1.0],
                max_pixel_value=255.0,
            ),
            ToTensorV2(),
        ],
    )
    inference_transform = A.Compose(
        [
            A.Resize(height=160, width=160),
            A.Normalize(
                mean=[0.0, 0.0, 0.0],
                std=[1.0, 1.0, 1.0],
                max_pixel_value=255.0,
            ),
            ToTensorV2(),
        ],
    )
    datamodule = VOCDatamodule(
        train_transform=train_transform, inference_transform=inference_transform,
    )
    model = SegmentationModel(UNet())

    tb_logger = pl_loggers.TensorBoardLogger(save_dir='logs/')
    trainer = Trainer(max_epochs=500, accelerator='gpu', logger=tb_logger)
    trainer.fit(model, datamodule)

if __name__ == '__main__':
    main()


import requests
from get_token import token

student_id = 2
url = f'http://127.0.0.1:3001/alunos/{student_id}'

headers = {
  'Authorization': token
}

user_data = {
	"nome": "luana",
	"sobrenome": "da silva",
	"email": "laaa@email.com",
	"idade": "20",
	"peso": "60.08",
	"altura": "1.70"
}

response = requests.put(url=url, json=user_data, headers=headers)

if response.ok:
  response_data = response.json()
  print(response_data)

else:
  print(response.status_code)
  print(response.text)


import asyncio
from openai import AsyncOpenAI
import os
from FileTransform import file_transform
import ChatFileConfig
import json
import shutil

config = ChatFileConfig.Config("./config.json")
config.get_init()
storage = []

## ChatGPT参数设置

client = AsyncOpenAI(
    base_url = config.base_url,
    api_key = config.api_key,
)

# 发送请求给 OpenAI GPT
async def chatmd(message,dialogue_history,model=config.model,temperature=config.temperature,max_tokens=config.max_tokens):
    # 将当前消息添加到对话历史中
    dialogue_history.append(message)
    # 发送请求给 OpenAI GPT
    response = await client.chat.completions.create(
        model=model,
        messages=dialogue_history,
        temperature=temperature, # 控制模型输出的随机程度
        max_tokens=max_tokens,  # 控制生成回复的最大长度
        stream=True, # 是否是流式生成
    )

    print("\nGPT>> ",end="")
    assistant_message = ""
    async for part in response:
        w = part.choices[0].delta.content or ""
        assistant_message += w
        print(w,end="")
    print("\n")
    dialogue_history.append({'role':'assistant','content':assistant_message})

## 让GPT读取文章
async def gpt_read_content(content,dialogue_file):

    ## 获得对话历史
    if os.path.exists(dialogue_file):
        with open(dialogue_file, 'r', encoding='utf-8') as f:
            dialogue_history = json.load(f)
        # 将读文章指令发送给他
        end_message = "文章已发送完毕，接下来我将提出一些与文章相关的问题，请根据内容以markdown格式进行回答，我的第一个问题是'Summarize the main content of the article.'"
        end_message = {'role':'user','content':end_message}
        await chatmd(end_message,dialogue_history)
    else:
        if content is None:
            print(f"not exists {dialogue_file}")
        dialogue_history = [
            {'role':'system','content':config.sys_content}
        ]

        ## 字符串切分
        def ContentSplit(string,length):
            return [string[i:i+length] for i in range(0, len(string), length)]

        contents = ContentSplit(content,1000) # 分段内容
        pages = len(contents) # 分段数

        ## 分段输入
        # start
        start_message = f"我现在会将文章的内容分 {pages} 部分发送给你。请确保你已经准备好接收，接收到文章发送完毕的指令后，请准备回答我的问题。"
        dialogue_history.append({'role':'user','content':start_message})
        # 文章
        for i in range(pages):
            content_message = {'role':'user','content':contents[i]}
            dialogue_history.append(content_message)
        # end
        end_message = "文章已发送完毕，接下来我将提出一些与文章相关的问题，请你使用中文，根据内容以markdown格式进行回复，我的第一个问题是'Summarize the main content of the article.'"
        end_message = {'role':'user','content':end_message}
        await chatmd(end_message,dialogue_history)

        ## 保存阅读会话
        with open(dialogue_file,"w",encoding="utf-8") as f:
            json.dump(dialogue_history,f)
    return dialogue_history

## 交流循环
async def main_loop(file_path):
    print("##SYS: 正在处理文件...")
    content,dialogue_file,floder_name = file_transform(file_path)
    print(floder_name.center(75,'='))
    print("##SYS: 正在分析文件...")
    dialogue_history = await gpt_read_content(content,dialogue_file)

    while True:
        user_input = input("User: ")
        if user_input == "quit":
            return True
        message = {'role':'user','content':user_input.strip()}
        await chatmd(message,dialogue_history)

async def main_loop2(floder_name,dialogue_file):
    print(floder_name.center(75,'='))
    print("##SYS: 正在分析文件...")
    dialogue_history = await gpt_read_content(None,dialogue_file)

    while True:
        user_input = input("Usr>> ")
        if user_input == "quit":
            return True
        message = {'role':'user','content':user_input.strip()}
        await chatmd(message,dialogue_history)

def get_dir(path):
    return os.listdir(path)

async def main():
    while True:
        storage = get_dir(config.data_path)
        user_input = input("\n##SYS: 请输入指令(ls, edit, del <FloderName>, <FloderName>, <FilePath>):\n\nUsr>> ").strip().lstrip()
        if user_input == "ls":
            print(*storage)
        elif user_input in storage:
            dialogue_file = config.data_path + "/" + user_input + "/dialogue.json"
            await main_loop2(user_input,dialogue_file)
        elif user_input[0:3] == "del":
            floder = user_input[3:]
            del_path = config.data_path + '/' + floder.strip().lstrip()
            print(f"##SYS: you will delete {del_path}")
            if os.path.exists(del_path):
                shutil.rmtree(del_path)
            else:
                print("\n##SYS: Wrong FloderName...")
        elif user_input == "edit":
            print(" Edit(enter to pass) ".center(75,'='))
            with open("./config.json",'r',encoding="utf-8") as f:
                config_ = json.load(f)
                url_base = input(f"##SYS: url_base({config_['base_url']}):").strip().lstrip()
                if url_base != "":
                    config_['base_url'] = url_base
                api_key = input(f"##SYS: api_key({config_['api_key']}):").strip().lstrip()
                if api_key != "":
                    config_['api_key'] = api_key
                model = input(f"##SYS: model({config_['model']}):").strip().lstrip()
                if model != "":
                    config_['model'] = model
            with open("./config.json",'w',encoding="utf-8") as f:
                json.dump(config_,f,ensure_ascii = False)
            config.get_init()
        else:
            if os.path.exists(user_input):
                await main_loop(user_input)
            else:
                print("\n##SYS: Wrong file path...")

if __name__ == "__main__":
    asyncio.run(main())


import argparse
import os
import re
import subprocess
import xml.dom.minidom as mxml
import time

import entity_finder
import builder
from unity_tester import UnityTester

def cmd_build(entities, pymk_root, args):
    print("======[Start to build applications]=========")
    for app in entities.apps:
        builder.AppBuilder(app, pymk_root)
    print("======[Finished building applications]======")

def cmd_test(entity_map, pymk_root, args):
    print("======[Start testing of code]===============")
    tester = UnityTester(entity_map, pymk_root, args)
    tester.prepare()
    tester.run()
    print("======[Finished testing of code]============")

def cmd_clean(entity_map, pymk_root, args):
    print("clean")

def cmd_stats(entity_map, pymk_root, args):
    print("stats")

def cmd_info(entity_map, pymk_root, args):
    print("info")

def main():
    parser = argparse.ArgumentParser(description="Tool to build and test c code.", epilog="Commands iterate through sub-directories recursively, looking for sources, dependencies and unit tests.")
    subparsers = parser.add_subparsers()

    parser_build = subparsers.add_parser("build", help="Iterate directories, compile and link modules and programs.")
    parser_build.set_defaults(func=cmd_build)

    parser_test  = subparsers.add_parser("test",  help="Iterate directories, generate mocks, build and run tests.")
    parser_test.set_defaults(func=cmd_test)

    parser_clean = subparsers.add_parser("clean", help="Remove build and test files.")
    parser_clean.set_defaults(func=cmd_clean)

    parser_stats = subparsers.add_parser("stats", help="Displays useful info about modules.")
    parser_stats.set_defaults(func=cmd_stats)

    parser_info  = subparsers.add_parser("info",  help="Display info on how to use this build system.")
    parser_info.set_defaults(func=cmd_info)

    arguments = parser.parse_args()

    pymk_root = os.path.dirname(os.path.abspath(__file__))
    pymk_root = pymk_root.replace("/cygdrive", "")
    print(pymk_root)

    entities = entity_finder.EntityFinder(pymk_root, exclude_dir_names=["mocks"])
    entities.run()

    arguments.func(entities, pymk_root, arguments)

if __name__ == "__main__":
    main()


import numpy
from srxraylib.plot.gol import plot, set_qt
set_qt()
import scipy.constants as codata

rms = numpy.linspace(0,20,21)
theta = 1.25 * numpy.pi / 180

wavelength = codata.h * codata.c / codata.e / 250
print("Wavelength: %g A" % (wavelength * 1e10))

delta_phi = 2 * rms * 1e-9 * numpy.sin(theta) # wavelength / 14

sr1 = 1 -  (2 * numpy.pi / wavelength * delta_phi)**2
sr2 = numpy.exp(-(2 * numpy.pi / wavelength * delta_phi)**2)
sr3 = (1 -  0.5 * (2 *numpy.pi / wavelength * delta_phi)**2) ** 2
sr4 =  1 - (2 * numpy.pi * rms)**2

print(sr1,sr2,sr3)

plot(rms,sr1,
     rms, sr2,
     rms, sr3,
     legend=["orig","exp","square"],
     yrange=[0,1.1])


import os
import shutil

import pyttsx3 as pt

# huawei cloud tts

from huaweicloud_sis.client.tts_client import TtsCustomizationClient
from huaweicloud_sis.bean.tts_request import TtsCustomRequest
from huaweicloud_sis.bean.sis_config import SisConfig
from huaweicloud_sis.utils.logger_utils import logger, file_handler
import logging.handlers

# 修改huawei tts所维护的log的位置
logger.removeHandler(file_handler)
file_handler.close()
try:
    os.remove('huaweicloud_sis.log')
except:
    pass

"""
http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&text=
"""

def base_tts(text, location):
    engine = pt.init()
    engine.save_to_file(text, location)
    engine.runAndWait()

def huawei_tts(text, location):
    # 修改huawei tts所维护的log的位置
    adapted_handler = logging.handlers.RotatingFileHandler('misc/tts/huaweicloud_sis.log', maxBytes=1024 * 1024,
                                                           backupCount=5, encoding='utf-8')
    adapted_handler.setLevel(logging.INFO)
    adapted_handler.setFormatter(logging.Formatter('[%(asctime)s] - [%(levelname)s] - [%(message)s]'))
    logger.addHandler(adapted_handler)
    assert len(text.encode('utf8')) <= 500
    paras = get_config()
    ak, sk, region, project_id = paras
    text = text  # 待合成文本，不超过500字
    path = location  # 保存路径，需要具体到音频文件，如D:/test.wav，可在设置中选择不保存本地

    # step1 初始化客户端
    config = SisConfig()
    config.set_connect_timeout(10)  # 设置连接超时，单位s
    config.set_read_timeout(10)  # 设置读取超时，单位s
    # 设置代理，使用代理前一定要确保代理可用。 代理格式可为[host, port] 或 [host, port, username, password]
    # config.set_proxy(proxy)
    ttsc_client = TtsCustomizationClient(ak, sk, region, project_id, sis_config=config)

    # step2 构造请求
    ttsc_request = TtsCustomRequest(text)
    # 设置请求，所有参数均可不设置，使用默认参数
    # 设置属性字符串， language_speaker_domain, 默认chinese_xiaoyan_common
    ttsc_request.set_property('chinese_huaxiaowen_common')
    # 设置音频格式，默认wav，可选mp3和pcm
    ttsc_request.set_audio_format('wav')
    # 设置采样率，8000 or 16000, 默认8000
    ttsc_request.set_sample_rate('16000')
    # 设置音量，[0, 100]，默认50
    ttsc_request.set_volume(50)
    # 设置音高, [-500, 500], 默认0
    ttsc_request.set_pitch(0)
    # 设置音速, [-500, 500], 默认0
    ttsc_request.set_speed(0)
    # 设置是否保存，默认False
    ttsc_request.set_saved(True)
    # 设置保存路径，只有设置保存，此参数才生效
    ttsc_request.set_saved_path(path)

    # step3 发送请求，返回结果。如果设置保存，可在指定路径里查看保存的音频。
    try:
        result = ttsc_client.get_ttsc_response(ttsc_request)
    # print(json.dumps(result, indent=2, ensure_ascii=False))
    finally:
        logger.removeHandler(adapted_handler)
        adapted_handler.close()

def get_config():
    par = []
    with open(r'misc/tts/huawei.txt') as f:
        for line in f:
            line = line.strip()
            if not line or line[0] == '#':
                continue
            assert line
            par.append(line)
    return par

def render(text, location):
    try:
        huawei_tts(text, location)
    except:
        base_tts(text, location)

if __name__ == '__main__':
    base_tts('今天的天气还是挺不错的', r'E:\Files\Projects\TyTalk\test.wav')


__all__ = [
    'PixieDustHandler', 'PixieDustLogHandler', 'ExecuteCodeHandler', 'PixieAppHandler',
    'PixieAppListHandler', 'PixieAppPublishHandler', 'ChartShareHandler', 'StatsHandler',
    'AdminHandler', 'ChartEmbedHandler', 'ChartsHandler', 'OEmbedChartHandler', 'LoginHandler',
    'AdminCommandHandler'
]

import inspect
import json
import os
import traceback
from uuid import uuid4
import tornado
from tornado.log import app_log
import pixiegateway
from pixiegateway.exceptions import CodeExecutionError, AppAccessError
from pixiegateway.session import SessionManager

class BaseHandler(tornado.web.RequestHandler):
    """Base class for all PixieGateway handler"""
    def initialize(self):
        self.output_json_error = False

    def _handle_request_exception(self, exc):
        print("Got an exception: {}".format(exc))
        if isinstance(exc, AppAccessError):
            return self.send_error(401)

        html_error = "
Unexpected error:

{}
".format(
            str(exc) if isinstance(exc, CodeExecutionError) else traceback.format_exc()
        )
        if self.output_json_error:
            msg = {
                "buffers": [],
                "channel": "iopub",
                "content": {
                    "data": {
                        "text/html": html_error
                    },
                    "metadata": {},
                    "transient": {}
                },
                "header": {
                    "username": "pixiegateway",
                    "msg_type": "display_data",
                    "msg_id": uuid4().hex,
                    "version": "5.2"
                },
                "metadata": {},
                "msg_id": "",
                "msg_type":"display_data",
                "parent_header": {}
            }
            self.write(json.dumps([msg]))
        else:
            self.write(html_error)
        self.finish()

    # def create_template_loader2(self, template_path):
    #     print("Create template loader: {}".format(template_path))
    #     file = None
    #     if template_path.startswith("/"):
    #         file = pixiegateway.__file__
    #         print("resolving: {}".format(file))
    #     else:
    #         file = inspect.getfile(inspect.currentframe())

    #     template_path = os.path.dirname(
    #         os.path.abspath(
    #             file
    #         )
    #     )
    #     settings = self.application.settings
    #     kwargs = {}
    #     if "autoescape" in settings:
    #         # autoescape=None means "no escaping", so we have to be sure
    #         # to only pass this kwarg if the user asked for it.
    #         kwargs["autoescape"] = settings["autoescape"]
    #     if "template_whitespace" in settings:
    #         kwargs["whitespace"] = settings["template_whitespace"]
    #     return tornado.template.Loader(template_path, **kwargs)

    def render_string(self, template_name, **kwargs):
        try:
            self.template_name = template_name
            return super(BaseHandler, self).render_string(template_name.strip("/"), **kwargs)
        finally:
            self.template_name = None

    def get_template_path(self):
        if self.template_name.startswith("/"):
            file_path = pixiegateway.__file__
        else:
            file_path = inspect.getfile(inspect.currentframe())
        return os.path.dirname(os.path.abspath(file_path))

    def prepare(self):
        """
        Retrieve session for current user
        """
        self.session = SessionManager.instance().get_session(self)
        app_log.debug("session %s", self.session)

    def get_current_user(self):
        return self.get_secure_cookie("pd_user")

    def set_default_headers(self):
        self.set_header("Access-Control-Allow-Origin", "*")
        self.set_header("Access-Control-Allow-Headers", "x-requested-with")
        self.set_header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')

from .adminHandlers import AdminHandler, StatsHandler, AdminCommandHandler
from .handlers import (PixieDustHandler, PixieDustLogHandler, ExecuteCodeHandler, PixieAppHandler,
    PixieAppListHandler, PixieAppPublishHandler, ChartShareHandler,
    ChartEmbedHandler, ChartsHandler, OEmbedChartHandler, LoginHandler)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models
import datetime
from django.utils.timezone import utc

class Migration(migrations.Migration):

    dependencies = [
        ('todoitems', '0002_auto_20190615_2059'),
    ]

    operations = [
        migrations.AlterField(
            model_name='todoitem',
            name='expired_date',
            field=models.DateTimeField(default=datetime.datetime(2019, 6, 15, 21, 2, 54, 399837, tzinfo=utc)),
        ),
        migrations.AlterField(
            model_name='todoitem',
            name='start_date',
            field=models.DateTimeField(null=True),
        ),
    ]


from datetime import date
from datetime import datetime

# Custom function to calculate time differences

def time_dif(date, choice):
    diff = date - choice
    return diff

# Give instructions
print("This program will calculate the difference between today's date and certain historical events.")

# Initialize today's date and historical events
today = date.today()
napoleon = date(1769, 8, 15)
pearl = date(1941, 12, 7)
wright = date(1903, 12, 17)

# Print list of choices
choice = input("Would you like to calculate the time between today and: \n(1) Napoleon's brithday \n(2) The attack on Pearl Harbor \n(3) The flight of the Wright brothers\n(4) Custom date\n")

# Print results based on user's choice
if choice == "1":
    print("Napoleon\'s birthday was on " + napoleon.strftime("%B %d, %Y") +
          ". That was {} ago.".format(time_dif(today, napoleon)))
elif choice == "2":
    print("The attack on Pearl Harbor occurred on " + pearl.strftime("%B %d, %Y") +
          ". That was {} ago.".format(time_dif(today, pearl)))
elif choice == "3":
    print("The Wright brothers flew their first flight on " +
          wright.strftime("%B %d, %Y") + ". That was {} ago.".format(time_dif(today, wright)))
elif choice == "4":
    custom_input = input(
        "Today's date is {}. Please enter the past date you would like to calculate from: \n(Use format mm/dd/yyyy)\n".format(today.strftime("%B %d, %Y")))
    custom_datetime = datetime.strptime(custom_input, '%m/%d/%Y')
    print("The time difference between today and " + custom_datetime.date().strftime(
        "%B %d, %Y") + " is {}.".format(time_dif(today, custom_datetime.date())))
else:
    print("Invalid Selection")


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
VIOLANI MATTEO
MATRICOLA: 921109

PROGETTO DI PROGRAMMAZIONE RETI, TRACCIA 2 WEB-SERVER
@author: teo
"""
import sys, signal
import http.server
import socketserver
import cgi
import json
import HTMLdataGenerate as HTMLgen

#modulo per estrapolazione di parametri da richiesta tipo GET
#da installare pip install w3lib
from w3lib.url import url_query_parameter as url_param

ADMIN_PATH_PAGE = "html/admin.html"
INFO_PATH = 'html/info.html'

PRENOT_PAGE = '/prenVisita.html'
ADMIN_PAGE = '/admin.html'
COVID_PAGE = '/covid.html'
INFO_PAGE = '/info.html'
ADD_DOTT = 'add'
RMV_DOTT = 'remove'
PREN_PAGE = '/prenVisita.html'

#configurazione porta socket server, se non specificata il default è 8080
if sys.argv[1:] :
    port = int(sys.argv[1])
else:
    port = 8080

server_addr= ('',port)

#pagina di accesso alla schermata da amministratore
logInPage= '''
                <form method="post" accept-charset=utf-8>

Accesso Amministratore

				User:
  				<input type="text" id="user" name="user">

				Password:
  				<input type="password" id="psw" name="psw">

			<input type="submit" value="login" style="">
                </form>
                '''

#pagina di negato login causa password o user non corretti
loginErrorPage= '''

User o Password Errate

Clicca qui per riprovare--> riprova

'''

addDoctorSuccessPage ='''

Aggiunta/Rimozione dottore avvenuta con successo

Clicca qui per tonrare indietro--> Indietro

'''

editDoctorFailPage = '''

Aggiunta/Rimozione dottore NON CONCLUSA

Caso di aggiunta: Medico già presente o presenza di campi vuoti

Caso rimozione: Medico assente nel database

Clicca qui per tonrare indietro--> Indietro

'''

#
class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler):

    def logInPage(self):
        self.send_response(307) #codice stato http di temporaneo reindirizzamento
        self.end_headers()
        self.wfile.write(bytes(logInPage, 'utf-8'))

    def failDoctorPage(self):
        self.send_response(200)
        self.end_headers()
        #invio di pagina di errore rimozione dottore
        self.wfile.write(bytes(editDoctorFailPage, 'utf-8'))

    def editDoctorRequest(self):
        request = self.path
        # estrapolazione dati prenotazione ricevuti dal form
        nameDoctor= url_param( request,'dottName')
        codeDoctor= url_param( request,'dottCode')
        editMode= url_param(request, 'mode')
#       print(nameDoctor)
#       print(codeDoctor)
#       print(editMode)
        #memorizzazione nuovo medico nel file json (databse ipotetico)
        with open("json/dottori.json", "r+") as out:
            #conversione dei file json in oggetto python--> vocabolario
            dott =json.loads(out.read())
            #aggiunta del dottore nel file
        print(dott)
        try:
            if editMode == ADD_DOTT:
            #se i campi sono vuoti non aggiungere medico vuoto
                if (not nameDoctor) or (not codeDoctor):
                    #se uno dei due campi è vuoto si genera una eccezione
                    #che manda al client la pagina di fallimento aggiunta
                    raise TypeError
                else:
                    if codeDoctor in dott["code"]:
                    #se il medico è già presente impedisce di mettere doppi
                        raise TypeError
                    else:
                        dott["name"].append(nameDoctor)
                        dott["code"].append(codeDoctor)
                        print("AGGIUNTO dottore: "+ str(nameDoctor) +', '+ str(codeDoctor))
            else:
            #richiesta di cancellazione dottore, effetuata solo
            #se presente nel database
                if codeDoctor in dott["code"] and editMode == RMV_DOTT:
                #ricava l'indice in cui si trova il cod. del medico
                    idx_code = dott["code"].index(codeDoctor)
                    print("inizio rimozione dottore ")
                    print(dott["name"][idx_code] +' '+dott["code"][idx_code])
                    #rimozione di nameDoctor e codeDoctor dal dizionario python
                    #che poi sara convertito in file json
                    dott["name"].pop(idx_code)
                    dott["code"].pop(idx_code)
                else:
                    raise TypeError

            with open("json/dottori.json", "r+") as out:
                #cancellazione del file
                out.seek(0)
                out.truncate()
                #scrittura dell'oggeto python in formato json
                json.dump(dott,out)

            #risposta di OK al client
            self.send_response(200)
            self.end_headers()
            #invio di pagina di conferma aggiunta dottore
            self.wfile.write(bytes(addDoctorSuccessPage, 'utf-8'))

        except Exception as e:
            print("Errore: "+ str(e))
            self.failDoctorPage()

    def checkLogin(self):
        try:
            form = cgi.FieldStorage(
            fp=self.rfile,
            headers=self.headers,
            environ={'REQUEST_METHOD':'POST'})

            #estrapolo i campi immessi in delle variabili
            #(si trascura la sicurezza dei dati)
            user= form.getvalue('user')
            pssw= form.getvalue('psw')
            print(user +' '+ pssw)
            with open("json/login.json", "r") as inFile:
                #caricamento e conversione dati da formato json a
                #dictionary di python
                users = json.loads(inFile.read())

                if (users[user] == pssw):
                    #se esiste l'username inserito si verifica che la password
                    #digitata corrisonda con quella salvata nel database (login.json)
                    self.send_response(200)
                    self.end_headers()
                    adminHTMLpage = HTMLgen.genAdminHTMLPage()
                    self.wfile.write(bytes(adminHTMLpage, 'utf-8'))

                else:
                    print("Password errata")
                    self.send_response(401)
                    self.end_headers()
                    self.wfile.write(bytes(loginErrorPage,'utf-8'))

        except Exception as e:
            print(e)
            self.send_response(401)
            self.end_headers()
            self.wfile.write(bytes(loginErrorPage,'utf-8'))

    #in questo metodo si definisce come il server deve agirea richieste di tipo GET
    def do_GET(self):
        request = self.path
        print('file REQUESTED: ' + request)
        #se si vuole accedere alla pagina da amministratore prima si viene reindirizzati
        #alla logInPage
        if request == ADMIN_PAGE:
            self.logInPage()
        else:
            #gestione della richiesta di aggiungere un nuovo medico;
            #verifico che la path ricevuta contenga i campi del form che rigurda
            #l'aggiunta del medico
            if (request.__contains__(ADMIN_PAGE) and
                request.__contains__("dottName") and
                request.__contains__("dottCode")):
                print("RICHIESTA Modifica DOTTORi")
                self.editDoctorRequest()
            else:
                if request == COVID_PAGE:
                    #il client ha rischiesto la pagina con i dati covid
                    self.send_response(200)
                    self.end_headers()
                    #scarico il file json con i dati delle vaccinazioni in locale
                    covidHTML = HTMLgen.refreshCovidData()
                    #restituisco la pagina con tab covid aggiornata
                    self.wfile.write(bytes(covidHTML, 'utf-8'))
                else:
                    if request == PREN_PAGE:
                        #il client ha richiesto la pagina delle prenotazioni
                        #genera la pagina delle prenotazioni a runtime
                        prenHTML = HTMLgen.genPrenotVisita()
                        self.send_response(200)
                        self.end_headers()
                        #invio la pagina della prenotazione al client in risposta
                        self.wfile.write(bytes(prenHTML, 'utf-8'))
                    else:
                        #se pagina info richiesta, reindirizzati alla corretta path
                        if request == INFO_PAGE:
                            self.path = INFO_PATH
                        #se non si tratta di nessun caso sopra chiamo il metodo del
                        #modulo per risolvere la richiesta e restituire il file
                        #, se presente nella path, corrispondente all 'url digitato
                        return http.server.SimpleHTTPRequestHandler.do_GET(self)

    def doPrenotazione(self):
        print('\nPrenotazione_POST')
        try:
            # estrapolazione dati prenotazione ricevuti dal form
            form = cgi.FieldStorage(
            fp=self.rfile,
            headers=self.headers,
            environ={'REQUEST_METHOD':'POST'})

        # Con getvalue prendo i dati inseriti dall'utente
            doctor = form.getvalue('doctor')
            healthService = form.getvalue('prestazione')
            name = form.getvalue('fname')
            lastName = form.getvalue('lname')

        # creo messaggio da restituire all'utente
            output="RICHIESTA ACCOLTA  NOME e COGNOME: " + name + " "+ lastName +" Prestazione: " + healthService + " Dottore: "+ doctor +"\n"
            succP1 = '''
Aggiunta dottore avvenuta con successo
''' +output
            succP2='''

Clicca qui per tonrare indietro--> Indietro
'''
            #comunico al client che il mesasggio post è stato ricevuto correttamente
            self.send_response(200)
        except:
            self.send_error(404, 'Campi non compilati presenti!')
            return;
        self.end_headers()
        #invio al client un messaggio di ricevuta consegna
        self.wfile.write(bytes(succP1+succP2, 'utf-8'))

        # Salvataggio prenotazione in formato json in un file (ipotetico database)
        with open("json/prenotazioni.json", "r+") as out:
            lines = out.readlines()
            #eliminazione ultime due righe di chiusura json
            out.seek(0)
            out.truncate()
            out.writelines(lines[:-2])
            #creazione stringa da aggiungere in fondo al file, per aggiungere
            #prenotazione nuova effettuata
            info = ',{"name": "' + name + '","lastName" :"' + lastName +'", "doctor" : "'+ doctor+ '","service" : "'+ healthService + '" } \n'
            info += '] \n }'
            #scrittura nuova prenotazione con chiusura oggetto json
            out.write(info)

    def do_POST(self):
        print("POST path: "+self.path)
        #con l'if vado a distiguere i casi dei due form, quello delle prenotazini
        #da quello utilizzato per il login dell'admin
        if (self.path == PRENOT_PAGE):
            self.doPrenotazione()

        else :
            #RICHIESTA (POST) LOGIN PAGINA ADMIN
            if (self.path == ADMIN_PAGE):
                print("\nlogin_POST")
                self.checkLogin()

            else :
                self.send_error(404, 'Richiesta non gestita')

Handler = MyHttpRequestHandler
#configurazione di un server multi thread per consentire l'accesso multimo
server = socketserver.ThreadingTCPServer(server_addr,Handler)

server.daemon_threads = True

#concede la riassegnazione del socket
server.allow_reuse_address = True

#funzione interupt per consentire l'arresto del server corretamente
def signal_handler(signal, frame):
    print("Ctrl+C premuto: interruzione http server...")
    try:
        if (server):
            server.server_close()
            print('server terminato correttamente')
    finally:
        sys.exit()

#l'interupt da tastiera (ctrl-c) avvia signal_handler
signal.signal(signal.SIGINT, signal_handler)
print("Server avviato correttamente\n")
print("...in attesa di richieste sulla porta: "+ str(port))
while True:
    server.serve_forever()

server.server_close()


import random
import argparse
import os
import shutil
import sys
if os.name == 'nt':
    import win32api, win32con

def check_positive(number):
    if number <= 0:
        sys.exit('ERROR! The supplied number is 0 or negative. Only numbers greater than 0 are allowed.')

def is_hidden(file):
    if os.name == 'nt':
        if win32api.GetFileAttributes(file) & (win32con.FILE_ATTRIBUTE_HIDDEN | win32con.FILE_ATTRIBUTE_SYSTEM):
            return True
    else:
        if os.path.basename(file).startswith('.'):
            return True

    return False

# Generate random number between 0 and step size
def random_start(limit):
    # TODO: We could add a seed into SystemRandom(*seed*) to make runs reproducible
    start = random.SystemRandom().randint(0, limit - 1)  # Limit -1 as we start at 0
    print('Randomly starting at file no. ' + str(start + 1))
    return start

# Get a list of the files
def list_dir(path, file_extension=None):
    try:
        files = os.listdir(path)
    except PermissionError:
        sys.exit('ERROR! Could not read directory ' + path + '. No permission.')
    except FileNotFoundError:
        sys.exit('ERROR! Could not read directory ' + path + '. Invalid path.')

    files.sort()

    data = []
    if file_extension is not None:
        for file in files:
            if file.endswith(file_extension):
                if not is_hidden(path + file):
                    data.append(path + file)
        return data
    else:
        for file in files:
            if not is_hidden(path + file):
                data.append(path + file)
        return data

# Generate list of files to copy
def draw_sample(files, start, step):
    size = len(files)
    if size < start or size < step:
        sys.exit('ERROR! Step size is larger than number of files! '
                 'Choose a smaller step size and check that you are working on the correct directory.')

    sample_set = []
    i = start
    while i < size:  # This is important! The index of the last file is len - 1, as the index starts at 0!
        sample_set.append(files[i])
        i = i + step

    return sample_set

# Perform copying action
def write_sample(sample_set, destination=None):
    if destination is not None:
        path = destination
    else:
        path = 'sample'
        print('No directory specified, writing output to folder ' + path + '/')

    # Check if path exists
    if os.path.exists(path):
        # If it exists it should be empty
        if len(os.listdir(path)) != 0:
            sys.exit('ERROR! The directory ' + path + ' is not empty. '
                                                      'Please specify another directory using the -o option.')
    # If not, try to create it
    else:
        try:
            os.mkdir(path)
        except PermissionError:
            sys.exit('ERROR! Could not create directory ' + path + '. No permission.')
        except FileNotFoundError:
            sys.exit('ERROR! Could not create directory ' + path + '. Invalid path.')

    for entry in sample_set:
        shutil.copy2(entry, path)  # TODO: Should we follow symlinks?

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Draw a uniform random sample from a folder containing an image stack')
    parser.add_argument('step', action='store', type=int, help='Step size')
    parser.add_argument('directory', action='store', type=str, help='Directory containing the image stack')
    parser.add_argument('-o', '--output', action='store', type=str, required=False,
                        help='Directory to write the sample to. '
                             'If not supplied, a folder with the name sample is created in the current directory')
    parser.add_argument('-f', '--filetype', action='store', type=str, required=False,
                        help='Type of the files to draw a sample from '
                             '(e.g. if non images are present in the directory)')
    # TODO: Option to link files?
    #  This would safe space if the user works on the same machine the image stack is located on

    # TODO: Option to supply random value seed to recreate a specific run?

    # TODO: Option to specify a file pattern? => Would extend the file type filter

    # TODO: Status output for bored users?

    args = parser.parse_args()

    step_size = args.step
    directory = args.directory
    output = args.output
    filetype = args.filetype

    check_positive(step_size)

    print('Generating list of files')
    file_list = list_dir(directory, filetype)

    print('Creating a random number')
    start_point = random_start(step_size)

    print('Creating list of samples')
    sample = draw_sample(file_list, start_point, step_size)

    print('Writing samples to output directory')
    write_sample(sample, output)

    print('Done!')