Ejemplo n.º 1
0
    def _createScheduleFromDbDict(cls, conn, data):
        schedule = None
        if data:
            try:
                schedule = cls(data["id"])
                schedule.type = data["type"]
                schedule.channelId = data["channel_id"]
                schedule.startTime = data["start_time"]
                schedule.endTime = data["end_time"]
                schedule.title = data["title"]
                schedule.preferHd = data["prefer_hd"]
                schedule.preferUnscrambled = data["prefer_unscrambled"]
                schedule.dupMethod = data["dup_method"]
                schedule.startEarly = data["start_early"]
                schedule.endLate = data["end_late"]
                schedule.inactive = data["inactive"]

                if schedule.channelId != -1:
                    schedule.channel = Channel.getFromDb(
                        conn, schedule.channelId)
                    if not schedule.channel:
                        schedule.channelId = -1

                Cache().cache("schedules", schedule.id, schedule)

            except:
                cls._logger.error(
                    "_createScheduleFromDbDict: unexpected error: %s" %
                    (sys.exc_info()[0]))
                printTraceback()
        return schedule
Ejemplo n.º 2
0
 def deleteFromDb(self, conn):
     assert self._tableName != None, "Not the right class: %r" % (self)
     if conn:
         conn.execute("DELETE FROM %s WHERE id=?" % (self._tableName),
                      (self._id, ))
         self._channelUrlClass.deleteByChannelIdFromDb(conn, self._id)
         Cache().purge(self._tableName, self._id)
Ejemplo n.º 3
0
    def changeStatus(self, conn, status):
        assert self._tableName != None, "Not the right class: %r" % (self)
        if self._id == -1:
            self._logger.error(
                "changeStatus: cannot change recording status; recording not in database yet"
            )
            return
        if self._status != status:
            self._status = status
            if conn:
                if status == RecordingState.RECORDING_FINISHED or status == RecordingState.RECORDING_UNFINISHED:
                    generalConfig = GeneralConfig(Config())
                    recordingFilename = os.path.abspath(
                        os.path.join(generalConfig.recordingsPath,
                                     self._filename))
                    if os.path.exists(recordingFilename):
                        self._fileSize = os.stat(recordingFilename).st_size
                    self._logger.warning(
                        "changeStatus: recording (un)finished; id=%d, status=%d, fileSize=%d"
                        % (self._id, self._status, self._fileSize))
                    conn.execute(
                        "UPDATE %s SET status=?, file_size=? WHERE id=?" %
                        (self._tableName), (status, self._fileSize, self._id),
                        logger=self._logger)
                else:
                    conn.execute("UPDATE %s SET status=? WHERE id=?" %
                                 (self._tableName), (status, self._id),
                                 logger=self._logger)

            Cache().purge(self._tableName, self.id)
        else:
            self._logger.warning(
                "changeStatus: status of recording with id=%d didn't change: %d == %d"
                % (self._id, self._status, status))
Ejemplo n.º 4
0
    def addToDb(self, conn):
        assert self._tableName != None, "Not the right class: %r" % (self)
        if conn:
            #            channel = None
            # This shouldn't be necessary
            #             if self._id != -1:
            #                 channel = self.getFromDb( conn, self._id )
            #                 if not channel:
            #                     self._id = -1

            if self._id != -1:
                conn.execute(
                    "UPDATE %s SET number=?, epg_id=?, name=?, name_short=?, logo=?, thumbnail=?, radio=?, inactive=? WHERE id=?"
                    % (self._tableName),
                    (self._number, self._epgId, self._name, self._nameShort,
                     self._logo, self._thumbnail, self._radio, self._inactive,
                     self._id))
            else:
                channelId = conn.insert(
                    "INSERT INTO %s (number, epg_id, name, name_short, logo, thumbnail, radio, inactive) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
                    % (self._tableName),
                    (self._number, self._epgId, self._name, self._nameShort,
                     self._logo, self._thumbnail, self._radio, self._inactive))
                if channelId:
                    self._id = channelId

            if self._id:
                urls = self._channelUrlClass.getAllFromDb(conn, self._id)
                for key in urls.keys():
                    if not self._urls.has_key(key):
                        urls[key].deleteFromDb(conn, self._id)
                for key in self._urls.keys():
                    self._urls[key].addToDb(conn, self._id)

            Cache().cache(self._tableName, self.id, self)
