#!/usr/bin/env python
#
# Electrum - Lightweight Bitcoin Client
# Copyright (C) 2015 Thomas Voegtlin
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import os
import json
import stat

from datetime import datetime

from enum import IntEnum

from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtCore import Qt, QItemSelectionModel
from PyQt5.QtWidgets import QPushButton, QVBoxLayout, QLabel, QGridLayout, QLineEdit

from electrum.gui.qt.util import Buttons, CloseButton, OkButton, HelpLabel, WindowModalDialog, MyTreeView, read_QIcon

from electrum.crypto import sha256

from electrum.plugin import BasePlugin, hook
from electrum.i18n import _

from electrum.util import user_dir, bh2u
from electrum.util import PR_TYPE_ONCHAIN, PR_TYPE_LN

def read_schedule_file():
    schedule_path = os.path.join(user_dir(), 'schedule')
    if not os.path.exists(schedule_path):
        return {}
    try:
        with open(schedule_path, "r", encoding='utf-8') as f:
            data = f.read()
        result = json.loads(data)
    except:
        return {}
    if not type(result) is dict:
        return {}
    return result

def write_schedule_file(schedule_dict):
    schedule_path = os.path.join(user_dir(), 'schedule')
    s = json.dumps(schedule_dict, indent=4, sort_keys=True)
    try:
        with open(schedule_path, "w", encoding='utf-8') as f:
            f.write(s)
        os.chmod(schedule_path, stat.S_IREAD | stat.S_IWRITE)
    except FileNotFoundError:
        if os.path.exists(schedule_path):
            raise

ROLE_REQUEST_TYPE = Qt.UserRole
ROLE_REQUEST_ID = Qt.UserRole + 1

class CronPaymentsList(MyTreeView):

    class Columns(IntEnum):
        ADDRESS = 0
        AMOUNT = 1
        DESCRIPTION = 2
        LAST_TIME = 3
        NEXT_TIME = 4

    headers = {
        Columns.ADDRESS: _('Address'),
        Columns.AMOUNT: _('Amount'),
        Columns.DESCRIPTION: _('Description'),
        Columns.LAST_TIME: _('Last payment'),
        Columns.NEXT_TIME: _('Next payment'),
    }
    filter_columns = [Columns.LAST_TIME, Columns.NEXT_TIME, Columns.AMOUNT]

    def __init__(self, parent):
        super().__init__(parent, lambda: None,
                         stretch_column=self.Columns.DESCRIPTION,
                         editable_columns=[])
        self.setMinimumSize(900, 200)
        self.setSortingEnabled(True)
        self.setModel(QStandardItemModel(self))
        self.update()

    def update(self):
        current_schedule = read_schedule_file()
        _list = current_schedule.get('items', [])
        self.model().clear()
        self.update_headers(self.__class__.headers)
        for idx, cron_item in enumerate(_list):
            item = cron_item['invoice']
            schedule = cron_item['schedule']
            invoice_type = item['type']
            if invoice_type == PR_TYPE_LN:
                key = item['rhash']
                icon_name = 'lightning.png'
            elif invoice_type == PR_TYPE_ONCHAIN:
                key = bh2u(sha256(repr(item))[0:16])
                icon_name = 'bitcoin.png'
                if item.get('bip70'):
                    icon_name = 'seal.png'
            else:
                raise Exception('Unsupported type')
            address_str = item['outputs'][0][1]
            message = item['message']
            amount = item['amount']
            amount_str = self.parent.format_amount(amount, whitespaces=True)
            last_str = ''
            next_str = ''
            try:
                from croniter import croniter
                cron = '{minute} {hour} {day} {month} {week}'.format(**schedule)
                item = croniter(cron, datetime.now())
                next_str = item.get_next(datetime).isoformat()
            except:
                pass
            labels = [address_str, amount_str, message, last_str, next_str, ]
            items = [QStandardItem(e) for e in labels]
            self.set_editability(items)
            items[self.Columns.ADDRESS].setIcon(read_QIcon(icon_name))
            items[self.Columns.ADDRESS].setData(key, role=ROLE_REQUEST_ID)
            items[self.Columns.ADDRESS].setData(invoice_type, role=ROLE_REQUEST_TYPE)
            self.model().insertRow(idx, items)
        self.selectionModel().select(self.model().index(0,0), QItemSelectionModel.SelectCurrent)
        self.filter()

class CronPaymentsDialog(WindowModalDialog):

    def __init__(self, parent, title=None):
        WindowModalDialog.__init__(self, parent, title=title)
        vbox = QVBoxLayout(self)
        cron_list = CronPaymentsList(parent)
        vbox.addWidget(cron_list)
        vbox.addStretch()
        vbox.addLayout(Buttons(QPushButton('Edit'), QPushButton('Delete'), ))
        vbox.addStretch()
        vbox.addLayout(Buttons(CloseButton(self), ))

class ScheduleDialog(WindowModalDialog):

    def __init__(self, main_win, outputs):
        WindowModalDialog.__init__(self, main_win, title=_("New scheduled payment"),)
        vbox = QVBoxLayout(self)
        self.setMinimumSize(400, 150)
        minute_label = HelpLabel(_('minute:'), _('''
Defines a specific minute for an hour.
Allowed characters:
"*" any value
"," value list separator
"-" range of values
"/" step values
0-59 allowed values
'''))
        hour_label = HelpLabel(_('hour:'), _('''
Defines a specific hour during the day.
Allowed characters:
"*" any value
"," value list separator
"-" range of values
"/" step values
0-23 allowed values
'''))
        day_label = HelpLabel(_('day of month:'), _('''
Defines the day number for the month.
Allowed characters:
"*" any value
"," value list separator
"-" range of values
"/" step values
0-31 allowed values
'''))
        month_label = HelpLabel(_('month:'), _('''
Defines a month during the year.
Allowed characters:
"*" any value
"," value list separator
"-" range of values
"/" step values
1-12 allowed values
'''))
        week_label = HelpLabel(_('day of week:'), _('''
Specifies the day of the week.
Allowed characters:
"*" any value
"," value list separator
"-" range of values
"/" step values
0-6 allowed values
'''))
        self.minute_edit = QLineEdit('*')
        self.minute_edit.setFixedWidth(30)
        self.minute_edit.textChanged.connect(self.update_schedule_details)
        self.hour_edit = QLineEdit('*')
        self.hour_edit.setFixedWidth(30)
        self.hour_edit.textChanged.connect(self.update_schedule_details)
        self.day_edit = QLineEdit('*')
        self.day_edit.setFixedWidth(30)
        self.day_edit.textChanged.connect(self.update_schedule_details)
        self.month_edit = QLineEdit('*')
        self.month_edit.setFixedWidth(30)
        self.month_edit.textChanged.connect(self.update_schedule_details)
        self.week_edit = QLineEdit('*')
        self.week_edit.setFixedWidth(30)
        self.week_edit.textChanged.connect(self.update_schedule_details)
        grid = QGridLayout()
        grid.addWidget(minute_label, 0, 0, alignment=Qt.AlignCenter)
        grid.addWidget(hour_label, 0, 1, alignment=Qt.AlignCenter)
        grid.addWidget(day_label, 0, 2, alignment=Qt.AlignCenter)
        grid.addWidget(month_label, 0, 3, alignment=Qt.AlignCenter)
        grid.addWidget(week_label, 0, 4, alignment=Qt.AlignCenter)
        grid.addWidget(self.minute_edit, 1, 0, alignment=Qt.AlignCenter)
        grid.addWidget(self.hour_edit, 1, 1, alignment=Qt.AlignCenter)
        grid.addWidget(self.day_edit, 1, 2, alignment=Qt.AlignCenter)
        grid.addWidget(self.month_edit, 1, 3, alignment=Qt.AlignCenter)
        grid.addWidget(self.week_edit, 1, 4, alignment=Qt.AlignCenter)
        every_day = QPushButton('every day')
        every_day.clicked.connect(lambda: self.set_as_text('0 0 * * *'))
        every_week = QPushButton('every week')
        every_week.clicked.connect(lambda: self.set_as_text('0 0 * * 0'))
        every_month = QPushButton('every month')
        every_month.clicked.connect(lambda: self.set_as_text('0 0 1 * *'))
        every_year = QPushButton('every year')
        every_year.clicked.connect(lambda: self.set_as_text('0 0 1 1 *'))
        self.schedule_details = QLabel('')
        vbox.addLayout(grid)
        vbox.addLayout(Buttons(every_day, every_week, every_month, every_year))
        vbox.addWidget(self.schedule_details)
        vbox.addStretch()
        vbox.addLayout(Buttons(CloseButton(self), OkButton(self)))
        self.update_schedule_details()

    def set_as_text(self, value):
        items = value.split(' ')
        self.minute_edit.setText(items[0])
        self.minute_edit.repaint()
        self.hour_edit.setText(items[1])
        self.hour_edit.repaint()
        self.day_edit.setText(items[2])
        self.day_edit.repaint()
        self.month_edit.setText(items[3])
        self.month_edit.repaint()
        self.week_edit.setText(items[4])
        self.week_edit.repaint()
        self.update_schedule_details()

    def update_schedule_details(self):
        try:
            from croniter import croniter
            item = croniter(self.as_text(), datetime.now())
            t = item.get_next(datetime)
        except:
            self.schedule_details.setText('')
            return
        self.schedule_details.setText('new payment expected on:\n%s' % t.strftime("%A, %d %B %Y at %I:%M%p"))

    def as_text(self):
        return '%s %s %s %s %s' % (
            self.minute_edit.text(),
            self.hour_edit.text(),
            self.day_edit.text(),
            self.month_edit.text(),
            self.week_edit.text(),
        )

    def as_dict(self):
        return {
            'minute': self.minute_edit.text(),
            'hour': self.hour_edit.text(),
            'day': self.day_edit.text(),
            'month': self.month_edit.text(),
            'week': self.week_edit.text(),
        }

class ScheduleButton(QPushButton):

    def __init__(self, main_window):
        QPushButton.__init__(self, _("Schedule"))
        self.clicked.connect(lambda: self._on_clicked(main_window))

    def _on_clicked(self, main_win):
        outputs = main_win.read_outputs()
        if main_win.check_send_tab_outputs_and_show_errors(outputs):
            return
        d = ScheduleDialog(main_win, outputs)
        d.show()
        if not d.exec_():
            return
        invoice = main_win.read_invoice()
        if not invoice:
            return
        current_schedule = read_schedule_file()
        if 'items' not in current_schedule:
            current_schedule['items'] = []

        current_schedule['items'].append({
            'invoice': invoice,
            'schedule': d.as_dict(),
        })
        write_schedule_file(current_schedule)
        main_win.do_clear()
        main_win.show_message(_('New automatic payment scheduled successfully'))

class Plugin(BasePlugin):

    def fullname(self):
        return 'Cron Payments'

    def description(self):
        return _("Automated scheduled BitCoin payments")

    def is_available(self):
        return True

    def __init__(self, parent, config, name):
        BasePlugin.__init__(self, parent, config, name)
        self.start_scheduler()

    def on_cron_payments_menu_clicked(self, window, tools_menu):
        d = CronPaymentsDialog(window, _("Scheduled payments"))
        d.show()

    @hook
    def init_menubar_tools(self, main_window, tools_menu):
        tools_menu.addSeparator()
        tools_menu.addAction(_("&Scheduled payments"), lambda: self.on_cron_payments_menu_clicked(main_window, tools_menu))
        buttons = main_window.send_grid.itemAtPosition(6, 1)
        schedule_button = ScheduleButton(main_window)
        buttons.addWidget(schedule_button)

    def execute_payment(self):
        # TODO: to be continue
        pass

    def start_scheduler(self):
        # TODO: to be continue
        pass


