# -*- coding: utf-8 -*-
from tei_auxiliary import prepare_func_name, clean_text, css_value_parser
from docx_constructor import DocxConstructor
from css_builder import CSS
import bs4

class DocxCommon:

    """
    A class of auxiliary functions for DOCXBuilder
    """

    @staticmethod
    def empty(*args, **kwargs):
        """
        Stub function. Does nothing.
        """
        pass

    @staticmethod
    def is_nested(elem, child=None):
        """
        Checks if the vertex has children of a certain type.
        :param elem: html node
        :param child: str
        :return: bool
        """
        if child and len(elem.find_all(child)) > 0:
            return True
        elif not child:
            elem = [i
                for i in elem.childGenerator()
                if isinstance(elem, bs4.element.Tag)
            ]
            return len(elem) > 1
        return False

    @staticmethod
    def if_display_block(cls):
        """
        Checks whether node has display_block class.
        :param cls: list
        :return: bool
        """
        if "display-block" in cls:
            return True
        return False

    def execute_elem_func(self, elem):
        """
        Selects a function for element parsing.
        :param elem: html node
        :return: None
        """
        cls = elem.attrs['class']
        if cls:
            func = prepare_func_name(cls[0])
            getattr(self, func, self.empty)()

    # def execute_p_func(self, elem, p):
    #     cls = elem.attrs['class']
    #     if cls:
    #         func = prepare_func_name(cls[0], ftype="style")
    #         getattr(self, func, self.empty)(*p)

    def find_children(self, parent, child_type, **kwargs):
        """
        Finds children elements by type.
        :param parent: html node
        :param child_type: node selector str
        :param kwargs: b4s find_all arguments
        :return: None
        """
        children = parent.find_all(child_type, **kwargs)
        func = prepare_func_name(child_type)
        for child in children:
            getattr(self, func, self.empty)(child)

class DOCXBuilder(DocxCommon):

    """
    Interface for converting to DOCX format.
    """

    def __init__(self, css=None):
        self.DC = DocxConstructor()
        if isinstance(css, CSS):
            self.css = css.css_dict()
        else:
            self.css = {}

    def transform(self, soup, output_filename="output.docx"):
        """
        The function starts the transformation process.
        :param soup: b4s object
        :param output_filename: str
        :return: None
        """
        self.find_children(soup, "p")
        self.DC.save(output_filename)

    # def parse_article(self, doc):
    #     for elem in doc:
    #         if elem.name and elem.text != "\n":
    #             self.find_children(elem, "p")

    def parse_p(self, part):
        """
        Parse paragraph.
        :param part: bs4 entry
        :return: None
        """
        classes = part.attrs.get("class", [])
        spans = part.find_all("span")
        if spans:
            self.parse_sp(classes, spans)
        elif not self.is_nested(part):
            self.parse_lowest_level(classes, part)

    def parse_sp(self, classes, spans):
        """
        Parse paragraph nested with span.
        :param classes: list
        :param spans: bs4 entry
        :return: None
        """
        params = self.DC.add_line('')
        self.apply_class_styles(classes, params)
        paragraph = params[0]
        for span in spans:
            if not self.is_nested(span, child="span"):
                paragraph = self.parse_span(span, paragraph)

    def parse_span(self, span, paragraph):
        """
        Parse span.
        :param span: bs4 entry
        :param paragraph: python-docx paragraph
        :return: None
        """
        classes = span.attrs.get("class", [])
        if self.if_display_block(classes):
            params = self.parse_display_block(classes, span)
        else:
            text = clean_text(span.text)
            run = paragraph.add_run(" " + text)
            params = (paragraph, run, run.font)
            self.apply_class_styles(classes, params)
        return params[0]

    def parse_display_block(self, classes, part):
        """
        Parse entry with display_block class.
        Forms a new paragraph with the content.
        :param classes: list
        :param part: bs4 entry
        :return: None
        """
        self.parse_lowest_level(classes, part)
        params = self.DC.add_line('')
        return params

    def parse_lowest_level(self, classes, part):
        """
        Parse text level element.
        :param classes: list
        :param part: bs4 entry
        :return: None
        """
        text = clean_text(part.text)
        if text:
            params = self.DC.add_line(text)
            self.apply_class_styles(classes, params)
            return params
        elif 'pb' in classes:
            self.add_new_page()

    def add_new_page(self):
        """
        Adds a new page.
        :return: None
        """
        params = self.DC.add_line('')
        self.DC.add_pb(*params)

    def apply_class_styles(self, classes, params):
        """
        Apply class styles.
        :param classes: list
        :param params: python-docx paragraph params (paragraph, run, run.font)
        :return: None
        """
        for cl in classes:
            cl_tag = '.{}'.format(cl)
            self.apply_custom_style(cl_tag, params)

    def apply_custom_style(self, tag, params):
        """
        Apply custom styles according to the class tag.
        :param tag: str
        :param params: python-docx paragraph params (paragraph, run, run.font)
        :return: None
        """
        rule = self.css.get(tag)
        if rule:
            style_obj = rule.style
            for key in style_obj.keys():
                self.parse_style_rule(style_obj, key, params)

    def parse_style_rule(self, style_obj, key, params):
        """
        Parses a css rule and translates it into docx-python functions.
        Extends all references to units of measurement. Defines the method.
        :param style_obj: cssutils rule object for a specific selector
        :param key: name of a style parameter (font-weight, etc.)
        :param params: python-docx paragraph params (paragraph, run, run.font)
        :return: None
        """

        line = style_obj[key]
        func = prepare_func_name(key, ftype='add')
        value = css_value_parser(line)
        if value.get('measure'):
            getattr(self.DC, func, self.empty)(
                *params, eval(value['value']),
                measure=value['measure'])

        elif list(value.keys()) == ['r', 'g', 'b']:
            values = [int(i) for i in value.values()]
            getattr(self.DC, func, self.empty)(*params, values)

        else:
            getattr(self.DC, func, self.empty)(*params, line)


