#-*- coding:utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2011 OpenERP SA (<http://openerp.com>). All Rights Reserved
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
###################################################
from datetime import date
from num2words import num2words
from odoo import models, fields, api
from datetime import date
from datetime import date, timedelta
import datetime
import time

class SampleDevelopmentReport(models.AbstractModel):
	_name = 'report.over_due_report.module_report'

	@api.model
	def render_html(self,docids, data=None):
		report_obj = self.env['report']
		report = report_obj._get_report_from_name('over_due_report.module_report')
		active_wizard = self.env['overdue.reportpdf'].search([])
		emp_list = []
		for x in active_wizard:
			emp_list.append(x.id)
		emp_list = emp_list
		emp_list_max = max(emp_list)

		partner_list = []
		for x in active_wizard[-1].partner_ids:
			partner_list.append(x.id)
		record_wizard = self.env['overdue.reportpdf'].search([('id','=',emp_list_max)])

		record_wizard_del = self.env['overdue.reportpdf'].search([('id', '!=', emp_list_max)])
		record_wizard_del.unlink()

		date = record_wizard.date
		filters = record_wizard.filters
		ext_type_of_cust = record_wizard.ext_type_of_cust
		partner_ids = record_wizard.partner_ids
		mt_ids = record_wizard.mt_ids
		wt_ids = record_wizard.wt_ids
		ct_ids = record_wizard.ct_ids
		pos_ids = record_wizard.pos_ids
		ec_ids = record_wizard.ec_ids

		if filters == 'all':
			fill_type = 'All'
			if ext_type_of_cust == 'MT':
				customer = self.env['res.partner'].search([('ext_type_of_cust','=','MT'),('customer','=',True)])
			if ext_type_of_cust == 'WT':
				customer = self.env['res.partner'].search([('ext_type_of_cust','=','WT'),('customer','=',True)])
			if ext_type_of_cust == 'CT':
				customer = self.env['res.partner'].search([('ext_type_of_cust','=','CT'),('customer','=',True)])
			if ext_type_of_cust == 'partner':
				customer = self.env['res.partner'].search([('ext_type_of_cust','=','partner'),('customer','=',True)])
			if ext_type_of_cust == 'POS':
				customer = self.env['res.partner'].search([('ext_type_of_cust','=','POS'),('customer','=',True)])
			if ext_type_of_cust == 'EC':
				customer = self.env['res.partner'].search([('ext_type_of_cust','=','EC'),('customer','=',True)])

		if filters == 'specfic':
			fill_type = 'Specific'
			if ext_type_of_cust == 'MT':
				customer = []
				for x in mt_ids:
					customer.append(x)
			if ext_type_of_cust == 'WT':
				customer = []
				for x in wt_ids:
					customer.append(x)
			if ext_type_of_cust == 'CT':
				customer = []
				for x in ct_ids:
					customer.append(x)
			if ext_type_of_cust == 'partner':
				customer = []
				for x in partner_ids:
					customer.append(x)
			if ext_type_of_cust == 'POS':
				customer = []
				for x in pos_ids:
					customer.append(x)
			if ext_type_of_cust == 'EC':
				customer = []
				for x in ec_ids:
					customer.append(x)

		dated = []
		for x in customer:
			inv = []
			# if x.property_payment_term_id:
			payment_day = x.property_payment_term_id.line_ids.days
			start_date = datetime.datetime.strptime(date,"%Y-%m-%d")
			new_date = start_date - timedelta(days=payment_day)
			month = str(new_date)
			monthed = str(month[:7])
			invoices = self.env['account.invoice'].search([('type','=','out_invoice'),('state','=','open'),('partner_id.id','=',x.id)])
			if invoices:
				for y in invoices:
					if y.date_invoice:
						if date > str(y.date_invoice):
							inv.append({
								'number':y.number,
								'date':y.date_invoice,
								'source':y.origin,
								'total':y.amount_total,
								'remain':y.residual,
								})

			dated.append({
				'name':x.name,
				'inv':inv,
				})

		if ext_type_of_cust == 'MT':
			cust_type = 'Modern Trade'
		if ext_type_of_cust == 'WT':
			cust_type = 'Traditional Trade'
		if ext_type_of_cust == 'CT':
			cust_type = 'Corporate Trade'
		if ext_type_of_cust == 'partner':
			cust_type = 'Partner'
		if ext_type_of_cust == 'POS':
			cust_type = 'POS'
		if ext_type_of_cust == 'EC':
			cust_type = 'E-Commerce'

		docargs = {
			'doc_ids': docids,
			'doc_model': 'res.partner',
			'dated': dated,
			'inv': inv ,
			'date': date,
			'fill_type': fill_type,
			'cust_type': cust_type,
		}

		return report_obj.render('over_due_report.module_report', docargs)


import uuid
import datetime
import json
import hashlib

from django.conf import settings
from django.core.urlresolvers import reverse

from google.appengine.api import taskqueue
from google.appengine.api import logservice

import bq

def _trim(obj, size=65536):
    s = str(obj)
    if len(s) < size:
        s = s[:size] + '...'
    return s

def _log_level_to_name(level):
    mapping = {
        0: 'DEBUG',
        1: 'INFO',
        2: 'WARNING',
        3: 'ERROR',
        4: 'CRITICAL',
    }
    return mapping.get(level)

class BigQueryLogger(object):

    timestamp_format = '%Y-%m-%d %H:%M:%S.%f'
    fields = [
        {'name': 'uuid', 'type': 'STRING'},
        {'name': 'timestamp', 'type': 'TIMESTAMP'},
        {'name': 'path', 'type': 'STRING'},
        {'name': 'post_data', 'type': 'STRING'},
        {'name': 'get_data', 'type': 'STRING'},
    ]

    def __init__(self):
        self.service = None

    def log(self, path, get, post):
        payload = {
            'uuid': str(uuid.uuid1()),
            'timestamp': datetime.datetime.utcnow().strftime(self.timestamp_format),
            'path': path,
            'get_data': _trim(get),
            'post_data': _trim(post)
        }
        taskqueue.add(
            queue_name='api-logger',
            url=reverse('log-to-bigquery'),
            params={'payload': json.dumps(payload), 'uuid': payload['uuid']})

    def send_to_bq(self, uuid, json_payload):
        if not self.service:
            self.service = bq.BigQueryStreamingInsert(
                project=settings.BIGQUERY_LOGGER_PROJECT,
                dataset=settings.BIGQUERY_LOGGER_DATASET,
                table=settings.BIGQUERY_LOGGER_TABLE,
                fields=self.fields,
                credentials=(settings.GOOGLE_API_SERVICE_ACCOUNT, settings.GOOGLE_API_PRIVATE_KEY)
            )
        self.service.insert_one(uuid, json.loads(json_payload))

class RequestLogToBigQuery(object):
    # ['app_id', 'api_mcycles', 'combined', 'cost', 'end_time', 'host', 'instance_key',
    # 'ip', 'latency', 'mcycles', 'method', 'nickname', 'pending_time', 'referrer',
    # 'replica_index', 'request_id', 'resource', 'response_size', 'start_time', 'status',
    # 'url_map_entry', 'user_agent', 'version_id', 'was_loading_request']
    fields = [
        {"name": "request_id", "type": "STRING"},
        {"name": "resource", "type": "STRING"},
        {"name": "app_id", "type": "STRING"},
        {"name": "version_id", "type": "STRING"},
        {"name": "api_mcycles", "type": "INTEGER"},
        {"name": "combined", "type": "STRING"},
        {"name": "cost", "type": "FLOAT"},
        {"name": "start_time", "type": "FLOAT"},
        {"name": "end_time", "type": "FLOAT"},
        {"name": "host", "type": "STRING"},
        {"name": "instance_key", "type": "STRING"},
        {"name": "ip", "type": "STRING"},
        {"name": "latency", "type": "FLOAT"},
        {"name": "mcycles", "type": "INTEGER"},
        {"name": "method", "type": "STRING"},
        {"name": "nickname", "type": "STRING"},
        {"name": "pending_time", "type": "FLOAT"},
        {"name": "referrer", "type": "STRING"},
        {"name": "replica_index", "type": "INTEGER"},
        {"name": "response_size", "type": "INTEGER"},
        {"name": "status", "type": "INTEGER"},
        {"name": "url_map_entry", "type": "STRING"},
        {"name": "user_agent", "type": "STRING"},
        {"name": "was_loading_request", "type": "BOOLEAN"},
    ]

    def __init__(self):
        self.service = None

    def __call__(self, request_log):
        payload = dict(
            (f, getattr(request_log, f, None))
            for f in [field['name'] for field in self.fields])
        if not self.service:
            self.service = bq.BigQueryStreamingInsert(
                project=settings.BIGQUERY_REQUESTLOGS_PROJECT,
                dataset=settings.BIGQUERY_REQUESTLOGS_DATASET,
                table=settings.BIGQUERY_REQUESTLOGS_TABLE,
                fields=self.fields,
                credentials=(settings.GOOGLE_API_SERVICE_ACCOUNT, settings.GOOGLE_API_PRIVATE_KEY)
            )
        self.service.insert_one(request_log.request_id, payload)

class AppLogToBigQuery(object):
    request_fields = [
        {"name": "request_id", "type": "STRING"},
        {"name": "resource", "type": "STRING"},
        {"name": "app_id", "type": "STRING"},
        {"name": "version_id", "type": "STRING"},
        {"name": "combined", "type": "STRING"},
        {"name": "start_time", "type": "FLOAT"},
        {"name": "end_time", "type": "FLOAT"},
        {"name": "instance_key", "type": "STRING"},
    ]
    app_fields = [
        {"name": "time", "type": "FLOAT"},
        {"name": "level", "type": "INTEGER"},
        {"name": "message", "type": "STRING"},
    ]
    extra_fields = [
        {"name": "level_name", "type": "STRING"},
        {"name": "app_log_id", "type": "STRING"},
    ]

    def __init__(self):
        self.service = None

    def __call__(self, request_log, app_log):

        app_log_id = hashlib.sha1(
            repr(request_log.request_id) +
            repr(app_log.time) +
            repr(app_log.level) +
            repr(app_log.message)
        ).hexdigest()

        request_payload = [
            (f, getattr(request_log, f, None))
            for f in [field['name'] for field in self.request_fields]]
        app_payload = [
            (f, getattr(app_log, f, None))
            for f in [field['name'] for field in self.app_fields]]
        extra_payload = [
            ('level_name', _log_level_to_name(app_log.level)),
            ('app_log_id', app_log_id)]

        fields = self.request_fields + self.app_fields + self.extra_fields
        payload = dict(request_payload + app_payload + extra_payload)

        if not self.service:
            self.service = bq.BigQueryStreamingInsert(
                project=settings.BIGQUERY_APPLOGS_PROJECT,
                dataset=settings.BIGQUERY_APPLOGS_DATASET,
                table=settings.BIGQUERY_APPLOGS_TABLE,
                fields=fields,
                credentials=(settings.GOOGLE_API_SERVICE_ACCOUNT, settings.GOOGLE_API_PRIVATE_KEY)
            )
        self.service.insert_one(app_log_id, payload)

def save_logs_to_bq(start, end):
    for request_log in logservice.fetch(
        start_time=start,
        end_time=end,
        minimum_log_level=logservice.LOG_LEVEL_DEBUG,
        include_incomplete=False,
        include_app_logs=True):
        # ignore incomplete requests or requests for taskqueue
        if not request_log.finished or request_log.task_queue_name:
            continue
        request_log_to_bq(request_log)
        for app_log in request_log.app_logs:
            app_log_to_bq(request_log, app_log)

bqlogger = BigQueryLogger()
request_log_to_bq = RequestLogToBigQuery()
app_log_to_bq = AppLogToBigQuery()


# from django.shortcuts import render
from django.http import JsonResponse
from landing.models import *
from django.core.mail import send_mail
from django.template import loader
from urllib.parse import unquote

# Create your views here.

# почты манагеров
emails = {
    'fibo': ['6427055@btour.ru', '7377101@gmail.com', 'micemgr@btour.ru'],
    'global-fitness': ['6427055@btour.ru', '7377101@gmail.com', 'micemgr@btour.ru'],
    'vialek': ['6427055@btour.ru', '7377101@gmail.com'],
    'union-consult': ['6427055@btour.ru', '7377101@gmail.com'],
    'Сборы': ['6427055@btour.ru', '7377101@gmail.com', 'micemgr@btour.ru'],
    'test': ['aldouswhite@gmail.com'],
}

