# !/usr/bin/python
import schedule
import functools
import sys
import time

from configfile import config
from dao.minerdao import MinerDao
from dao.payoutdao import PayoutDao
from dao.workerdao import WorkerDao

from etherminepl import Eterminepl

class App(object):

    __appstat = config["app"]
    __json = None
    json_err = False

    # miner_stat = dict(balance="0", chashrate="0", ahashrate="0")

    """docstring for App"""

    def __init__(self):
        super(App, self).__init__()
        # self.__json = self.get_json_from_file()
        self.ethpool = Eterminepl(self.__appstat["wallet"])
        self.ethpool.get_all_response()
        if self.ethpool.get_response_status() == False:
            self.json_err = True
        else:
            self.json_err = False

        # if self.get_jsondata() == 1:
        #     print("Could not fetch JSON from host")

    def get_json_err(self):
        return self.json_err

    def insert_in_worker(self, sessionid):
        __json_workername = []
        _json = self.ethpool.get_workers()
        for val in _json['data']:
            __json_workername.append(val['worker'])

        _workerDao = WorkerDao()

        __db_workername = _workerDao.get_worker()
        __res = self.returnNotMatches(__db_workername, __json_workername)

        for val in _json['data']:
            _workerDao.set_worker(val, sessionid)
            _workerDao.insert_worker()
            _workerDao.insert_worker_stat()

        if __res[0] > __res[1]:
            for val in __res[0]:
                _workerDao.set_worker(val, sessionid, False)
                _workerDao.insert_worker()
                _workerDao.insert_worker_stat()

        if not __res[0]:
            r = self.returnNotMatches(
                config["app"]["workers"], __json_workername)
            for val in r[0]:
                _workerDao.set_worker(val, sessionid, False)
                _workerDao.insert_worker()
                _workerDao.insert_worker_stat()

        _workerDao.close_conn()

    def insert_in_payout(self):
        _json = self.ethpool.get_payouts()
        for val in _json['data']:
            _payoutdao = PayoutDao(val)
            _payoutdao.insert_payout()
            _payoutdao.insert_paidstatus(config["app"]["workers"])
            _payoutdao.close_conn()

    def insert_in_minerstat(self):
        _json = self.ethpool.get_currentStats()
        _minerdao = MinerDao(_json)
        sessionid = _minerdao.insert_mining()
        _minerdao.close_conn()
        return sessionid

    def returnNotMatches(self, a, b):
        return [[x for x in a if x not in b], [x for x in b if x not in a]]

    # def get_json_from_file(self):
    #     with open('tmp.json') as f:
    #         return js.load(f)