from silex.engines.thread import ThreadEngine

from carbon.db.constant import JobStatus, TaskType
from carbon.db.models import CarbonJobsModel, CarbonTasksModel
from carbon.utils.logger import logger
from carbon.utils.target_parser import is_ip_target, is_url_target

class SplitEngine(ThreadEngine):
    def __init__(self, app_ctx, name):
        super(SplitEngine, self).__init__(app_ctx, name)

    def _worker(self):
        logger.info("{} start!".format(self.name))

        while self.is_running():

            rows = CarbonJobsModel.instance.filter(status=JobStatus.READY).all()
            if not rows:
                self.ev.wait(1)
                continue

            for row in rows:
                payloads = row.payloads
                job_id = row.id
                scan_type = row.scan_types
                cycle = row.cycle

                row.status = JobStatus.PREPARE
                row.save()

                self.split_job(payloads, job_id, cycle, scan_type)

        logger.info("{} stop!".format(self.name))

    def split_job(self, payloads, job_id, cycle, scan_types):
        """
        将job切分成task
        :param payloads:
        :param job_id:
        :param cycle:
        :param scan_types:
        :return:
        """

        # 将payload转换为list，并且去掉空串
        payloads = payloads.replace("\r", "\n")
        payloads = payloads.split("\n")
        payloads.remove("")

        # 将job的扫描类型转换为list
        scan_types = scan_types.split(",")

        # 循环处理每个payload
        for payload in payloads:
            if is_ip_target(payload):
                # 这里不需要考虑任务转换功能
                self._add_task(payload, scan_types, job_id, cycle, "ip")
            elif is_url_target(payload):
                # 添加url类型的任务
                self._add_task(payload, scan_types, job_id, cycle, "url")
            else:
                logger.warning("skipping unknown task: {}".format(payload))
                continue

    @staticmethod
    def _add_task(target, scan_types, job_id, cycle, _type):

        _task_types = {
            "ip": [
                TaskType.PORT,
                TaskType.ATTACK,
            ],
            "url": [
                TaskType.SUB_DOMAIN,
                TaskType.BRUTE_DIR,
                TaskType.WEB_SPIDER,
                TaskType.ATTACK,
            ]
        }

        for st in scan_types:
            if st in _task_types.get(_type, []):
                CarbonTasksModel.instance.create_task(
                    job_id, st, target, cycle
                )


import torch
import torch.nn as nn
from collections import OrderedDict

class RobertaWithClfHead(nn.Module):
    def __init__(self, roberta, config):
        super(RobertaWithClfHead, self).__init__()

        self.roberta = roberta
        self.classifier = nn.Sequential(OrderedDict([
            ('dropout_1', nn.Dropout(config['dropout_prob'])),
            ('dense', nn.Linear(in_features=config['linear_1_in_features']*config["no_of_pooling_layers"], out_features=config['linear_1_out_features'])),
            ('batchnorm', nn.BatchNorm1d(num_features=config['linear_1_out_features'])) if config["normalization"]=="batchnorm" else ('layernorm', nn.LayerNorm(config['linear_1_out_features'])),
            ('relu', nn.ReLU()) if config['activation']=='relu' else ('gelu', nn.GELU()),
            ('dropout_2', nn.Dropout(config['dropout_prob'])),
            ('out_proj', nn.Linear(in_features=config['linear_1_out_features'], out_features=config['num_labels']))
        ]))
        self.classifier.apply(self.init_weights)

    def forward(self, no_of_pooling_layers=1, **kwargs):
        inputs = kwargs

        if no_of_pooling_layers == 1:
            outputs = self.roberta(**inputs, output_hidden_states=True)
            hidden_states = outputs.last_hidden_state
            pooler = hidden_states[:, 0, :]
        else:
            outputs = self.roberta(**inputs, output_hidden_states=True)
            hidden_states = outputs.hidden_states
            pooling_layers = [l for l in range(-no_of_pooling_layers, 0)]
            pooler = torch.cat(tuple([hidden_states[i] for i in pooling_layers]), dim=-1)[:, 0, :]

        logits = self.classifier(pooler)
        return logits

    def init_weights(self,m):
        if type(m) == nn.Linear:
            torch.nn.init.xavier_uniform_(m.weight)
            torch.nn.init.zeros_(m.bias)


# -*- coding: utf-8 -*-
import requests
import bs4
from bs4 import BeautifulSoup as bs
import json
import lxml
import re

def getHTMLText(url):
    try:
        r = requests.get(url, timeout=30)
        r.raise_for_status()    # 产生异常信息
        r.encoding = r.apparent_encoding
        print(r.json())
        return r.text
    except:
        return "网页获取错误"
    return ""

def fillUnivList(ulist, html):
    soup = bs(html, 'html.parser')
    for tr in soup.find('tbody').children:
        if isinstance(tr, bs4.element.Tag):
            tds = tr('td')  # tds = tr.find_all('td')
            ulist.append([tds[0].string, tds[1].string, tds[2].string, tds[3].string])

def printUnivList(ulist, num):
    tplt = "{0:^10}\t{1:{4}^10}\t{2:{4}^10}\t{3:^10}"
    print(tplt.format("排名", "学校名称", "省市", "分数", chr(12288)))
    for i in range(num):
        u = ulist[i]
        print(tplt.format(u[0], u[1], u[2], u[3], chr(12288)))

def main():
    uinfo = []
    url = "https://vjudge.net/comment/thread?path=error&t=1524644989968.5"
    html = getHTMLText(url)
    print(html)
    # fillUnivList(uinfo, html)
    # printUnivList(uinfo, 100)

main()


