Skip to content

Commit

Permalink
utils.py: simplify report function
Browse files Browse the repository at this point in the history
  • Loading branch information
maxim-belkin committed May 20, 2018
1 parent 1e7efe2 commit 2ba1381
Showing 1 changed file with 26 additions and 24 deletions.
50 changes: 26 additions & 24 deletions bin/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,36 +65,38 @@ def add(self, location, fmt, *args):

self.messages.append((location, fmt.format(*args)))

@staticmethod
def pretty(item):
location, message = item
if isinstance(location, type(None)):
return message
elif isinstance(location, str):
return location + ': ' + message
elif isinstance(location, tuple):
return '{0}:{1}: '.format(*location) + message
else:
assert False, 'Unknown item "{0}"'.format(item)

@staticmethod
def key(item):
location, message = item
if isinstance(location, type(None)):
return ('', -1, message)
elif isinstance(location, str):
return (location, -1, message)
elif isinstance(location, tuple):
return (location[0], location[1], message)
else:
assert False, 'Unknown item "{0}"'.format(item)

def report(self, stream=sys.stdout):
"""Report all messages in order."""

if not self.messages:
return

def pretty(item):
location, message = item
if isinstance(location, type(None)):
return message
elif isinstance(location, str):
return location + ': ' + message
elif isinstance(location, tuple):
return '{0}:{1}: '.format(*location) + message
else:
assert False, 'Unknown item "{0}"'.format(item)

def key(item):
location, message = item
if isinstance(location, type(None)):
return ('', -1, message)
elif isinstance(location, str):
return (location, -1, message)
elif isinstance(location, tuple):
return (location[0], location[1], message)
else:
assert False, 'Unknown item "{0}"'.format(item)

for m in sorted(self.messages, key=key):
print(pretty(m), file=stream)
for m in sorted(self.messages, key=self.key):
print(self.pretty(m), file=stream)


def read_markdown(parser, path):
Expand Down

0 comments on commit 2ba1381

Please sign in to comment.