from __future__ import absolute_import

import doctest
import unittest
from data_index.controllers import datasets_controller
from data_index.models.dataset import Dataset
from data_index.models.list_datasets_response import ListDatasetsResponse
from data_index.test import BaseTestCase
from six import BytesIO
from flask import json

class TestDatasetsController(BaseTestCase):
    """Datasets tests."""

    def test_list_datasets(self):
        query_string = [('pageSize', 56), ('pageToken', 'pageToken_example')]
        response = self.client.open(
            '/v1/datasets',
            method='GET',
            content_type='application/json',
            query_string=query_string)
        self.assertStatus(response, 501,
                          "Response body is : " + response.data.decode('utf-8'))

    def test_create_dataset(self):
        ds = Dataset(name='datasets/foo')
        self.must_create_dataset(ds)

        got = self.must_get_dataset(ds.name)
        self.assertEqual(ds, got)

    def test_create_dataset_duplicate(self):
        ds = Dataset(name='datasets/foo')
        self.must_create_dataset(ds)

        resp = self.client.open(
            '/v1/datasets',
            method='POST',
            data=json.dumps(ds),
            content_type='application/json')
        self.assertStatus(resp, 409)

    def test_get_dataset_not_found(self):
        resp = self.client.open('/v1/datasets/foo', method='GET')
        self.assertStatus(resp, 404)

    def test_update_dataset(self):
        ds = Dataset(name='datasets/foo')
        self.must_create_dataset(ds)

        resp = self.client.open(
            '/v1/' + ds.name,
            method='PUT',
            data=json.dumps(ds),
            content_type='application/json')
        self.assertStatus(resp, 200)

    def test_update_dataset_not_found(self):
        resp = self.client.open(
            '/v1/datasets/notfound',
            method='PUT',
            data=json.dumps({
                'name': 'datasets/notfound'
            }),
            content_type='application/json')
        self.assertStatus(resp, 404)

    def test_update_dataset_mismatch_names(self):
        ds = Dataset(name='datasets/foo')
        self.must_create_dataset(ds)

        resp = self.client.open(
            '/v1/' + ds.name,
            method='PUT',
            data=json.dumps({
                'name': 'datasets/bar'
            }),
            content_type='application/json')
        self.assertStatus(resp, 400)

    def test_delete_dataset(self):
        ds = Dataset(name='datasets/foo')
        ds_untouched = Dataset(name='datasets/untouched')
        self.must_create_dataset(ds)
        self.must_create_dataset(ds_untouched)

        resp = self.client.open('/v1/' + ds.name, method='DELETE')
        self.assertStatus(resp, 200)
        resp = self.client.open('/v1/' + ds.name, method='GET')
        self.assertStatus(resp, 404)
        self.must_get_dataset(ds_untouched.name,
                              "unrelated dataset was deleted")

    def test_delete_dataset_not_found(self):
        ds_untouched = Dataset(name='datasets/untouched')
        self.must_create_dataset(ds_untouched)

        resp = self.client.open('/v1/datasets/notfound', method='DELETE')
        self.assertStatus(resp, 404)
        self.must_get_dataset(ds_untouched.name,
                              "unrelated dataset was deleted")

class TestExamplesInDocstrings(unittest.TestCase):
    """Test examples in doc strings."""

    def test_doctest(self):
        result = doctest.testmod(datasets_controller, report=True)
        self.assertEqual(0, result.failed)

if __name__ == '__main__':
    import unittest
    unittest.main()


import wx
import os
import sys
import threading
import subprocess
import time
import shutil

from defs import *
import command_fnc
import mod_cmd_info
import mod_cmd_log
import mod_cmd_status
import mod_cmd_commit

globals = {
'session':'',
'snapshot':'',
}

[
 wxID_frmMain,
 wxID_frmMain_pnlMain,
 wxID_frmMain_txtHeader,
 wxID_frmMain_txtConsole,
 wxID_frmMain_btnInfo,
 wxID_frmMain_btnLog,
 wxID_frmMain_btnStatus,
 wxID_frmMain_btnCommit,

 wxID_frmMain_pnlData,
 wxID_frmMain_txtData,
]  = [wx.NewId() for _init_ctrls in range(10)]

class CMainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, id=wxID_frmMain, parent=None, pos=wx.Point(0, 0), size=wx.Size(1000, 800), style=wx.DEFAULT_FRAME_STYLE, title='boar-gui')

        self.Splitter = wx.SplitterWindow(self, -1)

        # TOP
        self.pnlTop = wx.Panel(id=-1, parent=self.Splitter)
        self.szrTop = wx.BoxSizer(orient=wx.VERTICAL)
        self.pnlTop.SetSizer(self.szrTop)
        self.mod = ''

        # BOT
        self.pnlBot     = wx.Panel(id=-1, parent=self.Splitter)
        self.txtConsole = wx.TextCtrl(parent=self.pnlBot, id=wxID_frmMain_txtConsole, style=wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL|wx.TE_WORDWRAP)

        # Buttons
        self.ckbConsole  = wx.CheckBox(  id=-1,                      parent=self.pnlBot, label='(console)    ')
        self.btnInfo     = wx.Button(    id=wxID_frmMain_btnInfo,    parent=self.pnlBot, label='Info')
        self.btnLog      = wx.Button(    id=wxID_frmMain_btnLog,     parent=self.pnlBot, label='Log')
        self.btnStatus   = wx.Button(    id=wxID_frmMain_btnStatus,  parent=self.pnlBot, label='Status')
        self.btnCommit   = wx.Button(    id=wxID_frmMain_btnCommit,  parent=self.pnlBot, label='Commit')

        self.szButtons = wx.BoxSizer(orient=wx.HORIZONTAL)
        self.szButtons.Add(self.ckbConsole)
        self.szButtons.Add(self.btnInfo)
        self.szButtons.Add(self.btnLog)
        self.szButtons.Add(self.btnStatus)
        self.szButtons.Add(self.btnCommit)

        self.szrBot = wx.BoxSizer(orient=wx.VERTICAL)
        self.szrBot.AddWindow(self.szButtons,  0, wx.ALL | wx.EXPAND)
        self.szrBot.AddWindow(self.txtConsole, 1, wx.ALL | wx.EXPAND)
        self.pnlBot.SetSizer(self.szrBot)

        # ALL
        self.Splitter.SplitHorizontally(self.pnlTop , self.pnlBot, 600)
        self.Layout()

        # Binds
        self.Bind(wx.EVT_BUTTON, self.OnBtnInfo,   self.btnInfo)
        self.Bind(wx.EVT_BUTTON, self.OnBtnLog,    self.btnLog)
        self.Bind(wx.EVT_BUTTON, self.OnBtnStatus, self.btnStatus)
        self.Bind(wx.EVT_BUTTON, self.OnBtnCommit, self.btnCommit)

    def OnBtnInfo(self, event):
        mod_cmd_info.Open_Info(self)

        command_fnc.Command('boar info', self.ckbConsole.IsChecked(), self.OnData_Info)
        command_fnc.Command('boar --version', self.ckbConsole.IsChecked(), self.OnData_Info)
        pass

    def OnBtnLog(self, event):
        mod_cmd_log.Open_Log(self)
        command_fnc.ThreadCommand('boar log', self.ckbConsole.IsChecked(), self.OnData_Log)
        pass

    def OnBtnStatus(self, event):
        mod_cmd_status.Open_Status(self)
        command_fnc.ThreadCommand('boar status', self.ckbConsole.IsChecked(), self.OnData_Status)
        pass

    def OnBtnCommit(self, event):
        mod_cmd_commit.Open_Commit(self)
        command_fnc.ThreadCommand('boar ci --message=\"boar-gui\"', self.ckbConsole.IsChecked(), self.OnData_Commit)
        pass

    def OnData_Info(self, line, mode, var={}):
        global globals
        return mod_cmd_info.OnData_Info(self, line, mode, globals, var)

    def OnData_Log(self, line, mode, var={}):
        global globals
        return mod_cmd_log.OnData_Log(self, line, mode, globals, var)

    def OnData_Status(self, line, mode, var={}):
        global globals
        return mod_cmd_status.OnData_Status(self, line, mode, globals, var)

    def OnData_Commit(self, line, mode, var={}):
        global globals
        return mod_cmd_commit.OnData_Commit(self, line, mode, globals, var)

if __name__ == '__main__':
    app = wx.PySimpleApp() #redirect=False
    frame = CMainFrame()
    frame.Show()
    app.SetTopWindow(frame)

    command_fnc.RedirectText(frame.txtConsole)

    frame.OnBtnInfo(None)

    app.MainLoop()


# -*- coding: utf-8
from openpyxl.reader.excel import load_workbook
from torcms.model.minforcatalog import MInforCatalog

wb = load_workbook(filename='./database/meta/info_tags.xlsx')
sheet_ranges_arr = [wb['Sheet1'], wb['Sheet2']]
class_arr = ['D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T']
sig_name_arr = []
mappcat = MInforCatalog()

def uu():
    # 在分类中排序
    order_index = 1
    # 父类索引
    papa_index = 1
    # 子类索引
    c_index = 1
    papa_id = 0
    uid = ''
    p_dic = {}
    # 逐行遍历
    for sheet_ranges in sheet_ranges_arr:

        for row_num in range(3, 48):
            # 父类
            p_cell_val = sheet_ranges['A{0}'.format(row_num)].value
            if p_cell_val and p_cell_val != '':
                cell_arr = p_cell_val.split(',')
                p_uid = cell_arr[0].strip().strip('t')
                priv_mask = cell_arr[1].strip().strip('t')
                t_name_arr = sheet_ranges['B{0}'.format(row_num)].value.strip().split(',')
                u_uid = '{0}00'.format(p_uid)

            # 子类
            b_cell_val = sheet_ranges['B{0}'.format(row_num)].value
            c_cell_val = sheet_ranges['C{0}'.format(row_num)].value
            if c_cell_val and c_cell_val != '':
                cell_arr = b_cell_val.split(',')
                c_iud = cell_arr[0].strip().strip('t')
                priv_mask = cell_arr[1].strip().strip('t')
                t_name_arr = c_cell_val.strip().split(',')
                u_uid = '{0}{1}'.format(p_uid, c_iud)
            post_data = {
                'name': [t_name_arr[0]],
                'slug': [t_name_arr[1]],
                'order': [order_index],
                'uid': [u_uid],
                'priv_mask': [priv_mask],
            }
            print(post_data)
            mappcat.insert_data(u_uid, post_data)
            order_index = order_index + 1

if __name__ == '__main__':
    uu()


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

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^quawazu/$', views.question_list, name='question_list'),
    url(r'^quawazu/info/$', views.info, name='info'),
    url(r'^quawazu/help/$', views.help, name='help'),
    url(r'^quawazu/add/$', views.question_edit, name='question_add'),
    url(r'^quawazu/mod/(?P<question_id>\d+)/$', views.question_edit,
        name='question_mod'),
    url(r'^quawazu/del/(?P<question_id>\d+)/$', views.question_del,
        name='question_del'),
    # choices
    url(r'^choice/(?P<question_id>\d+)/$', views.ChoiceList.as_view(),
        name='choice_list'),
    url(r'^choice/add/(?P<question_id>\d+)/$', views.choice_edit,
        name='choice_add'),
    url(r'^choice/mod/(?P<question_id>\d+)/(?P<choice_id>\d+)/$',
        views.choice_edit, name='choice_mod'),
    url(r'^choice/del/(?P<question_id>\d+)/(?P<choice_id>\d+)/$',
        views.choice_del, name='choice_del'),
]


