From 997e68f2c24ecedd870485810ff623ce105b00a1 Mon Sep 17 00:00:00 2001 From: Etoh Date: Thu, 10 Apr 2014 13:21:20 +0100 Subject: [PATCH] Improve port input error handling --- syncplay/ui/ConfigurationGetter.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/syncplay/ui/ConfigurationGetter.py b/syncplay/ui/ConfigurationGetter.py index e994c08..74a19f2 100644 --- a/syncplay/ui/ConfigurationGetter.py +++ b/syncplay/ui/ConfigurationGetter.py @@ -90,6 +90,18 @@ class ConfigurationGetter(object): self._playerFactory = PlayerFactory() def _validateArguments(self): + def _isPortValid(varToTest): + try: + if (varToTest == "" or varToTest is None): + return False + if (str(varToTest).isdigit() == False): + return False + varToTest = int(varToTest) + if (varToTest > 65535 or varToTest < 1): + return False + return True + except: + return False for key in self._boolean: if(self._config[key] == "True"): self._config[key] = True @@ -108,9 +120,11 @@ class ConfigurationGetter(object): elif(key == "host"): self._config["host"], self._config["port"] = self._splitPortAndHost(self._config["host"]) hostNotValid = (self._config["host"] == "" or self._config["host"] is None) - portNotValid = (self._config["port"] == "" or self._config["port"] is None) - if(hostNotValid or portNotValid): + portNotValid = (_isPortValid(self._config["port"]) == False) + if(hostNotValid): raise InvalidConfigValue("Hostname can't be empty") + elif(portNotValid): + raise InvalidConfigValue("Port must be valid") elif(self._config[key] == "" or self._config[key] is None): raise InvalidConfigValue("{} can't be empty".format(key.capitalize())) @@ -136,7 +150,11 @@ class ConfigurationGetter(object): if(host): if ':' in host: host, port = host.split(':', 1) - return host, int(port) + try: + port = int(port) + except ValueError: + pass + return host, port def _checkForPortableFile(self): path = utils.findWorkingDir()