Example #1
0
 def saveQDataStream(self):
     error = None
     fh = None
     try:
         fh = QFile(self.__fname)
         if not fh.open(QIODevice.WriteOnly):
             raise IOError(str(fh.errorString()))
         stream = QDataStream(fh)
         stream.writeInt32(MovieContainer.MAGIC_NUMBER)
         stream.writeInt32(MovieContainer.FILE_VERSION)
         stream.setVersion(QDataStream.Qt_4_2)
         for key, movie in self.__movies:
             stream << movie.title
             stream.writeInt16(movie.year)
             stream.writeInt16(movie.minutes)
             stream << movie.acquired << movie.location \
                    << movie.notes
     except EnvironmentError as e:
         error = "Failed to save: {0}".format(e)
     finally:
         if fh is not None:
             fh.close()
         if error is not None:
             return False, error
         self.__dirty = False
         return True, "Saved {0} movie records to {1}".format(
             len(self.__movies),
             QFileInfo(self.__fname).fileName())
Example #2
0
 def open(self):
     self.offerSave()
     path = (QFileInfo(self.filename).path()
             if not self.filename.isEmpty() else ".")
     fname = QFileDialog.getOpenFileName(self,
             "Page Designer - Open", path,
             "Page Designer Files (*.pgd)")
     if fname.isEmpty():
         return
     self.filename = fname
     fh = None
     try:
         fh = QFile(self.filename)
         if not fh.open(QIODevice.ReadOnly):
             raise IOError, unicode(fh.errorString())
         items = self.scene.items()
         while items:
             item = items.pop()
             self.scene.removeItem(item)
             del item
         self.addBorders()
         stream = QDataStream(fh)
         stream.setVersion(QDataStream.Qt_4_2)
         magic = stream.readInt32()
         if magic != MagicNumber:
             raise IOError, "not a valid .pgd file"
         fileVersion = stream.readInt16()
         if fileVersion != FileVersion:
             raise IOError, "unrecognised .pgd file version"
         while not fh.atEnd():
             self.readItemFromStream(stream)
     except IOError, e:
         QMessageBox.warning(self, "Page Designer -- Open Error",
                 "Failed to open {0}: {1}".format(self.filename, e))
Example #3
0
 def save(self):
     exception = None
     fh = None
     try:
         if self.filename.isEmpty():
             raise IOError("no filename specified for saving")
         fh = QFile(self.filename)
         if not fh.open(QIODevice.WriteOnly):
             raise IOError(str(fh.errorString()))
         stream = QDataStream(fh)
         stream.writeInt32(MAGIC_NUMBER)
         stream.writeInt16(FILE_VERSION)
         stream.setVersion(QDataStream.Qt_4_1)
         for ship in self.ships:
             stream << ship.name << ship.owner << ship.country \
                    << ship.description
             stream.writeInt32(ship.teu)
         self.dirty = False
     except IOError as e:
         exception = e
     finally:
         if fh is not None:
             fh.close()
         if exception is not None:
             raise exception
Example #4
0
 def save(self):
     exception = None
     fh = None
     try:
         if self.filename.isEmpty():
             raise IOError("no filename specified for saving")
         fh = QFile(self.filename)
         if not fh.open(QIODevice.WriteOnly):
             raise IOError(str(fh.errorString()))
         stream = QDataStream(fh)
         stream.writeInt32(MAGIC_NUMBER)
         stream.writeInt16(FILE_VERSION)
         stream.setVersion(QDataStream.Qt_4_1)
         for ship in self.ships:
             stream << ship.name << ship.owner << ship.country \
                    << ship.description
             stream.writeInt32(ship.teu)
         self.dirty = False
     except IOError as e:
         exception = e
     finally:
         if fh is not None:
             fh.close()
         if exception is not None:
             raise exception
