implement BaseMessage.__repr__

Take advantage of the information contained in signature/parameter
objects to represent positional and keyword arguments properly. Fall
back to using the funcsigs module on Python 2.
This commit is contained in:
Nils Philippsen
2016-09-22 15:59:35 +02:00
parent 8f36d863cf
commit 2233f60f58
2 changed files with 17 additions and 0 deletions

View File

@@ -11,3 +11,4 @@ Flask-Script
Flask-SQLAlchemy
Flask-Migrate
python-fedora
funcsigs # Python2 only

View File

@@ -25,6 +25,11 @@
"""Generic messaging functions."""
import re
try:
from inspect import signature
except ImportError:
from funcsigs import signature
from rida import logger
@@ -36,6 +41,17 @@ class BaseMessage(object):
"""
self.msg_id = msg_id
def __repr__(self):
init_sig = signature(self.__init__)
args_strs = (
"{}={!r}".format(name, getattr(self, name))
if param.default != param.empty
else repr(getattr(self, name))
for name, param in init_sig.parameters.items())
return "{}({})".format(type(self).__name__, ', '.join(args_strs))
@staticmethod
def from_fedmsg(topic, msg):
"""