Skip to content

Commit

Permalink
Run ruff format
Browse files Browse the repository at this point in the history
  • Loading branch information
kdaily committed Dec 20, 2024
1 parent 1e6a6b5 commit 7be8454
Show file tree
Hide file tree
Showing 63 changed files with 2,023 additions and 1,201 deletions.
15 changes: 9 additions & 6 deletions awscli/autocomplete/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,24 @@ class LazyClientCreator(object):
a client. This class manages this process.
"""
def __init__(self,
import_name='awscli.clidriver.create_clidriver'):

def __init__(self, import_name='awscli.clidriver.create_clidriver'):
self._import_name = import_name
self._session_cache = {}

def create_client(self, service_name, parsed_region=None,
parsed_profile=None, **kwargs):
def create_client(
self, service_name, parsed_region=None, parsed_profile=None, **kwargs
):
if self._session_cache.get(parsed_profile) is None:
session = self.create_session()
session.set_config_variable('profile', parsed_profile)
self._session_cache[parsed_profile] = session
self._session_cache[parsed_profile].set_config_variable(
'region', parsed_region)
'region', parsed_region
)
return self._session_cache[parsed_profile].create_client(
service_name, **kwargs)
service_name, **kwargs
)

def create_session(self):
return lazy_call(self._import_name).session
137 changes: 88 additions & 49 deletions awscli/autocomplete/autogen.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@
It can also be used to regen completion data as new heuristics are added.
"""

import logging
from collections import defaultdict, namedtuple
from difflib import SequenceMatcher

LOG = logging.getLogger(__name__)
Resource = namedtuple('Resource', ['resource_name', 'ident_name',
'input_parameters', 'operation', 'jp_expr'])
Resource = namedtuple(
'Resource',
[
'resource_name',
'ident_name',
'input_parameters',
'operation',
'jp_expr',
],
)


class ServerCompletionHeuristic(object):
Expand All @@ -25,8 +34,9 @@ def __init__(self, singularize=None):
singularize = BasicSingularize()
self._singularize = singularize