# This decorator can be applied to
def with_logging(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        print('LOG: Running job "%s"' % func.__name__)
        result = func(*args, **kwargs)
        print('LOG: Job "%s" completed' % func.__name__)
        return result
    return wrapper

@with_logging
def job():
    work = App()
    if work.get_json_err():
        print("Could not fetch JSON from host")
    else:
        sessionid = work.insert_in_minerstat()
        work.insert_in_worker(sessionid)
        work.insert_in_payout()
        print("row insert: ", time.ctime())

if __name__ == '__main__':
    try:
        print("start: %s" % time.ctime())
        schedule.every(config["app"]["interval"]).minutes.do(job)
        while True:
            schedule.run_pending()
            time.sleep(1)
    except KeyboardInterrupt as e:
        sys.exit(0)

# if __name__ == '__main__':
#     try:
#         work = App()
#         if work.get_json_err():
#             print("Could not fetch JSON from host")
#         else:
#             sessionid = work.insert_in_minerstat()
#             work.insert_in_worker(sessionid)
#             work.insert_in_payout()
#             print("row insert: ", time.ctime())
#     	# Exit cleanly on ctl-c
#     except KeyboardInterrupt as e:
#     	sys.exit(0)


"""
Code adapted from: https://github.com/locuslab/TCN/blob/master/TCN/tcn.py
"""

import torch
import torch.nn as nn
from torch.nn.utils import weight_norm

class Chomp2d(nn.Module):
    def __init__(self, chomp_size):
        super(Chomp2d, self).__init__()
        self.chomp_size = chomp_size

    def forward(self, x):
        return x[:, :, :-self.chomp_size, :-self.chomp_size].contiguous()

class TemporalBlock2DHW(nn.Module):
    def __init__(self, n_inputs, n_outputs, kernel_size, stride, dilation, padding, dropout=0.2, apply_weight_norm=False):
        super(TemporalBlock2DHW, self).__init__()
        self.conv1 = nn.Conv2d(n_inputs, n_outputs, kernel_size, stride=stride, padding=padding, dilation=dilation)
        self.chomp1 = Chomp2d(padding)
        self.relu1 = nn.ReLU()
        self.dropout1 = nn.Dropout(dropout)

        '''
        self.conv2 = nn.Conv2d(n_outputs, n_outputs, kernel_size, stride=stride, padding=padding, dilation=dilation)
        self.chomp2 = Chomp2d(padding)
        self.relu2 = nn.ReLU()
        self.dropout2 = nn.Dropout(dropout)
        '''

        if apply_weight_norm:
            self.conv1 = weight_norm(self.conv1)
            #self.conv2 = weight_norm(self.conv2)

        self.relu = nn.ReLU()
        self.init_weights()
        self.kernel_size = kernel_size

    def init_weights(self):
        torch.nn.init.xavier_normal_(self.conv1.weight.data, 0.02)
        #torch.nn.init.xavier_normal_(self.conv2.weight.data, 0.02)

    def forward(self, x):
        out = self.conv1(x)

        if self.kernel_size > 1:
            out = self.chomp1(out)
        out = self.relu1(out)
        out = self.dropout1(out)
        '''
        out = self.conv2(out)
        out = self.chomp2(out)
        out = self.relu2(out)
        out = self.dropout2(out)
        '''
        res = x
        return self.relu(out + res)

class TemporalConvNet2DHW(nn.Module):
    def __init__(self, num_inputs, num_channels, kernel_size=2, dropout=0.2):
        super(TemporalConvNet2DHW, self).__init__()
        layers = []
        num_levels = len(num_channels)
        for i in range(num_levels):
            dilation_size = 2 ** i
            in_channels = num_inputs if i == 0 else num_channels[i-1]
            out_channels = num_channels[i]
            layers += [TemporalBlock2DHW(in_channels, out_channels, kernel_size, stride=1, dilation=dilation_size,
                                     padding=(kernel_size-1) * dilation_size, dropout=dropout)]

        self.network = nn.Sequential(*layers)

    def forward(self, x):
        x = self.network(x)

        return x


#!/usr/bin/env python

"""
@package ion_functions.test.generic_functions
@file ion_functions/test/generic_functions.py
@author Christopher Mueller
@brief Unit tests for generic_functions module
"""

from nose.plugins.attrib import attr
from ion_functions.test.base_test import BaseUnitTestCase

import numpy as np
from ion_functions.data import generic_functions as gfunc

@attr('UNIT', group='func')
class TestGenericFunctionsUnit(BaseUnitTestCase):

    def test_magnetic_declination(self):
        """
        Test magnetic_declination function.

        Some values based on those defined in the WMM document,
        WMM2010testvalues.pdf which accompanies the software.  Others
        were created and checked using online calculators.

        Implemented by Stuart Pearce, April 2013
        """

        lat = np.array([45.0, 45.0, 80.0, 0.0, -80.0, 80.0, 0.0, -80.0])
        lon = np.array([-128.0, -128.0, 0.0, 120.0,
                        240.0, 0.0, 120.0, 240.0])
        z = np.array([0.0, 1000.0, 0.0, 0.0,
                      0.0, 100000.0, 100000.0, 100000.0])
        timestamp = np.array([3575053740.7382507,  # 2013-04-15 22:29:00
                              3575053740.7382507,  # UTC
                              3471292800.0,        # 2010-01-01 UTC
                              3471292800.0,
                              3471292800.0,
                              3471292800.0,
                              3471292800.0,
                              3471292800.0])
        zflag = np.array([-1, -1, -1, -1, -1, 1, 1, 1])
        output = []
        for ii in range(len(lat)):
            out_ = gfunc.magnetic_declination(lat[ii],lon[ii],
                                              timestamp[ii],z[ii],zflag[ii])
            output.append(out_)
        output = np.array(output)

        check_values = np.array([16.46093044096720,
                                 16.46376239313584,
                                 -6.13,
                                 0.97,
                                 70.21,
                                 -6.57,
                                 0.94,
                                 69.62])
        self.assertTrue(np.allclose(output, check_values,
                                    rtol=0, atol=1e-2))

    def test_ntp_to_unix_time(self):
        """
        Test ntp_to_unix_time function.

        Timestamp Values gathered from various internet sources
        including the NTP FAQ and HOWTO.

        Implemented by Stuart Pearce, April 2013
        """
        ntp_timestamps = np.array([3176736750.7358608,
                                   3359763506.2082224,
                                   3575049755.4380851])

        output = gfunc.ntp_to_unix_time(ntp_timestamps)

        check_values = np.array([967747950.735861,
                                 1150774706.2082224,
                                 1366060955.438085])
        self.assertTrue(np.allclose(output, check_values,
                                    rtol=0, atol=1e-6))

    def test_extract_parameters(self):
        """
        Test extract_parameter function.

        Array values created by author.

        Implemented by Christopher Wingard, April 2013
        """

        in_array = np.array([34, 67, 12, 15, 89, 100, 54, 36])
        self.assertTrue(np.equal(34, gfunc.extract_parameter(in_array, 0)))
        self.assertTrue(np.equal(67, gfunc.extract_parameter(in_array, 1)))
        self.assertTrue(np.equal(12, gfunc.extract_parameter(in_array, 2)))
        self.assertTrue(np.equal(15, gfunc.extract_parameter(in_array, 3)))
        self.assertTrue(np.equal(89, gfunc.extract_parameter(in_array, 4)))
        self.assertTrue(np.equal(100, gfunc.extract_parameter(in_array, 5)))
        self.assertTrue(np.equal(54, gfunc.extract_parameter(in_array, 6)))
        self.assertTrue(np.equal(36, gfunc.extract_parameter(in_array, 7)))


import re
import json

question_list = []

def Question_Filled(question):
    if not question["Number"]:
        return False
    if not question["Question"]:
        return False
    if not question["A"]:
        return False
    if not question["B"]:
        return False
    if not question["C"]:
        return False
    if not question["D"]:
        return False
    return True

def Blank_Question():
    question = {
        "Number": 1,
        "Question" : "",
        "A" : "",
        "B" : "",
        "C" : "",
        "D" : "",
        "Answer": ""
    }
    return question

def Add_Question(question):
    question_list.append(question)

def main():
    question_number = 1
    question_pattern = r"Question\s[\d]+\:\s([\S\s]+\?)"
    answer_pattern = r"([\w]+)\.\s+([\S\s]+)\."

    question = Blank_Question()

    with open(r"c:/Code/GTBootCampMain/04-Web-APIs/02-Homework/Assets/Questions.txt", encoding="utf8") as questions:
        while True:
            line = questions.readline()
            if not line:
                break
            if line.rstrip():
                match = re.search(question_pattern, line)
                if match:
                    question["Number"] = question_number
                    question_number += 1
                    question["Question"] = match.group(1)

                match = re.search(answer_pattern, line)
                if match:
                    question[match.group(1)]  = match.group(2)

                if Question_Filled(question):
                    Add_Question(question)
                    question = Blank_Question()

    question_number = 0
    with open(r"c:/Code/GTBootCampMain/04-Web-APIs/02-Homework/Assets/Answers.txt", encoding="utf8") as answers:
        while True:
            line = answers.readline()
            if not line:
                break
            if line.rstrip():
                match = re.search(answer_pattern, line)
                if match:
                    question_list[question_number]["Answer"] = match.group(1)
                    question_number += 1

    f = open(r"c:/Code/GTBootCampMain/04-Web-APIs/02-Homework/Assets/Questions.json", "w")
    f.write(json.dumps(question_list))
    f.close()

if(__name__) == "__main__":
    main()


from django.conf import settings
from django.core import mail
from django.test import TestCase, override_settings, tag
from django.urls import reverse

from django_otp.oath import TOTP
from django_otp.plugins.otp_totp.models import TOTPDevice
from magicauth import settings as magicauth_settings

from aidants_connect_web.tests.factories import AidantFactory

@tag("usagers")
class LoginTests(TestCase):
    @classmethod
    def setUpTestData(cls):
        cls.aidant = AidantFactory(
            is_active=False,
            post__with_otp_device=True,
            post__with_carte_totp=True,
        )
        cls.aidant_with_totp_card = AidantFactory(post__with_carte_totp=True)

    def test_inactive_aidant_with_valid_totp_cannot_login(self):
        response = self.client.post(
            "/accounts/login/", {"email": self.aidant.email, "otp_token": "123456"}
        )
        self.assertEqual(response.status_code, 200)
        # Check explicit message is displayed
        self.assertContains(
            response, "Votre compte existe mais il n’est pas encore actif."
        )
        # Check no email was sent
        self.assertEqual(len(mail.outbox), 0)

    def test_magicauth_login_redirects(self):
        response = self.client.get(f"/{magicauth_settings.LOGIN_URL}")

        self.assertRedirects(response, reverse("login"), status_code=301)

    # Prevent being throttled between test attemps
    @override_settings(OTP_TOTP_THROTTLE_FACTOR=0)
    def test_tolerance_on_otp_challenge(self):
        totp_device: TOTPDevice = self.aidant_with_totp_card.carte_totp.totp_device
        token_generator = TOTP(
            totp_device.bin_key,
            totp_device.step,
            totp_device.t0,
            totp_device.digits,
            totp_device.drift,
        )

        def login_and_assert(token, tolerance=1):
            response = self.client.post(
                "/accounts/login/",
                {"email": self.aidant_with_totp_card.email, "otp_token": token},
            )

            self.assertEqual(200, response.status_code)
            totp_device.refresh_from_db()
            self.assertEqual(tolerance, totp_device.tolerance)

        login_and_assert(token_generator.token() + 1, 1)
        login_and_assert(token_generator.token() + 1, 1)
        login_and_assert(
            token_generator.token() + 1, settings.DRIFTED_OTP_CARD_TOLERANCE
        )

        # Login with correct token
        response = self.client.post(
            "/accounts/login/",
            {
                "email": self.aidant_with_totp_card.email,
                "otp_token": token_generator.token(),
            },
        )

        self.assertEqual(302, response.status_code)
        # Simulate proper login
        self.client.force_login(self.aidant_with_totp_card)
        totp_device.refresh_from_db()
        self.assertEqual(1, totp_device.tolerance)


# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#     http://www.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.
#
import logging
import struct

from iotdb.utils.Field import Field

# for package
from iotdb.utils.IoTDBConstants import TSDataType
from iotdb.utils.IoTDBRpcDataSet import IoTDBRpcDataSet
from iotdb.utils.RowRecord import RowRecord

import pandas as pd

logger = logging.getLogger("IoTDB")

class SessionDataSet(object):
    def __init__(
        self,
        sql,
        column_name_list,
        column_type_list,
        column_name_index,
        query_id,
        client,
        statement_id,
        session_id,
        query_data_set,
        ignore_timestamp,
    ):
        self.iotdb_rpc_data_set = IoTDBRpcDataSet(
            sql,
            column_name_list,
            column_type_list,
            column_name_index,
            ignore_timestamp,
            query_id,
            client,
            statement_id,
            session_id,
            query_data_set,
            1024,
        )

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.close_operation_handle()

    def get_fetch_size(self):
        return self.iotdb_rpc_data_set.get_fetch_size()

    def set_fetch_size(self, fetch_size):
        self.iotdb_rpc_data_set.set_fetch_size(fetch_size)

    def get_column_names(self):
        return self.iotdb_rpc_data_set.get_column_names()

    def get_column_types(self):
        return self.iotdb_rpc_data_set.get_column_types()

    def has_next(self):
        return self.iotdb_rpc_data_set.next()

    def next(self):
        if not self.iotdb_rpc_data_set.get_has_cached_record():
            if not self.has_next():
                return None
        self.iotdb_rpc_data_set.has_cached_record = False
        return self.construct_row_record_from_value_array()

    def construct_row_record_from_value_array(self):
        out_fields = []
        for i in range(self.iotdb_rpc_data_set.get_column_size()):
            index = i + 1
            data_set_column_index = i + IoTDBRpcDataSet.START_INDEX
            if self.iotdb_rpc_data_set.get_ignore_timestamp():
                index -= 1
                data_set_column_index -= 1
            column_name = self.iotdb_rpc_data_set.get_column_names()[index]
            location = (
                self.iotdb_rpc_data_set.get_column_ordinal_dict()[column_name]
                - IoTDBRpcDataSet.START_INDEX
            )

            if not self.iotdb_rpc_data_set.is_null_by_index(data_set_column_index):
                value_bytes = self.iotdb_rpc_data_set.get_values()[location]
                data_type = self.iotdb_rpc_data_set.get_column_type_deduplicated_list()[
                    location
                ]
                field = Field(data_type)
                if data_type == TSDataType.BOOLEAN:
                    value = struct.unpack(">?", value_bytes)[0]
                    field.set_bool_value(value)
                elif data_type == TSDataType.INT32:
                    value = struct.unpack(">i", value_bytes)[0]
                    field.set_int_value(value)
                elif data_type == TSDataType.INT64:
                    value = struct.unpack(">q", value_bytes)[0]
                    field.set_long_value(value)
                elif data_type == TSDataType.FLOAT:
                    value = struct.unpack(">f", value_bytes)[0]
                    field.set_float_value(value)
                elif data_type == TSDataType.DOUBLE:
                    value = struct.unpack(">d", value_bytes)[0]
                    field.set_double_value(value)
                elif data_type == TSDataType.TEXT:
                    field.set_binary_value(value_bytes)
                else:
                    raise RuntimeError("unsupported data type {}.".format(data_type))
            else:
                field = Field(None)
            out_fields.append(field)

        return RowRecord(
            struct.unpack(">q", self.iotdb_rpc_data_set.get_time_bytes())[0], out_fields
        )

    def close_operation_handle(self):
        self.iotdb_rpc_data_set.close()

    def todf(self):
        return resultset_to_pandas(self)

def resultset_to_pandas(result_set: SessionDataSet) -> pd.DataFrame:
    """
    Transforms a SessionDataSet from IoTDB to a Pandas Data Frame
    Each Field from IoTDB is a column in Pandas
    :param result_set:
    :return:
    """
    return result_set.iotdb_rpc_data_set.resultset_to_pandas()

def get_typed_point(field: Field, none_value=None):
    choices = {
        # In Case of Boolean, cast to 0 / 1
        TSDataType.BOOLEAN: lambda field: 1 if field.get_bool_value() else 0,
        TSDataType.TEXT: lambda field: field.get_string_value(),
        TSDataType.FLOAT: lambda field: field.get_float_value(),
        TSDataType.INT32: lambda field: field.get_int_value(),
        TSDataType.DOUBLE: lambda field: field.get_double_value(),
        TSDataType.INT64: lambda field: field.get_long_value(),
    }

    result_next_type: TSDataType = field.get_data_type()

    if result_next_type in choices.keys():
        return choices.get(result_next_type)(field)
    elif result_next_type is None:
        return none_value
    else:
        raise Exception(f"Unknown DataType {result_next_type}!")


from django.template import RequestContext
from django.shortcuts import render_to_response
from django.conf import settings
from lib.utilities import pymongo_query, format_query_output, get_log_window, format_query_input
from netshed.greylist.models import Greylist
import time

def index(request):
    """ GET request to mongoengine db queries """

    query = dict([(str(param), str(val).lower()) for param, val in request.GET.iteritems() if val])

    search_time = 0
    loglines = []
    db_results = ''
    if 'search' in query:
        if query['search'] == 'search':

            # generic pymongo query from utilities
            start = time.time()
            loglines = pymongo_query(query, 'greylist', settings.LOCAL)
            search_time = start - time.time()

        # greylist database search
        elif query['search'] == 'database query':
            del query['date']
            del query['search']

            start = time.time()
            db_results = Greylist.objects.filter(**format_query_input(query))
            search_time = start - time.time()

    dates = get_log_window('greylist', settings.LOCAL)
    return render_to_response('greylist/index.html', {'db_results': db_results, 'query': format_query_output(query), 'dates':dates, 'loglines':loglines, 'time':search_time})


import socketio
import eventlet
from template_matching import get_game_state
from algorithm import get_command

# create a Socket.IO server
sio = socketio.Server()

@sio.on('send_screenshot')
def send_screenshot(sid, screenshot):
	game_state = get_game_state(screenshot.replace('data:image/png;base64,', ''))
	trex = game_state['trex_location']
	obstacles = game_state['obstacle_locations']
	command = get_command(trex, obstacles)
	sio.emit('execute_command', command)

def main():
	# wrap with a WSGI application
	app = socketio.WSGIApp(sio)
	eventlet.wsgi.server(eventlet.listen(('', 8000)), app)

if __name__ == '__main__':
	main()


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

import os
import subprocess
from git import Repo
import json
import logging

logging.basicConfig(level=logging.DEBUG,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger('info_branchs')

def get_all_branches_info(path):
    res = []
    for lFile in os.listdir(path):
        p = os.path.join(path, lFile)
        if os.path.isdir(p) \
                and lFile not in ['.', '..'] \
                and not os.path.islink(p):
            if lFile == '.git':
                logger.debug("Found a git folder %s", p)
                info = {}
                repo = Repo(p)
                info.update({'path': path,
                    'repo_url': str(repo.remotes.origin.url),
                    'branch': str(repo.head.ref),
                    'commit': str(repo.head.reference.commit),
                    'is_dirty': repo.is_dirty()})
                res.append(info)
            else:
                r = get_all_branches_info(p)
                if r:
                    res = res + r
    return res

if __name__ == '__main__':
    b_info = get_all_branches_info('/home/truiz/working/lodigroup/instance_as_prod')
    with open('branches_info.txt', 'w') as fout:
        json.dump(b_info, fout, sort_keys=True, indent=4, ensure_ascii=False)

    #_apply_recursive('/home/truiz/working/backupws')


#!/home/lmanrique/anaconda3/bin/python

import numpy as np
import statistics
import time

def time_stat(func, size, ntrials):

  # the time to generate the random array should not be included
  x = 0
  sum_time = 0

  while x < ntrials:
    data = np.random.rand(size)

    # modify this function to time func with ntrials times using a new random array each time
    start = time.perf_counter()
    res = func(data)
    sum_time += (time.perf_counter() - start)
    x += 1

    # return the average run time
  return sum_time / ntrials

if __name__ == '__main__':
  print('{:.6f}s for statistics.mean'.format(time_stat(statistics.mean, 10**5, 30)))
  print('{:.6f}s for np.mean'.format(time_stat(np.mean, 10**5, 30)))


import time
from Bubot.Helpers.ExtException import ExtException, Unauthorized, AccessDenied
from Bubot.Helpers.Action import Action, async_action
from aiohttp_session import get_session
# from bubot.Helpers.Сryptography.SignedData import SignedData
from aiohttp import web
from Bubot.Core.BubotHelper import BubotHelper
from BubotObj.OcfDevice.subtype.WebServer.ApiHelper import WebResponse as Response
# from bubot.Catalog.Account.Account import Account
from BubotObj.User.User import User
import re
from bson.json_util import dumps, loads

class ApiHandler:
    clear = re.compile('[^a-zA-Z0-9._]')
    api_class_cache = {}

    def __init__(self, request):
        self.session = None
        # self.request = request
        self.storage = request.app['storage']
        self.app = request.app
        self.device = request.app['device']
        self.log = self.device.log

    async def prepare(self, request, device, obj_name, action, prefix, response_class):
        try:
            api_class = self.get_api_class(device, obj_name)(response_class)
        except Exception as err:
            raise ExtException('Handler not found', detail=f'{device}/{obj_name}', parent=err)
        api_action = f'{prefix}_{action}'
        self.session = await get_session(request)
        self.session['last_visit'] = time.time()
        try:
            task = getattr(api_class, api_action)
        except Exception as err:
            raise ExtException('Handler not found', detail=f'{device}/{obj_name}/{action}')
        return task(self)

    # async def get_json_data(self):
    #     data = await self.request.text()
    #     if not data:
    #         raise Exception('empty data')
    #     return loads(data)

    def get_api_class(self, device, obj_name):
        if obj_name:
            uid = f'{device}.{obj_name}'
        else:
            uid = device
        try:
            return self.api_class_cache[uid]
        except KeyError:
            pass
        if obj_name:
            try:
                api_class = BubotHelper.get_extension_class(obj_name, device, suffix='Api')
            except:
                api_class = BubotHelper.get_obj_class(obj_name, suffix='Api')
        else:
            api_class = BubotHelper.get_subtype_class('OcfDevice', device, suffix='Api')
        self.api_class_cache[uid] = api_class
        return api_class

    @async_action
    async def check_right(self, **kwargs):
        kwargs['account'] = kwargs['account'] if kwargs.get('account') else self.session['account']
        kwargs['user'] = self.session.get('user')
        if not kwargs['user']:
            raise Unauthorized()
        if not kwargs['account']:
            raise AccessDenied()
        kwargs['storage'] = self.storage
        return await User.check_right(**kwargs)

    @staticmethod
    async def loads_request_data(view):
        data = await view.request.text()
        return loads(data) if data else {}

    @staticmethod
    async def loads_json_request_data(view):
        if view.request.method == 'GET':
            return dict(view.request.query)
        else:
            return await view.loads_request_data(view)

class HttpHandler(web.View, ApiHandler):

    def __init__(self, request):
        web.View.__init__(self, request)
        ApiHandler.__init__(self, request)
        # self.storage = request.app['storage']
        # self.app = request.app
        # self.session = None
        # self.data = None
        self.lang = request.headers.get('accept-language')
        pass

    async def get(self):
        return await self.request_handler('api')

    async def post(self):
        async def www_form_decode():
            return dict(await self.request.post())
            pass

        async def json_decode():
            return await self.request.json()

        data_decoder = {
            'application/x-www-form-urlencoded': www_form_decode,
            'application/json': json_decode
        }
        data_type = self.request.headers.get('content-type')
        if data_type and data_type in data_decoder:
            self.data = await data_decoder[data_type]()

        return await self.request_handler('api')

    async def request_handler(self, prefix, **kwargs):
        _action = Action(name=f'{self.__class__.__name__}.request_handler')
        device = self.request.match_info.get('device')
        obj_name = self.request.match_info.get('obj_name')
        action = self.request.match_info.get('action')

        try:
            task = await self.prepare(self.request, device, obj_name, action, prefix, Response)
            response = _action.add_stat(await task)
            _action.set_end()
            response.headers['stat'] = dumps(_action.stat, ensure_ascii=True)
            return response
        except ExtException as err:
            return Response.json_response(ExtException(
                action=_action.name,
                dump={
                    "device": device,
                    "obj_name": obj_name,
                    "action": action
                },
                parent=err).to_dict(), status=err.http_code)
        except Exception as err:
            return Response.json_response(ExtException(action=_action.name, parent=err).to_dict(), status=500)

    async def notify(self, data):
        return


import traceback
import lark

TAB = "    "

TYPE_MAP = {
	"String": "str",
	"Boolean": "bool",
	"Byte": "int",
	"Integer": "int",
	"Long": "int",
	"LongLong": "int",
	"Single": "float",
	"Double": "float",
	# Currency, Date, Variant
	}

TYPE_SUFFIX_MAP = {
	"$": "str",
	"%": "int", 	# Integer
	"&": "int", 	# Long
	"#": "float", 	# Double
	"!": "float", 	# Single
	# "@":  "float", # Currency
	}

OP_MAP = {
	"=": "==",
	"<>": "!=",
	"And": "and", 	# approximate equivalence. Python does lazy evaluation
	"AndAlso": "and",
	"Or": "or", 	# approximate equivalence. Python does lazy evaluation
	"OrElse": "or",
	"&": "+",
	"Mod": "%",
	"Is": "is",
	}

class GenerateInfos:
	def __init__(self):
		self.indent = ""
		self.option_explicit = False
		self.option_base = 0
		self.def_func_name = None # to handle return value
		self.on_error_resume_next = False
		self.on_error_goto = None
		self.with_stack = []
		self.global_vars = []
		self.vars = self.global_vars

class CustomException(Exception):
    pass

class CustomRethrowException(Exception):
    def __init__(self, message, inner_error):
        super().__init__(message)
        #self.inner_error = inner_error

def generate(ast, verbose=False):
	infos = GenerateInfos()

	src = []
	src.append("import os, sys, numpy as np")
	src.append("")
	src.append("""#sys.path.append(r"..\..\lark\examples")""")
	src.append("from vba_py_app import *")
	src.append("")

	if verbose:
		print(ast.pretty())

	generate_stmts(ast, src, infos)

	print(f"Code generated. \t #lines{len(src)}")
	return '\n'.join(src)

has_unsupported_feature = False
def unsupported_feature(msg, src, infos):
	global has_unsupported_feature
	has_unsupported_feature = True

	print(">!", msg)
	msg = f"{infos.indent}raise Exception(\"{msg}\")";
	if src is not None:
		src.append(msg)
	else:
		return src
	#assert False

def generate_stmts(ast, src, infos):
	assert get_type(ast) == "statements"

	stmts = ast.children
	if len(stmts)==0:
		src.append(f"{infos.indent}pass")
	for s in stmts:
		generate_stmt(s, src, infos)

def generate_stmt(ast, src, infos):
	try:
		generate_stmt_throws(ast, src, infos)
	except Exception as e:
		traceback.print_exc()
		if is_node(ast):
			print("\nAST:\n", ast.pretty(), "\n")
		else:
			print("\nAST is TOKEN:\n", ast, "\n")

		src.append("")
		src.append(f"{infos.indent}#!! Generation error here")
		src.append(f"{infos.indent}raise Exception('Generation error here')")
		src.append("")
		#raise

def generate_stmt_throws(ast, src, infos):
	s = ast
	t = get_type(ast)
	n = len(ast.children) if is_node(ast) else None

	# print("generate_stmt", t )
	if t == "statements":
		generate_stmts(ast, src, infos)
	elif t == "statements_inline":
		for i in range(0, n, 2):
			generate_stmt(ast.children[i], src, infos)
	elif t == "option_stmt":
		generate_option_stmt(ast, src, infos)
	elif t == "dim_stmt":
		generate_dim_stmt(ast, src, infos)
	elif t == "comment_stmt":
		txt = generate_fragment(ast, infos)
		src.append(f"{infos.indent}{txt}")
	elif t == "NEWLINE":
		src.append("")
	elif t == "function_definition_stmt":
		generate_function_definition_stmt(ast, src, infos)
	elif t == "exit_stmt":
		generate_exit_stmt(ast, src, infos)
	elif t == "sub_call_stmt":
		generate_sub_call_stmt(ast, src, infos)
	elif t == "assign_stmt":
		generate_assign_stmt(ast, src, infos)
	elif t == "if_stmt_multilines":
		generate_if_stmt(ast, src, infos)
	elif t == "if_stmt_inline":
		generate_if_stmt(ast, src, infos, inline = True)
	elif t == "for_in_stmt":
		generate_for_in_stmt(ast, src, infos)
	elif t == "for_each_stmt":
		generate_for_each_stmt(ast, src, infos)
	elif t == "while_wend_stmt":
		generate_loop_stmt(ast, src, infos, pre_condition=True, while_condition=True)
	elif t == "while_do_loop_stmts":
		generate_loop_stmt(ast, src, infos, pre_condition=True, while_condition=True)
	elif t == "do_loop_while_stmts":
		generate_loop_stmt(ast, src, infos, pre_condition=False, while_condition=True)
	elif t == "until_do_loop_stmts":
		generate_loop_stmt(ast, src, infos, pre_condition=True, while_condition=False)
	elif t == "do_loop_until_stmts":
		generate_loop_stmt(ast, src, infos, pre_condition=False, while_condition=False)
	elif t == "with_stmt":
		generate_with_stmt(ast, src, infos)
	elif t == "on_error_stmt":
		generate_on_error_stmt(ast, src, infos)
	elif t == "goto_anchor_stmt":
		generate_goto_anchor_stmt(ast, src, infos)
	elif t == "smnt_new_line":
		if n==1:
			# only new line
			pass
		else:
			# comment and newline
			txt = generate_fragment(ast.children[0], infos)
			src.append(f"{infos.indent}{txt}")
	elif t == "empty_new_line":
		src.append("")
	elif t in [ "preprocessor_if_stmt", "goto_stmt", "exit_stmt", "if_stmt_inline", "for_each_stmt" ]:
		# "with_stmt",
		unsupported_feature(f"Unsupported statement type {t}.", src, infos)
	elif t in [ "open_stmt", "print_stmt", "close_stmt" ]:
		unsupported_feature(f"Unsupported file operations (open, print, close).", src, infos)
	else:
		raise Exception(f"Unknown statement type {t}")

def generate_option_stmt(ast, src, infos):
	opt = get_text(ast.children[1])
	if opt == "Explicit":
		infos.option_explicit = True
	elif opt == "Base":
		base = get_int(ast.children[2])
		assert base==0 or base==1 	# as per VBA spec
		infos.option_base = base
	else:
		unsupported_feature(f"Unsupported Option {opt}.", src, infos)

def generate_dim_stmt(ast, src, infos):
	decls = find_nodes_by_type(ast, "dim_decl")
	tmp = []
	for decl in decls:
		var = decl.children[0]
		typ = None
		has_new = False
		range_indices = None

		# implicit type
		if get_type(var) == "TYPED_NAME" and var.value[-1] in TYPE_SUFFIX_MAP:
			typ = TYPE_SUFFIX_MAP[var.value[-1]]
			var = generate_fragment(var, infos)
		# explicit type
		xtyp = find_node_by_type(decl, "type_info")
		if xtyp:
			vb_type = generate_fragment(xtyp.children[-1], infos)
			has_new = find_node_by_type(xtyp, "NEW") is not None
			if vb_type in TYPE_MAP:
				typ = TYPE_MAP[vb_type]
			if has_new:
				typ = vb_type 	# best we can do !!

		xrng = find_node_by_type(decl, "ranges")
		xrngs = find_nodes_by_type(decl, "range")
		if xrng is not None or len(xrngs)>0:
			range_indices = []
		for rng in xrngs:
			if len(rng.children) == 1:
				range_indices.append([ infos.option_base, generate_ith_fragment(rng, 0, infos) ])
			elif len(rng.children) == 3:
				range_indices.append([ generate_ith_fragment(rng, 0, infos), generate_ith_fragment(rng, 2, infos) ])
			else:
				raise False

		if range_indices is not None:
			shape = ", ".join([ f"{rng[1]}-{rng[0]}+1" for rng in range_indices])
			offset = ", ".join([ f"{rng[0]}" for rng in range_indices])
			if typ == "str" or typ == "int" or typ == "float":
				tmp.append(f"{var} = np.ndarray(shape=({shape},), offset_isnt_what_we_need=({offset},), dtype={typ})")
			else:
				tmp.append(f"{var} = np.ndarray(shape=({shape},), offset_isnt_what_we_need=({offset},))")
		elif typ == "ArrayList":
				tmp.append(f"{var} = []")
		else:
			if typ == "str":
				tmp.append(f"{var} = \"\"")
			elif typ == "int":
				tmp.append(f"{var} = 0")
			elif typ == "float":
				tmp.append(f"{var} = 0.0")
			elif has_new:
				tmp.append(f"{var} = {typ}()")
			elif typ is None:
				tmp.append(f"{var} = None")

		infos.vars.append(var)

	src.append(f"{infos.indent}{'; '.join(tmp)}")

def generate_function_definition_stmt(ast, src, infos):
	assert get_type(ast.children[0]) == "function_or_sub"
	assert get_type(ast.children[2]) == "function_args"

	type_anotation = ""
	vb_type = generate_ith_fragment(find_node_by_type(ast, "type_info"), 1, infos)
	if vb_type in TYPE_MAP:
		type_anotation = " -> "+TYPE_MAP[vb_type]
	if get_text(ast.children[0]) == "Sub":
		type_anotation = " -> None "

	name = ast.children[1].value
	args_csv = generate_fragment(ast.children[2], infos)
	stmts = find_node_by_type(ast, "statements")

	src.append(f"def {name}({args_csv}){type_anotation}:" )
	try:
		infos.def_func_name = name
		infos.indent +=TAB
		infos.vars = [] + infos.global_vars 	# clone global scope as local one
		generate_stmts(stmts, src, infos)

		if get_text(ast.children[0]) == "Function":
			src.append(f"{infos.indent}return {infos.def_func_name}")
	finally:
		infos.def_func_name = None
		infos.indent = infos.indent[:-len(TAB)]
		infos.vars = infos.global_vars 	# restore global scope

	src.append(f"") # !!

def generate_exit_stmt(ast, src, infos):
	assert get_type(ast.children[0]) == "EXIT"
	t = get_type(ast.children[1])

	if t == "FUNCTION":
		src.append(f"{infos.indent}return {infos.def_func_name}")
	elif t == "SUB":
		src.append(f"{infos.indent}return")
	elif t == "for":
		# check which for loopwe are exiting from !!
		src.append(f"{infos.indent}break")
	else:
		unsupported_feature("Unsupported Exit {t}", src, infos)

def generate_sub_call_stmt(ast, src, infos):
	if get_type(ast.children[0]) == "call":

		if get_type(ast.children[1]) != "func_call":

			# no arguments, no parentheses, just "Call fct"
			name = generate_fragment(ast.children[1], infos)
			src.append(f"{infos.indent}{name}()")

		else:
			x = generate_func_call(ast.children[1], infos)
			src.append(f"{infos.indent}{x}");

	else:

		assert get_type(ast.children[0]) in [ "any_name", "member_access", "func_call", "with_access" ]
		#assert get_type(ast.children[1]) == "func_args_sub"

		name = generate_fragment(ast.children[0], infos)
		args_csv = generate_func_args(ast.children[1], infos)

		src.append(f"{infos.indent}{name}({args_csv})")

def generate_func_call(ast, infos):
	n = len(ast.children)

	assert get_type(ast.children[0]) in [ "any_name", "member_access", "func_call", "with_access" ]
	assert get_type(ast.children[1]) == "LPAR"
	assert get_type(ast.children[2]) == "func_args"
	assert get_type(ast.children[3]) == "RPAR"

	name = generate_fragment(ast.children[0], infos)
	array_indices =  name in infos.vars or is_builtin_array(name)	# functions can't be assigned to variables, so that must be an array
	args_csv = generate_func_args(ast.children[2], infos, array_indices=array_indices)

	if array_indices:
		return f"{name}{args_csv}"

	return f"{name}({args_csv})"

def is_builtin_array(lhs):
	if lhs.endswith(")") or lhs.endswith("]"):
		return True
	return False

def generate_func_args(ast, infos, array_indices = False):
	assert get_type(ast) == "func_args" or get_type(ast) == "func_args_sub"

	if len(ast.children)==0:
		return ""

	args = []
	prev_is_sep = True
	for arg in ast.children:
		if get_type(arg) == "COMMA":
			if prev_is_sep:
				args.append("None")
			else:
				prev_is_sep = True
		else:
			args.append(generate_fragment(arg, infos))
			prev_is_sep = False
	if prev_is_sep:
		# ending with comma
		args.append("None")

	if array_indices:
		return "".join([ f"[{a}]" for a in args ])
	return ", ".join(args)

def generate_assign_stmt(ast, src, infos):
	if get_type(ast.children[1]) == "EQUAL":
		k=0
	elif get_type(ast.children[2]) == "EQUAL":
		k=1
	else:
		assert False

	var = generate_fragment(ast.children[k+0], infos)
	val = generate_fragment(ast.children[k+2], infos)

	if var == infos.def_func_name:
		# !! if we assign to the name of the function then we can't have recursive code
		# !! if we rename, we have to rename every assignment and evaluation
		pass
	else:
		infos.vars.append(var)

	src.append(f"{infos.indent}{var} = {val}")

def generate_if_stmt(ast, src, infos, inline=False):
	n = len(ast.children)

	for k in range(0, n, 4 if inline else 5):
		t = get_type(ast.children[k+0])
		if t == "if" or t == "elseif":
			expr = generate_fragment(ast.children[k+1], infos)
			stmts = ast.children[k+(3 if inline else 4)]

			ie = "if" if k==0 else "elif"
			src.append(f"{infos.indent}{ie} {expr}:")
			try:
				infos.indent +=TAB
				generate_stmt(stmts, src, infos)
			finally:
				infos.indent = infos.indent[:-len(TAB)]
		elif t == "else" :
			stmts = ast.children[k+(1 if inline else 2)]

			src.append(f"{infos.indent}else:")
			try:
				infos.indent +=TAB
				generate_stmt(stmts, src, infos)
			finally:
				infos.indent = infos.indent[:-len(TAB)]
		elif t == "end":
			pass
		else:
			assert False

def generate_for_in_stmt(ast, src, infos):
	n = len(ast.children)

	assert get_type(ast.children[0]) == "for"
	assert get_type(ast.children[2]) == "EQUAL"
	assert get_type(ast.children[4]) == "to"

	i  = generate_fragment(ast.children[1], infos)
	i0 = generate_fragment(ast.children[3], infos)
	i1 = generate_fragment(ast.children[5], infos)
	di = generate_ith_fragment(find_node_by_type(ast, "for_in_step"), 1, infos)
	stmts = find_node_by_type(ast, "statements")

	if di is None :
		src.append(f"{infos.indent}for {i} in range({i0}, {i1}):")
	else:
		src.append(f"{infos.indent}for {i} in range({i0}, {i1}, {di}):")
	try:
		infos.indent +=TAB
		generate_stmts(stmts, src, infos)
	finally:
		infos.indent = infos.indent[:-len(TAB)]

def generate_for_each_stmt(ast, src, infos):
	n = len(ast.children)

	assert get_type(ast.children[0]) == "for"
	assert get_type(ast.children[1]) == "each"
	assert get_type(ast.children[3]) == "in"

	k  = generate_fragment(ast.children[2], infos)
	v = generate_fragment(ast.children[4], infos)
	stmts = find_node_by_type(ast, "statements")

	src.append(f"{infos.indent}for {k} in {v}:")
	try:
		infos.indent +=TAB
		generate_stmts(stmts, src, infos)
	finally:
		infos.indent = infos.indent[:-len(TAB)]

def generate_loop_stmt(ast, src, infos, pre_condition, while_condition):
	cond = find_node_by_type(ast, "while_condition" if while_condition else "until_condition")
	stmts = find_node_by_type(ast, "statements")

	cond = generate_fragment(cond.children[1], infos)

	if pre_condition:
		if while_condition:
			src.append(f"{infos.indent}while {cond}:")
		else:
			src.append(f"{infos.indent}while not ({cond}):")
	else:
		src.append(f"{infos.indent}while True:")

	try:
		infos.indent +=TAB
		generate_stmts(stmts, src, infos)
		if not pre_condition:
			if while_condition:
				src.append(f"{infos.indent}if not({cond}):")
				src.append(f"{infos.indent}{TAB}break")
			else:
				src.append(f"{infos.indent}if {cond}:")
				src.append(f"{infos.indent}{TAB}break")
	finally:
		infos.indent = infos.indent[:-len(TAB)]

def generate_with_stmt(ast, src, infos):
	n = len(ast.children)

	assert get_type(ast.children[0]) == "with"

	var = f"__x{len(src)+1}"
	val  = generate_fragment(ast.children[1], infos)
	stmts = find_node_by_type(ast, "statements")

	src.append(f"{infos.indent}with {val} as {var}:")
	try:
		infos.with_stack.append(var)
		infos.indent +=TAB
		generate_stmts(stmts, src, infos)
	finally:
		infos.indent = infos.indent[:-len(TAB)]

def generate_with_access(ast, infos):
	n = len(ast.children)

	assert n == 2
	assert get_type(ast.children[0]) == "WITH_PREFIX"

	object = infos.with_stack[-1]
	member = generate_fragment(ast.children[1], infos)

	return f"{object}.{member}"

def generate_on_error_stmt(ast, src, infos):
	action = get_text(ast.children[2])
	tag = get_text(ast.children[3])
	if action == "Resume":
		assert tag == "next"
		infos.on_error_resume_next = True
		infos.on_error_goto = None
	elif action == "GoTo":
		infos.on_error_resume_next = False
		if tag == "0" or tag == 0:
			infos.on_error_goto = None
		else:
			src.append(f"{infos.indent}try:")
			infos.indent +=TAB
			infos.on_error_goto = ast.children[3].value
	else:
		assert False

def generate_goto_anchor_stmt(ast, src, infos):
	tag = generate_fragment(ast.children[0], infos)
	if infos.on_error_goto is not None:
		if infos.on_error_goto == tag:
			# matching On Error Goto Xyz
			infos.indent = infos.indent[:-len(TAB)]
			infos.on_error_goto = None
			src.append(f"{infos.indent}finally:")
			src.append(f"{infos.indent}{TAB}pass")
		else:
			print(infos.on_error_goto, "<>", tag)
			assert False
	else:
		src.append(f"{infos.indent}# Goto tag :{tag}")

def generate_ith_fragment(ast, ith, infos):
	if ast is None: return None
	return generate_fragment(ast.children[ith], infos)

# generate expression fragments
def generate_fragment(ast, infos):
	if ast is None: return None

	t = get_type(ast)
	n = len(ast.children) if is_node(ast) else None
	if t == "NUMBER":
		return ast.value
	elif t == "VB_STRING":
		txt = ast.value[1:-1]
		txt= txt.replace('""', '"')
		txt= txt.replace('\\', '\\\\').replace('"', '\\"')
		return f'"{txt}"'
	elif t == "NAME":
		return ast.value
	elif t == "TYPED_NAME":
		var = ast.value
		if var[-1] in TYPE_SUFFIX_MAP:
			var = var[:-1] 	# ignoring the type suffix
		return var
	elif t == "any_name":
		assert n==1
		return generate_fragment(ast.children[0], infos)
	elif t == "with_access":
		return generate_with_access(ast, infos)
	elif t == "comment_stmt":
		assert len(ast.children)==1
		txt = get_text(ast)
		txt = txt[1:].strip()
		return f"# {txt}"
	elif t == "function_args":
		return ", ".join([ generate_fragment(arg, infos) for arg in ast.children ])
	elif t == "function_arg":
		name = generate_fragment(ast.children[0], infos)

		type_anotation = ""
		vb_type = generate_ith_fragment(find_node_by_type(ast, "type_info"), 1, infos)
		if vb_type in TYPE_MAP:
			type_anotation = " : "+TYPE_MAP[vb_type]

		default_value = generate_ith_fragment(find_node_by_type(ast, "default_value"), 1, infos)
		if default_value is None:
			default_value = ""
		else:
			default_value = f" = {default_value}"

		return f"{name}{type_anotation}{default_value}"
	elif t == "func_call":
		return generate_func_call(ast, infos)
	elif t == "func_arg":
		if n==1:
			return generate_fragment(ast.children[0], infos)
		elif n==2:
			return generate_fragment(ast.children[0], infos) + " = " + generate_fragment(ast.children[1], infos)
		else:
			assert False
	elif t == "member_access":
		assert len(ast.children)==2
		return generate_fragment(ast.children[0], infos) + "." + generate_fragment(ast.children[1], infos)
	elif t == "expr_binary":
		assert n==3
		op = ast.children[1].value
		lhs = generate_fragment(ast.children[0], infos)
		rhs = generate_fragment(ast.children[2], infos)

		if op in OP_MAP:
			op = OP_MAP[op]
		if op in [ "Like", "Eqv", "Imp" ]:
			unsupported_feature(f"Unsupported {op} operator.", None, infos)
		else:
			return  f"{lhs} {op} {rhs}"
	elif t == "expr_unary":
		assert n==2
		op = get_text(ast.children[0])
		arg = generate_fragment(ast.children[1], infos)

		if op == "Not":
			op = "not"
		return  f"{op} {arg}"
	elif t == "type_expr":
		if n==1:
			return ast.children[0].value
		elif n==3:
			return ast.children[0].value + "." + ast.children[2].value
		else:
			assert False
	else:
		raise Exception(f"Unknown expression type {t}")

def get_type(ast):
	if is_node(ast):
		return ast.data
	else:
		return ast.type

def get_text(ast):
	if is_node(ast):
		n = len(ast.children)
		if n == 0: return ast.data
		if n == 1: return ast.children[0].value
		assert False
	else:
		return ast.value

def get_int(ast):
	return int(get_text(ast))

def is_node(ast):
	return isinstance(ast, lark.tree.Tree)

def is_token(ast):
	return isinstance(ast, lark.lexer.Token)

def find_node_by_type(ast, data_type):
	found = None
	for child in ast.children:
		if get_type(child)== data_type:
			assert found is None
			found = child
	return found

def find_nodes_by_type(ast, data_type):
	# recursive !!
	tmp = ast.find_data(data_type)
	tmp = list(tmp)
	return tmp

if __name__ == '__main__':
	import vba_parser as p
	test_file = r"vba_demo_input.bas"
	#test_file = r"..\..\xjs\vba\modJsonStringify.bas"
	src = p.transpile_one(test_file, verbose=False, generate=True)
	print()
	print(src)


from django import template
from Freshy.models import OrderItem, ItemRating
register = template.Library()

@register.simple_tag
def get_order_items(obj):
    my_orders = OrderItem.objects.filter(order=obj)
    my_str = ''
    for item in my_orders:
        my_str += 'Food Name: ' + item.item.name + '
'
        my_str += 'Direction On Map:
'
        my_str += 'Kitchen Phone# ' + item.item.kitchen.phone + '
'
    return my_str

@register.simple_tag
def get_order_item_selling(obj):
    my_orders = OrderItem.objects.filter(order=obj)
    my_str = ''
    for item in my_orders:
        my_str += 'Food Name: ' + item.item.name + '
'
    return my_str

@register.simple_tag
def date_format(obj):
    return obj.strftime('%Y-%m-%d')

@register.simple_tag
def get_ratings(obj):
    stars = 0
    count = 0
    entry_list = ItemRating.objects.filter(item__id=obj)
    for item in entry_list:
        stars += item.rating.stars
        count = count + 1
    if count == 0:
        return 0
    return stars/count


import url
import tkinter as tk
from PIL import Image, ImageTk
import general

'''
This is using a known user-agent to extract the data.
https://stackoverflow.com/questions/16627227/http-error-403-in-python-3-web-scraping
'''
try:
    from urllib.request import Request, urlopen
    req = Request('https://www.surfline.com/surf-report/duck-pier/5842041f4e65fad6a7708a45', headers={'User-Agent': 'Mozilla/5.0'})
    webpage = urlopen(req).read()
    page = webpage.decode( 'UTF-8' )

except:
    print( 'Problem parsing web data.' )
    quit()

# Handles whether opening delimiter uses AM or PM
try:

    OPENING_DELIM = 'Northern Outer Banks PM Report:
'
    CLOSING_DELIM = '
'  # check this. changes from time to time

    start_index = page.index(OPENING_DELIM) + len(OPENING_DELIM)

except:

    OPENING_DELIM = 'Northern Outer Banks AM Report:
'
    CLOSING_DELIM = '
' # check this. changes from time to time

    start_index = page.index( OPENING_DELIM ) + len( OPENING_DELIM )

end_index = page.index( CLOSING_DELIM, start_index )

conditions = page[ start_index : end_index ]

# gets rid of ' ' left over in HTML code
conditions = general.remove( conditions, ' ' )

# formats text for the GUI
conditions = general.guiformat( conditions )

window = tk.Tk()
window.title( 'SURF CONDITIONS' )
window.geometry( '990x550')

im = ImageTk.PhotoImage(url.get_web_image( 'https://s3-us-west-1.amazonaws.com/sl-coldfusion-static-prod/surfnews/images/2010/09_september/earl_south/full/JesseHines_AvalonPier_OBX_Earl-Lusk.jpg'))
panel = tk.Label( window, image = im, height=0, width=0 )
panel.grid()

update_label = tk.Label( text= 'TODAY\'S CONDITIONS: \n\n' + conditions, font=('Times New Roman',15), bg= '#D9C3D3' )
update_label.grid( column=0, row=0 )

window.mainloop()


import numpy as np
import matplotlib.pyplot as plt

data = np.load('Alphabets.npy')
labels = np.load('Alphabet_labels.npy')
alphabets =  np.array(['C','I','O','P','S','U','X','Z'])
for i in range(0,500):
    plt.imshow(data[i].reshape(28,28))
    print(alphabets[labels[i]])
    plt.show()
print(data.shape,labels.shape)


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

# In[24]:

import openpyxl
import pandas as pd
import os
import datetime
#from IPython.display import display

# In[25]:

def print_full(x):
    df = pd.DataFrame(x)
    pd.set_option('display.max_rows', 2000)
    pd.set_option('display.max_columns', 20)
    display(df)

# In[26]:

excelname= "kd"
sheetname = "kd"

wb = openpyxl.load_workbook(str(excelname)+".xlsx")
ws = wb[str(sheetname)]

dataset = []

i = 1
while(ws.cell(row=i, column=1).value!=None):
    row = []
    j = 1
    while(ws.cell(row=i, column=j).value!=None):
        row.append(ws.cell(row=i, column=j).value)
        j = j +1
    dataset.append(row)
    i = i + 1

# In[27]:

pd.DataFrame(dataset)

# In[28]:

trade=[]
for i in range(0,len(dataset)):
    row=[]
    for j in range(0,11):
        row.append(0)
    trade.append(row)
trade[0][0] = "date"
trade[0][1] = "open"
trade[0][2] = "high"
trade[0][3] = "low"
trade[0][4] = "close"
trade[0][5] = "rsv"
trade[0][6] = "k"
trade[0][7] = "d"
trade[0][8] = "buy_day"
trade[0][9] = "cum_day"
trade[0][10] = "sell_day"

# In[29]:

#抓KD數據
for i in range(1, len(trade)):
    trade[i][0] = dataset[i][0]
    trade[i][1] = float(dataset[i][1])
    trade[i][2] = float(dataset[i][2])
    trade[i][3] = float(dataset[i][3])
    trade[i][4] = float(dataset[i][4])

#rsv
#RSV(t) = (close(t) - min(low(z)| z = t, t-1, ..., t-8)/(max(high(z)) - min(low(z)| z = t, t-1, ..., t-8)*100 , t = 9, 11, ..., T
for i in range(9, len(trade)):
    rsv = 0
    high = []
    low = []
    for j in range(0, 9):
        high.append(trade[i-j][2])
        low.append(trade[i-j][3])
    rsv = (trade[i][4]-min(low))/(max(high)-min(low))*100
    trade[i][5] = rsv
    print(rsv)

# In[30]:

#k (K值 初始值 = 50, at day 9，所以 K(9) = 50)
trade[9][6] = 50

for i in range(10,len(trade)):
    K = 0
    K = (trade[i-1][6]*2/3) + (trade[i][5]*1/3)
    trade[i][6] = K
    print(K)

# In[31]:

#d (D值 初始值 = 50, at day 9，所以 D(9) = 50)
trade[9][7] = 50

for i in range(10,len(trade)):
    D = 0
    D =(trade[i-1][7]*2/3) + (trade[i][6]*1/3) #trade[i][6]不能直接寫成K，因為寫K會只抓到最後一筆K值
    trade[i][7] = D
    print(D)

# In[32]:

#KD黃金交叉
#當KD指標的K值由下往上突破D值，建議買進、做多
#K(t) > D(t)
for i in range(10, len(trade)):
    if(trade[i][6] > trade[i][7])and(trade[i-1][9]==0):
        trade[i][8] = 1
        trade[i][9] = 1
        trade[i][10] = 0
    elif(trade[i][6] < trade[i][7])and(trade[i-1][9]==1):
        trade[i][8] = 0
        trade[i][9] = 0
        trade[i][10] = 1
    elif(trade[i-1][2]>0):
        trade[i][8] = 0
        trade[i][9] = 1
        trade[i][10] = 0

# In[33]:

for i in range(1, len(trade)):
    trade[i][0] = dataset[i][0]     #把日期、股價給trade
    trade[i][7] = float(dataset[i][7])

# In[34]:

for i in range(0, len(trade)):
    for j in range(0, len(trade[1])):
        ws.cell(row=i+1, column=j+1).value= trade[i][j]
wb.save('kd3.xlsx')

# In[ ]:

# In[ ]:


#!/usr/bin/env python
# -*- coding:utf-8 -*-
import matplotlib.pyplot as plt
import logging as log
import numpy as np
import sys
import os

from keras.preprocessing.image import ImageDataGenerator
from keras.utils import to_categorical
from keras.datasets import mnist
from keras import optimizers
from keras import layers
from keras import models
from util import *

log.basicConfig(level=log.INFO)

## 设置KERAS根目录，所有的数据集都从这个目录加载
log.info("set ENV `KERAS_HOME` ...")
os.environ["KERAS_HOME"] = "./keras"

## 设置GPU参数
import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.6
set_session(tf.Session(config=config))

## 从`https://www.kaggle.com/c/dogs-vs-cats/data`数据集提取一个小型的数据集
# get_small_dataset("/Users/uc/Downloads/dogs-vs-cats/", "./dataset/dogs-vs-cats")

## 加载图片并标准化
train_datagen = ImageDataGenerator(
	rescale = 1./255,
	rotation_range = 40,
	width_shift_range = 0.2,
	height_shift_range = 0.2,
	shear_range = 0.2,
	zoom_range = 0.2,
	horizontal_flip = True,
	fill_mode = 'nearest')

test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
		"./dataset/dogs-vs-cats/train",
		# All images will be resized to 150x150
		target_size=(150, 150),
		batch_size=32,
		# Since we use binary_crossentropy loss, we need binary labels
		class_mode='binary')

validation_generator = test_datagen.flow_from_directory(
	   	"./dataset/dogs-vs-cats/validation",
		target_size=(150, 150),
		batch_size=32,
		class_mode='binary')

## 构建模型
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Flatten())
model.add(layers.Dropout(0.5))
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))