def send_hotel_order(request):

    # d = {
    #     'status': 'ok',
    #     'landing': request.GET.get('landing')
    # }
    # return JsonResponse(d)

    order = HotelOrder()

    hotel = request.GET.get('hotel', '')
    hotel = unquote(hotel)
    order.hotel = hotel

    order.arrival = request.GET.get('arrival', '')
    order.departure = request.GET.get('departure', '')
    order.guests = request.GET.get('guests', '')
    order.names = request.GET.get('names', '')
    order.breakfast = request.GET.get('breakfast', '')

    try:
        if request.GET.get('want_single', ''):
            order.single = int(request.GET.get('single', 0))
        if request.GET.get('want_double', ''):
            order.double = int(request.GET.get('double', 0))
    except ValueError:
        order.single = order.double = 0

    order.landing = request.GET.get('landing', '')
    order.phone = request.GET.get('phone', '')
    order.email = request.GET.get('email', '')

    order.save()

    # загружаем шаблон для письма
    template = loader.render_to_string(
        'landing_hotel_order_email.html',
        context={'order': order},
    )

    send_mail(
        'Заказ отеля с лендинга' + order.landing,
        '',  # текстовая версия
        'landing@btour.ru',
        ['to@example.com'],
        fail_silently=False,
        html_message=template,
    )

    d = {
        'status': 'ok',
    }
    return JsonResponse(d)

def send_service_order(request):
    order = ServiceOrder()

    order.date_time = request.GET.get('date_time', '')
    order.comment = request.GET.get('comment', '')
    order.visa = request.GET.get('visa', '')
    order.vip = request.GET.get('vip', '')
    order.transfer = request.GET.get('transfer', '')
    order.excursion = request.GET.get('excursion', '')
    order.tickets = request.GET.get('tickets', '')

    order.landing = request.GET.get('landing', '')
    order.phone = request.GET.get('phone', '')
    order.email = request.GET.get('email', '')

    order.save()

    # загружаем шаблон для письма
    template = loader.render_to_string(
        'landing_service_order_email.html',
        context={'order': order},
    )

    send_mail(
        'Заказ услуги с лендинга' + order.landing,
        '',  # текстовая версия
        'landing@btour.ru',
        ['to@example.com'],
        fail_silently=False,
        html_message=template,
    )

    d = {
        'status': 'ok'
    }

    return JsonResponse(d)

def send_promo_request(request):
    order = PromoRequest()

    order.contact = request.GET.get('email', '')

    order.save()

    # загружаем шаблон для письма
    template = loader.render_to_string(
        'landing_fibo_promo_email.html',
        context={'order': order},
    )

    send_mail(
        'Запрос промокода FIBO',
        '',  # текстовая версия
        'landing@btour.ru',
        ['to@example.com'],
        fail_silently=False,
        html_message=template,
    )

    d = {
        'status': 'ok'
    }

    return JsonResponse(d)


import numpy as np
from datetime import datetime
from lxml import etree
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import datetime
from pymongo import MongoClient
from lxml import etree
from pytz import timezone
import re
import requests

def getPrice(html_tree, xpath):
    price = html_tree.xpath(xpath)[0].text.strip()
    price = price.replace('$', '').replace(',', '')
    price = int(price)
    return price

def downloadLink(link):

    #defines header information
    headers = {
        "Connetion" : "close",
        "User-Agent" : "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)"
    }
    print(link)

    # using timeouts and requests library
    #  fixes timeout problems
    html = requests.get(link, timeout = 500, headers = headers)
    html = html.content

    return html

client2 = MongoClient('localhost', 27017)
db2 = getattr(client2, 'mmr_dumps')
collection_mmr_dumps = getattr(db2, 'dumps1')

list_delta = []
list_markup = []

#for listing in collection_mmr_dumps.find({'mid_info.make': 'ACURA'}):
for listing in collection_mmr_dumps.find():
    #print('anotha 1')
    #print(listing['link'])
    html_source = listing['html']

    html_tree = etree.HTML(html_source)

    if not ('Sorry, no pricing available.' in html_source or 'is invalid' in html_source):

        #print(html_source)

        #time.sleep(10)

        name = str(html_tree.xpath('string(//*[@id="mmrFavorite"])')).strip()

        auction_above = getPrice(html_tree, """//*[@id="content"]/table[3]/tbody/tr[2]/td[1]""")
        auction_average = getPrice(html_tree, """//*[@id="content"]/table[3]/tbody/tr[2]/td[2]""")
        auction_below = getPrice(html_tree, """//*[@id="content"]/table[3]/tbody/tr[2]/td[3]""")

        est_retaill_above = getPrice(html_tree, """//*[@id="content"]/table[3]/tbody/tr[3]/td[1]""")
        est_retaill_average = getPrice(html_tree, """//*[@id="content"]/table[3]/tbody/tr[3]/td[2]""")
        est_retaill_below = getPrice(html_tree, """//*[@id="content"]/table[3]/tbody/tr[3]/td[3]""")

        odometer_average = html_tree.xpath("""//*[@id="ymms_lookup"]/div[2]/div/div/fieldset/div[5]/input[1]""")[0].get(
            'value')
        region = html_tree.xpath("""//*[@id="select"]/optgroup[1]/option[1]""")[0].get('value')

        curr_date_range = html_tree.xpath("""//*[@id="content"]/h3[2]""")
        curr_date_range = curr_date_range[0].text
        # print(curr_date_range)
        curr_date_range = curr_date_range.split()
        curr_date_range.pop(0)

        curr_year = curr_date_range.pop()
        from_date_month = curr_date_range.pop(0)
        from_date_day = curr_date_range.pop(0)
        from_dt_str = from_date_month + ' ' + from_date_day + ' ' + curr_year

        from_dt = datetime.datetime.strptime(from_dt_str, '%b %d %Y')

        curr_date_range.pop(0)

        to_date_month = curr_date_range.pop(0)
        to_date_day = curr_date_range.pop(0)

        to_dt_str = to_date_month + ' ' + to_date_day + ' ' + curr_year
        to_dt = datetime.datetime.strptime(from_dt_str, '%b %d %Y')

        #print('curr year', curr_year)
        #print('from month day', from_date_month, from_date_day)
        #print('to month day', to_date_month, to_date_day)

        #print(name)
        #print('auction prices', auction_above, auction_average, auction_below)
        #print('retail prices', est_retaill_above, est_retaill_average, est_retaill_below)
        #print('avg odometer', odometer_average)
        #print('region', region)
        #print('from dt', from_dt)
        #print('to dt', to_dt)

        if auction_above != 0 and auction_average != 0 and auction_below != 0 and est_retaill_above != 0 and est_retaill_average != 0 and est_retaill_below != 0:
            above_delta = est_retaill_above - auction_above
            avg_delta = est_retaill_average - auction_average
            below_delta = est_retaill_below - auction_below

            general_delta_avg = (avg_delta + below_delta + above_delta) / 3

            if (general_delta_avg > 0):
                list_delta.append(general_delta_avg)

            print(listing['mid_info']['year'], listing['mid_info']['make'], listing['mid_info']['model'], listing['mid_info']['style'])
            #print('above delta', above_delta)
            #print('avg delta', avg_delta)
            #print('below delta', below_delta)
            print('delta avg', general_delta_avg)

            above_markup = (above_delta / auction_above) * 100
            avg_markup = (avg_delta / auction_average) * 100
            below_markup = (below_delta / auction_below) * 100

            general_markup = (above_markup + avg_markup + below_markup) / 3

            if (general_markup > 0):
                list_markup.append(general_markup)

            #print('above markup percentage', above_markup)
            #print('avg markup percentage', avg_markup)
            #print('below markup percentage', below_markup)
            print('markup avg', general_markup)
            print('*' * 50)

a = np.array(list_delta)
b = np.array(list_markup)

print('delta mean', a.mean())
print('delta min', a.min())
print('delta max', a.max())

print('markup mean', b.mean())
print('markup min', b.min())
print('markup max', b.max())


import os
from pathlib import Path
import seaborn as sns
import scipy as sp
import numpy as np
import pandas as pd
from .figureCommon import subplotLabel, getSetup
from ..imports import channels, importReceptors
from ..flow import bead_regression
from ..FCimports import combineWells, compMatrix, applyMatrix, import_gates, apply_gates, importF
from FlowCytometryTools import FCMeasurement, ThresholdGate, PolyGate
from matplotlib import pyplot as plt
from matplotlib import cm

path_here = os.path.dirname(os.path.dirname(__file__))
plt.rcParams['svg.fonttype'] = 'none'