# coding:utf-8
"""
create on July 14, 2020 By Wayne YU
Email: ieeflsyu@outlook.com
Function:

根据汤姐的建议，研究中国AS TOP5两两之间互联网络对比的情况

CN-TOP5

AS4809(电信CN2网)
AS4134(电信163网)
AS132203(腾讯公司)
AS45102(阿里巴巴）
AS24429(阿里巴巴-淘宝）

- - - - - - - - - -
Plus

AS4837(联通169网)
AS9929(联通IP承载A网)
AS9808(移动CMNET网)

此处可以进行两个实验，
其一，按照汤姐的意思，对中国排名TOP5的AS网络画像及其互联关系进行综合分析。
其二，对我国三家运行商五个AS网络（2+2+1=5）的网络画像及其互联关系进行综合分析。

二者的思路和方法是一样，只是实验时，分组不同而已

第一组（CN-TOP5），4809、4134、132203、45102、24429
第二组（CN-ISP），4809、4134、4837、9929、9808
- - - - - - - - - - - - - - - - - -
需要用到的一些统计数据源
最新的AS互联关系数据，000LocalData/as_relationships/serial-1/20200701.as-rel.txt
AS-Info，000LocalData/as_Gao/asn_info.txt
国家缩写对应关系中文，000LocalData/as_geo/GeoLite2-Country-Locations-zh-CN.csv

以上面的数据作为输入，
1）统计每个AS号的互联关系，包括总的互联数量、对等AS号、客户AS号、提供商AS号
2）绘制每个AS号的网络画像，ASN、ORG、Country、Relationships

++++++++++++++++++++++++++++++++++++++++++
V2
汤姐希望，对4809、4134、701、6939、174、3356
各个AS的对等方以及客户网络中ISP和Not-ISP的数量进行比较

"""
import time
import csv

def write_to_csv(res_list, des_path):
    """
    把给定的List，写到指定路径的文件中
    :param res_list:
    :param des_path:
    :return: None
    """
    print("write file <%s> ..." % des_path)
    csv_file = open(des_path, 'w', newline='', encoding='utf-8')
    try:
        writer = csv.writer(csv_file, delimiter=",")
        for i in res_list:
            writer.writerow(i)
    except Exception as e:
        print(e)
    finally:
        csv_file.close()
    print("write finish!")

def as_info_anlysis(as_info_file, country_map_cn):
    """
    根据as info和Country缩写中文映射关系，绘制全球所有AS号的简单画像
    :param as_info_file:
    :param country_map_cn:
    :return:
    """
    # 获取国家缩写与其对应的中文国家名
    # print(country_map_cn)
    country_info_dict = {}
    file_read = open(country_map_cn, 'r', encoding='utf-8')
    for line in file_read.readlines():
        line = line.strip().split(",")
        country_info_dict[line[4]] = line[5]
    # 绘制全球所有AS号的简单画像
    # print(as_info_file)
    as_info_dict = {}
    as_info_file_read = open(as_info_file, 'r', encoding='utf-8')
    for line in as_info_file_read.readlines():
        line = line.strip().split("\t")
        as_number = line[0]
        as_org = line[1][0:-4]
        as_country = country_info_dict[line[1][-2:]].strip("\"")
        # print(as_number, as_org, as_country)
        as_info_dict[as_number] = [as_org, as_country]
    return as_info_dict

def as_rel_anlysis(as_number_list, as_rel_file):
    """
    根据传入的as 号码，统计其互联关系，包括Peer AS, Transit Customer AS, Transit Provider AS
    :param as_rel_file:
    :param as_number_list:
    :return as_rel_dict:
    """
    as_rel_dict = {}
    for item_as in as_number_list:
        peer_as_list = []  # 存储Peer AS list
        transit_customer_as_list = []  # 存储Transit Customer AS list
        transit_provider_as_list = []  # 存储Transit Provider AS list
        file_read = open(as_rel_file, 'r', encoding='utf-8')
        for line in file_read.readlines():
            if line.strip().find('#') == 0:
                continue
            as_0 = line.strip().split('|')[0]
            as_1 = line.strip().split('|')[1]
            rel_type = line.strip().split('|')[2]
            if as_0 == item_as:  # 如果位于第一位
                if rel_type == '0':  # Peer关系，后面是对等网络的AS号
                    peer_as_list.append(as_1)
                if rel_type == '-1':  # Transit关系,后面是客户的AS号
                    transit_customer_as_list.append(as_1)
            if as_1 == item_as:  # 如果位于第二位
                if rel_type == '0':  # Peer关系，前面是对等网络的AS号
                    peer_as_list.append(as_0)
                if rel_type == '-1':  # Transit关系，前面是提供商的AS号
                    transit_provider_as_list.append(as_0)
        as_rel_dict[item_as] = [peer_as_list, transit_customer_as_list, transit_provider_as_list]
    return as_rel_dict

def gain_as_core_map_dict(as_core_map_file):
    """
    根据AS Core Map File获取每个AS号的信息
    :param as_core_map_file:
    :return as_core_map_dict:
    """
    as_core_map_dict = {}
    file_read = open(as_core_map_file, 'r', encoding='utf-8')
    for line in file_read.readlines():
        line = line.strip().split('|')
        as_core_map_dict[line[0]] = line[1:]
    return as_core_map_dict

def print_as_set(as_set, as_info_dict, as_core_map_dict):
    """
    根据传入的as_set,结合as_info_dict,按特定格式输出
    若为空集，则还需要进行进行异常处理
    :param as_set:
    :param as_info_dict:
    :param as_core_map_dict:
    :return:
    """
    # pass
    # print(as_set)
    if len(as_set) == 0:
        print("None")
    else:
        main_isp_cnt = 0
        for item in as_set:
            try:
                if int(as_core_map_dict[item][2]) > 3:
                    tye_flag = "ISP"
                    main_isp_cnt += 1
                else:
                    tye_flag = "Not-ISP(Possible)"
                print(item, ":", as_info_dict[item], tye_flag)
            except Exception as e:
                print(item, ":[暂无记录]")
        print("\nISP Count:", main_isp_cnt)
        print("Not-ISP(Possible) Count:", len(as_set) - main_isp_cnt)
        print("- - -  - - - - - - - - - - - - - - - - - - - -")

