コード例 #1
0
    def getProjects(self, delegate, **kwargs):
        if tools.supports(delegate, "handleProjects") == False:
            raise Exception(
                "the delegate must implement the handleProjects function")

        url = self.url

        opts = tools.Options(**kwargs)

        if opts.hasOpt("namespace"):
            url += "/namespaces/" + opts.getOpt("namespace")

        url += "/espservers"

        if opts.hasOpt("name"):
            url += "/" + opts.getOpt("name")

        response = requests.get(url)

        if response.status_code == 404:
            if tools.supports(delegate, "notFound"):
                delegate.notFound()
            else:
                raise Exception("error: not found")
            return

        data = response.json()

        if "code" in data and data["code"] == 404:
            s = ""
            if self._ns != None:
                s += self._ns + "/"
            s += self._project

            if tools.supports(delegate, "notFound"):
                delegate.notFound(s)
            else:
                raise Exception("project not found: " + s)
        else:
            kind = data["kind"]
            a = None

            if kind == "ESPServer":
                a = [data]
            elif kind == "ESPServerList":
                a = data["items"]

            delegate.handleProjects(a)
コード例 #2
0
            def handlePod(self, pod):
                url = self._k8s.baseUrl
                url += "api/v1"
                url += "/namespaces/" + self._k8s.namespace
                url += "/pods/" + pod["metadata"]["name"]
                url += "/log"

                response = requests.get(url)

                if (response.status_code == 200):
                    log = []
                    o = None

                    for line in response.text.split("\n"):
                        if len(line) > 0:
                            try:
                                o = json.loads(line)
                            except:
                                o = {}
                                o["message"] = line
                            log.append(o)

                    delegate.handleLog(log)
                else:
                    if tools.supports(delegate, "error"):
                        delegate.error(request, error)
                    else:
                        raise Exception("error: " + error)
コード例 #3
0
    def addDelegate(self, delegate):
        if tools.supports(delegate, "handleLog") == False:
            raise Exception("the delegate must implement the handleLog method")

        if tools.addTo(self._delegates, delegate):
            if len(self._delegates) == 1:
                self.start()
コード例 #4
0
    def message(self, message):
        if (self.isHandshakeComplete):
            return

        name = ""
        value = None

        for i in range(0, len(message)):
            c = message[i]
            if (c == '\n'):
                if (len(name) == 0):
                    break
                if (self._headers == None):
                    self._headers = {}
                if (value != None):
                    self._headers[name] = value.strip()
                else:
                    self._headers[name] = ""

                name = ""
                value = None
            elif (value != None):
                value += c
            elif (c == ':'):
                value = ""
            else:
                name += c

        status = self.getHeader("status")

        if (status != None):
            value = int(status)
            if (value == 200):
                self._handshakeComplete = True
                self.handshakeComplete()

                if (tools.supports(self._delegate, "connected")):
                    self._delegate.connected(self)
            elif (value == 401):
                if (self._authorization != None):
                    self._websocket.send(self._authorization)
                elif (tools.supports(self._delegate, "authenticate")):
                    scheme = self.getHeader("www-authenticate")
                    self._delegate.authenticate(self, scheme)
コード例 #5
0
    def closed(self):
        for c in self._datasources.values():
            c.clear()

        if tools.supports(self._delegate, "closed"):
            self._delegate.closed(self)

        if self._autoReconnect:
            thread = threading.Thread(target=self.reconnect)
            thread.daemon = True
            thread.start()
