8000 bpo-36876: [c-analyzer tool] Add a "capi" subcommand to the c-analyzer tool. by ericsnowcurrently · Pull Request #23918 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-36876: [c-analyzer tool] Add a "capi" subcommand to the c-analyzer tool. #23918

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 17 commits into from
Dec 24, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add table rendering for CLI use.
  • Loading branch information
ericsnowcurrently committed Dec 24, 2020
commit 042e756df38b8ac1f1c1620c3db84ed6395a4b87
114 changes: 114 additions & 0 deletions Tools/c-analyzer/c_common/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,117 @@ def _normalize_table_file_props(header, sep):
else:
sep = None
return header, sep


##################################
# stdout tables

WIDTH = 20


def build_table(columns, *, sep=' '):
if isinstance(columns, str):
columns = columns.replace(',', ' ').strip().split()
columns = _parse_columns(columns)
return _build_table(columns, sep=sep)


def _parse_colspec(raw):
width = align = fmt = None
if raw.isdigit():
width = int(raw)
align = 'left'
elif raw == '<':
align = 'left'
elif raw == '^':
align = 'middle'
elif raw == '>':
align = 'right'
else:
# XXX Handle combined specs.
fmt = raw
return width, align, fmt


def _render_colspec(spec):
if not spec:
return ''
width, align, colfmt = spec

parts = []
if align:
if align == 'left':
align = '<'
elif align == 'middle':
align = '^'
elif align == 'right':
align = '>'
else:
raise NotImplementedError
parts.append(align)
if width:
parts.append(str(width))
if colfmt:
raise NotImplementedError
return ''.join(parts)


def _parse_column(raw):
if isinstance(raw, str):
colname, _, rawspec = raw.partition(':')
spec = _parse_colspec(rawspec)
return (colname, spec)
else:
raise NotImplementedError


def _render_column(colname, spec):
spec = _render_colspec(spec)
if spec:
return f'{colname}:{spec}'
else:
return colname


def _parse_columns(columns):
parsed = []
for raw in columns:
column = _parse_column(raw)
parsed.append(column)
return parsed


def _resolve_width(width, colname, defaultwidth):
if width:
if not isinstance(width, int):
raise NotImplementedError
return width

if not defaultwidth:
return WIDTH
elif not hasattr(defaultwidth, 'get'):
return defaultwidth or WIDTH

defaultwidths = defaultwidth
defaultwidth = defaultwidths.get(None) or WIDTH
return defaultwidths.get(colname) or defaultwidth


def _build_table(columns, *, sep=' ', defaultwidth=None):
header = []
div = []
rowfmt = []
for colname, spec in columns:
width, align, colfmt = spec
width = _resolve_width(width, colname, defaultwidth)
if width != spec[0]:
spec = width, align, colfmt

header.append(f' {{:^{width}}} '.format(colname))
div.append('-' * (width + 2))
rowfmt.append(' {' + _render_column(colname, spec) + '} ')
return (
sep.join(header),
sep.join(div),
sep.join(rowfmt),
)
0