From 0728c1a8be5f76d62fae79a9adcc998081049f9e Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Mon, 28 May 2018 10:21:51 -0400 Subject: [PATCH] msgproto: Add support for a get_constant_int() method Signed-off-by: Kevin O'Connor --- klippy/msgproto.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/klippy/msgproto.py b/klippy/msgproto.py index 66362e18..739f2cb1 100644 --- a/klippy/msgproto.py +++ b/klippy/msgproto.py @@ -327,18 +327,18 @@ class MessageParser: logging.exception("process_identify error") raise error("Error during identify: %s" % (str(e),)) class sentinel: pass - def get_constant(self, name, default=sentinel): + def get_constant(self, name, default=sentinel, parser=str): if name not in self.config: if default is not self.sentinel: return default raise error("Firmware constant '%s' not found" % (name,)) - return self.config[name] - def get_constant_float(self, name, default=sentinel): - if name not in self.config and default is not self.sentinel: - return default try: - return float(self.config[name]) - except ValueError: - raise error("Firmware constant '%s' not a float" % (name,)) - except KeyError: - raise error("Firmware constant '%s' not found" % (name,)) + value = parser(self.config[name]) + except: + raise error("Unable to parse firmware constant %s: %s" % ( + name, self.config[name])) + return value + def get_constant_float(self, name, default=sentinel): + return self.get_constant(name, default, parser=float) + def get_constant_int(self, name, default=sentinel): + return self.get_constant(name, default, parser=int)