# API version=2
import numpy as np
from django.shortcuts import redirect
from rest_framework.decorators import api_view
import random
import string
from random import randint
from rest_framework.response import Response
from .models import SignalSet

COLORS = ['navy', 'blue', 'teal', 'aqua', 'green', 'lime', 'olive', 'yellow', 'maroon', 'red', 'purple', 'fuchsia',
          'gray', 'black']

def generate_random_string(length):
    letters = string.ascii_lowercase
    rand_string = ''.join(random.choice(letters) for _ in range(length))
    return rand_string

@api_view(['GET'])
def previous_signals_list_view(request):
    try:
        qs = SignalSet.objects.first()
        return Response(qs.signals, status=200)
    except AttributeError:
        return redirect('signal-list')

@api_view(['GET'])
def signal_list_view(request):
    value = []
    list_of_signals = []
    signals_quantity = randint(5, 25)
    length_of_lst = randint(100, 1000)
    read_only_values = np.random.choice([True, False], size=(signals_quantity,), p=[1 / 3, 2 / 3])
    for index in range(signals_quantity):
        for _ in range(length_of_lst):
            level = randint(0, 1)
            value.append(level)
        list_of_signals.append({"id": signals_quantity - index,
                                "name": f'Сигнал {generate_random_string(5) + str(signals_quantity - index)}',
                                "readOnly": read_only_values[index],
                                "color": random.choice(COLORS),
                                "value": value[:]})
        value.clear()
    signal_set = SignalSet()
    signal_set.signals = list_of_signals
    signal_set.save()
    if SignalSet.objects.all().count() > 2:
        SignalSet.objects.first().delete()
    return Response(list_of_signals, status=200)


import cv2
import chainer
import numpy as np
import chainer.functions as F
from vggface import VGGFace
from chainercv.links.model.vgg import VGG16
# pylint: disable=E1101

vgg = VGGFace(n_class=2622)
vgg.pick = 'prob'
vgg.remove_unused()
chainer.serializers.load_npz('vggface.npz', vgg)

x = cv2.imread('./images/ak.png', cv2.IMREAD_COLOR)
x = cv2.resize(x, dsize=(224, 224)).astype('f')
x = x[None, ].transpose(0, 3, 1, 2)

x[:, 0, :, :] -= 93.5940   # B
x[:, 1, :, :] -= 104.7624  # G
x[:, 2, :, :] -= 129.1863  # R

with open('./names.txt', 'r') as f:
    names = f.readlines()

with chainer.no_backprop_mode(), chainer.using_config('train', False):
    score = vgg(x).data

idx = np.argmax(score)

print('Predict: %s' % names[idx])
print('Score: %.02f' % score[0][idx])


# -*- coding: utf-8 -*-
"""
Created on Sun Jan 13 22:05:38 2019

@author: Nicolas
"""
import numpy as np
doPlot=True
try:
    import matplotlib.pyplot as plt
except:
    doPlot=False

helpString={
        'menu':'\n Menü, h: Hilfe, c: Ende',
        'help':"Hilfe: \n t: Talentwurf \n tl: ^ loop \n tw: W'keit Talentwurf zu schaffen \n twl: ^ loop \n twp: Plottet TW/TaW (nur in Python Konsole) \n mk: Menschenk \n sn: Sinnen \n c: Ende \n h: Hilfe \n sonst: xdy",
        'initMK':'sind diese Menschenk Werte (E1 E2 E3 TaW) richtig (j/n)',
        'initSN':'sind diese Sinnensch Werte (E1 E2 E3 TaW) richtig (j/n)',
        'xdy':'konnte nicht als xdy interpretiert werden, h: Hilfe'
        }

needsInput={
        't':'E1 E2 E3 TaW+Mod opt:wdh',
        'tw':'E1 E2 E3 TaW+Mod',
        'twp':'E1 E2 E3 startTaW',
        'mk':'Erschwernis (keine: 0, >0 -> Erschwernis, <0 -> Erleichterung)',
        'sn':'Erschwernis (keine: 0, >0 -> Erschwernis, <0 -> Erleichterung)'
        }

#takes 'xdy' as string
def xdy(args):
    s=args[0]
    if 'd' not in s or len(args)>1:
        print(args,helpString['xdy'])
        return True
    try:
        dInd=s.find('d')
        x=int(s[:dInd])
        y=int(s[dInd+1:])
        w=np.random.randint(1,y+1, size=x)
        print(w, ' Summe=', sum(w))
    except ValueError:
        print(s,helpString['xdy'])
    return True

def T(args):
    args=[int(x) for x in args[1:]]
    wdh=1
    if len(args)==5:wdh=args[4]
    for _ in range(wdh): throw(['', args[0], args[1], args[2], args[3]], 0)
    return True

def TL(_):
    while True:
        print('E1 E2 E3 TaW+Mod opt:wdh')
        s=input()
        try:
            args=[int(x) for x in s.split()]
            wdh=1
            if len(args)==5:wdh=args[4]
            for _ in range(wdh): throw(['', args[0], args[1], args[2], args[3]], 0)
        except ValueError:
            break
    return True

#einzelner Vergleich, return: bool
def vgl(e1, e2, e3, w1, w2, w3, taw):
    if(taw<=0):
        er1=e1-w1+taw
        er2=e2-w2+taw
        er3=e3-w3+taw
        return min(er1, er2, er3)>=0
    ag1=max(0,w1-e1)
    ag2=max(0,w2-e2)
    ag3=max(0,w3-e3)
    return (ag1+ag2+ag3)<=taw

#Eigenschaften 1 2 und 3, Taw+Erleichterung-Erschwernis
#Krit/Patzer werden nicht berücksichtigt
def Wkeit(l):
    [e1, e2, e3, TaW]=l
    count=0
    for x in range(1,21):
        for y in range(1,21):
            for z in range(1,21):
                count=count+vgl(e1,e2,e3,x, y, z,TaW)
    return count/(20**3)

#Talent W'keit ohne loop
def TW(args):
    try:
        l=[int(x) for x in args[1:]]
        print(Wkeit(l))
    except ValueError:
        return True
    return True

#Talent W'keit mit loop
def TWL(_):
    while True:
        print('E1 E2 E3 TaW+Mod')
        s=input()
        try:
            l=[int(x) for x in s.split()]
            print(Wkeit(l), '\n')
        except ValueError:
            break
    return True

#plottet tw gegen TaW, funktioniert nicht im normalen cmd
def TWP(args):
    E=[int(x) for x in args[1:5]]
    x=[]
    y=[]
    while True:
        x.append(E[3])
        y.append(Wkeit(E))
        if y[len(y)-1] > 0.99 : break
        E[3]=E[3]+1
    if doPlot:
        plt.scatter(x,y)
        plt.show()
    else : print([(x[i],y[i]) for i in range(len(x))])
    return True

# makes a throw for everyone in
def useRead(index, modS):
    if txtRead[index][0]:
        try:
            mod=int(modS)
            print('Erg  Name  W1 W2 W3')
        except ValueError:
            print('Das war keine ganze Zahl, -> mod=0')
            print('Erg  Name  W1 W2 W3')
            mod=0
        for line in txtRead[index][1]:
            throw(line, mod)
    else:
        print('update the .txt')

# can be used for any talent, given a split line from the txt files
def throw(line, mod):
    p=''
    k=''
    w=np.random.randint(1,21, size=3)
    if 1 in w: k='Krit?'
    if 20 in w: p='Patzer?'
    erg=vgl(int(line[1]), int(line[2]), int(line[3]), w[0], w[1], w[2], int(line[4])-mod)
    name=line[0]
    if len(name)>0: print(f'{erg:4} {name:10}',w , p, k)
    else: print(f'{erg:4}',w , p, k)

def Mk(args):
    useRead(0, args[1])
    return True

def Sn(args):
    useRead(1, args[1])
    return True

def end(_): return False

def he(_):
    print(helpString['help'])
    return True

#gets a string, tries to get args from the user
def getMissingArgs(s):
    print(needsInput[s])
    args=input()
    args=args.split()
    ret=[s]
    for i in range(len(args)):
        ret.append(args[i])
    return ret

def init():
    funDict={
        't':T,
        'tl':TL,
        'tw':TW,
        'twl':TWL,
        'twp':TWP,
        'mk':Mk,
        'sn':Sn,
        'c':end,
        'h':he}
    try:
        mkData=open('MK.txt')
        mkData=mkData.readlines()
        print(helpString['initMK'])
        print(''.join(mkData))
        mkOK=input()
        if mkOK in 'jy':
            mkOK = True
            mkData=[[y for y in x.split()] for x in mkData[0:len(mkData)]]
        else: mkOK = False
    except FileNotFoundError:
        print('\n kein MK.txt')
        input()
        mkData=[]
        mkOK=False

    try:
        snData=open('SN.txt')
        snData=snData.readlines()
        print(helpString['initSN'])
        print(''.join(snData))
        snOK=input()
        if snOK in 'jy':
            snOK = True
            snData=[[y for y in x.split()] for x in snData[0:len(snData)]]
        else: snOK = False
    except FileNotFoundError:
        print('\n kein SN.txt')
        input()
        snData=[]
        snOK=False
    return funDict, [(mkOK, mkData), (snOK, snData)]

funDict, txtRead=init()
var=True
while var:
    print(helpString['menu'])
    s=input()
    args=s.split()
    if (len(args)==1 and s in needsInput):
        args=getMissingArgs(args[0])
    var=funDict.get(args[0], xdy)(args)


#Imports
import json
from PIL import Image, ImageDraw, ImageFont
import numpy as np

def plot_boxes(image_dict, image, location = None, saver = 0): # Write the required arguments
	# The function should plot the predicted boxes on the images and save them.
	# Tip: keep the dimensions of the output image less than 800 to avoid RAM crashes.

	num_of_bboxes = len(image_dict['bboxes'])
	if num_of_bboxes > 5:
		num_of_bboxes = 5

	for j in image_dict['bboxes']: # for one bbox
		x_min, y_min, x_max, y_max = j['bbox']
		shape = [(x_min, y_min), (x_max, y_max)]
		drawer = ImageDraw.Draw(image)
		drawer.rectangle(shape, outline ="red", width=5)
		my_font = ImageFont.truetype('arial.ttf', 20)
		drawer.text((x_min,y_min), j['category'], font=my_font, fill = (255, 255, 0))
	if saver == 1:
		image.save(location + image_dict['img_fn'].split('/')[-1])
	return image


from glob import glob
from os import path
from itertools import cycle
from vispy import gloo
from vispy import app
import numpy as np