Example #5
0
 def saveQDataStream(self):
     error = None
     fh = None
     try:
         fh = QFile(self.__fname)
         if not fh.open(QIODevice.WriteOnly):
             raise IOError(str(fh.errorString()))
         stream = QDataStream(fh)
         stream.writeInt32(MovieContainer.MAGIC_NUMBER)
         stream.writeInt32(MovieContainer.FILE_VERSION)
         stream.setVersion(QDataStream.Qt_4_2)
         for key, movie in self.__movies:
             stream << movie.title
             stream.writeInt16(movie.year)
             stream.writeInt16(movie.minutes)
             stream << movie.acquired << movie.notes
     except EnvironmentError as e:
         error = "Failed to save: {0}".format(e)
     finally:
         if fh is not None:
             fh.close()
         if error is not None:
             return False, error
         self.__dirty = False
         return True, "Saved {0} movie records to {1}".format(
                 len(self.__movies),
                 QFileInfo(self.__fname).fileName())
Example #6
0
def open_to_stream(filename, mode, stream_version=QDataStream.Qt_4_5):
    file = QFile(filename)
    file.open(mode)

    stream = QDataStream(file)
    stream.setVersion(stream_version)
    return FileStream(file, stream)
Example #7
0
def read_project(settings, openFileName):
    """ Toplevel reader routine. """

    status = None
    handle = None
    try:
        handle = QFile(openFileName)
        if not handle.open(QIODevice.ReadOnly):
            raise IOError(handle.errorString())

        stream = QDataStream(handle)

        # check header
        magic = stream.readInt32()
        if magic != MAGIC_NUMBER:
            status = ("Unrecognized file type - \n{0}\nis not "
                           "a sconcho spf file!").format(openFileName)
            raise IOError(status)

        version = stream.readInt32()
        stream.setVersion(QDataStream.Qt_4_5)

        # initialize API specific entries
        repeatLegends = {}
        rowRepeats = []
        textItems = []
        rowLabels = {}
        columnLabels = {}

        if version == 1:
            (patternGridItems, legendItems, colors, activeSymbol, 
             patternRepeats) = read_API_1_version(stream, settings)

        elif version == 2:
             (patternGridItems, legendItems, colors, activeSymbol, 
             patternRepeats, repeatLegends, rowRepeats, textItems) = \
                     read_API_2_version(stream, settings)
        elif version == 3:
             (patternGridItems, legendItems, colors, activeSymbol, 
             patternRepeats, repeatLegends, rowRepeats, textItems,
             rowLabels, columnLabels) = \
                     read_API_3_version(stream, settings)
        else:
            raise IOError("unsupported API version")
            

    except (IOError, OSError) as e:
        status = "Failed to open %s: %s " % (openFileName, e)

    finally:
        if handle is not None:
            handle.close()
        if status is not None:
            return (False, status, None, None, None, None, None, None, 
                    None, None)

    return (True, None, patternGridItems, legendItems, colors, 
            activeSymbol, patternRepeats, repeatLegends, rowRepeats,
            textItems, rowLabels, columnLabels)
Example #8
0
    def readResponse(self):
        global paralen
        global words
        stream = QDataStream(self.socket)
        stream.setVersion(QDataStream.Qt_4_2)

        while True:
            if self.nextBlockSize == 0:
                if self.socket.bytesAvailable() < SIZEOF_UINT16:
                    break
                self.nextBlockSize = stream.readUInt16()
            if self.socket.bytesAvailable() < self.nextBlockSize:
                break
            inpdata = QString()

            stream >> inpdata
            s = str(inpdata)
            s = s.split(" ", 1)
            global flag2
            if flag2 == 1:
                if int(s[0]) == 1000 and (s[1]) == "0":
                    self.lbl.setText(
                        "<font size='8'>Waiting for other players . . . </font>"
                    )

                else:
                    flag2 = 0  # flag2==0: para is fetched and game is not finished
                    parastr = s[1].split(" ", 1)
                    n = int(str(parastr[0]))
                    allparas = paralist()
                    global paragraph
                    global paralen
                    paragraph = parastr[1]
                    paralen = len(paragraph.split())
                    self.lbl.setText("<font size=\"" + str(paratextsize) +
                                     "\">" + paragraph + "</font>")
                    self.lcdwpm.display(0)
                    self.lcdaccuracy.display(0)
                    self.lcdtimer.display(0)
                    self.progbar.setValue(0)
                    self.exitlbl.setVisible(False)
                    updatecurrentWordFromPara()
                    global totalChars
                    totalChars = len(paragraph)
                    self.timer.stop()
                    self.qle.setReadOnly(True)
                    self.timer2.start(1000)
            if flag2 == 2:
                if int(s[0]) == 1009:
                    self.lbl.setText(s[1])
                    self.lbl.setStyleSheet(
                        _fromUtf8("font: 24pt \"mry_KacstQurn\";"
                                  "color:#4C3327;"))

            elif flag2 == 0 and int(s[0]) != 1000 and int(
                    s[0]) != 1010 and int(s[0]) != 1009:
                self.lcdaccuracy.display(int(str(inpdata)))
            self.nextBlockSize = 0
