Ejemplo n.º 1
0
    def sendReply(self, action, *args, **kwargs):

        try:

            if hasattr(self, "socket"):

                reply = QByteArray()
                stream = QDataStream(reply, QIODevice.WriteOnly)
                stream.setVersion(QDataStream.Qt_4_2)
                stream.writeUInt32(0)

                stream.writeQString(action)

                for arg in args:
                    if isinstance(arg, int):
                        stream.writeInt(int(arg))
                    elif isinstance(arg, str):
                        stream.writeQString(arg)
                    elif isinstance(arg, list):
                        stream.writeQString(str(arg))

                #stream << action << options
                stream.device().seek(0)

                stream.writeUInt32(reply.size() - 4)

                self.socket.write(reply)

#            else :
#                # no socket !?
#                self.quit()

        except:
            self.log.exception("Something awful happened when sending reply !")
Ejemplo n.º 2
0
    def testIt(self):
        w = QGLWidget()
        w.makeCurrent()

        b = QGLBuffer()
        b.setUsagePattern(QGLBuffer.DynamicDraw)

        self.assertTrue(b.create())
        self.assertTrue(b.bufferId() != 0)
        self.assertTrue(b.bind())

        data = QByteArray("12345")
        b.allocate(data)
        self.assertEqual(b.size(), data.size())

        m = b.map(QGLBuffer.ReadOnly)
        if m:
            self.assertEqual(m, py3k.buffer(py3k.b(data.data())))
            b.unmap()

            m = b.map(QGLBuffer.ReadWrite)
            m[3] = py3k.b('A')
            b.unmap()
            result, rdata = b.read(3, 1)
            self.assertTrue(result)
            self.assertEqual(py3k.b('A'), rdata.data())
        else:
            print(" memory mapping is not possible in this OpenGL implementation.")
        b.release()
Ejemplo n.º 3
0
    def preparePacket(self, action, *args, **kwargs):

        reply = QByteArray()
        stream = QDataStream(reply, QIODevice.WriteOnly)
        stream.setVersion(QDataStream.Qt_4_2)
        stream.writeUInt32(0)

        stream.writeQString(action)

        for arg in args:
            if type(arg) is LongType:
                stream.writeQString(str(arg))
            elif type(arg) is IntType:
                stream.writeInt(arg)
            elif isinstance(arg, basestring):
                stream.writeQString(arg)
            elif type(arg) is StringType:
                stream.writeQString(arg)
            elif type(arg) is FloatType:
                stream.writeFloat(arg)
            elif type(arg) is ListType:
                stream.writeQString(str(arg))

        stream.device().seek(0)

        stream.writeUInt32(reply.size() - 4)

        return reply
Ejemplo n.º 4
0
    def preparePacket(self, action, *args, **kwargs) :

        reply = QByteArray()
        stream = QDataStream(reply, QIODevice.WriteOnly)
        stream.setVersion(QDataStream.Qt_4_2)
        stream.writeUInt32(0)
        
        stream.writeQString(action)
        
        for arg in args :
            if type(arg) is LongType :
                stream.writeQString(str(arg))
            elif type(arg) is IntType:
                stream.writeInt(arg)
            elif isinstance(arg, basestring):                       
                stream.writeQString(arg)                  
            elif type(arg) is StringType  :
                stream.writeQString(arg)
            elif type(arg) is FloatType:
                stream.writeFloat(arg)
            elif type(arg) is ListType:
                stream.writeQString(str(arg))
        
        stream.device().seek(0)
        
        stream.writeUInt32(reply.size() - 4)  

        return reply  
Ejemplo n.º 5
0
    def testIt(self):
        w = QGLWidget()
        w.makeCurrent()

        b = QGLBuffer()
        b.setUsagePattern(QGLBuffer.DynamicDraw)

        self.assertTrue(b.create())
        self.assertTrue(b.bufferId() != 0)
        self.assertTrue(b.bind())

        data = QByteArray("12345")
        b.allocate(data)
        self.assertEqual(b.size(), data.size())

        m = b.map(QGLBuffer.ReadOnly)
        if m:
            self.assertEqual(m, py3k.buffer(py3k.b(data.data())))
            b.unmap()

            m = b.map(QGLBuffer.ReadWrite)
            m[3] = py3k.b('A')
            b.unmap()
            result, rdata = b.read(3, 1)
            self.assertTrue(result)
            self.assertEqual(py3k.b('A'), rdata.data())
        else:
            print(
                " memory mapping is not possible in this OpenGL implementation."
            )
        b.release()
