MCPcopy
hub / github.com/google/python-fire / _CallAndUpdateTrace

Function _CallAndUpdateTrace

fire/core.py:652–696  ·  view source on GitHub ↗

Call the component by consuming args from args, and update the FireTrace. The component could be a class, a routine, or a callable object. This function calls the component and adds the appropriate action to component_trace. Args: component: The component to call args: Args for calli

(component, args, component_trace, treatment='class',
                        target=None)

Source from the content-addressed store, hash-verified

650
651
652def _CallAndUpdateTrace(component, args, component_trace, treatment='class',
653 target=None):
654 """Call the component by consuming args from args, and update the FireTrace.
655
656 The component could be a class, a routine, or a callable object. This function
657 calls the component and adds the appropriate action to component_trace.
658
659 Args:
660 component: The component to call
661 args: Args for calling the component
662 component_trace: FireTrace object that contains action trace
663 treatment: Type of treatment used. Indicating whether we treat the component
664 as a class, a routine, or a callable.
665 target: Target in FireTrace element, default is None. If the value is None,
666 the component itself will be used as target.
667 Returns:
668 component: The object that is the result of the callable call.
669 remaining_args: The remaining args that haven't been consumed yet.
670 """
671 if not target:
672 target = component
673 filename, lineno = inspectutils.GetFileAndLine(component)
674 metadata = decorators.GetMetadata(component)
675 fn = component.__call__ if treatment == 'callable' else component
676 parse = _MakeParseFn(fn, metadata)
677 (varargs, kwargs), consumed_args, remaining_args, capacity = parse(args)
678
679 # Call the function.
680 if inspectutils.IsCoroutineFunction(fn):
681 loop = asyncio.get_event_loop()
682 component = loop.run_until_complete(fn(*varargs, **kwargs))
683 else:
684 component = fn(*varargs, **kwargs)
685
686 if treatment == 'class':
687 action = trace.INSTANTIATED_CLASS
688 elif treatment == 'routine':
689 action = trace.CALLED_ROUTINE
690 else:
691 action = trace.CALLED_CALLABLE
692 component_trace.AddCalledComponent(
693 component, target, consumed_args, filename, lineno, capacity,
694 action=action)
695
696 return component, remaining_args
697
698
699def _MakeParseFn(fn, metadata):

Callers 1

_FireFunction · 0.85

Calls 3

_MakeParseFnFunction · 0.85
parseFunction · 0.85
AddCalledComponentMethod · 0.80

Tested by

no test coverage detected