def judge_isp(as_set, as_core_map_dict):
    """
    根据传入的信息，判断集合中isp的数量和not-isp（Possible）的数量
    :param as_set:
    :param as_core_map_dict:
    :return:
    """
    isp_cnt = 0
    not_isp_cnt = 0
    for item in as_set:
        try:
            if int(as_core_map_dict[item][2]) > 3:
                isp_cnt += 1
            else:
                not_isp_cnt += 1
        except Exception as e:
            not_isp_cnt += 1
    return isp_cnt, not_isp_cnt

if __name__ == "__main__":
    time_start = time.time()  # 记录启动的时间
    # as_list = ["4809", "4134", "132203", "45102", "24429"]
    # as_list = ["4809", "4134", "4837", "9929", "9808"]
    as_list = ["4809", "4134", "701", "6939", "174", "3356", "15169"]
    my_as_rel_file = "../000LocalData/as_relationships/serial-1/20200701.as-rel.txt"
    my_as_info_file = "../000LocalData/as_Gao/asn_info.txt"
    my_country_map_cn = "../000LocalData/as_geo/GeoLite2-Country-Locations-zh-CN.csv"
    my_as_core_map_file = "../000LocalData/as_map/as_core_map_data_new20200701.csv"

    my_as_core_map_dict = gain_as_core_map_dict(my_as_core_map_file)
    # print(my_as_core_map_dict)

    my_as_info_dict = as_info_anlysis(my_as_info_file, my_country_map_cn)
    my_as_rel_dict = as_rel_anlysis(as_list, my_as_rel_file)
    # print(len(my_as_rel_dict["4809"][0]), len(my_as_rel_dict["4809"][1]), len(my_as_rel_dict["4809"][2]))
    print("=>TOP AS 两两对比分析程序(v1)")
    print("=>输入需要分析TOP AS组，程序可以自动生成该组AS两两之间的BGP互联关系对比分析报告")
    print("TOP AS Group:", as_list)
    for i in range(0, len(as_list)):
        for j in range(i+1, len(as_list)):
            print("= = =  = = = = = = = = = = = = = = = = = = = = = = = = = =")
            print("AS%s VS AS%s" % (as_list[i], as_list[j]))
            as_i_peer = len(my_as_rel_dict[as_list[i]][0])
            as_i_customer = len(my_as_rel_dict[as_list[i]][1])
            as_i_provider = len(my_as_rel_dict[as_list[i]][2])

            as_j_peer = len(my_as_rel_dict[as_list[j]][0])
            as_j_customer = len(my_as_rel_dict[as_list[j]][1])
            as_j_provider = len(my_as_rel_dict[as_list[j]][2])

            as_i_set = set(my_as_rel_dict[as_list[i]][0]
                           + my_as_rel_dict[as_list[i]][1]
                           + my_as_rel_dict[as_list[i]][2])

            as_j_set = set(my_as_rel_dict[as_list[j]][0]
                           + my_as_rel_dict[as_list[j]][1]
                           + my_as_rel_dict[as_list[j]][2])

            print("AS%s Info:%s" % (as_list[i], my_as_info_dict[as_list[i]]))
            print("AS%s Info:%s" % (as_list[j], my_as_info_dict[as_list[j]]))
            print("= = =  = = = = = = = = = = = = = = = = = = = = = = = = = =")

            print("\n互联关系数量对比")
            print("All Count:", len(as_i_set), "VS", len(as_j_set))
            print("Peer AS Count:", as_i_peer, "VS", as_j_peer)
            print("Transit Customer AS Count:", as_i_customer, "VS", as_j_customer)
            print("Transit Provider AS Count:", as_i_provider, "VS", as_j_provider)
            # print(as_i_set)
            # print(as_j_set)
            print("Peer AS(Judge ISP, Not-ISP(Possible)):",
                  judge_isp(set(my_as_rel_dict[as_list[i]][0]), my_as_core_map_dict),
                  "VS",
                  judge_isp(set(my_as_rel_dict[as_list[j]][0]), my_as_core_map_dict))

            print("Customer AS(Judge ISP, Not-ISP(Possible)):",
                  judge_isp(set(my_as_rel_dict[as_list[i]][1]), my_as_core_map_dict),
                  "VS",
                  judge_isp(set(my_as_rel_dict[as_list[j]][1]), my_as_core_map_dict))
            print("= = =  = = = = = = = = = = = = = = = = = = = = = = = = = =")

            # print("\n所有互联AS列表集合运算")
            # intersection_as_list = as_i_set.intersection(as_j_set)
            # print("\nAll-Intersection AS List(%s):" % (len(intersection_as_list)))
            # # print(intersection_as_list)
            # print_as_set(intersection_as_list, my_as_info_dict, my_as_core_map_dict)
            #
            # left_difference_as_list = as_i_set.difference(as_j_set)
            # print("\nAll-Left Difference AS List(%s):" % (len(left_difference_as_list)))
            # # print(left_difference_as_list)
            # print_as_set(left_difference_as_list, my_as_info_dict, my_as_core_map_dict)
            #
            # right_difference_as_list = as_j_set.difference(as_i_set)
            # print("\nAll-Right Difference AS List(%s):" % (len(right_difference_as_list)))
            # # print(right_difference_as_list)
            # print_as_set(right_difference_as_list, my_as_info_dict, my_as_core_map_dict)
            #
            # print("\n对等AS列表集合运算")
            # as_i_set_peer = set(my_as_rel_dict[as_list[i]][0])
            # as_j_set_peer = set(my_as_rel_dict[as_list[j]][0])
            #
            # intersection_as_list_peer = as_i_set_peer.intersection(as_j_set_peer)
            # print("\nPeer-Intersection AS List(%s):" % (len(intersection_as_list_peer)))
            # # print(intersection_as_list_peer)
            # print_as_set(intersection_as_list_peer, my_as_info_dict, my_as_core_map_dict)
            #
            # left_difference_as_list_peer = as_i_set_peer.difference(as_j_set_peer)
            # print("\nPeer-Left Difference AS List(%s):" % (len(left_difference_as_list_peer)))
            # print_as_set(left_difference_as_list_peer, my_as_info_dict, my_as_core_map_dict)
            #
            # right_difference_as_list_peer = as_j_set_peer.difference(as_i_set_peer)
            # print("\nPeer-Right Difference AS List(%s):" % (len(right_difference_as_list_peer)))
            # print_as_set(right_difference_as_list_peer, my_as_info_dict, my_as_core_map_dict)
            #
            # print("\n客户AS列表集合运算")
            # as_i_set_customer = set(my_as_rel_dict[as_list[i]][1])
            # as_j_set_customer = set(my_as_rel_dict[as_list[j]][1])
            #
            # intersection_as_list_customer = as_i_set_customer.intersection(as_j_set_customer)
            # print("\nCustomer-Intersection AS List(%s):" % (len(intersection_as_list_customer)))
            # print_as_set(intersection_as_list_customer, my_as_info_dict, my_as_core_map_dict)
            #
            # left_difference_as_list_customer = as_i_set_customer.difference(as_j_set_customer)
            # print("\nCustomer-Left Difference AS List(%s):" % (len(left_difference_as_list_customer)))
            # print_as_set(left_difference_as_list_customer, my_as_info_dict, my_as_core_map_dict)
            #
            # right_difference_as_list_customer = as_j_set_customer.difference(as_i_set_customer)
            # print("\nCustomer-Right Difference AS List(%s):" % (len(right_difference_as_list_customer)))
            # print_as_set(right_difference_as_list_customer, my_as_info_dict, my_as_core_map_dict)
            #
            # print("\n提供商AS列表集合运算")
            # as_i_set_provider = set(my_as_rel_dict[as_list[i]][2])
            # as_j_set_provider = set(my_as_rel_dict[as_list[j]][2])
            #
            # intersection_as_list_provider = as_i_set_provider.intersection(as_j_set_provider)
            # print("\nProvider-Intersection AS List(%s):" % (len(intersection_as_list_provider)))
            # print_as_set(intersection_as_list_provider, my_as_info_dict, my_as_core_map_dict)
            #
            # left_difference_as_list_provider = as_i_set_provider.difference(as_j_set_provider)
            # print("\nProvider-Left Difference AS List(%s):" % (len(left_difference_as_list_provider)))
            # print_as_set(left_difference_as_list_provider, my_as_info_dict, my_as_core_map_dict)
            #
            # right_difference_as_list_provider = as_j_set_provider.difference(as_i_set_provider)
            # print("\nProvider-Right Difference AS List(%s):" % (len(right_difference_as_list_provider)))
            # print_as_set(right_difference_as_list_provider, my_as_info_dict, my_as_core_map_dict)

    time_end = time.time()  # 记录结束的时间
    print("\n=>Scripts Finish, Time Consuming:", (time_end - time_start), "S")


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