Ejemplo n.º 6
0
    def testQByteArrayValid(self):
        '''QDataStream <<>> QByteArray - valid'''
        self.stream << QByteArray("hello")

        res = QByteArray()

        self.read_stream >> res
        self.assertEqual(res, QByteArray("hello"))
Ejemplo n.º 7
0
    def testRawData(self):
        data = QDataStream()
        self.assertEqual(data.readRawData(4), None)

        ba = QByteArray()
        data = QDataStream(ba, QIODevice.WriteOnly)
        data.writeRawData('AB\x00C')
        self.assertEqual(ba.data(), py3k.b('AB\x00C'))

        data = QDataStream(ba)
        self.assertEqual(data.readRawData(4), py3k.b('AB\x00C'))
Ejemplo n.º 8
0
    def from_hash(cls, hash):
        u = User(hash['identifier'], hash['name'])
        u.image = QImage.fromData(QByteArray(
            hash['picture_bytes'].data)).scaledToHeight(128)
        #u.image = None
        return u

        f = open(hash['picture_bytes'], "rb")
        bytes = f.read(-1)
        f.close()
        u.image = QImage.fromData(QByteArray(bytes)).scaledToHeight(256)

        return u
Ejemplo n.º 9
0
    def image(self,pixmap):
        if pixmap == None:
            self._image_as_pixmap = None
            self.picture_data = None
        else:

            byte_array = QByteArray()
            qbuffer = QBuffer(byte_array)
            qbuffer.open(QIODevice.WriteOnly)
            pixmap.toImage().save(qbuffer, "PNG")

            self._image_as_pixmap = None
            self.picture_data = memoryview(byte_array.data()) # memory view to support python 2 and 3
Ejemplo n.º 10
0
 def testSetNum(self):
     b = QByteArray()
     b.setNum(py3k.long(-124124))
     self.assertEqual(b, "-124124")
     b = QByteArray()
     b.setNum(-124124)
     self.assertEqual(b, "-124124")
     b = QByteArray()
     b.setNum(-0.5)
     self.assertEqual(b, "-0.5")
Ejemplo n.º 11
0
    def setUp(self):
        self.dom = QDomDocument()

        self.goodXmlData = QByteArray('''
        <typesystem package="PySide.QtXml">
            <value-type name="QDomDocument"/>
            <value-type name="QDomElement"/>
        </typesystem>
        ''')

        self.badXmlData = QByteArray('''
        <typesystem package="PySide.QtXml">
            <value-type name="QDomDocument">
        </typesystem>
        ''')
Ejemplo n.º 12
0
    def testInRange(self):
        #QByteArray[x] where x is a valid index
        string = 'abcdefgh'
        obj = QByteArray(string)

        for i in range(len(string)):
            self.assertEqual(obj[i], py3k.b(string[i]))
Ejemplo n.º 13
0
    def testInRangeReverse(self):
        #QByteArray[x] where x is a valid index (reverse order)
        string = 'abcdefgh'
        obj = QByteArray(string)

        for i in range(len(string) - 1, 0, -1):
            self.assertEqual(obj[i], py3k.b(string[i]))