VERT_SHADER = """

uniform float blend;
uniform float view_size;

attribute vec2  a_position;
attribute float a_size;
attribute vec3  a_color;

attribute vec2  a_position2;
attribute float a_size2;
attribute vec3  a_color2;

varying vec4 v_fg_color;
varying vec4 v_bg_color;
varying float v_radius;

void main (void) {

    v_radius = (a_size * (1.0 - blend) + a_size2 * blend) * view_size;
    v_fg_color  = vec4(0.0, 0.0, 0.0, 0.5);
    v_bg_color  = vec4(a_color, 1.0) * (1 - blend) + vec4(a_color2, 1.0) * blend;

    gl_Position = vec4(a_position, 0, 1.0) * (1 - blend) + vec4(a_position2, 0, 1.0) * blend;
    gl_PointSize = 2.0*v_radius + 2;
}
"""

FRAG_SHADER = """
#version 120

varying vec4 v_fg_color;
varying vec4 v_bg_color;
varying float v_radius;
void main()
{

    float size = 2.0*v_radius + 2;
    float r = length((gl_PointCoord.xy - vec2(0.5,0.5))*size);
    if(r < v_radius)
        gl_FragColor = v_bg_color;
    else if(r < v_radius + 1)
    {
        gl_FragColor = vec4(v_bg_color[0], v_bg_color[1], v_bg_color[2], (v_radius + 1 - r) * 0.5);
    }
    else
        gl_FragColor = vec4(0, 0, 0, 0);
}
"""

def load_circles():
    photo_circles = []

    circle_dtype = [("position", np.float32, 2),
                    ("size", np.float32),
                    ("color", np.float32, 3)]

    for fn in glob(path.join(path.dirname(__file__), "*.csv")):
        data = np.loadtxt(fn, delimiter=",")
        circles = np.zeros(len(data), dtype=circle_dtype)
        circles["position"] = data[:, :2] / 256.0 - 1
        circles["size"] = data[:, 2] / 512.0 * 1.01
        circles["color"] = data[:, 3:] / 256.0
        circles["position"][:, 1] *= -1
        photo_circles.append(circles)

    photos = cycle(photo_circles)
    return photos

class Canvas(app.Canvas):

    def __init__(self):
        app.Canvas.__init__(self, keys='interactive', size=(400, 400))
        self.photos = load_circles()

    def set_data(self, circles1, circles2):
        self.program['a_color'] = circles1["color"].copy()
        self.program['a_position'] = circles1["position"].copy()
        self.program['a_size'] = circles1["size"].copy()

        self.program['a_color2'] = circles2["color"].copy()
        self.program['a_position2'] = circles2["position"].copy()
        self.program['a_size2'] = circles2["size"].copy()

    def on_initialize(self, event):
        self.program = gloo.Program(VERT_SHADER, FRAG_SHADER)

        self.circles1 = next(self.photos)
        self.circles2 = next(self.photos)
        self.set_data(self.circles1, self.circles2)

        self.blend = 0.01
        self.target_blend = 1.0
        self.program['blend'] = self.blend
        self.count = 0

        gloo.set_state(clear_color='white', blend=True,
                       blend_func=('src_alpha', 'one_minus_src_alpha'))

        self._timer = app.Timer('auto', connect=self.on_timer, start=True)

    def on_timer(self, event):
        step = min((self.blend * 0.96 + self.target_blend * 0.04) - self.blend, 0.02)
        self.blend += step

        if self.blend > 0.999 or self.blend < 0.001:
            self.blend = self.target_blend
            self.count += 1
        if self.count == 100:
            self.count = 0
            self.blend = 0.0
            self.circles1, self.circles2 = self.circles2, next(self.photos)
            self.set_data(self.circles1, self.circles2)
        self.program['blend'] = self.blend
        self.update()

    def on_resize(self, event):
        w, h = event.size
        s = min(w, h)
        gloo.set_viewport(0, 0, s, s)
        self.program['view_size'] = s

    def on_draw(self, event):
        gloo.clear(color=True, depth=True)
        self.program.draw('points')

if __name__ == '__main__':
    c = Canvas()
    c.show()
    app.run()


import world
import random
import torch
import numpy as np
import torch.optim as optim
from world import ARGS
from models import DQN
from collections import namedtuple

# ------------------------------------------------
def getOptimizer(optimizer=None):
    '''
        combine optimizer with related setting
    '''
    OptimizerSpec = namedtuple("OptimizerSpec", ["constructor", "kwargs"])
    if optimizer is None:
        optimizer = optim.RMSprop

    wrapped_optimizer = OptimizerSpec(
            constructor = optimizer,
            kwargs      = dict(lr = ARGS.lr, alpha=ARGS.alpha, eps=ARGS.eps)
        )
    return wrapped_optimizer
# ------------------------------------------------
def init_model(env,
               model : DQN):
    '''
        using the env information to init DQN model
    '''
    channel = ARGS.framelen * 1
    # we assert that all the obs are gray images
    num_actions = env.action_space.n
    # obtain Discrete actions of an env
    return model(in_channels=channel, num_actions=num_actions)
# ------------------------------------------------
def set_global_seeds(i):
    try:
        import torch
    except ImportError:
        pass
    else:
        torch.manual_seed(i)
    np.random.seed(i)
    random.seed(i)
# ------------------------------------------------
def linear_interpolation(l, r, alpha):
    return l + alpha * (r - l)

def TENSOR(*tensors):
    results = []
    for tensor in tensors:
        results.append(torch.from_numpy(tensor).float())
    return results

def TO(*tensors, **kwargs):
    results = []
    for tensor in tensors:
        results.append(tensor.to(world.DEVICE))
    return results
# ------------------------------------------------
class Schedule(object):
    def value(self, t):
        """Value of the schedule at time t"""
        raise NotImplementedError()
# ------------------------------------------------
class ConstantSchedule(object):
    def __init__(self, value):
        """Value remains constant over time.
        Parameters
        ----------
        value: float
            Constant value of the schedule
        """
        self._v = value

    def value(self, t):
        """See Schedule.value"""
        return self._v
# ------------------------------------------------
class PiecewiseSchedule(object):
    def __init__(self, endpoints, interpolation=linear_interpolation, outside_value=None):
        """Piecewise schedule.
        endpoints: [(int, int)]
            list of pairs `(time, value)` meanining that schedule should output
            `value` when `t==time`. All the values for time must be sorted in
            an increasing order. When t is between two times, e.g. `(time_a, value_a)`
            and `(time_b, value_b)`, such that `time_a <= t < time_b` then value outputs
            `interpolation(value_a, value_b, alpha)` where alpha is a fraction of
            time passed between `time_a` and `time_b` for time `t`.
        interpolation: lambda float, float, float: float
            a function that takes value to the left and to the right of t according
            to the `endpoints`. Alpha is the fraction of distance from left endpoint to
            right endpoint that t has covered. See linear_interpolation for example.
        outside_value: float
            if the value is requested outside of all the intervals sepecified in
            `endpoints` this value is returned. If None then AssertionError is
            raised when outside value is requested.
        """
        idxes = [e[0] for e in endpoints]
        assert idxes == sorted(idxes)
        self._interpolation = interpolation
        self._outside_value = outside_value
        self._endpoints      = endpoints

    def value(self, t):
        """See Schedule.value"""
        for (l_t, l), (r_t, r) in zip(self._endpoints[:-1], self._endpoints[1:]):
            if l_t <= t and t < r_t:
                alpha = float(t - l_t) / (r_t - l_t)
                return self._interpolation(l, r, alpha)

        # t does not belong to any of the pieces, so doom.
        assert self._outside_value is not None
        return self._outside_value
# ------------------------------------------------
class LinearSchedule(object):
    def __init__(self, schedule_timesteps, final_p, initial_p=1.0):
        """Linear interpolation between initial_p and final_p over
        schedule_timesteps. After this many timesteps pass final_p is
        returned.
        Parameters
        ----------
        schedule_timesteps: int
            Number of timesteps for which to linearly anneal initial_p
            to final_p
        initial_p: float
            initial output value
        final_p: float
            final output value
        """
        self.schedule_timesteps = schedule_timesteps
        self.final_p             = final_p
        self.initial_p          = initial_p

    def value(self, t):
        """See Schedule.value"""
        fraction  = min(float(t) / self.schedule_timesteps, 1.0)
        return self.initial_p + fraction * (self.final_p - self.initial_p)


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

import os, sys, shutil
from glob import glob
from datetime import datetime

path = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.dirname(os.path.normpath(path)))

from config import DIR_DATABASE, DIR_CLIMDATA, DIR_DOWNLOADS, LOG, LOGIN, PASSWORD
from Utils.utils import ConnexionServeur, DDH

#### serveur ftp par defaut: icare
###################################
try:
    prd = sys.argv[1]
except IndexError:
    print("produit non saisi: MYD04,MYD05,MYD07, severi_aerus, omaeruv")
    sys.exit()
if prd not in ["MYD04","MYD05","MYD07","seviri_aerus", "omaeruv"]:
    print("LE PRODUIT N'EXISTE PAS, CHOISIR DANS LA LISTE: MYD04 MYD05 MYD07 seviri_aerus omaeruv")
    sys.exit()
if prd == 'seviri_aerus':
    sat = 'msg'
elif prd == 'omaeruv':
    sat = 'aura_omi'
else:
    sat = 'modis'

pathDB = os.path.join(DIR_DATABASE, sat, prd)
pathCD = os.path.join(DIR_CLIMDATA, sat, prd)
pathDL = os.path.join(DIR_DOWNLOADS, prd)
try:
    os.chdir(pathDL)
except OSError:
    os.mkdir(pathDL)
    os.chdir(pathDL)

cs = ConnexionServeur(prd, LOGIN, PASSWORD)

################### LISTE DES FICHIERS TELECHARGES #########################################
yearDir = [d for d in sorted(os.listdir(pathDB)) if '20' in d][-1]
lastHDF = sorted(glob(os.path.join(pathDB, yearDir) + '/*.h*'))[-1].split('/')[-1]

if prd == 'omaeruv':
    date1 = datetime(2017,4,15) #DDH(lastHDF).date
    d = cs.omiFileDate(date1)
    dw = cs.omiDownload(pathDL)
    with open(os.path.join(LOG, 'tri_%s_%s.txt' % (prd, datetime.now().date())), 'w') as out:
        for f in sorted(glob('*.h*')):
            out.write(cs.triHDF(f) + '\n')
else:
    try:
        listyear,f,path = cs.connexion()
    except ValueError:
        print(cs.connexion())
        sys.exit()
    listyear = listyear[listyear.index(yearDir):]# selection des annees manquantes
    print("dernier fichier .hdf/he5 telecharge: %s" % lastHDF)
    print("annee(s) a telecharger: %s" % " ".join(listyear))

    errors = []
    for year in listyear[:]:
        output = []
        f.cwd(path+"/"+year)
        if sat == 'modis':
            listdate = sorted(f.nlst())
            if year == yearDir:
                doy = DDH(lastHDF).doy
                ldate = listdate[listdate.index(doy)+1:]
            else:
                ldate = listdate
            for day in ldate[:]:
                try:
                    f.cwd(path+"/"+year+"/"+day)
                    listfiles = f.nlst("*.hdf")
                    for fi in f.nlst("*.hdf"):
                        try:
                            heure = DDH(fi).heure
                            if (1000<int(heure)<1500) & (f.size(fi)>=100000):  #selection des fichiers entre 10h et 15h et d une taille superieure a 100000 octets
                                print('%s, %s' % (fi, cs.convertSize(f.size(fi))))
                                f.retrbinary("RETR %s" % fi, open(fi,"wb").write)
                        except:
                            errors.append([fi + '  Unexpected error'])
                            continue
                except:
                    errors.append("restricted access %s" % day)
                    continue
            with open(os.path.join(LOG, 'tri_%s_%s.txt' % (prd, datetime.now().date())), 'w') as out:
                for fhdf in sorted(glob('*.h*')):
                    out.write(cs.triHDF(fhdf) + '\n')
        else:
            prd = 'seviri'
            f.cwd(path+"/"+year)
            listfiles = f.nlst("*.h5")
            listfiles = listfiles[listfiles.index(lastHDF)+1:]
            f.sendcmd("TYPE i")
            for fi in listfiles:
                if f.size(fi) >= 100000:
                    try:
                        f.retrbinary("RETR %s" % fi, open(fi,"wb").write)
                        print('%s, %s' % (fi, cs.convertSize(f.size(fi))))
                    except:
                        errors.append([fi + '  Unexpected error'])
                        continue
    f.close()
    with open(os.path.join(LOG, 'error_%s_%s.txt' % (prd, datetime.now().date())), 'w') as err:
        for l in errors:
            err.write(l + '\n')