Example #9
0
def read_project(settings, openFileName):
    """ Toplevel reader routine. """

    status = None
    handle = None
    try:
        handle = QFile(openFileName)
        if not handle.open(QIODevice.ReadOnly):
            raise IOError(handle.errorString())

        stream = QDataStream(handle)

        # check header
        magic = stream.readInt32()
        if magic != MAGIC_NUMBER:
            status = ("Unrecognized file type - \n{0}\nis not "
                      "a sconcho spf file!").format(openFileName)
            raise IOError(status)

        version = stream.readInt32()
        stream.setVersion(QDataStream.Qt_4_5)

        # initialize API specific entries
        repeatLegends = {}
        rowRepeats = []
        textItems = []
        rowLabels = {}
        columnLabels = {}

        if version == 1:
            (patternGridItems, legendItems, colors, activeSymbol,
             patternRepeats) = read_API_1_version(stream, settings)

        elif version == 2:
            (patternGridItems, legendItems, colors, activeSymbol,
            patternRepeats, repeatLegends, rowRepeats, textItems) = \
                    read_API_2_version(stream, settings)
        elif version == 3:
            (patternGridItems, legendItems, colors, activeSymbol,
            patternRepeats, repeatLegends, rowRepeats, textItems,
            rowLabels, columnLabels) = \
                    read_API_3_version(stream, settings)
        else:
            raise IOError("unsupported API version")

    except (IOError, OSError) as e:
        status = "Failed to open %s: %s " % (openFileName, e)

    finally:
        if handle is not None:
            handle.close()
        if status is not None:
            return (False, status, None, None, None, None, None, None, None,
                    None)

    return (True, None, patternGridItems, legendItems, colors, activeSymbol,
            patternRepeats, repeatLegends, rowRepeats, textItems, rowLabels,
            columnLabels)
Example #10
0
 def issueRequest(self, inpdata):
     self.request = QByteArray()
     stream = QDataStream(self.request, QIODevice.WriteOnly)
     stream.setVersion(QDataStream.Qt_4_2)
     stream.writeUInt16(0)
     stream << inpdata
     stream.device().seek(0)
     stream.writeUInt16(self.request.size() - SIZEOF_UINT16)
     if self.socket.isOpen():
         self.socket.close()
     self.socket.connectToHost("127.0.0.1", PORT)
Example #11
0
def save_project(canvas, colors, activeSymbol, settings, saveFileName):
    """ Toplevel writer routine. 

    """

    # prepare data structures
    patternGridItems = get_patternGridItems(canvas)
    legendItems = canvas.gridLegend.values()
    patternRepeats = get_patternRepeats(canvas)
    repeatLegends = canvas.repeatLegend
    rowRepeats = canvas.rowRepeatTracker
    rowLabels = canvas.rowLabels
    columnLabels = canvas.columnLabels
    textItems = canvas.canvasTextBoxes
    assert(len(patternRepeats) == len(repeatLegends))

    status = None
    handle = None
    try:
        handle = QFile(saveFileName)
        if not handle.open(QIODevice.WriteOnly | QIODevice.Truncate):
            raise IOError(unicode(handle.errorString()))

        stream = QDataStream(handle)

        # write header
        stream.writeInt32(MAGIC_NUMBER)
        stream.writeInt32(API_VERSION)
        stream.setVersion(QDataStream.Qt_4_5)

        # write content
        write_patternGridItems(stream, patternGridItems)
        write_legendItems(stream, legendItems)
        write_colors(stream, colors)
        write_active_symbol(stream, activeSymbol)
        write_patternRepeats(stream, patternRepeats)
        write_repeatLegends(stream, repeatLegends)
        write_rowRepeats(stream, rowRepeats)
        write_textItems(stream, textItems)
        write_row_labels(stream, rowLabels)
        write_column_labels(stream, columnLabels)
        write_settings(stream, settings)


    except (IOError, OSError) as e:
        status = "Failed to save: %s " % e

    finally:
        if handle is not None:
            handle.close()
        if status is not None:
            return (False, status)

    return (True, None)