def makeFigure():
    """Get a list of the axis objects and create a figure"""

    ax, f = getSetup((10, 8), (3, 4), multz={8: 1, 10: 1})
    subplotLabel(ax)
    Tcell_pathname = path_here + "/data/flow/2019-11-08 monomer IL-2 Fc signaling/CD4 T cells - IL2-060 mono, IL2-060 dimeric"
    NK_CD8_pathname = path_here + "/data/flow/2019-11-08 monomer IL-2 Fc signaling/NK CD8 T cells - IL2-060 mono, IL2-060 dimeric"

    Tcell_sample, _ = importF2(Tcell_pathname, "A")
    NK_CD8_sample, _ = importF2(NK_CD8_pathname, "A")

    Tcell_sample = combineWells(Tcell_sample).subsample(0.2)
    NK_CD8_sample = combineWells(NK_CD8_sample).subsample(0.2)

    Tcell_sample = applyMatrix(Tcell_sample, compMatrix('2019-11-08', '1', 'A'))
    NK_CD8_sample = applyMatrix(NK_CD8_sample, compMatrix('2019-11-08', '1', 'B'))

    Tcell_sample = Tcell_sample.transform("tlog", channels=['VL1-H', 'VL4-H', 'BL1-H', 'BL3-H'])  # Tlog transformations
    NK_CD8_sample = NK_CD8_sample.transform("tlog", channels=['RL1-H', 'VL4-H', 'BL1-H', 'BL2-H'])  # Tlog transformations

    cd4_gate = ThresholdGate(6500.0, ['VL4-H'], region='above') & ThresholdGate(8000.0, ['VL4-H'], region='below')
    ax[0] = Tcell_sample.plot(['VL4-H'], gates=cd4_gate, ax=ax[0])  # CD4
    plt.title("Singlet Lymphocytes")
    #ax.set(xlabel= "CD4", ylabel="Events")
    plt.grid()

    sampleCD4 = Tcell_sample.gate(cd4_gate)
    Treg_gate = PolyGate([(4.2e3, 7.2e3), (6.5e03, 7.2e03), (6.5e03, 5.3e03), (4.9e03, 5.3e03), (4.2e03, 5.7e03)], ('VL1-H', 'BL1-H'), region='in', name='treg')
    Thelp_gate = PolyGate([(1.8e03, 3.1e03), (1.8e03, 4.9e03), (6.0e03, 4.9e03), (6.0e03, 3.1e03)], ('VL1-H', 'BL1-H'), region='in', name='thelper')

    _ = sampleCD4.plot(['VL1-H', 'BL1-H'], gates=[Treg_gate, Thelp_gate], gate_colors=['red', 'red'], cmap=cm.jet, ax=ax[1])  # CD4
    plt.title("CD4+ Cells")
    plt.xlabel("CD25")
    plt.ylabel("FOXP3")
    plt.grid()

    #CD8+ Cells
    CD3CD8gate = PolyGate([(7.5e3, 8.4e3), (4.7e3, 8.4e3), (4.7e03, 6.5e03), (7.5e03, 6.5e03)], ('VL4-H', 'RL1-H'), region='in', name='treg')
    _ = NK_CD8_sample.plot(['VL4-H', 'RL1-H'], gates=CD3CD8gate, gate_colors='red', cmap=cm.jet, ax=ax[2])  # CD3, CD8
    plt.title("Singlet Lymphocytes")
    plt.xlabel("CD3")
    plt.ylabel("CD8")
    plt.grid()

    # NK Cells
    NKgate = PolyGate([(4.8e3, 5.1e3), (5.9e3, 5.1e3), (5.9e03, 6.1e03), (4.8e03, 6.1e03)], ('VL4-H', 'BL1-H'), region='in', name='treg')
    CD56brightgate = PolyGate([(4.8e3, 6.3e3), (5.9e3, 6.3e3), (5.9e03, 7.3e03), (4.8e03, 7.3e03)], ('VL4-H', 'BL1-H'), region='in', name='treg')
    _ = NK_CD8_sample.plot(['VL4-H', 'BL1-H'], gates=[NKgate, CD56brightgate], gate_colors=['red', 'red'], cmap=cm.jet, ax=ax[3])  # CD3, CD56
    plt.title("Singlet Lymphocytes")
    plt.xlabel("CD3")
    plt.ylabel("CD56")
    plt.grid()

    # Gating for live cells
    sample1A, _, _ = importF("4-23", "1", "A", 1, "IL2R", None)
    sample2B, _, _ = importF("4-23", "1", "B", 2, "IL2R", None)
    sample3C, _, _ = importF("4-23", "1", "C", 3, "IL2R", None)
    panel1 = sample1A.transform("tlog", channels=['VL6-H', 'VL4-H', 'BL1-H', 'VL1-H', 'BL3-H']).subsample(0.2)
    panel2 = sample2B.transform("tlog", channels=['VL4-H', 'BL3-H']).subsample(0.2)
    panel3 = sample3C.transform("tlog", channels=['VL6-H', 'VL4-H', 'BL3-H']).subsample(0.2)

    cd3cd4_gate = PolyGate([(5.0e03, 7.3e03), (5.3e03, 5.6e03), (8.0e03, 5.6e03), (8.0e03, 7.3e03)], ('VL4-H', 'VL6-H'), region='in', name='cd3cd4')
    _ = panel1.plot(['VL4-H', 'VL6-H'], gates=cd3cd4_gate, gate_colors=['red'], cmap=cm.jet, ax=ax[4])  # CD3, CD4
    plt.title("Singlet Lymphocytes")
    plt.xlabel("CD3")
    plt.ylabel("CD4")
    plt.grid()

    samplecd3cd4 = panel1.gate(cd3cd4_gate)
    thelp_gate = PolyGate([(0.2e03, 6.8e03), (0.2e03, 4.4e03), (3.7e03, 4.4e03), (5.7e03, 5.9e03), (5.7e03, 6.8e03)], ('VL1-H', 'BL1-H'), region='in', name='thelp')
    treg_gate = PolyGate([(3.8e03, 4.4e03), (3.8e03, 3.0e03), (6.5e03, 2.9e03), (6.5e03, 5.0e03), (5.7e03, 5.8e03)], ('VL1-H', 'BL1-H'), region='in', name='treg')
    _ = samplecd3cd4.plot(['VL1-H', 'BL1-H'], gates=[thelp_gate, treg_gate], gate_colors=['red', 'red'], cmap=cm.jet, ax=ax[5])  # CD3, CD4
    plt.title("CD3+CD4+ cells")
    plt.xlabel("CD25")
    plt.ylabel("CD127")
    plt.grid()

    nk_gate = PolyGate([(3.3e3, 5.4e3), (5.3e3, 5.4e3), (5.3e3, 7.3e3), (3.3e3, 7.3e3)], ('VL4-H', 'BL3-H'), region='in', name='nk')
    nkt_gate = PolyGate([(5.6e3, 5.1e3), (7.6e3, 5.1e3), (7.6e3, 7.1e3), (5.6e3, 7.1e3)], ('VL4-H', 'BL3-H'), region='in', name='nkt')
    _ = panel2.plot(['VL4-H', 'BL3-H'], gates=[nk_gate, nkt_gate], gate_colors=['red', 'red'], cmap=cm.jet, ax=ax[6])  # CD56 vs. CD3
    samplenk = panel2.gate(nk_gate)
    samplenkt = panel2.gate(nkt_gate)
    plt.title("Singlet Lymphocytes")
    plt.xlabel("CD3")
    plt.ylabel("CD56")
    plt.grid()

    cd8_gate = PolyGate([(4.2e3, 5.7e3), (8.1e3, 5.7e3), (8.1e3, 8.0e3), (4.2e3, 8.0e3)], ('VL4-H', 'VL6-H'), region='in', name='cd8')
    _ = panel3.plot(['VL4-H', 'VL6-H'], gates=cd8_gate, gate_colors=['red'], cmap=cm.jet, ax=ax[7])  # CD8 vs. CD3
    plt.title("Singlet Lymphocytes")
    plt.xlabel("CD3")
    plt.ylabel("CD8")

    for i, axs in enumerate(ax):
        if i == 0:
            print(" ")
            # weird error replace later, axs is not correct object type
            # axs.set(xlabel='CD4',ylabel='Events')
        elif i == 1:
            axs.set_title('T Cell Gating')
            axs.set(xlabel='CD25', ylabel='FOXP3')
        elif i == 2:
            axs.set_title('CD8+ Cells Gating')
            axs.set(xlabel='CD3', ylabel='CD8')
        elif i == 3:
            axs.set_title('NK Cells Gating')
            axs.set(xlabel='CD3', ylabel='CD56')
        elif i == 4:
            axs.set_title('CD3+CD4+ Gating')
            axs.set(xlabel='CD3', ylabel='CD4')
        elif i == 5:
            axs.set_title('T reg and T Helper Gating')
            axs.set(xlabel='CD25', ylabel='CD127')
        elif i == 6:
            axs.set_title('NK and NKT Gating')
            axs.set(xlabel='CD3', ylabel='CD56')
        elif i == 7:
            axs.set_title('CD3+CD8+ Gating')
            axs.set(xlabel='CD3', ylabel='CD8')
        if i != 0:
            axs.grid()

    receptorPlot(ax[8])
    IL2RahiLoPlot(ax[9])

    return f

def importF2(pathname, WellRow):
    """
    Import FCS files. Variable input: name of path name to file. Output is a list of Data File Names in FCT Format
    Title/file names are returned in the array file --> later referenced in other functions as title/titles input argument
    """
    # Declare arrays and int
    file = []
    sample = []
    z = 0
    # Read in user input for file path and assign to array file
    pathlist = Path(r"" + str(pathname)).glob("**/*.fcs")
    for path in pathlist:
        wellID = path.name.split("_")[1]
        if wellID[0] == WellRow:
            file.append(str(path))
    file.sort()
    assert file != []
    # Go through each file and assign the file contents to entry in the array sample
    for entry in file:
        sample.append(FCMeasurement(ID="Test Sample" + str(z), datafile=entry))
        z += 1
    # Returns the array sample which contains data of each file in folder (one file per entry in array)
    return sample, file

def receptorPlot(ax1):

    df_rec = getReceptors()
    df_test = df_rec.loc[(df_rec['Cell Type'] == 'T-helper') & (df_rec['Receptor'] == 'CD122')]
    # write to csv
    update_path = path_here + "/data/receptor_levels.csv"
    df_rec.to_csv(str(update_path), index=False, header=True)

    cell_names = ["T-reg", "T-helper", "NK", "CD8+", "BNK"]
    receptors_ = ["CD25", "CD122", "CD132", "CD127"]

    # calculate mean, variance, and skew for each replicate
    df_stats = calculate_moments(df_rec, cell_names, receptors_)

    # plots log10 of mean on
    celltype_pointplot(ax1, df_stats, "Mean")
    """
    receptor_levels_Beta = getReceptors(correlation="CD122")
    receptor_levels_Gamma = getReceptors(correlation="CD132")

    cell_types = ['T-reg']

    for index, cell_type in enumerate(cell_types):

        alphaBLevels = receptor_levels_Beta.loc[(receptor_levels_Beta['Cell Type'] == cell_type) & (receptor_levels_Beta['Receptor'] == 'CD25')]
        betaLevels = receptor_levels_Beta.loc[(receptor_levels_Beta['Cell Type'] == cell_type) & (receptor_levels_Beta['Receptor'] == 'CD122')]

        alphaGLevels = receptor_levels_Gamma.loc[(receptor_levels_Gamma['Cell Type'] == cell_type) & (receptor_levels_Gamma['Receptor'] == 'CD25')]
        gammaLevels = receptor_levels_Gamma.loc[(receptor_levels_Gamma['Cell Type'] == cell_type) & (receptor_levels_Gamma['Receptor'] == 'CD132')]

        alphaBCounts = alphaBLevels['Count'].reset_index(drop=True)
        betaCounts = betaLevels['Count'].reset_index(drop=True)
        d = {'alpha': alphaBCounts, 'beta': betaCounts}
        recepCounts = pd.DataFrame(data=d)
        recepCounts = recepCounts.dropna()
        recepCounts = recepCounts[(recepCounts[['alpha', 'beta']] != 0).all(axis=1)].sample(frac=0.20)

        hex1 = ax2
        hex1.hexbin(recepCounts['alpha'], recepCounts['beta'], xscale='log', yscale='log', mincnt=1, cmap='viridis')
        hex1.set_xlabel('CD25 (IL2RA)')
        hex1.set_ylabel('CD122(IL2RB)')
        hex1.set_title(cell_type + ' Alpha-Beta correlation')

        alphaGCounts = alphaGLevels['Count'].reset_index(drop=True)
        gammaCounts = gammaLevels['Count'].reset_index(drop=True)
        d2 = {'alpha': alphaGCounts, 'gamma': gammaCounts}
        recepCounts2 = pd.DataFrame(data=d2)
        recepCounts2 = recepCounts2.dropna()
        recepCounts2 = recepCounts2[(recepCounts2[['alpha', 'gamma']] != 0).all(axis=1)].sample(frac=0.20)

        hex2 = ax3
        hex2.hexbin(recepCounts2['alpha'], recepCounts2['gamma'], xscale='log', yscale='log', mincnt=1, cmap='viridis')
        hex2.set_xlabel('CD25 (IL2RA)')
        hex2.set_ylabel('CD132 (IL2RG)')
        hex2.set_title(cell_type + ' Alpha-Gamma correlation')
    """

    return

def calculate_moments(df, cell_names, receptors):
    """ Calculates mean, variance, and skew for each replicate. """
    df_stats = pd.DataFrame(columns=["Cell Type", "Receptor", "Mean", "Variance", "Skew", "Date", "Plate"])
    for _, cell in enumerate(cell_names):
        for _, receptor in enumerate(receptors):
            for _, date in enumerate(["4-23", "4-26", "5-16"]):
                for _, plate in enumerate(["1", "2"]):
                    df_subset = df.loc[(df["Cell Type"] == cell) & (df["Receptor"] == receptor) & (df["Date"] == date) & (df["Plate"] == plate)]["Count"]
                    mean_ = np.log10(df_subset.mean())
                    var_ = np.log10(df_subset.var())
                    skew_ = np.log10(df_subset.skew())
                    df_new = pd.DataFrame(columns=["Cell Type", "Receptor", "Mean", "Variance", "Skew", "Date", "Plate"])
                    df_new.loc[0] = [cell, receptor, mean_, var_, skew_, date, plate]
                    df_stats = pd.concat([df_stats, df_new])

    return df_stats

def celltype_pointplot(ax, df, moment):
    """ Plots a given distribution moment with SD among replicates for all cell types and receptors. """
    sns.pointplot(x="Cell Type", y=moment, hue="Receptor", data=df, ci='sd', join=False, dodge=True, ax=ax, estimator=sp.stats.gmean)
    ax.set_ylabel("log(" + moment + ")")
    ax.set_xticklabels(ax.get_xticklabels(), rotation=25, rotation_mode="anchor", ha="right", position=(0, 0.02), fontsize=7.5)
    print(df.groupby(["Cell Type", "Receptor"]).Mean.mean())