model.compile(loss='binary_crossentropy', optimizer=optimizers.RMSprop(lr=1e-4), metrics=['acc'])

## 训练模型
history = model.fit_generator(
	train_generator,
	steps_per_epoch = 100,
	epochs = 100,
	validation_data = validation_generator,
	validation_steps = 50)

model.save('./model/cats_and_dogs_small_2.h5')

acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']

epochs = range(len(acc))

plt.subplot(211)
plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()

plt.subplot(212)
plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()

plt.show()


from tkinter import *
import numpy as np
import matplotlib
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from scipy import signal as sp
import matplotlib.pyplot as plt

gui_4 = Tk()
gui_4.title('Tarea 1')
gui_4.configure(background='SlateBlue')
gui_4.geometry("1000x800")

# Etiqueta
lab_1 = Label(gui_4, text="Numero de Armonicos")
lab_1.configure(background='White')
lab_1.grid(row=0, column=1, padx=(10, 10), pady=(10, 10))
n = IntVar()
n.set(10)

# Etiqueta
lab_2 = Label(gui_4, text="ti")
lab_2.configure(background='white')
lab_2.grid(row=2, column=0, padx=(10, 10), pady=(10, 10))
ti = DoubleVar()
ti.set(1)

# Etiqueta
lab_1 = Label(gui_4, text="tf")
lab_1.configure(background='white')
lab_1.grid(row=2, column=1, padx=(10, 10), pady=(10, 10))
tf = DoubleVar()
tf.set(10)