Example #12
0
def save_project(canvas, colors, activeSymbol, settings, saveFileName):
    """ Toplevel writer routine. 

    """

    # prepare data structures
    patternGridItems = get_patternGridItems(canvas)
    legendItems = canvas.gridLegend.values()
    patternRepeats = get_patternRepeats(canvas)
    repeatLegends = canvas.repeatLegend
    rowRepeats = canvas.rowRepeatTracker
    rowLabels = canvas.rowLabels
    columnLabels = canvas.columnLabels
    textItems = canvas.canvasTextBoxes
    assert (len(patternRepeats) == len(repeatLegends))

    status = None
    handle = None
    try:
        handle = QFile(saveFileName)
        if not handle.open(QIODevice.WriteOnly | QIODevice.Truncate):
            raise IOError(unicode(handle.errorString()))

        stream = QDataStream(handle)

        # write header
        stream.writeInt32(MAGIC_NUMBER)
        stream.writeInt32(API_VERSION)
        stream.setVersion(QDataStream.Qt_4_5)

        # write content
        write_patternGridItems(stream, patternGridItems)
        write_legendItems(stream, legendItems)
        write_colors(stream, colors)
        write_active_symbol(stream, activeSymbol)
        write_patternRepeats(stream, patternRepeats)
        write_repeatLegends(stream, repeatLegends)
        write_rowRepeats(stream, rowRepeats)
        write_textItems(stream, textItems)
        write_row_labels(stream, rowLabels)
        write_column_labels(stream, columnLabels)
        write_settings(stream, settings)

    except (IOError, OSError) as e:
        status = "Failed to save: %s " % e

    finally:
        if handle is not None:
            handle.close()
        if status is not None:
            return (False, status)

    return (True, None)
Example #13
0
 def _loadRulesByExport(self, e_file):
     qfile = QFile(e_file)
     if qfile.open(QIODevice.ReadOnly):
         inp = QDataStream(qfile)
         inp.setVersion(QDataStream.Qt_4_5)
         tmp = inp.readQVariantHash()
         #tmp = QVariant(inp)
         rules = self._loadRulesFromVariantHash(tmp) 
         qfile.close()
         #if (tmp.isEmpty())
         #return false;
         #qDebug("Processing was successful!");
         return rules
     else:
         print "error: file " + file + "not open"
         return None
Example #14
0
 def loadQDataStream(self):
     error = None
     fh = None
     try:
         fh = QFile(self.__fname)
         if not fh.open(QIODevice.ReadOnly):
             raise IOError(str(fh.errorString()))
         stream = QDataStream(fh)
         magic = stream.readInt32()
         if magic != MovieContainer.MAGIC_NUMBER:
             raise IOError("unrecognized file type")
         version = stream.readInt32()
         if version < MovieContainer.OLD_FILE_VERSION:
             raise IOError("old and unreadable file format")
         elif version > MovieContainer.FILE_VERSION:
             raise IOError("new and unreadable file format")
         old = False
         if version == MovieContainer.OLD_FILE_VERSION:
             old = True
         stream.setVersion(QDataStream.Qt_4_2)
         self.clear(False)
         while not stream.atEnd():
             title = QString()
             acquired = QDate()
             location = QString()
             notes = QString()
             stream >> title
             year = stream.readInt16()
             minutes = stream.readInt16()
             if old:
                 stream >> acquired >> notes
             else:
                 stream >> acquired >> location >> notes
             self.add(Movie(title, year, minutes, acquired, location,
                            notes))
     except EnvironmentError as e:
         error = "Failed to load: {0}".format(e)
     finally:
         if fh is not None:
             fh.close()
         if error is not None:
             return False, error
         self.__dirty = False
         return True, "Loaded {0} movie records from {1}".format(
             len(self.__movies),
             QFileInfo(self.__fname).fileName())