def getReceptors(correlation=None):
    # import bead data and run regression to get equations
    lsq_cd25, lsq_cd122, lsq_cd132, lsq_cd127 = run_regression()

    # create dataframe with gated samples (all replicates)
    df_gates = import_gates()

    if correlation == 'CD122':
        df_signal = apply_gates("4-23", "1", df_gates, correlation="CD122")
        df_signal = pd.concat([df_signal, apply_gates("4-23", "2", df_gates, correlation="CD122")])
        df_signal = pd.concat([df_signal, apply_gates("4-26", "1", df_gates, correlation="CD122")])
        df_signal = pd.concat([df_signal, apply_gates("4-26", "2", df_gates, correlation="CD122")])
    elif correlation == 'CD132':
        df_signal = apply_gates("4-23", "1", df_gates, correlation="CD132")
        df_signal = pd.concat([df_signal, apply_gates("4-23", "2", df_gates, correlation="CD132")])
        df_signal = pd.concat([df_signal, apply_gates("4-26", "1", df_gates, correlation="CD132")])
        df_signal = pd.concat([df_signal, apply_gates("4-26", "2", df_gates, correlation="CD132")])
    else:
        df_signal = apply_gates("4-23", "1", df_gates)
        df_signal = pd.concat([df_signal, apply_gates("4-23", "2", df_gates)])
        df_signal = pd.concat([df_signal, apply_gates("4-26", "1", df_gates)])
        df_signal = pd.concat([df_signal, apply_gates("4-26", "2", df_gates)])
        df_signal = pd.concat([df_signal, apply_gates("5-16", "1", df_gates)])
        df_signal = pd.concat([df_signal, apply_gates("5-16", "2", df_gates)])

    # make new dataframe for receptor counts
    df_rec = pd.DataFrame(columns=["Cell Type", "Receptor", "Count", "Date", "Plate"])
    cell_names = ["T-reg", "T-helper", "NK", "CD8+", "BNK"]
    receptors_ = ["CD25", "CD122", "CD132", "CD127"]
    channels_ = ["VL1-H", "BL5-H", "RL1-H", "BL1-H"]
    lsq_params = [lsq_cd25, lsq_cd122, lsq_cd132, lsq_cd127]
    dates = ["4-23", "4-26", "5-16"]
    plates = ["1", "2"]

    # calculate receptor counts
    for _, cell in enumerate(cell_names):
        for j, receptor in enumerate(receptors_):
            for _, date in enumerate(dates):
                for _, plate in enumerate(plates):
                    data = df_signal.loc[(df_signal["Cell Type"] == cell) & (df_signal["Receptor"] == receptor) & (df_signal["Date"] == date) & (df_signal["Plate"] == plate)][channels_[j]]
                    data = data[data >= 0]
                    rec_counts = np.zeros(len(data))
                    for k, signal in enumerate(data):
                        A, B, C, D = lsq_params[j]
                        rec_counts[k] = C * (((A - D) / (signal - D)) - 1)**(1 / B)
                    df_add = pd.DataFrame({"Cell Type": np.tile(cell, len(data)), "Receptor": np.tile(receptor, len(data)),
                                           "Count": rec_counts, "Date": np.tile(date, len(data)), "Plate": np.tile(plate, len(data))})
                    df_rec = pd.concat([df_rec, df_add])

    return df_rec

def run_regression():
    """ Imports bead data and runs regression to get least squares parameters for conversion of signal to receptor count. """
    sampleD, _ = importF2(path_here + "/data/flow/2019-04-23 Receptor Quant - Beads", "D")
    sampleE, _ = importF2(path_here + "/data/flow/2019-04-23 Receptor Quant - Beads/", "E")
    sampleF, _ = importF2(path_here + "/data/flow/2019-04-23 Receptor Quant - Beads/", "F")
    sampleI, _ = importF2(path_here + "/data/flow/2019-05-16 Receptor Quant - Beads/", "F")

    recQuant1 = np.array([0., 4407, 59840, 179953, 625180])  # CD25, CD122
    recQuant2 = np.array([0., 7311, 44263, 161876, 269561])  # CD132
    recQuant3 = np.array([4407, 59840, 179953, 625180, 0.0])  # CD127

    _, lsq_cd25 = bead_regression(sampleD, channels['D'], recQuant1)
    _, lsq_cd122 = bead_regression(sampleE, channels['E'], recQuant1, 2, True)
    _, lsq_cd132 = bead_regression(sampleF, channels['F'], recQuant2)
    _, lsq_cd127 = bead_regression(sampleI, channels["I"], recQuant3)

    return lsq_cd25, lsq_cd122, lsq_cd132, lsq_cd127

cellTypeDict = {"Treg": r"$T_{reg}$",
                "Treg $IL2Ra^{lo}$": r"$T_{reg}$ $IL2Ra^{lo}$",
                "Treg $IL2Ra^{hi}$": r"$T_{reg}$ $IL2Ra^{hi}$",
                "Thelper": r"$T_{helper}$",
                "Thelper $IL2Ra^{lo}$": r"$T_{helper}$ $IL2Ra^{lo}$",
                "Thelper $IL2Ra^{hi}$": r"$T_{helper}$ $IL2Ra^{hi}$"}

receptorDict = {"IL2Ra": "IL2Rα",
                "IL2Rb": r"$IL2·β$"}

hiLoCells = ["$T_{reg}$", "$T_{reg}$ $IL2Ra^{lo}$", "$T_{reg}$ $IL2Ra^{hi}$", "$T_{helper}$", "$T_{helper}$ $IL2Ra^{lo}$", "$T_{helper}$ $IL2Ra^{hi}$"]

def IL2RahiLoPlot(ax):
    """Quickly plots IL2Ra and IL2Rb for given population"""
    receptorDF = importReceptors()
    receptorDF = receptorDF.replace(cellTypeDict)
    receptorDF = receptorDF.replace(receptorDict)
    receptorDF = receptorDF.loc[(receptorDF["Receptor"].isin(["IL2Rα", "$IL2·β$"]) & (receptorDF["Cell Type"].isin(hiLoCells)))]
    sns.barplot(x="Cell Type", y="Mean", hue="Receptor", data=receptorDF, ax=ax)


# conding: utf-8
from datetime import datetime as dt
import datetime

if __name__ == '__main__':

    f = open('tickets.txt', 'r')
    original = [(r.split(' ')[0], r.split(' ')[1].rstrip('\n')) for r in f]
    f.close()

    used_list = []

    before_end = datetime.date(2016, 1, 1)

    while True:
        discovery = False
        key = ''
        date = ''
        start = ''
        end = ''
        term = 0

        for d in range(1, 367):
            before_end += datetime.timedelta(days=1)

            for d in original:
                key = d[0] + '-' + d[1]
                date = d[1].split('-')
                start = date[0].split('/')
                end = date[1].split('/')

                if start[0] == str(before_end.month) and start[1] == str(before_end.day):
                    discovery = True
                    break

            if discovery is True:# or (before_end.month == 12 and before_end.day == 31):
                break

        if discovery is True:
            start = dt.strptime('2016-' + str(start[0]) + '-' + str(start[1]), '%Y-%m-%d')
            end = dt.strptime('2016-' + str(end[0]) + '-' + str(end[1]), '%Y-%m-%d')
            term = (end - start).days
            used_list.append((key, term))
            original.pop(original.index(d))
            before_end = end

            if len(original) == 0:

                for used in used_list:
                    print(used)
                break

        # if before_end.month == 12 and before_end.day == 31:
        #     print(used_list)
        #     break


from random import random, seed
import math

import pyglet
from pyglet.window import key, mouse
from pyglet.gl import *

import scene, world
from phys import engine
from entity.entity import entity
from entity.square import square
from entity.circle import circle

class simulation(object):
    def __init__(self, width, height):
        # create pyglet window
        self.window = pyglet.window.Window()
        self.window.on_draw = self.on_draw
        self.window.on_key_press = self.on_key_press
        self.window.on_key_release = self.on_key_release
        self.window.width = width
        self.window.height = height
        self.key_pressed = []

        # create fps display
        self.fps_display = pyglet.clock.ClockDisplay()
        self.clock = 0

        # sync clock
        pyglet.clock.schedule_interval(self.tick, 1.0/60.0)
        pyglet.clock.set_fps_limit(60)

        # create world
        world_width = 3000
        world_height = 3000
        self.world = world.world(world_width, world_height)

        # create scene- match dimensions of the app window
        self.scene = scene.scene(self.world, offset_x=0, offset_y=0,width=width, height=height)

        # create physics engine
        self.engine = engine.engine()

        self.sun = []
        self.moon = []
        self.earth = []

        # throw some objects in there for now
        moon_pos = dict(x=world_width/2, y=world_height/2)
        moon = circle(position=moon_pos, color=(100, 100, 100, 255), radius=400, num_vertices=50, z_index=100)
        self.world.add_entity(moon)
        self.moon.append(moon)

        moon_pos = dict(x=world_width/2, y=world_height/2)
        moon = circle(position=moon_pos, color=(200, 200, 200, 255), radius=380, num_vertices=50, z_index=101)
        self.world.add_entity(moon)
        self.moon.append(moon)

        sun_pos = dict(x=world_width/4, y=world_height/4)
        sun = circle(position=sun_pos, color=(255, 255, 0, 200), radius=100, num_vertices=50, z_index=3)
        self.world.add_entity(sun)
        self.sun.append(sun)

        sun_pos = dict(x=world_width/4, y=world_height/4)
        sun = circle(position=sun_pos, color=(255, 215, 0, 200), radius=110, num_vertices=50, z_index=2)
        self.world.add_entity(sun)
        self.sun.append(sun)

        sun_pos = dict(x=world_width/4, y=world_height/4)
        sun = circle(position=sun_pos, color=(255, 150, 0, 200), radius=120, num_vertices=50, z_index=1)
        self.world.add_entity(sun)
        self.sun.append(sun)

        earth_pos = dict(x=world_width/8, y=world_height/8)
        earth = circle(position=earth_pos, color=(255, 255, 255, 100), radius=42, num_vertices=50, z_index=100)
        self.world.add_entity(earth)
        self.earth.append(earth)

        earth_pos = dict(x=world_width/8, y=world_height/8)
        earth = circle(position=earth_pos, color=(27, 92, 174, 255), radius=38, num_vertices=50, z_index=101)
        self.world.add_entity(earth)
        self.earth.append(earth)

        earth_pos = dict(x=world_width/8 + 5, y=world_height/8 + 5)

        land = [dict(x=-10, y=30), dict(x=0,y=0), dict(x=15, y=7.5), dict(x=20, y=15), dict(x=22, y=10), dict(x=20,y=-15)]
        earth = entity(position=earth_pos, color=(27, 98, 22, 225), z_index=102, vertices=land)
        self.world.add_entity(earth)
        self.earth.append(earth)

    def tick(self, dt):
        # update physics
        self.engine.update()

        # update scene
        self.scene.update()

        # crappy orbit stuff, just temporary. Cosnidering making a system class that handles sets of objects
        scale = 2 * (math.fabs(self.moon[0].position['y'] - self.sun[0].position['y']) / self.world.height ) + .5
        # rotate sun
        for _ in self.sun:
            _.orbit_around(self.moon[0].position, self.moon[0].radius + 150, (dt * 360) / 120)
            _.scale(scale)

        for _ in self.earth:
            if scale > .5:
                _.z_index = self.moon[0].z_index + 1
            else:
                _.z_index = self.moon[0].z_index - 1
            _.scale(scale)
            _.rotate(1)
            _.orbit_around(self.sun[0].position, self.sun[0].radius + 150, (dt * 360) / 10)

        # move scene
        if key.LEFT in self.key_pressed:
            self.scene.translateX(-10)
        if key.RIGHT in self.key_pressed:
            self.scene.translateX(10)
        if key.UP in self.key_pressed:
            self.scene.translateY(-10)
        if key.DOWN in self.key_pressed:
            self.scene.translateY(10)
        if key.Q in self.key_pressed:
            self.scene.rotate(.1)

        self.clock += 1

    def on_draw(self):
        # clear window
        self.window.clear()
        glClear(GL_COLOR_BUFFER_BIT)
        glLoadIdentity()

        # draw background
        self.scene.draw_background()

        # # redraw scene
        self.scene.render()

        # draw fps clock
        self.fps_display.draw()

        # draw foreground/ui ? in here or scene

    def on_key_press(self, symbol, modifiers):
        if symbol == key.LEFT:
            self.key_pressed.append(key.LEFT)
        elif symbol == key.RIGHT:
            self.key_pressed.append(key.RIGHT)
        elif symbol == key.UP:
            self.key_pressed.append(key.UP)
        elif symbol == key.DOWN:
            self.key_pressed.append(key.DOWN)
        elif symbol == key.Q:
            self.key_pressed.append(key.Q)
        elif symbol == key.E:
            self.key_pressed.append(key.E)

    def on_key_release(self, symbol, modifiers):
        self.key_pressed.remove(symbol)

sim = simulation(1500, 1000)
pyglet.app.run()


import os
import json
import shutil
import sqlite3
import xlrd

config_file_name = "db_config.ini"
config_file_template = {"db_config": [{"output_db_name1": ["included_excel_file1", "included_excel_file2"]}, {"output_db_name2": ["included_excel_file3", "included_excel_file4"]}]}

def get_config_root_path():
    return os.path.join(os.path.join(os.getcwd(), "testroot"), "hello")

def get_config_file_path():
    return os.path.join(get_config_root_path(), "db_config.ini")

def get_data_source_root_path():
    return os.path.join(os.getcwd(), "data_source")

def get_output_root_path():
    return os.path.join(os.getcwd(), "output_path")

def get_xlsx_file_data(file_name):
    print("[Info] Load " + file_name + " data.")
    data_file_full_path = os.path.join(get_data_source_root_path(), file_name)
    workbook = xlrd.open_workbook(data_file_full_path)
    index = file_name.index('.')
    sheet_name = file_name[0:index]
    sheet_data = workbook.sheet_by_name(sheet_name)
    return sheet_name, sheet_data

