From 154e7563263044ed5c94a256614fc16e5dbfb656 Mon Sep 17 00:00:00 2001 From: Fabio Vitello Date: Thu, 7 Oct 2021 16:55:48 +0200 Subject: [PATCH] #595 raise an exception if arguments are not numeric --- .../NotoPyIfDistributor/src/_ifdist.py | 84 +++++++++++-------- 1 file changed, 48 insertions(+), 36 deletions(-) diff --git a/Noto/Servers/NotoPyIfDistributor/src/_ifdist.py b/Noto/Servers/NotoPyIfDistributor/src/_ifdist.py index 864e56163..6be5741f3 100644 --- a/Noto/Servers/NotoPyIfDistributor/src/_ifdist.py +++ b/Noto/Servers/NotoPyIfDistributor/src/_ifdist.py @@ -76,30 +76,38 @@ def main(argv): sys.exit(1) commandstr=[] - - if int(argv[0]) not in range (1,3): - sys.stderr.write(str('error Enter a valid input channel number')) - userLogger.logError("Enter a valid input channel number") - simpleClient.disconnect() - sys.exit(1) - selectedInput='input'+argv[0] + + try: + if int(argv[0]) not in range (1,3): + sys.stderr.write(str('error Enter a valid input channel number')) + userLogger.logError("Enter a valid input channel number") + simpleClient.disconnect() + sys.exit(1) + + selectedInput='input'+argv[0] - if int(argv[1]) not in range (-1,7): - sys.stderr.write(str('error Enter an allowed value for pol')) - userLogger.logError("Enter an allowed value for pol") - simpleClient.disconnect() - sys.exit(1) + if int(argv[1]) not in range (-1,7): + sys.stderr.write(str('error Enter an allowed value for pol')) + userLogger.logError("Enter an allowed value for pol") + simpleClient.disconnect() + sys.exit(1) - selectedPol=argv[1] + selectedPol=argv[1] - if float(argv[2]) not in range (-1,64): - sys.stderr.write(str('error Enter an allowed value for att')) - userLogger.logError("Enter an allowed value for att") + if float(argv[2]) not in range (-1,64): + sys.stderr.write(str('error Enter an allowed value for att')) + userLogger.logError("Enter an allowed value for att") + simpleClient.disconnect() + sys.exit(1) + + selectedAtt=argv[2] + + except ValueError: + sys.stderr.write(str('error Enter a numeric value')) + userLogger.logError("Enter a numeric value") simpleClient.disconnect() sys.exit(1) - - selectedAtt=argv[2] #if selectedPol and selectedAtt are -1 print information on current pol and att if int(selectedPol) == -1 and float(selectedAtt)==-1: @@ -115,30 +123,34 @@ def main(argv): commandstr=[selectedInput+","+selectedPol, "att"+argv[0]+","+selectedAtt] answerstr = "" + for i in range(0,len(commandstr)): userLogger.logNotice("IFDist setup according to %s command"%(commandstr[i])) answer=send_command(ip,port, commandstr[i]) - answerstr+=str(answer) + answerstr+= str(answer) + if answer=="Fail": - newEx = ComponentErrorsImpl.SocketErrorExImpl() - add_user_message(newEx,"Unable to communicate to IFDist") - sys.stderr.write(str('error Unable to communicate to IFDist')) - userLogger.logError(newEx) - simpleClient.disconnect() - sys.exit(1) + newEx = ComponentErrorsImpl.SocketErrorExImpl() + add_user_message(newEx,"Unable to communicate to IFDist") + sys.stderr.write(str('error Unable to communicate to IFDist')) + userLogger.logError(newEx) + simpleClient.disconnect() + sys.exit(1) - elif answer[0]=="NAK": - newEx = ComponentErrorsImpl.NakExImpl() - add_user_message(newEx,"IFDist command error") - sys.stderr.write(str('error IFDist command error')) - userLogger.logError(newEx) - simpleClient.disconnect() - sys.exit(1) + elif answer[0]=="NAK": + newEx = ComponentErrorsImpl.NakExImpl() + add_user_message(newEx,"IFDist command error") + sys.stderr.write(str('error IFDist command error')) + userLogger.logError(newEx) + simpleClient.disconnect() + sys.exit(1) else: - userLogger.logNotice( "Answer: %s"%(answer)) - - sys.stderr.write(str(answerstr)) + userLogger.logNotice( "Answer: %s"%(answer)) + + + print >> sys.stderr, answerstr.replace(","," ") + if __name__=="__main__": - main(sys.argv[1:]) \ No newline at end of file + main(sys.argv[1:]) -- GitLab