Ejemplo n.º 14
0
 def test_connection(self):
     self.message.setText(
         '<span style="font-size: 10px; color: #000000;">' +
         'Verbindung wird getestet.' + '</span>')
     self.nam.finished.connect(self.test_reply)
     self.nam.sslErrors.connect(self.ssl_errors)
     ssl_config = QSslConfiguration().defaultConfiguration()
     ssl_config.setCiphers(QSslSocket().supportedCiphers())
     if self.certificate:
         certificate = QSslCertificate(encoded=self.certificate,
                                       format=QSsl.Pem)
         ssl_config.setCaCertificates([certificate])
     else:
         ssl_config.setCaCertificates([])
     url = QUrl(self.url_edit.text())
     url.setPath("/".join(
         filter(bool, (url.path() + "/ajax/read.php").split("/"))))
     request = QNetworkRequest(url)
     request.setSslConfiguration(ssl_config)
     request.setRawHeader(
         "Authorization", "Basic ".encode('utf-8') + b64encode(
             (self.username_edit.text() + ":" +
              self.password_edit.text()).encode('utf-8')))
     request.setHeader(QNetworkRequest.ContentTypeHeader,
                       "application/x-www-form-urlencoded")
     self.replies.add(self.nam.post(request, QByteArray()))
Ejemplo n.º 15
0
 def testIt(self):
     if not py3k.IS_PY3K:
         ba = QByteArray('1234567890')
         self.assertEqual(ba[2:4], '34')
         self.assertEqual(ba[:4], '1234')
         self.assertEqual(ba[4:], '567890')
         self.assertEqual(len(ba[4:1]), 0)
Ejemplo n.º 16
0
    def open(self, address, method='get', headers={}, auth=None, body=None,
             default_popup_response=None):
        """Opens a web page.

        :param address: The resource URL.
        :param method: The Http method.
        :param headers: An optional dict of extra request hearders.
        :param auth: An optional tuple of HTTP auth (username, password).
        :param body: An optional string containing a payload.
        :param default_popup_response: the default response for any confirm/
        alert/prompt popup from the Javascript (replaces the need for the with
        blocks)
        :return: Page resource, All loaded resources.
        """
        body = body or QByteArray()
        try:
            method = getattr(QNetworkAccessManager,
                             "%sOperation" % method.capitalize())
        except AttributeError:
            raise Error("Invalid http method %s" % method)
        request = QNetworkRequest(QUrl(address))
        request.CacheLoadControl(0)
        for header in headers:
            request.setRawHeader(header, headers[header])
        self._auth = auth
        self._auth_attempt = 0  # Avoids reccursion

        self.main_frame.load(request, method, body)
        self.loaded = False

        if default_popup_response is not None:
            Ghost._prompt_expected = (default_popup_response, None)
            Ghost._confirm_expected = (default_popup_response, None)

        return self.wait_for_page_loaded()
Ejemplo n.º 17
0
    def image(self):
        if self._image_as_pixmap is None and self.picture_data:
            # Caution ! QImage and stuff work only if QApplication has been initialized !
            image = QImage.fromData(QByteArray(self.picture_data))
            self._image_as_pixmap = QPixmap.fromImage(image.convertToFormat(QImage.Format_RGB16, Qt.MonoOnly).scaledToHeight(128))

        return self._image_as_pixmap
Ejemplo n.º 18
0
 def keyPressEvent(self, e):
     if e.matches(QKeySequence.Paste):
         clipboard = QApplication.clipboard()
         mimeData = clipboard.mimeData()
         if mimeData.hasImage():
             image = clipboard.image()
             byteArray = QByteArray()
             buf = QBuffer(byteArray)
             buf.open(QIODevice.WriteOnly)
             image.save(buf, "PNG")
             self.label.setText('Uploading')
             self.thread = NetThread(str(byteArray.toBase64()))
             self.thread.finished.connect(self.onThreadEnd)
             self.thread.start()
         else:
             self.label.setText('No picture in clipboard')
 def testConcatPythonStringAndQByteArray(self):
     #Test concatenation of a Python string with a QByteArray, in this order
     concat_python_string_add_qbytearray_worked = True
     qba = QByteArray('foo')
     result = 'bar\x00' + qba
     self.assertEqual(type(result), QByteArray)
     self.assertEqual(result, 'bar\x00foo')
 def setUp(self):
     super(QPixmapQDatastream, self).setUp()
     self.source_pixmap = QPixmap(100, 100)
     self.source_pixmap.fill(Qt.red)
     self.output_pixmap = QPixmap()
     self.buffer = QByteArray()
     self.read_stream = QDataStream(self.buffer, QIODevice.ReadOnly)
     self.write_stream = QDataStream(self.buffer, QIODevice.WriteOnly)
