Skip to content

Commit

Permalink
Improved diagnostic prints
Browse files Browse the repository at this point in the history
Details:

* The PYWBEMCLI_DIAGNOSTICS env var and the verbose switch turned on
  the diplay of the Click context objects in some situations.
  This change improves that as follows:
  - The env var alone is sufficient to enable the prints. This makes
    the feature useable in test situations.
  - The output lines now start with DIAGNOSTICS: for better identification
    between other output.
  - The attributes are now sorted by attribute name.

Signed-off-by: Andreas Maier <andreas.r.maier@gmx.de>
  • Loading branch information
andy-maier committed Sep 8, 2020
1 parent f3bd32b commit 9aad04f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
25 changes: 13 additions & 12 deletions pywbemtools/pywbemcli/_context_obj.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,12 @@ def execute_cmd(self, cmd):
context.execute_cmd(lambda: cmd_instance_query(context, query, options))
"""
# Verbose and env var PYWBEMCLI_DIAGNOSTICS displays the click
# interpetation of the command and parameters from click context
# This is a diagnostic for developer internal use so the
# env variable is not documented.
if self.verbose and os.getenv('PYWBEMCLI_DIAGNOSTICS'):
# Env.var PYWBEMCLI_DIAGNOSTICS turns on disgnostic prints for developer
# use and is therefore not documented.
if os.getenv('PYWBEMCLI_DIAGNOSTICS'):
ctx = click.get_current_context()
click.echo('COMMAND="{}", params={!r}'.format(ctx.info_name,
ctx.params))
click.echo('DIAGNOSTICS: command={!r}, params={!r}'.
format(ctx.info_name, ctx.params))
display_click_context_parents(display_attrs=True)

if not self.pdb:
Expand Down Expand Up @@ -437,9 +435,11 @@ def display_click_context(ctx, msg=None, display_attrs=True):
if not display_attrs:
click.echo(ctx.obj)
else:
click.echo('{0} {1}, attrs: {2}'.format(
click.echo('{0} {1}, attrs:\n {2}'.format(
msg, ctx,
'\n '.join('{0}: {1}'.format(i, v) for (i, v) in attrs.items())))
'\n '.join(
'{0}: {1}'.format(i, v)
for (i, v) in sorted(attrs.items()))))


def display_click_context_parents(display_attrs=False):
Expand All @@ -451,7 +451,8 @@ def display_click_context_parents(display_attrs=False):
ctx = click.get_current_context()
disp_ctx = ctx.parent
while disp_ctx is not None:
display_click_context(disp_ctx, msg='level {}'.format(level),
display_attrs=display_attrs)
level += -1
display_click_context(
disp_ctx, msg='DIAGNOSTICS: Context at parent level {}:'.
format(level), display_attrs=display_attrs)
level += 1
disp_ctx = disp_ctx.parent
8 changes: 4 additions & 4 deletions pywbemtools/pywbemcli/pywbemcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,10 +719,10 @@ def create_server_instance(connection_name):
log, verbose, pdb,
warn,
connections_repo)
if verbose and os.getenv('PYWBEMCLI_DIAGNOSTICS'):
print('CONTEXT_OBJ {!r}'.format(ctx.obj))
print('CLICK CTX {}'.format(ctx))
display_click_context(ctx, msg="After adding Context",
# Env.var PYWBEMCLI_DIAGNOSTICS turns on disgnostic prints for developer
# use and is therefore not documented.
if os.getenv('PYWBEMCLI_DIAGNOSTICS'):
display_click_context(ctx, msg="DIAGNOSTICS: Initial context:",
display_attrs=True)

_python_nm = sys.version_info[0:2]
Expand Down

0 comments on commit 9aad04f

Please sign in to comment.