[vdrpylib-cvslog] vdrpylib/vdr svdrp.py,1.4,1.5
Status: Alpha
Brought to you by:
rshortt
From: Rob S. <rs...@us...> - 2004-11-24 17:18:41
|
Update of /cvsroot/vdrpylib/vdrpylib/vdr In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv29782 Modified Files: svdrp.py Log Message: -Add SVDRP_CMD_* constants. -Add an option (default for now) to close the SVDRP connection after each command because VDR supports only one open connection. -Checks for host and port. -Debug connection handling, see where we can improve. -Add a method to return the current channel. Index: svdrp.py =================================================================== RCS file: /cvsroot/vdrpylib/vdrpylib/vdr/svdrp.py,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** svdrp.py 25 Nov 2002 22:03:51 -0000 1.4 --- svdrp.py 24 Nov 2004 17:18:27 -0000 1.5 *************** *** 24,27 **** --- 24,28 ---- import telnetlib import time + from types import * import channel *************** *** 31,34 **** --- 32,62 ---- # SVDRP constants SVDRP_PORT=2001 + SVDRP_CMD_CHAN = 'chan' + SVDRP_CMD_CLRE = 'clre' + SVDRP_CMD_DELC = 'delc' + SVDRP_CMD_DELR = 'delr' + SVDRP_CMD_DELT = 'delt' + SVDRP_CMD_GRAB = 'grab' + SVDRP_CMD_HELP = 'help' + SVDRP_CMD_HITK = 'hitk' + SVDRP_CMD_LSTC = 'lstc' + SVDRP_CMD_LSTE = 'lste' + SVDRP_CMD_LSTR = 'lstr' + SVDRP_CMD_LSTT = 'lstt' + SVDRP_CMD_MESG = 'mesg' + SVDRP_CMD_MODC = 'modc' + SVDRP_CMD_MODT = 'modt' + SVDRP_CMD_MOVC = 'movc' + SVDRP_CMD_MOVT = 'movt' + SVDRP_CMD_NEWC = 'newc' + SVDRP_CMD_NEWT = 'newt' + SVDRP_CMD_NEXT = 'next' + SVDRP_CMD_PUTE = 'pute' + SVDRP_CMD_SCAN = 'scan' + SVDRP_CMD_STAT = 'stat' + SVDRP_CMD_UPDT = 'updt' + SVDRP_CMD_VOLU = 'volu' + SVDRP_CMD_QUIT = 'quit' + class SVDRP(telnetlib.Telnet): *************** *** 42,58 **** the plain telnet interface may also be used to drive the protocol manually. """ ! def __init__(self, host = None, port = SVDRP_PORT): ! if host is None: ! telnetlib.Telnet.__init__(self) ! else: ! telnetlib.Telnet.__init__(self, host, port) ! # consume all output ! result = '' ! while not result: ! result = self.read_very_eager() ! def open(self, host, port = SVDRP_PORT): """Connect to a host. --- 70,91 ---- the plain telnet interface may also be used to drive the protocol manually. + + close_connection makes SVDRP close the connection after each command + because unfortunately the SVDRP interface only supports one open + connection (HELLO?!?!). """ ! def __init__(self, host, port = SVDRP_PORT, close_connection = 1): ! self.host = host ! self.port = port ! self.close_connection = close_connection ! ! telnetlib.Telnet.__init__(self, self.host, self.port) ! #print 'RLS: after telnet init' ! ! if self.close_connection: ! self.close() ! def open(self, host = None, port = SVDRP_PORT): """Connect to a host. *************** *** 62,66 **** Don't try to reopen an already connected instance. """ ! telnetlib.Telnet.open(self, host, port) --- 95,129 ---- Don't try to reopen an already connected instance. """ ! #print 'RLS: in open 1' ! if not host and not self.host: ! return ! ! if self.get_socket(): ! return ! ! if host: ! #print 'RLS: in open 2' ! telnetlib.Telnet.open(self, host, port) ! else: ! #print 'RLS: in open 3' ! telnetlib.Telnet.open(self, self.host, self.port) ! ! #time.sleep(0.2) ! ! # consume all output ! result = '' ! while not result: ! #print 'RLS: reading very eager' ! result = self.read_very_eager() ! #print 'RLS: read' ! #print 'RLS: result="%s"' % result ! ! ! def close(self): ! self.write('quit\n') ! reply = self.read_reply() ! #time.sleep(0.2) ! telnetlib.Telnet.close(self) ! return reply *************** *** 77,91 **** """ pat = re.compile('^\d\d\d .*\n', re.M) ! reply = '' ! buffer = self.read_very_eager() ! while not pat.search(buffer): ! reply = reply + buffer ! time.sleep(0.2) buffer = self.read_very_eager() ! reply = reply + buffer return reply ! def write_cmd(self, cmd): """Send text to the server and return its reply. --- 140,158 ---- """ pat = re.compile('^\d\d\d .*\n', re.M) ! #reply = '' ! reply = self.read_very_eager() ! #print 'RLS: initial reply "%s"' % reply ! while not pat.search(reply): buffer = self.read_very_eager() ! reply = reply + buffer ! #print 'RLS: reply now "%s"' % reply ! #time.sleep(0.2) ! #buffer = self.read_very_eager() ! #print 'RLS: reading reply got "%s"' % buffer ! #reply = reply + buffer return reply ! def write_cmd(self, cmd, close = -1): """Send text to the server and return its reply. *************** *** 99,107 **** if len(cmd) == 0: return '' if cmd[-1] != '\n': cmd = cmd + '\n' self.read_very_eager() self.write(cmd) ! return self.read_reply() --- 166,187 ---- if len(cmd) == 0: return '' + + self.open() + if cmd[-1] != '\n': cmd = cmd + '\n' self.read_very_eager() self.write(cmd) ! #time.sleep(0.2) ! reply = self.read_reply() ! ! if close > 0: ! self.close() ! #print 'RLS: want to close %d' % close ! elif close == -1 and self.close_connection: ! #print 'RLS: want to close %d' % close ! self.close() ! ! return reply *************** *** 122,126 **** ! def chan(self, channel, vdr = None): """Switch the current channel. --- 202,206 ---- ! def chan(self, chan, vdr = None): """Switch the current channel. *************** *** 136,146 **** string returned by VDR or None if the command failed. """ ! if isinstance(channel, channel.Channel): result = self.write_cmd('chan ' + channel.name + '\n') ! elif isinstance(channel, string._StringType): ! result = self.write_cmd('chan ' + channel + '\n') if result[0:3] == '250': ! if data is not None: ! return data.getchannel(result.split(None, 2)[1]) else: return result[3:] --- 216,226 ---- string returned by VDR or None if the command failed. """ ! if isinstance(chan, channel.Channel): result = self.write_cmd('chan ' + channel.name + '\n') ! elif isinstance(chan, StringType) or isinstance(chan, IntType): ! result = self.write_cmd('chan %s\n' % chan) if result[0:3] == '250': ! if vdr: ! return vdr.getchannel(result.split(None, 2)[1]) else: return result[3:] *************** *** 150,153 **** --- 230,240 ---- + def current_chan(self): + """ + Return the currently tuned channel. + """ + return self.write_cmd('chan\n') + + def lstc(self, id = None): """List channel details. *************** *** 198,202 **** successfully, false else. """ ! result = self.write_cmd('pute\n') if result[0:3] != '354': print 'pute returned: ' + result --- 285,289 ---- successfully, false else. """ ! result = self.write_cmd('pute\n', close = 0) if result[0:3] != '354': print 'pute returned: ' + result *************** *** 208,211 **** --- 295,302 ---- result = self.read_reply() + + if self.close_connection: + self.close() + if result[0:3] != '250': print 'pute returned: ' + result |