Ejemplo n.º 21
0
def qvariant_decode(data):
    byteArray = QByteArray.fromBase64(data)
    buffer = QBuffer(byteArray)
    buffer.open(QIODevice.ReadOnly)
    stream = QDataStream(buffer)
    result = stream.readQVariant()
    buffer.close()
    return result
Ejemplo n.º 22
0
def qvariant_decode(data):
    byteArray = QByteArray.fromBase64(data)
    buffer = QBuffer(byteArray)
    buffer.open(QIODevice.ReadOnly)
    stream = QDataStream(buffer)
    result = stream.readQVariant()
    buffer.close()
    return result
Ejemplo n.º 23
0
    def sendReply(self, action, *args, **kwargs):

        try:

            if hasattr(self, "socket"):

                reply = QByteArray()
                stream = QDataStream(reply, QIODevice.WriteOnly)
                stream.setVersion(QDataStream.Qt_4_2)
                stream.writeUInt32(0)

                stream.writeQString(action)

                for arg in args:
                    if type(arg) is LongType:
                        stream.writeQString(str(arg))
                    if type(arg) is IntType:
                        stream.writeInt(int(arg))
                    elif type(arg) is StringType:
                        stream.writeQString(arg)
                    elif isinstance(arg, basestring):
                        stream.writeQString(arg)
                    elif type(arg) is FloatType:
                        stream.writeFloat(arg)
                    elif type(arg) is ListType:
                        stream.writeQString(str(arg))
                    elif type(arg) is QFile:
                        arg.open(QIODevice.ReadOnly)
                        fileDatas = QByteArray(arg.readAll())
                        stream.writeInt32(fileDatas.size())
                        stream.writeRawData(fileDatas.data())
                        arg.close()
                # stream << action << options
                stream.device().seek(0)

                stream.writeUInt32(reply.size() - 4)

                self.socket.write(reply)

        #            else :
        #                # no socket !?
        #                self.quit()

        except:
            self.log.exception("Something awful happened when sending reply !")
Ejemplo n.º 24
0
def restoreWidgetGeo(widget, geoStr):
    if not geoStr or geoStr.count(',') != 1:
        return fixWidgetSizeOnHiDpi(widget)

    a, b = geoStr.split(',')
    success = widget.restoreGeometry(QByteArray.fromHex(a))
    geoRatio = float(b)
    if success and abs(scaleRatio - geoRatio) > 0.01:
        widget.resize(widget.size() / geoRatio * scaleRatio)
Ejemplo n.º 25
0
    def testCorruptData(self):
        '''QDataStream reading corrupt data'''
        data = []

        data.append((QByteArray('\x00\x00\x00\x01\x02'),
                     QDataStream.ReadCorruptData,
                     QBitArray()))

        self._check_bitarray(data)
Ejemplo n.º 26
0
Archivo: t1.py Proyecto: zjx4041739/dlp
    def perform(self, url, method='GET', post_data=dict()):
        request = self._make_request(url)

        if method == 'GET':
            self.web_view.load(request)
        else:
            encoded_data = self._urlencode_post_data(post_data)
            request.setRawHeader(
                'Content-Type',
                QByteArray('application/x-www-form-urlencoded'))
            self.web_view.load(request, QNetworkAccessManager.PostOperation,
                               encoded_data)
Ejemplo n.º 27
0
    def sendReply(self, action, *args, **kwargs):
        
        try:
            
            if hasattr(self, "socket"):

                reply = QByteArray()
                stream = QDataStream(reply, QIODevice.WriteOnly)
                stream.setVersion(QDataStream.Qt_4_2)
                stream.writeUInt32(0)
                
                stream.writeQString(action)

    
                for arg in args:
                    if isinstance(arg, int):
                        stream.writeInt(int(arg))
                    elif isinstance(arg, str):                       
                        stream.writeQString(arg) 
                    elif isinstance(arg, list):
                        stream.writeQString(str(arg))                        

                #stream << action << options
                stream.device().seek(0)
                
                stream.writeUInt32(reply.size() - 4)

                
                self.socket.write(reply)



#            else :
#                # no socket !?
#                self.quit()

        except:
                self.log.exception("Something awful happened when sending reply !")  
