diff --git a/python_apps/pypo/schedule/telnetliquidsoap.py b/python_apps/pypo/schedule/telnetliquidsoap.py index 946fcd463..6f4cf3a53 100644 --- a/python_apps/pypo/schedule/telnetliquidsoap.py +++ b/python_apps/pypo/schedule/telnetliquidsoap.py @@ -36,14 +36,13 @@ class TelnetLiquidsoap: self.current_prebuffering_stream_id = None def __connect(self): - return telnetlib.Telnet(self.ls_host, self.ls_port) + return self.__connect() def __is_empty(self, tn, queue_id): return True def queue_clear_all(self): - try: - self.telnet_lock.acquire() + with self.telnet_lock: tn = self.__connect() for i in self.queues: @@ -53,14 +52,9 @@ class TelnetLiquidsoap: tn.write("exit\n") self.logger.debug(tn.read_all()) - except Exception: - raise - finally: - self.telnet_lock.release() def queue_remove(self, queue_id): - try: - self.telnet_lock.acquire() + with self.telnet_lock: tn = self.__connect() msg = 'queues.%s_skip\n' % queue_id @@ -69,15 +63,9 @@ class TelnetLiquidsoap: tn.write("exit\n") self.logger.debug(tn.read_all()) - except Exception: - raise - finally: - self.telnet_lock.release() - def queue_push(self, queue_id, media_item): - try: - self.telnet_lock.acquire() + with self.telnet_lock: tn = self.__connect() if not self.__is_empty(tn, queue_id): @@ -95,17 +83,10 @@ class TelnetLiquidsoap: tn.write("exit\n") self.logger.debug(tn.read_all()) - except Exception: - raise - finally: - self.telnet_lock.release() - def stop_web_stream_buffer(self): - try: - self.telnet_lock.acquire() - tn = telnetlib.Telnet(self.ls_host, self.ls_port) - #dynamic_source.stop http://87.230.101.24:80/top100station.mp3 + with self.telnet_lock: + tn = self.__connect() msg = 'http.stop\n' self.logger.debug(msg) @@ -118,16 +99,9 @@ class TelnetLiquidsoap: tn.write("exit\n") self.logger.debug(tn.read_all()) - except Exception, e: - self.logger.error(str(e)) - finally: - self.telnet_lock.release() - def stop_web_stream_output(self): - try: - self.telnet_lock.acquire() - tn = telnetlib.Telnet(self.ls_host, self.ls_port) - #dynamic_source.stop http://87.230.101.24:80/top100station.mp3 + with self.telnet_lock: + tn = self.__connect() msg = 'dynamic_source.output_stop\n' self.logger.debug(msg) @@ -136,15 +110,9 @@ class TelnetLiquidsoap: tn.write("exit\n") self.logger.debug(tn.read_all()) - except Exception, e: - self.logger.error(str(e)) - finally: - self.telnet_lock.release() - def start_web_stream(self, media_item): - try: - self.telnet_lock.acquire() - tn = telnetlib.Telnet(self.ls_host, self.ls_port) + with self.telnet_lock: + tn = self.__connect() #TODO: DO we need this? msg = 'streams.scheduled_play_start\n' @@ -158,15 +126,10 @@ class TelnetLiquidsoap: self.logger.debug(tn.read_all()) self.current_prebuffering_stream_id = None - except Exception, e: - self.logger.error(str(e)) - finally: - self.telnet_lock.release() def start_web_stream_buffer(self, media_item): - try: - self.telnet_lock.acquire() - tn = telnetlib.Telnet(self.ls_host, self.ls_port) + with self.telnet_lock: + tn = self.__connect() msg = 'dynamic_source.id %s\n' % media_item['row_id'] self.logger.debug(msg) @@ -180,15 +143,10 @@ class TelnetLiquidsoap: self.logger.debug(tn.read_all()) self.current_prebuffering_stream_id = media_item['row_id'] - except Exception, e: - self.logger.error(str(e)) - finally: - self.telnet_lock.release() def get_current_stream_id(self): - try: - self.telnet_lock.acquire() - tn = telnetlib.Telnet(self.ls_host, self.ls_port) + with self.telnet_lock: + tn = self.__connect() msg = 'dynamic_source.get_id\n' self.logger.debug(msg) @@ -199,10 +157,6 @@ class TelnetLiquidsoap: self.logger.debug("stream_id: %s" % stream_id) return stream_id - except Exception, e: - self.logger.error(str(e)) - finally: - self.telnet_lock.release() def disconnect_source(self, sourcename): self.logger.debug('Disconnecting source: %s', sourcename) @@ -212,34 +166,22 @@ class TelnetLiquidsoap: elif(sourcename == "live_dj"): command += "live_dj_harbor.kick\n" - try: - self.telnet_lock.acquire() - tn = telnetlib.Telnet(self.ls_host, self.ls_port) + with self.telnet_lock: + tn = self.__connect() self.logger.info(command) tn.write(command) tn.write('exit\n') tn.read_all() - except Exception, e: - self.logger.error(traceback.format_exc()) - finally: - self.telnet_lock.release() def telnet_send(self, commands): - try: - self.telnet_lock.acquire() - - tn = telnetlib.Telnet(self.ls_host, self.ls_port) + with self.telnet_lock: + tn = self.__connect() for i in commands: self.logger.info(i) tn.write(i) tn.write('exit\n') tn.read_all() - except Exception, e: - self.logger.error(str(e)) - finally: - self.telnet_lock.release() - def switch_source(self, sourcename, status): self.logger.debug('Switching source: %s to "%s" status', sourcename, @@ -262,44 +204,29 @@ class TelnetLiquidsoap: def liquidsoap_get_info(self): self.logger.debug("Checking to see if Liquidsoap is running") response = "" - try: - self.telnet_lock.acquire() - tn = telnetlib.Telnet(self.host, self.port) + with self.telnet_lock: + tn = self.__connect() msg = "version\n" tn.write(msg) tn.write("exit\n") response = tn.read_all() - except Exception, e: - self.logger.error(str(e)) - return None - finally: - self.telnet_lock.release() return response def update_liquidsoap_station_name(self, station_name): - try: - try: - self.telnet_lock.acquire() - tn = telnetlib.Telnet(self.host, self.port) - command = ('vars.station_name %s\n' % - station_name).encode('utf-8') - self.logger.info(command) - tn.write(command) - tn.write('exit\n') - tn.read_all() - except Exception, e: - self.logger.error(str(e)) - finally: - self.telnet_lock.release() - except Exception, e: - self.logger.error("Exception %s", e) + with self.telnet_lock: + tn = self.__connect() + command = ('vars.station_name %s\n' % + station_name).encode('utf-8') + self.logger.info(command) + tn.write(command) + tn.write('exit\n') + tn.read_all() def get_liquidsoap_connection_status(self, current_time): output = None - try: - self.telnet_lock.acquire() - tn = telnetlib.Telnet(self.host, self.port) + with self.telnet_lock: + tn = self.__connect() # update the boot up time of Liquidsoap. Since Liquidsoap is not # restarting, we are manually adjusting the bootup time variable # so the status msg will get updated. @@ -312,43 +239,28 @@ class TelnetLiquidsoap: tn.write(connection_status) tn.write('exit\n') - output = tn.read_all() - except Exception, e: - self.logger.error(str(e)) - finally: - self.telnet_lock.release() - return None + return output def update_liquidsoap_stream_format(self, stream_format): - try: - self.telnet_lock.acquire() - tn = telnetlib.Telnet(self.host, self.port) + with self.telnet_lock: + tn = self.__connect() command = ('vars.stream_metadata_type %s\n' % stream_format).encode('utf-8') self.logger.info(command) tn.write(command) tn.write('exit\n') tn.read_all() - except Exception, e: - self.logger.error("Exception %s", e) - finally: - self.telnet_lock.release() def update_liquidsoap_transition_fade(self, fade): - try: - self.telnet_lock.acquire() - tn = telnetlib.Telnet(self.host, self.port) + with self.telnet_lock: + tn = self.__connect() command = ('vars.default_dj_fade %s\n' % fade).encode('utf-8') self.logger.info(command) tn.write(command) tn.write('exit\n') tn.read_all() - except Exception, e: - self.logger.error("Exception %s", e) - finally: - self.telnet_lock.release() class DummyTelnetLiquidsoap: