sintonia/python_apps/pypo/pypocli.py

175 lines
4.7 KiB
Python
Raw Normal View History

2010-11-05 15:54:15 +01:00
"""
Python part of radio playout (pypo)
"""
2010-11-05 15:54:15 +01:00
import time
from optparse import *
import sys
import os
import signal
2010-11-05 15:54:15 +01:00
import logging
import logging.config
import logging.handlers
from Queue import Queue
from pypopush import PypoPush
from pypofetch import PypoFetch
from pypofile import PypoFile
from recorder import Recorder
from pypomessagehandler import PypoMessageHandler
2010-11-05 15:54:15 +01:00
from configobj import ConfigObj
# custom imports
from api_clients import api_client
2010-11-05 15:54:15 +01:00
2011-03-02 23:26:39 +01:00
PYPO_VERSION = '1.1'
2010-11-05 15:54:15 +01:00
# Set up command-line options
2010-11-05 15:54:15 +01:00
parser = OptionParser()
# help screen / info
2010-11-05 15:54:15 +01:00
usage = "%prog [options]" + " - python playout system"
parser = OptionParser(usage=usage)
# Options
parser.add_option("-v", "--compat", help="Check compatibility with server API version", default=False, action="store_true", dest="check_compat")
parser.add_option("-t", "--test", help="Do a test to make sure everything is working properly.", default=False, action="store_true", dest="test")
parser.add_option("-b", "--cleanup", help="Cleanup", default=False, action="store_true", dest="cleanup")
2010-11-05 15:54:15 +01:00
parser.add_option("-c", "--check", help="Check the cached schedule and exit", default=False, action="store_true", dest="check")
# parse options
(options, args) = parser.parse_args()
# configure logging
logging.config.fileConfig("logging.cfg")
# loading config file
try:
config = ConfigObj('/etc/airtime/pypo.cfg')
2010-11-05 15:54:15 +01:00
except Exception, e:
logger = logging.getLogger()
logger.error('Error loading config file: %s', e)
2010-11-05 15:54:15 +01:00
sys.exit()
2010-11-05 15:54:15 +01:00
class Global:
def __init__(self):
2011-03-02 23:26:39 +01:00
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()
2011-03-02 23:26:39 +01:00
def test_api(self):
self.api_client.test()
"""
def check_schedule(self):
2011-03-02 23:26:39 +01:00
logger = logging.getLogger()
try:
schedule_file = open(self.schedule_file, "r")
schedule = pickle.load(schedule_file)
schedule_file.close()
except Exception, e:
logger.error("%s", e)
schedule = None
for pkey in sorted(schedule.iterkeys()):
playlist = schedule[pkey]
print '*****************************************'
print '\033[0;32m%s %s\033[m' % ('scheduled at:', str(pkey))
print 'cached at : ' + self.cache_dir + str(pkey)
print 'played: ' + str(playlist['played'])
print 'schedule id: ' + str(playlist['schedule_id'])
print 'duration: ' + str(playlist['duration'])
print 'source id: ' + str(playlist['x_ident'])
print '-----------------------------------------'
for media in playlist['medias']:
print media
"""
def keyboardInterruptHandler(signum, frame):
logger = logging.getLogger()
logger.info('\nKeyboard Interrupt\n')
sys.exit(0)
2011-03-02 23:26:39 +01:00
2010-11-05 15:54:15 +01:00
if __name__ == '__main__':
logger = logging.getLogger()
logger.info('###########################################')
logger.info('# *** pypo *** #')
logger.info('# Liquidsoap Scheduled Playout System #')
logger.info('###########################################')
signal.signal(signal.SIGINT, keyboardInterruptHandler)
2010-11-05 15:54:15 +01:00
# initialize
g = Global()
while not g.selfcheck(): time.sleep(5)
2011-03-02 23:26:39 +01:00
logger = logging.getLogger()
2010-11-05 15:54:15 +01:00
if options.test:
g.test_api()
sys.exit()
api_client = api_client.api_client_factory(config)
api_client.register_component("pypo")
pypoFetch_q = Queue()
recorder_q = Queue()
pypoPush_q = Queue()
"""
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
and pypo will parse this schedule to determine which file has the highest
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)
pf.daemon = True
pf.start()
pp = PypoPush(pypoPush_q)
pp.daemon = True
pp.start()
recorder = Recorder(recorder_q)
recorder.daemon = True
recorder.start()
pmh.join()
pfile.join()
pf.join()
pp.join()
recorder.join()
logger.info("pypo fetch exit")
sys.exit()
"""
if options.check:
try: g.check_schedule()
except Exception, e:
print e
if options.cleanup:
try: pf.cleanup('scheduler')
except Exception, e:
print e
"""