from django.db import models, migrations
from django.conf import settings

class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('dashboard', '0043_auto_20150802_1151'),
    ]

    operations = [
        migrations.CreateModel(
            name='AppointmentNote',
            fields=[
                ('note_ptr', models.OneToOneField(parent_link=True, auto_created=True, primary_key=True, serialize=False, to='dashboard.Note')),
                ('date', models.DateField(verbose_name=b'Appointment date')),
                ('time', models.TimeField(verbose_name=b'Appointment time')),
                ('reason_for_visit', models.CharField(max_length=200)),
                ('clinic', models.ManyToManyField(related_name='appointments', to='dashboard.Clinic')),
                ('doctor', models.ManyToManyField(related_name='appointments', to=settings.AUTH_USER_MODEL)),
            ],
            bases=('dashboard.note',),
        ),
    ]


from flask_mail import Message
from flask import current_app, render_template
from WateringSite import mail
from threading import Thread

def send_async_email(app, msg):
    with app.app_context():
        mail.send(msg)

def send_email(subject, sender, recipients, text_body, html_body):
    msg = Message(subject, sender=sender, recipients=recipients)
    msg.body = text_body
    msg.html = html_body
    Thread(target=send_async_email,
           args=(current_app._get_current_object(), msg)).start()

def send_guest_access_email(device, sender, recipient):
    token = device.get_guest_access_token((72*60*60))
    send_email('Invitation to a [Plant Party]',
               sender=("{} at HannahsPlants.party".format(sender.username), current_app.config['ADMINS'][0]),
               recipients=[recipient["Email"]],
               text_body=render_template('email/guest_access.txt',
                                         sender=sender, recipient=recipient, token=token),
               html_body=render_template('email/guest_access.html',
                                         sender=sender, recipient=recipient, token=token))


def Enter(uid,nick_enter,users,msg_list):
    if type(msg_list)==int: # type integer : check  / type list : print
        if uid not in users:
            users[uid]=[nick_enter,"on"]
        if uid in users and users[uid][1]=="off":
            users[uid][1]="on"
            Change(uid,nick_enter,users,msg_list)
    else :
        nick = users[uid][0]
        msg = nick+"님이 들어왔습니다."
        msg_list.append(msg)

def Leave(uid,nick_notUse,users,msg_list):
    if type(msg_list)==int: # type integer : check  / type list : print
        users[uid][1]="off"
    else :
        nick = users[uid][0]
        msg = nick+"님이 나갔습니다."
        msg_list.append(msg)

def Change(uid,nick,users,msg_list):
    if type(msg_list)==int: # type integer : check  / type list : print
        users[uid][0]=nick

def solution(record):
    answer = []
    cmdDict={
        "Enter":Enter,
        "Leave":Leave,
        "Change":Change
    }

    cmd_list=[]
    for cmd in record:
        t = list(cmd.split(" "))
        cmd_list.append(t)

    users={}

    #final nickname simulation
    for cmd in cmd_list:
        param1=cmd[0]#cmd
        param2=cmd[1]#uid
        param3=""#nickname
        if len(cmd)==3 :
            param3=cmd[2]
        param4=users
        cmdDict[param1](param2,param3,param4,1)

    #print msg
    for cmd in cmd_list:
        param1=cmd[0]#cmd
        if param1 == "Change":
            continue
        param2=cmd[1]#uid
        param3=""#nickname
        if len(cmd)==3 :
            param3=cmd[2]
        param4=users
        cmdDict[param1](param2,param3,param4,answer)

    return answer