# Etiqueta
lab_2 = Label(gui_4, text="dt")
lab_2.configure(background='white')
lab_2.grid(row=2, column=2, padx=(10, 10), pady=(10, 10))
dt = DoubleVar()
dt.set(0.001)

# Caja de texto
ent1 = Entry(gui_4, textvariable=n)
ent1.configure(background='white')
ent1.grid(row=1, column=0, padx=(10, 10), pady=(10, 10))
ent1.config(width=5)

# Caja de texto
ent2 = Entry(gui_4, textvariable=ti)
ent2.configure(background='white')
ent2.grid(row=3, column=0, padx=(10, 10), pady=(10, 10))
ent2.config(width=5)

# Caja de texto
ent3 = Entry(gui_4, textvariable=tf)
ent3.configure(background='white')
ent3.grid(row=3, column=1, padx=(10, 10), pady=(10, 10))
ent3.config(width=5)

# Caja de texto
ent4 = Entry(gui_4, textvariable=dt)
ent4.configure(background='white')
ent4.grid(row=3, column=2, padx=(10, 10), pady=(10, 10))
ent4.config(width=5)

fun1 = StringVar(gui_4)
fun1.set('Señal')

menu = OptionMenu(gui_4, fun1, 'Trigonometrica',
                  'Exponencial', 'Logaritmica', 'Algebraica')