def generate_completion_descriptions(self, service_model,
prune_completions=True):
def generate_completion_descriptions(
self, service_model, prune_completions=True
):
"""
:param service_model: A botocore.model.ServiceModel.
Expand All @@ -47,10 +57,13 @@ def generate_completion_descriptions(self, service_model,
if op_name.lower().startswith(self._RESOURCE_VERB_PREFIX):
candidates.append(op_name)
all_resources = self._generate_resource_descriptions(
candidates, service_model)
candidates, service_model
)
all_operations = self._generate_operations(
self._filter_operation_names(service_model.operation_names),
all_resources, service_model)
all_resources,
service_model,
)
if prune_completions:
self._prune_resource_identifiers(all_resources, all_operations)
return {
Expand All @@ -60,14 +73,18 @@ def generate_completion_descriptions(self, service_model,
}

def _filter_operation_names(self, op_names):
return [name for name in op_names
if not name.lower().startswith(self._OPERATION_EXCLUDES)]
return [
name
for name in op_names
if not name.lower().startswith(self._OPERATION_EXCLUDES)
]

def _generate_resource_descriptions(self, candidates, service_model):
all_resources = {}
for op_name in candidates:
resources = self._resource_for_single_operation(
op_name, service_model)
op_name, service_model
)
if resources is not None:
for resource in resources:
self._inject_resource(all_resources, resource)
Expand Down Expand Up @@ -96,15 +113,17 @@ def _generate_operations(self, op_names, resources, service_model):
)
return op_map

def _add_completion_data_for_operation(self, op_map, op_name,
service_model, reverse_mapping):
def _add_completion_data_for_operation(
self, op_map, op_name, service_model, reverse_mapping
):
op_model = service_model.operation_model(op_name)
input_shape = op_model.input_shape
if not input_shape:
return
for member in input_shape.members:
member_name = self._find_matching_member_name(
member, reverse_mapping)
member, reverse_mapping
)
if member_name is None:
continue
resource_name = self._find_matching_op_name(
Expand All @@ -113,9 +132,11 @@ def _add_completion_data_for_operation(self, op_map, op_name,
op = op_map.setdefault(op_name, {})
param = op.setdefault(member, {})
param['completions'] = [
{'parameters': {},
'resourceName': resource_name,
'resourceIdentifier': member_name}
{
'parameters': {},
'resourceName': resource_name,
'resourceIdentifier': member_name,
}
]

def _find_matching_op_name(self, op_name, candidates):
Expand All @@ -138,9 +159,7 @@ def _find_matching_op_name(self, op_name, candidates):
matcher.set_seq1(candidate)
match_ratio = matcher.ratio()
matching_score.append((match_ratio, candidate))
return sorted(
matching_score, key=lambda x: x[0], reverse=True
)[0][1]
return sorted(matching_score, key=lambda x: x[0], reverse=True)[0][1]

def _find_matching_member_name(self, member, reverse_mapping):
# Try to find something in the reverse mapping that's close
Expand Down Expand Up @@ -173,11 +192,18 @@ def _resource_for_single_operation(self, op_name, service_model):
# conventions.
op_model = service_model.operation_model(op_name)
output = op_model.output_shape
list_members = [member for member, shape in output.members.items()
if shape.type_name == 'list']
list_members = [
member
for member, shape in output.members.items()
if shape.type_name == 'list'
]
if len(list_members) != 1:
LOG.debug("Operation does not have exactly one list member, "
"skipping: %s (%s)", op_name, list_members)
LOG.debug(
"Operation does not have exactly one list member, "
"skipping: %s (%s)",
op_name,
list_members,
)
return
resource_member_name = list_members[0]
list_member = output.members[resource_member_name].member
Expand All @@ -186,52 +212,64 @@ def _resource_for_single_operation(self, op_name, service_model):
required_members = op_model.input_shape.required_members
if list_member.type_name == 'structure':
return self._resource_from_structure(
op_name, resource_member_name, list_member, required_members)
op_name, resource_member_name, list_member, required_members
)
elif list_member.type_name == 'string':
return [self._resource_from_string(
op_name, resource_member_name, required_members,
)]
return [
self._resource_from_string(
op_name,
resource_member_name,
required_members,
)
]

def _resource_from_structure(self, op_name,
resource_member_name, list_member,
required_members):
def _resource_from_structure(
self, op_name, resource_member_name, list_member, required_members
):
op_with_prefix_removed = self._remove_verb_prefix(op_name)
singular_name = self._singularize.make_singular(
op_with_prefix_removed)
singular_name = self._singularize.make_singular(op_with_prefix_removed)
resources = []
for member_name in list_member.members:
jp_expr = (
'{resource_member_name}[].{member_name}').format(
resource_member_name=resource_member_name,
member_name=member_name)
r = Resource(singular_name, member_name, required_members,
op_name, jp_expr)
jp_expr = ('{resource_member_name}[].{member_name}').format(
resource_member_name=resource_member_name,
member_name=member_name,
)
r = Resource(
singular_name, member_name, required_members, op_name, jp_expr
)
resources.append(r)
return resources

def _resource_from_string(self, op_name, resource_member_name,
required_members):
def _resource_from_string(
self, op_name, resource_member_name, required_members
):
op_with_prefix_removed = self._remove_verb_prefix(op_name)
singular_name = self._singularize.make_singular(
op_with_prefix_removed)
singular_name = self._singularize.make_singular(op_with_prefix_removed)
singular_member_name = self._singularize.make_singular(
resource_member_name)
r = Resource(singular_name, singular_member_name, required_members,
op_name,
'{resource_member_name}[]'.format(
resource_member_name=resource_member_name))
resource_member_name
)
r = Resource(
singular_name,
singular_member_name,
required_members,
op_name,
'{resource_member_name}[]'.format(
resource_member_name=resource_member_name
),
)
return r

def _remove_verb_prefix(self, op_name):
for prefix in self._RESOURCE_VERB_PREFIX:
# 'ListResources' -> 'Resources'
if op_name.lower().startswith(prefix):
op_with_prefix_removed = op_name[len(prefix):]
op_with_prefix_removed = op_name[len(prefix) :]
return op_with_prefix_removed

def _prune_resource_identifiers(self, all_resources, all_operations):
used_identifiers = self._get_identifiers_referenced_by_operations(
all_operations)
all_operations
)
for resource, resource_data in list(all_resources.items()):
identifiers = resource_data['resourceIdentifier']
known_ids_for_resource = used_identifiers.get(resource, set())
Expand All @@ -248,7 +286,8 @@ def _get_identifiers_referenced_by_operations(self, operations):
used_identifiers = {}
for completion in self._all_completions(operations):
used_identifiers.setdefault(completion['resourceName'], set()).add(
completion['resourceIdentifier'])
completion['resourceIdentifier']
)
return used_identifiers

def _all_completions(self, operations):
Expand Down
34 changes: 24 additions & 10 deletions awscli/autocomplete/completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class AutoCompleter(object):
completions for specific cases (e.g model-based completions,
server-side completions, etc).
"""

