ioctlSet: implement a __format__ function which has a "," formatter.

The "," formatter will format the output as comma separated rather than
space separated.
This commit is contained in:
Chris PeBenito 2016-03-28 09:28:24 -04:00
parent e8d9d611eb
commit ab41dc81e6

View File

@ -158,11 +158,24 @@ class AVRule(BaseTERule):
class ioctlSet(set):
"""
A set with an overridden str function which compresses
the output into ioctl ranges.
A set with overridden string functions which compresses
the output into ioctl ranges instead of individual elements.
"""
def __format__(self, spec):
"""
String formating.
The standard formatting (no specification) will render the
ranges of ioctls, space separated.
The , option by itself will render the ranges of ioctls,
comma separated
Any other combination of formatting options will fall back
to set's formatting behavior.
"""
def __str__(self):
# generate short permission notation
perms = sorted(self)
shortlist = []
@ -173,10 +186,18 @@ class ioctlSet(set):
else:
shortlist.append("0x{0:04x}".format(group[0]))
if not spec:
return " ".join(shortlist)
elif spec == ",":
return ", ".join(shortlist)
else:
return super(ioctlSet, self).__format__(spec)
def __str__(self):
return "{0}".format(self)
def __repr__(self):
return "{{ {0} }}".format(self)
return "{{ {0:,} }}".format(self)
def ranges(self):
"""