msgproto: Add support for a get_constant_int() method

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2018-05-28 10:21:51 -04:00
parent 75f8709947
commit 0728c1a8be
1 changed files with 10 additions and 10 deletions

View File

@ -327,18 +327,18 @@ class MessageParser:
logging.exception("process_identify error") logging.exception("process_identify error")
raise error("Error during identify: %s" % (str(e),)) raise error("Error during identify: %s" % (str(e),))
class sentinel: pass 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 name not in self.config:
if default is not self.sentinel: if default is not self.sentinel:
return default return default
raise error("Firmware constant '%s' not found" % (name,)) 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: try:
return float(self.config[name]) value = parser(self.config[name])
except ValueError: except:
raise error("Firmware constant '%s' not a float" % (name,)) raise error("Unable to parse firmware constant %s: %s" % (
except KeyError: name, self.config[name]))
raise error("Firmware constant '%s' not found" % (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)