Ejemplo n.º 5
0
    def addToDb(self, conn):
        if conn:
            #             schedule = None
            #             if self._id != -1:
            #                 schedule = Schedule.getFromDb( conn, self._id )
            #                 if not schedule:
            #                     self._id = -1

            if self._id != -1:
                conn.execute(
                    """
                                 UPDATE
                                     schedules
                                 SET
                                     type=?,
                                     channel_id=?,
                                     start_time=?,
                                     end_time=?,
                                     title=?,
                                     prefer_hd=?,
                                     prefer_unscrambled=?,
                                     dup_method=?,
                                     start_early=?,
                                     end_late=?,
                                     inactive=? 
                                 WHERE
                                     id=?
                              """,
                    (self._type, self._channelId, self._startTime,
                     self._endTime, self._title, self._preferHd,
                     self._preferUnscrambled, self._dupMethod,
                     self._startEarly, self._endLate, self._inactive,
                     self._id))
            else:
                scheduleId = conn.insert(
                    """
                                             INSERT INTO
                                                 schedules (type,
                                                            channel_id,
                                                            start_time,
                                                            end_time,
                                                            title,
                                                            prefer_hd,
                                                            prefer_unscrambled,
                                                            dup_method,
                                                            start_early,
                                                            end_late,
                                                            inactive)
                                             VALUES
                                                 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
                                          """,
                    (self._type, self._channelId, self._startTime,
                     self._endTime, self._title, self._preferHd,
                     self._preferUnscrambled, self._dupMethod,
                     self._startEarly, self._endLate, self._inactive))
                if scheduleId:
                    self._id = scheduleId

            Cache().cache("schedules", self.id, self)
Ejemplo n.º 6
0
    def getFromDb(cls, conn, id):  # @ReservedAssignment
        schedule = Cache().get("schedules", id)
        if not schedule and conn:
            row = conn.execute("SELECT * FROM schedules WHERE id = ?", (id, ))
            if row:
                schedule = cls._createScheduleFromDbDict(conn, row[0])

        return schedule
Ejemplo n.º 7
0
 def getFromDb(cls, conn, id):  # @ReservedAssignment
     assert cls._tableName != None, "Not the right class: %r" % (cls)
     channel = Cache().get(cls._tableName, id)
     if not channel and conn:
         row = conn.execute(
             "SELECT * FROM %s WHERE id=?" % (cls._tableName), (id, ))
         if row:
             channel = cls._createChannelFromDbDict(conn, row[0])
     return channel
Ejemplo n.º 8
0
    def getFromDb(cls, conn, id_):
        assert cls._tableName != None, "Not the right class: %r" % (cls)
        recording = Cache().get(cls._tableName, id_)
        if not recording and conn:
            row = conn.execute(
                "SELECT * FROM %s WHERE id = ?" % (cls._tableName), (id_, ))
            if row:
                recording = cls._createRecordingFromDbDict(conn, row[0])

        return recording
Ejemplo n.º 9
0
    def deleteFromDb(self, conn):
        assert self._tableName != None, "Not the right class: %r" % (self)
        if conn:
            if self._epgProgramId != -1:
                # remove RecordingProgram
                recordingProgram = RecordingProgram.getFromDb(
                    conn, self._epgProgramId)
                recordingProgram.deleteFromDb(conn)

            conn.execute("DELETE FROM %s WHERE id = ?" % (self._tableName),
                         (self._id, ),
                         logger=self._logger)

            Cache().purge(self._tableName, self.id)
Ejemplo n.º 10
0
    def deleteFromDb(self, conn, rerecord=False):
        assert self._tableName != None, "Not the right class: %r" % (self)
        if conn:
            self._logger.warning(
                "Remove recording id=%d (%s recorded at %s from %s; rerecord=%s)"
                % (self._id, self._title,
                   datetime.datetime.fromtimestamp(
                       self._startTime), self._channelName, rerecord))

            # Re-record prevention only makes sense when recording is linked to an actual program
            if self._epgProgramId != -1:
                if not rerecord:
                    self._logger.info(
                        "Creating copy of recording to prevent re-recording of this program"
                    )
                    oldRecording = OldRecording.copy(self)
                    oldRecording.addToDb(conn)
                else:
                    # remove RecordingProgram
                    self._logger.info(
                        "Remove recording program to allow re-recording of this program"
                    )
                    recordingProgram = RecordingProgram.getFromDb(
                        conn, self._epgProgramId)
                    if recordingProgram:
                        recordingProgram.deleteFromDb(conn)
                    else:
                        self._logger.error(
                            "Recording program with epgProgramId=%d does not exist!"
                            % (self._epgProgramId))

            generalConfig = GeneralConfig(Config())
            recordingFilename = os.path.abspath(
                os.path.join(generalConfig.recordingsPath, self._filename))
            if os.path.islink(recordingFilename) or os.path.isfile(
                    recordingFilename):
                os.unlink(recordingFilename)

            conn.execute("DELETE FROM %s WHERE id=?" % (self._tableName),
                         (self._id, ),
                         logger=self._logger)

            Cache().purge(self._tableName, self.id)
