def LookupAndParseEntry(ref, args, request):
    """Parses the entry into the request, performing a lookup first if necessary.

  Args:
    ref: None.
    args: The parsed args namespace.
    request: The update entry request.

  Returns:
    Request containing the parsed entry.
  Raises:
    UnderSpecifiedEntryError: if ENTRY was only partially specified.
    RequiredMutexGroupError: if both or neither ENTRY, --lookup-entry specified.
  """
    del ref
    entry_ref = args.CONCEPTS.entry.Parse()

    # Parse() will raise an error if the entry flags are specified without the
    # anchor, so we don't need to handle that case. However no error is returned
    # if a positional is specified but parsing fails, so we check for that here.
    if args.IsSpecified('entry') and not entry_ref:
        raise UnderSpecifiedEntryError(
            'Argument [ENTRY : --entry-group=ENTRY_GROUP --location=LOCATION] was '
            'not fully specified.')

    if ((entry_ref and args.IsSpecified('lookup_entry'))
            or (not entry_ref and not args.IsSpecified('lookup_entry'))):
        raise concept_exceptions.RequiredMutexGroupError(
            'entry',
            '([ENTRY : --entry-group=ENTRY_GROUP --location=LOCATION] '
            '| --lookup-entry)')

    if entry_ref:
        request.name = entry_ref.RelativeName()
    else:
        client = entries_v1.EntriesClient()
        request.name = client.Lookup(args.lookup_entry).name
    return request
Exemple #2
0
        def _ParseConcept(node):
            """Recursive parsing."""
            if not node.is_group:
                fallthroughs = []
                if node.arg_name:
                    fallthroughs.append(deps_lib.ArgFallthrough(node.arg_name))
                fallthroughs += node.fallthroughs
                return node.concept.Parse(
                    DependencyViewFromValue(
                        functools.partial(deps_lib.GetFromFallthroughs,
                                          fallthroughs, parsed_args),
                        marshalled_dependencies=node.dependencies))

            # TODO(b/120132521) Replace and eliminate argparse extensions
            also_optional = [
            ]  # The optional concepts that were not specified.
            have_optional = [
            ]  # The specified optional (not required) concepts.
            have_required = []  # The specified required concepts.
            need_required = []  # The required concepts that must be specified.
            namespace = {}
            for name, child in six.iteritems(node.dependencies):
                result = None
                try:
                    result = _ParseConcept(child)
                    if result:
                        if child.concept.required:
                            have_required.append(child.concept)
                        else:
                            have_optional.append(child.concept)
                    else:
                        also_optional.append(child.concept)
                except exceptions.MissingRequiredArgumentError:
                    need_required.append(child.concept)
                namespace[name] = result

            if need_required:
                missing = ' '.join(GetPresentationNames(need_required))
                if have_optional or have_required:
                    specified_parts = []
                    if have_required:
                        specified_parts.append(' '.join(
                            GetPresentationNames(have_required)))
                    if have_required and have_optional:
                        specified_parts.append(':')
                    if have_optional:
                        specified_parts.append(' '.join(
                            GetPresentationNames(have_optional)))

                    specified = ' '.join(specified_parts)
                    if have_required and have_optional:
                        if node.concept.required:
                            specified = '({})'.format(specified)
                        else:
                            specified = '[{}]'.format(specified)
                    raise exceptions.ModalGroupError(
                        node.concept.GetPresentationName(), specified, missing)

            count = len(have_required) + len(have_optional)
            if node.concept.mutex:
                specified = ' | '.join(
                    GetPresentationNames(node.concept.concepts))
                if node.concept.required:
                    specified = '({specified})'.format(specified=specified)
                    if count != 1:
                        raise exceptions.RequiredMutexGroupError(
                            node.concept.GetPresentationName(), specified)
                else:
                    if count > 1:
                        raise exceptions.OptionalMutexGroupError(
                            node.concept.GetPresentationName(), specified)

            return node.concept.Parse(DependencyView(namespace))