Пример #1
0
    def _BuildFullFallthroughsMap(self, parsed_args=None):
        """Helper method to build all fallthroughs including arg names."""
        fallthroughs_map = {}
        for attribute in self.concept_spec.attributes:
            attribute_name = attribute.name
            attribute_fallthroughs = []

            # Start the fallthroughs list with the primary associated arg for the
            # attribute.
            arg_name = self.attribute_to_args_map.get(attribute_name)
            if arg_name:
                arg_value = getattr(parsed_args,
                                    util.NamespaceFormat(arg_name), None)
                # The only args that should be lists are anchor args for plural
                # resources.
                plural = (attribute_name == self.concept_spec.anchor.name
                          and self.plural)
                if isinstance(arg_value, list) and not plural:
                    arg_value = arg_value[0] if arg_value else None
                if plural and arg_value is None:
                    arg_value = []
                attribute_fallthroughs.append(
                    deps_lib.ArgFallthrough(arg_name, arg_value))

            attribute_fallthroughs += self.fallthroughs_map.get(
                attribute_name, [])
            fallthroughs_map[attribute_name] = attribute_fallthroughs
        return fallthroughs_map
Пример #2
0
 def _Call(self, parsed_args):
     arg_value = getattr(parsed_args, util.NamespaceFormat(self.arg_name),
                         None if self.plural else [])
     # Positional arguments will always be stored in argparse as lists, even if
     # nargs=1. If not supposed to be plural, transform into a single value.
     if not self.plural and isinstance(arg_value, list):
         return arg_value[0] if arg_value else None
     else:
         return arg_value
Пример #3
0
    def GetDest(self, parameter_name, prefix=None):
        """Returns the argument parser dest name for parameter_name with prefix.

    Args:
      parameter_name: The resource parameter name.
      prefix: The prefix name for parameter_name if not None.

    Returns:
      The argument parser dest name for parameter_name.
    """
        del prefix  # Unused.
        attribute_name = self._AttributeName(parameter_name)
        flag_name = self.resource_info.attribute_to_args_map.get(
            attribute_name, None)
        if not flag_name:
            return None
        return util.NamespaceFormat(flag_name)
Пример #4
0
    def BuildFullFallthroughsMap(self, parsed_args=None):
        """Builds map of all fallthroughs including arg names.

    Fallthroughs are a list of objects that, when called, try different ways of
    getting values for attributes (see googlecloudsdk.calliope.concepts.deps.
    _Fallthrough). This method builds a map from the name of each attribute to
    its fallthroughs, including the "primary" fallthrough representing its
    corresponding argument value in parsed_args if any, and any fallthroughs
    that were configured for the attribute beyond that.

    Args:
      parsed_args: the parsed namespace.

    Returns:
      {str: [deps_lib._Fallthrough]}, a map from attribute name to its
      fallthroughs.
    """
        fallthroughs_map = {}
        for attribute in self.concept_spec.attributes:
            attribute_name = attribute.name
            attribute_fallthroughs = []

            # Start the fallthroughs list with the primary associated arg for the
            # attribute.
            arg_name = self.attribute_to_args_map.get(attribute_name)
            if arg_name:
                arg_value = getattr(parsed_args,
                                    util.NamespaceFormat(arg_name), None)
                # The only args that should be lists are anchor args for plural
                # resources.
                plural = (attribute_name == self.concept_spec.anchor.name
                          and self.plural)
                if isinstance(arg_value, list) and not plural:
                    arg_value = arg_value[0] if arg_value else None
                if plural and arg_value is None:
                    arg_value = []
                attribute_fallthroughs.append(
                    deps_lib.ArgFallthrough(arg_name, arg_value))

            attribute_fallthroughs += self.fallthroughs_map.get(
                attribute_name, [])
            fallthroughs_map[attribute_name] = attribute_fallthroughs
        return fallthroughs_map
Пример #5
0
    def _BuildFinalFallthroughsMap(self, parsed_args=None):
        """Helper method to build all fallthroughs including arg names."""
        final_fallthroughs_map = {}
        for attribute in self.concept_spec.attributes:
            attribute_name = attribute.name
            attribute_fallthroughs = []

            # Start the fallthroughs list with the primary associated arg for the
            # attribute.
            arg_name = self.attribute_to_args_map.get(attribute_name)
            if arg_name:
                arg_value = getattr(parsed_args,
                                    util.NamespaceFormat(arg_name), None)
                # Required positionals end up stored as lists of strings.
                if isinstance(arg_value, list) and attribute.value_type == str:
                    arg_value = arg_value[0] if arg_value else None
                attribute_fallthroughs.append(
                    deps_lib.ArgFallthrough(arg_name, arg_value))

            attribute_fallthroughs += self.fallthroughs_map.get(
                attribute_name, [])
            final_fallthroughs_map[attribute_name] = attribute_fallthroughs
        return final_fallthroughs_map
Пример #6
0
def ValidateResourceIsCompleteIfSpecified(args, resource_arg_name):
    """Raises a ParseError if the given resource_arg_name is partially specified."""
    if not hasattr(args.CONCEPTS, resource_arg_name):
        return

    concept_info = args.CONCEPTS.ArgNameToConceptInfo(resource_arg_name)
    associated_args = [
        util.NamespaceFormat(arg)
        for arg in concept_info.attribute_to_args_map.values()
    ]

    # If none of the relevant args are specified, we're good.
    if not [arg for arg in associated_args if args.IsSpecified(arg)]:
        return

    try:
        # Re-parse this concept, but treat it as required even if it originally
        # wasn't. This will trigger a meaningful user error if it's underspecified.
        concept_info.ClearCache()
        concept_info.allow_empty = False
        concept_info.Parse(args)
    except concepts.InitializationError as e:
        raise handlers.ParseError(resource_arg_name, six.text_type(e))
 def _Call(self, parsed_args):
     prefix = getattr(parsed_args,
                      concepts_util.NamespaceFormat(self.arg_name), None)
     index = getattr(parsed_args, 'index', None)
     return '{}-{}'.format(prefix, index)
Пример #8
0
 def _Call(self, parsed_args):
     arg_value = getattr(parsed_args, util.NamespaceFormat(self.arg_name),
                         None)
     return arg_value