8000 bpo-36876: [c-analyzer tool] Additional CLI updates for "capi" command. by ericsnowcurrently · Pull Request #23929 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-36876: [c-analyzer tool] Additional CLI updates for "capi" command. #23929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Dec 25, 2020
Merged
Prev Previous commit
Next Next commit
Improve sorting.
  • Loading branch information
ericsnowcurrently committed Dec 25, 2020
commit f1e3f787c73ad4cdc833326fdf0406bffeb927f0
4 changes: 4 additions & 0 deletions Tools/c-analyzer/cpython/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ def process_format(args, *, argv=None):
if args.format not in _capi._FORMATS:
parser.error(f'unsupported format {orig!r}')

# XXX Add an option to ignore empty sets.

# XXX Add --sort-by, --sort and --no-sort.

parser.add_argument('filenames', nargs='*', metavar='FILENAME')
process_progress = add_progress_cli(parser)

Expand Down
56 changes: 50 additions & 6 deletions Tools/c-analyzer/cpython/_capi.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,12 @@ def _parse_line(line, prev=None):
return None


LEVELS = {
LEVELS = [
'stable',
'cpython',
'private',
'internal',
}
]

def _get_level(filename, name, *,
_cpython=INCLUDE_CPYTHON + os.path.sep,
Expand Down Expand Up @@ -313,6 +313,32 @@ def _collate(items, groupby):
return collated, groupby, maxfilename, maxname, maxextra


def _get_sortkey(sort, _groupby, _columns):
if sort is True or sort is None:
# For now:
def sortkey(item):
return (
item.level == 'private',
LEVELS.index(item.level),
KINDS.index(item.kind),
os.path.dirname(item.file),
os.path.basename(item.file),
item.name,
)
return sortkey

sortfields = 'not-private level kind dirname basename name'.split()
elif isinstance(sort, str):
sortfields = sort.replace(',', ' ').strip().split()
elif callable(sort):
return sort
else:
raise NotImplementedError

# XXX Build a sortkey func from sortfields.
raise NotImplementedError


##################################
# CLI rendering

Expand Down Expand Up @@ -353,7 +379,12 @@ def render(items, **kwargs):
return render


def render_table(items, *, columns=None, groupby='kind', verbose=False):
def render_table(items, *,
columns=None,
groupby='kind',
sort=True,
verbose=False,
):
if groupby:
collated, groupby, maxfilename, maxname, maxextra = _collate(items, groupby)
if groupby == 'kind':
Expand All @@ -370,6 +401,9 @@ def render_table(items, *, columns=None, groupby='kind', verbose=False):
# XXX Support no grouping?
raise NotImplementedError

if sort:
sortkey = _get_sortkey(sort, groupby, columns)

if columns:
def get_extra(item):
return {extra: getattr(item, extra)
Expand Down Expand Up @@ -416,7 +450,10 @@ def get_extra(item):
yield ''
yield header
yield div
for item in collated[group]:
grouped = collated[group]
if sort:
grouped = sorted(grouped, key=sortkey)
for item in grouped:
yield fmt.format(
filename=item.relfile,
name=item.name,
Expand All @@ -430,7 +467,10 @@ def get_extra(item):
yield f'total: {total}'


def render_full(items, *, groupby=None, verbose=False):
def render_full(items, *, groupby=None, sort=True, verbose=False):
if sort:
sortkey = _get_sortkey(sort, groupby, None)

if groupby:
collated, groupby, _, _, _ = _collate(items, groupby)
for group, grouped in collated.items():
Expand All @@ -440,10 +480,14 @@ def render_full(items, *, groupby=None, verbose=False):
yield ''
if not grouped:
continue
if sort:
grouped = sorted(grouped, key=sortkey)
for item in grouped:
yield from _render_item_full(item, groupby, verbose)
yield ''
else:
if sort:
items = sorted(items, key=sortkey)
for item in items:
yield from _render_item_full(item, None, verbose)
yield ''
Expand All @@ -462,7 +506,7 @@ def _render_item_full(item, groupby, verbose):
print(' ---------------------------------------')


def render_summary(items, *, groupby='kind', verbose=False):
def render_summary(items, *, groupby='kind', sort 484C =None, verbose=False):
total = 0
summary = summarize(items, groupby=groupby)
# XXX Stablize the sorting to match KINDS/LEVELS.
Expand Down
0