コード例 #6
0
    def run(self):
        while self._running:

            if self._paused:
                while True:
                    time.sleep(1)
                    if self._paused == False:
                        break

            current = datetime.datetime.now()
            eventsources = []
            depsPending = False
            minInterval = 5000
            completed = 0

            for es in self._eventsources.values():
                if es.repeat >= 0 and es.done == False:
                    if es.sending == False:
                        if es.checkDependencies():
                            diff = current.timestamp() - es.timestamp
                            interval = es.interval

                            if diff > interval:
                                if interval < minInterval:
                                    minInterval = interval
                                eventsources.append(es)
                            else:
                                diff = current.getTime(
                                ) - es.timestamp + interval

                                if diff < minInterval:
                                    minInterval = diff
                        else:
                            depsPending = True
                else:
                    completed += 1

            for es in eventsources:
                if es.repeat >= 0:
                    es.process()

            if completed == len(self._eventsources.values()):
                self._running = False
            else:
                if depsPending:
                    interval = 1
                else:
                    interval = minInterval / 1000

                time.sleep(interval)

        if tools.supports(self._delegate, "complete"):
            threading.Timer(1, self._delegate.complete(self)).start()
コード例 #7
0
    def handshakeComplete(self):

        for c in self._datasources.values():
            c.open()

        for p in self._publishers.values():
            p.open()

        if len(self._log._delegates) > 0:
            self._log.start()

        if len(self._stats._delegates) > 0:
            self._stats.set()

        if tools.supports(self._delegate, "connected"):
            self._delegate.connected(self)
コード例 #8
0
    def loadModel(self, delegate):
        if tools.supports(delegate, "modelLoaded") == False:
            raise Exception(
                "The stats delegate must implement the modelLoaded method")

        id = tools.guid()
        self._modelDelegates[id] = ModelDelegate(self, delegate)

        o = {}
        o["request"] = "model"
        o["id"] = id
        o["schema"] = True
        o["index"] = True
        o["xml"] = True

        self.send(o)
コード例 #9
0
    def getNamespaces(self,delegate):
        if tools.supports(delegate,"handleNamespaces") == False:
            raise Exception("the delegate must implement the handleNamespaces function")

        url = self.baseUrl
        url += "api/v1/namespaces"

        response = requests.get(url)

        a = []

        if response.status_code == 200:
            data = response.json()
            a = data["items"]

        delegate.handleNamespaces(a)
コード例 #10
0
    def getLog(self, delegate):
        if tools.supports(delegate, "handleLog") == False:
            raise Exception(
                "the delegate must implement the handleLog function")

        if self.namespace == None or self.project == None:
            raise Exception(
                "the instance requires both namespace and project name to get the pod"
            )

        class Tmp(object):
            def __init__(self, k8s):
                self._k8s = k8s

            def handlePod(self, pod):
                url = self._k8s.baseUrl
                url += "api/v1"
                url += "/namespaces/" + self._k8s.namespace
                url += "/pods/" + pod["metadata"]["name"]
                url += "/log"

                response = requests.get(url)

                if (response.status_code == 200):
                    log = []
                    o = None

                    for line in response.text.split("\n"):
                        if len(line) > 0:
                            try:
                                o = json.loads(line)
                            except:
                                o = {}
                                o["message"] = line
                            log.append(o)

                    delegate.handleLog(log)
                else:
                    if tools.supports(delegate, "error"):
                        delegate.error(request, error)
                    else:
                        raise Exception("error: " + error)

        self.getPod(Tmp(self))
コード例 #11
0
 def notFound(self):
     if tools.supports(delegate, "notFound"):
         delegate.notFound()
     else:
         raise Exception("error: not found")
コード例 #12
0
 def deliverInfoChange(self):
     for d in self._delegates:
         if tools.supports(d, "infoChanged"):
             d.infoChanged(self)
コード例 #13
0
    def addDelegate(self, delegate):
        if tools.supports(delegate, "dataChanged") == False:
            raise Exception(
                "the delegate must implement the dataChanged method")

        tools.addTo(self._delegates, delegate)
コード例 #14
0
 def setSchema(self, xml):
     self._schema.fromXml(xml)
     for d in self._delegates:
         if tools.supports(d, "schemaSet"):
             d.schemaSet(self)
コード例 #15
0
    def deliver(self, xml):
        model = Model(xml)

        if tools.supports(self._delegate, "modelLoaded"):
            self._delegate.modelLoaded(model, self._connection)