Example #15
0
 def loadQDataStream(self):
     error = None
     fh = None
     try:
         fh = QFile(self.__fname)
         if not fh.open(QIODevice.ReadOnly):
             raise IOError(str(fh.errorString()))
         stream = QDataStream(fh)
         magic = stream.readInt32()
         if magic != MovieContainer.MAGIC_NUMBER:
             raise IOError("unrecognized file type")
         version = stream.readInt32()
         if version < MovieContainer.OLD_FILE_VERSION:
             raise IOError("old and unreadable file format")
         elif version > MovieContainer.FILE_VERSION:
             raise IOError("new and unreadable file format")
         old = False
         if version == MovieContainer.OLD_FILE_VERSION:
             old = True
         stream.setVersion(QDataStream.Qt_4_2)
         self.clear(False)
         while not stream.atEnd():
             title = QString()
             acquired = QDate()
             location = QString()
             notes = QString()
             stream >> title
             year = stream.readInt16()
             minutes = stream.readInt16()
             if old:
                 stream >> acquired >> notes
             else:
                 stream >> acquired >> location >> notes
             self.add(Movie(title, year, minutes, acquired,
                            location, notes))
     except EnvironmentError as e:
         error = "Failed to load: {0}".format(e)
     finally:
         if fh is not None:
             fh.close()
         if error is not None:
             return False, error
         self.__dirty = False
         return True, "Loaded {0} movie records from {1}".format(
                 len(self.__movies),
                 QFileInfo(self.__fname).fileName())
Example #16
0
 def run(self):
         fh = QFile(self.filename)
         fhlen = self._file_len(self.filename)
         if not fh.open(QIODevice.ReadOnly):
                 raise IOError, unicode(fh.errorString())
         self.data_block = {}
         stream = QDataStream(fh)
         stream.setVersion(QDataStream.Qt_4_2)
         magic = stream.readInt32()
         if magic != gMAGICNUM:
                 raise IOError, "not a valid .jd file"
         fileVersion = stream.readInt16()
         if fileVersion != gFILEVERSION:
                 raise IOError, "unrecognised .jd file version"
         while not fh.atEnd():
                 self._ReadItemFromStream(stream)
         
         self.emit(SIGNAL("PROJECTLOADERDONE(PyQt_PyObject)"), self.data_block)
Example #17
0
 def save(self):
     exception = None
     fh = None
     try:
         if self.filename.isEmpty():
             raise IOError, "no filename specified for saving"
         fh = QFile(self.filename)
         if not fh.open(QIODevice.WriteOnly):
             raise IOError, unicode(fh.errorString())
         stream = QDataStream(fh)
         stream.writeInt32(MAGIC_NUMBER)
         stream.writeInt16(FILE_VERSION)
         stream.setVersion(QDataStream.Qt_4_1)
         for ship in self.ships.values():
             stream << ship.name << ship.owner << ship.country 
             stream << ship.description
             stream.writeInt32(ship.teu)
         self.dirty = False
     except IOError, err:
         exception = err
Example #18
0
 def save(self):
     if self.filename.isEmpty():
         path = "."
         fname = QFileDialog.getSaveFileName(self,
                 "Page Designer - Save As", path,
                 "Page Designer Files (*.pgd)")
         if fname.isEmpty():
             return
         self.filename = fname
     fh = None
     try:
         fh = QFile(self.filename)
         if not fh.open(QIODevice.WriteOnly):
             raise IOError, unicode(fh.errorString())
         self.scene.clearSelection()
         stream = QDataStream(fh)
         stream.setVersion(QDataStream.Qt_4_2)
         stream.writeInt32(MagicNumber)
         stream.writeInt16(FileVersion)
         for item in self.scene.items():
             self.writeItemToStream(stream, item)
     except IOError, e:
         QMessageBox.warning(self, "Page Designer -- Save Error",
                 "Failed to save {0}: {1}".format(self.filename, e))