Ejemplo n.º 28
0
def getIcon(obj, disabled=False, path=None):
    if not path:
        path = iconPath
    if not getattr(obj, '_icon', None):
        obj._icon = addIconToFCAD(obj._iconName, path)
    if not disabled:
        return obj._icon
    if not getattr(obj, '_iconDisabled', None):
        name = getattr(obj, '_iconDisabledName', None)
        if name:
            obj._iconDisabled = addIconToFCAD(name, path)
        else:
            key = os.path.join(path, obj._iconName) + '.disabled'
            fmt = None
            try:
                if FreeCADGui.isIconCached(key):
                    obj._iconDisabled = key
                    return key
                else:
                    fmt = 'PNG'
            except Exception:
                pass
            pixmap = FreeCADGui.getIcon(obj._icon).pixmap(*iconSize,
                                                          mode=QIcon.Disabled)
            icon = QIcon(pixmapDisabled)
            icon.paint(QPainter(pixmap), 0, 0, iconSize[0], iconSize[1],
                       Qt.AlignCenter)
            data = QByteArray()
            buf = QBuffer(data)
            buf.open(QIODevice.WriteOnly)
            if fmt:
                pixmap.save(buf, fmt)
                FreeCADGui.addIcon(key, data.data(), fmt)
            else:
                pixmap.save(buf, 'XPM')
                key = data.data().decode('latin1')
            obj._iconDisabled = key
    return obj._iconDisabled
Ejemplo n.º 29
0
    def sendReply(self, action, *args, **kwargs) :

        if self in self.parent.recorders :

    
            reply = QByteArray()
            stream = QDataStream(reply, QIODevice.WriteOnly)
            stream.setVersion(QDataStream.Qt_4_2)
            stream.writeUInt32(0)
            
            stream.writeQString(action)
            
            for arg in args :
                if type(arg) is LongType :
                    stream.writeQString(str(arg))
                elif type(arg) is IntType:
                    stream.writeInt(arg)
                elif isinstance(arg, basestring):                       
                    stream.writeQString(arg)                  
                elif type(arg) is StringType  :
                    stream.writeQString(arg)
                elif type(arg) is FloatType:
                    stream.writeFloat(arg)
                elif type(arg) is ListType:
                    stream.writeQString(str(arg))

            stream.device().seek(0)
            
            stream.writeUInt32(reply.size() - 4)
            
            if self.socket.isValid() and self.socket.state() == 3 :
                
                if self.socket.write(reply) == -1 :
                    self.log.debug("error socket write")
                    self.socket.abort()
            else :
                self.log.debug("send reply - incorrect socket to write")
                self.socket.abort()
Ejemplo n.º 30
0
 def mapBytes(self, kode):
     im = self.draw(kode)
     if isinstance(kode, UKode):
         qxm = pilfunc.pixmap2qt(im)
     elif isinstance(kode, (BKode, SKode)):
         qxm = pilfunc.bitmap2qt(im)
     else:
         raise ValueError
     bts = QByteArray()
     buffer = QBuffer(bts)
     buffer.open(QIODevice.WriteOnly)
     qxm.save(buffer, "png")
     buffer.close()
     return bts
