clean up indentation + imports for python apps
This commit is contained in:
parent
b6483cd952
commit
0a3c9c7351
7 changed files with 183 additions and 212 deletions
|
@ -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/<script>.log\"\n");
|
||||
|
@ -206,18 +204,18 @@ class PypoFetch(Thread):
|
|||
# we could just restart liquidsoap but it take more time somehow.
|
||||
self.logger.info("Restarting pypo...")
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
def regenerateLiquidsoapConf(self, setting):
|
||||
existing = {}
|
||||
# create a temp file
|
||||
|
||||
|
||||
setting = sorted(setting.items())
|
||||
try:
|
||||
fh = open('/etc/airtime/liquidsoap.cfg', 'r')
|
||||
except IOError, e:
|
||||
#file does not exist
|
||||
self.write_liquidsoap_config(setting)
|
||||
|
||||
|
||||
self.logger.info("Reading existing config...")
|
||||
# read existing conf file and build dict
|
||||
while True:
|
||||
|
@ -226,9 +224,9 @@ class PypoFetch(Thread):
|
|||
# empty line means EOF
|
||||
if not line:
|
||||
break
|
||||
|
||||
|
||||
line = line.strip()
|
||||
|
||||
|
||||
if line[0] == "#":
|
||||
continue
|
||||
|
||||
|
@ -243,7 +241,7 @@ class PypoFetch(Thread):
|
|||
value = ''
|
||||
existing[key] = value
|
||||
fh.close()
|
||||
|
||||
|
||||
# dict flag for any change in cofig
|
||||
change = {}
|
||||
# this flag is to detect disable -> disable change
|
||||
|
@ -251,7 +249,7 @@ class PypoFetch(Thread):
|
|||
state_change_restart = {}
|
||||
#restart flag
|
||||
restart = False
|
||||
|
||||
|
||||
self.logger.info("Looking for changes...")
|
||||
# look for changes
|
||||
for k, s in setting:
|
||||
|
@ -267,13 +265,13 @@ class PypoFetch(Thread):
|
|||
self.logger.info("'Need-to-restart' state detected for %s...", s[u'keyname'])
|
||||
restart = True;
|
||||
else:
|
||||
stream, dump = s[u'keyname'].split('_',1)
|
||||
stream, dump = s[u'keyname'].split('_', 1)
|
||||
if "_output" in s[u'keyname']:
|
||||
if (existing[s[u'keyname']] != s[u'value']):
|
||||
self.logger.info("'Need-to-restart' state detected for %s...", s[u'keyname'])
|
||||
restart = True;
|
||||
state_change_restart[stream] = True
|
||||
elif ( s[u'value'] != 'disabled'):
|
||||
elif (s[u'value'] != 'disabled'):
|
||||
state_change_restart[stream] = True
|
||||
else:
|
||||
state_change_restart[stream] = False
|
||||
|
@ -284,10 +282,10 @@ class PypoFetch(Thread):
|
|||
if not (s[u'value'] == existing[s[u'keyname']]):
|
||||
self.logger.info("Keyname: %s, Curent value: %s, New Value: %s", s[u'keyname'], existing[s[u'keyname']], s[u'value'])
|
||||
change[stream] = True
|
||||
|
||||
|
||||
# set flag change for sound_device alway True
|
||||
self.logger.info("Change:%s, State_Change:%s...", change, state_change_restart)
|
||||
|
||||
|
||||
for k, v in state_change_restart.items():
|
||||
if k == "sound_device" and v:
|
||||
restart = True
|
||||
|
@ -306,7 +304,7 @@ class PypoFetch(Thread):
|
|||
updates the status of liquidsoap connection to the streaming server
|
||||
This fucntion updates the bootup time variable in liquidsoap script
|
||||
"""
|
||||
|
||||
|
||||
self.telnet_lock.acquire()
|
||||
try:
|
||||
tn = telnetlib.Telnet(LS_HOST, LS_PORT)
|
||||
|
@ -314,25 +312,25 @@ class PypoFetch(Thread):
|
|||
# we are manually adjusting the bootup time variable so the status msg will get
|
||||
# updated.
|
||||
current_time = time.time()
|
||||
boot_up_time_command = "vars.bootup_time "+str(current_time)+"\n"
|
||||
boot_up_time_command = "vars.bootup_time " + str(current_time) + "\n"
|
||||
tn.write(boot_up_time_command)
|
||||
tn.write("streams.connection_status\n")
|
||||
tn.write('exit\n')
|
||||
|
||||
|
||||
output = tn.read_all()
|
||||
except Exception, e:
|
||||
self.logger.error(str(e))
|
||||
finally:
|
||||
self.telnet_lock.release()
|
||||
|
||||
|
||||
output_list = output.split("\r\n")
|
||||
stream_info = output_list[2]
|
||||
|
||||
|
||||
# streamin info is in the form of:
|
||||
# eg. s1:true,2:true,3:false
|
||||
streams = stream_info.split(",")
|
||||
self.logger.info(streams)
|
||||
|
||||
|
||||
fake_time = current_time + 1
|
||||
for s in streams:
|
||||
info = s.split(':')
|
||||
|
@ -340,7 +338,7 @@ class PypoFetch(Thread):
|
|||
status = info[1]
|
||||
if(status == "true"):
|
||||
self.api_client.notify_liquidsoap_status("OK", stream_id, str(fake_time))
|
||||
|
||||
|
||||
def update_liquidsoap_stream_format(self, stream_format):
|
||||
# Push stream metadata to liquidsoap
|
||||
# TODO: THIS LIQUIDSOAP STUFF NEEDS TO BE MOVED TO PYPO-PUSH!!!
|
||||
|
@ -356,7 +354,7 @@ class PypoFetch(Thread):
|
|||
self.logger.error("Exception %s", e)
|
||||
finally:
|
||||
self.telnet_lock.release()
|
||||
|
||||
|
||||
def update_liquidsoap_transition_fade(self, fade):
|
||||
# Push stream metadata to liquidsoap
|
||||
# TODO: THIS LIQUIDSOAP STUFF NEEDS TO BE MOVED TO PYPO-PUSH!!!
|
||||
|
@ -372,14 +370,14 @@ class PypoFetch(Thread):
|
|||
self.logger.error("Exception %s", e)
|
||||
finally:
|
||||
self.telnet_lock.release()
|
||||
|
||||
|
||||
def update_liquidsoap_station_name(self, station_name):
|
||||
# Push stream metadata to liquidsoap
|
||||
# TODO: THIS LIQUIDSOAP STUFF NEEDS TO BE MOVED TO PYPO-PUSH!!!
|
||||
try:
|
||||
self.logger.info(LS_HOST)
|
||||
self.logger.info(LS_PORT)
|
||||
|
||||
|
||||
self.telnet_lock.acquire()
|
||||
try:
|
||||
tn = telnetlib.Telnet(LS_HOST, LS_PORT)
|
||||
|
@ -387,7 +385,7 @@ class PypoFetch(Thread):
|
|||
self.logger.info(command)
|
||||
tn.write(command)
|
||||
tn.write('exit\n')
|
||||
tn.read_all()
|
||||
tn.read_all()
|
||||
except Exception, e:
|
||||
self.logger.error(str(e))
|
||||
finally:
|
||||
|
@ -403,7 +401,7 @@ class PypoFetch(Thread):
|
|||
to the cache dir (Folder-structure: cache/YYYY-MM-DD-hh-mm-ss)
|
||||
- runs the cleanup routine, to get rid of unused cached files
|
||||
"""
|
||||
def process_schedule(self, schedule_data):
|
||||
def process_schedule(self, schedule_data):
|
||||
self.last_update_schedule_timestamp = time.time()
|
||||
self.logger.debug(schedule_data)
|
||||
media = schedule_data["media"]
|
||||
|
@ -411,7 +409,7 @@ class PypoFetch(Thread):
|
|||
|
||||
# Download all the media and put playlists in liquidsoap "annotate" format
|
||||
try:
|
||||
|
||||
|
||||
"""
|
||||
Make sure cache_dir exists
|
||||
"""
|
||||
|
@ -420,15 +418,15 @@ class PypoFetch(Thread):
|
|||
os.makedirs(download_dir)
|
||||
except Exception, e:
|
||||
pass
|
||||
|
||||
|
||||
for key in media:
|
||||
media_item = media[key]
|
||||
if(media_item['type'] == 'file'):
|
||||
fileExt = os.path.splitext(media_item['uri'])[1]
|
||||
dst = os.path.join(download_dir, media_item['id']+fileExt)
|
||||
dst = os.path.join(download_dir, media_item['id'] + fileExt)
|
||||
media_item['dst'] = dst
|
||||
media_filtered[key] = media_item
|
||||
|
||||
|
||||
self.media_prepare_queue.put(copy.copy(media_filtered))
|
||||
except Exception, e: self.logger.error("%s", e)
|
||||
|
||||
|
@ -440,7 +438,7 @@ class PypoFetch(Thread):
|
|||
# cleanup
|
||||
try: self.cache_cleanup(media)
|
||||
except Exception, e: self.logger.error("%s", e)
|
||||
|
||||
|
||||
def cache_cleanup(self, media):
|
||||
"""
|
||||
Get list of all files in the cache dir and remove them if they aren't being used anymore.
|
||||
|
@ -449,18 +447,18 @@ class PypoFetch(Thread):
|
|||
"""
|
||||
cached_file_set = set(os.listdir(self.cache_dir))
|
||||
scheduled_file_set = set()
|
||||
|
||||
|
||||
for mkey in media:
|
||||
media_item = media[mkey]
|
||||
fileExt = os.path.splitext(media_item['uri'])[1]
|
||||
scheduled_file_set.add(media_item["id"] + fileExt)
|
||||
|
||||
|
||||
unneeded_files = cached_file_set - scheduled_file_set
|
||||
|
||||
|
||||
self.logger.debug("Files to remove " + str(unneeded_files))
|
||||
for file in unneeded_files:
|
||||
self.logger.debug("Removing %s" % os.path.join(self.cache_dir, file))
|
||||
os.remove(os.path.join(self.cache_dir, file))
|
||||
for f in unneeded_files:
|
||||
self.logger.debug("Removing %s" % os.path.join(self.cache_dir, f))
|
||||
os.remove(os.path.join(self.cache_dir, f))
|
||||
|
||||
def main(self):
|
||||
# Bootstrap: since we are just starting up, we need to grab the
|
||||
|
@ -471,10 +469,10 @@ class PypoFetch(Thread):
|
|||
self.process_schedule(self.schedule_data)
|
||||
self.set_bootstrap_variables()
|
||||
|
||||
loops = 1
|
||||
loops = 1
|
||||
while True:
|
||||
self.logger.info("Loop #%s", loops)
|
||||
try:
|
||||
try:
|
||||
"""
|
||||
our simple_queue.get() requires a timeout, in which case we
|
||||
fetch the Airtime schedule manually. It is important to fetch
|
||||
|
@ -488,8 +486,8 @@ class PypoFetch(Thread):
|
|||
|
||||
Currently we are checking every 3600 seconds (1 hour)
|
||||
"""
|
||||
|
||||
|
||||
|
||||
|
||||
message = self.fetch_queue.get(block=True, timeout=self.listener_timeout)
|
||||
self.handle_message(message)
|
||||
except Exception, e:
|
||||
|
@ -497,7 +495,7 @@ class PypoFetch(Thread):
|
|||
top = traceback.format_exc()
|
||||
self.logger.error('Exception: %s', e)
|
||||
self.logger.error("traceback: %s", top)
|
||||
|
||||
|
||||
success, self.schedule_data = self.api_client.get_schedule()
|
||||
if success:
|
||||
self.process_schedule(self.schedule_data)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue