#!/usr/bin/python3 import time import sys import redis from argparse import ArgumentParser # own libs from modules.cli_tool.padavan import Padavan from libraries.exceptions.auraexceptions import PlaylistException from libraries.base.config import AuraConfig class Guru(AuraConfig): parser = None args = None # ------------------------------------------------------------------------------------------ # def __init__(self): super(Guru, self).__init__() self.init_argument_parser() self.handle_arguments() def handle_arguments(self): if self.args.stoptime: start = time.time() if not self.args.quiet: print("Guru thinking...") try: p = Padavan(self.args, self.config) p.meditate() except PlaylistException as pe: # typically there is no next file found if not self.args.quiet: print(pe) else: print("") exit(4) except redis.exceptions.TimeoutError as te: print("Timeout when waiting for redis message. Is AURA daemon running? Exiting...") exit(3) if not self.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)]) if self.args.stoptime: end = time.time() exectime = end-start print("execution time: "+str(exectime)+"s") def init_argument_parser(self): try: self.create_parser() self.args = self.parser.parse_args() except (ValueError, TypeError) as e: if self.parser is not None: self.parser.print_help() print() print(e) exit(1) def create_parser(self): self.parser = ArgumentParser() # options self.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") self.parser.add_argument("-q", "--quiet", action="store_true", dest="quiet", default=False, help="Just the result will outputed to stout") self.parser.add_argument("-rd", "--recreate-database", action="store_true", dest="recreatedb", default=False, help="Do you want to recreate the database?") # getter self.parser.add_argument("-pcs", "--print-connection-status", action="store_true", dest="get_connection_status", default=False, help="Prints the status of the connection to liquidsoap, pv and tank") self.parser.add_argument("-gam", "--get-active-mixer", action="store_true", dest="get_active_mixer", default=False, help="Which mixer is activated?") self.parser.add_argument("-pms", "--print-mixer-status", action="store_true", dest="get_mixer_status", default=False, help="Prints all mixer sources and their states") self.parser.add_argument("-pap", "--print-act-programme", action="store_true", dest="get_act_programme", default=False, help="Prints the actual Programme, the controller holds") # liquid manipulation self.parser.add_argument("-am", "--select-mixer", action="store", dest="select_mixer", default=-1, metavar="MIXERNAME", help="Which mixer should be activated?") self.parser.add_argument("-dm", "--de-select-mixer", action="store", dest="deselect_mixer", default=-1, metavar="MIXERNAME", help="Which mixer should be activated?") self.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]") # playlist in/output self.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") self.parser.add_argument("-pmq", "--print-message-queue", action="store_true", dest="print_message_queue", default=False, help="Prints message queue") # playlist manipulation self.parser.add_argument("-spe", "--swap-playlist-entries", action="store", dest="swap_playlist_entries", default=0, metavar=("FROM", "TO"), nargs=2, help="Swaps the sources of two Playlistentries") self.parser.add_argument("-dpe", "--delete-playlist-entry", action="store", dest="delete_playlist_entry", default=0, metavar="INDEX", nargs=1, help="Delete Playlistentry at INDEX") self.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. Set fromtime with this format: 2017-12-31T13:30:00") # , type=valid_playlist_entry) # send a redis message self.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 self.parser.add_argument("-gnf", "--get-next-file-for", action="store", dest="get_file_for", default=False, metavar="PLAYLISTTYPE", help="For which type you wanna GET a next audio file?") self.parser.add_argument("-snf", "--set-next-file-for", action="store", dest="set_file_for", default=False, metavar=("PLAYLISTTYPE", "FILE"), nargs=2, help="For which type you wanna SET a next audio file?") self.parser.add_argument("-np", "--now-playing", action="store_true", dest="now_playing", default=False, help="Which source is now playing") self.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!") def valid_playlist_entry(argument): from datetime import datetime 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 # # ## ## ## ## ## # # # # ENTRY FUNCTION # # # # ## ## ## ## ## # # def main(): Guru() # # ## ## ## ## ## ## # # # # End ENTRY FUNCTION # # # # ## ## ## ## ## ## # # if __name__ == "__main__": main()