Ejemplo n.º 31
0
    def __init__(self, file_path, qimage=None):
        file_name = os.path.basename(file_path)
        self.src_file_path = file_path
        super(Image, self).__init__(name=file_name)
        self.index = len(Image.instances)
        Image.instances.append(self)
        base, ext = os.path.splitext(file_path)
        mime_suffix = ext.lower()[1:]
        if mime_suffix == 'jpg':
            mime_suffix = 'jpeg'
        self.mime_type = 'image/{}'.format(mime_suffix)

        # Need to write this out temporarily or permanently
        # depending on resource_format
        if qimage:
            writer = QImageWriter(
                file_path,
                QByteArray(bytes(str(mime_suffix).encode("latin-1"))))
            writer.write(qimage)
            # delete the write to close file handle
            del writer

        if ExportSettings.resource_format == ResourceFormats.SOURCE:
            if not qimage:
                shutil.copy(file_path, ExportSettings.out_dir)
            self.uri = file_name
        else:
            with open(file_path, 'rb') as f:
                img_bytes = f.read()

            # Remove the temp qimage because resource_format isn't source
            if qimage:
                os.remove(file_path)

            if (ExportSettings.resource_format == ResourceFormats.BIN
                    or ExportSettings.file_format == 'glb'):
                single_buffer = Buffer.instances[0]
                buffer_end = len(single_buffer)
                single_buffer.byte_str += img_bytes
                self.buffer_view = BufferView(single_buffer, buffer_end)

                # 4-byte-aligned
                aligned_len = (len(img_bytes) + 3) & ~3
                for i in range(aligned_len - len(img_bytes)):
                    single_buffer.byte_str += b'0'

        if (ExportSettings.file_format == 'gltf' and
                ExportSettings.resource_format == ResourceFormats.EMBEDDED):
            self.uri = "data:application/octet-stream;base64," + base64.b64encode(
                img_bytes).decode("latin-1")
Ejemplo n.º 32
0
    def sendReply(self, action, *args, **kwargs):

        if self in self.parent.recorders:

            reply = QByteArray()
            stream = QDataStream(reply, QIODevice.WriteOnly)
            stream.setVersion(QDataStream.Qt_4_2)
            stream.writeUInt32(0)

            stream.writeQString(action)

            for arg in args:
                if type(arg) is LongType:
                    stream.writeQString(str(arg))
                elif type(arg) is IntType:
                    stream.writeInt(arg)
                elif isinstance(arg, basestring):
                    stream.writeQString(arg)
                elif type(arg) is StringType:
                    stream.writeQString(arg)
                elif type(arg) is FloatType:
                    stream.writeFloat(arg)
                elif type(arg) is ListType:
                    stream.writeQString(str(arg))

            stream.device().seek(0)

            stream.writeUInt32(reply.size() - 4)

            if self.socket.isValid() and self.socket.state() == 3:

                if self.socket.write(reply) == -1:
                    self.log.debug("error socket write")
                    self.socket.abort()
            else:
                self.log.debug("send reply - incorrect socket to write")
                self.socket.abort()
Ejemplo n.º 33
0
    def testQByteArrayNull(self):
        '''QDataStream <<>> QByteArray - null'''
        self.stream << QByteArray()

        res = QByteArray()

        self.read_stream >> res
        self.assertEqual(res, QByteArray())
        self.assertTrue(res.isEmpty())
        self.assertTrue(res.isNull())
Ejemplo n.º 34
0
    def testQByteArrayEmpty(self):
        '''QDataStream <<>> QByteArray - empty'''
        self.stream << QByteArray("")

        res = QByteArray()

        self.read_stream >> res
        self.assertEqual(res, QByteArray(""))
        self.assertTrue(res.isEmpty())
        self.assertFalse(res.isNull())
Ejemplo n.º 35
0
    def testLatin1(self):
        '''QTextStream readLine for simple Latin1 strings'''

        data = []

        data.append((QByteArray(), []))
        data.append((QByteArray('\n'), ['']))
        data.append((QByteArray('\r\n'), ['']))
        data.append((QByteArray('ole'), ['ole']))
        data.append((QByteArray('ole\n'), ['ole']))
        data.append((QByteArray('ole\r\n'), ['ole']))
        data.append((QByteArray('ole\r\ndole\r\ndoffen'), ['ole', 'dole', 'doffen']))

        self._check_data(data)
Ejemplo n.º 36
0
    def __init__(self, devices=None, filename=None, parent=None):
        QObject.__init__(self, parent)

        self.pullTimer = QTimer(self)
        self.buf = QByteArray()
        self.devices = devices
        self.device = QAudioDeviceInfo.defaultOutputDevice()
        self.generator = None
        self.audioOutput = None
        self.output = None
        self.fmt = QAudioFormat()
        self.pullMode = False
        self.dump = filename
        self._label = SUSPEND_LABEL

        self.initializeAudio()