def correct_cell_value_type(cell_value, value_type):
    if value_type.upper() == "INT":
        return int(cell_value)

    if value_type.upper() == "REAL":
        return float(cell_value)

    return cell_value

def import_data(table_name, data_sheet, db_connection):
    print("[Info] Import " + table_name + " to table.")

    rows_count = data_sheet.nrows
    cols_count = data_sheet.ncols

    if not rows_count >= 2:
        print("[Error] No data in " + table_name + ".xlsx.")
        return

    create_table_script = "create table " + table_name + "("

    for c in range(0, cols_count):
        key_name = data_sheet.cell(0, c).value
        key_type = data_sheet.cell(1, c).value

        if (key_name is None) or (key_name == "") or (key_type is None) or (key_type == ""):
            print("[Error] Wrong data struct in " + table_name + ".xlsx.")
            return

        if c == 0:
            create_table_script += key_name + " " + key_type + " primary key"
        else:
            create_table_script += ", " + key_name + " " + key_type

    create_table_script += ")"
    print("[Info] Create table script: " + create_table_script)

    db_connection.execute(create_table_script)

    update_table_script = "insert into " + table_name + " values ("
    split = ""
    for t in range(0, cols_count):
        update_table_script += split + "?"
        split = ","
    update_table_script += ")"

    #print(update_table_script)

    for r in range(2, rows_count):
        value_set = ()

        for c in range(0, cols_count):
            cell_data = data_sheet.cell(r, c).value
            cell_data = correct_cell_value_type(cell_data, data_sheet.cell(1, c).value)
            temp = (cell_data,)
            value_set = value_set + temp

        db_connection.execute(update_table_script, value_set)

def generate_default_config_file():
    json_data = json.dumps(config_file_template, sort_keys=False, indent=4)

    try:
        fp = open(get_config_file_path(), 'w+')
        fp.write(json_data)
        fp.close()
    except:
        print("[Error] Cannot create <" + config_file_name + "> file.")

    exit(0)

def load_config_file():
    print("... Load Config File - Start ...")
    config_file_path = get_config_file_path()

    ret = None

    if os.path.exists(config_file_path) and os.path.isfile(config_file_path):
        try:
            fp = open(config_file_path, 'r')
            ret = json.load(fp)
            fp.close()
        except:
            print("[Error] Cannot open / read <" + config_file_name + ">.")
            exit(0)

        if ret is None:
            print("[Error] <" + config_file_name + "> file format is wrong.")
            exit(0)

    else:
        print("[Error] <" + config_file_name + "> not found.")
        generate_default_config_file()

    print("... Load Config File - Done ...")
    return ret

def valid_config_file(config):
    print("... Valid Config File - Start ...")

    if not ("db_config" in config):
        print("[Error] <" + config_file_name + "> content is invalid.")
        exit(0)

    valid_config = {}

    for db_set in config['db_config']:
        for db_name in db_set.keys():
            if not (db_name in valid_config):
                valid_config[db_name] = []

            data_source_file_list = db_set[db_name]

            if data_source_file_list is None:
                print("[Warning] No xlsx file exports to " + db_name + ".")
                continue

            for data_source_file_name in data_source_file_list:
                full_file_name = os.path.join(get_data_source_root_path(), data_source_file_name)

                if not os.path.exists(full_file_name) or not os.path.isfile(full_file_name):
                    print("[Warning] " + full_file_name + " does not exist.")
                else:
                    if not (data_source_file_name in valid_config[db_name]):
                        valid_config[db_name].append(data_source_file_name)

    return valid_config

    print("... Valid Config File - End ...")

def check_environment():
    print("... Check Environment - Start ...")

    if not os.path.exists(get_config_root_path()):
        os.makedirs(get_config_root_path())

    if not os.path.exists(get_data_source_root_path()):
        os.makedirs(get_data_source_root_path())

    if not os.path.exists(get_output_root_path()):
        os.makedirs(get_output_root_path())

    print("... Check Environment - End ...")

def remove_outdated_db_file():
    print("... Remove Outdated DB File - Start ...")

    is_success = True

    file_list = os.listdir(get_output_root_path())
    for file_name in file_list:
        full_path = os.path.join(get_output_root_path(), file_name)

        if os.path.isfile(full_path):
            try:
                print("Remove " + full_path)
                os.remove(full_path)
            except:
                print("[Error] Fail to remove " + full_path)
                is_success = False

        if os.path.isdir(full_path) and (file_name > ".svn") - (file_name < ".svn"):
            try:
                print("Remove " + full_path)
                shutil.rmtree(full_path)
            except:
                print("[Error] Fail to remove " + full_path)
                is_success = False

    if not is_success:
        exit(0)

    print("... Remove Outdated DB File - End ...")

def generate_db_file(config):
    print("... Start Generate DB - Start ...")

    for db_name, data_file_list in config.items():
        if db_name == "":
            print("[Warning] Db name is empty and will be skipped.")
            continue

        print("[Info] Build <" + db_name + ">.")

        db_file_full_path = os.path.join(get_output_root_path(), db_name)
        conn = sqlite3.connect(db_file_full_path)

        for file_name in data_file_list:
            table_name, db_data = get_xlsx_file_data(file_name)
            import_data(table_name, db_data, conn)

        conn.close()

    print("... Start Generate DB - End ...")

if __name__ == "__main__":
    check_environment()
    build_config = load_config_file()
    build_config = valid_config_file(build_config)
    remove_outdated_db_file()
    generate_db_file(build_config)


#!/usr/bin/python
# Copyright 2018 - Drew Mahler andrew.h.mahler@gmail.com

'''
General Framework for analyzing tweets
	1. Reads all tweet files into a list of tweet objects
		--> Tweet object: tweet information + User Object
'''
import json

class TwitterUser():
	def __init__(self, screenname, profilename, user_id, user_since):
		self.screenname = screenname
		self.profilename = profilename
		self.id = user_id
		self.since = user_since

class Tweet():
	# Note to self: objects can inherit! And its useful
	def __init__(self, tweet, tweeted_at, result_type, screenname, profilename, user_id, user_since):
		self.tweet = tweet
		self.tweeted_at = screenname
		self.result_type = result_type
		self.user = TwitterUser(screenname, profilename, user_id, user_since)

def tweetunpacking(filelist):
	# Create a list with all of the tweets
	tweets = []
	for json_file in filelist:
		with open(json_file) as json_data:
		    outermost = json.load(json_data)
		    current_tweets = outermost['statuses']
		    for tweet in current_tweets:
		    	user = tweet['user']
		    	metadata = tweet['metadata']
		    	tweet = Tweet(tweet['text'], tweet['created_at'], metadata['result_type'], \
		    		          user['screen_name'], user['name'], user['id'], user['created_at'])
		    	tweets.append(tweet)

	# Tweets hierarchy - high level
	## file (dictionary) -> key: search-metadata (dictionary)
	##                   -> key: statuses (AKA tweets) (list of dictionaries) -> tweet (dictionary) -> user (dictionary)

	# A list of tweets
	return tweets


import http
import json
import pandas as pd
import calendar
import os
from datetime import timedelta, date
from typing import Dict, List, Tuple
from http import client

class Tradier:
    def __init__(self, auth: str, storage_path="data/"):
        self.storage_path = storage_path
        self.connection = http.client.HTTPSConnection(
            "sandbox.tradier.com", 443, timeout=30)
        self.headers = headers = {"Accept": "application/json",
                                  "Authorization": "Bearer {}".format(auth)}

    def request(self, endpoint: str):
        self.connection.request("GET", endpoint, None, self.headers)
        try:
            response = self.connection.getresponse()
            content = response.read()
            if int(str(response.status)) == 200:
                return json.loads(bytes.decode(content))
            return None
        except http.HTTPException as e:
            return e

    def options(self, symbol: str):
        return Options(self, symbol)

    def historical_data(self, symbol: str):
        endpoint = "/v1/markets/history?symbol={}".format(symbol)
        return self.request(endpoint)

    def load_data(self, symbol: str) -> pd.DataFrame:
        path = self.storage_path + "{}.csv".format(symbol)
        if os.path.exists(path):
            df = pd.read_csv(path)
            df = df.set_index(pd.DatetimeIndex(df["date"]))
            df = df.loc[:, ~df.columns.str.contains('date')]
            df = df.loc[:, ~df.columns.str.contains('^Unnamed')]
            return df
        else:
            try:
                df = pd.DataFrame(self.historical_data(
                    symbol).get("history", {}).get("day", []))
                df = df.set_index(pd.DatetimeIndex(df["date"]))
                df = df.loc[:, ~df.columns.str.contains('date')]
                df = df.loc[:, ~df.columns.str.contains('^Unnamed')]
                df.to_csv(path)
                return df
            except Exception as e:
                print(e)
                return None

def third_fridays(d, n):
    """Given a date, calculates n next third fridays
    https://stackoverflow.com/questions/28680896/how-can-i-get-the-3rd-friday-of-a-month-in-python/28681097"""

    def next_third_friday(d):
        """ Given a third friday find next third friday"""
        d += timedelta(weeks=4)
        return d if d.day >= 15 else d + timedelta(weeks=1)

    # Find closest friday to 15th of month
    s = date(d.year, d.month, 15)
    result = [s + timedelta(days=(calendar.FRIDAY - s.weekday()) % 7)]

    # This month's third friday passed. Find next.
    if result[0] < d:
        result[0] = next_third_friday(result[0])

    for i in range(n - 1):
        result.append(next_third_friday(result[-1]))

    return result

class Options:
    def __init__(self, tradier: Tradier, symbol: str):
        self.api = tradier
        self.symbol = symbol
        self.cache = {}

    def call(self, expiration: date, strike: int) -> pd.DataFrame:
        chain = "{symbol}{y}{m:02d}{d:02d}C{strike:05d}000".format(symbol=self.symbol, y=str(
            expiration.year)[2:], m=expiration.month, d=expiration.day, strike=strike)
        if chain in self.cache:
            return self.cache[chain]
        else:
            df = self.api.load_data(chain)
            self.cache[chain] = df
            return df

    def put(self, expiration: date, strike: int) -> pd.DataFrame:
        chain = "{symbol}{y}{m:02d}{d:02d}P{strike:05d}000".format(symbol=self.symbol, y=str(
            expiration.year)[2:], m=expiration.month, d=expiration.day, strike=strike)
        if chain in self.cache:
            return self.cache[chain]
        else:
            df = self.api.load_data(chain)
            self.cache[chain] = df
            return df

    def initialize_repository(self):
        '''
        Download all of the historical price data for all monthly expirations within a 10%
        price range of the underlying for that month. This can be manually changed in the code
        below. By default, this downloads data from 2018 only. Beyond that is an exercise for the reader
        '''

        # historical price data for the underlying, which we will merge in
        data = self.api.load_data(self.symbol)

        # calculate monthly high and low for the underlying
        monthly_range = {}
        for m in range(1, 13):
            try:
                x = data[date(2018, m, 1):date(2018, m + 1, 1)]
                monthly_range[m] = dict(low=min(x['low']), high=max(x['high']))
            except:
                # If we don't have data for this month, just extrapolate from the previous month
                monthly_range[m] = dict(
                    low=monthly_range[m - 1]['low'], high=monthly_range[m - 1]['high'])

        for m, k in monthly_range.items():
            expiration = third_fridays(date(2018, m, 1), 1)[0]
            # Get all strikes that are 10% below the monthly low and 10% above the monthly high
            strikes = [x for x in range(int(k['low'] * .9), int(k['high'] * 1.1))]
            # Download and save all of the option chains
            for s in strikes:
                self.call(expiration, s)
                self.put(expiration, s)

api = Tradier("cAAafzsmJ3lZbH0k9sChzi3JmMBx")
api.options("SPX").initialize_repository()


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import glob
import os.path
import math

month_vec = ['01','02','03','04','05','06','07','08','09','10','11']
#month_vec = ['07','08','09','10']

threshold = [10,20,30]
quantile = [0.5,0.75,0.9]

filter_negative = True

# path = '/Users/wcoupe/Documents/flight_summary_v1.0/2018/'
# allFiles = os.listdir(path)

# df_summary = pd.DataFrame()
# idx=-1
# for month in range(len(month_vec)):
# 	path = '/Users/wcoupe/Documents/flight_summary_v1.0/2018/' + str(month_vec[month])
# 	allFiles = os.listdir(path)
# 	indiv_files = []
# 	for file_name in range(len(allFiles)):
# 		if 'KCLT.fullFlightSummary.v1.0' in allFiles[file_name]:
# 			print(allFiles[file_name])
# 			df = pd.read_csv(path + '/' + allFiles[file_name],parse_dates=['apreq_final'])
# 			df = df.assign(apreq_local = df['apreq_final'].dt.tz_localize("UTC").dt.tz_convert("US/Eastern"))
# 			indiv_files.append(df)