menu.grid(row=1, column=3, padx=(10, 10), pady=(10, 10))

def Mostrar_Grafica():
    harm = n.get()
    ini = ti.get()
    fin = tf.get()
    delt = dt.get()
    op = fun1.get()
    T = fin-ini
    w = (2*np.pi)/T
    t = np.arange(ini, fin, delt)
    ak = np.zeros((harm, 1))
    bk = np.zeros((harm, 1))
    m = len(t)

    if op == 'Trigonometrica':
        x = np.cos(t)
    if op == 'Exponencial':
        x = np.exp(t)
    if op == 'Logaritmica':
        x = np.log(t)
    if op == 'Algebraica':
        x = t**2

# Calculo del coeficiente A0
    A0 = 0
    for i in range(1, m):
        i = int(i)
        A0 = A0 + (1/T)*x[i]*delt

    n_w = np.arange(1, harm+1, 1)
    ck = np.zeros((harm, 1))
    phik = np.zeros((harm, 1))

# Calculo de los armonicos Ak y Bk
    for i in range(1, harm):
        i = int(i)
        for j in range(1, m):
            j = int(j)
            ak[i] = ak[i] + ((2/T)*x[j]*np.cos(i*t[j]*w)*delt)
            bk[i] = bk[i] + ((2/T)*x[j]*np.sin(i*t[j]*w)*delt)
            ck[i] = (ak[i]**2 + bk[i]**2)**0.5
            phik[i] = -1*(np.arctan(bk[i]/ak[i]))

    t1 = np.arange(ini, 2*fin, delt)
    xf = A0
    for i in range(1, harm):
        i = int(i)
        xf = xf + ak[i]*np.cos(i*w*t1) + bk[i]*np.sin(i*w*t1)

    plt.figure(1)
    plt.clf()
    plt.title('Señal')
    plt.plot(t, x)
    plt.gcf().canvas.draw()

    plt.figure(2)
    plt.clf()
    plt.title('Espectro Amplitud')
    plt.stem(n_w, ck)
    plt.gcf().canvas.draw()

    plt.figure(3)
    plt.clf()
    plt.title('Señal en Series')
    plt.plot(t1, xf)
    plt.gcf().canvas.draw()

    plt.figure(4)
    plt.clf()
    plt.title('Espectro Fase')
    plt.stem(n_w, phik)
    plt.gcf().canvas.draw()

