def ResourceInfo(self, args): """Returns the command resource ResourceInfo object. Handles the --async flag. Args: args: argparse.Namespace, An object that contains the values for the arguments specified in the ._Flags() and .Args() methods. Raises: ResourceRegistryAttributeError: If --async is set and the resource_registry info does not have an async_collection attribute. UnregisteredCollectionError: If the async_collection name is not in the resource registry. Returns: A resource object dispatched by display.Displayer(). """ collection = self.Collection() # pylint: disable=assignment-from-none if not collection: return None info = resource_registry.Get(collection) if not getattr(args, 'async', False): return info if not info.async_collection: raise resource_exceptions.ResourceRegistryAttributeError( 'Collection [{collection}] does not have an async_collection ' 'attribute.'.format(collection=collection)) info = resource_registry.Get(info.async_collection) # One more indirection allowed for commands that have a different operations # format for --async and operations list. if info.async_collection: info = resource_registry.Get(info.async_collection) return info
def GetListCommand(self, parameter_info): """Returns the list command argv given parameter_info.""" info = resource_registry.Get(self.collection) if info.cache_command: command = info.cache_command elif info.list_command: command = info.list_command elif self._deprecated_list_command_callback: command = ' '.join(self._deprecated_list_command_callback( parameter_info.parsed_args)) else: if self._deprecated_list_command: command = self._deprecated_list_command else: command = resource_property.ConvertToSnakeCase( self.collection).replace('_', '-') + ' list' command = command.replace('.', ' ') return command.split(' ') + ['--uri', '--quiet', '--format=disable']
def RemoteCompleter(parsed_args, **unused_kwargs): """Runs list command on resource to generate completion data.""" list_command_updates_cache = False info = resource_registry.Get(resource) if info.cache_command: command = info.cache_command.split(' ') list_command_updates_cache = True elif info.list_command: command = info.list_command.split(' ') elif list_command_callback_fn: command = list_command_callback_fn(parsed_args) elif command_line: command = command_line.replace('.', ' ').split(' ') else: command = _LowerCaseWithDashes(resource).split('.') + [ 'list', '--uri' ] if info.bypass_cache: # Don't cache - use the cache_command results directly. return RemoteCompletion.RunListCommand(cli, command, parse_output=True) options = [] try: line = os.getenv('COMP_LINE') prefix = '' if line: for i in range(len(line) - 1, -1, -1): c = line[i] if c == ' ' or c == '\t': break prefix = c + prefix parms = {} if command[0] in _OPTIONAL_PARMS: for arg in _OPTIONAL_PARMS[command[0]]: for attrib in dict(arg): if hasattr(parsed_args, attrib): fun = arg[attrib] value = fun(parsed_args) if value: parms[attrib] = value command.append('--' + attrib) command.append(value) resource_link = _GetResourceLink(parms, resource) ccache = RemoteCompletion() options = ccache.GetFromCache(resource_link, prefix) if options is not None: return options items = RemoteCompletion.RunListCommand( cli, command, list_command_updates_cache=list_command_updates_cache) if list_command_updates_cache: options = ccache.GetFromCache(resource_link, prefix) or [] if options: RemoteCompletion.CACHE_HITS -= 1 return options # This part can be dropped when all commands are subclassed. options = [] self_links = [] for selflink in items: self_links.append(selflink) _, name = selflink.rsplit('/', 1) if not prefix or name.startswith(prefix): options.append(name) if self_links: ccache.StoreInCache(self_links) if resource_link.count( '*') > 0: # There are unknown parts. options = ccache.GetFromCache( resource_link, prefix, increment_counters=False) or [] except Exception: # pylint:disable=broad-except log.error('completion command [%s] failed', ' '.join(command), exc_info=True) return [] return options
def RemoteCompleter(parsed_args, **unused_kwargs): """Runs list command on resource to generate completion data.""" resource = ro_resource command_line = ro_command_line list_command_callback_fn = ro_list_command_callback_fn list_command_updates_cache = False info = resource_registry.Get(resource) if info.cache_command: command = info.cache_command.split(' ') elif list_command_callback_fn: command = list_command_callback_fn(parsed_args) else: command = command_line.split('.') + ['list'] if info.bypass_cache: # Don't cache - use the cache_command results directly. return RemoteCompletion.RunListCommand( cli, command, parse_output=True) list_command_updates_cache = True options = [] try: if hasattr(parsed_args, 'property'): if parsed_args.property == 'compute/region': resource = 'compute.regions' command = resource.split('.') + ['list'] elif parsed_args.property == 'compute/zone': resource = 'compute.zones' command = resource.split('.') + ['list'] elif parsed_args.property != 'project': return options line = os.getenv('COMP_LINE') prefix = '' if line: for i in range(len(line)-1, -1, -1): c = line[i] if c == ' ' or c == '\t': break prefix = c + prefix project = properties.VALUES.core.project.Get(required=True) parms = {} if command[0] in _OPTIONAL_PARMS: for arg in _OPTIONAL_PARMS[command[0]]: for attrib in dict(arg): if hasattr(parsed_args, attrib): fun = arg[attrib] value = fun(parsed_args) if value: parms[attrib] = value command.append('--' + attrib) command.append(value) parms['project'] = project resource_link = resources.Parse('+', parms, resource, resolve=False) resource_link = resource_link.WeakSelfLink() lst = resource_link.split('*') resource_missing = len(lst) > 1 ccache = RemoteCompletion() options = ccache.GetFromCache(resource_link, prefix) if options is not None: return options items = RemoteCompletion.RunListCommand( cli, command, list_command_updates_cache=list_command_updates_cache) if list_command_updates_cache: options = ccache.GetFromCache(resource_link, prefix) or [] if options: RemoteCompletion.CACHE_HITS -= 1 return options # This part can be dropped when all commands are subclassed. options = [] self_links = [] for item in items: # Get a selflink for the item if command[0] == 'compute': if 'selfLink' in item: instance_ref = resources.Parse(item['selfLink']) selflink = instance_ref.SelfLink() elif resource_link: selflink = resource_link.rstrip('+') + item['name'] else: instance_ref = resources.Create(resource, project=item.project, instance=item.instance) selflink = instance_ref.SelfLink() self_links.append(selflink) lst = selflink.split('/') name = lst[-1] if not prefix or name.startswith(prefix): options.append(name) if self_links: ccache.StoreInCache(self_links) if resource_missing: options = ccache.GetFromCache(resource_link, prefix, increment_counters=False) or [] except Exception: # pylint:disable=broad-except log.error(resource + 'completion command failed', exc_info=True) return [] return options
def ResourceInfo(self, args): """Returns the command resource ResourceInfo object.""" collection = self.Collection(args) return resource_registry.Get(collection) if collection else None