aboutsummaryrefslogtreecommitdiffstats
path: root/klippy/msgproto.py
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2021-06-09 14:46:56 -0400
committerKevin O'Connor <kevin@koconnor.net>2021-06-09 18:58:26 -0400
commit31fcd491fd9fd99f81599558b5256cfaaa078da0 (patch)
tree0e7858ba24d325d2be74edec420788e45321830e /klippy/msgproto.py
parentf00281d1e670e00ebcef06076abf2116c3f2d461 (diff)
downloadkutter-31fcd491fd9fd99f81599558b5256cfaaa078da0.tar.gz
kutter-31fcd491fd9fd99f81599558b5256cfaaa078da0.tar.xz
kutter-31fcd491fd9fd99f81599558b5256cfaaa078da0.zip
serialhdl: Support prepending a warn_prefix to error and log messages
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'klippy/msgproto.py')
-rw-r--r--klippy/msgproto.py33
1 files changed, 18 insertions, 15 deletions
diff --git a/klippy/msgproto.py b/klippy/msgproto.py
index c3c0cda9..cdecd8e7 100644
--- a/klippy/msgproto.py
+++ b/klippy/msgproto.py
@@ -105,8 +105,8 @@ class Enumeration:
def encode(self, out, v):
tv = self.enums.get(v)
if tv is None:
- raise error("Unknown value '%s' in enumeration '%s'" % (
- v, self.enum_name))
+ raise error("Unknown value '%s' in enumeration '%s'"
+ % (v, self.enum_name))
self.pt.encode(out, tv)
def parse(self, s, pos):
v, pos = self.pt.parse(s, pos)
@@ -221,7 +221,8 @@ class UnknownFormat:
class MessageParser:
error = error
- def __init__(self):
+ def __init__(self, warn_prefix=""):
+ self.warn_prefix = warn_prefix
self.unknown = UnknownFormat()
self.enumerations = {}
self.messages = []
@@ -231,6 +232,8 @@ class MessageParser:
self.version = self.build_versions = ""
self.raw_identify_data = ""
self._init_messages(DefaultMessages)
+ def _error(self, msg, *params):
+ raise error(self.warn_prefix + (msg % params))
def check_packet(self, s):
if len(s) < MESSAGE_MIN:
return 0
@@ -277,7 +280,7 @@ class MessageParser:
mid = self.messages_by_id.get(msgid, self.unknown)
params, pos = mid.parse(s, MESSAGE_HEADER_SIZE)
if pos != len(s)-MESSAGE_TRAILER_SIZE:
- raise error("Extra data at end of message")
+ self._error("Extra data at end of message")
params['#name'] = mid.name
return params
def encode(self, seq, cmd):
@@ -302,10 +305,10 @@ class MessageParser:
msgname = parts[0]
mp = self.messages_by_name.get(msgname)
if mp is None:
- raise error("Unknown command: %s" % (msgname,))
+ self._error("Unknown command: %s", msgname)
if msgformat != mp.msgformat:
- raise error("Command format mismatch: %s vs %s" % (
- msgformat, mp.msgformat))
+ self._error("Command format mismatch: %s vs %s",
+ msgformat, mp.msgformat)
return mp
def create_command(self, msg):
parts = msg.strip().split()
@@ -314,7 +317,7 @@ class MessageParser:
msgname = parts[0]
mp = self.messages_by_name.get(msgname)
if mp is None:
- raise error("Unknown command: %s" % (msgname,))
+ self._error("Unknown command: %s", msgname)
try:
argparts = dict(arg.split('=', 1) for arg in parts[1:])
for name, value in argparts.items():
@@ -330,14 +333,14 @@ class MessageParser:
raise
except:
#logging.exception("Unable to extract params")
- raise error("Unable to extract params from: %s" % (msgname,))
+ self._error("Unable to extract params from: %s", msgname)
try:
cmd = mp.encode_by_name(**argparts)
except error as e:
raise
except:
#logging.exception("Unable to encode")
- raise error("Unable to encode: %s" % (msgname,))
+ self._error("Unable to encode: %s", msgname)
return cmd
def fill_enumerations(self, enumerations):
for add_name, add_enums in enumerations.items():
@@ -366,7 +369,7 @@ class MessageParser:
msgtype = 'output'
self.messages.append((msgtag, msgtype, msgformat))
if msgtag < -32 or msgtag > 95:
- raise error("Multi-byte msgtag not supported")
+ self._error("Multi-byte msgtag not supported")
msgid = msgtag & 0x7f
if msgtype == 'output':
self.messages_by_id[msgid] = OutputFormat(msgid, msgformat)
@@ -396,7 +399,7 @@ class MessageParser:
raise
except Exception as e:
logging.exception("process_identify error")
- raise error("Error during identify: %s" % (str(e),))
+ self._error("Error during identify: %s", str(e))
def get_version_info(self):
return self.version, self.build_versions
def get_messages(self):
@@ -410,12 +413,12 @@ class MessageParser:
if name not in self.config:
if default is not self.sentinel:
return default
- raise error("Firmware constant '%s' not found" % (name,))
+ self._error("Firmware constant '%s' not found", name)
try:
value = parser(self.config[name])
except:
- raise error("Unable to parse firmware constant %s: %s" % (
- name, self.config[name]))
+ self._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)