1# -*- coding: utf-8 -*- # 2# Copyright 2013 Google LLC. All Rights Reserved. 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16"""Argcomplete completers for various config related things.""" 17 18from __future__ import absolute_import 19from __future__ import division 20from __future__ import unicode_literals 21 22from googlecloudsdk.command_lib.util import completers 23from googlecloudsdk.core import module_util 24from googlecloudsdk.core import properties 25from googlecloudsdk.core.configurations import named_configs 26 27 28def PropertiesCompleter(prefix, **unused_kwargs): 29 """An argcomplete completer for property and section names.""" 30 all_sections = properties.VALUES.AllSections() 31 options = [] 32 33 if '/' in prefix: 34 # Section has been specified, only return properties under that section. 35 parts = prefix.split('/', 1) 36 section = parts[0] 37 prefix = parts[1] 38 if section in all_sections: 39 section_str = section + '/' 40 props = properties.VALUES.Section(section).AllProperties() 41 options.extend([section_str + p for p in props if p.startswith(prefix)]) 42 else: 43 # No section. Return matching sections and properties in the default 44 # group. 45 options.extend([s + '/' for s in all_sections if s.startswith(prefix)]) 46 section = properties.VALUES.default_section.name 47 props = properties.VALUES.Section(section).AllProperties() 48 options.extend([p for p in props if p.startswith(prefix)]) 49 50 return options 51 52 53def NamedConfigCompleter(prefix, **unused_kwargs): 54 """An argcomplete completer for existing named configuration names.""" 55 configs = list(named_configs.ConfigurationStore.AllConfigs().keys()) 56 return [c for c in configs if c.startswith(prefix)] 57 58 59class PropertyValueCompleter(completers.Converter): 60 """A completer for a specific property value. 61 62 The property value to be completed is not known until completion time. 63 """ 64 65 def Complete(self, prefix, parameter_info): 66 properties.VALUES.core.print_completion_tracebacks.Set(True) 67 prop_name = parameter_info.GetValue('property') 68 if not prop_name: 69 # No property specified. This should have been caught by the caller. 70 return None 71 prop = properties.FromString(prop_name) 72 if not prop: 73 # Property is invalid. This should have been caught by the caller. 74 return None 75 76 if prop.choices: 77 # Fixed set of possible values - easy. 78 return [c for c in prop.choices if c.startswith(prefix)] 79 80 if prop.completer: 81 # prop.completer is the module path for the resource value completer. 82 completer_class = module_util.ImportModule(prop.completer) 83 completer = completer_class(cache=self.cache) 84 return completer.Complete(prefix, parameter_info) 85 86 # No completer for this property. 87 return None 88 89 def Update(self, parameter_info=None, aggregations=None): 90 """No completion cache for properties.""" 91 del parameter_info, aggregations 92