def __init__(self, parser, completers):
"""
Expand Down Expand Up @@ -54,8 +55,16 @@ class CompletionResult(object):
stores metadata about the completion.
"""
def __init__(self, name, starting_index=0, required=False,
cli_type_name='', help_text='', display_text=None):

def __init__(
self,
name,
starting_index=0,
required=False,
cli_type_name='',
help_text='',
display_text=None,
):
self.name = name
self.starting_index = starting_index
self.required = required
Expand All @@ -65,17 +74,22 @@ def __init__(self, name, starting_index=0, required=False,

def __eq__(self, other):
return (
isinstance(other, self.__class__) and
self.name == other.name and
self.starting_index == other.starting_index and
self.display_text == other.display_text
isinstance(other, self.__class__)
and self.name == other.name
and self.starting_index == other.starting_index
and self.display_text == other.display_text
)

def __repr__(self):
return '%s(%s, %s, %s, %s, %s, %s)' % (self.__class__.__name__, self.name,
self.starting_index, self.required,
self.cli_type_name, self.help_text,
self.display_text)
return '%s(%s, %s, %s, %s, %s, %s)' % (
self.__class__.__name__,
self.name,
self.starting_index,
self.required,
self.cli_type_name,
self.help_text,
self.display_text,
)


class BaseCompleter(object):
Expand Down
10 changes: 6 additions & 4 deletions awscli/autocomplete/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
from awscli.autocomplete.serverside.custom_completers.ddb.autocomplete import \
add_ddb_completers
from awscli.autocomplete.serverside.custom_completers.logs.autocomplete import \
add_log_completers
from awscli.autocomplete.serverside.custom_completers.ddb.autocomplete import (
add_ddb_completers,
)
from awscli.autocomplete.serverside.custom_completers.logs.autocomplete import (
add_log_completers,
)


def get_custom_completers():
Expand Down
8 changes: 3 additions & 5 deletions awscli/autocomplete/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
INDEX_FILE = os.path.join(INDEX_DIR, '%s.index' % cli_version)
BUILTIN_INDEX_FILE = os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
'data', 'ac.index'
'data',
'ac.index',
)


Expand All @@ -33,10 +34,7 @@ def __init__(self, db_filename=None):
@property
def _connection(self):
if self._db_conn is None:
kwargs = {
'check_same_thread': False,
'isolation_level': None
}
kwargs = {'check_same_thread': False, 'isolation_level': None}
if self._db_filename.startswith('file::memory:'):
# This statement was added because old versions of sqlite
# don't support 'uri' but we use it for tests and because
Expand Down
Loading

0 comments on commit 7be8454

Please sign in to comment.