Ejemplo n.º 37
0
    def __init__(self, url, title=None, geometry=None, icon=None):
        super(Browser, self).__init__()
        
        self.setGeometry(*geometry)
        self.setWindowTitle(title)
        
        if icon:
            pixmap = QtGui.QPixmap()
            if type(icon) == tuple:  # package, not filepath
                img_data = pkgutil.get_data(*icon)
            else:
                with open(icon) as fh:
                    img_data = fh.read()
            pixmap.loadFromData(QByteArray(img_data))
            self.setWindowIcon(QtGui.QIcon(pixmap))

        self.load(QUrl(url))
Ejemplo n.º 38
0
    def new_picture(self):
        dialog = QFileDialog()
        if (dialog.exec_()):
            filename = dialog.selectedFiles()[0]
            # print "ok {}".format(filename)

            f = open(filename,"rb")
            bytes = f.read(-1)
            f.close()

            image = QImage.fromData(QByteArray(bytes))

            if image:
                # save as buffer(bytes)

                pixmap = QPixmap.fromImage(image.convertToFormat(QImage.Format_RGB16, Qt.MonoOnly).scaledToHeight(128))
                self.set_pixmap_as_icon(pixmap)
Ejemplo n.º 39
0
 def takeSnapshot(self, path):
     '''
     screen capture in png format
     :param path: saved file path
     :return: None
     '''
     p1 = self.cmd.popen(['screencap', '-p'], stdout=PIPE)
     p = Popen(['perl', '-pe', 's/\x0D\x0D\x0A/\x0A/g'], stdin=p1.stdout, stdout=PIPE)
     out, error = p.communicate()
     ba = QByteArray.fromRawData(out)
     img = QImage.fromData(ba, 'PNG')
     orient = self.getCurrDisplay()['orientation']
     if orient == 1:
         img = img.transformed(QMatrix().rotate(-90))
     elif orient == 2:
         img = img.transformed(QMatrix().rotate(-180))
     elif orient == 3:
         img = img.transformed(QMatrix().rotate(-270))
     img.save(path, 'PNG')
Ejemplo n.º 40
0
 def testAppend(self):
     b = QByteArray()
     b.append("A")
     self.assertEqual(b.size(), 1)
     b.append("AB")
     self.assertEqual(b.size(), 3)
Ejemplo n.º 41
0
 def testIt(self):
     data = py3k.b("foobar")
     a = QByteArray.fromRawData(data)
     self.assertEqual(type(a), QByteArray)
     self.assertEqual(a.data(), data)
Ejemplo n.º 42
0
 def testToNumberFloat(self):
     obj = QByteArray('37.109')
     self.assertEqual((ctypes.c_float(37.109).value, True),
                      obj.toFloat())
Ejemplo n.º 43
0
 def testToNumberUShort(self):
     obj = QByteArray('37')
     self.assertEqual((37, True), obj.toUShort())
Ejemplo n.º 44
0
 def testDataWithZeros(self):
     s1 = "123\000321"
     ba = QByteArray(s1)
     s2 = ba.data()
     self.assertEqual(py3k.b(s1), s2)
     self.assertEqual(s1, ba)
Ejemplo n.º 45
0
 def testData(self):
     url = QByteArray("http://web.openbossa.org/")
     self.assertEqual(url.data(), py3k.b("http://web.openbossa.org/"))
Ejemplo n.º 46
0
 def loadIcon(self):
     bytearr = QByteArray.fromBase64(self.i)
     pixmap = QPixmap()
     pixmap.loadFromData(bytearr)
     return QIcon(pixmap)
Ejemplo n.º 47
0
 def testPathSeparator(self):
     #QByteArray.split('/')
     obj = QByteArray(unittest.__file__)
     self.assertEqual(obj.split('/'), unittest.__file__.split('/'))
Ejemplo n.º 48
0
 def testToNumberDouble(self):
     obj = QByteArray('37.109')
     self.assertEqual((ctypes.c_double(37.109).value, True),
                      obj.toDouble())
Ejemplo n.º 49
0
 def testNullStrings(self):
     ba = QByteArray('\x00')
     self.assertEqual(ba.at(0), '\x00')
     self.assertEqual(ba[0], py3k.b('\x00'))
