def _ParseUncached(self, parsed_args=None): """Lazy parsing function for resource. Args: parsed_args: the parsed Namespace. Returns: the initialized resource or a list of initialized resources if the resource argument was pluralized. """ fallthroughs_map = self.BuildFullFallthroughsMap() if not self.plural: try: return self.concept_spec.Initialize( deps_lib.Deps(fallthroughs_map, parsed_args=parsed_args)) except concepts.InitializationError: if self.allow_empty: return None raise anchor = self.concept_spec.anchor.name anchor_fallthroughs = fallthroughs_map.get(anchor, []) # Iterate through the values provided to the anchor argument, creating for # each a separate parsed resource. resources = [] for i, anchor_fallthrough in enumerate(anchor_fallthroughs): try: anchor_values = anchor_fallthrough.GetValue(parsed_args) except deps_lib.FallthroughNotFoundError: continue for arg_value in anchor_values: def F(return_value=arg_value): return return_value fallthrough = deps_lib.Fallthrough( F, anchor_fallthrough.hint, active=anchor_fallthrough.active) fallthroughs_map[anchor] = (anchor_fallthroughs[:i] + [fallthrough] + anchor_fallthroughs[i:]) resources.append( self.concept_spec.Initialize( deps_lib.Deps(fallthroughs_map, parsed_args=parsed_args))) return resources if self.allow_empty: return resources return self.concept_spec.Initialize( deps_lib.Deps(fallthroughs_map, parsed_args=parsed_args))
def GetValue(self, parameter_name, check_properties=True): # type: (...) -> typing.Optional[str] """Returns the program state value for parameter_name. Args: parameter_name: The parameter name. check_properties: bool, whether to check the properties (unused). Returns: The program state value for parameter_name. """ del check_properties # Unused. deps_obj = deps.Deps(self.resource_info.BuildFullFallthroughsMap(), parsed_args=self.parsed_args) attribute_name = ( self.resource_info.resource_spec.AttributeName(parameter_name)) current = properties.VALUES.core.disable_prompts.GetBool() # TODO(b/73073941): Come up with a better way to temporarily disable # prompts. This prevents arbitrary fallthroughs with prompting from # being run during completion. properties.VALUES.core.disable_prompts.Set(True) try: return deps_obj.Get(attribute_name) if attribute_name else None except deps.AttributeNotFoundError: return None finally: properties.VALUES.core.disable_prompts.Set(current)
def GetDeps(self): """Builds the deps.Deps object to get attribute values. Gets a set of fallthroughs for each attribute of the handler's concept spec, including any argument values that were registered through RegisterArg. Then initializes the deps object. Returns: (deps_lib.Deps) the deps object representing all data dependencies. """ 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_info = self.arg_info_map.get(attribute_name, None) attribute_fallthroughs.append( deps_lib.ArgFallthrough(arg_name, arg_info)) attribute_fallthroughs += self.fallthroughs_map.get(attribute_name, []) final_fallthroughs_map[attribute_name] = attribute_fallthroughs return deps_lib.Deps(final_fallthroughs_map)
def Parse(self, parsed_args=None): """Lazy parsing function for resource. Args: parsed_args: the parsed Namespace. Returns: the initialized resource or a list of initialized resources if the resource argument was pluralized. """ fallthroughs_map = self._BuildFullFallthroughsMap(parsed_args) if not self.plural: return self.concept_spec.Initialize( deps_lib.Deps(fallthroughs_map)) anchor = self.concept_spec.anchor.name anchor_arg_name = self.attribute_to_args_map.get(anchor) anchor_fallthroughs = fallthroughs_map.get(anchor, []) anchor_arg_values = None # Remove the original ArgFallthrough that contains all the anchor values. if anchor_arg_name and anchor_fallthroughs: anchor_arg_fallthrough = anchor_fallthroughs.pop(0) if (isinstance(anchor_arg_fallthrough, deps_lib.ArgFallthrough) and anchor_arg_fallthrough.arg_name == anchor_arg_name): anchor_arg_values = anchor_arg_fallthrough.arg_value # Resources only become plural if multiple arg values are provided. If no # argument values were found for whatever reason, no need to split up into # multiple resources. if anchor_arg_values is None: return [ self.concept_spec.Initialize(deps_lib.Deps(fallthroughs_map)) ] # Iterate through the values provided to the anchor argument, creating for # each a separate parsed resource. resources = [] for arg_value in anchor_arg_values: fallthrough = deps_lib.ArgFallthrough(anchor_arg_name, arg_value) fallthroughs_map[anchor] = [fallthrough] + anchor_fallthroughs resources.append( self.concept_spec.Initialize(deps_lib.Deps(fallthroughs_map))) return resources
def GetDeps(self): """Builds the deps.Deps object to get attribute values. Gets a set of fallthroughs for each attribute of the handler's concept spec, including any argument values that were registered through RegisterArg. Then initializes the deps object. Returns: (deps_lib.Deps) the deps object representing all data dependencies. """ final_fallthroughs_map = self._BuildFinalFallthroughsMap() return deps_lib.Deps(final_fallthroughs_map)
def GetDeps(self, parsed_args=None): """Builds the deps.Deps object to get attribute values. Gets a set of fallthroughs for each attribute of the handler's concept spec, including any argument values that were registered through RegisterArg. Then initializes the deps object. Args: parsed_args: (calliope.parser_extensions.Namespace) the parsed arguments from command line. Returns: (deps_lib.Deps) the deps object representing all data dependencies. """ final_fallthroughs_map = self._BuildFinalFallthroughsMap( parsed_args=parsed_args) return deps_lib.Deps(final_fallthroughs_map)