From 0a3c9c73519e36038df4fad105fad56119885645 Mon Sep 17 00:00:00 2001 From: Martin Konecny Date: Tue, 26 Jun 2012 22:41:11 -0400 Subject: [PATCH] clean up indentation + imports for python apps --- python_apps/pypo/install/pypo-initialize.py | 14 -- python_apps/pypo/pypocli.py | 40 +++-- python_apps/pypo/pypofetch.py | 120 +++++++------- python_apps/pypo/pypofile.py | 1 - python_apps/pypo/pypomessagehandler.py | 21 ++- python_apps/pypo/pypopush.py | 168 ++++++++++---------- python_apps/pypo/recorder.py | 31 ++-- 7 files changed, 183 insertions(+), 212 deletions(-) diff --git a/python_apps/pypo/install/pypo-initialize.py b/python_apps/pypo/install/pypo-initialize.py index bbefe7ddb..12eb870ca 100644 --- a/python_apps/pypo/install/pypo-initialize.py +++ b/python_apps/pypo/install/pypo-initialize.py @@ -109,20 +109,6 @@ try: print e sys.exit(1) - """ - logging.basicConfig(format='%(message)s') - - #generate liquidsoap config file - #access the DB and generate liquidsoap.cfg under /etc/airtime/ - ac = api_client.api_client_factory(config, logging.getLogger()) - ss = ac.get_stream_setting() - - if ss is not None: - generate_liquidsoap_config(ss) - else: - print "Unable to connect to the Airtime server." - """ - #initialize init.d scripts subprocess.call("update-rc.d airtime-playout defaults >/dev/null 2>&1", shell=True) diff --git a/python_apps/pypo/pypocli.py b/python_apps/pypo/pypocli.py index 1cc8ff2c8..eb528c176 100644 --- a/python_apps/pypo/pypocli.py +++ b/python_apps/pypo/pypocli.py @@ -3,12 +3,10 @@ Python part of radio playout (pypo) """ import time -from optparse import * +from optparse import OptionParser import sys import signal import logging -import logging.config -import logging.handlers import locale import os from Queue import Queue @@ -53,11 +51,11 @@ parser.add_option("-c", "--check", help="Check the cached schedule and exit", de def configure_locale(): logger.debug("Before %s", locale.nl_langinfo(locale.CODESET)) current_locale = locale.getlocale() - + if current_locale[1] is None: logger.debug("No locale currently set. Attempting to get default locale.") default_locale = locale.getdefaultlocale() - + if default_locale[1] is None: logger.debug("No default locale exists. Let's try loading from /etc/default/locale") if os.path.exists("/etc/default/locale"): @@ -69,17 +67,17 @@ def configure_locale(): sys.exit(1) else: new_locale = default_locale - + logger.info("New locale set to: %s", locale.setlocale(locale.LC_ALL, new_locale)) - - - + + + reload(sys) sys.setdefaultencoding("UTF-8") current_locale_encoding = locale.getlocale()[1].lower() logger.debug("sys default encoding %s", sys.getdefaultencoding()) logger.debug("After %s", locale.nl_langinfo(locale.CODESET)) - + if current_locale_encoding not in ['utf-8', 'utf8']: logger.error("Need a UTF-8 locale. Currently '%s'. Exiting..." % current_locale_encoding) sys.exit(1) @@ -92,7 +90,7 @@ try: except Exception, e: print "Couldn't configure logging" sys.exit() - + configure_locale() # loading config file @@ -105,11 +103,11 @@ except Exception, e: class Global: def __init__(self): self.api_client = api_client.api_client_factory(config) - + def selfcheck(self): self.api_client = api_client.api_client_factory(config) return self.api_client.is_server_compatible() - + def test_api(self): self.api_client.test() @@ -160,7 +158,7 @@ if __name__ == '__main__': g = Global() while not g.selfcheck(): time.sleep(5) - + logger = logging.getLogger() if options.test: @@ -173,9 +171,9 @@ if __name__ == '__main__': pypoFetch_q = Queue() recorder_q = Queue() pypoPush_q = Queue() - + telnet_lock = Lock() - + """ This queue is shared between pypo-fetch and pypo-file, where pypo-file is the receiver. Pypo-fetch will send every schedule it gets to pypo-file @@ -183,19 +181,19 @@ if __name__ == '__main__': priority, and will retrieve it. """ media_q = Queue() - + pmh = PypoMessageHandler(pypoFetch_q, recorder_q) pmh.daemon = True pmh.start() - + pfile = PypoFile(media_q) pfile.daemon = True pfile.start() - + pf = PypoFetch(pypoFetch_q, pypoPush_q, media_q, telnet_lock) pf.daemon = True pf.start() - + pp = PypoPush(pypoPush_q, telnet_lock) pp.daemon = True pp.start() @@ -210,7 +208,7 @@ if __name__ == '__main__': #recorder.join() #pp.join() pf.join() - + logger.info("pypo fetch exit") sys.exit() """ diff --git a/python_apps/pypo/pypofetch.py b/python_apps/pypo/pypofetch.py index b9cb6e76b..2e3ab4280 100644 --- a/python_apps/pypo/pypofetch.py +++ b/python_apps/pypo/pypofetch.py @@ -3,9 +3,7 @@ import os import sys import time -import logging import logging.config -import shutil import json import telnetlib import copy @@ -44,11 +42,11 @@ class PypoFetch(Thread): self.media_prepare_queue = media_q self.last_update_schedule_timestamp = time.time() self.listener_timeout = 3600 - + self.telnet_lock = telnet_lock - + self.logger = logging.getLogger(); - + self.cache_dir = os.path.join(config["cache_dir"], "scheduler") self.logger.debug("Cache dir %s", self.cache_dir) @@ -63,24 +61,24 @@ class PypoFetch(Thread): os.makedirs(dir) except Exception, e: pass - + self.schedule_data = [] self.logger.info("PypoFetch: init complete") - + """ Handle a message from RabbitMQ, put it into our yucky global var. Hopefully there is a better way to do this. """ def handle_message(self, message): - try: + try: self.logger.info("Received event from Pypo Message Handler: %s" % message) - - m = json.loads(message) + + m = json.loads(message) command = m['event_type'] self.logger.info("Handling command: " + command) - + if command == 'update_schedule': - self.schedule_data = m['schedule'] + self.schedule_data = m['schedule'] self.process_schedule(self.schedule_data) elif command == 'update_stream_setting': self.logger.info("Updating stream setting...") @@ -100,7 +98,7 @@ class PypoFetch(Thread): elif command == 'disconnect_source': self.logger.info("disconnect_on_source show command received...") self.disconnect_source(self.logger, self.telnet_lock, m['sourcename']) - + # update timeout value if command == 'update_schedule': self.listener_timeout = 3600 @@ -115,7 +113,7 @@ class PypoFetch(Thread): self.logger.error('Exception: %s', e) self.logger.error("traceback: %s", top) self.logger.error("Exception in handling Message Handler message: %s", e) - + @staticmethod def disconnect_source(logger, lock, sourcename): logger.debug('Disconnecting source: %s', sourcename) @@ -124,7 +122,7 @@ class PypoFetch(Thread): command += "master_harbor.kick\n" elif(sourcename == "live_dj"): command += "live_dj_harbor.kick\n" - + lock.acquire() try: tn = telnetlib.Telnet(LS_HOST, LS_PORT) @@ -135,7 +133,7 @@ class PypoFetch(Thread): logger.error(str(e)) finally: lock.release() - + @staticmethod def switch_source(logger, lock, sourcename, status): logger.debug('Switching source: %s to "%s" status', sourcename, status) @@ -146,12 +144,12 @@ class PypoFetch(Thread): command += "live_dj_" elif(sourcename == "scheduled_play"): command += "scheduled_play_" - + if(status == "on"): command += "start\n" else: command += "stop\n" - + lock.acquire() try: tn = telnetlib.Telnet(LS_HOST, LS_PORT) @@ -162,7 +160,7 @@ class PypoFetch(Thread): logger.error(str(e)) finally: lock.release() - + """ grabs some information that are needed to be set on bootstrap time and configures them @@ -174,13 +172,13 @@ class PypoFetch(Thread): self.logger.error('Unable to get bootstrap info.. Existing pypo...') sys.exit(0) else: - self.logger.debug('info:%s',info) + self.logger.debug('info:%s', info) for k, v in info['switch_status'].iteritems(): self.switch_source(self.logger, self.telnet_lock, k, v) self.update_liquidsoap_stream_format(info['stream_label']) self.update_liquidsoap_station_name(info['station_name']) self.update_liquidsoap_transition_fade(info['transition_fade']) - + def write_liquidsoap_config(self, setting): fh = open('/etc/airtime/liquidsoap.cfg', 'w') self.logger.info("Rewriting liquidsoap.cfg...") @@ -197,7 +195,7 @@ class PypoFetch(Thread): if temp == "": temp = "0" buffer_str += temp - + buffer_str += "\n" fh.write(api_client.encode_to(buffer_str)) fh.write("log_file = \"/var/log/airtime/pypo-liquidsoap/