Ejemplo n.º 11
0
    def _createRecordingFromDbDict(cls, conn, data):
        recording = None
        if data:
            try:
                recording = cls(data["id"])
                recording.scheduleId = data["schedule_id"]
                recording.epgProgramId = data["epg_program_id"]
                recording.channelId = data["channel_id"]
                recording.channelName = data["channel_name"]
                recording.channelUrlType = data["channel_url_type"]
                recording.startTime = data["start_time"]
                recording.endTime = data["end_time"]
                recording.length = data["length"]
                recording.title = data["title"]
                recording.filename = data["filename"]
                recording.fileSize = data["file_size"]
                recording.streamArguments = data["stream_arguments"]
                recording.type = data["type"]
                recording.scrambled = data["scrambled"]
                recording.marker = data["marker"]
                recording.status = data["status"]
                recording.rerecord = data["rerecord"]

                if recording.epgProgramId != -1:
                    recording.epgProgram = RecordingProgram.getFromDb(
                        conn, recording.epgProgramId)
                    if not recording.epgProgram:
                        recording.epgProgramId = -1

                if recording.channelId != -1:
                    recording.channel = Channel.getFromDb(
                        conn, recording.channelId)
                    if not recording.channel:
                        recording.channelId = -1

                Cache().cache(cls._tableName, recording.id, recording)
            except:
                cls._logger.error(
                    "_createRecordingFromDbDict: unexpected error: %s" %
                    (sys.exc_info()[0]))
                printTraceback()

        return recording
Ejemplo n.º 12
0
    def _createChannelFromDbDict(cls, conn, channelData):
        channel = None
        if channelData:
            try:
                channel = cls(channelData["id"])
                channel.number = channelData["number"]
                channel.epgId = channelData["epg_id"]
                channel.name = channelData["name"]
                channel.nameShort = channelData["name_short"]
                channel.logo = channelData["logo"]
                channel.thumbnail = channelData["thumbnail"]
                channel.radio = channelData["radio"]
                channel.inactive = channelData["inactive"]
                channel.getUrlsFromDb(conn)

                Cache().cache(cls._tableName, channel.id, channel)
            except:
                cls._logger.error(
                    "_createChannelFromDbDict: unexpected error: %s" %
                    (sys.exc_info()[0]))
                printTraceback()
                channel = None
        return channel
Ejemplo n.º 13
0
    def deleteFromDb(self, conn):
        if conn:
            conn.execute("DELETE FROM schedules WHERE id = ?", (self._id, ))

            Cache().purge("schedules", self._id)
Ejemplo n.º 14
0
    def addToDb(self, conn):
        assert self._tableName != None, "Not the right class: %r" % (self)
        if conn:
            #            recording = None
            # This shouldn't be necessary
            #             if self._id != -1:
            #                 recording = self.getFromDb( conn, self._id )
            #                 if not recording:
            #                     self._id = -1

            if self._id != -1:
                conn.execute(
                    """
                                 UPDATE
                                     %s
                                 SET
                                     schedule_id=?,
                                     epg_program_id=?,
                                     channel_id=?,
                                     channel_name=?,
                                     channel_url_type=?,
                                     start_time=?,
                                     end_time=?,
                                     length=?,
                                     title=?,
                                     filename=?,
                                     file_size=?,
                                     stream_arguments=?,
                                     type=?,
                                     scrambled=?,
                                     marker=?,
                                     status=?,
                                     rerecord=?
                                 WHERE
                                     id=?
                              """ % (self._tableName),
                    (self._scheduleId, self._epgProgramId, self._channelId,
                     self._channelName, self._channelUrlType, self._startTime,
                     self._endTime, self._length, self._title, self._filename,
                     self._fileSize, self._streamArguments, self._type,
                     self._scrambled, self._marker, self._status,
                     self._rerecord, self._id),
                    logger=self._logger)
            else:
                if self._filename == "":
                    self._filename = self._generateFilename(conn)

                if self._epgProgramId == -1 and self._epgProgram:
                    assert isinstance(
                        self._epgProgram, RecordingProgram
                    ), "self._epgProgram not of type RecordingProgram: %r" % (
                        self._epgProgram)
                    self._epgProgram.addToDb(conn)
                    self._epgProgramId = self._epgProgram.id

                recordingId = conn.insert(
                    """
                                              INSERT INTO
                                                  %s (schedule_id,
                                                      epg_program_id,
                                                      channel_id,
                                                      channel_name,
                                                      channel_url_type,
                                                      start_time,
                                                      end_time,
                                                      length,
                                                      title,
                                                      filename,
                                                      file_size,
                                                      stream_arguments,
                                                      type,
                                                      scrambled,
                                                      marker,
                                                      status,
                                                      rerecord)
                                              VALUES
                                                  (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
                                           """ % (self._tableName),
                    (self._scheduleId, self._epgProgramId, self._channelId,
                     self._channelName, self._channelUrlType, self._startTime,
                     self._endTime, self._length, self._title, self._filename,
                     self._fileSize, self._streamArguments, self._type,
                     self._scrambled, self._marker, self._status,
                     self._rerecord))
                if recordingId:
                    self._id = recordingId
                else:
                    self._logger.error("Recording not inserted into table %s" %
                                       (self._tableName))

            Cache().cache(self._tableName, self.id, self)