♻️ (pypo) cleanup pypo/__main__.py
Clean up code and add python 3 support (with python 2 bc).
This commit is contained in:
parent
cb0daddaf5
commit
36891c18c8
|
@ -1,39 +1,36 @@
|
|||
"""
|
||||
Python part of radio playout (pypo)
|
||||
"""
|
||||
from __future__ import absolute_import
|
||||
|
||||
from optparse import OptionParser
|
||||
from datetime import datetime
|
||||
|
||||
import telnetlib
|
||||
|
||||
import time
|
||||
import sys
|
||||
import signal
|
||||
import logging
|
||||
import locale
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import signal
|
||||
import sys
|
||||
import telnetlib
|
||||
import time
|
||||
|
||||
from Queue import Queue
|
||||
from api_clients import api_client
|
||||
from configobj import ConfigObj
|
||||
from datetime import datetime
|
||||
from optparse import OptionParser
|
||||
from queue import Queue
|
||||
from threading import Lock
|
||||
|
||||
from pypopush import PypoPush
|
||||
from pypofetch import PypoFetch
|
||||
from pypofile import PypoFile
|
||||
from recorder import Recorder
|
||||
from listenerstat import ListenerStat
|
||||
from pypomessagehandler import PypoMessageHandler
|
||||
from pypoliquidsoap import PypoLiquidsoap
|
||||
from timeout import ls_timeout
|
||||
from .listenerstat import ListenerStat
|
||||
from .pypofetch import PypoFetch
|
||||
from .pypofile import PypoFile
|
||||
from .pypoliquidsoap import PypoLiquidsoap
|
||||
from .pypomessagehandler import PypoMessageHandler
|
||||
from .pypopush import PypoPush
|
||||
from .recorder import Recorder
|
||||
from .timeout import ls_timeout
|
||||
from . import pure
|
||||
|
||||
from configobj import ConfigObj
|
||||
|
||||
# custom imports
|
||||
from api_clients import api_client
|
||||
import pure
|
||||
|
||||
LOG_PATH = '/var/log/airtime/pypo/pypo.log'
|
||||
LOG_PATH = "/var/log/airtime/pypo/pypo.log"
|
||||
LOG_LEVEL = logging.INFO
|
||||
logging.captureWarnings(True)
|
||||
|
||||
|
@ -45,42 +42,54 @@ 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(
|
||||
"-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(
|
||||
"-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")
|
||||
parser.add_option(
|
||||
"-b",
|
||||
"--cleanup",
|
||||
help="Cleanup",
|
||||
default=False,
|
||||
action="store_true",
|
||||
dest="cleanup",
|
||||
)
|
||||
|
||||
parser.add_option("-c",
|
||||
"--check",
|
||||
help="Check the cached schedule and exit",
|
||||
default=False,
|
||||
action="store_true",
|
||||
dest="check")
|
||||
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()
|
||||
|
||||
LIQUIDSOAP_MIN_VERSION = "1.1.1"
|
||||
|
||||
PYPO_HOME='/var/tmp/airtime/pypo/'
|
||||
PYPO_HOME = "/var/tmp/airtime/pypo/"
|
||||
|
||||
|
||||
def configure_environment():
|
||||
os.environ["HOME"] = PYPO_HOME
|
||||
os.environ["TERM"] = 'xterm'
|
||||
os.environ["TERM"] = "xterm"
|
||||
|
||||
|
||||
configure_environment()
|
||||
|
||||
|
@ -90,23 +99,27 @@ logging.captureWarnings(True)
|
|||
# configure logging
|
||||
try:
|
||||
# Set up logging
|
||||
logFormatter = logging.Formatter("%(asctime)s [%(module)s] [%(levelname)-5.5s] %(message)s")
|
||||
logFormatter = logging.Formatter(
|
||||
"%(asctime)s [%(module)s] [%(levelname)-5.5s] %(message)s"
|
||||
)
|
||||
rootLogger = logging.getLogger()
|
||||
rootLogger.setLevel(LOG_LEVEL)
|
||||
logger = rootLogger
|
||||
|
||||
fileHandler = logging.handlers.RotatingFileHandler(filename=LOG_PATH, maxBytes=1024 * 1024 * 30,
|
||||
backupCount=8)
|
||||
fileHandler = logging.handlers.RotatingFileHandler(
|
||||
filename=LOG_PATH, maxBytes=1024 * 1024 * 30, backupCount=8
|
||||
)
|
||||
fileHandler.setFormatter(logFormatter)
|
||||
rootLogger.addHandler(fileHandler)
|
||||
|
||||
consoleHandler = logging.StreamHandler()
|
||||
consoleHandler.setFormatter(logFormatter)
|
||||
rootLogger.addHandler(consoleHandler)
|
||||
except Exception, e:
|
||||
print "Couldn't configure logging", e
|
||||
except Exception as e:
|
||||
print("Couldn't configure logging", e)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def configure_locale():
|
||||
"""
|
||||
Silly hacks to force Python 2.x to run in UTF-8 mode. Not portable at all,
|
||||
|
@ -123,21 +136,26 @@ def configure_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")
|
||||
logger.debug(
|
||||
"No default locale exists. Let's try loading from \
|
||||
/etc/default/locale"
|
||||
)
|
||||
if os.path.exists("/etc/default/locale"):
|
||||
locale_config = ConfigObj('/etc/default/locale')
|
||||
lang = locale_config.get('LANG')
|
||||
locale_config = ConfigObj("/etc/default/locale")
|
||||
lang = locale_config.get("LANG")
|
||||
new_locale = lang
|
||||
else:
|
||||
logger.error("/etc/default/locale could not be found! Please \
|
||||
run 'sudo update-locale' from command-line.")
|
||||
logger.error(
|
||||
"/etc/default/locale could not be found! Please \
|
||||
run 'sudo update-locale' from command-line."
|
||||
)
|
||||
sys.exit(1)
|
||||
else:
|
||||
new_locale = default_locale
|
||||
|
||||
logger.info("New locale set to: %s", \
|
||||
locale.setlocale(locale.LC_ALL, new_locale))
|
||||
logger.info(
|
||||
"New locale set to: %s", locale.setlocale(locale.LC_ALL, new_locale)
|
||||
)
|
||||
|
||||
reload(sys)
|
||||
sys.setdefaultencoding("UTF-8")
|
||||
|
@ -145,9 +163,10 @@ def configure_locale():
|
|||
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)
|
||||
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)
|
||||
|
||||
|
||||
|
@ -155,11 +174,12 @@ configure_locale()
|
|||
|
||||
# loading config file
|
||||
try:
|
||||
config = ConfigObj('/etc/airtime/airtime.conf')
|
||||
except Exception, e:
|
||||
logger.error('Error loading config file: %s', e)
|
||||
config = ConfigObj("/etc/airtime/airtime.conf")
|
||||
except Exception as e:
|
||||
logger.error("Error loading config file: %s", e)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
class Global:
|
||||
def __init__(self, api_client):
|
||||
self.api_client = api_client
|
||||
|
@ -170,11 +190,13 @@ class Global:
|
|||
def test_api(self):
|
||||
self.api_client.test()
|
||||
|
||||
|
||||
def keyboardInterruptHandler(signum, frame):
|
||||
logger = logging.getLogger()
|
||||
logger.info('\nKeyboard Interrupt\n')
|
||||
logger.info("\nKeyboard Interrupt\n")
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
@ls_timeout
|
||||
def liquidsoap_get_info(telnet_lock, host, port, logger):
|
||||
logger.debug("Checking to see if Liquidsoap is running")
|
||||
|
@ -185,7 +207,7 @@ def liquidsoap_get_info(telnet_lock, host, port, logger):
|
|||
tn.write(msg)
|
||||
tn.write("exit\n")
|
||||
response = tn.read_all()
|
||||
except Exception, e:
|
||||
except Exception as e:
|
||||
logger.error(str(e))
|
||||
return None
|
||||
finally:
|
||||
|
@ -193,6 +215,7 @@ def liquidsoap_get_info(telnet_lock, host, port, logger):
|
|||
|
||||
return get_liquidsoap_version(response)
|
||||
|
||||
|
||||
def get_liquidsoap_version(version_string):
|
||||
m = re.match(r"Liquidsoap (\d+.\d+.\d+)", version_string)
|
||||
|
||||
|
@ -201,42 +224,49 @@ def get_liquidsoap_version(version_string):
|
|||
else:
|
||||
return None
|
||||
|
||||
|
||||
if m:
|
||||
current_version = m.group(1)
|
||||
return pure.version_cmp(current_version, LIQUIDSOAP_MIN_VERSION) >= 0
|
||||
return False
|
||||
|
||||
|
||||
def liquidsoap_startup_test():
|
||||
|
||||
liquidsoap_version_string = \
|
||||
liquidsoap_get_info(telnet_lock, ls_host, ls_port, logger)
|
||||
liquidsoap_version_string = liquidsoap_get_info(
|
||||
telnet_lock, ls_host, ls_port, logger
|
||||
)
|
||||
while not liquidsoap_version_string:
|
||||
logger.warning("Liquidsoap doesn't appear to be running!, " + \
|
||||
"Sleeping and trying again")
|
||||
logger.warning(
|
||||
"Liquidsoap doesn't appear to be running!, " + "Sleeping and trying again"
|
||||
)
|
||||
time.sleep(1)
|
||||
liquidsoap_version_string = \
|
||||
liquidsoap_get_info(telnet_lock, ls_host, ls_port, logger)
|
||||
liquidsoap_version_string = liquidsoap_get_info(
|
||||
telnet_lock, ls_host, ls_port, logger
|
||||
)
|
||||
|
||||
while pure.version_cmp(liquidsoap_version_string, LIQUIDSOAP_MIN_VERSION) < 0:
|
||||
logger.warning("Liquidsoap is running but in incorrect version! " + \
|
||||
"Make sure you have at least Liquidsoap %s installed" % LIQUIDSOAP_MIN_VERSION)
|
||||
logger.warning(
|
||||
"Liquidsoap is running but in incorrect version! "
|
||||
+ "Make sure you have at least Liquidsoap %s installed"
|
||||
% LIQUIDSOAP_MIN_VERSION
|
||||
)
|
||||
time.sleep(1)
|
||||
liquidsoap_version_string = \
|
||||
liquidsoap_get_info(telnet_lock, ls_host, ls_port, logger)
|
||||
liquidsoap_version_string = liquidsoap_get_info(
|
||||
telnet_lock, ls_host, ls_port, logger
|
||||
)
|
||||
|
||||
logger.info("Liquidsoap version string found %s" % liquidsoap_version_string)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
logger.info('###########################################')
|
||||
logger.info('# *** pypo *** #')
|
||||
logger.info('# Liquidsoap Scheduled Playout System #')
|
||||
logger.info('###########################################')
|
||||
if __name__ == "__main__":
|
||||
logger.info("###########################################")
|
||||
logger.info("# *** pypo *** #")
|
||||
logger.info("# Liquidsoap Scheduled Playout System #")
|
||||
logger.info("###########################################")
|
||||
|
||||
#Although all of our calculations are in UTC, it is useful to know what timezone
|
||||
#the local machine is, so that we have a reference for what time the actual
|
||||
#log entries were made
|
||||
# Although all of our calculations are in UTC, it is useful to know what timezone
|
||||
# the local machine is, so that we have a reference for what time the actual
|
||||
# log entries were made
|
||||
logger.info("Timezone: %s" % str(time.tzname))
|
||||
logger.info("UTC time: %s" % str(datetime.utcnow()))
|
||||
|
||||
|
@ -251,16 +281,16 @@ if __name__ == '__main__':
|
|||
success = False
|
||||
while not success:
|
||||
try:
|
||||
api_client.register_component('pypo')
|
||||
api_client.register_component("pypo")
|
||||
success = True
|
||||
except Exception, e:
|
||||
except Exception as e:
|
||||
logger.error(str(e))
|
||||
time.sleep(10)
|
||||
|
||||
telnet_lock = Lock()
|
||||
|
||||
ls_host = config['pypo']['ls_host']
|
||||
ls_port = config['pypo']['ls_port']
|
||||
ls_host = config["pypo"]["ls_host"]
|
||||
ls_port = config["pypo"]["ls_port"]
|
||||
|
||||
liquidsoap_startup_test()
|
||||
|
||||
|
@ -272,8 +302,7 @@ if __name__ == '__main__':
|
|||
recorder_q = Queue()
|
||||
pypoPush_q = Queue()
|
||||
|
||||
pypo_liquidsoap = PypoLiquidsoap(logger, telnet_lock,\
|
||||
ls_host, ls_port)
|
||||
pypo_liquidsoap = PypoLiquidsoap(logger, telnet_lock, ls_host, ls_port)
|
||||
|
||||
"""
|
||||
This queue is shared between pypo-fetch and pypo-file, where pypo-file
|
||||
|
@ -284,19 +313,21 @@ if __name__ == '__main__':
|
|||
media_q = Queue()
|
||||
|
||||
# Pass only the configuration sections needed; PypoMessageHandler only needs rabbitmq settings
|
||||
pmh = PypoMessageHandler(pypoFetch_q, recorder_q, config['rabbitmq'])
|
||||
pmh = PypoMessageHandler(pypoFetch_q, recorder_q, config["rabbitmq"])
|
||||
pmh.daemon = True
|
||||
pmh.start()
|
||||
|
||||
pfile = PypoFile(media_q, config['pypo'])
|
||||
pfile = PypoFile(media_q, config["pypo"])
|
||||
pfile.daemon = True
|
||||
pfile.start()
|
||||
|
||||
pf = PypoFetch(pypoFetch_q, pypoPush_q, media_q, telnet_lock, pypo_liquidsoap, config['pypo'])
|
||||
pf = PypoFetch(
|
||||
pypoFetch_q, pypoPush_q, media_q, telnet_lock, pypo_liquidsoap, config["pypo"]
|
||||
)
|
||||
pf.daemon = True
|
||||
pf.start()
|
||||
|
||||
pp = PypoPush(pypoPush_q, telnet_lock, pypo_liquidsoap, config['pypo'])
|
||||
pp = PypoPush(pypoPush_q, telnet_lock, pypo_liquidsoap, config["pypo"])
|
||||
pp.daemon = True
|
||||
pp.start()
|
||||
|
||||
|
|
|
@ -51,6 +51,7 @@ setup(name='airtime-playout',
|
|||
'argparse',
|
||||
'configobj',
|
||||
'docopt',
|
||||
'future',
|
||||
'kombu',
|
||||
'mutagen',
|
||||
'poster',
|
||||
|
|
Loading…
Reference in New Issue