fig = plt.figure(figsize=(3, 2))
canv = FigureCanvasTkAgg(fig, master=gui_4)
canv.get_tk_widget().grid(row=9, column=0, padx=(10, 10), pady=(10, 10))

fig2 = plt.figure(figsize=(3, 2))
canv2 = FigureCanvasTkAgg(fig2, master=gui_4)
canv2.get_tk_widget().grid(row=9, column=1, padx=(10, 10), pady=(10, 10))

fig3 = plt.figure(figsize=(3, 2))
canv3 = FigureCanvasTkAgg(fig3, master=gui_4)
canv3.get_tk_widget().grid(row=11, column=0, padx=(10, 10), pady=(10, 10))

fig4 = plt.figure(figsize=(3, 2))
canv4 = FigureCanvasTkAgg(fig4, master=gui_4)
canv4.get_tk_widget().grid(row=11, column=1, padx=(10, 10), pady=(10, 10))

plt.figure(1)
plt.clf()
plt.title('ORIGINAL')

plt.figure(2)
plt.clf()
plt.title('ESPECTRO EN AMPLITUD')

plt.figure(3)
plt.clf()
plt.title('RECONSTRUIDA')

plt.figure(4)
plt.clf()
plt.title('ESPECTRO EN FASE')

# Button
btn = Button(gui_4, text="GRAFICAR", command=Mostrar_Grafica)
btn.grid(row=3, column=3, padx=(10, 10), pady=(10, 10))

gui_4.mainloop()


"""
Content Provider:       Thingiverse
ETL Process:            Use the API to identify all CC0 3D Models.
Output:                 TSV file containing the 3D models
                        their respective images and meta-data.
Notes:                  https://www.thingiverse.com/developers/getting-started
                        All API requests require authentication.
                        Rate limiting is 300 per 5 minute window.
"""
import argparse
import logging
import os
import json
from operator import itemgetter
from datetime import datetime, timedelta, timezone

import common.requester as requester
import common.storage.image as image_class

logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s:  %(message)s',
    level=logging.INFO
)

logger = logging.getLogger(__name__)

PER_PAGE_LIMIT = 30
IMAGES_LIMIT = 1000
DELAY = 1.0  # Time between each two consecutive requets
HOST = 'thingiverse.com'
ENDPOINT = f'https://api.{HOST}/newest'
PROVIDER = 'thingiverse'
LICENSE = 'cc0'
LICENSE_VERSION = '1.0'
LICENSE_TEXT = 'Creative Commons'
TOKEN = os.getenv('THINGIVERSE_API_KEY')
DEFAULT_QUERY_PARAMS = {
    'access_token': TOKEN,
}

delayed_requester = requester.DelayedRequester(DELAY)
image_store = image_class.ImageStore(provider=PROVIDER)

def main(date):
    """
    This script pulls the data for a given date from Thingiverse API
    and writes it into a .TSV file to be eventually read
    into our DB.
    Required Arguments:
    date:  Date String in the form YYYY-MM-DD.  This is the date for
           which running the script will pull data.
    """
    logger.info(f'Processing Thingiverse API for date: {date}')

    start_timestamp = _derive_timestamp_pair(date)

    cur_page = 1
    total_images = 0
    is_valid = True

    while is_valid and total_images < IMAGES_LIMIT:
        thing_batch = _get_things_batch(
            cur_page
        )
        total_images, is_valid = _process_thing_batch(
            thing_batch, total_images, start_timestamp)
        cur_page = cur_page + 1

    total_images = image_store.commit()
    logger.info(f'Total images: {total_images}')
    logging.info('Terminated!')

def _process_thing_batch(thing_batch, total_images, start_timestamp):
    is_valid = True
    if thing_batch is not None:
        thing_batch = list(thing_batch)
        batch_total_images = list(filter(None, list(map(lambda thing: _process_thing(
            str(thing), start_timestamp), thing_batch))))

        if '-1' in batch_total_images:
            is_valid = False
            batch_total_images = []

        total_images += sum(batch_total_images)

    return total_images, is_valid

def _derive_timestamp_pair(date):
    date_obj = datetime.strptime(date, '%Y-%m-%d')
    utc_date = date_obj.replace(tzinfo=timezone.utc)
    start_timestamp = str(int(utc_date.timestamp()))
    #end_timestamp = str(int((utc_date + timedelta(days=1)).timestamp()))
    return start_timestamp

def _get_things_batch(page=1, retries=5):
    query_params = _build_query_params(
        page
    )
    thing_batch = None
    response_json = _get_response_json_list(
        query_params=query_params, retries=retries)
    if response_json is not None:
        thing_batch = map(lambda thing: thing['id'], response_json)

    if 'batchcomplete' in response_json:
        logger.debug('Found batchcomplete')

    return thing_batch

def _build_query_params(
        page=1,
        default_query_params=DEFAULT_QUERY_PARAMS
):
    query_params = default_query_params.copy()
    query_params.update({'per_page': PER_PAGE_LIMIT})
    query_params.update({'page': page})

    return query_params

def _get_response_json_list(
    query_params,
    endpoint=ENDPOINT,
    retries=0
):
    response_json = None

    if retries < 0:
        logger.error('No retries remaining.  Failure.')
        raise Exception('Retries exceeded')

    response = delayed_requester.get(
        endpoint,
        params=query_params,
        timeout=60
    )
    if response is not None and response.status_code == 200:
        try:
            response_json = response.json()
        except Exception as e:
            logger.warning(f'Could not get response_json.\n{e}')
            response_json = None
    if response_json is None or (type(response_json) != list and response_json.get('error') is not None):
        logger.warning(f'Bad response_json:  {response_json}')
        logger.warning(
            'Retrying:\n_get_response_json(\n'
            f'    {endpoint},\n'
            f'    {query_params},\n'
            f'    retries={retries - 1}'
            ')'
        )
        response_json = _get_response_json_list(
            query_params,
            endpoint=endpoint,
            retries=retries - 1
        )

    return response_json

def _build_thing_query(thing, default_query_params=DEFAULT_QUERY_PARAMS):
    endpoint = f'https://api.thingiverse.com/things/{thing}'
    query_params = default_query_params.copy()
    return endpoint, query_params

def _validate_license(response_json):
    license_ = None
    license_version = None
    if not (('license' in response_json) and (
            LICENSE_TEXT.lower() in response_json['license'].lower())):
        logging.warning('license not detected')
    else:
        license_ = LICENSE
        license_version = LICENSE_VERSION

    return license_, license_version

def _create_meta_dict(
    thing_data
):
    meta_data = {
        'description': thing_data.get('description', ''),
        'title': thing_data.get('name', '')
    }

    return meta_data

def _build_foreign_landing_url(thing_data, thing):
    foreign_landing_url = None
    if 'public_url' in thing_data:
        foreign_landing_url = thing_data['public_url'].strip()
    else:
        foreign_landing_url = f'https://www.thingiverse.com/thing:{thing}'

    return foreign_landing_url

def _build_creator_data(thing_data):
    creator = None
    creator_url = None
    try:
        if ('first_name' in thing_data['creator']) and (
                'last_name' in thing_data['creator']):
            creator = f"{thing_data['creator']['first_name']} {thing_data['creator']['last_name']}"

        if (creator.strip() == '') and ('name' in thing_data['creator']):
            creator = thing_data['creator']['name']

        if 'public_url' in thing_data['creator']:
            creator_url = thing_data['creator']['public_url'].strip()

    except Exception as e:
        logger.warning(f'Could not find "creator" in "thing_data". \n {e}')

    return creator, creator_url

def _create_tags_list(thing):
    logging.info(f'Requesting tags for thing: {thing}')
    endpoint = f'https://api.thingiverse.com/things/{thing}/tags'
    tags = _get_response_json_list(DEFAULT_QUERY_PARAMS, endpoint, retries=5)
    tags_list = None
    if tags is not None:
        tags_list = list(map(lambda tag: {'name': str(
            tag['name'].strip()), 'provider': 'thingiverse'}, tags))
        tags_list.sort(key=itemgetter('name'))
    return tags_list

def _get_image_list_json(thing):
    endpoint = f'https://api.thingiverse.com/things/{thing}/files'
    image_list = _get_response_json_list(
        DEFAULT_QUERY_PARAMS, endpoint, retries=5)
    if image_list is None:
        logging.warning('Image Not Detected!')

    return image_list

def _get_image_url_thumbnail(images):
    thumbnail = None
    image_url = None
    for image_size in images:

        if str(image_size['type']).strip().lower() == 'display':

            if str(image_size['size']).lower() == 'medium':
                thumbnail = image_size['url'].strip()

            if str(image_size['size']).lower() == 'large':
                image_url = image_size['url'].strip()

            elif image_url is None:
                image_url = thumbnail

        else:
            continue

    return thumbnail, image_url

