Skip to content

Commit

Permalink
More reliable method for getting display call args
Browse files Browse the repository at this point in the history
  • Loading branch information
sivel committed Feb 10, 2025
1 parent bcc86b6 commit dc13412
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/ansible_runner/display_callback/callback/awx_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

# pylint: disable=W0212

from __future__ import (absolute_import, division, print_function)
from __future__ import (absolute_import, annotations, division, print_function)

# Python
import json
Expand All @@ -28,8 +28,11 @@
import collections
import contextlib
import datetime
import inspect
import os
import sys
import types
import typing as t
import uuid
from copy import copy

Expand All @@ -41,6 +44,8 @@
from ansible.utils.display import Display
from ansible.utils.multiprocessing import context as multiprocessing_context

if t.TYPE_CHECKING:
P = t.ParamSpec('P')

DOCUMENTATION = '''
callback: awx_display
Expand Down Expand Up @@ -247,6 +252,18 @@ def dump_end(self, fileobj):
event_context = EventContext()


@functools.cache
def _getsignature(f: t.Callable) -> inspect.Signature:
return inspect.signature(f)


@functools.cache
def _getcallargs(sig: inspect.Signature, *args: P.args, **kwargs: P.kwargs) -> types.MappingProxyType:
ba = sig.bind(*args, **kwargs)
ba.apply_defaults()
return types.MappingProxyType(ba.arguments)


def with_context(**context):
global event_context # pylint: disable=W0602

Expand Down Expand Up @@ -274,8 +291,10 @@ def with_verbosity(f):

@functools.wraps(f)
def wrapper(*args, **kwargs):
host = args[2] if len(args) >= 3 else kwargs.get('host', None)
caplevel = args[3] if len(args) >= 4 else kwargs.get('caplevel', 2)
sig = _getsignature(f)
callargs = _getcallargs(sig, *args, **kwargs)
host = callargs.get('host')
caplevel = callargs.get('caplevel')
context = {'verbose': True, 'verbosity': (caplevel + 1)}
if host is not None:
context['remote_addr'] = host
Expand All @@ -295,8 +314,10 @@ def wrapper(*args, **kwargs):
# core 2.14 and newer proxy display, return if we are in a fork
return f(*args, **kwargs)

log_only = args[5] if len(args) >= 6 else kwargs.get('log_only', False)
stderr = args[3] if len(args) >= 4 else kwargs.get('stderr', False)
sig = _getsignature(f)
callargs = _getcallargs(sig, *args, **kwargs)
log_only = callargs.get('log_only')
stderr = callargs.get('stderr')
event_uuid = event_context.get().get('uuid', None)
# If writing only to a log file or there is already an event UUID
# set (from a callback module method), skip dumping the event data.
Expand Down

0 comments on commit dc13412

Please sign in to comment.