Skip to content

Commit

Permalink
feat(mi): add --sort option
Browse files Browse the repository at this point in the history
Fixes #100
  • Loading branch information
rubik committed May 30, 2017
1 parent 7b6cdc2 commit 15e4f36
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
4 changes: 3 additions & 1 deletion radon/cli/__init__.py
Expand Up @@ -92,7 +92,7 @@ def raw(paths, exclude=None, ignore=None, summary=False, json=False):
@program.command
@program.arg('paths', nargs='+')
def mi(paths, min='A', max='C', multi=True, exclude=None, ignore=None,
show=False, json=False):
show=False, json=False, sort=False):
'''Analyze the given Python modules and compute the Maintainability Index.
The maintainability index (MI) is a compound metric, with the primary aim
Expand All @@ -112,6 +112,7 @@ def mi(paths, min='A', max='C', multi=True, exclude=None, ignore=None,
comments.
:param -s, --show: If given, the actual MI value is shown in results.
:param -j, --json: Format results in JSON.
:param --sort: If given, results are sorted in ascending order.
'''
config = Config(
min=min.upper(),
Expand All @@ -120,6 +121,7 @@ def mi(paths, min='A', max='C', multi=True, exclude=None, ignore=None,
ignore=ignore,
multi=multi,
show=show,
sort=sort,
)

harvester = MIHarvester(paths, config)
Expand Down
9 changes: 7 additions & 2 deletions radon/cli/harvest.py
Expand Up @@ -250,9 +250,14 @@ def filtered_results(self):
'''Filter results with respect with their rank.'''
for key, value in self.results:
if ('error' in value or
self.config.min <= value['rank'] <= self.config.max):
self.config.min <= value['rank'] <= self.config.max):
yield (key, value)

def _sort(self, results):
if self.config.sort:
return sorted(results, key=lambda el: el[1]['mi'])
return results

def as_json(self):
'''Format the results as JSON.'''
return json.dumps(dict(self.filtered_results))
Expand All @@ -263,7 +268,7 @@ def as_xml(self):

def to_terminal(self):
'''Yield lines to be printed to a terminal.'''
for name, mi in self.filtered_results:
for name, mi in self._sort(self.filtered_results):
if 'error' in mi:
yield name, (mi['error'],), {'error': True}
continue
Expand Down

0 comments on commit 15e4f36

Please sign in to comment.