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
Show file tree
Hide file tree
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
Support flexible formats.
  • Loading branch information
ericsnowcurrently committed Dec 24, 2020
commit 8fdda67dbc2f3c92ebe97f47f49a1264d8350b25
188 changes: 125 additions & 63 deletions Tools/c-analyzer/c_common/tables.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import csv
import re
import textwrap

from . import NOT_SET, strutil, fsutil

Expand Down Expand Up @@ -220,83 +222,141 @@ def _normalize_table_file_props(header, sep):
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 resolve_columns(specs):
if isinstance(specs, str):
specs = specs.replace(',', ' ').strip().split()
return _resolve_colspecs(specs)


def build_table(specs, *, sep=' ', defaultwidth=None):
columns = resolve_columns(specs)
return _build_table(columns, sep=sep, defaultwidth=defaultwidth)


_COLSPEC_RE = re.compile(textwrap.dedent(r'''
^
(?:
[[]
(
(?: [^\s\]] [^\]]* )?
[^\s\]]
) # <label>
[]]
)?
( \w+ ) # <field>
(?:
(?:
:
( [<^>] ) # <align>
( \d+ ) # <width1>
)
|
(?:
(?:
:
( \d+ ) # <width2>
)?
(?:
:
( .*? ) # <fmt>
)?
)
)?
$
'''), re.VERBOSE)


def _parse_fmt(fmt):
if fmt.startswith(tuple('<^>')):
align = fmt[0]
width = fmt[1:]
if width.isdigit():
return int(width), align
return None, None


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'
m = _COLSPEC_RE.match(raw)
if not m:
return None
label, field, align, width1, width2, fmt = m.groups()
if not label:
label = field
if width1:
width = None
fmt = f'{align}{width1}'
elif width2:
width = int(width2)
if fmt:
_width, _ = _parse_fmt(fmt)
if _width == width:
width = None
else:
width = None
return field, label, width, fmt


def _normalize_colspec(spec):
if len(spec) == 1:
raw, = spec
return _resolve_column(raw)

if len(spec) == 4:
label, field, width, fmt = spec
if width:
fmt = f'{width}:{fmt}' if fmt else width
elif len(raw) == 3:
label, field, fmt = spec
if not field:
label, field = None, label
elif not isinstance(field, str) or not field.isidentifier():
fmt = f'{field}:{fmt}' if fmt el 10000 se field
label, field = None, label
elif len(raw) == 2:
label = None
field, fmt = raw
if not field:
field, fmt = fmt, None
elif not field.isidentifier() or fmt.isidentifier():
label, field = field, fmt
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)
fmt = f':{fmt}' if fmt else ''
if label:
return _parse_colspec(f'[{label}]{field}{fmt}')
else:
raise NotImplementedError
return _parse_colspec(f'{field}{fmt}')


def _render_column(colname, spec):
spec = _render_colspec(spec)
if spec:
return f'{colname}:{spec}'
def _resolve_colspec(raw):
if isinstance(raw, str):
spec = _parse_colspec(raw)
else:
return colname
spec = _normalize_colspec(raw)
if spec is None:
raise ValueError(f'unsupported column spec {raw!r}')
return spec


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


def _resolve_width(width, colname, defaultwidth):
def _resolve_width(spec, defaultwidth):
_, label, width, fmt = spec
if width:
if not isinstance(width, int):
raise NotImplementedError
return width
elif width and fmt:
width, _ = _parse_fmt(fmt)
if width:
return width

if not defaultwidth:
return WIDTH
Expand All @@ -305,22 +365,24 @@ def _resolve_width(width, colname, defaultwidth):

defaultwidths = defaultwidth
defaultwidth = defaultwidths.get(None) or WIDTH
return defaultwidths.get(colname) or defaultwidth
return defaultwidths.get(label) 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
for spec in columns:
label, field, _, colfmt = spec
width = _reso E864 lve_width(spec, defaultwidth)
if colfmt:
colfmt = f':{colfmt}'
else:
colfmt = f':{width}'

header.append(f' {{:^{width}}} '.format(colname))
header.append(f' {{:^{width}}} '.format(label))
div.append('-' * (width + 2))
rowfmt.append(' {' + _render_column(colname, spec) + '} ')
rowfmt.append(f' {{{field}{colfmt}}} ')
return (
sep.join(header),
sep.join(div),
Expand Down
14 changes: 9 additions & 5 deletions Tools/c-analyzer/cpython/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,22 @@ def process_kinds(args, *, argv=None):
parser.add_argument('--group-by', dest='groupby',
choices=['level', 'kind'])

parser.add_argument('--format', choices=['brief', 'full', 'summary'], default='brief')
parser.add_argument('--format', default='brief')
parser.add_argument('--summary', dest='format',
action='store_const', const='summary')
def process_format(args, *, argv=None):
orig = args.format
args.format = _capi.resolve_format(args.format)
if isinstance(args.format, str):
if args.format not in _capi._FORMATS:
parser.error(f'unsupported format {orig!r}')

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

return [
process_levels,
process_format,
process_progress,
]

Expand All @@ -271,10 +278,7 @@ def cmd_capi(filenames=None, *,
verbosity=VERBOSITY,
**kwargs
):
try:
render = _capi.FORMATS[format or 'brief']
except KeyError:
raise ValueError(f'unsupported format {format!r}')
render = _capi.get_renderer(format)

filenames = _files.iter_header_files(filenames, levels=levels)
#filenames = (file for file, _ in main_for_filenames(filenames))
Expand Down
Loading
0