# 	df = pd.concat(indiv_files)
# 	df = df[ (df['apreq_final'].notnull()) & (df['arrival_aerodrome_icao_name'] == 'KATL') ].reset_index(drop=True)
# 	#df = df[ (df['apreq_final'].notnull())  ].reset_index(drop=True)

# 	for flight in range(len(df)):
# 		idx+=1
# 		ref_time = pd.Timestamp( str(df.loc[flight,'apreq_local']).split(' ')[0] + ' 00:00:00' )
# 		df_summary.loc[idx,'gufi'] = df.loc[flight,'gufi']
# 		df_summary.loc[idx,'arrival_aerodrome_icao_name'] = df.loc[flight,'arrival_aerodrome_icao_name']
# 		df_summary.loc[idx,'month'] = int(month_vec[month])
# 		df_summary.loc[idx,'apreq_relative_local'] = pd.Timedelta( df.loc[flight,'apreq_final'] - ref_time ).total_seconds() / float(60)
# 		df_summary.loc[idx,'apreq_15_minute_index'] = int( math.floor(df_summary.loc[idx,'apreq_relative_local']/15) )
# 		df_summary.loc[idx,'tbfm_assigned_delay'] = df.loc[flight,'tbfm_assigned_delay'] / float(60)
# 		df_summary.loc[idx,'apreq_num_updates'] = df.loc[flight,'apreq_num_updates']
# 		df_summary.loc[idx,'apreq_compliance_sec'] = df.loc[flight,'apreq_compliance_sec']
# 		df_summary.loc[idx,'apreq_compliance_truncated_sec'] = df.loc[flight,'apreq_compliance_truncated_sec']
# 		df_summary.loc[idx,'apreq_compliant'] = df.loc[flight,'apreq_compliant']
# 		df_summary.loc[idx,'apreq_prescheduled'] = df.loc[flight,'apreq_prescheduled']
# 		df_summary.loc[idx,'eobt_at_preschedule'] = df.loc[flight,'eobt_at_preschedule']
# 		df_summary.loc[idx,'pilot_ready_time'] = df.loc[flight,'pilot_ready_time']
# 		df_summary.loc[idx,'departure_stand_actual_time'] = df.loc[flight,'departure_stand_actual_time']

# df_summary.to_csv('apreq_times.csv',index=False)

df_summary = pd.read_csv('apreq_times.csv')

max_index = int(df_summary['apreq_15_minute_index'].max())

above = np.zeros((len(threshold),len(month_vec)))
q = np.zeros((len(quantile),len(month_vec)))
avg_delay = np.zeros(len(month_vec))
count = np.zeros(len(month_vec))
count_neg = np.zeros(len(month_vec))

for month in range(len(month_vec)):
	df_temp = df_summary[ (df_summary['month'] == int(month_vec[month]) )]
	if filter_negative:
		df_temp = df_temp[ df_temp['tbfm_assigned_delay']>=0]
	for value in range(len(threshold)):
		# above[value,month] = len( df_summary[ (df_summary['month'] == int(month_vec[month]) ) & (df_summary['tbfm_assigned_delay'] >= threshold[value]) ] ) / float(len( df_summary[ (df_summary['month'] == int(month_vec[month]) )]  ))
		above[value,month] = len(df_temp[ df_temp['tbfm_assigned_delay'] >= threshold[value] ]) / float(len(df_temp))

	for q_val in range(len(quantile)):
		q[q_val,month] = df_temp['tbfm_assigned_delay'].quantile( quantile[q_val] )

	avg_delay[month] = df_temp['tbfm_assigned_delay'].mean()
	count[month] = len(df_temp)
	count_neg[month] = len( df_temp[ df_temp['tbfm_assigned_delay'] < 0] )

plt.figure(figsize=(14,10))

plt.subplot(3,1,1)
plt.bar(np.arange(len(month_vec)),count,color='green',alpha=0.5,edgecolor='black',label='Count of Flights')
plt.bar(np.arange(len(month_vec)),count_neg,color='green',alpha=0.8,edgecolor='black',label='Count of Flights: TBFM Delay < 0')
plt.grid(True,alpha=0.5)
plt.xticks(np.arange(len(month_vec)), month_vec)
plt.legend(loc='upper left',fontsize=7)
plt.ylabel('Count')
plt.title('Flight Counts')

plt.subplot(3,1,2)
for q_val in range(len(quantile)):
	plt.bar( np.arange(len(month_vec)) , q[q_val,:],alpha = 0.8 / float(1+q_val),color='purple' , edgecolor='black',label='Quantile ' + str(quantile[q_val]) )

plt.plot(np.arange(len(month_vec)) ,avg_delay,'-',marker='d',markersize=9,markeredgecolor='black',color='purple' ,label='Mean' )
plt.legend(loc='upper left',fontsize=7)
plt.grid(True,alpha=0.5)
plt.xticks(np.arange(len(month_vec)), month_vec)
plt.ylabel('TBFM Delay [Minutes]')
plt.title('TBFM Assigned Delay')
#plt.ylim([0,23])

for value in range(len(threshold)):

	plt.subplot(3,1,3)
	plt.plot(above[value,:],marker='o',linewidth=3,markersize=13,markeredgecolor='black',alpha=0.7,label='Percentage TBFM Delay > ' + str(threshold[value]) + ' Minutes')
	plt.ylabel('Percentage Above Threshold Value')
	plt.xlabel('Month')
	plt.legend(loc='upper left',fontsize=7)
	plt.grid(True,alpha=0.6)
	plt.xticks(np.arange(len(month_vec)), month_vec)
	plt.title('Percentage of Flights Experiencing TBFM Assigned Delay Above Threshold')
#plt.ylim([-0.05,.25])

plt.tight_layout()
if filter_negative:
	plt.savefig('tbfm_assigned_delay_no_negative.png')
else:
	plt.savefig('tbfm_assigned_delay_with_negative.png')


import cv2 as cv
import numpy as np

img = cv.imread('./image/water_coins.jpg')
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV+cv.THRESH_OTSU)
kernel = np.ones((3, 3), np.uint8)
cv.imshow('gth', thresh)
#开运算q
opening = cv.morphologyEx(thresh, cv.MORPH_OPEN, kernel, iterations=2)
cv.imshow('open', opening)
#膨胀
sure_bg = cv.dilate(opening, kernel, iterations=3)
cv.imshow('sure_bg', sure_bg)
#距离运算
dist_trans = cv.distanceTransform(opening, cv.DIST_L2, 5)
cv.imshow('dist', dist_trans)
#阈值计算
ret, sure_fg = cv.threshold(dist_trans, 0.6*dist_trans.max(), 255, cv.THRESH_BINARY)
cv.imshow('sure', sure_fg)
#减运算
sure_fg = np.uint8(sure_fg)
unknown = cv.subtract(sure_bg, sure_fg)
cv.imshow('haha',unknown)
ret, markers1 = cv.connectedComponents(sure_fg)

markers = markers1+1

markers[unknown==255] = 0
marker_img = np.uint8(markers)

markers3 = cv.watershed(img, markers)
img[markers3 == -1] = [255, 0, 0]
cv.imshow('img', img)
cv.waitKey(0)


import networkx as nx
import matplotlib.pyplot as plt
import pandas as pd

class GraphVisualization:

    def __init__(self):
        self.G = nx.Graph()

    def addEdges(self, df):
        myedges = []
        for index, row in df.iterrows():
            myedges.append([row['SOURCE_SUBREDDIT'], row['TARGET_SUBREDDIT'], row['LINK_SENTIMENT']*row['count']])
        self.G.add_weighted_edges_from(myedges)

    def visualize(self):
        return [len(c) for c in sorted(nx.connected_components(self.G), key=len, reverse=True)]
        # nx.draw_networkx(self.G)
        # print("Done drawing")
        # plt.show()

    def getLargest(self):
        return max(nx.connected_components(self.G), key=len)

myGraph = GraphVisualization() # create graphviz object
df = pd.read_csv("../data/cleaned.csv") # load data from csv file
myGraph.addEdges(df) # load data into graph

res = myGraph.visualize() # get list of connected components
print("Number of connected subgraphs: " + str(len(res)))
print("Number of vertices in each subgraph: ")
print(res)

bigBoi = myGraph.getLargest() # gets all vertices of largets connected graph
print("Number of vertices in largest connected subgraph: " + str(len(bigBoi)))

toDrop = [] # list to track which rows to delete
count = 0 # track number of edges retained
for index, row in df.iterrows():
    if row['SOURCE_SUBREDDIT'] in bigBoi and row['TARGET_SUBREDDIT'] in bigBoi: # retains row if both src and des subreddit are in largest connected subgraph
        count += 1 # increments count of retained edges
    else:
        toDrop.append(index) # adds row number to delete from graph if src and des subreddit are not in largest connected component

print("Number of edges in largest connected subgraph: " + str(count))
print("Number of edges dropped from original CSV: " + str(len(toDrop)))

df2 = df.drop(toDrop) # creates copy of csv data and deletes edges not in largest connected graph

df2.to_csv("../data/connected.csv", index=False) # saves edge data of largest connected subgraph to csv

print("\nSanity check:")
cleaned = GraphVisualization() # create graphviz object
cleaned.addEdges(df2) # load data from csv file
res = cleaned.visualize() # get list of connected components
print("Number of connected subgraphs in largest connected subgraph: " + str(len(res)))
print("Number of vertices in largest connected subgraph: " + str(res[0]));


#!/usr/bin/env python3

import json
import requests
import argparse
import codecs
import subprocess
import os
import hashlib
import time
import glob
from slack import WebClient
from slack.errors import SlackApiError
from github import Github

# github actions is having issues calling trufflehog3 on some files due to LFS problems.
# this could be better.
git_lfs_problem_repos = [ 'repo-name'
                        ]

def new_scan(report_path):
#    process = subprocess.Popen(['/usr/local/bin/trufflehog3',
    process = subprocess.Popen(['trufflehog3',
                                '--output', report_path,
                                '--format', 'json',
                                '--no-entropy',
                                '--max-depth=1000',
                                '--line-numbers',
                                '--no-current',
                                './'
                                ],
                           stdout=subprocess.PIPE,
                           universal_newlines=True)

    while True:
        output = process.stdout.readline()
        print(output.strip())
        # Do something else
        return_code = process.poll()
        if return_code is not None:
            print('RETURN CODE', return_code)
            # Process has finished, read rest of the output
            for output in process.stdout.readlines():
                print(output.strip())
            break

def parse_report_for_issues(repo_name, report_path, suppressions_path, ignore_paths, slack_webhook, slack_alert, github_issue):
    filename = report_path
    with open(filename, 'r+') as json_file:
        json_data = json.load(json_file)
        for issue in json_data:
            #print("Entire Issue: {}\n".format(issue))
            message = "New Finding Alert\n"
            message += "--Repo: " + repo_name + "\n"
            message += "--Date: " + json.dumps(issue['date']) + "\n"
            message += "--Path: " + json.dumps(issue['path']) + "\n"
            message += "--Branch: " + json.dumps(issue['branch']) + "\n"
            message += "--Commit Hash: " + json.dumps(issue['commitHash']) + "\n"
            message += "--Commit: " + json.dumps(issue['commit']) + "\n"
            message += "--Reason: " + json.dumps(issue['reason']) + "\n"
            #hack - Some long strings, like javascript, just clog up the console and alerting
            for found_string in issue['stringsFound']:
                if os.path.exists(ignore_paths):
                    file = open(ignore_paths, 'r')
                    file_lines = file.readlines()
                    #count = 0
                    for line in file_lines:
                        #print("Line{}: {}".format(count, line.strip().split(' ', 1)[0]))
                        if json.dumps(issue['path']) == line.strip().split(' ', 1)[0]:
                            print("--String Discovered: Redacted - too long")
                            message += "--String Discovered: Redacted - too long\n"
                        else:
                            print("--String Discovered: " + json.dumps(found_string))
                            message += "--String Discovered: " + json.dumps(found_string) + "\n"
                else:
                    print("--String Discovered: " + json.dumps(found_string) + "\n")
                    message += "--String Discovered: " + json.dumps(found_string) + "\n"
            digest = hashlib.sha256()
            digest.update(str.encode(repo_name) + str.encode(json.dumps(issue['path'])) + str.encode(json.dumps(found_string)))
            issue_title = "secret discovered - " + json.dumps(issue['path'] + " - " + digest.hexdigest() )
            message += "--SHA256: " + digest.hexdigest() + "\n"
            message += "\n To Forever Suppress This Finding From Alerting see https://github.com/netlify/security-netlify-trufflehog3#suppression_file_path \n"
            print(message)
            print("\n")

            # Checking if commitHash or sha256 digest is in suppressions file
            suppressions_matched = "false"
            if os.path.exists(suppressions_path):
                file = open(suppressions_path, 'r')
                file_lines = file.readlines()
                for line in file_lines:
                    #print("Line{}: {}".format(count, line.strip().split(' ', 1)[0]))
                    if json.dumps(issue['commitHash']).strip('\"') == line.strip().split(' ', 1)[0] or digest.hexdigest() == line.strip().split(' ', 1)[0]:
                        suppressions_matched = "true"

            #Suppress notifications of these paths explicitly
            path_matched = "false"
            if os.path.exists(ignore_paths):
                file = open(ignore_paths, 'r')
                file_lines = file.readlines()
                for line in file_lines:
                    #print("Line from file: {}".format(line.strip().split(' ', 1)[0]))
                    #print("Line from report: ", json.dumps(issue['path']).strip('"'))
                    if json.dumps(issue['path']).strip('"') == line.strip().split(' ', 1)[0]:
                        path_matched = "true"

            #If not suppressed, send to slack and/or github
            if suppressions_matched == "false" and path_matched == "false":
                #print("Message: " + message)
                #print("\n")
                if slack_alert == "true":
                    send_slack_alert(slack_webhook, message)
                if github_issue == "true":
                    dedup_and_create_gh_issue(message, issue_title)

    #except:
        #print(" [ERROR] Cannot open file: " + filename)