def _add_images(image_list,
                foreign_landing_url,
                license_,
                license_version,
                creator,
                creator_url,
                title,
                tags_list):
    total_images = 0
    for image in image_list:
        total_images = image_store.add_item(
            foreign_landing_url=foreign_landing_url,
            image_url=image[1],
            thumbnail_url=image[2],
            license_=license_,
            license_version=license_version,
            foreign_identifier=image[0],
            creator=creator,
            creator_url=creator_url,
            title=title,
            meta_data=image[3],
            raw_tags=tags_list,
            source='Thigniverse'
        )
    return total_images

def _process_image_list(image_list, description):
    images_data = []
    for image in image_list:
        meta_data = {}
        thumbnail = None
        image_url = None
        foreign_landing_id = None
        meta_data['description'] = description
        if ('default_image' in image) and image['default_image']:
            if 'url' in image['default_image']:
                meta_data['3d_model'] = image['default_image']['url']
                foreign_landing_id = str(image['default_image']['id'])
                images = image['default_image']['sizes']
                thumbnail, image_url = _get_image_url_thumbnail(images)
                if image_url is None:
                    logging.warning('Image Not Detected!')
                    continue

                images_data.append(
                    [
                        image_url if not foreign_landing_id else foreign_landing_id,
                        image_url,
                        thumbnail,
                        '\\N' if not meta_data else json.dumps(meta_data)
                    ]
                )
            else:
                logging.warning('3D Model Not Detected!')
                continue
        else:
            logging.warning('Image Not Detected!')

    return images_data

def _process_thing(thing, start_timestamp):
    endpoint, query_params = _build_thing_query(thing)
    license_ = None
    license_version = None
    creator = None
    creator_url = None
    foreign_landing_url = None
    total_images = 0
    total_images_list = []
    logging.info('Processing thing: {}'.format(thing))
    response_json = _get_response_json_list(query_params, endpoint, retries=5)

    if response_json is not None:
        modified_date = response_json.get('modified', '')
        if modified_date is not None:
            modified_date = modified_date.split('T')[0].strip()
            modified_date = _derive_timestamp_pair(modified_date)
            if modified_date >= start_timestamp:
                license_, license_version = _validate_license(response_json)
                meta_data = _create_meta_dict(response_json)
                foreign_landing_url = _build_foreign_landing_url(
                    response_json, thing)
                creator, creator_url = _build_creator_data(response_json)
                tags_list = _create_tags_list(str(thing))
                image_list = _get_image_list_json(thing)
                total_images_list = _process_image_list(
                    image_list, meta_data['description'])
                total_images = _add_images(total_images_list, foreign_landing_url, license_,
                                           license_version, creator, creator_url, meta_data['title'], tags_list)
            else:
                logging.warning('Modified Date Not Valid')
                total_images = '-1'
        else:
            logging.warning('Modified Date Not Found')
            total_images = '-1'
    else:
        logging.warning('Thing Not Found, response_json is None')
        total_images = '-1'

    return total_images

if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description='Thingiverse API Job',
        add_help=True
    )
    parser.add_argument(
        '--date',
        help='Identify images uploaded on a date (format: YYYY-MM-DD).')
    args = parser.parse_args()
    if args.date:
        date = args.date
    else:
        date_obj = datetime.now() - timedelta(days=2)
        date = datetime.strftime(date_obj, '%Y-%m-%d')

    main(date)


"""
Michael Patel
April 2020

Project description:
    Build a GAN to create basketball shoe designs

File description:
    For generating a gif of training epochs
"""
################################################################################
# Imports
import os
import glob
import imageio

################################################################################
# Main
if __name__ == "__main__":
    # ----- VISUALIZATION ----- #
    results_dir = input("Enter images directory: ")
    gif_filename = os.path.join(os.getcwd(), "results\\training.gif")

    # get all images
    image_files_pattern = results_dir + "\\*.png"
    filenames = glob.glob(image_files_pattern)

    # write all images to gif
    with imageio.get_writer(gif_filename, mode="I", fps=1.0) as writer:  # 'I' for multiple images
        for f in filenames:
            image = imageio.imread(f)
            writer.append_data(image)


import json
import sys
import os
import semver
from github import Github, GithubException, InputGitAuthor

HTTP_REQUEST_RETRIES = 3
HTTP_REQUEST_DELAY_IN_SECONDS = 2
HTTP_REQUEST_DELAY_MULTIPLIER = 2

packageUser =  os.environ["packageUser"]
packagePAT = os.environ["packagePAT"]
packageEmail =  os.environ["packageEmail"]

ORGANIZATION = "ballerina-platform"
STANDARD_LIBRARY = "stdlib"

MASTER_BRANCH = "master"
MAIN_BRANCH = "main"

VERSION_UPDATE_BRANCH_NAME = "automated/stdlib_version_update"
PULL_REQUEST_TITLE = "[Automated] Update Stdlib module versions"
PULL_REQUEST_BODY = "$subject"

PROPERTIES_FILE = "gradle.properties"

javaArraysModuleName = 'stdlibJavaArraysVersion'
OAuth2ModuleName = 'stdlibOAuth2Version'

SKIPPING_MODULES = ["kafka", "nats", "stan", "rabbitmq", "java.jdbc", "mysql", "serdes"]

def main():
    print("Checking Ballerina Distribution for stdlib version updates")
    module_list = get_stdlib_modules()
    repo = fetch_ballerina_distribution_repo()
    properties_file = fetch_properties_file(repo)
    current_versions = get_current_module_versions(properties_file)
    modified_properties_file, commit_flag, updated_modules = update_properties_file(properties_file, module_list, current_versions)
    if commit_flag:
        commit_changes(modified_properties_file, repo, updated_modules)
        create_pull_request(repo)
        print("Updated gradle.properties file in Ballerina Distribution Successfully")
    else:
        print("Stdlib versions in gradle.properties file are up to date")

# Get stdlib module details from stdlib_modules.json file
def get_stdlib_modules():
    try:
        with open('./release/resources/stdlib_modules.json') as f:
            module_list = json.load(f)
    except:
        print('Failed to read stdlib_modules.json')
        sys.exit()

    return module_list['modules']

# Fetch ballerina-distribution repository with GitHub credentials
def fetch_ballerina_distribution_repo():
    github = Github(packagePAT)
    try:
        repo = github.get_repo(ORGANIZATION + "/" + 'ballerina-distribution')
    except:
        print("Error fetching repository ballerina-distribution")

    return repo

# Fetch the gradle.properties file from the ballerina-distribution repo
def fetch_properties_file(repo):
    try:
        source = repo.get_branch(MAIN_BRANCH)
    except GithubException:
        source = repo.get_branch(MASTER_BRANCH)

    try:
        branch = repo.get_branch(branch=VERSION_UPDATE_BRANCH_NAME)
        repo.merge(VERSION_UPDATE_BRANCH_NAME, source.commit.sha, "Sync default branch")
        file = repo.get_contents(PROPERTIES_FILE, ref=VERSION_UPDATE_BRANCH_NAME)
    except GithubException:
        file = repo.get_contents(PROPERTIES_FILE)

    data = file.decoded_content.decode("utf-8")

    return data

# Get current versions of stdlib modules from gradle.properties file
def get_current_module_versions(properties_file):
    current_versions = {}

    for line in properties_file.splitlines():
        if STANDARD_LIBRARY in line and 'Version=' in line:
            module_name = line.split('=')[0]
            version = line.split('=')[1]
            current_versions[module_name] = version

    return current_versions

# Compare latest version with current version
# Return 1 if latest version > current version
# Return 0 if latest version = current version
# Return -1 if latest version < current version
def compare_version(latest_version, current_version):
    if semver.compare(latest_version.split('-')[0], current_version.split('-')[0]) == 1:
        return latest_version
    else:
        return latest_version

# Update stdlib module versions in the gradle.properties file with module details fetched from stdlib_modules.json
def update_properties_file(data, modules, current_versions):
    modified_data = ''
    updated_modules = []
    current_line = ''
    commit_flag = False

    line_list = data.splitlines()

    for line in line_list:
        if STANDARD_LIBRARY in line.lower():
            current_line = line
            break
        line += '\n'
        modified_data += line
    modified_data = modified_data[0:-1]

    level = 1
    for module in modules:
        if module['level'] == level:
            line = "\n# Stdlib Level " + f"{level:02d}" + "\n"
            modified_data += line
            level += 1

        module_name = module['name'].split('-')[-1]
        latest_version = module['version']

        if module_name == 'jballerina.java.arrays':
            version = compare_version(latest_version, current_versions[javaArraysModuleName])
            line = javaArraysModuleName + "=" + version + "\n"
        elif module_name == 'oauth2':
            version = compare_version(latest_version, current_versions[OAuth2ModuleName])
            line = OAuth2ModuleName + "=" + version + "\n"
        elif module_name in SKIPPING_MODULES:
            continue
        else:
            module_name_in_naming_convention = STANDARD_LIBRARY + module_name.capitalize() + 'Version'
            if module_name_in_naming_convention in current_versions:
                version = compare_version(latest_version, current_versions[module_name_in_naming_convention])
            else:
                version = latest_version
            line = STANDARD_LIBRARY + module_name.capitalize() + "Version=" + version + "\n"

        if line[0:-1] not in line_list:
            updated_modules.append(module_name)
        modified_data += line

    for line in line_list[line_list.index(current_line):len(line_list)]:
        current_line = line
        if STANDARD_LIBRARY not in line.lower() and line != '':
            break

    modified_data += "\n"

    for line in line_list[line_list.index(current_line):len(line_list)]:
        if STANDARD_LIBRARY not in line.lower():
            line += "\n"
            modified_data += line

    # modified_data = modified_data[0:-1]
    if modified_data != data:
        commit_flag = True

    return modified_data, commit_flag, updated_modules

# Commit changes made to the gradle.properties file
def commit_changes(data, repo, updated_modules):
    author = InputGitAuthor(packageUser, packageEmail)

    # If branch already exists checkout and commit else create new branch from master branch and commit
    try:
        source = repo.get_branch(MAIN_BRANCH)
    except GithubException:
        source = repo.get_branch(MASTER_BRANCH)

    try:
        repo.get_branch(branch=VERSION_UPDATE_BRANCH_NAME)
        repo.merge(VERSION_UPDATE_BRANCH_NAME, source.commit.sha, "Sync default branch")
    except:
        repo.create_git_ref(ref=f"refs/heads/" + VERSION_UPDATE_BRANCH_NAME, sha=source.commit.sha)

    contents = repo.get_contents(PROPERTIES_FILE, ref=VERSION_UPDATE_BRANCH_NAME)

    if len(updated_modules) > 0:
        commit_message = "Bump the version of stdlib module(s) - "
        for updated_module in updated_modules:
            commit_message += updated_module
            commit_message += " "
    else:
        commit_message = "Update gradle.properties"

    repo.update_file(
        contents.path,
        commit_message,
        data,
        contents.sha,
        branch=VERSION_UPDATE_BRANCH_NAME,
        author=author
    )

# Create a PR from the branch created
def create_pull_request(repo):
    pulls = repo.get_pulls(state='open', head=VERSION_UPDATE_BRANCH_NAME)

    pr_exists = 0

    # Check if a PR already exists for the module
    for pull in pulls:
        if PULL_REQUEST_TITLE in pull.title:
            pr_exists = pull.number

    # If PR doesn't exists create a new PR
    if pr_exists == 0:
        try:
            repo.create_pull(
                title=PULL_REQUEST_TITLE,
                body=PULL_REQUEST_BODY,
                head=VERSION_UPDATE_BRANCH_NAME,
                base=MAIN_BRANCH
            )
        except GithubException:
            repo.create_pull(
                title=PULL_REQUEST_TITLE,
                body=PULL_REQUEST_BODY,
                head=VERSION_UPDATE_BRANCH_NAME,
                base=MASTER_BRANCH
            )

main()


from django.conf.urls import url
from django.views.decorators.csrf import csrf_exempt

from . import consumers
from . import views

app_name = "dockerapps"

