Skip to content
Snippets Groups Projects
guru.py 6.29 KiB
Newer Older
Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
# own libs
from libraries.base.config import ConfigReader
from modules.tools.padavan import Padavan
from libraries.exceptions.auraexceptions import FallbackException

class Guru:
    config = ConfigReader()
    config.loadConfig()

    # ------------------------------------------------------------------------------------------ #
    def __init__(self):
        try:
            parser = ArgumentParser()

Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
            # 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")
            parser.add_argument("-q", "--quiet", action="store_true", dest="quiet", default=False,
                                help="Just the result will outputed to stout")
Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed

Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
            parser.add_argument("-gam", "--get-active-mixer", action="store_true", dest="get_active_mixer", default=False,
            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's avatar
Gottfried Gaisbauer committed
            parser.add_argument("-am", "--select-mixer", action="store", dest="select_mixer", default=-1, metavar="MIXERNUM",
                                help="Which mixer should be activated?", type=int)

Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
            parser.add_argument("-dm", "--de-select-mixer", action="store", dest="deselect_mixer", default=-1, metavar="MIXERNUM",
                                help="Which mixer should be activated?", type=int)

Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
            parser.add_argument("-vm", "--volume", action="store", dest="set_volume", default=0, metavar=("MIXERNUM","VOLUME"), nargs=2,
                                help="Set volume of a mixer source", type=int)

            #parser.add_argument("-as", "--add-source", action="store", dest="add_source", default=False,
            #                    help="Add new source to LiquidSoap mixer [Experimental]")
            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")
            parser.add_argument("-spe", "--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",
            parser.add_argument("-ipe", "--insert-playlist-entry", action="store", dest="insert_playlist_entry", default=0, metavar=("FROMTIME", "SOURCE"), nargs=2,
                                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")

            # calls from liquidsoap
            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?")
            parser.add_argument("-ip", "--init-player", action="store_true", dest="init_player", default=False,
                                help="Reset liquidsoap volume and mixer activations?")

            if len(sys.argv) == 1:
                raise ValueError("No Argument passed!")

        except ValueError as e:
            parser.print_help()
            print()
            print(e)
        except TypeError as e:
            parser.print_help()
            print()
            print(e)
Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
        if args.stoptime:
            start = time.time()

        try:
            p = Padavan(args, self.config)
            p.meditate()
        except FallbackException as fe:
            print(fe)
            exit(4)
        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: ")
        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)])
Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
        if args.stoptime:
            end = time.time()
            exectime = end-start
            print("execution time: "+str(exectime)+"s")

        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])
# # ## ## ## ## ## # #
# # ENTRY FUNCTION # #
# # ## ## ## ## ## # #
def main():
Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
    Guru()
# # ## ## ## ## ## ## # #
# # End ENTRY FUNCTION # #
# # ## ## ## ## ## ## # #