Ejemplo n.º 1
0
 def testGetArgSpecClassNoInit(self):
   args, varargs, keywords, defaults = inspectutils.GetArgSpec(
       tc.OldStyleEmpty)
   self.assertEqual(args, [])
   self.assertEqual(defaults, ())
   self.assertEqual(varargs, None)
   self.assertEqual(keywords, None)
Ejemplo n.º 2
0
 def testGetArgSpecMethod(self):
   args, varargs, keywords, defaults = inspectutils.GetArgSpec(
       tc.NoDefaults().double)
   self.assertEqual(args, ['count'])
   self.assertEqual(defaults, ())
   self.assertEqual(varargs, None)
   self.assertEqual(keywords, None)
Ejemplo n.º 3
0
def Completions(component, verbose=False):
    """Gives possible Fire command completions for the component.

  A completion is a string that can be appended to a command to continue that
  command. These are used for TAB-completions in Bash for Fire CLIs.

  Args:
    component: The component whose completions to list.
    verbose: Whether to include all completions, even private members.
  Returns:
    A list of completions for a command that would so far return the component.
  """
    if inspect.isroutine(component) or inspect.isclass(component):
        fn_args = inspectutils.GetArgSpec(component).args
        return _CompletionsFromArgs(fn_args)

    elif isinstance(component, (tuple, list)):
        return [str(index) for index in range(len(component))]

    elif inspect.isgenerator(component):
        # TODO: There are currently no commands available for generators.
        return []

    else:
        return [
            _FormatForCommand(member_name)
            for member_name, unused_member in _Members(component, verbose)
        ]
Ejemplo n.º 4
0
 def testGetArgSpecReturnType(self):
   # Asserts that the named tuple returned by GetArgSpec has the appropriate
   # fields.
   argspec = inspectutils.GetArgSpec(tc.identity)
   args, varargs, keywords, defaults = argspec
   self.assertEqual(argspec.args, args)
   self.assertEqual(argspec.defaults, defaults)
   self.assertEqual(argspec.varargs, varargs)
   self.assertEqual(argspec.keywords, keywords)
Ejemplo n.º 5
0
def _MakeParseFn(fn):
    """Creates a parse function for fn.

  Args:
    fn: The function or class to create the parse function for.
  Returns:
    A parse function for fn. The parse function accepts a list of arguments
    and returns (varargs, kwargs), remaining_args. The original function fn
    can then be called with fn(*varargs, **kwargs). The remaining_args are
    the leftover args from the arguments to the parse function.
  """
    fn_args, fn_varargs, fn_keywords, fn_defaults = inspectutils.GetArgSpec(fn)
    metadata = decorators.GetMetadata(fn)

    # Note: num_required_args is the number of arguments without default values.
    # All of these arguments are required.
    num_required_args = len(fn_args) - len(fn_defaults)

    def _ParseFn(args):
        """Parses the list of `args` into (varargs, kwargs), remaining_args."""
        kwargs, remaining_args = _ParseKeywordArgs(args, fn_args, fn_keywords)

        # Note: _ParseArgs modifies kwargs.
        parsed_args, kwargs, remaining_args, capacity = _ParseArgs(
            fn_args, fn_defaults, num_required_args, kwargs, remaining_args,
            metadata)

        if fn_varargs or fn_keywords:
            # If we're allowed *varargs or **kwargs, there's always capacity.
            capacity = True

        if fn_keywords is None and kwargs:
            raise FireError('Unexpected kwargs present', kwargs)

        # If we accept *varargs, then use all remaining arguments for *varargs.
        if fn_varargs is not None:
            varargs, remaining_args = remaining_args, []
        else:
            varargs = []

        for index, value in enumerate(varargs):
            varargs[index] = _ParseValue(value, None, None, metadata)

        varargs = parsed_args + varargs

        consumed_args = args[:len(args) - len(remaining_args)]
        return (varargs, kwargs), consumed_args, remaining_args, capacity

    return _ParseFn
Ejemplo n.º 6
0
def UsageString(component, trace=None, verbose=False):
  """Returns a string showing how to use the component as a Fire command."""
  command = trace.GetCommand() + ' ' if trace else ''

  if inspect.isroutine(component) or inspect.isclass(component):
    args, varargs, keywords, defaults = inspectutils.GetArgSpec(component)
    return _UsageStringFromFnDetails(command, args, varargs, keywords, defaults)

  elif isinstance(component, (list, tuple)):
    length = len(component)
    if length == 0:
      return command
    elif length == 1:
      return command + '[0]'
    else:
      return command + '[0..{cap}]'.format(cap=length - 1)

  else:
    completions = completion.Completions(component, verbose)
    if command:
      completions = [''] + completions
    return '\n'.join(command + end for end in completions)
Ejemplo n.º 7
0
 def testGetArgSpecSlotWrapper(self):
   args, varargs, keywords, defaults = inspectutils.GetArgSpec(tc.NoDefaults)
   self.assertEqual(args, [])
   self.assertEqual(defaults, ())
   self.assertEqual(varargs, None)
   self.assertEqual(keywords, None)
Ejemplo n.º 8
0
 def testGetArgSpecBuiltin(self):
   args, varargs, keywords, defaults = inspectutils.GetArgSpec('test'.upper)
   self.assertEqual(args, [])
   self.assertEqual(defaults, ())
   self.assertEqual(varargs, 'vars')
   self.assertEqual(keywords, 'kwargs')
Ejemplo n.º 9
0
 def testGetArgSpec(self):
   args, varargs, keywords, defaults = inspectutils.GetArgSpec(tc.identity)
   self.assertEqual(args, ['arg1', 'arg2'])
   self.assertEqual(defaults, (10,))
   self.assertEqual(varargs, 'arg3')
   self.assertEqual(keywords, 'arg4')