urlpatterns = [
    url(
        regex=r"^(?P<project>[0-9a-f-]+)/dockerapps/image/$",
        view=views.DockerImageListView.as_view(),
        name="image-list",
    ),
    url(
        regex=r"^(?P<project>[0-9a-f-]+)/dockerapps/image/create$",
        view=views.DockerImageCreateView.as_view(),
        name="image-create",
    ),
    url(
        regex=r"^(?P<project>[0-9a-f-]+)/dockerapps/image/(?P<image>[0-9a-f-]+)/$",
        view=views.DockerImageDetailView.as_view(),
        name="image-detail",
    ),
    url(
        regex=r"^(?P<project>[0-9a-f-]+)/dockerapps/image/(?P<image>[0-9a-f-]+)/update/$",
        view=views.DockerImageUpdateView.as_view(),
        name="image-update",
    ),
    url(
        regex=r"^(?P<project>[0-9a-f-]+)/dockerapps/image/(?P<image>[0-9a-f-]+)/delete/$",
        view=views.DockerImageDeleteView.as_view(),
        name="image-delete",
    ),
    url(
        regex=r"^(?P<project>[0-9a-f-]+)/dockerapps/image/(?P<image>[0-9a-f-]+)/control/$",
        view=views.DockerImageJobControlView.as_view(),
        name="image-control",
    ),
    url(
        regex=r"^(?P<project>[0-9a-f-]+)/dockerapps/image/(?P<image>[0-9a-f-]+)/pull/$",
        view=views.DockerImagePullView.as_view(),
        name="image-pull",
    ),
    # Background job views
    url(
        regex=r"^(?P<project>[0-9a-f-]+)/dockerapps/image-job/(?P<job>[0-9a-f-]+)/$",
        view=views.ImageBackgroundJobDetailView.as_view(),
        name="image-job-detail",
    ),
    url(
        regex=r"^(?P<project>[0-9a-f-]+)/dockerapps/container-job/(?P<job>[0-9a-f-]+)/$",
        view=views.ContainerStateControlBackgroundJobDetailView.as_view(),
        name="process-job-detail",
    ),
    # Proxy to embedded Docker / Shiny app.  NB: there is a "partner" websocket_urlpattern through Django Channels.
    url(
        regex=(
            r"^(?P<project>[0-9a-f-]+)/dockerapps/(?P<image>[0-9a-f-]+)/proxy/"
            "(?P<process>[0-9a-f-]+)/(?P<path>.*)$"
        ),
        view=csrf_exempt(views.DockerProxyView.as_view()),
        name="docker-proxy",
    ),
]

websocket_urlpatterns = [
    url(
        (
            r"^dockerapps/(?P<project>[0-9a-f-]+)/dockerapps/(?P<image>[0-9a-f-]+)/proxy/"
            "(?P<process>[0-9a-f-]+)/(?P<path>.*)$"
        ),
        consumers.TunnelConsumer,
    )
]


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

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('eldsterapp', '0005_auto_20160218_1423'),
    ]

    operations = [
        migrations.CreateModel(
            name='EldsterappTesttable',
            fields=[
                ('id', models.IntegerField(serialize=False, primary_key=True)),
                ('name', models.CharField(max_length=100)),
            ],
            options={
                'db_table': 'eldsterapp_testtable',
                'managed': False,
            },
        ),
        migrations.DeleteModel(
            name='testTable',
        ),
    ]


#!/usr/bin/env python

from __future__ import print_function

import sys
import time

from test_gripper import GripperTester
from test_tf_frames import TfTester
from ariac_example import ariac_example
import rospy
import rostest

import tf
import tf2_geometry_msgs  # noqa
import tf2_py as tf2
import tf2_ros

class GripperBoxDropTester(GripperTester, TfTester):

    def test(self):
        self.comp_class = ariac_example.MyCompetitionClass()
        ariac_example.connect_callbacks(self.comp_class)
        time.sleep(1.0)
        self.prepare_tf()

        # Pre-defined initial pose because sometimes the arm starts "droopy"
        self._send_arm_to_initial_pose()

        # Pre-defined pose that puts the gripper in contact with a product.
        self._send_arm_to_product()

        # Enable the gripper so that it picks up the product.
        self._test_enable_gripper()

        # Move the product over the shipping box using a pre-defined sequence of poses.
        self._send_arm_to_shipping_box()
        self.assertTrue(
            self.comp_class.current_gripper_state.enabled, 'Gripper no longer enabled')
        self.assertFalse(
            self.comp_class.current_gripper_state.attached, 'Product is still attached')

        self._test_dropped_product_pose()

        time.sleep(1.0)

    def _test_dropped_product_pose(self):
        self._test_pose(
            [0.15, 0.15, 0.0],
            tf.transformations.quaternion_from_euler(0, 0, 0.2),
            self.camera_above_box0 + '_gear_part_3_frame',
            self.camera_above_box0 + '_shipping_box_0_frame'
        )

if __name__ == '__main__':
    rospy.init_node('test_gripper_box_drop', anonymous=True)

    # Wait until /clock is being published; this can take an unpredictable
    # amount of time when we're downloading models.
    while rospy.Time.now().to_sec() == 0.0:
        print('Waiting for Gazebo to start...')
        time.sleep(1.0)
    # Take an extra nap, to allow plugins to be loaded
    time.sleep(12.0)
    print('OK, starting test.')

    rostest.run('test_ariac', 'test_gripper_box_drop', GripperBoxDropTester, sys.argv)


"""This module defines Pool class"""
import math
import random
import config

POOL_WIDTH = int(config.CONFIG['Pool']['Width'])
POOL_HEIGHT = int(config.CONFIG['Pool']['Height'])

class Pool:
    """Pool class for our fish"""
    def __init__(self):
        self._width = POOL_WIDTH
        self._height = POOL_HEIGHT
        self.fishes = []

    def get_size(self):
        """Return pool size"""
        return self._width, self._height

    def fill(self, fish_type, number: int):
        """Fills the pool"""
        self.fishes += [fish_type(
            random.randint(0, self._width - 1),
            random.randint(0, self._height - 1)
        ) for _ in range(number)]

    def __str__(self):
        val = '+' + '-' * self._width + '+\n'
        p_val = [[' ']*self._width for _ in range(self._height)]
        for fish in self.fishes:
            p_val[fish.get_pos()[0]][fish.get_pos()[1]] = repr(fish)
        for row in p_val:
            val += '|' + ''.join(row) + '|\n'
        val += '+' + '-'*self._width + '+\n'
        return val

    def tick(self):
        """The fish cycle in the pool"""
        for fish in self.fishes:
            fish.move(self)
            fish.eat(self)

        for fish in self.fishes.copy():
            if not fish.is_alive():
                self.kill(fish)
                break
        else:
            for fish in self.fishes.copy():
                fish.born(self)

    def get_nearest_victim(self, axis_x, axis_y):
        """Find nearest dish..em, sorry, i wanted to say 'fish'"""
        nearest_victims = [fish.get_pos() for fish in self.fishes
                           if fish.is_victim]
        if not nearest_victims:
            return (0, 0)
        return tuple(min(
            nearest_victims,
            key=lambda f: math.hypot(f[0] - axis_x, f[1] - axis_y)))

    def get_victim(self, pos):
        """Get victim)"""
        return [fish for fish in self.fishes
                if fish.get_pos() == pos and fish.is_victim()]

    def kill(self, victim):
        """Death is just the beginning"""
        self.fishes.remove(victim)


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

from djangoplus.admin.models import User
from djangoplus.test import TestCase
from django.conf import settings

class AdminTestCase(TestCase):

    def test_app(self):

        User.objects.create_superuser('admin', None, settings.DEFAULT_PASSWORD)

        self.open('/')
        self.wait(2)

        self.login('admin', settings.DEFAULT_PASSWORD)
        self.click_icon('Configurações')
        self.click_link('Editar Perfil')
        self.enter('Nome', 'Administrador')
        self.enter('Senha', '123')
        self.enter('Confirmação', '123')
        self.click_button('Atualizar Perfil')
        self.logout()
        self.login('admin', '123')
        self.click_icon('Configurações')
        self.click_link('Configurações')
        self.enter('Nome', 'My Project')
        self.click_button('Atualizar')

        self.click_icon('Usuários')
        self.click_link('Cadastrar')
        self.enter('Nome', 'Carlos Breno')
        self.enter('E-mail', 'brenokcc@yahoo.com.br')
        self.enter('Login', 'brenokcc')
        self.enter('Senha', 'senha')
        self.click_button('Cadastrar')
        self.logout()
        self.login('brenokcc', 'senha')
        self.logout()

        self.login('admin', '123')
        self.click_icon('Usuários')
        self.look_at('Carlos Breno')

        self.click_button('Alterar Senha')
        self.enter('Senha', '321')
        self.enter('Confirmação', '321')
        self.click_button('Alterar')
        self.logout()

        self.login('brenokcc', '321')
        self.logout()


import codecs
import os
import sys

from setuptools import setup, find_packages

HERE = os.path.abspath(os.path.dirname(__file__))

def read(*parts):  # Stolen from txacme
    with codecs.open(os.path.join(HERE, *parts), 'rb', 'utf-8') as f:
        return f.read()

def readme():
    # Prefer the ReStructuredText README, but fallback to Markdown if it hasn't
    # been generated
    if os.path.exists('README.rst'):
        return read('README.rst')
    else:
        return read('README.md')

install_requires = [
    'acme',
    'cryptography',
    'klein == 15.3.1',
    'requests',
    'treq',
    # Twisted 17.1.0 causes problems with treq.testing
    # https://github.com/twisted/treq/issues/164
    'Twisted != 17.1.0',
    'txacme >= 0.9.1',
    'uritools >= 1.0.0'
]
if sys.version_info < (3, 3):
    install_requires.append('ipaddress')

setup(
    name='marathon-acme',
    version='0.1.1.dev0',
    license='MIT',
    url='https://github.com/praekeltfoundation/marathon-acme',
    description=("Automated management of Let's Encrypt certificates for apps "
                 "running on Mesosphere Marathon"),
    author='Jamie Hewland',
    author_email='jamie@praekelt.com',
    long_description=readme(),
    packages=find_packages(),
    install_requires=install_requires,
    classifiers=[
        'Development Status :: 3 - Alpha',
        'Framework :: Twisted',
        'Intended Audience :: System Administrators',
        'License :: OSI Approved :: MIT License',
        'Natural Language :: English',
        'Programming Language :: Python',
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: Implementation :: CPython',
        'Programming Language :: Python :: Implementation :: PyPy'
    ],
    entry_points={
        'console_scripts': ['marathon-acme = marathon_acme.cli:_main'],
    }
)


import os, time, sys, datetime

class IO_Object(object):
      path=None
      src_path=None
      args=None
      def __init__(self,args):
          # input: arguments from program input
          if (len(args.comment.strip()))>0:
             comment='-'.join(args.comment.split(' '))
             self.path=os.path.join(str(datetime.datetime.now()).replace(':','-').replace(' ','-')[:-7]+'-'+comment)
          else:
             self.path=str(datetime.datetime.now()).replace(':','-').replace(' ','-')[:-7]
          self.path+='-bin%d-batch%d'%(args.n_bins,args.batch_size)
          self.path=os.path.join('Retrieval_result',self.path)
          self.src_path=os.path.join(self.path,'source_codes')

          self.args=args

      def makeFolder(self):
          assert self.path is not None
          assert self.src_path is not None
          if os.path.exists('Retrieval_result')==False:
             os.mkdir('Retrieval_result')
          os.mkdir(self.path)
          os.mkdir(self.src_path)

      def backup(self):
          assert self.src_path is not None
          import shutil,glob

          for file_name in glob.glob(os.path.join(sys.path[0],'*.py')):
              shutil.copy(file_name,self.src_path)
          if self.args is not None:
             with open(os.path.join(self.path,'opts.txt'),'w',encoding='utf-8') as dest:
                  dest.write(str(self.args)+'\n')

      def writeList(self,results,fileName):
          with open(os.path.join(self.path,fileName),'w',encoding='utf-8') as dest:
               for item in results:
                   dest.write(str(item)+'\n')
      def writeRun(self,run_dict):
          with open(os.path.join(self.path,'runs.tsv'),'w',encoding='utf-8') as dest:
            list_run_items=list(run_dict.items())
            list_run_items.sort(key=lambda x:x[0])
            for qid,list_pair in list_run_items:
                rank=0
                for pair in list_pair:
                    rank+=1
                    dest.write('%s\tQ0\t%s\t%d\t%f\t%s\n'%(qid,pair[0],rank,pair[1],'srm'))