def get_slack_webhook_from_env() -> str:
    slack_webhook=os.environ.get('SECRET_SCAN_SLACK_WEBHOOK', None)
    if slack_webhook is None:
        raise ValueError(
            'Must provide slack_webhook.'
        )
    return slack_webhook

def get_gh_access_token_from_env() -> str:
    gh_access_token=os.environ.get('SECRET_SCAN_GH_ACCESS_TOKEN', None)
    if gh_access_token is None:
        raise ValueError(
            'Must provide github_access_token.'
        )
    return gh_access_token

def get_repo_from_env() -> str:
    repo_path = os.environ.get('GITHUB_REPO', None)
    if repo_path is None:
        raise ValueError(
               'Must provide repo.',
               'eg. netlify/repo_to_scan'
        )
    return repo_path

def matches_issue_in_repo(repo_path, g, issue_title):
    issues = get_issues_from_repo(repo_path, g)
    for issue in issues:
#        print(f"Issue : {issue.title} \n")
        if issue.title == issue_title:
            print("Issue " + issue.title + " already exists\n")
            issue_matched = "true"
            return issue_matched

def get_issues_from_repo(repo_path, g):
    repo = g.get_repo(repo_path)
    return repo.get_issues()

def dedup_and_create_gh_issue(message, issue_title):
    repo_path = get_repo_from_env()
    print("Repo Path: " + repo_path + "\n")
    gh_access_token = get_gh_access_token_from_env()
    g = Github(gh_access_token)
    issue_matched = matches_issue_in_repo(repo_path, g, issue_title)
    if issue_matched != "true":
        create_gh_issue(message, issue_title, repo_path, g)

def create_gh_issue(message, issue_title, repo_path, g):
    repo = g.get_repo(repo_path)
    i = repo.create_issue(
        title=issue_title,
        body=message,
        labels=[
             "security",
             "security-risk: low",
             "trufflehog"
        ]
    )

def send_slack_alert(slack_webhook, message):
    data = {
        'text': message,
        'username': 'Trufflehog-Bot',
        'icon_emoji': ':boar:'
    }

    response = requests.post(slack_webhook, data=json.dumps(
        data), headers={'Content-Type': 'application/json'})

def main():
    slack_webhook = "Null"
    slack_alert = "false"
    github_issue = "false"
    repo_name = get_repo_from_env()

    parser = argparse.ArgumentParser(description="Trufflehog Secret Scanner")
    parser.add_argument('-r',"--report-path",required=True,default="trufflehog_report.json",help="Location of Required Filepath of Trufflehog Report File to be Parsed")
    parser.add_argument('-p',"--suppressions-path",required=False,default="suppressions-trufflehog",help="Location of Optional Suppressions List File")
    parser.add_argument('-i',"--ignore-paths",required=False,default="ignore-paths-trufflehog",help="Location of Optional Paths-to-Ignore List File")
    parser.add_argument('-g',"--github",required=False,default=False,help="Create a Github Issue for Each Secret Found")
    parser.add_argument('-s',"--slack",required=False,default=False,help="Send a Slack Alert for Each Secret Found")
    args = parser.parse_args()

    if args.slack == "True" or args.slack == "true" or args.slack == "T" or args.slack == "t":
        slack_alert = "true"
    if args.github == "True" or args.github == "true" or args.github == "T" or args.github == "t":
        github_issue = "true"

    if slack_alert == "true":
        slack_webhook = get_slack_webhook_from_env()

    message = "Starting Trufflehog3 Scan and Report Parse\n"
    print(message)
    if slack_alert == "true":
        send_slack_alert(slack_webhook, message)

    new_scan(args.report_path)
    parse_report_for_issues(repo_name, args.report_path, args.suppressions_path, args.ignore_paths, slack_webhook, slack_alert, github_issue)

    message = "Trufflehog3 Scan and Report Parse Complete\n"
    print(message)
    if slack_alert == "true":
        send_slack_alert(slack_webhook, message)

if __name__ == "__main__":
    main()


import locale
from datetime import datetime

import dateparser
from babel.dates import format_date, get_date_format

from discord_birthday_bot import config_util as config

NO_YEAR = 1

STANDARD_LANG = 'en_US'

lang = config.get_language()

# Set locale #
try:
    locale.setlocale(locale.LC_ALL, lang + '.utf8')  # set locale
except locale.Error:
    print('Error: Locale %s not existing.' % lang)
    lang = STANDARD_LANG
    locale.setlocale(locale.LC_ALL, lang + '.utf8')

def get_current_locale():
    """Returns the current locale string without the '.utf8'."""
    return locale.getlocale(locale.LC_ALL)[0]

def parse_to_date(msg):
    """Parses a date string and returns a datetime object."""
    date = dateparser.parse(msg, locales=[get_current_locale()[:2]])  # parse date
    # If is current year, set year to 1 #
    if is_current_year(date):
        date = date.replace(year=NO_YEAR)
    return date

def parse_to_string(date):
    """Formats the date according to the current locale and the existence of the year."""
    date_format = get_date_format(format='long', locale=get_current_locale()).pattern
    if not has_year(date):
        date_format = date_format.replace('y', '').replace(',', '').rstrip()
    return format_date(date, format=date_format, locale=get_current_locale())

def has_year(date):
    """Returns True if the date contains the year, False otherwise."""
    return date.year != NO_YEAR

def is_current_year(date):
    """Returns True if the date is from the current year, False otherwise."""
    return date.year == datetime.now().year

def has_birthday(person):
    """Returns if user has birthday"""
    today = datetime.today()
    birthday = person.birthday
    return today.day == birthday.day and today.month == birthday.month

def get_age(person):
    """Gets age of person"""
    today = datetime.today()
    test = today.year - person.birthday.year - ((today.day, today.month) < (person.birthday.month, person.birthday.day))
    return test


import os, shutil, requests, multiprocessing, collections
from bs4 import BeautifulSoup
from multiprocessing import Pool
from tkinter import *

class MangaReaderNet:
    def __init__(self, lstMangas=None, report=None, pbar=None):
        self.__url = "http://www.mangareader.net/"
        self.__ctrl = "mangareadernet"
        self.__prittyName = "MangaReader"
        self.__imgList = dict()
        self._listMangas = lstMangas
        self.__txtReport = report
        if report:
            self.__outReport("Adapter %s loaded.\n" % self.__prittyName)
        self.__progress = pbar
        return

    def __outReport(self, text):
        self.__txtReport.insert(END, text)
        self.__txtReport.update()
        self.__txtReport.see(END)
        return

    def start(self):
        self.__outReport("Checking OnLine Chapters @%s.\n" % self.__prittyName)
        for key in self._listMangas:
            self.__progress['value'] += 1
            self.__progress.update()
            self._listMangas[key].onlineChapters = self.__checkChapter(self._listMangas[key])
            if len(self._listMangas[key].onlineChapters) > 0:
                self.__outReport("-- Manga '%s' has %d new chapter(s) avail.\n" %
                                 (self._listMangas[key].mangaName, len(self._listMangas[key].onlineChapters)))
                self.__downloadChapters(key)
        return

    def __buildURI(self, manga):
        uri = str(self.__url + re.sub("[ (),]", "-", manga)).lower()
        uri = uri.replace("'","")
        while "--" in uri:
            uri = uri.replace("--", "-")
        return uri

    def checkMangaExist(self, mangaName):
        result = dict()
        result[self.__ctrl] = "#skip"
        uri = self.__buildURI(mangaName)
        response = requests.get(uri)
        if response.status_code == requests.codes.ok:
            result[self.__ctrl] = uri
        return result

    def __checkChapter(self, item):
        resultChapters = dict()
        if self.__ctrl in item.adpMangaURL:
            for key, value in self.__getChapters(item.adpMangaURL[self.__ctrl]).items():
                if key not in item.offlineChapters:
                    resultChapters[key] = value
        else:
            self.__outReport("-- Error: Manga '%s' is not mapped to the adapter.\n" % item.mangaName)
        return resultChapters

    def __getChapters(self,uri):
        result = dict()
        if "#skip" not in uri:
            response = requests.get(uri)
            if response.status_code == requests.codes.ok:
                soup = BeautifulSoup(response.text, "html.parser")
                table = soup.find('table', attrs={'id':'listing'})
                for row in table.findAll('a'):
                    row = str(row).replace('</a>', '').replace('<a ', '').split('>')
                    begin = str(row).find('"') + 2
                    chapter = re.sub('[^0-9.]+', '', row[1])
                    if "." in chapter:
                        sanity = chapter.split(".")
                        if len(sanity[0]) == 0:
                            chapter = sanity[1]
                    result["Chapter 000"[:-len(chapter)] + chapter] = self.__url + str(row)[begin:str(row).find('"', begin)]
        return result

    def __downloadChapters(self, mangaKey):
        if len(self._listMangas[mangaKey].onlineChapters) == 0:
            return
        else:
            od = collections.OrderedDict(sorted(self._listMangas[mangaKey].onlineChapters.items()))
            self.__imgList.clear()
            for key, value in od.items():
                self.__outReport("---- Get pages for %s > " % key)
                key = os.path.normpath(os.path.join(self._listMangas[mangaKey].savePath, self._listMangas[mangaKey].mangaName, key))
                self.__imgList = self._downChapter(key, value)
                self.__outReport("%s pages.\n" % len(self.__imgList))
                if len(self.__imgList) > 0:
                    for key, value in self.__imgList.items():
                        self._savePage(key, value)

            # pool = Pool(processes=16)  # between 16 to 20 simultaneously downloads seems to be possible
            # pool.map(self._savePage,self.__imgList.items())
            # pool.close()
            # pool.join()
        return

    def _downChapter(self, *kwargs):
        imgList = dict()
        uri = kwargs[1]
        fDir = kwargs[0]
        response = requests.get(uri)
        if response.status_code == requests.codes.ok:
            soup = BeautifulSoup(response.text, "html.parser")
            table = soup.find('select', attrs={'id':'pageMenu'})
            pages = list()
            for row in table.findAll('option'):
                first = str(row).find("\"") + 1
                pages.append(str(row)[first:str(row).find("\"", first)])
            count = 0
            for page in pages:
                count += 1
                if page == "selected":
                    img = str(soup.find('div', attrs={'id':'imgholder'}))
                    start = img.find("src=\"") + 5
                    end = img.find("\"", start)
                    img = img[start:end]
                    imgList[fDir + "\\" + str("0000" + str(count))[-3:] + img[-4:]] = img
                else:
                    response = requests.get(self.__url[:-1] + page)
                    if response.status_code == requests.codes.ok:
                        soup = BeautifulSoup(response.text, "html.parser")
                        img = str(soup.find('div', attrs={'id':'imgholder'}))
                        start = img.find("src=\"") + 5
                        end = img.find("\"", start)
                        img = img[start:end]
                        imgList[fDir + "\\" + str("0000" + str(count))[-3:] + img[-4:]] = img
        del response
        if len(imgList) > 0:
            if os.path.exists(fDir):
                shutil.rmtree(fDir)
            else:
                os.makedirs(fDir)
        return imgList

    def _savePage(self, *kwargs):
        fname = kwargs[0]
        uri = kwargs[1]
        try:
            os.makedirs(fname[:fname.rfind('\\')])
        except:
            pass
        try:
            with open(fname, 'wb') as handle:
                response = requests.get(uri, stream=True)
                if response.ok:
                    for block in response.iter_content(1024):
                        if not block:
                            break
                        handle.write(block)
                del response
                handle.close()
        except:
            if os.path.exists(fname):
                os.remove(fname)
            try:
                with open(fname, 'wb') as handle:
                    response = requests.get(uri, stream=True)
                    if response.ok:
                        for block in response.iter_content(1024):
                            if not block:
                                break
                            handle.write(block)
                    del response
                    handle.close()
            except:
                pass
        finally:
            pass
        return


