#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
#

"""#

from twisted.internet import reactor

import sys, time
import os
from snAppyModules.snUseCases import *

#from snAppyTests.snTests import *
from snAppyModules.snQueryComposers import *
from snAppyModules.snParsers import *
from snAppyModules.snAppyConfig import *
from snAppyModules.pyDaemon3 import Daemon3

from twisted.internet import task
from twisted.python import threadpool as tp

class nxtClientFactory(ClientFactory):

    def __init__(self, ):
        super(nxtClientFactory, self).__init__()
        #log.msg("2c --nxtClientFactory---->  build Client Protocol" )
        self.ok=True

class nxtServerFactory(ServerFactory):

    def __init__(self, queryComposers, parsers, environ):
        super(nxtServerFactory, self).__init__()
        self.ok=True
        #print(environ)

        self.parser_XML = parsers['parser_XML']
        self.parser_777 = parsers['parser_777']
        self.parser_RPC = parsers['parser_RPC']
        self.parser_LOC = parsers['parser_LOC']
        self.parser_LOC.environ = environ

        self.qComp_777 = queryComposers['qComp_777']    # .API_calls
        self.qComp_XML = queryComposers['qComp_XML']
        self.qComp_LOC = queryComposers['qComp_LOC']

    def buildProtocol(self, addr):
        proto = ProxyServerProtocolSuperNET()
        # this is where we plug this namespace into the Protocol!
        proto.proxyServerFactory = self #
        log.msg(1*"\n1 nxtServerFactory---->  builds a  Protocol", str(proto), '@',str(addr))
        return proto

# Adapted from http://stackoverflow.com/a/15645169/221061
class ProxyServerProtocolSuperNET(protocol.Protocol):
    """# Client => Proxy
        # Here we receive the initial request.
        # Here we parse the request
        # Here we route the request to the client that sends the specific query to the remote server.
    """#

    def __init__(self):

        self.buffer = None
        self.client = None

    def connectionMade(self):
        """  here we must connect to EITHER jl777 OR some external data feed
             so either we do this as a deferred OR we move this into the GET processing routine where we find out what API is called
            Instantiate CLientFactory here, but not yet the Protocoal, because the Req needs to be parsed first in order to decide which Prot.
        """ #
        #log.msg("ProxyServerProtocolSuperNET 1 ----> build Protocol: connectionMade")
        self.clientFactory = nxtClientFactory()
        # NOTE: the nxtServerFactory instance is always the same, but the nxtClientFactory is a new one every time.

    def dataReceived(self, rawRequest):
        """ Protocol ONLY knows dataReceived. dataReceived Has to be overridden, and the processing pipeline added:
        render_GET, etc... """ #
        # ToDo: check mechnism: - self.client? this has to do with connection status somehow.
        if self.client:
            log.msg("ProxyServerProtocolSuperNET 2a::\n\n", str(rawRequest))
            self.client.write(rawRequest)
        else:
            #log.msg("ProxyServerProtocolSuperNET 2b--dataReceived- ", str(rawRequest))
            self.identify_req_type(rawRequest)

    def identify_req_type(self, rawRequest):
        """- this is low level as opposed to the Site API
            Doing this low level stuff myself is a bit tedious but it offers flexibility for custom mods.

        Called whenever data is received.

        Use this method to translate to a higher-level message.  Usually, some
        callback will be made upon the receipt of each complete protocol
        message.
        We can process both, GET and POST! (POST not implemented yet)

        """ #
        POST = rawRequest[:4]==bytes('POST',"utf-8")
        GET = rawRequest[:3]==bytes('GET',"utf-8")

        if POST:
            self.render_POST(rawRequest)
        elif GET:
            self.render_GET(rawRequest)
            #log.msg("ProxyServerProtocolSuperNET 3--parse_request-- ", str(rawRequest), " time: "  ,str(time.time()))
        else:
            self.transport.loseConnection()
            raise RuntimeError( ("request: "+str(rawRequest) ))

        return None

    def render_GET(self, request):
        """
        The requests are received here as raw bytestrings.
        Example: rawrequst:  <class 'bytes'>
        b'GET /nxt?requestType=getpeers HTTP/1.1\r\nUser-Agent: curl/7.35.0\r\nHost: 127.0.0.1:7777\r\nAccept: */*\r\ncontent-type: text/plain;\r\n\r\n'

        We don't use a lib here because it is very easy and we can't get into trouble with libs.

        Here we do all kinds of parsing. Can be modified at will to adapt.
        """ #

        #log.msg("ProxyServerProtocolSuperNET 4 render_GET GET. time---> ",str(time.time()))
        reqMeth = request.split()[1]

        ###################################################################
        #
        # here we have some internal contollers to start and stop use cases, and to stop the whole api
        #
        ###################################################################
        stopSelf = ( reqMeth[:6].decode("utf-8")  == '/stop?')
        if stopSelf:
            log.msg("snApi caught stop- shutting down @ time: "  ,str(time.time()))
            self.transport.loseConnection()
            os.kill(os.getpid(),15)

        startUC = ( reqMeth[:6].decode("utf-8")  == '/ucstart?')
        if startUC:
            log.msg("snApi caught startUC-   @ time: "  ,str(time.time()))
            self.transport.loseConnection()
            return None
        stopUC = ( reqMeth[:6].decode("utf-8")  == '/ucstop?')
        if startUC:
            log.msg("snApi caught stopUC-   @ time: "  ,str(time.time()))
            self.transport.loseConnection()
            return None

        reqOK = ( reqMeth[:5].decode("utf-8")  == '/nxt?') # drop all else snAp.py
        if not reqOK:
            self.transport.write(b'unspecific error in ProxyServerProtocolSuperNET.render_GET')
            self.transport.loseConnection()
            return None

        reqMeth = reqMeth[5:]
        reqMeth = reqMeth.decode()

        reqLi = reqMeth.split('&')
        reqDict = {}

        if len(reqLi) < 1:
            raise LookupError(str(request))
            self.transport.loseConnection()
            return None

        for req in reqLi:
            try:
                reqDict[req.split('=')[0]] = req.split('=')[1]
            except:
                raise LookupError(str(request))
                self.transport.loseConnection()

        if 'requestType' not in reqDict.keys():
            self.transport.loseConnection()
            return None

        #
        # This invokes different Client Server/Factory instances depending on what calls are requested.
        # port, parser, qcomposer
        #

        if reqDict['requestType'] in self.proxyServerFactory.qComp_777.API_calls:
            # send to the 777Composer
            # self.clientFactory.protocol - the 'protocol' MUST remain GENERIC!!! keep this Note here.
            self.clientFactory.protocol = ProxyClientProtocol777
            self.clientFactory.protocol.parser_777 = self.proxyServerFactory.parser_777
            self.clientFactory.server = self #
            self.clientFactory.requestType = reqDict['requestType'] # just put it in here to be available for the Parser!
            reactor.connectTCP( SERVER_ADDR_jl777, SERVER_PORT_SUPERNETHTTP, self.clientFactory)
            #print(6*"\nreqDict:", reqDict)
            self.newQuery = self.proxyServerFactory.qComp_777.make_777POST_Request(reqDict)
            #print(6*"\nquery:", self.newQuery)

        elif reqDict['requestType'] in  ["start","stop" ]:
        # direct to BTCD RPC, use that parser ONLY for START and stop call that must go through BTCD
        # Note: there is a 'passthrough' call in the api to talk to RPC coins.
        # only 'start' and 'stop' can't be issued to SuperNET directly.
            # self.clientFactory.protocol - the 'protocol' MUST remain GENERIC!!! keep this Note here.
            self.clientFactory.protocol = ProxyClientProtocolRPC
            self.clientFactory.protocol.parser_RPC = self.proxyServerFactory.parser_RPC
            self.clientFactory.server = self #
            self.clientFactory.requestType = reqDict['requestType'] # just put it in here to be available for the Parser!
            reactor.connectTCP( SERVER_ADDR_jl777, SERVER_PORT_BTCD_RPC, self.clientFactory)
            # NOTE: this uses the same querycomposer that 777 also uses!!!
            self.newQuery = self.proxyServerFactory.qComp_777.make_rawBytes_Request(reqDict)
            self.transport.loseConnection() # this is because we won't get a reply afte stoppping and we must close ourself!

        elif reqDict['requestType'] in self.proxyServerFactory.qComp_XML.API_calls:
            # self.clientFactory.protocol - the 'protocol' MUST remain GENERIC!!! keep this Note here.
            self.clientFactory.protocol = ProxyClientProtocolXML
            self.clientFactory.protocol.parser_XML = self.proxyServerFactory.parser_XML
            self.clientFactory.server = self #
            self.clientFactory.requestType = reqDict['requestType'] # just put it in here to be available for the Parser!
            reactor.connectTCP(SERVER_ADDR_xmlFeed1, SERVER_PORT_xmlFeed1, self.clientFactory)
            self.newQuery = self.proxyServerFactory.qComp_XML.make_XML_Request(reqDict)

        elif reqDict['requestType'] in self.proxyServerFactory.qComp_LOC.API_calls:
            self.clientFactory.protocol = ProxyClientProtocolLOC
            self.clientFactory.protocol.parser_LOC = self.proxyServerFactory.parser_LOC
            self.clientFactory.server = self #
            self.clientFactory.requestType = reqDict['requestType'] # just put it in here to be available for the Parser!
            #
            # LOCAL handling is different, we don't use POST or GET, but do other stuff as load files or so.
            #
            # use deferToThread in UCclasses
            #
            dataLocalCacheBytes = self.proxyServerFactory.qComp_LOC.make_LOC_Request(reqDict)
            dataLocalCacheParsedBytes = self.proxyServerFactory.parser_LOC.parse_XML( dataLocalCacheBytes, reqDict)
            self.write(dataLocalCacheParsedBytes)
            # call the ClientReceiving Function DIRECTLY here, we don't send the request through the interwebz
            self.newQuery = 'dummyUpForLocCache'
            return None # MAYBE GOOD to do this here because we go to self.write() DIRECTLY FROM HERE!?!?!

        else:
            raise LookupError(str(request))
            self.transport.loseConnection()
            return None

        if self.newQuery == 'error':
            raise LookupError(str(request))
            self.transport.loseConnection()
            return None

    def write(self, data):
        """ Here the processed reply finally leaves the proxy again and goes back to the requester:
            ServerPartOfProxy => WebClient
            This function expects fully processed and formatted reply data, it only returns the data w/o any further processing.
        """ #

        log.msg("ProxyServerProtocolSuperNET 5: proxy returns to client:", str(len(data)), str(type(data)), str((data)[:100]))#
        self.transport.write(data)
        self.transport.loseConnection()

class ProxyClientProtocolLOC(protocol.Protocol):
    """ This ProxyClient does local stuff """#
    pass

class ProxyClientProtocolRPC(protocol.Protocol):
    """ This ProxyClient queries the SuperNET server using POST DIRECTLY RPC """#

    def connectionMade(self):
        self.factory.server.client = self
        try:
            requestOUT = self.factory.server.newQuery     #.decode("utf-8")
        except:
            self.transport.loseConnection()
            return None
        # two end Protocol: here we WRITE out to the remote.
        # we expect the dataReceived back. What if that doenst happen? as in stop or start?
        self.transport.write(requestOUT)
        self.factory.server.requestOUT = '' # cleanup

    def read_raw_ERR(self):
        pass

    # NEED A DEFERRED TO READ RAW
    def read_raw_POST(self, data_RPC):
        print("**** data_777 --->", str(data_RPC))
        pass

    def dataReceived(self, data_RPC):
        """ this receives the RAW reply from the jl777lib. POSTprocessing needs to be done. """ #
        log.msg("ProxyClientProtocolRPC dataReceived - from remoteServer LENGTH:: ", str(len(data_RPC)))
        log.msg("ProxyClientProtocolRPC - from remoteServer data_777: ", str((data_RPC)))
        data_RPC_parsed = self.parser_RPC.parse_RPC(data_RPC, self.factory.requestType )
        self.factory.server.write(data_RPC_parsed)
        self.transport.loseConnection()
        return None

class ProxyClientProtocol777(protocol.Protocol):
    """ This ProxyClient queries the SuperNET server using POST DIRECTLY """#

    def connectionMade(self):

        self.factory.server.client = self
        #log.msg(15*"\n~~~~~~~~~~~~~>", self.factory.server.newQuery)
        try:
            preppedReq777 = self.factory.server.newQuery
        except:
            self.transport.write(b'ERROR in ProxyClientProtocol777.connectionMade')
            self.transport.loseConnection()
            return None
        #log.msg(15*"\n~~~~~~~~~~~~~>", preppedReq777)
        self.deferred = deferToThread(requests.post, FULL_URL, data=json.dumps(preppedReq777), headers= POSTHEADERS)
        self.deferred.addCallback(self.rcvPOST)
        self.deferred.addErrback(self.rcvPOSTERR)

        # stat1 = reactor.threadpool.waiters
        # stat2 = reactor.threadpool.workers
        # stat3 = reactor.threadpool.threads
        # stat4 = reactor.threadpool.q.queue
        #
        # log.msg("waiters: ", stat1)
        # log.msg("workers: ", stat2)
        # log.msg("threads: ", stat3)
        # log.msg("queue: ", stat4)

        # putting this into a Session() object fails miserabley due to some threading!
        # this is for raw transport level bytes writing!
        # self.transport.write(requestOUT)
        # self.factory.server.requestOUT = '' # cleanup

    def rcvPOSTERR(self,retPOSTERR):
        log.msg(10*"\n++++++++++ERRR in ProxyClientProtocol777+++", retPOSTERR, str(retPOSTERR))
        self.factory.server.write("rcvPOSTERR() ERROR")
        self.transport.loseConnection()
        return None

    def rcvPOST(self, data_777):
        """ this receives the RAW reply from the jl777lib. POSTprocessing needs to be done. """ #
        log.msg("ProxyClientProtocol777 dataReceived - from remoteServer type:: ", type(data_777))
        # here we receive the prepared data for sending back through the port to the GET requester
        data_777_parsedBytes = self.parser_777.parse_777(data_777, self.factory.requestType )
        #print(data_777_parsedBytes)
        self.factory.server.write(data_777_parsedBytes) #data_777_parsed)
        self.transport.loseConnection()
        return None

class ProxyClientProtocolXML(protocol.Protocol):
    """ This ProxyClient is using the TWISTED getPage function """#

    def connectionMade(self):
        self.factory.server.client = self

        try:

            requestOUT = self.factory.server.newQuery     #.decode("utf-8")

        except:
            self.transport.loseConnection()
            return None

        self.getPage_deferred =  getPage(requestOUT)
        # """can I do multiple here??"""

        self.getPage_deferred.addCallback(self.pageReceived)
        self.getPage_deferred.addErrback(self.handleFailure)
        self.factory.server.requestOUT = ''
        self.query_xmlFeed1 = False
        log.msg("-2e--ProxyClientProtocolXML----requestOUT   -----register deferred ---------->:",requestOUT) # only for GET , ppOST is different

    # Server => Proxy
    def handleFailure(self, err):
        raise RuntimeError(str(err))

    # this will be the deferreed
    def pageReceived(self, data_XML):

        log.msg("3 ProxyClientProtocolXML dataReceived - from remoteServer: ", str(len(data_XML)))
        log.msg(1*"\n3a dataReceived - from remoteServer: ", str(data_XML)[:200])

        print(self.parser_XML)
        print(self.parser_XML.parse_XML)
        print(self.factory.requestType)
        data_XML_parsed = self.parser_XML.parse_XML(data_XML,  self.factory.requestType)

        self.factory.server.write(data_XML_parsed)
        self.transport.loseConnection()
        return None

class SuperNETApiD(Daemon3):

    """ This is the SuperNET API Main class.
         This is instantiated by the DEMON controller app.
         This is supposed to be a singleton,
        and so are the class objects that are declared up here.  """#

    # instead of doing this in the class head here, we could also make a local method 'def makeContext'
    environ['snApiDir'] = os.getcwd()
    environ['localCacheDir'] = os.getcwd() + environ['CACHE_DIR']
    # note: there is something really unpleasant going on with the 'environ name here!
    # this creates the ApiCOntext!

    qComp_777 = QueryComposer_777(environ) # Querycomposer is part of the SERVER part, composes the query, and hands the Q to the CLIENT part.
    qComp_XML = QueryComposer_XML(environ)
    qComp_LOC = QueryComposer_LOC(environ)
    #qComp_RPC = QueryComposer_RPC(environ)

    parser_XML = Parser_XML(environ)
    parser_777 = Parser_777(environ)
    parser_RPC = Parser_RPC(environ)
    parser_LOC = Parser_LOC(environ)

    parsers = {
                'parser_XML':  parser_XML,\
                'parser_777':  parser_777,\
                'parser_RPC':  Parser_RPC,\
                'parser_LOC':  parser_LOC
                }

    queryComposers = {
                        'qComp_777': qComp_777,\
                        'qComp_XML': qComp_XML,\
                        'qComp_LOC': qComp_LOC
                        }

    UCs = [
            'start', 'stop', 'restart',
            'UC1', 'UC2', 'UC3', 'UC4', 'UC5', 'UC6',
            'UC7', 'UC8', 'UC9', 'UC10', 'UC11','UC12',
            ]

    def __init__(self, pidfile):
        self.environ = environ
        super(SuperNETApiD, self).__init__(pidfile)

    def run(self):
        log.msg("calling init")
        self.init()
        log.msg("init() done. starting reactor.run()")
        reactor.run()

    def init(self):
        """
        serverfactory gets all parsers and qcomps, and builds the clientFactories according to what is supposed to happen
        name scoping is very tricky here. In case of problems, check the object instance identities by print(self) here!
        The SERVERfactory always the same object! It does NOT get re-instantiated with each new call.
        Factory is somewhat flexible as to what argument types it gets! """#

        log.startLogging(sys.stdout) # check: logfile or other output ?
        # factory for ad hoc requests received from external sources
        serverFactory = nxtServerFactory(SuperNETApiD.queryComposers, SuperNETApiD.parsers, self.environ)
        serverFactory.protocol = ProxyServerProtocolSuperNET # <- this is not an instance this is the CLASS!!!!

        reactor.suggestThreadPoolSize(500) # should be ok
        log.msg("stats: ",reactor.threadpool.dumpStats())
        log.msg("workers: ", tp.ThreadPool.workers)

        serverFactory.reactor = reactor

        uc_scd_XML_SportsdataLLC = UC_Scheduler_XML(serverFactory,  self.environ ) # environ has the credentials and all
        timer1 = task.LoopingCall(uc_scd_XML_SportsdataLLC.periodic,  )
        timer1.start( TIMER_15000, now=True ) # slow heartbeat, start now TODO: the NOW does not seem to work!
        reactor.listenTCP(LISTEN_PORT_SNT, serverFactory)

        # can make as many as we want here with specific timers and tasks

    def runUC(self, UC):
        log.msg( 1 * "start UC: ", UC)

        if UC in self.UCs:
            print("UC1 - 0")
            self.initUC(UC)
        else:
            log.msg("UC name error")

        log.msg("initUC() done. starting reactor.run()")
        reactor.run()

    def initUC(self, UC):
        """
        serverfactory gets all parsers and qcomps, and builds the clientFactories according to what is supposed to happen
        name scoping is very tricky here. In case of problems, check the object instance identities by print(self) here!
        The SERVERfactory always the same object! It does NOT get re-instantiated with each new call.
        Factory is somewhat flexible as to what argument types it gets!
        """#

        self.UC_results = {'here we can collect results for individual test cases' : True}

        if UC == 'UC1':
            self.startUC1()
        elif UC == 'UC2':
            self.startUC2()
        elif UC == 'UC3':
            self.startUC3()
        elif UC == 'UC4':
            self.startUC4()
        elif UC == 'UC5':
            self.startUC5()
        elif UC == 'UC6':
            self.startUC6()
        elif UC == 'UC7':
            self.startUC7()
        elif UC == 'UC8':
            self.startUC8()
        elif UC == 'UC9':
            self.startUC9()
        elif UC == 'UC10':
            self.startUC10()
        elif UC == 'UC11':
            self.startUC11()
        elif UC == 'UC12':
            self.startUC12()

        else:
            log.msg("UC name error")

    def startUC1(self):
        log.startLogging(sys.stdout)
        serverFactory = nxtServerFactory(SuperNETApiD.queryComposers, SuperNETApiD.parsers, self.environ)
        serverFactory.protocol = ProxyServerProtocolSuperNET # <- this is not an instance this is the CLASS!!!!
        log.msg(1*"initUC1")
        uc1_pingPong = UC1_pingPong(serverFactory,  self, self.environ ,  ) # also hand in 'self' here as a means to stop self
        self.timer1 = task.LoopingCall(uc1_pingPong.periodic,  )
        self.timer1.start( TIMER_850 , now=True )
        reactor.suggestThreadPoolSize(500)
        reactor.listenTCP(LISTEN_PORT_SNT, serverFactory)

    def stopUC1(self, result):
        log.msg(1*"\n                           STOP UC1 with result: ", result, "\n")
        self.timer1.stop( )
        #self.stop()
        self.startUC2()

    def startUC2(self):
        log.startLogging(sys.stdout)
        serverFactory = nxtServerFactory(SuperNETApiD.queryComposers, SuperNETApiD.parsers, self.environ)
        serverFactory.protocol = ProxyServerProtocolSuperNET # <- this is not an instance this is the CLASS!!!!
        log.msg(1*"initUC2")
        uc2_havenode = UC2_havenode(serverFactory, self, self.environ )
        reactor.suggestThreadPoolSize(500)
        serverFactory.reactor = reactor # this # is only used ATM to access to access thread stats
        try:
            reactor.listenTCP(LISTEN_PORT_SNT, serverFactory)
        except Exception as e:
            log.msg("already listening, continue.{0}".format(str(e)))

        self.timer2 = task.LoopingCall(uc2_havenode.periodic,  )
        self.timer2.start( TIMER_850 , now=True )

    def stopUC2(self, result):
        log.msg(5*"\n\n                           STOP UC2 with result:  ", result, "\n")
        self.timer2.stop( )
        #self.stop()
        self.startUC3()

    def startUC3(self):
        log.startLogging(sys.stdout)
        serverFactory = nxtServerFactory(SuperNETApiD.queryComposers, SuperNETApiD.parsers, self.environ)
        serverFactory.protocol = ProxyServerProtocolSuperNET # <- this is not an instance this is the CLASS!!!!
        log.msg(10*"\ninitUC3")
        uc3_store_findvalue = UC3_store_findvalue(serverFactory, self, self.environ )
        serverFactory.reactor = reactor # this # is only used ATM to access to access thread stats
        reactor.suggestThreadPoolSize(500)
        try:
            reactor.listenTCP(LISTEN_PORT_SNT, serverFactory)
        except Exception as e:
            log.msg("already listening, continue.{0}".format(str(e)))

        self.timer3 = task.LoopingCall(uc3_store_findvalue.periodic,  )
        self.timer3.start( TIMER_850 , now=True )

    def stopUC3(self, result):
        log.msg(5*"\n\n                           STOP UC3 with result:  ", result, "\n")
        self.timer3.stop( )
        log.msg("STOP snappyDaemon")
        #self.stop()
        self.startUC4()

    def startUC4(self):
        log.startLogging(sys.stdout)
        serverFactory = nxtServerFactory(SuperNETApiD.queryComposers, SuperNETApiD.parsers, self.environ)
        serverFactory.protocol = ProxyServerProtocolSuperNET # <- this is not an instance this is the CLASS!!!!
        log.msg(1*"initUC4")
        reactor.suggestThreadPoolSize(500) # should be ok
        serverFactory.reactor = reactor # this # is only used ATM to access to access thread stats
        try:
            reactor.listenTCP(LISTEN_PORT_SNT, serverFactory) # this is needed to also recevies GET queries
        except Exception as e:
            log.msg("already listening, continue.{0}".format(str(e)))

        uc4_sendMSG = UC4_sendMSG(serverFactory, self,  self.environ )

        self.timer4 = task.LoopingCall(uc4_sendMSG.periodic,  )
        self.timer4.start( TIMER_850, now=True )

    def stopUC4(self,result):
        log.msg(5*"\n\n                           STOP UC4 with result:  ", result, "\n")
        self.timer4.stop( )
        log.msg("STOP snappyDaemon")
        #self.stop()
        self.startUC5()

    def startUC5(self):
        log.startLogging(sys.stdout)
        serverFactory = nxtServerFactory(SuperNETApiD.queryComposers, SuperNETApiD.parsers, self.environ)
        serverFactory.protocol = ProxyServerProtocolSuperNET # <- this is not an instance this is the CLASS!!!!
        log.msg(1*"initUC5")
        reactor.suggestThreadPoolSize(500) # should be ok
        serverFactory.reactor = reactor # this # is only used ATM to access to access thread stats
        try:
            reactor.listenTCP(LISTEN_PORT_SNT, serverFactory) # this is needed to also recevies GET queries
        except Exception as e:
            log.msg("already listening, continue.{0}".format(str(e)))

        uc5_sendBIN = UC5_sendBIN(serverFactory, self,  self.environ )

        self.timer5 = task.LoopingCall(uc5_sendBIN.periodic,  )
        self.timer5.start( TIMER_850 , now=True )

    def stopUC5(self,result):
        log.msg(5*"\n\n                           STOP UC5 with result:  ", result, "\n")
        self.timer5.stop( )
        log.msg("STOP snappyDaemon")
        #self.stop()
        self.startUC6()

    def startUC6(self):
        log.startLogging(sys.stdout)
        serverFactory = nxtServerFactory(SuperNETApiD.queryComposers, SuperNETApiD.parsers, self.environ)
        serverFactory.protocol = ProxyServerProtocolSuperNET # <- this is not an instance this is the CLASS!!!!
        log.msg(1*"initUC6")
        reactor.suggestThreadPoolSize(500) # should be ok
        serverFactory.reactor = reactor # this # is only used ATM to access to access thread stats
        try:
            reactor.listenTCP(LISTEN_PORT_SNT, serverFactory) # this is needed to also recevies GET queries
        except Exception as e:
            log.msg("already listening, continue.{0}".format(str(e)))

        uc6_checkMSG = UC6_checkMSG(serverFactory, self,  self.environ )

        self.timer6 = task.LoopingCall(uc6_checkMSG.periodic,  )
        self.timer6.start( TIMER_850 , now=True )

    def stopUC6(self,result):
        log.msg(5*"\n\n                           STOP UC6 with result:  ", result, "\n")
        self.timer6.stop( )
        log.msg("STOP snappyDaemon")
        #self.stop()
        self.startUC7()

    def startUC7(self):
        log.startLogging(sys.stdout)
        serverFactory = nxtServerFactory(SuperNETApiD.queryComposers, SuperNETApiD.parsers, self.environ)
        serverFactory.protocol = ProxyServerProtocolSuperNET # <- this is not an instance this is the CLASS!!!!
        log.msg(1*"initUC7")
        reactor.suggestThreadPoolSize(500) # should be ok
        serverFactory.reactor = reactor # this # is only used ATM to access to access thread stats
        try:
            reactor.listenTCP(LISTEN_PORT_SNT, serverFactory) # this is needed to also recevies GET queries
        except Exception as e:
            log.msg("already listening, continue.{0}".format(str(e)))

        uc7_findaddress = UC7_findaddress(serverFactory, self,  self.environ )

        self.timer7 = task.LoopingCall(uc7_findaddress.periodic,  )
        self.timer7.start( TIMER_850 , now=True )

    def stopUC7(self,result):
        log.msg(5*"\n\n                           STOP UC7 with result:  ", result, "\n")
        self.timer7.stop( )
        log.msg("STOP snappyDaemon")
        self.stop()
        #self.startUC8()

    def startUC8(self):
        log.startLogging(sys.stdout)
        serverFactory = nxtServerFactory(SuperNETApiD.queryComposers, SuperNETApiD.parsers, self.environ)
        serverFactory.protocol = ProxyServerProtocolSuperNET # <- this is not an instance this is the CLASS!!!!
        log.msg(1*"initUC8")
        reactor.suggestThreadPoolSize(500) # should be ok
        serverFactory.reactor = reactor # this # is only used ATM to access to access thread stats
        try:
            reactor.listenTCP(LISTEN_PORT_SNT, serverFactory) # this is needed to also recevies GET queries
        except Exception as e:
            log.msg("already listening, continue.{0}".format(str(e)))

        uc8_contacts = UC8_contacts(serverFactory, self,  self.environ )

        self.timer8 = task.LoopingCall(uc8_contacts.periodic,  )
        self.timer8.start( TIMER_850 , now=True )

    def stopUC8(self,result):
        log.msg(5*"\n\n                           STOP UC8 with result:  ", result, "\n")
        self.timer8.stop( )
        log.msg("STOP snappyDaemon")
        self.stop()

    def startUC9(self):
        log.startLogging(sys.stdout)
        serverFactory = nxtServerFactory(SuperNETApiD.queryComposers, SuperNETApiD.parsers, self.environ)
        serverFactory.protocol = ProxyServerProtocolSuperNET # <- this is not an instance this is the CLASS!!!!
        log.msg(1*"initUC9")
        reactor.suggestThreadPoolSize(500) # should be ok
        serverFactory.reactor = reactor # this # is only used ATM to access to access thread stats
        try:
            reactor.listenTCP(LISTEN_PORT_SNT, serverFactory) # this is needed to also recevies GET queries
        except Exception as e:
            log.msg("already listening, continue.{0}".format(str(e)))

        uc9_getdb = UC9_getdb(serverFactory, self,  self.environ )

        self.timer9 = task.LoopingCall(uc9_getdb.periodic,  )
        self.timer9.start( TIMER_850 , now=True )

    def stopUC9(self,result):
        log.msg(5*"\n\n                           STOP UC9 with result:  ", result, "\n")
        self.timer9.stop( )
        log.msg("STOP snappyDaemon")
        self.stop()

    def startUC10(self):
        log.startLogging(sys.stdout)
        serverFactory = nxtServerFactory(SuperNETApiD.queryComposers, SuperNETApiD.parsers, self.environ)
        serverFactory.protocol = ProxyServerProtocolSuperNET # <- this is not an instance this is the CLASS!!!!
        log.msg(1*"initUC10")
        reactor.suggestThreadPoolSize(500) # should be ok
        serverFactory.reactor = reactor # this # is only used ATM to access to access thread stats
        try:
            reactor.listenTCP(LISTEN_PORT_SNT, serverFactory) # this is needed to also recevies GET queries
        except Exception as e:
            log.msg("already listening, continue.{0}".format(str(e)))

        uc10_IDEX_placeAB  = UC10_IDEX_placeAB(serverFactory, self,  self.environ )

        self.timer10 = task.LoopingCall(uc10_IDEX_placeAB.periodic,  )
        self.timer10.start( TIMER_850 , now=True )

    def stopUC10(self,result):
        log.msg(5*"\n\n                           STOP UC10 with result:  ", result, "\n")
        self.timer10.stop( )
        log.msg("STOP snappyDaemon")
        self.stop()

    def startUC11(self):
        log.startLogging(sys.stdout)
        serverFactory = nxtServerFactory(SuperNETApiD.queryComposers, SuperNETApiD.parsers, self.environ)
        serverFactory.protocol = ProxyServerProtocolSuperNET # <- this is not an instance this is the CLASS!!!!
        log.msg(1*"initUC11")
        reactor.suggestThreadPoolSize(500) # should be ok
        serverFactory.reactor = reactor # this # is only used ATM to access to access thread stats
        try:
            reactor.listenTCP(LISTEN_PORT_SNT, serverFactory) # this is needed to also recevies GET queries
        except Exception as e:
            log.msg("already listening, continue.{0}".format(str(e)))

        uc11_priceDB  = UC11_priceDB(serverFactory, self,  self.environ )

        self.timer11 = task.LoopingCall(uc11_priceDB.periodic,  )
        self.timer11.start( TIMER_850 , now=True )

    def stopUC11(self,result):
        log.msg(5*"\n\n                           STOP UC11 with result:  ", result, "\n")
        self.timer11.stop( )
        log.msg("STOP snappyDaemon")
        self.stop()

    def startUC12(self):
        log.startLogging(sys.stdout)
        serverFactory = nxtServerFactory(SuperNETApiD.queryComposers, SuperNETApiD.parsers, self.environ)
        serverFactory.protocol = ProxyServerProtocolSuperNET # <- this is not an instance this is the CLASS!!!!
        log.msg(1*"initUC12")
        reactor.suggestThreadPoolSize(500) # should be ok
        serverFactory.reactor = reactor # this # is only used ATM to access to access thread stats
        try:
            reactor.listenTCP(LISTEN_PORT_SNT, serverFactory) # this is needed to also recevies GET queries
        except Exception as e:
            log.msg("already listening, continue.{0}".format(str(e)))

        uc12_save_restore_File = UC12_save_restore_File(serverFactory, self,  self.environ )

        self.timer12 = task.LoopingCall(uc12_save_restore_File.periodic,  )
        self.timer12.start( TIMER_850 , now=True )

    def stopUC12(self,result):
        log.msg(5*"\n\n                           STOP UC12 with result:  ", result, "\n")
        self.timer12.stop( )
        log.msg("STOP snappyDaemon")
        self.stop()

if __name__ == "__main__":

    pidFileName = '/tmp/SuperNET_API.pid'
    superNetApiD = SuperNETApiD(pidFileName) # '/tmp/daemon-example.pid')
    #
    # Note:
    # we are using standard Daemonization here and NOT twistd,
    # becasue that seems to be not properly ported to python3 yet.
    # Also, it is difficult to understand and badly documented.
    # It is better to do it with the standard python recipe, and have opportunity of intervention.
    #
    # Also, the sequence of starting reactor and Daemon is sensitive!

    UCs = [
            'start', 'stop', 'restart',
            'UC1', 'UC2', 'UC3', 'UC4', 'UC5', 'UC6',
            'UC7', 'UC8','UC9', 'UC10', 'UC11','UC12',
            ]

    if len(sys.argv) == 2:
        UC=sys.argv[1]
        if UC not in UCs:
            print("Unknown command")
            sys.exit(2)

        if 'start' == sys.argv[1]:
            superNetApiD.start()
        elif 'stop' == sys.argv[1]:
            superNetApiD.stop()
        elif 'restart' == sys.argv[1]:
            superNetApiD.restart()

        else:
            superNetApiD.startUC(UC)

        sys.exit(0)

    else:
        print("usage: %s start|stop|restart|UC" % sys.argv[0])
        sys.exit(2)


from typing import Dict, List, Set

from .token import RegularToken

class RegularLexer:
    def __init__(self):
        self.__token_list: List[RegularToken] = []
        self.__line: int = 1
        self.__error_index: int = -1
        self.__no_error_index: int = -1
        self.__skip: bool = False
        self.__compact_move_table: Dict[int, List[list]] = {
            0: [
                [0, {'{'}, [], 1],
                [0, {'|'}, [], 2],
                [0, {'.'}, [], 3],
                [0, {'\\'}, [], 4],
                [2, {'?'}, [('(', '.'), ('0', '9'), ('[', '^'), ('{', '}')], 5],
                [0, set(), [('0', '9')], 6],
                [0, {'['}, [], 7],
                [0, {'^'}, [], 8],
                [0, {']'}, [], 9],
                [0, {'('}, [], 10],
                [0, {')'}, [], 11],
                [0, {'-'}, [], 12],
                [0, {'+'}, [], 13],
                [0, {'*'}, [], 14],
                [0, {'?'}, [], 15],
                [0, {','}, [], 16],
                [0, {'}'}, [], 17]
            ],
            4: [
                [2, {'u'}, [], 18],
                [0, {'u'}, [], 19]
            ],
            19: [
                [0, set(), [('0', '9'), ('A', 'F'), ('a', 'f')], 20]
            ],
            20: [
                [0, set(), [('0', '9'), ('A', 'F'), ('a', 'f')], 21]
            ],
            21: [
                [0, set(), [('0', '9'), ('A', 'F'), ('a', 'f')], 22]
            ],
            22: [
                [0, set(), [('0', '9'), ('A', 'F'), ('a', 'f')], 23]
            ],
            1: [
                [0, {'_'}, [('A', 'Z'), ('a', 'z')], 24]
            ],
            24: [
                [0, {'_'}, [('0', '9'), ('A', 'Z'), ('a', 'z')], 24],
                [0, {'}'}, [], 25]
            ]
        }
        self.__character_set: Set[str] = {'6', '_', '7', 'x', '0', 'O', '9', 'l', '-', '1', 'b', 'P', 'F', 'e', 'Y', 'o', 'k', 'd', '^', 'n', 'W', '.', 'G', ')', '}', 'u', 'N', 'p', ']', 'A', ',', 'q', 't', 'f', 'Z', '8', 'R', '{', 'K', 'L', 'Q', '+', 'T', 'U', 'i', 'y', 'a', 'I', 'j', 'm', 'B', '3', 'C', '|', '*', 'J', 'h', 's', 'D', 'S', 'r', 'H', 'V', 'v', 'X', '[', 'w', 'M', 'z', '2', '?', '4', 'g', 'E', '5', 'c', '(', '\\'}
        self.__start_state: int = 0
        self.__end_state_set: Set[int] = {1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 23, 25}
        self.__lexical_symbol_mapping: Dict[int, str] = {
            1: '!symbol_16',
            2: '!symbol_1',
            3: '!symbol_2',
            5: '!symbol_4',
            6: '!symbol_6',
            7: '!symbol_7',
            8: '!symbol_8',
            9: '!symbol_9',
            10: '!symbol_10',
            11: '!symbol_11',
            12: '!symbol_12',
            13: '!symbol_13',
            14: '!symbol_14',
            15: '!symbol_15',
            16: '!symbol_17',
            17: '!symbol_18',
            18: '!symbol_3',
            19: '!symbol_3',
            23: '!symbol_5',
            25: 'reference'
        }
        self.__non_greedy_state_set: Set[int] = set()
        self.__symbol_function_mapping: Dict[str, List[str]] = {
            'reference': ['reference']
        }
        self.__lexical_function: Dict[str, callable] = {}

    def _invoke_lexical_function(self, symbol: str, token_string: str) -> str:
        self.__skip: bool = False
        if symbol in self.__symbol_function_mapping:
            for function in self.__symbol_function_mapping[symbol]:
                if function in self.__lexical_function:
                    token_string = self.__lexical_function[function](token_string)
                elif function == 'skip':
                    self.skip()
                elif function == 'newline':
                    self.newline()
        return token_string

    def _generate_token(self, state: int, token_string: str) -> None:
        symbol: str = self.__lexical_symbol_mapping.get(state, '!symbol')
        token_string: str = self._invoke_lexical_function(symbol, token_string)
        if not self.__skip:
            self.__token_list.append(RegularToken(token_string, self.__line, symbol))

    def token_list(self) -> List[RegularToken]:
        return self.__token_list

    def line(self) -> int:
        return self.__line

    def skip(self) -> None:
        self.__skip = True

    def newline(self) -> None:
        self.__line += 1

    def error_index(self) -> int:
        return self.__error_index

    def no_error_index(self) -> int:
        return self.__no_error_index

    def tokenize(self, text: str) -> int:
        self.__token_list: List[RegularToken] = []
        self.__error_index: int = self.__no_error_index
        self.__line: int = 1
        state: int = self.__start_state
        token_string: str = ''
        index: int = 0
        while index < len(text):
            character: str = text[index]
            index += 1
            get_token: bool = False
            if state in self.__non_greedy_state_set:
                get_token: bool = True
            if not get_token and state in self.__compact_move_table:
                for attribute, character_set, range_list, next_state in self.__compact_move_table[state]:
                    if attribute == 2:
                        condition: bool = character not in character_set
                        for min_character, max_character in range_list:
                            condition &= character < min_character or character > max_character
                    else:
                        condition: bool = character in character_set
                        if attribute == 1 and character not in self.__character_set:
                            condition: bool = True
                        for min_character, max_character in range_list:
                            if condition or min_character <= character <= max_character:
                                condition: bool = True
                                break
                    if condition:
                        token_string += character
                        state: int = next_state
                        break
                else:
                    if state in self.__end_state_set:
                        get_token: bool = True
                    else:
                        self.__error_index: int = index - 1
                        return self.__error_index
            else:
                if get_token or state in self.__end_state_set:
                    get_token: bool = True
                else:
                    self.__error_index: int = index - 1
                    return self.__error_index
            if get_token:
                self._generate_token(state, token_string)
                token_string: str = ''
                state: int = self.__start_state
                index -= 1
        if state in self.__end_state_set:
            self._generate_token(state, token_string)
        else:
            self.__error_index: int = index - 1
            return self.__error_index
        self.__token_list.append(RegularToken('', self.__line, '$'))
        return self.__error_index

    def register_function(self, function_name: str) -> callable:
        def decorator(f: callable):
            self.__lexical_function[function_name] = f
            return f
        return decorator


import requests

from secretsanta.celery import app
from secretsanta.settings import TELEGRAM_URL, TELEGRAM_BOT_TOKEN

def rate_limit(task, task_group):
    # берем соединение с брокером из пула
    with task.app.connection_for_read() as conn:
        # забираем токен
        msg = conn.default_channel.basic_get(task_group+'_tokens', no_ack=True)
        # получили None - очередь пуста, токенов нет
        if msg is None:
            # повторить таску через 1 сек
            task.retry(countdown=1)

@app.task(bind=True, queue='messages', max_retries=None)
def send_message(self, text, chat_id, reply_markup=None):
    rate_limit(self, 'messages')
    data = {
        "chat_id": chat_id,
        "text": text,
        "reply_markup": reply_markup,
        "parse_mode": "Markdown"
    }
    response = requests.post(f"{TELEGRAM_URL}/bot{TELEGRAM_BOT_TOKEN}/sendMessage", data=data)
    print(response.content)

@app.task(bind=True, queue='messages', max_retries=None)
def edit_message(self, text, chat_id, message_id, reply_markup=None):
    rate_limit(self, 'messages')
    data = {
        "chat_id": chat_id,
        "text": text,
        "message_id": message_id,
        "reply_markup": reply_markup,
        "parse_mode": "Markdown"
    }
    response = requests.post(f"{TELEGRAM_URL}/bot{TELEGRAM_BOT_TOKEN}/editMessageText", data=data)
    print(response.content)

@app.task(bind=True, queue='messages', max_retries=None)
def mailing(self, answer, recipients):
    for recipient in recipients:
        send_message.delay(answer, recipient)
    print(f"Рассылка произведена по {len(recipients)} чатам")


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

urlpatterns = [
    url(r'^violation/?', views.violation),
    url(r'^test/?', views.nginx_test),
    url(r'^login/?', views_old.login_service),
    url(r'^IllegalData-search/login/?', views_old.login_service),
    url(r'^illegal/?', views_old.violation_service),
    url(r'^IllegalData-search/vehicle/?', views_old.violation_service),
    url(r'^register/?', views_old.register_service),
]


'''
This program counts the thursdays in an given month and outputs the number to a user

'''

#allows me to run it from the comand line
import sys
#allows python to recognize dates
import datetime

def main(year, month):
    try:
        # dictonary indicates how many days there are in each month
        dict = {1: 31, 2: 28, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31}
        day = 1
        thurs_count = 0
        #while loop that counts thursdays each month
        while day <= dict[int(month)]:
            weekday = datetime.date(year, month, day).weekday()
            if weekday == 3:
                thurs_count += 1
            day += 1
        year = str(year)
        month = str(month)
        thurs_count = str(thurs_count)
        output = (thurs_count, month, year)
        output = str(output)
        #writes the number of thursdays to a output file
        with open('month_length.txt', 'w') as file:
            file.write(output)
    except:
        print("your year or month values were not valid, please try again")

# declares the main function
if __name__ == "__main__":
    main(int(sys.argv[1]),int(sys.argv[2]))


#!/usr/bin/python3
import os
import sys
import argparse
import yaml
import json
import jsonschema
from jsonschema import validate

def main(json_schema, yaml_mapping):
    """
    Verifies a yaml mapping file against a json schema.
    """
    schema = read_json(json_schema)

    mapping = read_yaml(yaml_mapping)

    try:
        validate(mapping, schema)
        print("file validates okay")
    except jsonschema.exceptions.ValidationError as ve:
        exit(ve)

def read_yaml(yaml_file):
    with open(yaml_file, 'r') as stream:
        try:
            yaml_data = yaml.load(stream)
            return yaml_data
        except yaml.YAMLError as exc:
            sys.exit(exc)

def read_json(json_file):
    with open(json_file, 'r') as stream:
        json_data = json.load(stream)
        return json_data

def is_valid_file(arg):
    if os.path.isfile(arg):
        return arg
    else:
        error = "error: The file %s does not exist" % arg
        sys.exit(error)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="Verifies a yaml mapping against a RELAX NG schema")

    parser.add_argument('schema',
                        help="jason-schema schema file",
                        metavar="SCHEMA",
                        type=lambda x: is_valid_file(x))
    parser.add_argument('mapping',
                        help="YAML file to be verified",
                        type=lambda x: is_valid_file(x))
    args = parser.parse_args()

    schema = args.schema
    mapping = args.mapping
    main(schema, mapping)