"""
#note

GOAL : 모든 기록 처리 후 최종적으로 방 개설한 사람이 보게되는 메세지 문자열

#in : "[닉네임]님이 들어왔습니다." : Enter uid nickname
#out : "[닉네임]님이 나갔습니다." : Leave uid nickname
#닉네임변경 : 중복가능 : change uid nickname

"""


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

# flask_restapi import
from tests import BaseTestCase
from flask_restapi.app import APIFlask
from flask_restapi.exceptions import APIError

app = APIFlask(__name__)
app.config['TESTING'] = True

@app.route('/json_view')
def json_view():
    return {'test': True}

@app.route('/string_view')
def string_view():
    """

    :rtype : object
    """
    return 'string'

@app.route('/api_exception')
def api_exception():
    raise APIError(code=1, message='test api exception.')

class APIFlaskTests(BaseTestCase):
    """

    """
    def test_json_view(self):
        with app.test_client() as client:
            response = client.get('/json_view')
            data = response.get_json()
            self.assertEqual(response.status_code, 200)
            self.assertTrue(data['data']['test'])

    def test_string_view(self):
        with app.test_client() as client:
            response = client.get('/string_view')
            data = response.get_json()
            self.assertEqual(response.status_code, 200)
            self.assertEqual(data['data'], 'string')

    def test_api_exception(self):
        with app.test_client() as client:
            response = client.get('/api_exception')
            data = response.get_json()
            self.assertEqual(response.status_code, 200)
            self.assertEqual(data['status']['code'], 1)


"""
다음은 233 이란 10진수를 2진수로 변환하는 과정을 나타낸 그림이다.

위 그림을 참조하여 라이브러리를 사용하지 말고 10진수를 n진수로 변환하는 프로그램을 작성하시오.. (단, n의 범위는 2 <= n <= 16)

예)
2진수로 변환 : 233,10 --> 11101001,2
8진수로 변환 : 233,10 --> 351,8
16진수로 변환 : 233,10 --> E9,16
"""

X, Y = map(int, input('X(10진수) 와 Y(변환할 진수) 입력: ').split())
stack = []
change_list = '0123456789ABCDEF'
while X // Y:
    stack.append(change_list[X%Y])
    X = X // Y
stack.append(change_list[X])
print(''.join(map(str, reversed(stack))), Y)

"""
# 다른 방법
# 재귀함수와 divmod 함수 사용
# 더 좋은 방법인지는 모르겠다.

def convert(n, base):
    T = "0123456789ABCDEF"
    q, r = divmod(n, base)
    if q == 0:
        return T[r]
    else:
        return convert(q, base) + T[r]

print(convert(233, 2))
print(convert(233, 8))
print(convert(233, 16))
"""

10진수를 n진수로 변환하기.py


import unittest
from unittest import mock

from ...management.email_templates import EmailTemplates

class TestClients(unittest.TestCase):
    def test_init_with_optionals(self):
        t = EmailTemplates(
            domain="domain", token="jwttoken", telemetry=False, timeout=(10, 2)
        )
        self.assertEqual(t.client.options.timeout, (10, 2))
        telemetry_header = t.client.base_headers.get("Auth0-Client", None)
        self.assertEqual(telemetry_header, None)

    @mock.patch("auth0.management.email_templates.RestClient")
    def test_create(self, mock_rc):
        mock_instance = mock_rc.return_value

        c = EmailTemplates(domain="domain", token="jwttoken")
        c.create({"a": "b", "c": "d"})

        mock_instance.post.assert_called_with(
            "https://domain/api/v2/email-templates", data={"a": "b", "c": "d"}
        )

    @mock.patch("auth0.management.email_templates.RestClient")
    def test_get(self, mock_rc):
        mock_instance = mock_rc.return_value

        c = EmailTemplates(domain="domain", token="jwttoken")
        c.get("this-template-name")

        mock_instance.get.assert_called_with(
            "https://domain/api/v2/email-templates/this-template-name"
        )

    @mock.patch("auth0.management.email_templates.RestClient")
    def test_update(self, mock_rc):
        mock_instance = mock_rc.return_value

        c = EmailTemplates(domain="domain", token="jwttoken")
        c.update("this-template-name", {"a": "b", "c": "d"})

        mock_instance.patch.assert_called_with(
            "https://domain/api/v2/email-templates/this-template-name",
            data={"a": "b", "c": "d"},
        )


# -*- coding: utf-8 -*-
"""
Created on Tue Mar 16 13:33:37 2021

Temoin tests

@author: Admin
"""

import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import preprocessing
from datetime import timedelta

#%% IMPORT personal mods
os.chdir('MODS')
from data_merge import merge_dfs
from dope_reg import dope_read
import dataread as d_
os.chdir('..')

plt.close('all')

def read_merge(files):
    """
    Merge all the data in the list files

    At a later point this could be made into the same function as terrain
    """

    print('The following files will be merged:')
    print(files)

    dfs = []
    for file in files:
        df = pd.read_csv(file,sep = '\t',encoding = 'utf-16')    #read each df in directory df
        df = df[df['datatype'] == 'Locomotion']                         #store only locomotion information

        #Error VPCore2
        #conc,subs = df['Conc'].iloc[0],df['Sub'].iloc[0]

        #sort values sn = , pn = ,location = E01-16 etcc., aname = A01-04,B01-04 etc.
        df = df.sort_values(by = ['sn','pn','location','aname'])
        df = df.reset_index(drop = True)

        #treat time variable - this gets the days and months the wrong way round
        df['time'] = pd.to_datetime(df['stdate'] + " " + df['sttime'], format = '%d/%m/%Y %H:%M:%S')

        maxrows = len(df)//48
        print('Before adjustment: total rows{}'.format(len(df)))
        df = df.iloc[:maxrows*48]
        print('After adjustment: total rows{}'.format(len(df)))
        dfs.append(df)

    return merge_dfs(dfs)

