give filesystem actions a nicer __repr__

This commit is contained in:
Michael Hudson-Doyle 2019-09-09 15:22:44 +12:00
parent d0151deec0
commit ce66777bd4
1 changed files with 28 additions and 4 deletions

View File

@ -75,13 +75,38 @@ def _remove_backlinks(obj):
_type_to_cls = {} _type_to_cls = {}
def fsobj__repr(obj):
args = []
for f in attr.fields(type(obj)):
if f.name.startswith("_"):
continue
v = getattr(obj, f.name)
if v is f.default:
continue
if f.metadata.get('ref', False):
v = v.id
elif f.metadata.get('reflist', False):
if isinstance(v, set):
delims = "{}"
else:
delims = "[]"
v = delims[0] + ", ".join(vv.id for vv in v) + delims[1]
elif f.metadata.get('redact', False):
v = "<REDACTED>"
else:
v = repr(v)
args.append("{}={}".format(f.name, v))
return "{}({})".format(type(obj).__name__, ", ".join(args))
def fsobj(typ): def fsobj(typ):
def wrapper(c): def wrapper(c):
c.__attrs_post_init__ = _set_backlinks c.__attrs_post_init__ = _set_backlinks
c.type = attributes.const(typ) c.type = attributes.const(typ)
c.id = attributes.idfield(typ) c.id = attributes.idfield(typ)
c._m = attr.ib(repr=None, default=None) c._m = attr.ib(repr=None, default=None)
c = attr.s(cmp=False)(c) c = attr.s(cmp=False, repr=False)(c)
c.__repr__ = fsobj__repr
_type_to_cls[typ] = c _type_to_cls[typ] = c
return c return c
return wrapper return wrapper
@ -263,8 +288,7 @@ class attributes:
@staticmethod @staticmethod
def backlink(*, default=None): def backlink(*, default=None):
return attr.ib( return attr.ib(
init=False, repr=False, default=default, init=False, default=default, metadata={'is_backlink': True})
metadata={'is_backlink': True})
@staticmethod @staticmethod
def const(value): def const(value):
@ -1009,7 +1033,7 @@ LUKS_OVERHEAD = 16*(2**20)
@fsobj("dm_crypt") @fsobj("dm_crypt")
class DM_Crypt: class DM_Crypt:
volume = attributes.ref(backlink="_constructed_device") # _Formattable volume = attributes.ref(backlink="_constructed_device") # _Formattable
key = attr.ib(repr=False) key = attr.ib(metadata={'redact': True})
dm_name = attr.ib(default=None) dm_name = attr.ib(default=None)
preserve = attr.ib(default=False) preserve = attr.ib(default=False)