#####################################################################################
## copie des .nc de la base vers le dossier du prd

filesDB = sorted(glob(os.path.join(pathCD, 'res*/'+prd+'*.nc')))
for f in filesDB:
    print("copie de %s dans %s" % (f, pathDL))
    shutil.copy(f, pathDL)

print("\n\ntelechargement effectue")


import sys
import os
from random import uniform
import random
import math
import bpy, bmesh

from mathutils import Vector
from nodes import Nodes
from streets import Streets
from auxiliarFunctions import *

#from voronoiDiagram import VoronoiDiagram

texturexdir="./textures/"

#Class "PrimaryStreets":
#    - Functionality: Class to create the primary streets of the city. There
#                    are two method to do it:
#                        - VORONOI ALGORITHM: we use a squared net where we can include
#                                                noise in the vertices.
#                        - RANDOM VERTICES: we start from a cloud of random vertices and
#                                            we join them by the factor of density and
#                                            also avoid the creation of triangle districts,
#                                            vertices without connections and extreme
#                                            streets, creating a convex graph where each
#                                            node will be a intersection between streets
#                                            and the edges will be the streets.
#
#    - Parameters:
#        - x: the streets will be from -x to x on the X axis
#        - y: the streets will be form -y to y on the Y axis
#        - streetSize: width of the streets
#        - density(Random Algorithm): density of the districts
#        - districtHeight(Voronoi Algorithm): height of the districts
#        - districtWidth(Voronoi Algorithm): width of the districts
class PrimaryStreets(object):

    def __init__(self,x,y,streetSize,density,districtHeight,districtWidth):

        distanceFactor = density

        self.vertices = []                              #List of vertices
        self.faces = []                                 #List of faces
        self.edges = []                                 #List of edges
        self.nodes = Nodes()                            #Nodes of the graph
        self.streets = Streets()                        #Streets
        self.streetSize = streetSize                    #Size of the streets
        self.minDistance = int(x/distanceFactor)        #Minimum distance between 2 vertices
        self.maxDistance = int(self.minDistance*2)      #Maximum distance between 2 vertices to be connected
        self.district = []                              #District formed by the streets

        #================VORONOI ALGORITHM==================#
        #===============(Creating the graph)================#

        #---------- v = VoronoiDiagram(x,y,density,districtHeight,districtWidth)
        #--------------------------------------------------------- v.noise(20.0)
        #--------------------------------------------------- points = v.vertices
        #------------------------------------------------------- edges = v.edges
        #------------------------------------------------- districts = v.regions
#------------------------------------------------------------------------------
        #------------------------------------------------------ for i in points:
            #---------------------------------------------- self.nodes.insert(i)
        #------------------------------------------------------- for i in edges:
            #------------------------------- self.nodes.addConnection(i[0],i[1])
        #--------------------------------------------------- for i in districts:
            #-------------------------------------- self.nodes.insertDistrict(i)
#------------------------------------------------------------------------------
        #-------------------------------- self.vertices = self.nodes.coordinates

        #===================================================#

        #=================RANDOM ALGORITHM==================#
        #===============(Creating the graph)================#

        #Create the cloud of points and insert as nodes in the graph
        nNodes = random.randint(x*int(distanceFactor),x*int(distanceFactor)+x)
        initialVertices = []
        for i in range(nNodes):
            coordinatesX = random.uniform(-x,x)
            coordinatesY = random.uniform(-y,y)
            self.nodes.insert([coordinatesX,coordinatesY,0])

          #Remove the nodes that are too close by the value of "self.minDistance"
        self.removeNodesTooClose()

        #Connect the nodes by the value of "self.maxDistance"
        self.conectNodes()

        #Delete the nodes that are not connected or make a street with a
        #extreme without connect
        self.removed = []
        self.deleteExtremes()
        for i in self.removed:
            self.nodes.deleteByCoordenate(i)

        #Include the result vertices to "self.vertices"
        self.vertices = self.nodes.coordinates

        #TO THE EXAMPLE IN THE DOCUMENTATION

        #-------------------------------------------- self.nodes.insert([0,0,0])
        #----------------------------------------- self.nodes.insert([100,50,0])
        #---------------------------------------- self.nodes.insert([100,100,0])
        #------------------------------------------ self.nodes.insert([0,150,0])
        #--------------------------------------- self.nodes.insert([-100,150,0])
        #----------------------------------------- self.nodes.insert([-100,0,0])
#------------------------------------------------------------------------------
        #----------------------------------------- self.nodes.addConnection(0,1)
        #----------------------------------------- self.nodes.addConnection(1,2)
        #----------------------------------------- self.nodes.addConnection(2,3)
        #----------------------------------------- self.nodes.addConnection(0,3)
        #----------------------------------------- self.nodes.addConnection(0,5)
        #----------------------------------------- self.nodes.addConnection(3,4)
        #----------------------------------------- self.nodes.addConnection(4,5)
#------------------------------------------------------------------------------
        #-------------------------------- self.vertices = self.nodes.coordinates

        #Look for the cycles in the graph according with the
        #coordinates of each node
        districts = self.getDistricts()

        for i in districts:
            self.nodes.insertDistrict(i)

        #===================================================#

        #==========CREATE THE STREET FROM THE GRAPH===================#

        #Generation of the vertices will be part of the intersections
        self.generateVerticesIntersections()

        #Make the faces that create the streets
        self.makeStreetFaces()

        #Make the faces that create the intersections
        self.makeIntersectionFaces()

        #Look for the district between the streets with the new
        #vertices included
        self.getDistrictVertices()

        #===========================================================#

        #Checking the clockwise of the face to set the right normals in Blender
        for i in range(len(self.faces)):
            vertices = []
            for j in self.faces[i]:
                vertices.append(self.vertices[j])

            if checkClockwise(vertices):
                self.faces[i].reverse()

        #To see the edges of the graph

        for i in self.nodes.connections:
            for j in self.nodes.connections[i]:
                if not ([i,j] in self.faces) and not ([j,i] in self.faces):
                    self.edges.append((i,j))

        mesh=bpy.data.meshes.new("primary streets mesh")
        self.object=bpy.data.objects.new("primary streets object",mesh)
        self.object.location=[0,0,0]
        bpy.context.scene.objects.link(self.object)
        mesh.from_pydata(self.vertices,[],self.faces)
        #mesh.from_pydata(self.vertices,self.edges,[]) #To see the edges of the graph
        #mesh.from_pydata(self.vertices,self.edges,self.faces) #To see the edges and streets of the graph

    #Function "material":
    # - Functionality: include a material for the primary streets. We use a black-grey
    #                    color for the material and we include a procedural texture of
    #                    Blender called "MUSGRAVE", to make the street more realistic.
    #                    We modify some parameters of the procedural texture in our way.
    def material(self):
        #Creating the material
        mat = bpy.data.materials.new('primaryStreetMaterial')
        diffuse_color = uniform(0.01,0.03)
        mat.diffuse_color = (diffuse_color,diffuse_color,diffuse_color)
        mat.diffuse_shader = 'LAMBERT'
        mat.diffuse_intensity = 1.0
        mat.specular_color = (1,1,1)
        mat.specular_shader = 'COOKTORR'
        mat.specular_intensity = 0.0
        mat.alpha = 1
        mat.ambient = 1

        #Creating the procedural texture
        sTex = bpy.data.textures.new('primaryStreetsTexture', type = 'MUSGRAVE')
        sTex.noise_basis = 'BLENDER_ORIGINAL'
        sTex.noise_scale = uniform(0.02,0.06)
        sTex.musgrave_type = 'MULTIFRACTAL'
        sTex.lacunarity = uniform(2.5,3.0)
        sTex.noise_intensity = uniform(0.3,0.5)

        #Adding texture to the material
        mtex = mat.texture_slots.add()
        mtex.texture = sTex
        mtex.texture_coords = 'OBJECT'
        mtex.use_map_color_diffuse = True
        color = uniform(0.02,0.07)
        mtex.color = (color,color,color)

        #Adding material to the object
        me=self.object.data
        me.materials.append(mat)

    def material2(self,glass=1):
        me=self.object.data

        self.mat1 = bpy.data.materials.new("glass")
        self.mat1.diffuse_shader = 'LAMBERT'
        self.mat1.diffuse_color = (0.68,0.68,0.89)
        self.mat1.diffuse_intensity = 0.2
        self.mat1.specular_shader = 'COOKTORR'
        self.mat1.specular_color = (0.8,0.8,1)
        self.mat1.specular_hardness = 240
        self.mat1.specular_intensity = 1.0

        self.mat1.raytrace_mirror.use=1
        self.mat1.mirror_color=(0.36,0.72,0.62)
        self.mat1.raytrace_mirror.reflect_factor=0.7

        textureFile=os.path.expanduser(texturexdir+"primaryStreets.png")

        try:
            img = bpy.data.images.load(textureFile)
        except:
            raise NameError("Cannot load image %s" % textureFile)

        pTex = bpy.data.textures.new("texturebuilding", type = 'IMAGE')
        pTex.image=img;

        # put the texture on a material with UV coordinates
        self.mat1.texture_slots.add()
        self.mat1.texture_slots[0].texture = pTex
        self.mat1.texture_slots[0].texture_coords = 'UV'
        self.mat1.texture_slots[0].uv_layer = 'primaryStreet'
        me.materials.append(self.mat1)

        # set the coordinates of each vertex of the faces in UV mapping (2D)
        me.uv_textures.new("primaryStreet")
        bm = bmesh.new()
        bm.from_mesh(me)
        bpy.ops.uv.smart_project()
        #-------------------------------------- uv_layer = bm.loops.layers.uv[0]