def preproc(df):
    """
    Preprocessing of the df to get it in correct form
    Return dictionary of dfs for each species - only with distances
    """

    #column for specie
    mapping = lambda a : {'E': 'Erpobdella','G':'Gammarus','R':'Radix'}[a[0]]
    df['specie'] = df['location'].map(mapping)

    #moi le column 'animal' n'a que des NaNs
    good_cols = ['time','location','stdate','specie','inact','inadur','inadist','smlct','smldur','smldist','larct','lardur','lardist','emptyct','emptydur']
    df = df[good_cols]

    #create animal 1-16 for all species
    df['animal'] = df['location'].str[1:].astype(int)

    df['abtime'] = df['time'].astype('int64')//1e9 #convert nano
    df['abtime'] = df['abtime'] - df['abtime'][0]

    #total distance inadist is only zeros?
    df['dist'] = df['inadist'] + df['smldist'] + df['lardist']

    #seperate into three dfs
    dfs = {}
    for spec in specie:

        temp = df[df['specie'] == specie[spec]]
        timestamps = temp['time'].unique()
        animals = temp['animal'].unique()
        df_dist = 0
        df_dist = pd.DataFrame(index = timestamps,columns = animals)

        for i in animals:
            temp_df = temp[temp['animal'] == i]
            df_dist[i] = temp_df['dist'].values

        dfs.update({spec:df_dist})

    return dfs

def remove_dead_test(df):

    """
    - Taken idea from main but simpler for smaller files
    - If percentage of 0s is high, remove individual from test

    """

    dead = []
    for col in df.columns:
        if df[col].value_counts()[0]/df[col].shape[0] > 0.98: dead.append(col)
    print('Remove : ',dead)
    return dead

def rolling_mean(df,timestep):

    #convert mins to number of 20 second intervals
    timestep = (timestep * 60)//20
    return df.rolling(timestep).mean().dropna()

def single_plot(series,title = ''):
    fig = plt.figure(figsize = (13,8))
    axe = fig.add_axes([0.1,0.1,0.8,0.8])
    axe.plot(series.index,series)
    axe.set_title(title)
    axe.tick_params(axis = 'x', rotation = 90)

    return fig,axe

