Ticket #2734: mythtv.py_additions.3.diff

File mythtv.py_additions.3.diff, 5.3 KB (added by Hadley Rich <hads@…>, 17 years ago)

Updated patch

  • mythvideo/mythvideo/scripts/MythTV.py

     
    5353class MythDB:
    5454        def __init__(self):
    5555                """
    56                 A connection to the mythtv database
     56                A connection to the mythtv database.
    5757                """
    5858                config_files = [
    5959                        '/usr/local/share/mythtv/mysql.txt',
     
    129129
    130130class MythTV:
    131131        """
    132         A connection to MythTV backend
     132        A connection to MythTV backend.
    133133        """
    134134        def __init__(self, conn_type='Monitor'):
    135135                self.db = MythDB()
     
    156156
    157157        def backendCommand(self, data):
    158158                """
    159                 Sends a command via a socket to the mythbackend in
    160                 the format that it expects. Returns the result from
    161                 the backend
     159                Sends a formatted command via a socket to the mythbackend.
     160
     161                Returns the result from the backend.
    162162                """
    163163                def recv():
    164164                        """
    165                         Reads the data returned fomr the backend
     165                        Reads the data returned from the backend.
    166166                        """
    167167                        # The first 8 bytes of the response gives us the length
    168168                        data = self.socket.recv(8)
     
    184184
    185185        def getPendingRecordings(self):
    186186                """
    187                 Returns a list of Program objects which are scheduled to be
    188                 recorded
     187                Returns a list of Program objects which are scheduled to be recorded.
    189188                """
    190189                programs = []
    191190                res = self.backendCommand('QUERY_GETALLPENDING').split(BACKEND_SEP)
     
    199198
    200199        def getScheduledRecordings(self):
    201200                """
    202                 Returns a list of Program objects which are scheduled to be
    203                 recorded
     201                Returns a list of Program objects which are scheduled to be recorded.
    204202                """
    205203                programs = []
    206204                res = self.backendCommand('QUERY_GETALLSCHEDULED').split(BACKEND_SEP)
     
    213211
    214212        def getUpcomingRecordings(self):
    215213                """
    216                 Returns a list of Program objects for programs which are actually
    217                 going to be recorded.
     214                Returns a list of Program objects which are scheduled to be recorded.
     215
     216                Sorts the list by recording start time and only returns those with
     217                record status of WillRecord.
    218218                """
    219219                def sort_programs_by_starttime(x, y):
    220220                        if x.starttime > y.starttime:
     
    231231                programs.sort(sort_programs_by_starttime)
    232232                return programs
    233233
     234        def getRecorderList(self):
     235                """
     236                Returns a list of recorders, or an empty list if none.
     237                """
     238                recorders = []
     239                c = self.db.cursor()
     240                c.execute('SELECT cardid FROM capturecard')
     241                row = c.fetchone()
     242                while row is not None:
     243                        recorders.append(int(row[0]))
     244                        row = c.fetchone()
     245                c.close()
     246                return recorders
     247       
    234248        def getFreeRecorderList(self):
    235249                """
    236                 Returns a list of free recorders, or an empty list if none
     250                Returns a list of free recorders, or an empty list if none.
    237251                """
    238252                res = self.backendCommand('GET_FREE_RECORDER_LIST').split(BACKEND_SEP)
    239253                recorders = [int(d) for d in res]
    240                 if recorders[0]:
    241                         return recorders
     254                return recorders
     255
     256        def getRecorderDetails(self, recorder_id):
     257                """
     258                Returns a Recorder object with details of the recorder.
     259                """
     260                c = self.db.cursor()
     261                c.execute("""SELECT cardid, cardtype, videodevice, hostname
     262                        FROM capturecard WHERE cardid = %s""", recorder_id)
     263                row = c.fetchone()
     264                if row:
     265                        recorder = Recorder(row)
     266                        return recorder
    242267                else:
    243                         return []
     268                        return None
    244269
    245270        def getCurrentRecording(self, recorder):
     271                """
     272                Returns a Program object for the current recorders recording.
     273                """
    246274                res = self.backendCommand('QUERY_RECORDER %s[]:[]GET_CURRENT_RECORDING' % recorder)
    247275                return Program(res.split(BACKEND_SEP))
    248276
    249277        def isRecording(self, recorder):
     278                """
     279                Returns a boolean as to whether the given recorder is recording.
     280                """
    250281                res = self.backendCommand('QUERY_RECORDER %s[]:[]IS_RECORDING' % recorder)
    251282                if res == '1':
    252283                        return True
    253284                else:
    254285                        return False
    255286
     287        def isActiveBackend(self, hostname):
     288                """
     289                Returns a boolean as to whether the given host is an active backend
     290                """
     291                res = self.backendCommand('QUERY_IS_ACTIVE_BACKEND[]:[]%s' % hostname)
     292                if res == 'TRUE':
     293                        return True
     294                else:
     295                        return False
     296
    256297class MythVideo:
    257298        def __init__(self):
    258299                self.db = MythDB()
    259300
    260301        def pruneMetadata(self):
     302                """
     303                Removes metadata from the database for files that no longer exist.
     304                """
    261305                c = self.db.cursor()
    262306                c.execute("""
    263307                        SELECT intid, filename
     
    336380                        return None
    337381
    338382        def setMetadata(self, data, id=None):
     383                """
     384                Adds or updates the metadata in the database for a video item.
     385                """
    339386                c = self.db.cursor()
    340387                if id is None:
    341388                        fields = ', '.join(data.keys())
     
    354401                        c.execute(sql, sql_values)
    355402                        c.close()
    356403
     404class Recorder:
     405        def __str__(self):
     406                return "Recorder %s (%s)" % (self.cardid, self.cardtype)
     407       
     408        def __repr__(self):
     409                return "Recorder %s (%s)" % (self.cardid, self.cardtype)
     410
     411        def __init__(self, data):
     412                """
     413                Load the list of data into the object.
     414                """
     415                self.cardid = data[0]
     416                self.cardtype = data[1]
     417                self.videodevice = data[2]
     418                self.hostname = data[3]
     419
    357420class Program:
     421        def __str__(self):
     422                return "%s (%s)" % (self.title, self.starttime.strftime('%Y-%m-%d %H:%M:%S'))
     423       
     424        def __repr__(self):
     425                return "%s (%s)" % (self.title, self.starttime.strftime('%Y-%m-%d %H:%M:%S'))
     426       
    358427        def __init__(self, data):
    359428                """
    360                 Load the list of data into the object
     429                Load the list of data into the object.
    361430                """
    362431                self.title = data[0]
    363432                self.subtitle = data[1]