def addLog(self, txt):
     """
     Append log to the logsEdit widget
     """
     Logger.instance().debug(txt)
     self.logsEdit.insertHtml(
         "%s - %s<br />" %
         (self.formatTimestamp(time.time()), unicode(self.strip_html(txt))))
     self.autoScrollOnTextEdit()
 def addLogError(self, txt):
     """
     Add log error
     """
     Logger.instance().error(txt)
     self.logsEdit.insertHtml(
         "<span style='color:red'>%s - %s</span><br />" %
         (self.formatTimestamp(time.time()), self.strip_html(txt)))
     self.autoScrollOnTextEdit()
 def addLogWarning(self, txt):
     """
     Add log warning
     """
     Logger.instance().info(txt)
     self.logsEdit.insertHtml(
         "<span style='color:darkorange'>%s - %s</span><br />" %
         (self.formatTimestamp(time.time()), self.strip_html(txt)))
     self.autoScrollOnTextEdit()
    def sendMessage(self, cmd, data='', more={}):
        """
        Send a message to the client
        """
        inData = False
        msg = {'cmd': cmd}

        # save data to temp area
        idFile = ''

        if len(data):
            try:
                idFile = "%s" % uuid.uuid4()
                pluginDataFile = '%s/%s' % (self.clientTmpPath, idFile)
                with open(pluginDataFile, mode='w') as myfile:
                    myfile.write(json.dumps(data))
                inData = True
            except Exception as e:
                print("unable to write data file from plugin: %s" % e)
                Logger.instance().error(
                    "unable to write data file from plugin: %s" % e)
                self.debug().addLogError("internal error on send message")

        msg.update({'in-data': inData})

        # add data parameters
        if inData: msg.update({'data-id': idFile})

        # add more params
        msg.update(more)

        # adding reference
        msg.update({
            'id': self.pluginName,
            'name': self.pluginName,
            'type': self.pluginType
        })
        Logger.instance().debug("%s" % msg)

        # encode message and send to stdout
        datagram = json.dumps(msg)

        datagramEncoded = base64.b64encode(bytes(datagram, "utf8"))
        print(str(datagramEncoded, "utf8"))
        sys.stdout.flush()
    def prepare(self):
        """
        """
        Logger.instance().debug("preparing plugin")

        # convert qicon to base64
        pixmapIcon = self.pluginIcon.pixmap(64,
                                            64,
                                            mode=QIcon.Normal,
                                            state=QIcon.Off)

        byteArray = QByteArray()
        buffer = QBuffer(byteArray)
        buffer.open(QIODevice.WriteOnly)
        pixmapIcon.save(buffer, "png", quality=100)

        iconBase64 = byteArray.toBase64().data()

        Logger.instance().debug("icon converted to base64")

        self.sendMessage(cmd='configure',
                         data=str(iconBase64, 'utf8'),
                         more={"description": self.pluginDescription})
    def onPluginMessage(self, msg):
        """
        On message received from the client
        """
        try:
            if not self.isRegistered and msg['cmd'] == 'registered':
                Logger.instance().debug("plugin registered")
                self.clientTmpPath = msg['tmp-path']
                self.debugPage.addLogSuccess(
                    txt="plugin registered [Type=%s]" % self.pluginType)
                self.isRegistered = True
                self.prepare()

            elif msg['cmd'] == 'keepalive':
                self.aliveClient.lastTimestamp = time.time()
                Logger.instance().debug("keepalive received")

            elif msg['cmd'] == 'run':
                data = None

                #read data
                if msg['in-data']:
                    # read the file
                    f = open("%s/%s" % (self.clientTmpPath, msg['data-id']),
                             'r')
                    all = f.read()
                    f.close()

                    dataJson = json.loads(all)

                    # delete them
                    os.remove("%s/%s" % (self.clientTmpPath, msg['data-id']))

                self.__onPluginRun(data=dataJson)

            elif msg['cmd'] == 'open-main':
                if self.mainPage is None:
                    return

                if self.TAB_MAIN_PAGE is not None:
                    self.mainTab.setCurrentIndex(self.TAB_MAIN_PAGE)

                self.__showWindows()

                if msg['in-data']:
                    # read the file
                    f = open("%s/%s" % (self.clientTmpPath, msg['data-id']),
                             'r')
                    all = f.read()
                    f.close()

                    dataJson = json.loads(all)

                    self.mainPage.insertData(data=dataJson)

                    # delete them
                    os.remove("%s/%s" % (self.clientTmpPath, msg['data-id']))

            else:
                pass
        except Exception as e:
            self.debugPage.addLogError(txt="error: %s" % e)
            self.mainTab.setCurrentIndex(TAB_DEBUG_PAGE)

            self.__showWindows()