Ejemplo n.º 1
0
 def Run(self, args):
     """This is what gets called when the user runs this command."""
     if (not args.IsSpecified('location')) and (
             not properties.VALUES.notebooks.location.IsExplicitlySet()):
         raise parser_errors.RequiredError(argument='--location')
     instance_service = util.GetClient().projects_locations_instances
     return list_pager.YieldFromList(
         instance_service,
         instance_util.CreateInstanceListRequest(args),
         field='instances',
         limit=args.limit,
         batch_size_attribute='pageSize')
Ejemplo n.º 2
0
    def Run(self, args):
        """This is what gets called when the user runs this command."""
        release_track = self.ReleaseTrack()
        client = util.GetClient(release_track)
        messages = util.GetMessages(release_track)
        if (not args.IsSpecified('location')) and (
                not properties.VALUES.notebooks.location.IsExplicitlySet()):
            raise parser_errors.RequiredError(argument='--location')

        environment_service = client.projects_locations_environments
        return list_pager.YieldFromList(environment_service,
                                        env_util.CreateEnvironmentListRequest(
                                            args, messages),
                                        field='environments',
                                        limit=args.limit,
                                        batch_size_attribute='pageSize')
Ejemplo n.º 3
0
    def _RunCreate(self, args):
        messages = secrets_api.GetMessages()
        secret_ref = args.CONCEPTS.secret.Parse()
        data = secrets_util.ReadFileOrStdin(args.data_file)

        locations = args.locations
        if not locations:
            raise parser_errors.RequiredError(argument='--locations')

        labels = labels_util.Diff.FromUpdateArgs(args).Apply(
            messages.Secret.LabelsValue).GetOrNone()

        secret = secrets_api.Secrets().Create(secret_ref=secret_ref,
                                              labels=labels,
                                              locations=locations)
        secrets_log.Secrets().Created(secret_ref)

        if not data:
            return secret

        version = secrets_api.Secrets().SetData(secret_ref, data)
        version_ref = secrets_args.ParseVersionRef(version.name)
        secrets_log.Versions().Created(version_ref)
        return version
Ejemplo n.º 4
0
 def _RunArgumenterrorOutsideArgparse(self, args):
     raise parser_errors.RequiredError(argument='--some-flag')
  def validate_specified_args(self, ai, specified_args, top=True):
    """Validate specified args against the arg group constraints.

    Each group may be mutually exclusive and/or required. Each argument may be
    required.

    Args:
      ai: ArgumentInterceptor, The argument interceptor containing the
        ai.arguments argument group.
      specified_args: set, The dests of the specified args.
      top: bool, True if ai.arguments is the top level group.

    Raises:
      ModalGroupError: If modal arg not specified.
      OptionalMutexError: On optional mutex group conflict.
      RequiredError: If required arg not specified.
      RequiredMutexError: On required mutex group conflict.

    Returns:
      True if the subgroup was specified.
    """
    also_optional = []  # The optional args in group that were not specified.
    have_optional = []  # The specified optional (not required) args.
    have_required = []  # The specified required args.
    need_required = []  # The required args in group that must be specified.
    for arg in sorted(ai.arguments, key=usage_text.GetArgSortKey):
      if arg.is_group:
        arg_was_specified = self.validate_specified_args(arg, specified_args,
                                                         top=False)
      else:
        arg_was_specified = arg.dest in specified_args
      if arg_was_specified:
        if arg.is_required:
          have_required.append(arg)
        else:
          have_optional.append(arg)
      elif arg.is_required:
        if not isinstance(arg, DynamicPositionalAction):
          need_required.append(arg)
      else:
        also_optional.append(arg)

    if need_required:
      if top or have_required and not (have_optional or also_optional):
        ai = parser_arguments.ArgumentInterceptor(self, arguments=need_required)
        self._Error(parser_errors.RequiredError(
            parser=self,
            argument=usage_text.GetArgUsage(
                ai, value=False, hidden=True, top=top)))
      if have_optional or have_required:
        have_ai = parser_arguments.ArgumentInterceptor(
            self, arguments=have_optional + have_required)
        need_ai = parser_arguments.ArgumentInterceptor(
            self, arguments=need_required)
        self._Error(parser_errors.ModalGroupError(
            parser=self,
            argument=usage_text.GetArgUsage(
                have_ai, value=False, hidden=True, top=top),
            conflict=usage_text.GetArgUsage(
                need_ai, value=False, hidden=True, top=top)))

    # Multiple args with the same dest are counted as 1 arg.
    count = (len(self.GetDestinations(have_required)) +
             len(self.GetDestinations(have_optional)))

    if ai.is_mutex:
      conflict = usage_text.GetArgUsage(ai, value=False, hidden=True, top=top)
      if ai.is_required:
        if count != 1:
          if count:
            argument = usage_text.GetArgUsage(
                sorted(have_required + have_optional,
                       key=usage_text.GetArgSortKey)[0],
                value=False, hidden=True, top=top)
          else:
            argument = None
          self._Error(parser_errors.RequiredMutexError(
              parser=self, argument=argument, conflict=conflict))
      elif count > 1:
        argument = usage_text.GetArgUsage(
            sorted(have_required + have_optional,
                   key=usage_text.GetArgSortKey)[0],
            value=False, hidden=True, top=top)
        self._Error(parser_errors.OptionalMutexError(
            parser=self, argument=argument, conflict=conflict))

    return bool(count)
 def _Func():
   flag = flag_name[2:] if flag_name.startswith('--') else flag_name
   flag_value = getattr(self, flag)
   if flag_value is None and not self.IsSpecified(flag):
     raise parser_errors.RequiredError(argument=flag_name)
   return flag_value
Ejemplo n.º 7
0
 def GetValueFunc():
   val = args[key] if key in args else None
   if val:
     return val
   raise parser_errors.RequiredError(argument=key)