import os
import sys
import importlib
from textwrap import dedent

import pytest

from .._setup_helpers import get_extensions
from . import reset_distutils_log  # noqa
from . import cleanup_import, run_setup

extension_helpers_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))  # noqa

def teardown_module(module):
    # Remove file generated by test_generate_openmp_enabled_py but
    # somehow needed in test_cython_autoextensions
    tmpfile = 'openmp_enabled.py'
    if os.path.exists(tmpfile):
        os.remove(tmpfile)

def _extension_test_package(tmpdir, request, extension_type='c',
                            include_numpy=False):
    """Creates a simple test package with an extension module."""

    test_pkg = tmpdir.mkdir('test_pkg')
    test_pkg.mkdir('apyhtest_eva').ensure('__init__.py')

    # TODO: It might be later worth making this particular test package into a
    # reusable fixture for other build_ext tests

    if extension_type in ('c', 'both'):
        # A minimal C extension for testing
        test_pkg.join('apyhtest_eva', 'unit01.c').write(dedent("""\
            #include <Python.h>

            static struct PyModuleDef moduledef = {
                PyModuleDef_HEAD_INIT,
                "unit01",
                NULL,
                -1,
                NULL
            };
            PyMODINIT_FUNC
            PyInit_unit01(void) {
                return PyModule_Create(&moduledef);
            }
        """))

    if extension_type in ('pyx', 'both'):
        # A minimal Cython extension for testing
        test_pkg.join('apyhtest_eva', 'unit02.pyx').write(dedent("""\
            print("Hello cruel angel.")
        """))

    if extension_type == 'c':
        extensions = ['unit01.c']
    elif extension_type == 'pyx':
        extensions = ['unit02.pyx']
    elif extension_type == 'both':
        extensions = ['unit01.c', 'unit02.pyx']

    include_dirs = ['numpy'] if include_numpy else []

    extensions_list = [
        "Extension('apyhtest_eva.{0}', [join('apyhtest_eva', '{1}')], include_dirs={2})".format(
            os.path.splitext(extension)[0], extension, include_dirs)
        for extension in extensions]

    test_pkg.join('apyhtest_eva', 'setup_package.py').write(dedent("""\
        from setuptools import Extension
        from os.path import join
        def get_extensions():
            return [{0}]
    """.format(', '.join(extensions_list))))

    test_pkg.join('setup.py').write(dedent("""\
        import sys
        from os.path import join
        from setuptools import setup, find_packages
        sys.path.insert(0, r'{extension_helpers_path}')
        from extension_helpers import get_extensions

        setup(
            name='apyhtest_eva',
            version='0.1',
            packages=find_packages(),
            ext_modules=get_extensions()
        )
    """.format(extension_helpers_path=extension_helpers_PATH)))

    if '' in sys.path:
        sys.path.remove('')

    sys.path.insert(0, '')

    def finalize():
        cleanup_import('apyhtest_eva')

    request.addfinalizer(finalize)

    return test_pkg

@pytest.fixture
def extension_test_package(tmpdir, request):
    return _extension_test_package(tmpdir, request, extension_type='both')

@pytest.fixture
def c_extension_test_package(tmpdir, request):
    # Check whether numpy is installed in the test environment
    has_numpy = bool(importlib.util.find_spec('numpy'))
    return _extension_test_package(tmpdir, request, extension_type='c',
                                   include_numpy=has_numpy)

@pytest.fixture
def pyx_extension_test_package(tmpdir, request):
    return _extension_test_package(tmpdir, request, extension_type='pyx')

def test_cython_autoextensions(tmpdir):
    """
    Regression test for https://github.com/astropy/astropy-helpers/pull/19

    Ensures that Cython extensions in sub-packages are discovered and built
    only once.
    """

    # Make a simple test package
    test_pkg = tmpdir.mkdir('test_pkg')
    test_pkg.mkdir('yoda').mkdir('luke')
    test_pkg.ensure('yoda', '__init__.py')
    test_pkg.ensure('yoda', 'luke', '__init__.py')
    test_pkg.join('yoda', 'luke', 'dagobah.pyx').write(
        """def testfunc(): pass""")

    # Required, currently, for get_extensions to work
    ext_modules = get_extensions(str(test_pkg))

    assert len(ext_modules) == 2
    assert ext_modules[0].name == 'yoda.luke.dagobah'

def test_compiler_module(capsys, c_extension_test_package):
    """
    Test ensuring that the compiler module is built and installed for packages
    that have extension modules.
    """

    test_pkg = c_extension_test_package
    install_temp = test_pkg.mkdir('install_temp')

    with test_pkg.as_cwd():
        # This is one of the simplest ways to install just a package into a
        # test directory
        run_setup('setup.py',
                  ['install',
                   '--single-version-externally-managed',
                   '--install-lib={0}'.format(install_temp),
                   '--record={0}'.format(install_temp.join('record.txt'))])

    with install_temp.as_cwd():
        import apyhtest_eva
        # Make sure we imported the apyhtest_eva package from the correct place
        dirname = os.path.abspath(os.path.dirname(apyhtest_eva.__file__))
        assert dirname == str(install_temp.join('apyhtest_eva'))

        import apyhtest_eva.compiler_version
        assert apyhtest_eva.compiler_version != 'unknown'


# pygame.key.get_pressed()
# pressed=pygame.key.get_pressed()
# pressed[pygame.K_UP]

import pygame
pygame.init()

screen=pygame.display.set_mode((700,500))
pygame.display.set_caption("Rectacle moving")
done=False
is_blue=True
x,y=50,50
a=3
clock=pygame.time.Clock()

while not done:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            done=True
        if event.type==pygame.KEYDOWN and event.key==pygame.K_SPACE:
            is_blue=not is_blue

    pressed=pygame.key.get_pressed()

    if pressed[pygame.K_UP]:
        y-=a
    if pressed[pygame.K_DOWN]:
        y+=a
    if pressed[pygame.K_LEFT]:
        x-=a
    if pressed[pygame.K_RIGHT]:
        x+=a

    if is_blue:
        color=(0,100,255)
    else:
        color=(255,50,0)

    screen.fill((0,0,0))
    pygame.draw.rect(screen,color,pygame.Rect(x,y,50,50))
    pygame.display.flip()
    clock.tick(60)

pygame.quit()


from myutils import *
import gym
import numpy as np
import scipy

class AC(RLAgent):
    def __init__(self,env,gamma=1.):
        super().__init__(env=env)
        self.gamma=gamma
        self.v=None
        self.q=None
        self.env.nS=self.env.observation_space.n
        self.env.nA=self.env.action_space.n

    def decide(self,state,train=False):
        return np.argmax(self.q[state])

    def evaluate(self,policy):
        # identity transition minus heuristics transition
        A=np.eye(self.env.nS)
        # rewards
        B=np.zeros((self.env.nS))
        # for each transition, update a and b accordingly
        for s in range(self.env.nS-1):
            for a in range(env.nA):
                pa=policy[s][a]
                for ps1, s1, reward, done in env.P[s][a]:
                    A[s,s1]-=self.gamma*pa*ps1
                    B[s]+=reward*pa*ps1
        # state values
        v=scipy.linalg.solve(A,B)
        # action values
        q=np.zeros((self.env.nS,self.env.nA))
        # for each transition, update q accordingly
        for s in range(self.env.nS-1):
            for a in range(env.nA):
                for ps1, s1, reward, done in env.P[s][a]:
                    q[s,a]+=ps1*(reward+self.gamma*v[s1])
        return v,q

    def learn(self,state=None,action=None,reward=None,new_state=None,new_action=None,done=None):
        print(f"agent learning by solving bellman equation of {self.env.nS} variables...")
        # transition prob. matrix
        p=np.zeros((self.env.nS,self.env.nA,self.env.nS))
        # reward matrix
        r=np.zeros((self.env.nS,env.nA))
        # populate p and r
        for s1 in range(self.env.nS-1):
            for a in range(env.nA):
                for ps2, s2, reward, done in env.P[s1][a]:
                    p[s1][a][s2]=ps2
                    r[s1][a]+=reward*ps2
        c=np.ones(self.env.nS)
        # negative of A in evaluate expanded along actions
        a_ub=self.gamma*p.reshape(-1,self.env.nS) \
             - np.repeat(np.eye(self.env.nS),env.nA,axis=0)
        # negative of B in evaluate expanded along actions
        b_ub=-r.reshape(-1)
        a_eq=np.zeros((0,self.env.nS))
        b_eq=np.zeros(0)
        bounds=[(None, None)]*self.env.nS
        sln=scipy.optimize.linprog(c,a_ub,b_ub,a_eq,b_eq,bounds=bounds,method='interior-point')
        v=sln.x
        q=r+self.gamma*np.dot(p,v)
        self.v,self.q=v,q

if __name__=='__main__':

    np.set_printoptions(precision=3)
    name='CliffWalking-v0'
    env=bootstrap(name,render=True)

    agent=Bellman(env)
    bellman_agent=solve(agent,env,0,0,1)
    bellman_policy=np.copy(bellman_agent.q)
    print(sep)
    bellman_actions=np.argmax(bellman_policy,axis=-1).reshape(env.shape)
    print(f"bellman actions: \n{bellman_actions}")

    print(sep)
    random_policy=np.ones((env.nS,env.nA))/env.nA
    state_values, action_values=agent.evaluate(random_policy)
    state_values=state_values.reshape(env.shape)
    action_values=np.argmax(action_values,axis=1).reshape(env.shape)
    print(f"random policy state values:\n{state_values}")
    print(f"random policy action values:\n{action_values}")

    print(sep)
    perfect_moves=np.ones(env.observation_space.n,dtype=int).reshape(env.shape)
    perfect_moves[-1,:]=0 #at bottom row, move up
    perfect_moves[:,-1]=2 #at rightmost column, move down
    print(f"perfect moves:\n{perfect_moves}")
    perfect_policy=np.eye(env.action_space.n)[perfect_moves.reshape(-1)]
    state_values, action_values=agent.evaluate(perfect_policy)
    state_values=state_values.reshape(env.shape)
    action_values=np.argmax(action_values,axis=-1).reshape(env.shape)
    print(f"perfect policy state values:\n{state_values}")
    print(f"perfect policy action values:\n{action_values}")


#!/usr/bin/env python
#encoding: utf8
#-----------------------------------------------------------------------------
#cam_test.py
#Last update	2020/09/03
#Copyright 2020 Shinmei Industry All rights reserved.
#-----------------------------------------------------------------------------
import rospy,cv2,math
import numpy as np
from sensor_msgs.msg import Image,CameraInfo
from cv_bridge import CvBridge, CvBridgeError

def main():
    # 入力画像の取得
    im = cv2.imread("/home/shinmei/ピクチャ/aoi_pic/circle.jpg")

    # グレースケール変換
    gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)

    # 2値化
    gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

    # ラベリング処理
    label = cv2.connectedComponentsWithStats(gray)

    # ブロブ情報を項目別に抽出
    n = label[0] - 1
    data = np.delete(label[2], 0, 0)
    center = np.delete(label[3], 0, 0)

    # ブロブ面積最大のインデックス
    max_index = np.argmax(data[:,4])

    # 面積最大ブロブの各種情報を表示
    print("外接矩形の左上x座標", data[:,0][max_index])
    print("外接矩形の左上y座標", data[:,1][max_index])
    print("外接矩形の幅", data[:,2][max_index])
    print("外接矩形の高さ", data[:,3][max_index])
    print("面積", data[:,4][max_index])
    print("中心座標:\n",center[max_index])

if __name__ == '__main__':
    main()