Ejemplo n.º 50
0
def main(url, output, option={}):
    result = {
        "error":[],
        "page":{},
        "resources":[],
        "capture":None,
    }
    #savedir = appdir + "/artifacts/ghost/" +  output
    #dump = savedir +  "/ghost.pkl"
    savedir = os.path.join(appdir, "artifacts/ghost")
    dump = savedir +  "/" + output
    try:
    	#if os.path.exists(savedir):
        #    shutil.rmtree(savedir)
        #os.makedirs(savedir)
        with open(dump, 'wb') as d:
            #umsgpack.dump(result, d)
            json.dump(result, d)
    except Exception as e:
        logger.error(str(e))
        result["error"].append(str(e))

    defaults = {
        "wait_timeout":60,
        "display": False,
        "viewport_size": (800, 600),
        "plugins_enabled": True,
        "java_enabled": True,
    }
    proxy_url = None
    http_method = "GET"
    req_headers = None
    body = None
    if option:
        if "user_agent" in option:
            defaults["user_agent"] = str(option["user_agent"])
        if "timeout" in option:
            defaults["wait_timeout"] = int(option["timeout"])
        if "proxy" in option:
            proxy_url = option["proxy"]
        if "method" in option:
            http_method = option["method"] 
        if "headers" in option:
            req_headers = option["headers"]
        if "post_data" in option:
            body = str(option["post_data"])
    logger.info(defaults)

    ghost = None
    try:
        ghost = Ghost(
            #log_level=logging.DEBUG,
            log_level=logging.INFO,
            plugin_path=[ appdir + '/plugins', '/usr/lib/mozilla/plugins', ],
            defaults=defaults,
        )
    except Exception as e:
        logger.error("ghost init failed. " + str(e))
        result["error"].append(str(e))

    with ghost.start() as session:
        if proxy_url:
            try:
                type = proxy_url.split(":")[0]
                server = proxy_url.split("/")[2]
                host = server.split(":")[0]
                port = server.split(":")[1]
                session.set_proxy(str(type),host=str(host),port=int(port),)
            except Exception as e:
                logger.debug(e)

        headers = {}
        if req_headers:
            logger.debug(req_headers)
            for h in req_headers:
                headers[str(h)] = str(req_headers[h])
            logger.debug(headers)

        if hasattr(ghost, "xvfb"):
            logger.info(ghost.xvfb)

        page = None
        resources =None
        try:
            page, resources = session.open(
                url.decode("utf-8"),
                method = http_method,
                headers = headers,
                body = body
            )
        except Exception as e:
            logger.error(str(e))
            result["error"].append(str(e))

        #if error:
        #    result["error"] = error.spilt(".")[-1]
        if page:
            result["page"] = {
                "url":page.url,
                "http_status":page.http_status,
                "headers":page.headers,
                #"content":session.content.encode("utf-8"),
                "content":base64.b64encode(session.content.encode("utf-8")),
                "seq":0,
                #"error":page.error.encode("utf-8").split(".")[-1],
                "error":page.error.split(".")[-1],
            }
            try:
                image = session.capture()
                ba = QByteArray()
                buffer = QBuffer(ba)
                buffer.open(QIODevice.WriteOnly)
                image.save(buffer, "PNG")
                bio = BytesIO(ba)
                bio.seek(0)
                #result["capture"] = bio.read()
                result["capture"] = base64.b64encode(bio.read())
                bio.flush()
                bio.close()
                ba.clear()
                buffer.close()
            except Exception as e:
                logger.error(str(e))
                result["error"].append(str(e))
        if resources:
            seq = 0
            for r in resources:
                seq += 1
                #logger.debug(r.url)
                dict = {
                    "url":r.url,
                    "http_status":r.http_status,
                    "headers":r.headers,
                    #"content":r.content.encode("utf-8"),
                    "content":base64.b64encode(r.content),
                    "seq":seq,
                    #"error":r.error.encode("utf-8").split(".")[-1],
                    "error":r.error.split(".")[-1],
                }
                result["resources"].append(dict)
    with open(dump, 'wb') as d:
        #umsgpack.dump(result, d)
        json.dump(result, d)
        logger.debug(dump)
    ghost.exit()
    return dump