rtemstoolkit/macros: Improve the macro output.

- Support optionally reporting just the keys and their values.
This commit is contained in:
Chris Johns
2019-06-06 20:51:31 +10:00
parent 89dcdbc9ff
commit 2b27eec2ae

View File

@@ -80,8 +80,9 @@ class macros:
pass pass
return us return us
def __init__(self, name = None, original = None, rtdir = '.'): def __init__(self, name = None, original = None, rtdir = '.', show_minimal = False):
self.files = [] self.files = []
self.str_show_minimal = show_minimal
self.macro_filter = re.compile(r'%{[^}]+}') self.macro_filter = re.compile(r'%{[^}]+}')
if original is None: if original is None:
self.macros = {} self.macros = {}
@@ -124,7 +125,14 @@ class macros:
text = '' text = ''
for f in self.files: for f in self.files:
text += '> %s%s' % (f, os.linesep) text += '> %s%s' % (f, os.linesep)
max_key_size = 0
for map in self.macros: for map in self.macros:
size = len(max(self.macros[map].keys(), key = lambda x: len(x)))
if size > max_key_size:
max_key_size = size
maps = sorted(self.macros)
maps.remove('global')
for map in ['global'] + maps:
rm = '-' rm = '-'
for rmap in self.read_maps: for rmap in self.read_maps:
if rmap[4:] == '___%s' % (map): if rmap[4:] == '___%s' % (map):
@@ -140,10 +148,13 @@ class macros:
text += '[%s] %s,%s%s' % (map, wm, rm, os.linesep) text += '[%s] %s,%s%s' % (map, wm, rm, os.linesep)
for k in sorted(self.macros[map].keys()): for k in sorted(self.macros[map].keys()):
d = self.macros[map][k] d = self.macros[map][k]
text += " %s:%s '%s'%s '%s'%s" % \ if self.str_show_minimal:
(k, ' ' * (20 - len(k)), text += " %s: %s" % (k, ' ' * (max_key_size - len(k)))
d[0], ' ' * (8 - len(d[0])), else:
d[1], ' ' * (10 - len(d[1]))) text += " %s:%s '%s'%s '%s'%s" % \
(k, ' ' * (max_key_size - len(k)),
d[0], ' ' * (8 - len(d[0])),
d[1], ' ' * (10 - len(d[1])))
if len(d[2]) == 0: if len(d[2]) == 0:
text += "''%s" % (os.linesep) text += "''%s" % (os.linesep)
else: else:
@@ -151,14 +162,18 @@ class macros:
text += "'''" text += "'''"
else: else:
text += "'" text += "'"
indent = False
ds = d[2].split('\n') ds = d[2].split('\n')
lc = 0 lc = 0
indent = False
for l in ds: for l in ds:
lc += 1 lc += 1
while len(l): while len(l):
if indent: if indent:
text += ' %21s %10s %12s' % (' ', ' ', ' ') if self.str_show_minimal:
text += ' %s ' % (' ' * max_key_size)
else:
text += ' %s %10s %12s' % (' ' * max_key_size, ' ', ' ')
indent = True
text += l[0:text_len] text += l[0:text_len]
l = l[text_len:] l = l[text_len:]
if len(l): if len(l):
@@ -169,7 +184,6 @@ class macros:
else: else:
text += "'" text += "'"
text += '%s' % (os.linesep) text += '%s' % (os.linesep)
indent = True
return text return text
def __iter__(self): def __iter__(self):