#------------------------------------------------------------------------------
        #------------------------------------------------ nFaces = len(bm.faces)
        #----------------------------------------------------- for fi in nFaces:
            #--------------------------------------------------- maxDistance = 0
            #--------------------------------- for v in len(bm.faces[fi].verts):
                #-------------------------- if v != (len(bm.faces[fi].verts)-1):
                    # d = dist(bm.faces[fi].verts[v].co,bm.faces[fi].verts[v+1].co)
                #--------------------------------------------------------- else:
                    # d = dist(bm.faces[fi].verts[v].co,bm.faces[fi].verts[0].co)
            #------ d1 = dist(bm.faces[fi].verts[0].co,bm.faces[fi].verts[1].co)
            #------ d2 = dist(bm.faces[fi].verts[1].co,bm.faces[fi].verts[2].co)
            #----------------------- bm.faces[fi].loops[0][uv_layer].uv = (0, 0)
            #-------------------- bm.faces[fi].loops[1][uv_layer].uv = (d1/4, 0)
            #----------------- bm.faces[fi].loops[2][uv_layer].uv = (d1/4, d2/2)
            #-------------------- bm.faces[fi].loops[3][uv_layer].uv = (0, d2/2)
        bm.to_mesh(me)

        # put the image behind the UV layer for the times when you're using the Multitexture shader on the 3D view.
        bm = bmesh.new()
        bm.from_mesh(me)
        tl = bm.faces.layers.tex.active
        if tl:
            for f in bm.faces:
                f[tl].image = me.materials[0].texture_slots[0].texture.image
        bm.to_mesh(me)

    #Function "materemoveNodesTooCloserial":
    # - Functionality: remove the nodes which are too close to each other according
    #                    with the value of "self.minDistance".
    def removeNodesTooClose(self):
        removed = []
        for i in range(self.nodes.nNodes-1):
            if not (self.nodes.getCoordinates(i) in removed):
                for j in range(i+1,self.nodes.nNodes):
                     if(dist(self.nodes.getCoordinates(i),self.nodes.getCoordinates(j)) < self.minDistance):
                        removed.append(self.nodes.getCoordinates(j))

        for i in removed:
            self.nodes.deleteByCoordenate(i)

    #Function "conectNodes":
    # - Functionality: connect the nodes which are close enough to each other according
    #                    with the value of "self.maxDistance" and checking the creation
    #                    of triangles and checking that the new connections do not intersect
    #                    with a connections made before.
    def conectNodes(self):
        for i in range(self.nodes.nNodes-1):
            for j in range(i+1,self.nodes.nNodes):
                if(dist(self.nodes.getCoordinates(i),self.nodes.getCoordinates(j)) < self.maxDistance):
                    if not self.checkTriangle(i, j):
                        if not self.checkIntersection(i,j):
                            self.nodes.addConnection(i,j)

    #Function "checkTriangle":
    # - Functionality: checking if we connect two node it will create a triangle, by
    #                    checking if the two nodes have any node in common in the nodes
    #                    that they are already connected.
    #
    # - Parameters:
    #    - a: index of the first node
    #    - b: index of the second node
    #
    # - Return:
    #    - bool, if the nodes will create a triangle or not
    def checkTriangle(self,a,b):
        for i in self.nodes.getConnection(a):
            for j in self.nodes.getConnection(b):
                if i == j:
                    return True

        return False

    #Function "checkIntersection":
    # - Functionality: checking if we connect two node it will create a intersection
    #                    between the edges of the graph that we have already created.
    #
    # - Parameters:
    #    - i: index of the first node
    #    - j: index of the second node
    #
    # - Return:
    #    - bool, if the nodes will intersect or no
    def checkIntersection(self,i,j):
        intersection = False
        for k in self.nodes.connections:
            for e in self.nodes.getConnection(k):
                v = lineIntersection([self.nodes.getCoordinates(i),self.nodes.getCoordinates(j)],
                                     [self.nodes.getCoordinates(k),self.nodes.getCoordinates(e)])
                if (v != False):
                    if checkPointInSegment4(self.nodes.getCoordinates(k),self.nodes.getCoordinates(e),[v[0],v[1],0]) and checkPointInSegment4(self.nodes.getCoordinates(i),self.nodes.getCoordinates(j),[v[0],v[1],0]):
                        intersection = True
                        break
            if intersection:
                break;

        return intersection

    #Function "deleteExtremes":
    # - Functionality: delete the nodes that are not connected with any node or that
    #                    is a extreme of a street without connection.
    def deleteExtremes(self):
        for i in range(self.nodes.nNodes):
            if not (self.nodes.getCoordinates(i) in self.removed):
                if(len(self.nodes.getConnection(i)) == 0) or (len(self.nodes.getConnection(i)) == 1):
                    self.removed.append(self.nodes.getCoordinates(i))
                    if (len(self.nodes.getConnection(i)) == 1):
                        self.recursiveDelete(self.nodes.getConnection(i)[0],i)

    #Function "recursiveDelete":
    # - Functionality: recursive function checking if we delete a node if it create a street
    #                    with a extreme without connect
    def recursiveDelete(self,index,previousIndex):
        if len(self.nodes.getConnection(index)) == 2:
            if not (self.nodes.getCoordinates(index) in self.removed):
                self.removed.append(self.nodes.getCoordinates(index))
            for i in self.nodes.getConnection(index):
                if i != previousIndex:
                    self.recursiveDelete(i,index)

    #Function "getDistricts":
    # - Functionality: looking for the areas created by the graph. The algortihm to do it
    #                    is a MCB (Minimum Cycle Basic) algorithm to a graph, by keep in
    #                    mind the coordinate of each node.
    #
    # - Return:
    #    - districts: a list of district where each element is a list of index of node
    #                    which make a district.
    def getDistricts(self):
        districts = []
        delete = []
        found = False
        for i in range(self.nodes.nNodes):
            for j in self.nodes.findDistricts(i,self.nodes.nNodes):
                delete = []

                polygon = []
                for k in range(len(j)):
                    v = self.nodes.getCoordinates(j[k])
                    polygon.append([v[0],v[1]])

                inside = False
                for k in range(self.nodes.nNodes):
                    p = self.nodes.getCoordinates(k)
                    if not (k in j):
                        inside = point_inside_polygon(p[0], p[1], polygon)
                        if inside:
                            break

                if not inside:
                    for k in range(len(districts)):
                        #Cycle taken is smaller than cycle we are going to compare
                        if len(districts[k]) > len(j):
                            found = True
                            for l in j:
                                if not (l in districts[k]):
                                    found = False
                                    break
                            if found:
                                delete.append(k)
                                found = False

                        #Cycle taken is bigger than cycle we are going to compare
                        elif len(districts[k]) < len(j):
                            found = True
                            for l in districts[k]:
                                if not (l in j):
                                    found = False
                                    break
                            if found:
                                break;

                        else:
                            found = True
                            for l in j:
                                if not (l in districts[k]):
                                    found = False
                                    break
                            if found:
                                break;

                    if not found:
                        districts.append(j)

                    delete.sort()
                    for k in range(len(delete)-1,-1,-1):
                        del districts[delete[k]]

        return districts

    #Function "generateVerticesIntersections":
    # - Functionality: calculate the vertices which create the face of a intersection, the face
    #                    result of a node of the graph and adding the vertices to "self.vertices"
    def generateVerticesIntersections(self):
        for i in range(self.nodes.nNodes):
            connectionCoordinates = []
            for j in self.nodes.getConnection(i):
                connectionCoordinates.append(self.nodes.getCoordinates(j))

            intersection = self.streets.calculateIntersection(self.nodes.getCoordinates(i),self.nodes.getConnection(i),connectionCoordinates,self.streetSize)
            self.streets.insertIntersection(intersection)
            for j in intersection:
                self.vertices.append(intersection[j][0])
                self.vertices.append(intersection[j][1])

    #Function "makeStreetFaces":
    # - Functionality: make the faces related to a edge of the graph
    def makeStreetFaces(self):
        made = []
        for i in range(self.streets.nIntersections):
            for j in self.streets.getIntersection(i):
                if not (([j,i] in made) or ([i,j] in made)):
                    face = self.streets.makeFaceStreet(i,j)
                    f = []
                    for k in face:
                        f.append(self.vertices.index(k))
                    self.faces.append(f)

                    made.append([j,i])
                    made.append([i,j])

    #Function "makeIntersectionFaces":
    # - Functionality: make the faces related to a node of the graph
    def makeIntersectionFaces(self):
        print('ENTRA MAKE INTERSECTION FACE')
        #To each node we execute the algorithm
        for i in range(self.streets.nIntersections):
            #That function return which vertices make the face and the order. Also return
            #new vertices that appeared in the generation and to which have to be substitute
            [face,newVertices,sustitute] = self.streets.makeIntersectionFace(i,self.nodes.coordinates,self.nodes.connections,self.streetSize)

            for j in sustitute:
                index1 = self.nodes.coordinates.index(j[0])
                index2 = self.vertices.index(j[0])

                for k in range(len(self.faces)):
                    for l in range(len(self.faces[k])):
                        if self.faces[k][l] == index2:
                            self.faces[k][l] == index1

                self.nodes.coordinates[index1] = j[1]
                self.vertices[index2] = j[1]

            if face != False:
                for j in newVertices:
                    self.vertices.append(j)

                f = []
                for j in face:
                    f.append(self.vertices.index(j))

                self.faces.append(f)
        print('SALE MAKE INTERSECTION FACE')

    #Function "makeIntersectionFaces":
    # - Functionality: get the district resulting after make the faces of the
    #                    streets and the intersection
    def getDistrictVertices(self):
        for i in range(len(self.nodes.districts)):
            self.district.append(self.streets.calculateDistrict(self.nodes.districts[i]))


from setuptools import setup, find_packages

entry_points = {}
entry_points['console_scripts'] = ['check-stale-issues = baldrick.scripts.stale_issues:main',
                                   'check-stale-pull-requests = baldrick.scripts.stale_pull_requests:main']

setup(version='0.0.dev0',
      name="baldrick",
      description="Helpers for GitHub bots",
      url='https://github.com/astrofrog/baldrick',
      packages=find_packages(),
      author='Stuart Mumford and Thomas Robitaille',
      entry_points=entry_points,
      extras_require={'test': ['pytest>=3.5', 'pytest-flake8', 'pytest-cov', 'codecov']},
      install_requires=[
          "flask",
          "pyjwt",
          "requests",
          "python-dateutil",
          "cryptography",
          "humanize",
          "toml",
          "ttldict"])


from abc import ABC, abstractmethod
from typing import List

from requests import get

from Item import Item

class ItemCollector(ABC):

    STEAM_MARKET_SEARCH_URL = 'https://steamcommunity.com/market/search/render/'
    PAGESIZE = 100

    def __init__(self):
        self._parameters = {
            "appid": 440,
            "norender": True,
            "count": ItemCollector.PAGESIZE,
            "start": 0
        }
        self._total_item_count = None
        self._response = None
        self._items = List[Item]

    def _get_successful_response(self, parameters: dict):
        status_code = 0
        while status_code != 200:
            self._get_new_response(parameters)
            status_code = self._response.status_code
            print("trying....")
            print(str(self._response))

    def fill_item_list(self, item_list: List[Item]):
        self._get_all_items()
        item_list.extend(self._items)

    def _set_total_item_count(self):
        self._total_item_count = self._response.json()["total_count"]
        print("total item count set: " + str(self._total_item_count))

    def _get_new_response(self, parameters: dict):
        print("get called with parameters", parameters)
        self._response = get(ItemCollector.STEAM_MARKET_SEARCH_URL, parameters)

    @abstractmethod
    def _get_all_items(self):
        raise NotImplementedError


