Newer
Older
#!/usr/bin/python3
import time

Gottfried Gaisbauer
committed
import sys

Gottfried Gaisbauer
committed
import redis
from argparse import ArgumentParser
from libraries.base.config import ConfigReader
from libraries.exceptions.auraexceptions import FallbackException
class Guru:
config = ConfigReader()
config.loadConfig()

Gottfried Gaisbauer
committed
# ------------------------------------------------------------------------------------------ #
def __init__(self):
try:
parser = ArgumentParser()
# commands
parser.add_argument("-fnp", "--fetch-new-programmes", action="store_true", dest="fetch_new_programme",
default=False, help="Fetch new programmes from calendarurl in comba.ini")
# options
parser.add_argument("-sep", "--stop-execution-time", action="store_true", dest="stoptime", default=False,
help="Prints the execution time at the end of the skript")

Gottfried Gaisbauer
committed
parser.add_argument("-q", "--quiet", action="store_true", dest="quiet", default=False,
help="Just the result will outputed to stout")
# getter
parser.add_argument("-gam", "--get-active-mixer", action="store_true", dest="get_active_mixer", default=False,
help="Which mixer is activated?")
parser.add_argument("-pms", "--print-mixer-status", action="store_true", dest="get_mixer_status", default=False,
help="Prints all mixer sources and their states")
parser.add_argument("-pap", "--print-act-programme", action="store_true", dest="get_act_programme", default=False,
help="Prints the actual Programme, the controller holds")

Gottfried Gaisbauer
committed
# liquid manipulation
parser.add_argument("-am", "--select-mixer", action="store", dest="select_mixer", default=-1, metavar="MIXERNUM",
help="Which mixer should be activated?", type=int)
parser.add_argument("-dm", "--de-select-mixer", action="store", dest="deselect_mixer", default=-1, metavar="MIXERNUM",
help="Which mixer should be activated?", type=int)
parser.add_argument("-vm", "--volume", action="store", dest="set_volume", default=0, metavar=("MIXERNUM","VOLUME"), nargs=2,

Gottfried Gaisbauer
committed
help="Set volume of a mixer source", type=int)

Gottfried Gaisbauer
committed
#parser.add_argument("-as", "--add-source", action="store", dest="add_source", default=False,
# help="Add new source to LiquidSoap mixer [Experimental]")

Gottfried Gaisbauer
committed
# playlist manipulation
parser.add_argument("-mpe", "--swap-playlist-entries", action="store", dest="swap_playlist_entries", default=0, metavar=("FROM", "TO"), nargs=2,
help="Swaps two Playlistentries")
parser.add_argument("-dpe", "--delete-playlist-entry", action="store", dest="delete_playlist_entry", default=0, metavar="INDEX",

Gottfried Gaisbauer
committed
help="Delete Playlistentry at INDEX")
parser.add_argument("-ape", "--add-playlist-entry", action="store", dest="add_playlist_entry", default=0, metavar=("INDEX", "FROMTIME", "SOURCE"), nargs=3,
help="Add a new Playlistentry at a given index", type=valid_playlist_entry)
parser.add_argument("-pmq", "--print-message-queue", action="store_true", dest="print_message_queue", default=False,
help="Prints message queue")
# send a redis message
parser.add_argument("-rm", "--redis-message", action="store", dest="redis_message", default=False, metavar=("CHANNEL", "MESSAGE"), nargs=2,
help="Send a redis message to the Listeners")

Gottfried Gaisbauer
committed
parser.add_argument("-gnf", "--get-next-fallback-file-for", action="store", dest="get_fallback_for", default=False, metavar="FALLBACKTYPE",
help="For which type you wanna GET a next audio file?")
parser.add_argument("-snf", "--set-next-fallback-file-for", action="store", dest="set_fallback_for", default=False, metavar=("FALLBACKTYPE", "FILE"), nargs=2,
help="For which type you wanna SET a next audio file?")

Gottfried Gaisbauer
committed
parser.add_argument("-ip", "--init-player", action="store_true", dest="init_player", default=False,
help="Reset liquidsoap volume and mixer activations?")
args = parser.parse_args()
if len(sys.argv) == 1:
raise ValueError("No Argument passed!")

Gottfried Gaisbauer
committed
except ValueError as e:
parser.print_help()
print()
print(e)
exit(1)

Gottfried Gaisbauer
committed
except TypeError as e:
parser.print_help()
print()
print(e)

Gottfried Gaisbauer
committed
exit(2)

Gottfried Gaisbauer
committed
if not args.quiet:
print("Guru thinking...")
try:
p = Padavan(args, self.config)
p.meditate()
except FallbackException as fe:
print(fe)
exit(4)

Gottfried Gaisbauer
committed
except redis.exceptions.TimeoutError as te:
print(te)
print("Timeout when waiting for redis message. Is AURA daemon running? Exiting...")
exit(3)
if not args.quiet:
print("...result: ")

Gottfried Gaisbauer
committed
if p.stringreply != "":
if p.stringreply[len(p.stringreply)-1] == "\n":
print(p.stringreply[0:len(p.stringreply) - 1])
else:
print(p.stringreply[0:len(p.stringreply)])
if args.stoptime:
end = time.time()
exectime = end-start
print("execution time: "+str(exectime)+"s")

Gottfried Gaisbauer
committed
def valid_playlist_entry(argument):
import datetime
import argparse
try:
index = int(argument[0])
fromtime = datetime.strptime(argument[1], "%Y-%m-%d")
source = argument[2]
return index, fromtime, source
except:
msg = "Not a valid date: '{0}'.".format(argument[0])
raise argparse.ArgumentTypeError(msg)
# # ## ## ## ## ## # #
# # ENTRY FUNCTION # #
# # ## ## ## ## ## # #
def main():
# # ## ## ## ## ## ## # #
# # End ENTRY FUNCTION # #
# # ## ## ## ## ## ## # #

Gottfried Gaisbauer
committed
if __name__ == "__main__":
main()