def plot_16(df,date_range,title = None):

    """
    Plot a 16 square subplots
    """
    fig,axe = plt.subplots(4,4,sharex = True,sharey = True,figsize = (20,12))
    for i in df.columns:
        axe[(i-1)//4,(i-1)%4].plot(df.index,df[i],color = colors[2])
        axe[(i-1)//4,(i-1)%4].axvspan(date_range[0], date_range[1], alpha=0.7, color='orange')
        axe[(i-1)//4,(i-1)%4].axvline(date_range[0] + pd.Timedelta(seconds = 30), color = 'red')
        axe[(i-1)//4,(i-1)%4].tick_params(axis='x', rotation=90)

    if title:
        plt.suptitle(title)

def dataplot_mark_dopage(axe,date_range):
    """
    Shade the doping period and mark the doping moment thoroughly
    """
    #shade over doping period - item extracts value from pandas series
    axe.axvspan(date_range[0], date_range[1], alpha=0.7, color='orange')
    axe.axvline(date_range[0] + pd.Timedelta(seconds = 30), color = 'red')

#take strategy from weekly results

if __name__ == '__main__':

    # parametres a modifier
    Tox = 765
    species = 'G'
    etude_ = 1
    specie = {'E': 'Erpobdella','G':'Gammarus','R':'Radix'}

    #find directory
    os.chdir(r'D:\VP\Viewpoint_data\Temoins\Tem_{}_0{}'.format(Tox,etude_))
    files = [file for file in os.listdir() if os.path.isfile(file)]

    #read files and wrangle
    df = d_.read_merge(files)
    dfs = d_.preproc(df)

    #select gammarus df
    df = dfs[species]
    df = df.drop(columns = d_.remove_dead(df))

    temoin_df = dope_read(reg = 'temoin_reg')

    temoin = temoin_df[temoin_df['TxM'] ==  Tox].iloc[etude_ - 1]
    case = 'Tox:{},  Methode: {}, Volume: {}, Date: {} {}-{}'.format(
        Tox,
        temoin['Molecule'],
        temoin['Concentration'],
        temoin['Start'].strftime("%m-%d"),
        temoin['Start'].strftime("%H:%M:%S"),
        temoin['End'].strftime("%H:%M:%S"))

    df_pre = df[df.index < temoin['Start']]
    df_post = df[(df.index > temoin['End']) & (df.index < (temoin['End']+pd.Timedelta(minutes = 30)))]

    t_mins = 5
    df_mean = d_.rolling_mean(df,t_mins)

    #plot for 1 hour scale
    if df.index[-1] > (temoin['End'] + pd.Timedelta(hours = 1)):
        d_.plot_16(df_mean[df_mean.index < (temoin['End']+pd.Timedelta(hours = 1))],[temoin['Start'],temoin['End']],title = 'Distance/20s -  {}'.format(case))
    else:
        d_.plot_16(df_mean,[temoin['Start'],temoin['End']],title = 'Distance/20s -  {}'.format(case))

    #plot for 15 minutes
    if temoin['Molecule'] != np.nan:
        short = df_mean[(df_mean.index > (temoin['Start']-pd.Timedelta(minutes = 6))) & ((df_mean.index < temoin['Start']+pd.Timedelta(minutes = 17)))]
        d_.plot_16(short,[temoin['Start'],temoin['End']],title = case)

    #plot of means axis = 1
    means = df_mean.median(axis = 1)
    fig,axe = d_.single_plot(means,title = 'Mean -  {}'.format(case))
    d_.dataplot_mark_dopage(axe,[temoin['Start'],temoin['End']])


import ROOT
import sys
from MuonPerformance.MuonAnalyser.histoHelper import *

dicStrSig = {
  "TDR":  "/xrootd/store/user/quark2930/muon_upgrade/TDRSpring2017/tdr_171023_2_zmm_200.root",
  "Run2": "/xrootd/store/user/quark2930/muon_upgrade/Run2/run2_171026_1_zmm.root",
  "RelVal": "/xrootd/store/user/quark2930/muon_upgrade/relval_zmm_0_171025_1.root",
}

dicStrTTH = {
  "TDR":  "/xrootd/store/user/quark2930/muon_upgrade/TDRSpring2017/tdr_171019_1_ttbar_200.root",
  "Run2": "/xrootd/store/user/quark2930/muon_upgrade/Run2/run2_171019_1_ttH.root",
}

dicStrBkg = {
  "TDR":  "/xrootd/store/user/quark2930/muon_upgrade/TDRSpring2017/tdr_171023_2_qcd_200.root",
  "Run2": "/xrootd/store/user/quark2930/muon_upgrade/Run2/run2_171026_1_qcd.root",
  "RelVal": "/xrootd/store/user/quark2930/muon_upgrade/relval_qcd_0_171025_1.root",
}

dicVar = {
  "run2Trk": "muon_TrkIsolation03",
  "run2PF":  "muon_pfNewIsoPt02_ch / muon.Pt()",
}

strIdx = sys.argv[ 1 ]
strSigBkg = sys.argv[ 2 ]
strPrefix = "" if len(sys.argv) < 4 else sys.argv[ 3 ]

strVar = dicVar[ strIdx.split("_")[ 0 ] ]
fCut = int(strIdx.split("_")[ 1 ]) * 0.005

fBinMax = 50.0
nNumBin = 50000

strCut = "muon.Pt() > 15 && 0.0 <= abs(muon.Eta()) && abs(muon.Eta()) < 2.4 && muon_isLoose"
strExtraCut = ""

if "PF" in strIdx and len(sys.argv) < 4:
  strExtraCut = " && muon_pfNewIsoPt04_nh / muon.Pt() < %0.8f"%(fCut)

strSig = dicStrSig[ "Run2" ]
strBkg = dicStrBkg[ "Run2" ]

hRes = ""

strTreename = "PatMuonAnalyser/reco" if "RelVal" not in strIdx else "MuonAnalyser/reco"

if strSigBkg == "sig" or strSigBkg == "Sig":
  #print strIdx + " (" + strSigBkg + "; " + strVar + ") : " + strCut + " && muon_signal" + strExtraCut
  hRes = makeTH1(strSig, strTreename, "title", [nNumBin, 0, fBinMax], strVar, strCut + " && muon_signal" + strExtraCut)
  #print "Sig (%s%s) : %lf"%(strIdx, strPrefix, hRes.GetIntegral())
else:
  #print strIdx + " (" + strSigBkg + "; " + strVar + ") : " + strCut + strExtraCut
  hRes = makeTH1(strBkg, strTreename, "title", [nNumBin, 0, fBinMax], strVar, strCut + strExtraCut)

hRes.SaveAs("makeroc_%s_%s%s.root"%(strIdx, strSigBkg, strPrefix))


import os
import csv
#importing modules to read file
budget_data=os.path.join("Resources", "budget_data.csv")
#initialize variables that will make sense later (just kidding theyre self-explanatory)
month_count=0
profit_losses=0
previous_month=0
month_diffs=[]
month_list=[]
#open file and store header
with open(budget_data) as csvfile:
    csvreader=csv.reader(csvfile,delimiter=',')
    csv_header=next(csvfile)

    #count months, calculate total profit/losses, populate list of month to month p/l and make corresponding list of months
    for row in csvreader:
        month_count+=1
        profit_losses+=int(row[1])
        if month_count>1:
            month_diffs.append(int(row[1])-previous_month)
            month_list.append(row[0])
        previous_month=int(row[1])
#analyze month to month p/l data
avg_diff=sum(month_diffs)/len(month_diffs)
max_loss=min(month_diffs)
theworstmonth=(month_list[month_diffs.index(max_loss)])
max_gain=max(month_diffs)
thebestmonth=(month_list[month_diffs.index(max_gain)])
#display findings
print(f"Financial Analsis \nTotal Months={month_count} \nTotal={profit_losses} \nAverage Change={avg_diff:.2f} \nGreatest Increase in Profits=({thebestmonth}){max_gain}\nGreatest Decrease in Profits=({theworstmonth}){max_loss}")
#define function for writing new text file
def write_to_file(filename, lines):
     with open(filename,"w") as text:
        for line in lines:
            text.write(f"{line}\n")
#call function with analysis output
write_to_file("analysis/analysis.txt",["Financial Analysis","--------------------",
            "Total Months=86","Total=22,564,198","Average Change=-8,311.11","Greatest Increase in Profits=(Aug-16)1,862,002)",
            "Greatest Decrease in Profits=(Feb-14)-1,825,558)"])


# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

from bkr.inttest.server.selenium import WebDriverTestCase
from bkr.inttest.server.webdriver_utils import login, click_menu_item, is_text_present
from bkr.inttest import get_server_base

class AddGroup(WebDriverTestCase):
    def setUp(self):
        self.browser = self.get_browser()
        self.group_name = 'd_group_d'
        self.group_display_name = 'd_group_d'

    def test_add_group(self):
        b = self.browser
        b.get(get_server_base())
        login(b)
        click_menu_item(b, 'Admin', 'Groups')
        b.find_element_by_link_text('Add').click()
        b.find_element_by_name('display_name').send_keys(self.group_display_name)
        b.find_element_by_name('group_name').send_keys(self.group_name)
        b.find_element_by_id('Group').submit()
        self.assertEquals(b.find_element_by_class_name('flash').text, 'OK')
        self.assert_(is_text_present(b, self.group_display_name))