"""Initial migration.

Revision ID: d436eb70162b
Revises: 99e70004589f
Create Date: 2021-02-03 00:28:58.745921

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'd436eb70162b'
down_revision = '99e70004589f'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('team', sa.Column('nickname', sa.String(length=120), nullable=False))
    op.add_column('team', sa.Column('short_name', sa.String(length=10), nullable=False))
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('team', 'short_name')
    op.drop_column('team', 'nickname')
    # ### end Alembic commands ###


import numpy as np
import pandas as pd
import MDAnalysis as md
import GromacsAnalysis.coordination as cn

import pickle
from sklearn.externals import joblib

from collections import Counter

u = md.Universe('..\data\md256_280k.tpr', '..\data\md256_280k.gro')
ow = u.select_atoms('name OW')

d5 = cn.structural_descriptor(u, 'name OW', kind='d5', ofilename='..\data\d5.xvg')
q4 = cn.structural_descriptor(u, 'name OW', kind='q4', ofilename='..\data\q4.xvg')

d5.run()
q4.run()

d5_vec = np.array(d5.structure_vec)
q4_vec = np.array(q4.structure_vec)

# make dataframe
df = pd.DataFrame(np.transpose([d5_vec, q4_vec]), columns=['d5', 'q4'])

# load clustering model
km_model = joblib.load('..\data\SpectralClustering_n256_280k.pickle')
y_pred_vec = km_model.fit_predict(df)
print(Counter(y_pred_vec))

ofilename = '..\data\spectral_n256_280k.gro'
with open(ofilename, "w") as ofile:
    ofile.write('droplet clustered with spectralclustering algorithm' + '\n')
    ofile.write(str(256) + '\n')
    pos_ow_mat = ow.positions
    for i, pos_ow_vec in enumerate(pos_ow_mat):
        res_num = i
        res_name = "OW{}".format(y_pred_vec[i])
        atom_num = i
        atom_name = "OW"
        x, y, z = pos_ow_vec * 0.1

        ofile.write("%5d%-5s%5s%5d%8.3f%8.3f%8.3f" % (res_num, res_name, atom_name, atom_num, x, y, z))
        ofile.write("\n")

    ofile.write("%.3f\t%.3f\t%.3f" % (20., 20., 20.))
    ofile.write("\n")

# make r_pos model
y_pred_vec = []
pos_ow_mat = ow.positions
com_vec = np.zeros(3)
for pos_ow_vec in pos_ow_mat:
    com_vec += pos_ow_vec
com_vec /= 256
for pos_ow_vec in pos_ow_mat:
    pos_ow_vec -= com_vec
    r = np.sqrt(pos_ow_vec[0]**2 + pos_ow_vec[1]**2 + pos_ow_vec[2]**2) * 0.1
    if r < 1.0:
        y_pred_vec.append(0)
    else:
        y_pred_vec.append(1)

ofilename = "..\data\\radial_n256_280k.gro"
with open(ofilename, "w") as ofile:
    ofile.write('droplet clustered with radial algorithm' + '\n')
    ofile.write(str(256) + '\n')
    pos_ow_mat = ow.positions
    for i, pos_ow_vec in enumerate(pos_ow_mat):
        res_num = i
        res_name = "OW{}".format(y_pred_vec[i])
        atom_num = i
        atom_name = "OW"
        x, y, z = pos_ow_vec * 0.1

        ofile.write("%5d%-5s%5s%5d%8.3f%8.3f%8.3f" % (res_num, res_name, atom_name, atom_num, x, y, z))
        ofile.write("\n")

    ofile.write("%.3f\t%.3f\t%.3f" % (20., 20., 20.))
    ofile.write("\n")


#!/usr/bin/env python2
print('starting imports')

import argparse
import imp
import json
import mmap
import os
import re
import signal
import struct
import sys
import time
import traceback
import yaml

import shm
import posix_ipc
import numpy as np
from auvlog.client import log as auvlog
import zmq

import vision_common
from capture_sources import posixshmutils
from capture_sources import CaptureSource

print('finished imports')

dirname = os.path.dirname(os.path.realpath(__file__))
logger = auvlog.vision

def run_capture_source(capture_source_direction, capture_source_parameters):
    source_type = capture_source_parameters['type']
    del capture_source_parameters['type']
    actual_capture_source = getattr(__import__(source_type), source_type)

    try:
        source = actual_capture_source(capture_source_direction.lower(), **capture_source_parameters)

        def signal_handler(*args, **kwargs):
            source.cleanup()
            vision_common.cleanup_pid()

        signal.signal(signal.SIGTERM, signal_handler)
        signal.signal(signal.SIGINT, signal_handler)
        source.acquisition_loop()
    except Exception as e:
        print(e)
        traceback.print_exc()
        sys.stdout.flush()
        sys.stderr.flush()

def run_module(module_name):
    m_logger = getattr(logger.module, module_name)
    sys.path.append('{}/modules'.format(dirname))

    module_file = imp.reload(__import__(module_name))
    module_class = getattr(module_file, module_name)

    m_logger.log('running module')

    module = module_class()

    signal.signal(signal.SIGTERM, vision_common.cleanup_pid)

    gui_pub = zmq.Context().socket(zmq.PUB)
    gui_pub.setsockopt(zmq.SNDHWM, 1)
    gui_pub.bind('ipc:///tmp/visiond_module_socket_{}'.format(module_name))

    options_list = module_file.vision_options if hasattr(module_file, 'vision_options') else []
    options_list = [(opt.__class__.__name__, opt.args) for opt in options_list]
    module.options_dict = {option[1][0]: option[1][1] for option in options_list}

    serialized = vision_common.serialize_options(options_list)
    extant = False
    try:
        sharedmem = posix_ipc.SharedMemory('/{}_module_options'.format(module_name), flags=posix_ipc.O_CREX,
                                       size=len(serialized) + 200)
    except posix_ipc.ExistentialError:
        extant = True
        sharedmem = posix_ipc.SharedMemory('/{}_module_options'.format(module_name), size=len(serialized) + 200)

    options_shm = mmap.mmap(sharedmem.fd, 0)

    if options_shm.read(4) == '\0'*4:
        extant = False

    options_lock = vision_common.NamedRWLock('{}_module_lock'.format(module_name))
    options_shm.seek(0)

    if not extant:
        options_lock.acquire_write()
        options_shm.write(serialized)
        options_lock.release_write()
    else:
        options_lock.acquire_read()
        options_shm.seek(0)
        length = struct.unpack('I', options_shm.read(4))[0]

        while length > 0:
            option = json.loads(options_shm.read(length))
            module.options_dict[option[1][0]] = option[1][1]
            length = struct.unpack('I', options_shm.read(4))[0]

        options_lock.release_read()

    direction = module_file.capture_source
    m_logger.log('connecting to {}'.format(direction))
    while True:
        try:
            im_shm = posixshmutils.shm_setup(direction)
            break
        except Exception as e:
            print(e)
            pass
        time.sleep(0.05)

    last_sem = 0

    while getattr(shm.vision_modules, module_name.split('.')[0]).get():
        module.postedImages = []
        last_sem, next_image, acq_time = posixshmutils.get_next_image(im_shm, last_sem)
        m_logger.log('image latency: {}'.format(time.time()*1000 - acq_time))
        try:
            module.process(next_image)
        except Exception as e:
            sys.stderr.write('{}\n'.format(e))
            traceback.print_exc(e, sys.stderr)

        options_lock.acquire_read()
        options_shm.seek(0)
        length = struct.unpack('I', options_shm.read(4))[0]

        while length > 0:
            option = json.loads(options_shm.read(length))
            module.options_dict[option[1][0]] = option[1][1]
            length = struct.unpack('I', options_shm.read(4))[0]

        options_lock.release_read()
        gui_pub.send_string(module_name, zmq.SNDMORE)
        while module.postedImages:
            tag, image = module.postedImages.pop(0)
            md = dict(tag=tag, dtype=str(image.dtype), shape=image.shape, )
            gui_pub.send_json(md, zmq.SNDMORE)
            gui_pub.send(image, zmq.SNDMORE)
        gui_pub.send_string('')

def start_daemon(config, daemon=False):
    with open(config) as config:
        config = yaml.load(config)

    logger.log('parsed options, killing old vision processes')
    stop_daemon(wait=True)
    logger.log('killed old vision processes, starting capture sources')

    for capture_source_name in config['capture_sources']:
        logger.log('starting capture source {}'.format(capture_source_name))
        vision_common.fork(run_capture_source,
                           args=(capture_source_name, config['capture_sources'][capture_source_name]))

    logger.log('started all capture sources')

    all_vision_modules = [(x[0], getattr(shm.vision_modules, x[0])) for x in shm.vision_modules._fields]
    if 'modules' in config:
        config_modules = [module.lower() for module in config['modules']]
        modules_to_start = []
        if 'modules' in config:
            for module_name, module in all_vision_modules:
                if module_name.lower() in config_modules:
                    modules_to_start.append(module_name)
                else:
                    module.set(False)

        start_modules(modules_to_start, daemon=daemon)

    logger.log('started all initial modules')
    logger.log('Starting vision processing')

def start_modules(modules, kill_old_modules=True, daemon=False):
    all_vision_modules = [(x[0], getattr(shm.vision_modules, x[0])) for x in shm.vision_modules._fields]

    for i, module in enumerate(modules):
        module_shm = None
        for module_name, _module_shm in all_vision_modules:
            if module_name.lower() == module.lower():
                module_shm = _module_shm
                module = module_name
                break
        if module_shm is None:
            continue

        if kill_old_modules and module_shm.get():
            module_shm.set(False)
            time.sleep(0.5)

        module_shm.set(True)
        vision_common.fork(run_module, args=(module,))

    if not daemon:
        sys.stdout.flush()
        try:
            while True:
                time.sleep(10)
        except:
            pass
        logger.log('Stopping daemon!')
        stop_daemon()

def stop_modules(modules):
    all_vision_modules = [(x[0], getattr(shm.vision_modules, x[0])) for x in shm.vision_modules._fields]

    for module in modules:
        for module_name, module_shm in all_vision_modules:
            if module_name.lower() == module.lower():
                module_shm.set(False)

def daemon_running():
    return [filename for filename in os.listdir('{}/pids'.format(dirname)) if '.pid' in filename]

def status():
    if daemon_running:
        print('Vision daemon is currently running on:')
        #print cameras

        #print modules
        for module_name in (x[0] for x in shm.vision_modules._fields):
            if getattr(shm.vision_modules, module_name).get():
                print(module_name)
    else:
        print('Vision daemon is not running')

def stop_daemon(wait=True):
    stop_modules(x[0] for x in shm.vision_modules._fields)

    time.sleep(0.1)

    for filename in (filename for filename in os.listdir('{}/pids'.format(dirname)) if '.pid' in filename):
        groups = re.findall('(\d+).pid', filename)
        if groups:
            pid = int(groups[0])
            try:
                os.kill(pid, signal.SIGTERM)
            except OSError:
                pass

    start_time = time.time()
    while wait and daemon_running():
        time.sleep(0.1)
        all_pids = [int(pid) for pid in os.listdir('/proc') if pid.isdigit()]
        for filename in (filename for filename in os.listdir('{}/pids'.format(dirname)) if '.pid' in filename):
            groups = re.findall('(\d+).pid', filename)
            if groups:
                pid = int(groups[0])
                if pid not in all_pids:
                    os.remove('{}/pids/{}'.format(dirname, filename))
                elif time.time() - start_time > 0.5:
                    try:
                        os.kill(pid, signal.SIGKILL)
                    except OSError:
                        pass

if __name__ == '__main__':
    logger.log('parsing options')
    parser = argparse.ArgumentParser(description='Start the vision daemon.')
    sp = parser.add_subparsers()

    sp_start = sp.add_parser('start', help='Starts the vision daemon, or an individual module')
    sp_start.set_defaults(which='start')
    sp_start.add_argument('-d', '--daemon', action='store_true', help='Run as a daemon')

    start_group = sp_start.add_argument_group()
    start_group.add_argument('config', type=str, metavar='module_or_config',
                             help='A config file to load, or module if the daemon is already running')
    start_group.add_argument('modules', metavar='module', type=str, help='Additional modules to start', nargs='*')

    sp_stop = sp.add_parser('stop', help='Stops the vision daemon, or an individual module')
    sp_stop.add_argument('modules', metavar='module', type=str,
                         help='A list of modules to stop. If empty, stops the daemon', nargs='*')
    sp_stop.set_defaults(which='stop')

    sp_status = sp.add_parser('status', help='Get vision daemon status')
    sp_status.set_defaults(which='status')

    args = parser.parse_args()
    running = daemon_running()

    sys.path.append('{}/capture_sources'.format(dirname))

    if args.which == 'start':
        modules = args.modules

        if os.path.isfile(args.config):
            start_daemon(args.config, daemon=(len(modules) > 0 or args.daemon))
            running = True
        else:
            modules.append(args.config)

        if running:
            if len(modules) > 0:
                logger.log('Starting modules: {}'.format(', '.join(modules)))
                start_modules(modules, daemon=args.daemon)
        else:
            logger.log('Vision daemon is not running! Start the vision daemon before you start any modules.')
    elif args.which == 'stop':
        if args.modules:
            if running:
                logger.log('Stopping modules {}'.format(', '.join(args.modules)))
                stop_modules(args.modules)
            else:
                logger.error('Vision daemon is not running! Start the vision daemon before you stop any modules.')
        else:
            logger.log('Stopping vision daemon!')
            stop_daemon()
    elif args.which == 'status':
        status()


# Copyright (c) nexB Inc. and others. All rights reserved.
# http://nexb.com and https://github.com/nexB/vulnerablecode/
# The VulnerableCode software is licensed under the Apache License version 2.0.
# Data generated with VulnerableCode require an acknowledgment.
#
# You may not use this software except in compliance with the License.
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#
# When you publish or redistribute any data created with VulnerableCode or any VulnerableCode
# derivative work, you must accompany this data with the following acknowledgment:
#
#  Generated with VulnerableCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES
#  OR CONDITIONS OF ANY KIND, either express or implied. No content created from
#  VulnerableCode should be considered or used as legal advice. Consult an Attorney
#  for any legal advice.
#  VulnerableCode is a free software from nexB Inc. and others.
#  Visit https://github.com/nexB/vulnerablecode/ for support and download.

import bisect
import dataclasses
import json
import re
from typing import List
from typing import Optional
from typing import Tuple

import requests
import saneyaml
import toml
import urllib3
from packageurl import PackageURL
from univers.versions import version_class_by_package_type

# TODO add logging here

cve_regex = re.compile(r"CVE-\d{4}-\d{4,7}", re.IGNORECASE)
is_cve = cve_regex.match
find_all_cve = cve_regex.findall

@dataclasses.dataclass(order=True, frozen=True)
class AffectedPackage:
    vulnerable_package: PackageURL
    patched_package: Optional[PackageURL] = None

def load_yaml(path):
    with open(path) as f:
        return saneyaml.load(f)

def load_json(path):
    with open(path) as f:
        return json.load(f)

def load_toml(path):
    with open(path) as f:
        return toml.load(f)

def fetch_yaml(url):
    response = requests.get(url)
    return saneyaml.load(response.content)

# FIXME: this is NOT how etags work .
# We should instead send the proper HTTP header
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-None-Match
# and integrate this finely in the processing as this typically needs to use
# streaming=True requests, and proper handling of the HTTP return code
# In all cases this ends up being a single request, not a HEADD followed
# by another real request
def create_etag(data_src, url, etag_key):
    """
    Etags are like hashes of web responses. For a data source `data_src`,
    we maintain (url, etag) mappings in the DB.  `create_etag`  creates
    (`url`, etag) pair. If a (`url`, etag) already exists then the code
    skips processing the response further to avoid duplicate work.

    `etag_key` is the name of header which contains the etag for the url.
    """
    etag = requests.head(url).headers.get(etag_key)
    if not etag:
        return True

    elif url in data_src.config.etags:
        if data_src.config.etags[url] == etag:
            return False

    data_src.config.etags[url] = etag
    return True

def contains_alpha(string):
    """
    Return True if the input 'string' contains any alphabet
    """

    return any([c.isalpha() for c in string])

def requests_with_5xx_retry(max_retries=5, backoff_factor=0.5):
    """
    Returns a requests sessions which retries on 5xx errors with
    a backoff_factor
    """
    retries = urllib3.util.Retry(
        total=max_retries,
        backoff_factor=backoff_factor,
        raise_on_status=True,
        status_forcelist=range(500, 600, 1),
    )
    adapter = requests.adapters.HTTPAdapter(max_retries=retries)
    session = requests.Session()
    session.mount("https://", adapter)
    session.mount("http://", adapter)
    return session

def nearest_patched_package(
    vulnerable_packages: List[PackageURL], resolved_packages: List[PackageURL]
) -> List[AffectedPackage]:
    class PackageURLWithVersionComparator:
        """
        This class is used to  get around bisect module's lack of supplying custom
        compartor. Get rid of this once we use python 3.10 which supports this.
        See https://github.com/python/cpython/pull/20556
        """

        def __init__(self, package):
            self.package = package
            self.version_object = version_class_by_package_type[package.type](package.version)

        def __eq__(self, other):
            return self.version_object == other.version_object

        def __lt__(self, other):
            return self.version_object < other.version_object

    vulnerable_packages = sorted(
        [PackageURLWithVersionComparator(package) for package in vulnerable_packages]
    )
    resolved_packages = sorted(
        [PackageURLWithVersionComparator(package) for package in resolved_packages]
    )

    resolved_package_count = len(resolved_packages)
    affected_package_with_patched_package_objects = []

    for vulnerable_package in vulnerable_packages:
        patched_package_index = bisect.bisect_right(resolved_packages, vulnerable_package)
        patched_package = None
        if patched_package_index < resolved_package_count:
            patched_package = resolved_packages[patched_package_index].package

        affected_package_with_patched_package_objects.append(
            AffectedPackage(
                vulnerable_package=vulnerable_package.package, patched_package=patched_package
            )
        )

    return affected_package_with_patched_package_objects

def split_markdown_front_matter(text: str) -> Tuple[str, str]:
    r"""
    Return a tuple of (front matter, markdown body) strings split from ``text``.
    Each can be an empty string.

    >>> text='''---
    ... title: DUMMY-SECURITY-2019-001
    ... description: Incorrect access control.
    ... cves: [CVE-2042-1337]
    ... ---
    ... # Markdown starts here
    ... '''
    >>> split_markdown_front_matter(text)
    ('title: DUMMY-SECURITY-2019-001\ndescription: Incorrect access control.\ncves: [CVE-2042-1337]', '# Markdown starts here')
    """
    # The doctest contains \n and for the sake of clarity I chose raw strings than escaping those.
    lines = text.splitlines()
    if lines[0] == "---":
        lines = lines[1:]
        text = "\n".join(lines)
        frontmatter, _, markdown = text.partition("\n---\n")
        return frontmatter, markdown

    return "", text


import sys
import argparse
import math
from cache import Cache
from cache import Block

# Check Arguments:
if len(sys.argv)!=11:
    print("USAGE: " + sys.argv[0] + " -f <trace file> -s <cache size KB> -b <block size> -a <associativity> -r <replacement policy>")
    sys.exit(1)

# Start Parsing:
parser = argparse.ArgumentParser()
parser.add_argument('-f', type=str, action='store', dest="Trace_File")
parser.add_argument('-s', type=int, action='store', dest="Cache_Size")
parser.add_argument('-b', type=int, action='store', dest="Block_Size")
parser.add_argument('-a', type=int, action='store', dest="Associativity")
parser.add_argument('-r', type=str, action='store', dest="R_Policy")

# Save Arguments:
results = parser.parse_args()

# Check Arguments:
if (results.s < 1) or (results.s > 8192):
    print("ERROR: Cache Size Less than 1KB or More Than 8MB")
    sys.exit(1)
if (results.b < 4) or (results.b > 64):
    print("ERROR: Block Size is Less Than 4 Bytes, or Greater Than 64 Bytes")
    sys.exit(1)
associativity = [1, 2, 4, 8, 16]
if not (results.a in associativity):
    print("ERROR: Associativity Usage - 1, 2, 4, 8, 16")
    sys.exit(1)
replace_policy = ["RR", "RND", "LRU"]
if not (results.r in replace_policy):
    print("ERROR: Replacement Policy - RR, LRU, RND:")
    sys.exit(1)

# Set Variables from Arguments:
cacheSize = results.s
blockSize = results.b
associativity = results.a
r_policy = results.r
traceFile = results.f
if (r_policy == "RND"):
    policyPrint = "Random"
if (r_policy == "RR"):
    policyPrint = "Round Robin"
if (r_policy == "LRU"):
    policyPrint = "Least Recently Used"

# Read Trace File:
traceFile = open(traceFile, "r")
if not traceFile:
    print("ERROR: Cannot open or find Trace File")
    sys.exit(1)

# Calculate Cache:
numberOfBlocks = (((cacheSize * 1024) / blockSize) / 1024) # Blocks in Bits
totalBlocks = ((cacheSize * 1024) / blockSize)
indexSize = int(math.log((numberOfBlocks * 1024 // associativity), 2))
offsetSize = math.log(blockSize, 2)
tagSize = int(32 - indexSize - offsetSize)
numberOfIndices = (2 ** indexSize)
calculatedIndicesKB = numberOfIndices / 1024
totalRows = (2 ** indexSize) / 1024
rows = int(totalRows * 1024)
overheadMemory = ((tagSize + 1) * associativity * numberOfIndices) / 8
mem = (overheadMemory) + (cacheSize * 1024)
memKB = mem / 1024
cost = (float(mem)/1024) * 0.05

# Display Header:
print("")
print("Cache Sumulator CS 3853 Spring 2020 - Group #18")
print("Trace File: " + results.f)
print("")
print("***** Cache Input Parameters *****")
print("Cache Size: \t\t\t" + str(results.s) + " KB")
print("Block Size: \t\t\t" + str(results.b) + " bytes")
print("Associativity: \t\t\t" + str(results.a))
print("Replacement Policy: \t\t" + str(policyPrint))

# Display Calculations:
print("")
print("***** Cache Calculated Values *****")
print("")
print("Total # Blocks: \t\t%.0f" % totalBlocks)
print("Tag Size: \t\t\t{}".format(tagSize)+" bits")
print("Index Size: \t\t\t{}".format(indexSize)+" bits")
print("Total # Rows: \t\t\t{}".format(numberOfIndices))
print("Overhead Size: \t\t\t%.0f" % overheadMemory + " bytes")
print("Implementation Memory Size: \t%.2f KB  " % memKB + "(%.0f bytes)" % mem)
print("Cost: \t\t\t\t$%.2f " % cost)
print("")
print("")

# Display Cache Results:
cache = Cache(traceFile, rows, blockSize, tagSize, associativity, indexSize, offsetSize, r_policy)
cache.cacheMe()
cache.outputCache()