Ejemplo n.º 1
0
 def load(self):
     exception = None
     fh = None
     try:
         if not self.filename:
             raise IOError("no filename specified for loading")
         fh = QtCore.QFile(self.filename)
         if not fh.open(QtCore.QIODevice.ReadOnly):
             raise IOError(fh.errorString())
         stream = QtCore.QDataStream(fh)
         magic = stream.readInt32()
         if magic != MAGIC_NUMBER:
             raise IOError("unrecognized file type")
         fileVersion = stream.readInt16()
         if fileVersion != FILE_VERSION:
             raise IOError("unrecognized file type version")
         self.ships = {}
         while not stream.atEnd():
             name = stream.readQString()
             owner = stream.readQString()
             country = stream.readQString()
             description = stream.readQString()
             teu = stream.readInt32()
             ship = Ship(name, owner, country, teu, description)
             self.ships[id(ship)] = ship
             self.owners.add(owner)
             self.countries.add(country)
         self.dirty = False
     except IOError as e:
         exception = e
     finally:
         if fh is not None:
             fh.close()
         if exception is not None:
             raise exception
Ejemplo n.º 2
0
 def save(self):
     exception = None
     fh = None
     try:
         if not self.filename:
             raise IOError("no filename specified for saving")
         fh = QtCore.QFile(self.filename)
         if not fh.open(QtCore.QIODevice.WriteOnly):
             raise IOError(fh.errorString())
         stream = QtCore.QDataStream(fh)
         stream.writeInt32(MAGIC_NUMBER)
         stream.writeInt16(FILE_VERSION)
         stream.setVersion(QtCore.QDataStream.Qt_4_5)
         for ship in self.ships:
             stream.writeQString(ship.name)
             stream.writeQString(ship.owner)
             stream.writeQString(ship.country)
             stream.writeQString(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
Ejemplo n.º 3
0
 def saveQDataStream(self):
     error = None
     file = None
     try:
         file = QtCore.QFile(self.__fname)
         if not file.open(QtCore.QIODevice.WriteOnly):
             raise IOError(file.errorString())
         stream = QtCore.QDataStream(file)
         stream.writeInt32(MovieContainer.MAGIC_NUMBER)
         stream.writeInt32(MovieContainer.FILE_VERSION)
         stream.setVersion(QtCore.QDataStream.Qt_4_2)
         for key, movie in self.__movies:
             stream.writeQString(movie.title)
             stream.writeInt16(movie.year)
             stream.writeInt16(movie.minutes)
             stream.writeQString(movie.acquired.toString(QtCore.Qt.ISODate))
             stream.writeQString(movie.notes)
     except EnvironmentError as e:
         error = "Failed to save: {}".format(e)
     finally:
         if file is not None:
             file.close()
         if error is not None:
             return False, error
         self.__dirty = False
         return (
             True,
             "Saved {} movie records to {}".format(
                 len(self.__movies),
                 QtCore.QFileInfo(self.__fname).fileName()),
         )
Ejemplo n.º 4
0
    def sendFortune(self):
        block = QtCore.QByteArray()
        out = QtCore.QDataStream(block, QtCore.QIODevice.WriteOnly)
        out.setVersion(QtCore.QDataStream.Qt_4_0)
        out.writeUInt16(0)
        fortune = self.fortunes[random.randint(0, len(self.fortunes) - 1)]

        out.writeString(fortune)
        out.device().seek(0)
        out.writeUInt16(block.size() - 2)

        clientConnection = self.tcpServer.nextPendingConnection()
        clientConnection.disconnected.connect(clientConnection.deleteLater)

        clientConnection.write(block)
        clientConnection.disconnectFromHost()
Ejemplo n.º 5
0
 def loadQDataStream(self):
     error = None
     file = None
     try:
         file = QtCore.QFile(self.__fname)
         if not file.open(QtCore.QIODevice.ReadOnly):
             raise IOError(file.errorString())
         stream = QtCore.QDataStream(file)
         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")
         new = False if version == MovieContainer.OLD_FILE_VERSION else True
         stream.setVersion(QtCore.QDataStream.Qt_4_2)
         self.clear(False)
         while not stream.atEnd():
             title = stream.readQString()
             year = stream.readInt16()
             minutes = stream.readInt16()
             acquired = QtCore.QDate.fromString(stream.readQString(),
                                                QtCore.Qt.ISODate)
             location = ""
             if new:
                 location = stream.readQString()
             notes = stream.readQString()
             self.add(Movie(title, year, minutes, acquired, location,
                            notes))
     except EnvironmentError as e:
         error = "Failed to load: {}".format(e)
     finally:
         if file is not None:
             file.close()
         if error is not None:
             return False, error
         self.__dirty = False
         return (
             True,
             "Loaded {} movie records from {}".format(
                 len(self.__movies),
                 QtCore.QFileInfo(self.__fname).fileName()),
         )
Ejemplo n.º 6
0
    def readFortune(self):
        instr = QtCore.QDataStream(self.tcpSocket)
        instr.setVersion(QtCore.QDataStream.Qt_4_0)

        if self.blockSize == 0:
            if self.tcpSocket.bytesAvailable() < 2:
                return

            self.blockSize = instr.readUInt16()

        if self.tcpSocket.bytesAvailable() < self.blockSize:
            return

        nextFortune = instr.readString()
        nextFortune = str(nextFortune, encoding='ascii')

        if nextFortune == self.currentFortune:
            QtCore.QTimer.singleShot(0, self.requestNewFortune)
            return

        self.currentFortune = nextFortune
        self.statusLabel.setText(self.currentFortune)
        self.getFortuneButton.setEnabled(True)